﻿var windowLoadQueue = null;

var keyList = {
    '8'  :'Backspace',
    '9'  : 'Tab',
    '13' :'Enter',
    '16' :'Shift',
    '17' :'Ctrl',
    '18' :'Alt',
    '19' :'Break',
    '20' :'CapsLock',
    '27' :'Escape',
    '32' :'Space',
    '33' :'PageUp',
    '34' :'PageDown',
    '35' :'End',
    '36' :'Home',
    '37' :'ArrowLeft',
    '38' :'ArrowUp',
    '39' :'ArrowRight',
    '40' :'ArrowDown',
    '45' :'Insert',
    '46' :'Delete',
    '48' :'0',
    '49' :'1',
    '50' :'2',
    '51' :'3',
    '52' :'4',
    '53' :'5',
    '54' :'6',
    '55' :'7',
    '56' :'8',
    '57' :'9',
    '65' :'A',
    '66' :'B',
    '67' :'C',
    '68' :'D',
    '69' :'E',
    '70' :'F',
    '71' :'G',
    '72' :'H',
    '73' :'I',
    '74' :'J',
    '75' :'K',
    '76' :'L',
    '77' :'M',
    '78' :'N',
    '79' :'O',
    '80' :'P',
    '81' :'Q',
    '82' :'R',
    '83' :'S',
    '84' :'T',
    '85' :'U',
    '86' :'V',
    '87' :'W',
    '88' :'X',
    '89' :'Y',
    '90' :'Z',
    '93' :'Select',
    '96' :'Numpad0',
    '97' :'Numpad1',
    '98' :'Numpad2',
    '99' :'Numpad3',
    '100':'Numpad4',
    '101':'Numpad5',
    '102':'Numpad6',
    '103':'Numpad7',
    '104':'Numpad8',
    '105':'Numpad9',
    '106':'MultipLy',
    '107':'Add',
    '109':'Subtract',
    '110':'DecimalPoint',
    '111':'Divide',
    '112':'F1',
    '113':'F2',
    '114':'F3',
    '115':'F4',
    '116':'F5',
    '117':'F6',
    '118':'F7',
    '119':'F8',
    '120':'F9',
    '121':'F10',
    '122':'F11',
    '123':'F12',
    '144':'NumLock',
    '145':'ScrollLock',
    '186':'SemiColon',
    '187':'EqualSign',
    '188':'Comma',
    '189':'Dash',
    '190':'Period',
    '191':'Slash',
    '219':'OpenBrace',
    '220':'BackSlash',
    '221':'CloseBrace',
    '222':'SingleQuote'
}

var isIE = /msie/i.test(navigator.userAgent) &! /opera/i.test(navigator.userAgent);

function GetKey(eventArg){
    var eventInfo = eventArg||window.event;
    return keyList[String(eventInfo.keyCode)];
}

function AddLoadEventToDocument(eventFunction){
    var insertBefore = arguments[1]||false;
    if(IsNull(windowLoadQueue)){
        windowLoadQueue = new EventQueue();
        if(IsNotNull(window.onload))
            windowLoadQueue.append(window.onload);
        window.onload = _runWindowLoadEvents;
    }
    if(insertBefore)
        windowLoadQueue.prepend(eventFunction);
    else
        windowLoadQueue.append(eventFunction);
}

function AddScript(scriptLocation){
    var headTag = GetFirstTagOfType("head");
    var newScript;
    
    if(IsNotNull(headTag) && !ScriptIsLoaded(scriptLocation)){
        newScript      = document.createElement("script")
        newScript.type = "text/ecmascript";
        newScript.src  = scriptLocation;
        headTag.appendChild(newScript);
    }
}

function FirstItemIn(arrayObject){
    var firstItem = 0;
    if(IsNotNull(arrayObject) && HasItems(arrayObject)){
        return arrayObject[firstItem];
    }
    return null;
}

function GetControl(id){
    return document.getElementById(id);
}

function GetFormControl(name){
    return document.forms[0][name];
}

function GetFirstTagOfType(tagName){
    var tags = document.getElementsByTagName(tagName);
    return FirstItemIn(tags);
}

function HasItems(arrayObject){
    return arrayObject.length>0;
}

function IntegerDivide(numerator, denominator){
    return (numerator - (numerator%denominator))/denominator;
}

function IsNotNull(objectReference){
    return objectReference != null;
}

function IsNull(objectReference){
    return objectReference == null;
}

function IsNumeric(value){
    if (typeof(IsNaN) != "undefined")
        return !IsNaN(value);
    else 
        return /^\-?\d+(\.\d+)?$/.test(String(value));
}

function ScriptIsLoaded(scriptLocation){
    var index   = 0;
    var scripts = document.getElementsByTagName("script");

    for(index=0;index<scripts.length;index++){
        if(scripts[index].src.toLowerCase()==scriptLocation.toLowerCase()){
            return true;
        }
    }
    return false;
}

function ScrollToControl(id){
    var control = document.getElementById(id);
    control.scrollIntoView();
}

function SearchStringValue(name){
    var searchPairs = window.location.search.slice(1).split(/\&/g);
    var x=-1;
    while(++x<searchPairs.length){
        var pair = searchPairs[x].split(/\=/);
        if(FirstItemIn(pair).toLowerCase()==name.toLowerCase()){
            return pair[1];
        }
    }
    return null;
}

function _runWindowLoadEvents(){
    windowLoadQueue.execute()
}

/* -- Modify and extend base objects -- */


Array.prototype.inBounds = _arrayInBounds;
function _arrayInBounds(value){
    var thisArray = this;
    return (value>=0 && value<thisArray.length);
}

String.prototype.trim = _stringTrim;
function _stringTrim(){
    thisString = this;
    var whiteSpaceAtBeginningOrEndOfString = /(^\s+|\s+$)/g;
    return thisString.replace(whiteSpaceAtBeginningOrEndOfString,'');
}



/* -------------  Custom Objects  -------------------------- */

        
/* -- Queue Object -- */
//------------------------------------------------------------------------
function Queue(){
    var thisQueue = this;
    thisQueue.current = -1;
    thisQueue.items   = null;
    thisQueue.length  = 0;
}
Queue.prototype.inBounds     = _arrayInBounds;
Queue.prototype.moveFirst    = _queueMoveFirst;
Queue.prototype.moveNext     = _queueMoveNext;
Queue.prototype.movePrevious = _queueMovePrevious;
Queue.prototype.moveLast     = _queueMoveLast;
Queue.prototype.append       = _queueAppend;
Queue.prototype.prepend      = _queuePrepend;
Queue.prototype.getCurrent   = _queueGetCurrent;

function _queueMoveFirst(){
    var thisQueue = this;
    if(thisQueue.length>0){
        thisQueue.current = 0;
        return true;
    }
    return false;
}

function _queueMoveNext(){
    var thisQueue = this;
    if (++thisQueue.current<thisQueue.length){
        return true;
    }
    return false;
}
function _queueMovePrevious(){
    var thisQueue = this;
    if(--thisQueue.current>0){
        return true;
    }
    return false;
}

function _queueMoveLast(){
    var thisQueue = this;
    if(thisQueue.length>0){
        thisQueue.current = thisQueue.length-1;
        return true;
    }
    return false;
}

function _queueAppend(item){
    var thisQueue = this;
    if(thisQueue.items==null)
        thisQueue.items=[item];
    else
        this.items.push(item);
        
    this.length = this.items.length;
}

function _queuePrepend(item){
    var thisQueue = this;
    if(this.items==thisQueue)
        thisQueue.items=[item];
    else
        thisQueue.items.unshift(item);
        
    thisQueue.length = thisQueue.items.length;
}

function _queueGetCurrent(){
    var thisQueue = this;
    if(thisQueue.inBounds(thisQueue.current))
        return thisQueue.items[thisQueue.current];
    return null;    
}
//------------------------------------------------------------------------





/* -- Event Queue Object -- */
//------------------------------------------------------------------------

function EventQueue(){
    var thisEventQueue = this;
    Queue.apply(thisEventQueue, arguments);
}
EventQueue.prototype                = new Queue();
EventQueue.prototype.constructor    = EventQueue;
EventQueue.prototype.execute        = _eventQueueExecute;
EventQueue.prototype.executeCurrent = _eventQueueExecuteCurrent;

function _eventQueueExecute(){
    var thisEventQueue = this;
    while(thisEventQueue.moveNext()){
        thisEventQueue.executeCurrent();
    }
}

function _eventQueueExecuteCurrent(){
    var thisEventQueue = this;
    var eventToFire = thisEventQueue.getCurrent();
    eventToFire();
}
//------------------------------------------------------------------------

