﻿// JScript File


//  PURPOSE:    Copies one array to another.
//              NB normally you'd use slice but it needs a 
//              newly initialized array.  Because we pass
//              the array we need to manually copy to it.  
//  USES:       -
//  INPUTS:     The arrays to copy to (to use for validation and displaying)
//  OUTPUTS:    -
 function copyArray(arrayTo, arrayFrom) {        
    var i;   
    for (i in arrayFrom){
        arrayTo[i] = arrayFrom[i];
    }
 }       
     

// Add Option to Obout ComboBox
// Inputs:  cbo     - Obout Combo Box (reference directly)
//          addText - String to add to combo 
function addOption(cbo,addText){       
    cbo.addOption('',addText,'','');                                   
    cbo.setSelectedIndex(cbo.Options.length-1);  
    CustomValidatePage();                                            
} 


// Access Query String from client-side.
// Supporting function/class
function PageQuery(q) {
    if(q.length > 1) 
        this.q = q.substring(1, q.length);
    else 
        this.q = null;
        this.keyValuePairs = new Array();
        if(q) {
            for(var i=0; i < this.q.split("&").length; i++) {
            this.keyValuePairs[i] = this.q.split("&")[i];
        }
    }
    this.getKeyValuePairs = function() { return this.keyValuePairs; }
    this.getValue = function(s) {
    for(var j=0; j < this.keyValuePairs.length; j++) {
        if(this.keyValuePairs[j].split("=")[0] == s)
	        return this.keyValuePairs[j].split("=")[1];
        }
        return false;
    }
    this.getParameters = function() {
    var a = new Array(this.getLength());
    for(var j=0; j < this.keyValuePairs.length; j++) {
        a[j] = this.keyValuePairs[j].split("=")[0];
    }
    return a;
    }
    this.getLength = function() { return this.keyValuePairs.length; }	
}

// Access Query String from client-side
// Call this function with query string key.
// Returns false if not found.
function queryString(key){
    var page = new PageQuery(window.location.search); 
    return unescape(page.getValue(key)); 
}


function debugAlert(msg){
    alert(msg);
}


function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}


function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}


//Handle enter key press to trigger button click
function focusNext(event, element) {
    var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    if (keyCode == 13) {
        var oNextObj = document.getElementById(element);
        if (oNextObj) {
            oNextObj.focus();
            oNextObj.click();
        } //end if
        return false;
    } //end if
} //end function

//Handle enter key press to trigger button click
function enterKeyEvent(event, method) {
    var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    if (keyCode == 13) {

        //Method to fire
        method();

        event.returnValue = false;
        event.cancel = true;
    } //end if
    return (keyCode != 13);
} //end function


// Apply CSS via javascript!
//
// INPUTS:
//a     defines the action you want the function to perform. 
//      Possible values:
//      *   swap    replaces class c1 with class c2 in object o. 
//      *   add     adds class c1 to the object o. 
//      *   remove  removes class c1 from the object o. 
//      *   check   test if class c1 is already applied to object o and returns true or false. 
//o     the object in question. 
//c1    the name of the first class 
//c2    the name of the second class 
function jscss(a, o, c1, c2) {
    switch (a) {
        case 'swap':
            o.className = !jscss('check', o, c1) ? o.className.replace(c2, c1) : o.className.replace(c1, c2);
            break;
        case 'add':
            if (!jscss('check', o, c1)) { o.className += o.className ? ' ' + c1 : c1; }
            break;
        case 'remove':
            var rep = o.className.match(' ' + c1) ? ' ' + c1 : c1;
            o.className = o.className.replace(rep, '');
            break;
        case 'check':
            return new RegExp('\\b' + c1 + '\\b').test(o.className)
            break;
    }
}     
