home *** CD-ROM | disk | FTP | other *** search
/ JavaScript CD Cookbook / JavaScript CD Cookbook.iso / mac / MACFILES / COOKIES / COMMON / CODE.CMN next >
Encoding:
Text File  |  1996-05-25  |  8.5 KB  |  347 lines  |  [TEXT/LMAN]

  1. /*
  2.     THE JAVASCRIPT COOKBOOK by Erica Sadun, webrx@mindspring.com
  3.     Copyright (C)1996 by Charles River Media.  All Rights Reserved.
  4.     
  5.     This applet can only be re-used or modifed by license holders of the
  6.     JavaScript Cookbook CD-ROM.  Credit must be given in the source
  7.     code and this copyright notice must be maintained. If you do
  8.     not hold a license to the JavaScript Cookbook, you may NOT
  9.     duplicate or modify this code for your own use.
  10.  
  11.     Use at your own risk. No warranty is given or implied of the suitability 
  12.     of this applet for any specific application. Neither Erica Sadun nor 
  13.     Charles River Media will be held responsible for any unwanted effects 
  14.     due to the use of this applet or any derivative. 
  15.     
  16.     COMMON FUNCTIONS USED IN MANY SCRIPTS
  17. */
  18.  
  19.  
  20. //----------------------PATH UTILITIES--------------------------
  21.  
  22. // Get the grandparent directory and append aDir
  23. function getSibling(aDir)
  24. {
  25.     var loc = ""+document.location
  26.     
  27.     // get this dir, then parent
  28.     var base = loc.substring(0, loc.lastIndexOf("/"))
  29.     base = base.substring(0, base.lastIndexOf("/")+1)
  30.     return base+aDir+'/'
  31. }
  32.  
  33. // Get the current directory.
  34. function getDir()
  35. {
  36.     var loc = ""+document.location
  37.     return(loc.substring(0, loc.lastIndexOf("/"))+"/")
  38. }
  39.  
  40. // Get the current directory.
  41. function getParent()
  42. {
  43.     var loc = ""+document.location
  44.     loc = (loc.substring(0, loc.lastIndexOf("/"))+"/")
  45.     return(loc.substring(0, loc.lastIndexOf("/"))+"/")
  46. }
  47.  
  48. // Get the relative path.
  49. function getRel(aPath)
  50. {
  51.     var loc = ""+document.location
  52.     var path = ""+aPath
  53.     var base = loc.substring(0, loc.lastIndexOf("/"))
  54.     
  55.  
  56.     while ((path.length > 0) && (path.indexOf("../")) >= 0)
  57.     {    
  58.         base = base.substring(0, base.lastIndexOf("/"))
  59.         path = path.substring((path.indexOf("../")+3),path.length)
  60.     }
  61.  
  62.     return ""+base+"/"+path
  63. }
  64.  
  65. //------------------STRING-ARRAY UTILITIES-------------------
  66.  
  67. // Find the substring at index n, counting 0 to n
  68. function doIndex(aString, n)
  69. {
  70.     
  71.     var str=""+aString
  72.     
  73.     // Count until the correct index
  74.     for(var i = 0; i < n; i++)
  75.     {
  76.         var where = str.indexOf(':', 1)
  77.         str = str.substring(where+1, str.length)
  78.     }
  79.  
  80.     // Lop off the end of the string
  81.     return str.substring(0, str.indexOf(':'))
  82. }
  83.  
  84. // Find the substring at index n, counting 0 to n
  85. function indexNumber(aString, n)
  86. {
  87.     return parseInt(doIndex(aString, n))
  88. }
  89.  
  90.  
  91. //------------------CUSTOM GMT UTILITY-----------------------------
  92.  
  93. // Return the 3-letter symbol for month #n
  94. function month(n)
  95. {
  96.     var m = "Jan:Feb:Mar:Apr:May:Jun:Jul:Aug:Sep:Oct:Nov:Dec:"
  97.     return doIndex(m, n)
  98. }
  99.  
  100. // Return the 3-letter symbol for day #n
  101. function day(n)
  102. {
  103.     var d = "Sun:Mon:Tue:Wed:Thu:Fri:Sat:"
  104.     return doIndex(d, n)
  105. }
  106.  
  107. // A Custom "GMT" Function
  108. function myGMT(d)
  109. {
  110.     tmp = day(d.getDay())+', '+d.getDate()+' '+month(d.getMonth())
  111.     tmp += ' 19'+d.getYear()+' '
  112.  
  113.     if (d.getHours() < 10) tmp += '0'
  114.     tmp += d.getHours()+':'
  115.     
  116.     if (d.getMinutes()< 10) tmp += '0'
  117.     tmp += d.getMinutes()+':'
  118.     
  119.     if (d.getSeconds() < 10) tmp += '0'
  120.     tmp += d.getSeconds()+" GMT"
  121.     
  122.     return tmp
  123. }
  124.  
  125.  
  126. //------------------RANDOM NUMBERS----------------------------
  127. var js_mult1=3141
  128. var js_mult2=5821
  129. var js_m1=100000000
  130. var js_m2=10000
  131. var js_iseed=0
  132. var js_iseed1=0
  133. var js_iseed2=0
  134.  
  135.  
  136. // Return a Random Integer between 0 and N-1
  137. function random(n)
  138. {
  139.     if (js_iseed == 0)
  140.     {
  141.         now = new Date()
  142.         js_iseed = now.getHours() + now.getMinutes() * 60 
  143.                     + now.getSeconds() * 3600
  144.     }
  145.     js_iseed1 = js_iseed / js_m2
  146.     js_iseed2 = js_iseed % js_m2
  147.     var tmp = (((js_iseed2 * js_mult1 + js_iseed1 * js_mult2) % js_m2) * 
  148.                 js_m2 + (js_iseed2 * js_mult2)) % js_m1
  149.     js_iseed = (tmp + 1) % js_m1
  150.     return (Math.floor((js_iseed/js_m1) * n))
  151. }
  152.  
  153.  
  154. //------------------ARRAY CREATION----------------------------
  155.  
  156. // create an array
  157. function createArray(n)
  158. {
  159.     for (var i = 0; i < n; i++) {this[i] = 0}
  160.     return this
  161. }
  162.  
  163. // --------------------HEXADECIMAL CONVERSION---------------------
  164.  
  165. // convert a single digit (0 - 16) into hex
  166. function enHex(aDigit)
  167. {
  168.     return("0123456789ABCDEF".substring(aDigit, aDigit+1))
  169. }
  170.  
  171. // convert a hex digit into decimal
  172. function deHex(aDigit)
  173. {
  174.     return("0123456789ABCDEF".indexOf(aDigit))
  175. }
  176.  
  177. // Convert a 24bit number to hex
  178. function toHex(n)
  179. {
  180.     return (enHex((0xf00000 & n) >> 20) +
  181.             enHex((0x0f0000 & n) >> 16) +
  182.             enHex((0x00f000 & n) >> 12) +
  183.             enHex((0x000f00 & n) >>  8) +
  184.             enHex((0x0000f0 & n) >>  4) +
  185.             enHex((0x00000f & n) >>  0))
  186. }
  187.  
  188. // Convert a six character hex to decimal
  189. function toDecimal(hexNum)
  190. {
  191.     var tmp = ""+hexNum.toUpperCase()
  192.     while (tmp.length < 6) tmp = "0"+tmp
  193.     
  194.     return ((deHex(tmp.substring(0,1)) << 20) +
  195.             (deHex(tmp.substring(1,2)) << 16) + 
  196.             (deHex(tmp.substring(2,3)) << 12) +
  197.             (deHex(tmp.substring(3,4)) << 8) +
  198.             (deHex(tmp.substring(4,5)) << 4) +
  199.             (deHex(tmp.substring(5,6))))
  200. }
  201.  
  202. // --------------------JSCcolor OBJECT---------------------
  203.  
  204. // Returns a JavaScript integer representing a JSCcolor object's color
  205. function rawColor()
  206. {
  207.     return (this.red << 16) + (this.green << 8) + this.blue
  208. }
  209.  
  210. // Returns a hex string representing a JSCcolor object's color
  211. function hexColor()
  212. {
  213.     return toHex(this.rawColor())
  214. }
  215.  
  216. // Set the JSCcolor object's color to that in a hex string.
  217. // This routine does not add the Ox prefix.
  218. function setColor(aString)
  219. {
  220.     var tmp = toDecimal(aString)
  221.  
  222.     this.red = (0xff0000 & tmp) >> 16
  223.     this.green = (0xff00 & tmp) >> 8
  224.     this.blue = (0xff & tmp)
  225.     
  226.     return this
  227. }
  228.  
  229. // Set the JSCcolor object's color to an integer
  230. function setDecimalColor(aNumber)
  231. {
  232.     var tmp = toHex(aNumber)
  233.  
  234.     this.red = eval('0x'+tmp.substring(0,2))
  235.     this.green = eval('0x'+tmp.substring(2,4))
  236.     this.blue = eval('0x'+tmp.substring(4,8))
  237.     
  238.     return this
  239. }
  240.  
  241. // Adjust a JSCcolor object's color (red, green or blue) by an offset
  242. function adjustColor(aColor, anOffset)
  243. {
  244.     this[aColor] += anOffset
  245.     if (this[aColor] > 0xff) this[aColor] = 0xff
  246.     if (this[aColor] < 0x0) this[aColor] = 0x0
  247. }
  248.  
  249. // Adjust one or more of a JSCcolor object's colors by an offset
  250. function adjustColors(colString, anOffset)
  251. {
  252.     var cs = colString.toUpperCase()
  253.     
  254.     if (cs.indexOf('R') != -1) this.adjustColor('red', anOffset)
  255.     if (cs.indexOf('G') != -1) this.adjustColor('green', anOffset)
  256.     if (cs.indexOf('B') != -1) this.adjustColor('blue', anOffset)
  257. }
  258.  
  259. // JSCcolor object constructor
  260. function JSCcolor(r, g, b)
  261. {
  262.     // set properties
  263.     this.red = r
  264.     this.green = g
  265.     this.blue = b
  266.     
  267.     // set methods
  268.     this.rawColor = rawColor
  269.     this.hexColor = hexColor
  270.     this.adjustColor = adjustColor
  271.     this.adjustColors = adjustColors
  272.     this.setColor = setColor
  273.     this.setDecimalColor = setDecimalColor
  274.  
  275.     return this
  276. }
  277.  
  278. // --------------------POPUP WINDOW---------------------
  279.  
  280. function PopIt(label, msg)
  281. {
  282.   // Set up Page Colors & Table
  283.   var s1 = 
  284.     "<TITLE>Information!</TITLE>" +
  285.     "<BODY BGCOLOR='ffffff'><TABLE BORDER=0><TR>" +
  286.         "<TD WIDTH=90% HEIGHT=90 VALIGN=TOP ALIGN=LEFT>"+
  287.         "<FONT SIZE=4>"
  288.   
  289.   // Emphasize Label
  290.   var s2 = "<FONT COLOR='FF0000'><B>"+label+"</B></FONT><P>"
  291.  
  292.   // Create a Close Button and Finish Table
  293.   var s3 =   
  294.     "</TD><TD WIDTH=10%> </TD></TR><TR><TD> </TD>"+
  295.     "<TD VALIGN=TOP ALIGN=RIGHT>"+
  296.     "<FORM><INPUT TYPE='BUTTON' VALUE='Okay'" +
  297.                    "onClick='self.close()'>"  +
  298.     "</FORM></TD></TR></TABLE></BODY>"
  299.  
  300.   // Create the Window
  301.   popup = window.open("","popDialog","height=160,width=300,scrollbars=no")
  302.   popup.document.write(s1+s2+msg+s3)
  303.   popup.document.close()
  304. }
  305.  
  306. // --------------------MATH STRINGS---------------------
  307.  
  308. // Strip a floating point's digits to #.### -- you set the digits
  309. function stripDigits(aNumber, digits)
  310. {
  311.   var str = "" + aNumber
  312.   var b = str.lastIndexOf(".")
  313.   
  314.   // add decimal point if needed
  315.   if (b < 0) str += "." 
  316.   
  317.   // pad with extra zeros in case we have too "round" a number
  318.   for (var i = 0; i < digits; i++) str += "0"
  319.  
  320.   // extract existing decimal or just return
  321.   if (b >= 0) return(str).substring(0,b+1+digits)
  322.   return str
  323. }
  324.  
  325. // Allow for dollar signs in numeric input
  326. function stripDollar(aString)
  327. {
  328.     // An empty field means skip and return zero
  329.     if (aString=="") return 0
  330.     
  331.     // Check if a dollar sign is found
  332.     var dollar = aString.indexOf("$")
  333.     var myStr = ""+aString
  334.     
  335.     // If so, skip past it
  336.     if (dollar >= 0)
  337.     {
  338.         var len = aString.length
  339.         myStr = ""+aString.substring(dollar+1, len)
  340.     }
  341.  
  342.     // Evaluate the rest of the string as a floating point
  343.     return parseFloat(myStr)
  344. }
  345.  
  346.  
  347.