/* CopyrightTag: Copyright (c) 2000-2008 Satmetrix Systems, Inc. All Rights Reserved. */
/* $Id: util.js,v 1.13 2008/01/08 23:55:22 build Exp $ */
/*
 * @(#)util.js    1.0 4/17/00 Gregory D. Clemenson
 *
 */

// History:
//   Jan 03 2000   jwalton - added checkTextLength255 function
//   Nov 17 2000   jwalton - isEmpty now uses trim instread of regexp so it works with Mac Netscape
//   Nov 15 2000     manda - changed method trim to fix 'single char not taken as idea' bug
//   Nov 10 2000   jwalton - added isInteger, isDouble, and isEmpty
//   Aug 14 2000     mchen - changed parameter name for
//                           isWhiteSpaceChar to fix bug in Netscape

// Byte-by-byte hex strings:
var hexDigits = "0123456789ABCDEF"
function hexString(n) {
    var str = ""
    if(n > 255) {
        str = hexString(n >> 8)
        n &= 255
    }
    return str + hexDigits.charAt(n >> 4) + hexDigits.charAt(n & 15)
}

// Turn an array to a tab-delimited string.
// Converts contained tabs to 4 spaces.
function packArray(ar) {
    var str = ""
    if(!ar || ar.length == 0) {
        return str
    }
    var len = ar.length
    for(var i = 0; i < len; ++i) {
        if(i > 0) {
            str += "\t"
        }
        var val = ar[i]
        if(typeof val == "string") {
            // Convert tabs:
            var x0 = 0
            var x1
            while((x1 = val.indexOf("\t", x0)) >= 0) {
                str += val.substring(x0, x1)
                str += "    "
                x0 = x1 + 1
            }
            str += x0 > 0 ? val.substring(x0, val.length) : val
        } else {
            str += val
        }
    }
    return str;
}

// Returns the input string minus leading and trailing whitespace
function trim(str) {
    if (!str) {
        return ""
    }
    var len = str.length
    var x0 = 0
    var xn = len-1
    while (x0 <= xn && isWhiteSpaceChar(str.charAt(x0))) {
        ++x0
    }
    while (xn >= x0 && isWhiteSpaceChar(str.charAt(xn))) {
        --xn
    }
	return str.substring(x0, xn+1)
//    return (x0 == xn) ? "" : str.substring(x0, xn+1)
}

function isWhiteSpaceChar(checkChar) {
    return " \t\n\r\f".indexOf(checkChar) >= 0 ? 1 : 0
}

/* Return true if the value passed in can be interpreted as an integer (negative or positive) */
function isInteger(val) {
	str = new String(val);
	for (var i=0; i<str.length; i++) {
		var ch = str.charAt(i);
		if (i == 0 && ch == "-") {
			continue;
		}
		if (ch < "0" || ch > "9") {
			return false;
		}
	}
	return true;
}

/* Return true if the value passed in can be interpreted as a number (integer or double) */
function isNumber(val) {
	oneDecimal = false;
	str = val.toString();
	for (var i=0; i<str.length; i++) {
		var ch = str.charAt(i);
		if (i == 0 && ch == "-") {
			continue;
		}
		if (ch == "." && !oneDecimal) {
			oneDecimal = true;
			continue;
		}
		if (ch < "0" || ch > "9") {
			return false;
		}
	}
	return true;	
}

/* Return true if the string is all whitespace (of any kind) */
function isEmpty(inString) {
	var trimmedStr = trim(inString);
	return (trimmedStr == null || trimmedStr == "");
	//var re = /\w+/;
	//answer = re.exec(inString);
	//return (answer == null || answer == "");
}

/*
 * Replace any high order numbers (first byte FF instead of 00)
 * with the regular numbers.This is work around the japanese
 * keyboard numbers that are entered in some of the browsers.
 * Numbers with high order bytes as FF do not match the less
 * than or greater than checks in our validation functions.
 */
 function replaceHighOrderNumbers(stringVal) {
    str = stringVal.toString();
    var newString = "";
    for (var i=0; i<str.length; i++) {
       var ch = str.charAt(i);
       var chIntVal = ch.charCodeAt(0);
       if (chIntVal > 65295 && chIntVal < 65306) {
          var num = chIntVal - 65296;
          newString = newString + num;
       } else {
          newString = newString + ch;
       }
    }
    return newString;
 }

/* Check if the widget passed in has more than 255 characters entered.  
 * If so, show an alert message and trim the string to 255.  This is 
 * done because the database will not accept more than 255 characters.
 * Note:  default is 255, can pass in other lengths if prefer...
 */
function checkTextLength255(textWidget, length) {
    if (length == null)
        length = 255;
    
    var len = textWidget.value.length;
        if (len > length) {
            textWidget.value = textWidget.value.substring(0, length);
            window.alert(stringReplace1(LENGTH_ERROR_STRING, length, false));
        }
}

/*
 * Converts the string for html.  Replaces <, >, ', ", &
 */
function convertStringForHtml(string) {
	if ( string && typeof(string) == "string" ) {
        var htmlStr = string.replace( /&/g, "&amp;" );
        htmlStr = htmlStr.replace( /\"/g, "&quot;" );
        htmlStr = htmlStr.replace( /</g, "&lt;" );
        htmlStr = htmlStr.replace( />/g, "&gt;" );
        htmlStr = htmlStr.replace( /'/g, "&#39;" );
        return htmlStr;
    }
    
    return string;
}

/* --JavaScript Collection Object-- */
//~~Author~~. John Call jcall@sark.com

function CCollection() {
/* --CCollection object-- */
	var lsize = 0;

	this.add = _add;
	this.remove = _remove;
	this.isEmpty = _isEmpty;
	this.size = _size;
	this.clear = _clear;
	this.clone = _clone;

	function _add(newItem) {
	/* --adds a new item to the collection-- */
		if (newItem == null) return;

		lsize++;
		this[(lsize - 1)] = newItem;
	}

	function _remove(index) {
	/* --removes the item at the specified index-- */
		if (index < 0 || index > this.length - 1) return;
		this[index] = null;

		/* --reindex collection-- */
		for (var i = index; i <= lsize; i++)
			this[i] = this[i + 1];

		lsize--;
	}

	function _isEmpty() { return lsize == 0 }	/* --returns
boolean if collection is/isn't empty-- */

	function _size() { return lsize }	/* --returns the size of
the collection-- */

	function _clear() {
	/* --clears the collection-- */
		for (var i = 0; i < lsize; i++)
			this[i] = null;

		lsize = 0;
	}

	function _clone() {
	/* --returns a copy of the collection-- */
		var c = new CCollection();

		for (var i = 0; i < lsize; i++)
			c.add(this[i]);

		return c;
	}
}

/* --JavaScript Collection Object-- */
//~~Author~~. John Call jcall@sark.com

function Dictionary() {
// --Dictionary Object--
	this.setAt = _setAt;
	this.lookUp = _lookUp;
	this.remove = _remove;
	this.clear = _clear;
	this.keys = _keys;
	this.toString = _toString;
	
	this.length = 0;
	this._skeys = new Object();
	
	function _setAt(key, val) {
		if (key != null) {
			this.length++;
			this._skeys[key] = val;
		}
	}
	
	function _remove(key) {
		if (this._skeys[key]) {
			this.length--;
			return delete this._skeys[key];
		}
		else
			return false;
	}
	
	function _clear() {
		this._skeys = new Object();
		this.length = 0;
	}
	
	function _lookUp(key) { return this._skeys[key] }
	
	function _keys() { return this._skeys }
	
	function _toString() { return "[object Dictionary]" }
}
