//////////////////////////////////////////////////////////////////////////
// Copyright 2007,2008. Paul Baker Integrations, Consulting, & Software L.L.C.
//
// No unauthorized use, modification, or distribution of this code is 
// permitted without written consent and/or contractual agreement.
//
//////////////////////////////////////////////////////////////////////////

var req;

String.prototype.trim = function() 
{
	a = this.replace(/^\s+/, '');
	return a.replace(/\s+$/, '');
};


function doLogon(e)
{
	var key;
	 if(window.event)
	 {
          key = window.event.keyCode;     //IE
     
	 }
	 else
	 {
          key = e.which;     //firefox
	 }
	 
     if(key == 13)
	 {
          Logon1();
	 }
	 else
	 {
          return true;
	 }

}

function Initialize()
{
	try
	{
		req = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(oc)
		{
			req = null;
		}
	}
	if(!req&&typeof XMLHttpRequest!="undefined")
	{
		req = new XMLHttpRequest();
	}
	
}


	
function Logon1()
{
	var le = document.getElementById("tbLogonEmail").value.trim();
	var lc = document.getElementById("tbLogonCode").value.trim();
	//alert(lp);
	//alert(ln);
	var now = new Date();
	var url = strLogonBaseURL + "?le=" + encodeURIComponent(le) + "&lc=" + encodeURIComponent(lc) + "&time=" + now.getTime();
	//alert(url);
	Initialize();
	
	if(req!=null)
	{
		req.onreadystatechange=Logon2;
		req.open("GET",url,true);
		req.send(null);
		
	}
	
}

function Logon2()
{
	if (req.readyState == 4)
	{
	// only if "OK"
	var theResult = document.getElementById("divStatus");
	//alert(req.status);
		if (req.status == 200)
		{
			//alert(req.responseText);
			var aResult = req.responseText.split(":");
			if(aResult[0]=="")
			{
				theResult.innerHTML = "<span class=\"RedTextBold\">Status: </span><span class=\"RedText\">There was an error authenticating. <br> Please verify your logon credentials and try again.</span><br>";
			}
			else
			{
				
				if(aResult[0].toLowerCase() != "true")
				{
					var errorContent = "<span class=\"RedTextBold\">Status: </span><span class=\"RedText\">There was an error authenticating. <br> Please verify your logon credentials and try again." 
					var errorText = aResult[1];
					if(errorText != '' || errorText != 'undefined')
					{
						errorContent += "<br>Error Message:  " + errorText; 
					}
					errorContent += "</span><br>";
					theResult.innerHTML = errorContent;
					
				}
				else
				{
					document.location = strRedirectURL;
				}
			}
		}
		else
		{
			theResult.innerHTML= "<span class=\"RedTextBold\">Status: </span><span class=\"RedText\">There was an error authenticating. <br> Could not connect to the authentication server.</span><br>";
		}
		
	}
		
}



function getCookieVal(strVal)
{
	var strResult = "";
	var strCookie = document.cookie;
	//alert(strCookie);
	var strKey = strVal + "=";
	var iKeyLen = strKey.length;
	var iUIDpos = strCookie.indexOf(strKey);
		if (iUIDpos != -1){
			var start = iUIDpos + iKeyLen;
			var end = strCookie.indexOf(";", start);
			if (end == -1) end = strCookie.length;
			strResult = (strCookie.substring(start, end))
		}
		else
		{
			strResult = "false";
		}
		//alert(strResult);
		return strResult.toLowerCase();
}

