// Common Variables...

var isIE = navigator.appName == 'Microsoft Internet Explorer';


// Common Functions...

function OpenWithCSS(sURL)
{
 HTML = '<iframe id="frm" src="' + sURL + '" width="1" height="1" frameborder="0"></iframe>' +
        '<script>\r\n' +
        'window.onload = function() {\r\n' +
        '       var oFrm = document.getElementById("frm");\r\n' +
        '       var oLinks = opener.document.getElementsByTagName("LINK");\r\n' +
        '       oldLinks = oFrm.contentWindow.document.getElementsByTagName("LINK");\r\n' +
        '       for(idx = 0; idx < oldLinks.length; idx++)\r\n' +
        '        oldLinks[idx].parentNode.removeChild(oldLinks[idx]);\r\n' +
        '       oBody = oFrm.contentWindow.document.getElementsByTagName("BODY")[0];\r\n' +
        '       for(idx = 0; idx < oLinks.length; idx++)\r\n' +
        '       {\r\n' +
        '        oLink = oFrm.contentWindow.document.createElement("LINK");\r\n' +
        '        oLink.href = oLinks[idx].href;\r\n' +
        '        oLink.rel = oLinks[idx].rel;\r\n' +
        '        oLink.type = oLinks[idx].type;\r\n' +
        '        oBody.appendChild(oLink);\r\n' +
        '       }\r\n' +
        '       document.title = oFrm.contentWindow.document.title;\r\n' +
        '       document.body.innerHTML = oBody.innerHTML;\r\n' +
        '      };\r\n' +
        '</script>';
 
 var oWin = window.open('', 'newWin', 'width=450, height=350, scrollbars=1, resizable=1');
 oWin.document.open();
 oWin.document.write(HTML);
 oWin.document.close();
}

function openWindow(lnk, wd, ht)
{
 newWin = open (lnk, "", "height=" + (ht == undefined ? 550 : ht) + ", width=" + (wd == undefined ? 550 : wd) + ", scrollbars=yes, resizable=yes, status=no, menubar=no, toolbar=no, location=no");
 newWin.focus();
}

function setUIMode(Mode)
{
 if(Mode == 'inactive')
 {
  oDUIDiv = document.createElement('DIV');
  oDUIDiv.id = 'DisableUI';
  oDUIDiv.innerHTML = 'Processing...';
  oDUIDiv.style.backgroundColor = '#CCCCCC';
  oDUIDiv.style.color = '#FF0000';
  oDUIDiv.style.cursor = 'wait';
  oDUIDiv.style.font = 'bold 20pt Arial, sans-serif';
  oDUIDiv.style.left = '0px';
  oDUIDiv.style.top = '0px';
  oDUIDiv.style.width = document.body.scrollWidth;
  oDUIDiv.style.height = document.body.scrollHeight;
  oDUIDiv.style.position = 'absolute';
  oDUIDiv.style.paddingLeft = document.body.scrollWidth / 2 - 35 + 'px';
  oDUIDiv.style.paddingTop = document.body.scrollHeight/ 2 - 25 + 'px';
  oDUIDiv.style.opacity = 0.5;
  oDUIDiv.style.filter = 'alpha(opacity=50)';
  oDUIDiv.style.zIndex = 999;
  document.getElementsByTagName('BODY')[0].appendChild(oDUIDiv);
  
 }
 else
  document.getElementsByTagName('BODY')[0].removeChild(document.getElementById('DisableUI'));
}

/* EXAMPLE
   ŻŻŻŻŻŻŻ
   objXHR = getXHRObject();
   objXHR.open("GET", 'index.aspx?Command=ws_Register', true);
   objXHR.callback = calbackFunc;
   objXHR.send(null);
*/

function getXHRObject(asyncMode)
{
 var XHR;

 if (window.XMLHttpRequest)
  XHR = new XMLHttpRequest();     // Firefox, Mozilla, Safari, ....
 else if (window.ActiveXObject)
  XHR = new ActiveXObject("Microsoft.XMLHTTP");  // Internet Explorer
 else
  XHR = false;

 if(asyncMode == undefined || asyncMode == true)
  XHR.onreadystatechange = processResponse;
  
 return XHR;
}

function processResponse()
{
 if(objXHR.readyState==4)
 {
  if(objXHR.status == 200)
  {
   Success = parseInt(objXHR.responseXML.firstChild.attributes[0].nodeValue);
   if(objXHR.responseXML.firstChild.childNodes.length > 0)
   {
    if(objXHR.responseXML.firstChild.firstChild.nodeName == 'Message')
    {
     Msg = objXHR.responseXML.firstChild.firstChild.firstChild.nodeValue;
     if(objXHR.hideMessage == undefined)
      alert(Msg);
    }
   }
   else
    Success = -1;
   
   if(objXHR.callback)
    objXHR.callback(Success);
  }
  else
   alert("Sorry! Some Error Occured!\nServer Returned: " + objXHR.statusText);
 }
}

function formatPhone(Evt)
{
 srcElem = isIE ? Evt.srcElement : Evt.target;
 Key     = Evt.keyCode != 0 ? Evt.keyCode : Evt.charCode;
 if((Key >= 48 && Key <= 57) || (Key >= 96 && Key <= 105))
 {
  Val = srcElem.value.replace(/[^0-9]/g, '');
  if(Val.length >= 10)
   Val = Val.substring(0, 9);
  srcElem.value = Val.replace(/^(\(?\d)/, '($1').replace(/^\((\d{3})\)?[ ]?/, '($1) ').replace(/\((\d{3})\)[ ]?(\d{3})-?/, '($1) $2-');
 }
 else if(!(Key == 13 || Key == 8 || Key == 9 || Key == 35 || Key == 36 || Key == 37 || Key == 39 || Key == 46))
 {
  if(isIE)
  {
   Evt.returnValue = false;
   Evt.cancelBubble = true;
  }
  else
  {
   Evt.preventDefault();
   Evt.stopPropagation();
  }
 }
}

/*

UNDER - DEVELOPMENT
===================

Example:

filterInput(event, '$0-9');

function filterInput(Evt, filterList)
{
 srcElem = isIE ? Evt.srcElement : Evt.target;
 Key     = Evt.keyCode != 0 ? Evt.keyCode : Evt.charCode;
 Result  = false;
 SpclChr = [';=+|-,./`(\\)\'', 59, 61, 107, 108, 109, 188, 190, 191, 192, 188, 188, 188, 188];
 
 SeqOpts = ['Key >= 48 && Key <= 57', 'Key >= 65 && Key <= 90', '(Key >= 97 && Key <= 122'];
 Tests   = ['Key == 8 || Key == 9 || Key == 13 || Key == 27 || Key == 35 || Key == 36 || Key == 37 || Key == 39 || Key == 46'];
 
 if(filterList.indexOf('0-9') != -1) Tests.push(SeqOpts[0]);
 if(filterList.indexOf('A-Z') != -1) Tests.push(SeqOpts[1]);
 if(filterList.indexOf('a-z') != -1) Tests.push(SeqOpts[2]);
 if()
 
 remList = filterList.replace(/0\-9|A\-Z|a\-z/g, '');
 Str = '';
 for(idx = 0; idx < remList.length; idx++)
 {
  Str += 'Char\t=> ' + remList.charAt(idx) + '\nA-Z\t=> ' + (/[A-Z]/.test(remList.charAt(idx)) !== false ? 'True' : 'False') + '\na-z\t=> ' + (/[a-z]/.test(remList.charAt(idx)) !== false ? 'True' : 'False') + '\n\n';
  Tests.push('Key == ' + (/[A-Z]/.test(remList.charAt(idx)) ? remList.charCodeAt(idx) + ' && Evt.shiftKey' : (/[a-z]/.test(remList.charAt(idx)) ? remList.charCodeAt(idx) - 32 : remList.charCodeAt(idx))));
 }
 alert(Str);
 
 TestResults = 'Test Results [' + Key + ']\n================\n\n';
 for(idx = 0; idx < Tests.length; idx++)
 {
  TestResults += Tests[idx] + ' : ' + (eval(Tests[idx]) ? 'True' : 'False') + '\n';
  Result = Result || eval(Tests[idx]);
 }
 
 alert(TestResults);
 
 if(!Result)
 {
  if(isIE)
  {
   Evt.returnValue = false;
   Evt.cancelBubble = true;
  }
  else
  {
   Evt.preventDefault();
   Evt.stopPropagation();
  }
 }
}
*/


// Useful Prototypes

String.prototype.trim = function() {

    return(this.replace(/^\s+/,'').replace(/\s+$/,''));

}



Array.prototype.exists = function (x, ignoreCase) {

    x = ignoreCase ? x.toLowerCase() : x;


    for (var i = 0; i < this.length; i++) {

        if ((ignoreCase ? this[i].toLowerCase() : this[i]) == x) return i;

    }


    return -1;

}



Array.prototype.map = function(fnc) {

    var a = new Array(this.length);


    for (var i = 0; i < this.length; i++) {

        a[i] = fnc(this[i]);

    }


    return a;

}



Array.prototype.equals = function(arr) {

    if (this.length != arr.length) return false;


    for (var i = 0; i < arr.length; i++) {

        if (this[i].compareArrays) { //likely nested array

            if (!this[i].compareArrays(arr[i])) return false;

            else continue;

        }

        if (this[i] != arr[i]) return false;

    }


    return true;

}


