<!--
/*
 * Author: Tom Esposito
 * Date: 03/25/2003
 * File: UtilityFunctions.js
 */

    var FORM_SUBMIT_COUNT = 0;
    var popup = null;
    /*
     * Trim() will return the variable passed without 
     * the leading and trailing spaces 
     */
    function Trim(val) {
        return (LTrim(RTrim(val)));
    }

    /*
     * LTrim() will return the variable passed without 
     * the leading spaces 
     */
    function LTrim(val) {
          var i;

        for (i = 0; i < val.length; i++) {
            if (val.charAt(i) != ' ') {
                break;
            }
        }
        return (val.substring(i, val.length));
    }

    /*
     * RTrim() will return the variable passed without 
     * the trailing spaces 
     */
    function RTrim(val) {
        var i;

        for (i = val.length; i >= 0; i--) {
            if (val.charAt(i - 1) != ' ') {
                break;
            }
        }
        return (val.substring(0, i));
    }

    /*
     * containsQuote() returns true if the variable passed contains a double quote
     * 
     * 
     */
    function containsQuote(val) {
        return (val.indexOf('"') != -1)?true:false;
    }

    /*
     * isEmpty() returns true if the variable passed is empty
     * after the Trim() function, and returns false is the 
     * passed variable is not empty after the Trim() function.
     */
    function isEmpty(val) {
        return (val == '')?true:false;
    }
   
    /*
     * isInteger() returns true if the variables passed is an 
     * integer or false if the variable is not a integer
     */
    function isInteger(val) {
        var integerList = '0123456789';
        var isInteger = false;
        for (var i = 0; i < val.length; i++) {
            isInteger = false;
            for (var j = 0; j < integerList.length; j++) {
                if (integerList.charAt(j) == val.charAt(i)) {
                    isInteger = true;
                    break;
                } 
            } 
            if (!isInteger) {
                break;
            }
        }
        return isInteger;
    }

    /*
     * isNegativeNumber() returns true if the variable represents a negative number
     * or false if a positive number
     */
    function isNegativeNumber(val) {
	if(val.indexOf("-") != -1) return true;

	return false;
    }

    /*
     * isDouble() returns true if the variables passed is an 
     * double or returns false if the variable is not a double
     * or the variable is a negative number
     */
    function isDouble(val) {
	if(isNegativeNumber(val)) return false;

	var decimalCtr = 0;
        var leftVal = '', rightVal = '';
      
        if (!isEmpty(val)) {
            if (val.charAt(0) == ".") {
                val = "0" + val;
            }
        }
        for (var i = 0; i < val.length; i++) {
            if (val.charAt(i) == '.') {
                decimalCtr++;
            } else if (decimalCtr == 0) {
                leftVal += val.charAt(i);
            } else if (decimalCtr == 1) {
                rightVal += val.charAt(i);
            }
        }
        if (decimalCtr == 0 && !isInteger(leftVal)) {
            return false;
        }
        if (isEmpty(leftVal) || isEmpty(rightVal)) {
            return false;
        } else if ((leftVal.length > 0 && !isInteger(leftVal)) || 
            (rightVal.length > 0 && !isInteger(rightVal))) {
                return false;
        } else {
            return true;
        }
    }

    /*
     * isAlphabetic() returns true if the variables passed is  
     * alphabetic or returns false if the variable is not 
     * alphabetic
     */
    function isAlphabetic(val, exceptions) {
        if (exceptions == null) {
            exceptions = "";
        }
        var upperCaseList = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + exceptions;
        var lowerCaseList = "abcdefghijklmnopqrstuvwxyz";
        var isUpperCase = false, isLowerCase = false;

        for (var i = 0; i < val.length; i++) {
            isUpperCase = isLowerCase = false;
            for (var j = 0; j < lowerCaseList.length; j++) {
                if (val.charAt(i) == lowerCaseList.charAt(j)) {
                    isLowerCase = true;
                    break;
                }
            }
            if (!isLowerCase) {
                for (var j = 0; j < upperCaseList.length; j++) {
                    if (val.charAt(i) == upperCaseList.charAt(j)) {
                        isUpperCase = true;
                        break;
                    }
                }
                if (!isUpperCase) {
                    break;
                }
            }
        }
        return (isLowerCase || isUpperCase)?true:false;
    }

    /*
     * isAlphaNumeric() returns true if the variables passed is  
     * alpha/numeric or returns false if the variable is not 
     * alpha/numeric
     */
    function isAlphaNumeric(val, exceptions) {
        var isAlphaNumeric = false;
      
        for (var i = 0; i < val.length; i++) {
            isAlphaNumeric = false;
            if (!isInteger(val.charAt(i))) {
                if (!isAlphabetic(val.charAt(i), exceptions)) {
                    break;
                } else {
                    isAlphaNumeric = true;
                }
            } else {
                isAlphaNumeric = true;
            }
        }
        return isAlphaNumeric;
    }
   
    /*
     * isDate() returns true if the variables passed is in a 
     * valid date format or returns false if the variable is 
     * not in a valid date format
     */
    function isDate(val) {
        if (val.length != 10) {
            return false;
        } else if (val.charAt(2) != '/' || val.charAt(5) != '/' ||
                !isInteger(val.substring(0, 2)) || 
                !isInteger(val.substring(3, 5)) || 
                !isInteger(val.substring(6, 10))) {
            return false;
        } else
            return true;
    }
   
    /*
     * isLeapYear() returns true if the variable passed is a 
     * leap year or returns false if the variable is 
     * not in a leap year; requires 4 digit year
     */
    function isLeapYear(val)  {
        var year = '';
      
        if (val.length >= 4) {
            year = val.substring(val.length - 4, val.length);
            if (isInteger(year)) {
                return ((parseInt(year) % 4 == 0)?true:false);
            }
        }
        return false;
    }

    /*
     * isEmail() returns true if the variables passed is in a 
     * valid e-mail address format or returns false if the 
     * variable is not in a valid e-mail address format
     */
    function isEmail(val) {
        if (isEmpty(val) || val.indexOf("@") == -1 || val.indexOf(".") == -1 || 
                (val.lastIndexOf(".") < val.indexOf("@")) ||
                (val.lastIndexOf(".") == (val.indexOf("@") + 1)) ||
                (val.lastIndexOf(".") == (val.length - 1)) ||
                (val.indexOf("@") == 0)) {
            return false;
        } else {
            return true;
        }
    }
   
    /*
     * isImageFile() returns true if the file name passed does 
     * not have a ".jpg" or ".gif" file extension, and returns
     * true if is does have a ".jpg" or ".gif" file extension.
     */
    function isImageFile(val) {
        if (val.length < 5) {
            return false;
        }
         
        var imgExt = val.substring(val.length - 4, val.length).toUpperCase();
      
        if (imgExt != ".JPG" && imgExt != ".GIF") {
            return false;
        } else {
            return true;
        }
    }
   
    /*
     * isHtmlFile() returns true if the file name passed does 
     * not have a ".htm" or ".html" file extension, and returns
     * true if is does have a ".htm" or ".html" file extension.
     */
    function isHtmlFile(val) {
        if (val.length < 5) {
            return false;
        }

        var htmExt = val.substring(val.length - 4, val.length).toUpperCase();
        var htmlExt = val.substring(val.length - 5, val.length).toUpperCase();

        if (htmExt != ".HTM" && htmlExt != ".HTML") {
            return false;
        } else {
          return true;
        }
    }
   
    /*
     * getFileName() will return the file name only when given 
     * full path of the file.
     */
    function getFileName(val) {
        if (isEmpty(val)) {
            return '';
        } else if(val.indexOf("/") == -1) {
            return val;
        }
         
        var i;

        for (i = val.length; i >= 0; i--) {
            if (val.charAt(i - 1) == '/' || 
                val.charAt(i - 1) == '\\') {
                    break;
            }
        }
        return (val.substring(i, val.length));
    }
   
    /*
     * sortArrayAsc() returns the array passed in ascending 
     * order.
     */
    function sortArrayAsc(oldArray) {
        var limit = oldArray.length;
        var newArray = new Array(limit);
        var element = null;

        for (var i = 0; i < limit; i++) {
            element = oldArray[i];
            for (var j = 0; j < limit; j++) {
                if (newArray[j] == '' || newArray[j] == null) {
                    newArray[j] = element;
                    break;
                } else if (newArray[j] > element) {
                    for (var k = limit; k > j - 1; k--) {
                        newArray[k] = newArray[k - 1];
                    }
                    newArray[j] = element;
                    break;
                }
            }
        }
        return newArray;
    }

    /*
     * sortArrayDesc() returns the array passed in descending 
     * order.
     */
    function sortArrayDesc(oldArray) {
        var limit = oldArray.length;
        var newArray = new Array(limit);
        var element = null;

        for (var i = 0; i < limit; i++) {
            element = oldArray[i];
            for (var j = 0; j < limit; j++) {
                if (newArray[j] == '' || newArray[j] == null) {
                    newArray[j] = element;
                    break;
                } else if (newArray[j] < element) {
                    for (var k = limit; k > j - 1; k--) {
                        newArray[k] = newArray[k - 1];
                    }
                    newArray[j] = element;
                    break;
                }
            }
        }
        return newArray;
    }
      
    /*
     * FormatNumber() returns the formatted number passed to 
     * the precision value passed.  If the number is not valid 
     * then null will be returned.
     */
    function FormatNumber(val, precision) {
        var decimalCtr = 0;
        var validZero = false;
        var prefix = '', suffix = '', extra = '';

        for (var i = 0; i < val.length; i++) {
            if (val.charAt(i) == '.') {
                decimalCtr++;
            } else if (decimalCtr == 0) {
                if (val.charAt(i) != '0' || validZero) {
                    prefix += val.charAt(i);
                    if (!validZero) {
                        validZero = true;
                    }
                }
            } else if (decimalCtr == 1 && suffix.length < precision) {
                suffix += val.charAt(i);
            } else {
                extra += val.charAt(i);
            }
        }
        if (prefix == '' && suffix == '') {
            return "1-";
        } else if ((prefix.length > 0 && !isInteger(prefix)) || 
                (suffix.length > 0 && !isInteger(suffix)) || 
                (extra.length > 0 && !isInteger(extra)) ||
                decimalCtr > 1) {
            return "2-";
        } else if ((suffix + suffix == 0) && prefix.length == 0) {
            return "0";
        } else {   
            if (suffix.length < precision) {
                for (var j = suffix.length; j < precision; j++) {
                    suffix += '0';
                }
            }
            if (prefix.length == 0) {
                prefix = '0';
            }
            if (precision == 0) {
                return (prefix);
            } else {
                return (prefix + '.' + suffix);
            }
        }
    }
   
    /*
     * setUrlParameter() will replace ' ' characters 
     * with the '+' character
     */
    function setUrlParameter(val) {
        var param = '';
      
        for (var i = 0; i < val.length; i++) {
            if (val.charAt(i) == ' ') {
                param += '+';
            } else {
                param += val.charAt(i);
            }
        }
        return param;
    }

    // animate the processing message in the status bar
    function setProcessingMessage(msg, ct, time, duration) {
        var second = 1000;
        
        // check for erroneous arguments
        if (ct == null) {
            ct = 0;
        } else if (ct < 0) {
            ct = 0;
        }
        // check if 6 periods have been displayed
        if (ct < 6) {
            // if not, add another to the message
            msg += ".";
            ct++;
        } else {
            // if so, reset the message to the beginning
            var tmp = null;
            
            for (var i = msg.length - 1; i >= 0; i--) {
                if (msg.charAt(i) == '.') {
                    tmp = msg.substring(0, i);
                } else {
                    break;
                }
            }
            msg = tmp;
            ct = 0;
        }    
        // show the message in the status bar
        window.status = msg;
        // animate the message every 1/5 of a second
        if (time == null) {
            time = 200;
        }
        if (duration != null) {
            duration = second / time * duration;
            if (duration <= 0) {
                window.status = "";
                return;
            }
            duration--;
        }
        window.setTimeout("setProcessingMessage('" + msg + "', " + ct + ", " 
                                                + time + ", " + duration + ");", time);
    }
    
    function setMessage(msg) {
        window.status = msg;
        setProcessingMessage(msg, 0, 500, 8);
    }

    /*
     * Called during the keypressed event of an input
     * field so that the number always appears as currency
     */
    function formatCurrency(obj) {
	var val = obj.value;

	//Strip the decimal point
	if(val.indexOf(".") != -1) {
		val = val.replace('.', '');
	}

	var len = val.length;
	if(len == 1) {
		obj.value = ".0" + val;
		return;
	} else if(len == 0) {
		obj.value = ".00";
		return;
	}
	obj.value = removeLeadingZero(val.substring(0, val.length - 2) + "." + val.substring(val.length - 2));
	return;
    }

    /*
     * Called during the keypressed event of an input
     * field so that the number always appears as currency
     */
    function formatCurrency1(obj) {
	var val = obj.value;

	//Strip all non - numeric
	for (var i = 0; i < val.length; i++) {
	     if (val.charAt(i) < '0' || val.charAt(i) > '9') {
	       val = val.replace(val.charAt(i), '');
	     }
	}
	var len = val.length;
	if(len == 1) {
		obj.value = ".0" + val;
		return;
	} else if(len == 0) {
		obj.value = ".00";
		return;
	}
	obj.value = removeLeadingZero(val.substring(0, val.length - 2) + "." + val.substring(val.length - 2));
	return;
    }
    function removeLeadingZero(val) {
      	if(val.charAt(0) != 0) return val;

	for(i = 0; i < val.length; i++) {
		if(val.charAt(i) == 0) {
		  val = val.substring(i + 1);
                } else {
		  return val;
		}
	}
	return val;
    }

    /*
     * Presents the user with a confirmation box
     */
    function confirmOperation(msg) {
	if(msg == null || msg == "") {
		msg = "Are you sure?";
	}
	return confirm(msg);
    }


    /*
     * Prevents multiple form submissions
     */
    function handleSubmit() {
	if(FORM_SUBMIT_COUNT > 0) {
		alert("Processing last request");
		return false;
	}
	FORM_SUBMIT_COUNT++;
	return true;
    }

    function newHrefRequest(theProcess) {
       window.open(theProcess,"my_new_window","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=600,height=600,top=0,left=50");
       return false;
    }

    function setPopUpLocation(loc, height, width, top, left, centered) {
        if(popup != null && !popup.closed) {
           popup.close();
        }
        
        if(height == undefined) {
            height = 500;
        }

        if(width == undefined) {
            width = 500;
        }

        if(top == undefined) {
            top = 200;
        }

        if(left == undefined) {
            left = 250;
        }
        
        if(!centered) {
	        popup = window.open(loc, 'popupWindow', 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=' + width + ',height=' + height + ',top=' + top + ',left='+left);
	    } else {
	    	if (document.all) {
	        	var xMax = screen.width, yMax = screen.height;
		    } else {
		        if (document.layers) {
		            var xMax = window.outerWidth, yMax = window.outerHeight;
		        } else {
		            var xMax = 1280, yMax=1024;
		        }
			}
			
		    var xOffset = (xMax - width)/2, yOffset = (yMax - height)/2;
		
		    popup = window.open(loc,'popupWindow', 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=' + width + ',height=' + height + ',screenX='+xOffset+',screenY='+yOffset+', top='+yOffset+',left='+xOffset+'');
	    }
	    
	    popup.focus();

    }
    


    
    function getCookie (name) {
    	if(document.cookie == null) {
    		return null;
    	}
    
        var arg = name + "=";
        var alen = arg.length;
        var clen = document.cookie.length;
        
        var i = 0;
        while (i < clen) {
            var j = i + alen;
            if (document.cookie.substring(i, j) == arg) {
            	return getCookieVal (j);
            }
            
            i = document.cookie.indexOf(" ", i) + 1;
          	if (i == 0) {
	    	    break;
	        }
        }
   		return null;
	}
	
	function getCookieVal (offset) {
   		var endstr = document.cookie.indexOf (";", offset);
   		if (endstr == -1) {
   			endstr = document.cookie.length;
   		}
   		return unescape(document.cookie.substring(offset, endstr));
	}

	function setCookie (name, value, expDate, domain) {
        var argv = setCookie.arguments;
        var argc = setCookie.arguments.length;
        var expires = (argc > 2) ? argv[2] : null;
        var path = (argc > 3) ? argv[3] : null;
        var domain = (argc > 4) ? argv[4] : null;
        var secure = (argc > 5) ? argv[5] : false;
        document.cookie = name + "=" + escape (value) +
                		((expires == null) ? "" : ("; expires=" +
						expires.toGMTString())) +
						((path == null) ? "" : ("; path=" + path)) +
						((domain == null) ? "" : ("; domain=" + domain)) +
						((secure == true) ? "; secure" : "");
	}