/*************************************************************************** Tools.c - Common tools Debug Debug_Object (object) Sprites Make a sprite as object from GIF file of same name Must be initialized at startup Sprite_Make (name) Move the sprite relative to field Sprite_Move (field, name, x, y) Time This generic time function updates global values time_stamp. Also returns milliseconds counter. Time_Stamp (); This generic time function updates global values time_milliseconds and time_stamp. Also returns delta value from last call. Time_Delta (); undefined == does not work on w2k/ie5 use "undefined" == typeof ***************************************************************************/ /*************************************************************************** Global variables ***************************************************************************/ var time_milliseconds = 0; var time_stamp; var debug_message = []; /*************************************************************************** Startup initialization ***************************************************************************/ // alert (Debug ("ScriptEngine", ScriptEngine())); // alert (Debug ("ScriptEngineMajorVersion", ScriptEngineMajorVersion())); // alert (Debug ("ScriptEngineMinorVersion", ScriptEngineMinorVersion())); if (("function" != typeof ScriptEngine) || ("JScript" != ScriptEngine()) || (5 > ScriptEngineMajorVersion()) || ((5 == ScriptEngineMajorVersion()) && (5 > ScriptEngineMinorVersion()))) alert ("This JScript has only been tested with IE 6"); document.write ("

"); /*************************************************************************** Functions ***************************************************************************/ /*************************************************************************** object object debug string. ***************************************************************************/ function Debug_Message (object) { var string; var i; if ("string" != (typeof object)) alert ("First argument is expected to be a string"); Debug_Message.innerText = 'Debug_Object ' + object; for (var i in eval(object)) { Debug_message.innerText += '\n' + object + "." + i + " = " + eval (object)[i]; } } /*************************************************************************** Generic object debug string ***************************************************************************/ function Debug (name, object) { var str; var string; if ("string" != (typeof name)) return "function Debug (name, object): " + "First argument is expected to be a string description."; str = typeof object; string = name + " is a " + str; if ("string" == str) string += "\n" + Debug_String (name, object); else if ("object" == str) string += "\n" + Debug_Object (name, object); else if ("function" == str) ; else if ("number" == str) string += "\nValue = " + object; else string += "\nUnrecognized typeof = " + str; return string; } /*************************************************************************** string object debug string. ***************************************************************************/ function Debug_String (object, value) { var string; if ("string" != (typeof object)) return "First argument is expected to be a string"; if ("string" != (typeof value)) return "Second argument is expected to be a string"; string = "String length = " + value.length + "\n"; string += "\"" + escape (value) + "\"\n"; return string; } /*************************************************************************** object object debug string. ***************************************************************************/ function Debug_Object (object, value) { var string; var i; if ("string" != (typeof object)) return "First argument is expected to be a string"; string = "Object length = " + value.length + "\n"; for (i in value) string += Debug (object + "." + i, value[i]); return string; } /*************************************************************************** Make a sprite as object from GIF file of same name Must be initialized at startup ***************************************************************************/ function Sprite_Make (name) { document.write('

'); } /****************************************************************************** Move the sprite relative to field ******************************************************************************/ function Sprite_Move (field, name, x, y) { name.style.pixelLeft = x + field.offsetLeft; name.style.pixelTop = y + field.offsetTop; } /*************************************************************************** Update time_stamp, return milliseconds ***************************************************************************/ function Time_Stamp () { time_stamp = new Date(); return time_stamp.getTime (); } /*************************************************************************** Time formatted to international standard string ***************************************************************************/ function Time_ISO (time) { var date; date = new Date(time); return date.getFullYear () + "-" + Leading_Zero (1 + date.getMonth ()) + "-" + Leading_Zero (date.getDate ()) + " " + Leading_Zero (date.getHours ()) + ":" + Leading_Zero (date.getMinutes ()) + ":" + Leading_Zero (date.getSeconds ()); } /*************************************************************************** Return two digit positive number with leading zero fill. ***************************************************************************/ function Leading_Zero (value) { return ("0" + value).slice (-2); } /*************************************************************************** Update time_stamp, time_milliseconds and return delta ***************************************************************************/ function Time_Delta () { var delta; delta = time_milliseconds; time_milliseconds = Time_Stamp (); if (0 == delta) return delta; delta = time_milliseconds - delta; // Average (debug_message, delta); // Debug_Message.innerText = Average (debug_message); return delta; } /*************************************************************************** Cookie maker full_list = Cookie (); crumb = Cookie ("entry to get"); Cookie (""); Cookie ("entry to delete", ""); Cookie ("entry to save", "value"); ***************************************************************************/ function Cookie (name, value) { var str; var string; var i; // Is this a full get of all cookies? if ("undefined" == typeof name) { str = document.cookie; str = str.split (";"); string = ""; for (i = 0; i < str.length; ++i) // Why did split put a space at the beginning of all // but the first string? string += str[i].substr (i > 0) + "\n"; return unescape (string); } // Is this a full delete of all cookies? if ("" == name.toString ()) { str = document.cookie; str = str.split (";"); for (i = 0; i < str.length; ++i) document.cookie = str[i] + "; expires=Fri, 31 Dec 1999"; return; } // Is this a crumb get? if ("undefined" == typeof value) { string = document.cookie; name = escape (name); str = string.split (";"); string = ""; for (i = 0; i < str.length; ++i) { // Why did split put a space at the beginning of all // but the first string? if (name == str[i].substr (i > 0, name.length)) { string = unescape (str[i].substr (name.length + 1 + (i > 0))); break; } } return string; } // Is this a crumb delete? if ("" == value.toString ()) { document.cookie = escape (name) + "=; expires=Fri, 31 Dec 1999"; return; } // Write cookie crumb document.cookie = escape (name) + "=" + escape (value) + "; expires=Fri, 31 Dec 2100"; } /*************************************************************************** Average values Keep track of lowest, next lowest, average, next highest, highest ***************************************************************************/ function Average (save, value) { var string; // Is this the initial value? if ("undefined" == typeof save[0]) { save[0] = value; save[1] = value; save[2] = value; save[3] = value; save[4] = value; return; } // Report format results request if (null == value) { string = "Lowest " + save[0]; string += " " + save[1]; string += " Average " + save[2]; string += " Highest " + save[3]; string += " " + save[4]; return string; } if (value < save[0]) { save[1] = save[0]; save[0] = value; } else if (value > save[0]) { if (save[0] == save[1]) save[1] = value; else if (value < save[1]) save[1] = value; } save[2] = (save[2] * 3 + value + 3) >> 2; if (value > save[4]) { save[3] = save[4]; save[4] = value; } else if (value < save[4]) { if (save[4] == save[3]) save[3] = value; else if (value > save[3]) save[3] = value; } } /*************************************************************************** ***************************************************************************/ function Scores_Update () { var str = []; var string; var i; // Make the array of previous high scores string = Cookie ("Scores"); // Array of top ten scores. Last entry may be blank. str = string.split ("\n", 10); // Format the number right align string = (" " + score_high).slice (-8); // replace the score (if any) at the end of the array str[str.length - 1] = string + " " + Time_ISO (Time_Stamp ()) + " " + Form.name.value; // Sort the high scores str.sort (Scores_Sort); // Combine and save string = ""; for (i = 0; i < str.length ; ++i) string += str[i] + "\n"; Cookie ("Scores", string); Cookie ("Current_Name", Form.name.value); Scores_List (); } /*************************************************************************** ***************************************************************************/ function Scores_Sort (s1, s2) { var v1, v2, re; v1 = parseInt (s1); v2 = parseInt (s2); if (v1 != v2) return v2 - v1; re = /[0-9]+-/; v1 = s1.slice (s1.search (re)); v2 = s2.slice (s2.search (re)); return v2.localeCompare (v1); } /*************************************************************************** ***************************************************************************/ function Scores_List () { Form.name.value = Cookie ("Current_Name"); if ("" == Form.name.value) alert ("Enter your name!"); Score_List.innerText = Cookie ("Scores"); }