home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-05-25 | 8.5 KB | 347 lines | [TEXT/LMAN] |
- /*
- THE JAVASCRIPT COOKBOOK by Erica Sadun, webrx@mindspring.com
- Copyright (C)1996 by Charles River Media. All Rights Reserved.
-
- This applet can only be re-used or modifed by license holders of the
- JavaScript Cookbook CD-ROM. Credit must be given in the source
- code and this copyright notice must be maintained. If you do
- not hold a license to the JavaScript Cookbook, you may NOT
- duplicate or modify this code for your own use.
-
- Use at your own risk. No warranty is given or implied of the suitability
- of this applet for any specific application. Neither Erica Sadun nor
- Charles River Media will be held responsible for any unwanted effects
- due to the use of this applet or any derivative.
-
- COMMON FUNCTIONS USED IN MANY SCRIPTS
- */
-
-
- //----------------------PATH UTILITIES--------------------------
-
- // Get the grandparent directory and append aDir
- function getSibling(aDir)
- {
- var loc = ""+document.location
-
- // get this dir, then parent
- var base = loc.substring(0, loc.lastIndexOf("/"))
- base = base.substring(0, base.lastIndexOf("/")+1)
- return base+aDir+'/'
- }
-
- // Get the current directory.
- function getDir()
- {
- var loc = ""+document.location
- return(loc.substring(0, loc.lastIndexOf("/"))+"/")
- }
-
- // Get the current directory.
- function getParent()
- {
- var loc = ""+document.location
- loc = (loc.substring(0, loc.lastIndexOf("/"))+"/")
- return(loc.substring(0, loc.lastIndexOf("/"))+"/")
- }
-
- // Get the relative path.
- function getRel(aPath)
- {
- var loc = ""+document.location
- var path = ""+aPath
- var base = loc.substring(0, loc.lastIndexOf("/"))
-
-
- while ((path.length > 0) && (path.indexOf("../")) >= 0)
- {
- base = base.substring(0, base.lastIndexOf("/"))
- path = path.substring((path.indexOf("../")+3),path.length)
- }
-
- return ""+base+"/"+path
- }
-
- //------------------STRING-ARRAY UTILITIES-------------------
-
- // Find the substring at index n, counting 0 to n
- function doIndex(aString, n)
- {
-
- var str=""+aString
-
- // Count until the correct index
- for(var i = 0; i < n; i++)
- {
- var where = str.indexOf(':', 1)
- str = str.substring(where+1, str.length)
- }
-
- // Lop off the end of the string
- return str.substring(0, str.indexOf(':'))
- }
-
- // Find the substring at index n, counting 0 to n
- function indexNumber(aString, n)
- {
- return parseInt(doIndex(aString, n))
- }
-
-
- //------------------CUSTOM GMT UTILITY-----------------------------
-
- // Return the 3-letter symbol for month #n
- function month(n)
- {
- var m = "Jan:Feb:Mar:Apr:May:Jun:Jul:Aug:Sep:Oct:Nov:Dec:"
- return doIndex(m, n)
- }
-
- // Return the 3-letter symbol for day #n
- function day(n)
- {
- var d = "Sun:Mon:Tue:Wed:Thu:Fri:Sat:"
- return doIndex(d, n)
- }
-
- // A Custom "GMT" Function
- function myGMT(d)
- {
- tmp = day(d.getDay())+', '+d.getDate()+' '+month(d.getMonth())
- tmp += ' 19'+d.getYear()+' '
-
- if (d.getHours() < 10) tmp += '0'
- tmp += d.getHours()+':'
-
- if (d.getMinutes()< 10) tmp += '0'
- tmp += d.getMinutes()+':'
-
- if (d.getSeconds() < 10) tmp += '0'
- tmp += d.getSeconds()+" GMT"
-
- return tmp
- }
-
-
- //------------------RANDOM NUMBERS----------------------------
- var js_mult1=3141
- var js_mult2=5821
- var js_m1=100000000
- var js_m2=10000
- var js_iseed=0
- var js_iseed1=0
- var js_iseed2=0
-
-
- // Return a Random Integer between 0 and N-1
- function random(n)
- {
- if (js_iseed == 0)
- {
- now = new Date()
- js_iseed = now.getHours() + now.getMinutes() * 60
- + now.getSeconds() * 3600
- }
- js_iseed1 = js_iseed / js_m2
- js_iseed2 = js_iseed % js_m2
- var tmp = (((js_iseed2 * js_mult1 + js_iseed1 * js_mult2) % js_m2) *
- js_m2 + (js_iseed2 * js_mult2)) % js_m1
- js_iseed = (tmp + 1) % js_m1
- return (Math.floor((js_iseed/js_m1) * n))
- }
-
-
- //------------------ARRAY CREATION----------------------------
-
- // create an array
- function createArray(n)
- {
- for (var i = 0; i < n; i++) {this[i] = 0}
- return this
- }
-
- // --------------------HEXADECIMAL CONVERSION---------------------
-
- // convert a single digit (0 - 16) into hex
- function enHex(aDigit)
- {
- return("0123456789ABCDEF".substring(aDigit, aDigit+1))
- }
-
- // convert a hex digit into decimal
- function deHex(aDigit)
- {
- return("0123456789ABCDEF".indexOf(aDigit))
- }
-
- // Convert a 24bit number to hex
- function toHex(n)
- {
- return (enHex((0xf00000 & n) >> 20) +
- enHex((0x0f0000 & n) >> 16) +
- enHex((0x00f000 & n) >> 12) +
- enHex((0x000f00 & n) >> 8) +
- enHex((0x0000f0 & n) >> 4) +
- enHex((0x00000f & n) >> 0))
- }
-
- // Convert a six character hex to decimal
- function toDecimal(hexNum)
- {
- var tmp = ""+hexNum.toUpperCase()
- while (tmp.length < 6) tmp = "0"+tmp
-
- return ((deHex(tmp.substring(0,1)) << 20) +
- (deHex(tmp.substring(1,2)) << 16) +
- (deHex(tmp.substring(2,3)) << 12) +
- (deHex(tmp.substring(3,4)) << 8) +
- (deHex(tmp.substring(4,5)) << 4) +
- (deHex(tmp.substring(5,6))))
- }
-
- // --------------------JSCcolor OBJECT---------------------
-
- // Returns a JavaScript integer representing a JSCcolor object's color
- function rawColor()
- {
- return (this.red << 16) + (this.green << 8) + this.blue
- }
-
- // Returns a hex string representing a JSCcolor object's color
- function hexColor()
- {
- return toHex(this.rawColor())
- }
-
- // Set the JSCcolor object's color to that in a hex string.
- // This routine does not add the Ox prefix.
- function setColor(aString)
- {
- var tmp = toDecimal(aString)
-
- this.red = (0xff0000 & tmp) >> 16
- this.green = (0xff00 & tmp) >> 8
- this.blue = (0xff & tmp)
-
- return this
- }
-
- // Set the JSCcolor object's color to an integer
- function setDecimalColor(aNumber)
- {
- var tmp = toHex(aNumber)
-
- this.red = eval('0x'+tmp.substring(0,2))
- this.green = eval('0x'+tmp.substring(2,4))
- this.blue = eval('0x'+tmp.substring(4,8))
-
- return this
- }
-
- // Adjust a JSCcolor object's color (red, green or blue) by an offset
- function adjustColor(aColor, anOffset)
- {
- this[aColor] += anOffset
- if (this[aColor] > 0xff) this[aColor] = 0xff
- if (this[aColor] < 0x0) this[aColor] = 0x0
- }
-
- // Adjust one or more of a JSCcolor object's colors by an offset
- function adjustColors(colString, anOffset)
- {
- var cs = colString.toUpperCase()
-
- if (cs.indexOf('R') != -1) this.adjustColor('red', anOffset)
- if (cs.indexOf('G') != -1) this.adjustColor('green', anOffset)
- if (cs.indexOf('B') != -1) this.adjustColor('blue', anOffset)
- }
-
- // JSCcolor object constructor
- function JSCcolor(r, g, b)
- {
- // set properties
- this.red = r
- this.green = g
- this.blue = b
-
- // set methods
- this.rawColor = rawColor
- this.hexColor = hexColor
- this.adjustColor = adjustColor
- this.adjustColors = adjustColors
- this.setColor = setColor
- this.setDecimalColor = setDecimalColor
-
- return this
- }
-
- // --------------------POPUP WINDOW---------------------
-
- function PopIt(label, msg)
- {
- // Set up Page Colors & Table
- var s1 =
- "<TITLE>Information!</TITLE>" +
- "<BODY BGCOLOR='ffffff'><TABLE BORDER=0><TR>" +
- "<TD WIDTH=90% HEIGHT=90 VALIGN=TOP ALIGN=LEFT>"+
- "<FONT SIZE=4>"
-
- // Emphasize Label
- var s2 = "<FONT COLOR='FF0000'><B>"+label+"</B></FONT><P>"
-
- // Create a Close Button and Finish Table
- var s3 =
- "</TD><TD WIDTH=10%> </TD></TR><TR><TD> </TD>"+
- "<TD VALIGN=TOP ALIGN=RIGHT>"+
- "<FORM><INPUT TYPE='BUTTON' VALUE='Okay'" +
- "onClick='self.close()'>" +
- "</FORM></TD></TR></TABLE></BODY>"
-
- // Create the Window
- popup = window.open("","popDialog","height=160,width=300,scrollbars=no")
- popup.document.write(s1+s2+msg+s3)
- popup.document.close()
- }
-
- // --------------------MATH STRINGS---------------------
-
- // Strip a floating point's digits to #.### -- you set the digits
- function stripDigits(aNumber, digits)
- {
- var str = "" + aNumber
- var b = str.lastIndexOf(".")
-
- // add decimal point if needed
- if (b < 0) str += "."
-
- // pad with extra zeros in case we have too "round" a number
- for (var i = 0; i < digits; i++) str += "0"
-
- // extract existing decimal or just return
- if (b >= 0) return(str).substring(0,b+1+digits)
- return str
- }
-
- // Allow for dollar signs in numeric input
- function stripDollar(aString)
- {
- // An empty field means skip and return zero
- if (aString=="") return 0
-
- // Check if a dollar sign is found
- var dollar = aString.indexOf("$")
- var myStr = ""+aString
-
- // If so, skip past it
- if (dollar >= 0)
- {
- var len = aString.length
- myStr = ""+aString.substring(dollar+1, len)
- }
-
- // Evaluate the rest of the string as a floating point
- return parseFloat(myStr)
- }
-
-
-