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; } 
}

function queryString(key)
{
 var page = new PageQuery(window.location.search); 
 return unescape(page.getValue(key)); 
}

function on_load()
{
 if(queryString('closeWin')=='1')
 {
  window.opener.window.location = "Login.aspx";
  self.close();
  return;
 }

 document.form1.txtUserName.focus();
}

function isIE()
{
 return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
}

function grayOut(vis, options) {
  // Pass true to gray out screen, false to ungray
  // options are optional.  This is a JSON object with the following (optional) properties
  // opacity:0-100         // Lower number = less grayout higher = more of a blackout 
  // zindex: #             // HTML elements with a higher zindex appear on top of the gray out
  // bgcolor: (#xxxxxx)    // Standard RGB Hex color code
  // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
  // Because options is JSON opacity/zindex/bgcolor are all optional and can appear
  // in any order.  Pass only the properties you need to set.
  var options = options || {};
  var zindex = options.zindex || 50;
  var opacity = options.opacity || 70;
  var opaque = (opacity / 100);
  var bgcolor = options.bgcolor || '#000000';
  var dark=document.getElementById('darkenScreenObject');
  if (!dark) {
    // The dark layer doesn't exist, it's never been created.  So we'll
    // create it here and apply some basic styles.
    // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
    var tbody = document.getElementsByTagName("body")[0];
    var tnode = document.createElement('div');           // Create the layer.
        tnode.style.position='absolute';                 // Position absolutely
        tnode.style.top='0px';                           // In the top
        tnode.style.left='0px';                          // Left corner of the page
        tnode.style.overflow='hidden';                   // Try to avoid making scroll bars            
        tnode.style.display='none';                      // Start out Hidden
        tnode.id='darkenScreenObject';                   // Name it so we can find it later
    tbody.appendChild(tnode);                            // Add it to the web page
    dark=document.getElementById('darkenScreenObject');  // Get the object.
  }
  if (vis)
  {
    // Calculate the page width and height
    //set the shader to cover the entire page and make it visible.
    dark.style.opacity=opaque;                      
    dark.style.MozOpacity=opaque;                  
    dark.style.filter='alpha(opacity='+opacity+')';
    dark.style.zIndex=zindex;        
    dark.style.backgroundColor=bgcolor;  
    dark.style.width='100%';
    dark.style.height='100%';
    dark.style.display='block';                          
  } else {
     dark.style.display='none';
  }
}

function ourValidate_form( numchecks)
{
 var u, p, t;
 u = document.getElementById("txtUserName").value;
 if (u.length < 6)
 {
  alert( "User name is too short");
  return false;
 }
 p = document.getElementById("txtUserPass").value;
 if (p.length < 6)
 {
  alert( "Password is too short");
  return false;
 }

 if (numchecks == 3)
 {
  t = document.getElementById("TokenTextBox").value;
  if (t.length < 6)
  {
   alert( "Token is too short");
   return false;
  }
 }
 
 if (numchecks == 2)
 {
     ajaxPost( "LoginHandler.ashx", "op=get_token\r\nuser=" + u + "\r\npass=" + p);
 }
 else if (numchecks == 3)
 {
     ajaxPost( "LoginHandler.ashx", "op=authenticate\r\nuser=" + u + "\r\npass=" + p + "\r\ntoken=" + t); 
 }

 return true;
}

function ajaxPost( strURL, params)
{
 var gbAsyncAjax = true;
 var xmlHttp = gethttprequest();
 if (xmlHttp == null)
  return;

 params = params.replace( /\u00A3/g, "%A3");
 params = escape(params).replace( /\+/g, "%2B");
 
 if (xmlHttp.overrideMimeType)
  xmlHttp.overrideMimeType("text/plain");
  
 if (gbAsyncAjax == true)
  xmlHttp.onreadystatechange = function(){ OnAjaxStateChange(xmlHttp); }
  
 xmlHttp.open("POST", strURL, gbAsyncAjax);  

 xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
 xmlHttp.setRequestHeader("Content-length", params.length);
 xmlHttp.setRequestHeader("Timeout", 1000*1800);
 //xmlHttp.setRequestHeader("Connection", "close");
 xmlHttp.send(params);

 if (gbAsyncAjax == false)
  ProcessAjaxResponse("" + xmlHttp.responseText);
}

function ProcessAjaxResponse( responseText)
{
 var processed;
 var strTemp = responseText;
 strTemp = unescape(strTemp.replace( /\+/g, "%20"));

 var strArray = strTemp.split("\r\n");

 for (cc = 0; cc < strArray.length; cc++)
 {
  var strPair = strArray[cc].split("=");
 
  processed = false;
  if (typeof PageAjaxHandler == 'function')
   processed = PageAjaxHandler( strPair[ 0], strPair[ 1]);

  if (processed == false)
   processed = DefaultAjaxHandler( strPair[ 0], strPair[ 1]);
 }
}

function doDialog( message, ctrlToFocus)
{
 alert(message);
 if (ctrlToFocus)
  ctrlToFocus.focus();
}

function DefaultAjaxHandler( strName, strValue)
{
 var nPendingCount = 0, nToPayFor = 0;
 var confirmText, pendingArray;
 switch(strName)
 {
  case "error_message":
   strValue = strValue.replace( /<br \/>/g, "\r\n");
   doDialog( strValue, null);
   return true;

  case "success":
   strValue = strValue.replace( /<br \/>/g, "\r\n");  
   doDialog( strValue, null);
   return true;
   
  case "reload":
   location.reload();
   return true;
   
  case "redirect":
   window.location = strValue;
   return true;   
   
  case "eval":
   eval( strValue);
   return true;
 }

 return false;
}

function OnAjaxStateChange( httpRequestObject)
{
 if(httpRequestObject.readyState==4)
 {
  if (httpRequestObject.status==200)
   ProcessAjaxResponse( httpRequestObject.responseText);
  else
  {
   var status, respText;

   try
   {
    status = httpRequestObject.status;
    
    try
    {
     respText = '\r\n\r\n' + httpRequestObject.responseText;
    }
    catch (e)
    {
     respText = '';
    }
   }
   catch (e)
   {
    status = 'UNKNOWN';
   }
 
   if (httpRequestObject.status == 12152)
    alert( "Connection to server was lost before operation completed.");
   else
    alert( "httpRequestObject.readyState=" + httpRequestObject.readyState + ", httpRequestObject.status=" + status + respText);
  }
 }
}

function gethttprequest()
{
 var httpObj = null;
 try
 {  // Firefox, Opera 8.0+, Safari
  httpObj=new XMLHttpRequest();
 }
 catch (e)
 {  // Internet Explorer
  try
  {
   httpObj=new ActiveXObject("Msxml2.XMLHTTP");
  }
  catch (e)
  {
   try
   {
    httpObj=new ActiveXObject("Microsoft.XMLHTTP");
   }
   catch (e)
   {
    alert("Your browser does not support AJAX!");
   }
  }
 }
 return httpObj;
}

function toggleButtons()
{
 var u, p, t, b1, b2;

 u = document.getElementById("txtUserName").value;
 p = document.getElementById("txtUserPass").value;
 t = document.getElementById("TokenTextBox").value;

 b1 = document.getElementById("GetTokenButton");
 b2 = document.getElementById("LoginButton");

 if (u.length >= 6 && p.length >= 6)
 {
  if (b1)
   b1.disabled=false;

   if (b2)
   {
    if (t.length >= 6)
      b2.disabled=false;
    else
      b2.disabled=true;
   }
 }
 else if (b1)
 {
   b1.disabled=true;
   b2.disabled=true;   
 }
}
