


/* Prototype(s) */

Array.prototype.max = function() { 
    if (this.length == 0) return undefined;
    
	var max = this[0];
	for (var i = 0; i < this.length; i++) { if (max < this[i]) max = this[i]; }
	
	return max;
}

if (Array.prototype.push == null) {
	Array.prototype.push = function(item) { this[this.length] = item; return this.length; }
}

if (String.prototype.trim == null) {
    String.prototype.trim = function() { return this.replace(/(^\s*)|(\s*$)/g, ""); }
}



/* Additional functionality for the document object to query elements by their css class name. */

document.getElementsByClassName = function(className, tag) {
    return getElementsByClassName(document, className, tag);
}

function getElementsByClassName(node, className, tag)
{
    if (!tag) tag = "*";

    var nodes = node.getElementsByTagName(tag);
    var length = nodes.length;

    var items = new Array();
    var pattern = new RegExp("(^|\\s)" + className + "(\\s|$)");

    for (var i = 0; i < length; i++)
    {
        var item = nodes[i];
        if (pattern.test(item.className))
        {
            items.push(item);
        }
    }

    return items;
}



/* Cross-browser "onload" handler */

window.onloadEvents = new Array();
window.onload = function() {
    var x = window.onloadEvents.length;
	for (var i = 0; i < x; i++) {
		eval(window.onloadEvents[i]);
	}
}

function addOnLoadEvent(handle) {
    window.onloadEvents.push(handle);
}



/* Cross-browser "onunload" handler */

window.onunloadEvents = new Array();
window.onunload = function() {
    var x = window.onunloadEvents.length;
	for (var i = 0; i < x; i++) {
		eval(window.onunloadEvents[i]);
	}
}

function addOnUnLoadEvent(handle) {
    window.onunloadEvents.push(handle);
}



/* Miscellaneous */

function autoFocus()
{
    for (var i = 0; i < document.forms.length; ++i)
    {
        var f = document.forms[i];
        for (var j = 0; j < f.elements.length; ++j)
        {
            if (f.elements[j].type == 'text' ||
                f.elements[j].type == 'textarea')
            {
                try 
                {
                    f.elements[j].focus();
                    return;
                }
                catch (e) {}
            }
        }
    }
}
addOnLoadEvent('autoFocus();');


// Added by AMB on 5/10/2007
// Formats a numeric string by adding commas.
function addCommas(nStr)
{
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}

/* New auto-tab functionality to fix some of the odd behaviors of the original functionality.
        1.  Tabbing back into a field that equalled its maxlength caused the auto-tab to move the
            cursor back to the next field.
        2.  Clicking into an auto-tab field worked fine but as soon as you press another key
            (e.g. left arrow) the cursor would auto-tab to the next field.
   
   NOTE:  Make sure to set the "maxlength" attribute of the field.
*/
function autoTab(input, e) 
{
    var keyCode = (agent.netscape) ? e.which : ((e.keyCode) ? e.keyCode : e.charCode);
    var filter = (agent.netscape) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
    if((input.getAttribute && input.value.length >= input.getAttribute("maxlength")) && !containsElement(filter, keyCode)) 
    {
        input.value = input.value.slice(0, input.getAttribute("maxlength"));
        
        var found = false, offset = 1, inputIndex = getIndex(input);
        while (!found)
        {
            try 
            { 
                // Make sure we're not past the end of the form.
                if (inputIndex + offset <= input.form.length)
                    input.form[(inputIndex + offset) % input.form.length].focus();
                
                found = true;  // we were able to successfully move focus to the next control.
            } 
            catch (e) 
            {
                offset++;
            }
        }        
    }
}

function containsElement(arr, ele) 
{
    var found = false, index = 0;
    while(!found && index < arr.length)
    {
        if(arr[index] == ele)
            found = true;
        else
            index++;
    }
    return found;
}

function getIndex(input) 
{
    var index = -1, i = 0;
    while (i < input.form.length && index == -1)
    {
        if (input.form[i] == input)
            index = i;
        else
            i++;
    }
    return index;
}
/* End auto-tab functionality */

// prevents use of script tags or other unwanted content.
// primarily used for checking usernames & passwords. AMB
function checkCredentialCharacter()
{ 
    if (window.event.keyCode == 60 || window.event.keyCode == 62)
        return false;
    else
        return true;
}

function focusWindow(linkplace,windowname)
{
	var win = window.open(linkplace,windowname);
	win.focus();
	return false;
}

function getWindowHeight()
{
    var winH;
    if (parseInt(navigator.appVersion)>3) 
    {
        if (navigator.appName=="Netscape") 
            winH = window.innerHeight;
        if (navigator.appName.indexOf("Microsoft")!=-1) 
            winH = document.body.offsetHeight;
    }
    return winH;
}

function getWindowWidth()
{
    var winW = 0;
    if (parseInt(navigator.appVersion)>3) 
    {
        if (navigator.appName=="Netscape") 
            winW = window.innerWidth;
        if (navigator.appName.indexOf("Microsoft")!=-1) 
            winW = document.body.offsetWidth;
    }
    return winW;
}

function resizeToFillPage(tagClientID)
{
    if (parseInt(navigator.appVersion)>3) 
    {
        var element = document.getElementById(tagClientID);
        if (element != null && element != undefined)
        {            
            element.style.height = getWindowHeight() + "px";
            element.style.width = getWindowWidth() + "px";
        }
    }
}


function resizeToFillParent(tagClientID)
{
    var element = document.getElementById(tagClientID);
    if (element != null && element != undefined)
    {
        var parentElement = element.parentNode;
        if (parentElement != null && parentElement != undefined)
        {
            element.style.width = parentElement.offsetWidth;
            element.style.height = parentElement.offsetHeight - 46;
        }
    }
}

function IsKeyCodeNumeric(keynum)
{
    var keychar;
    var numcheck;
    keychar = String.fromCharCode(keynum);
    numcheck = /\d/;
    var isValid = numcheck.test(keychar)
    return isValid;
}

function IsNumericKey(e)
{
    var keynum;
    var keychar;
    var numcheck;
    
    if(window.event) // IE
        keynum = e.keyCode;
    else if(e.which) // Netscape/Firefox/Opera
        keynum = e.which;

    var isValid = IsKeyCodeNumeric(keynum);
    // undefined occurs for some special keys (tab, arrows)
    return (isValid || keynum == 47 || keynum == 45 || keynum == undefined);
}

// Auto-inserts slashes in as the user types a date into a textbox
function AddDateSlashes(textbox, e)
{
    var value = textbox.value;
    var numericValue = "";
    var formattedValue = "";
    var keyPressed;
    
    if(window.event) // IE
        keyPressed = e.keyCode;
    else if(e.which) // Netscape/Firefox/Opera
        keyPressed = e.which;

    if (keyPressed != 37 && keyPressed != 39)
    {
        // Get numeric data
        for (i = 0; i < value.length; i++)
        {
            var keynum = value.charCodeAt(i);
            var charString = String.fromCharCode(keynum);
            if (IsKeyCodeNumeric(keynum))
                numericValue = numericValue + charString;
        }
        
        // add the nearest slash if we're at the right position
        if (numericValue.length > 4)
            formattedValue = numericValue.substring(0, 2) + "/" + numericValue.substring(2, 4) + "/" + numericValue.substring(4, numericValue.length);
        else if (numericValue.length > 2)
            formattedValue = numericValue.substring(0, 2) + "/" + numericValue.substring(2, numericValue.length);
        else
            formattedValue = numericValue;
        
        textbox.value = formattedValue;
    }
}

// Keeps slashes in the textbox and allows the user to type around them.  
// Auto-inserts slashes and persists them as placeholders.
function AddDateSlashes2(textbox, e)
{
    var value = textbox.value;
    var numericValue = "";
    var formattedValue = "";
    var keyPressed;
    var cursorIndex = getSelectionStart(textbox);
    
    if(window.event) // IE
        keyPressed = e.keyCode;
    else if(e.which) // Netscape/Firefox/Opera
        keyPressed = e.which;

    var isKeyNumeric = IsKeyCodeNumeric(keyPressed);
    
    // Exclude formatting for special keys.
    if (isKeyNumeric || keyPressed == 8 || keyPressed == 46) // numeric, backspace, or delete
    {    
        // Place the character where the cursor is.
        if (isKeyNumeric)
            value = value.substring(0, cursorIndex) + String.fromCharCode(keyPressed) + value.substring(cursorIndex);

        // Get numeric data
        numericValue = stripToNumeric(value);
        
        if (numericValue.length <= 8)
        {
            if (numericValue.length > 0)
                cursorIndex += 1;

            // Increase the cursor placement index if we'll be adding slashes.
            if (keyPressed != 8 &&(numericValue.length == 4 || numericValue.length == 2))
                cursorIndex += 1;
                
            // Add slashes if the user has typed in the month or the day.
            if (numericValue.length > 4)
                formattedValue = numericValue.substring(0, 2) + "/" + numericValue.substring(2, 4) + "/" + numericValue.substring(4, numericValue.length);
            else if (numericValue.length > 2)
                formattedValue = numericValue.substring(0, 2) + "/" + numericValue.substring(2, numericValue.length);
            else
                formattedValue = numericValue;
            
            // Add the additional formatting to the end of the incomplete date.
            if (formattedValue.length < 3)
            {                
                while (formattedValue.length < 2)
                    formattedValue = formattedValue + "b";
                
                formattedValue = formattedValue + "/";
            }
            
            if (formattedValue.length < 6)
            {
                while (formattedValue.length < 5)
                    formattedValue = formattedValue + "b";
                
                formattedValue = formattedValue + "/";
            }
            
            while (formattedValue.length < 10)
                formattedValue = formattedValue + "b";
            
           
//            if ((keyPressed == 46 || keyPressed == 8) && cursorIndex <= 10)
//                cursorIndex -= 1;
            
            textbox.value = formattedValue;    
            SetTextboxCursorPosition(textbox, cursorIndex);    
            return true;
        }
    }
    return false;
}

// Auto-inserts dashes in as the user types ssn into a textbox
function AddSSNDashes(textbox, e)
{
    var value = textbox.value;
    var numericValue = "";
    var formattedValue = "";
    var keyPressed;
    
    if(window.event) // IE
        keyPressed = e.keyCode;
    else if(e.which) // Netscape/Firefox/Opera
        keyPressed = e.which;

    if (keyPressed != 37 && keyPressed != 39)
    {
        // Get numeric data
        for (i = 0; i < value.length; i++)
        {
            var keynum = value.charCodeAt(i);
            var charString = String.fromCharCode(keynum);
            if (IsKeyCodeNumeric(keynum))
                numericValue = numericValue + charString;
        }

        // Add a slash if the user has entered the first 3 digits or the next group of 2 digits.    
        if (numericValue.length > 5)
            formattedValue = numericValue.substring(0, 3) + "-" + numericValue.substring(3, 5) + "-" + numericValue.substring(5, numericValue.length);
        else if (numericValue.length > 3)
            formattedValue = numericValue.substring(0, 3) + "-" + numericValue.substring(3, numericValue.length);
        else
            formattedValue = numericValue;
        
        textbox.value = formattedValue;
    }
}

// Keeps dashes in the textbox and allows the user to type around them.  
// Auto-inserts dashes and persists them as placeholders.
function AddSSNDashes2(textbox, e)
{
    var value = textbox.value;
    var numericValue = "";
    var formattedValue = "";
    var keyPressed;
    var cursorIndex = getSelectionStart(textbox);
    
    if(window.event) // IE
        keyPressed = e.keyCode;
    else if(e.which) // Netscape/Firefox/Opera
        keyPressed = e.which;

    //alert(keyPressed);
        
    // Exclude formatting for left/right arrows.
    if (IsKeyCodeNumeric(keyPressed) || keyPressed == 8 || keyPressed == 46) // numeric, backspace, or delete
    {    
        // Place the character where the cursor is.
        if (IsKeyCodeNumeric(keyPressed))
            value = value.substring(0, cursorIndex) + String.fromCharCode(keyPressed) + value.substring(cursorIndex);
        
        // Get numeric data
        numericValue = stripToNumeric(value);
        
        if (numericValue.length <= 9)
        {
            if (numericValue.length > 0)
                cursorIndex += 1;

            // Increase the cursor placement index if we'll be adding slashes.
            if (keyPressed != 8 &&(numericValue.length == 5 || numericValue.length == 3))
                cursorIndex += 1;
                
            // Add dashes if the user has typed in the first 3 digits or the second group of 2 digits.
            if (numericValue.length > 5)
                formattedValue = numericValue.substring(0, 3) + "-" + numericValue.substring(3, 5) + "-" + numericValue.substring(5, numericValue.length);
            else if (numericValue.length > 3)
                formattedValue = numericValue.substring(0, 3) + "-" + numericValue.substring(3, numericValue.length);
            else
                formattedValue = numericValue;
            
            // Add the additional formatting to the end of the incomplete ssn.
            if (formattedValue.length < 4)
            {
                while (formattedValue.length < 3)
                    formattedValue = formattedValue + " ";
                
                formattedValue = formattedValue + "-";
            }
            
            if (formattedValue.length < 7)
            {
                while (formattedValue.length < 6)
                    formattedValue = formattedValue + " ";
                
                formattedValue = formattedValue + "-";
            }
            
            while (formattedValue.length < 11)
                formattedValue = formattedValue + " ";
            
            if ((keyPressed == 46 || keyPressed == 8) && cursorIndex <= 11)
                cursorIndex -= 1;

            textbox.value = formattedValue;    
            SetTextboxCursorPosition(textbox, cursorIndex);    
        }
    }
}

// Sets the cursor caret to the specified location in the specified textbox.
function SetTextboxCursorPosition(textbox, index)
{
    if (textbox != null) 
    {
        if(textbox.createTextRange) 
        {
            var range = textbox.createTextRange();
            range.move('character', index);
            range.select();
        }
        else 
        {
            if(textbox.selectionStart) 
            {
                textbox.focus();
                textbox.setSelectionRange(index, index);
            }
            else
            {
                textbox.focus();
            }
        }
    }
}

function getSelectionStart(textbox) 
{
    if (textbox.createTextRange) 
    {
	    var r = document.selection.createRange().duplicate()
	    r.moveEnd('character', textbox.value.length)
	    if (r.text == '') return textbox.value.length
	    return textbox.value.lastIndexOf(r.text)
    } 
    else
    { 
        return textbox.selectionStart
    }
}

function stripToNumeric(value)
{
    var numericValue = "";
    
    for (i = 0; i < value.length; i++)
    {
        var keynum = value.charCodeAt(i);
        var charString = String.fromCharCode(keynum);
        if (IsKeyCodeNumeric(keynum))
            numericValue = numericValue + charString;
    }
    return numericValue;
}

function captureBackspace()
{
    if(window.event && window.event.keyCode == 8) 
       return false; 
}

