
var passText; 
var passScreenSize;
//called at onLoad event of LogIn.aspx
function InitLogon() 
{
	//AdjustWindow();
	LoadHandler();
	if(passScreenSize == 'true')
	{
	    AdjustWindow();
	}
	
}
//changeed this to txtUserId, top field on LogIn .aspx
function LoadHandler()
{
	//txtRememberMeFunction.value is used to set level of login automation
	//values expected here are "ID", "PASSWORD", "GOTOPAGE, and "NO"
	passText = document.frmLogIn.txtRememberMeFunction.value;
	DisplayCookieEval();
}

function DisplayCookieEval()
{	
	//if passText is not no pass through
	if(passText.toUpperCase() != "NO")
	{
		//if passText is password then do this, otherwise, just do the other
		if(passText.toUpperCase() == "PASSWORD")
		{
			//decrypt password here
			var cryptText = Decrypt(GetCookie("Password"));
			document.frmLogIn.txtPassword.value = cryptText
		}
		document.frmLogIn.txtUserID.value = GetCookie("ID");
		if (GetCookie("Company").length > 0)
		{
		   document.frmLogIn.txtCompanyDomain.value = GetCookie("Company");
		}
		document.frmLogIn.ddlGotoPage.value = GetCookie("GotoPage");
		document.frmLogIn.chkRememberMe.checked = (GetCookie("checkbox1state") == "true");
		if (document.frmLogIn.txtUserID.value == "")
		{
			document.frmLogIn.txtUserID.focus();
		}
		else if (document.frmLogIn.txtPassword.value == "")
		{
			document.frmLogIn.txtPassword.focus();
		}
		else
		{
			document.frmLogIn.txtUserID.focus();
		}	
	}
	
	if((GetCookie("checkbox2state") == "true") || (GetCookie("checkbox2state") == ""))
	{
	    document.frmLogIn.chkFullScreen.checked = "true";
	    passScreenSize = 'true';
	}
	else
	{
	    document.frmLogIn.chkFullScreen.checked = false;
	    passScreenSize = 'false';
	}
	
}

function RememberMeCheck()
{
    
//	if(document.frmLogIn.checkbox.checked)
	if(passText.toUpperCase() != "NO")
	{
		if(document.frmLogIn.chkRememberMe.checked)
		{ 	
			var exp  = new Date();
			var oneyearfromnow= exp.getTime() + (365 * 24 * 60 * 60 * 1000);
			exp.setTime(oneyearfromnow);
			
			SetCookie("ID", document.frmLogIn.txtUserID.value, exp);
			if(passText.toUpperCase() == "PASSWORD")
			{
				SetCookie("Password", document.frmLogIn.txtPassword.value, exp);
			}
			SetCookie("Company", document.frmLogIn.txtCompanyDomain.value, exp);
			SetCookie("GotoPage", document.frmLogIn.ddlGotoPage.value, exp);
			SetCookie("checkbox1state", true, exp);
			if(GetCookie("Company")!= document.frmLogIn.txtCompanyDomain.value)
				alert(document.frmLogIn.txtEnableCookies.value);
		}
		else
		{ 	var exp  = new Date();
			var oneyearfromnow= exp.getTime() + (365 * 24 * 60 * 60 * 1000);
			exp.setTime(oneyearfromnow);
			SetCookie("ID", "", exp);
			SetCookie("Password", "", exp);
			SetCookie("Company", "", exp);
			SetCookie("GotoPage", "", exp);
			SetCookie("checkbox1state", false, exp);
		}
	}
	if(document.frmLogIn.chkFullScreen.checked == true)
	{
	    SetCookie("checkbox2state", true, exp);
	    AdjustWindow();
	}
	else
	{
	    SetCookie("checkbox2state", false, exp);
	}
}

function ChangePassValidation()
{
	var pw1=document.frmLogIn.requiredOldPassword.value;
	var pw2=document.frmLogIn.requiredNewPassword.value;
	var pw3=document.frmLogIn.requiredConfirmNewPassword.value;
	if(pw1 == pw2)
	{
		alert(document.frmLogIn.newAndOldPassword.value);
		document.frmLogIn.requiredNewPassword.value = "";
		document.frmLogIn.requiredConfirmNewPassword.value = "";
		document.frmLogIn.requiredNewPassword.focus();
		return false;
	}
	if (pw2 != pw3)
		{
		alert (document.frmLogIn.newAndConfirmedPassword.value);
		document.frmLogIn.requiredNewPassword.value = "";
		document.frmLogIn.requiredConfirmNewPassword.value = "";
		document.frmLogIn.requiredNewPassword.focus();
		return false;
		}
	 return true;
}

function GetCookieVal (offset) 
{
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1) endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) 
{
	
	var retVal;
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) 
	{
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
			return GetCookieVal(j);
			i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return "" ;
}

function SetCookie (name, value, expires) 
{
	//encrypt if name == Password
	if (name.toUpperCase() == "PASSWORD")
	{
		value = Encrypt(value);
	}
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	//var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	document.cookie = name + "=" + escape (value) +
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
	((path == null) ? "" : ("; path=" + path)) +
	((domain == null) ? "" : ("; domain=" + domain)) +
	((secure == true) ? "; secure" : "");
}

function LogonOnSubmit(which) 
{
//doing field validation on form
//	a = fieldValidation(which);
//	if (a) 
//	{
		RememberMeCheck();
		SetPageSize();
//	}
//	return a;
}

function AdjustWindow() 
{
	window.moveTo(0,0);
	window.resizeTo(screen.availWidth, screen.availHeight);
	SetPageSize();
	
//    if(IsIEBrowser)
//    {
//        window.resizeTo(screen.availWidth,screen.availHeight);
//    }
//    else
//	  {
//		top.outerWidth = screen.availWidth;
//		top.outerHeight = screen.availHeight;
//	    
//	  }	
}

// Allows for No. of rows to be displayed in etimecard according to screen resolution.
function SetPageSize() 
{
	w = screen.availWidth;
	switch (w) 
	{
		case 640: pageSizeEd = 2; pageSizeVw = 2; break;
		case 800: pageSizeEd = 4; pageSizeVw = 4; break;
		case 1024: pageSizeEd = 10; pageSizeVw = 10; break;
		case 1152: pageSizeEd = 14; pageSizeVw = 14; break;
		case 1280: pageSizeEd = 18; pageSizeVw = 18; break;
		default: pageSizeEd = pageSizeVw = 20;
	}
	
	document.frmLogIn.txtEditPageSize.value = pageSizeEd;
	document.frmLogIn.txtViewPageSize.value = pageSizeVw;
}

function ChangePassValidation2()
{
	pw1= document.forms[0].requirednewpasswd.value;
	pw2=document.forms[0].requiredcfrmnewpasswd.value;
	if (pw1 != pw2)	
	{
		alert (document.frmLogIn.newAndConfirmedPassword.value);
		return false;
	}	
	else
		return true;

}

/*
function init_pwdexpired() 
{ 
	document.frmLogIn.requirednewpasswd.focus();
}

function init_pwdexpiring() 
{ 
	document.frmLogIn.requirednewpasswd.focus();
}

function cancel_pwdexpired() 
{ 
	document.frmLogIn.submit();
}

function cancel_pwdexpiring() 
{ 
	document.frmLogIn.submit();
}

function init_changepwd() 
{ 
	document.frmLogIn.requiredOldPassword.focus();
}

function init_forgotpwd() 
{ 
	document.frmLogIn.txtCompanyDomain.focus();
}

function linkToPwdChange() 
{
	document.frmLogIn.changePassword.value='1';
	document.frmLogIn.submit();
}

function init_newuser() 
{
	document.frmLogIn.txtCompanyDomain.focus();
}
*/

function Encrypt(TextToEncrypt) 
{
	var stringOut = new String;
	var temp = new Array();
	var temp2 = new Array();
	var textSize = TextToEncrypt.length;
	for (i = 0; i < textSize; i++) 
	{
		rnd = Math.round(Math.random() * 122) + 68;
		temp[i] = TextToEncrypt.charCodeAt(i) + rnd;
		temp2[i] = rnd;
	}
	for (i = 0; i < textSize; i++) 
	{
		stringOut += String.fromCharCode(temp[i], temp2[i]);
	}
	return stringOut;
}

function Decrypt(TextToDecrypt) 
{
	var stringOut = new String;
	var temp = new Array();
	var temp2 = new Array();
	
	if (TextToDecrypt.length != null)
	{
		var textSize = TextToDecrypt.length;
	}
	for (i = 0; i < textSize; i++) 
	{
		temp[i] = TextToDecrypt.charCodeAt(i);
		temp2[i] = TextToDecrypt.charCodeAt(i + 1);
	}
	for (i = 0; i < textSize; i = i+2) 
	{
		stringOut += String.fromCharCode(temp[i] - temp2[i]);
	}
	return stringOut;
}

function RequestPassword(firstTimeUser)
{
	var url;
	if (firstTimeUser)
		url = "RequestPassword.aspx?From=FirstTimeUser&UserID=" + document.frmLogIn.txtUserID.value + "&Company=" + document.frmLogIn.txtCompanyDomain.value;
	else		
		url = "RequestPassword.aspx?From=ForgotPassword&UserID=" + document.frmLogIn.txtUserID.value + "&Company=" + document.frmLogIn.txtCompanyDomain.value;

	window.open(AddDynamicSessionParameter(url),'_self','',true);
}

///////////this for the popup detector

//var alertTitle = "Remove Pop-up Blockers";
//var btn1Txt = " OK ";

//function createCustomAlert(txt) 
//{
//    if(IsIEBrowser) 
//    {  
//	    // if the modalContainer object already exists in the DOM, bail out.
//	    if(document.getElementById("modalContainer")) 
//	    {
//	        return;
//	    }
//	    else
//	    {
//	        // create the modalContainer div as a child of the BODY element
//	        var myObj = document.getElementsByTagName ('body')[0].appendChild(document.createElement("div"));
//	        myObj.id = "modalContainer";
//	        // make sure its as tall as it needs to be to overlay all the content on the page
//	        myObj.style.height = document.documentElement.scrollHeight + "px";
//	        // create the DIV that will be the alert 
//	        var alertObj = myObj.appendChild(document.createElement("div"));
//	        alertObj.id = "alertBox";
//	        // MSIE doesnt treat position:fixed correctly, so this compensates for positioning the alert
//	        if(document.all && !window.opera) 
//            {    
//                alertObj.style.top = document.documentElement.scrollTop + "px";
//	            // center the alert box
//	            alertObj.style.left = (document.documentElement.scrollWidth - alertObj.offsetWidth)/2 + "px";
//	            // create an H1 element as the title bar
//	            h1 = alertObj.appendChild(document.createElement("h1"));
//	            h1.appendChild(document.createTextNode(alertTitle));
//            	
//                // create a paragraph element to contain the txt argument
//                var msg = alertObj.appendChild(document.createElement("p"));
//                msg.appendChild(document.createTextNode(txt));  
//            	
//            	
//	            // create an anchor element to use as the confirmation button.
//	            var btn1;
//	            btn1 = alertObj.appendChild(document.createElement("a"));
//	            btn1.id = "OKBtn";
//	            btn1.appendChild(document.createTextNode(btn1Txt));
//	            btn1.href = "#";
//	            btn1.value = "true";
//    	        
//	        }    
//	    }    

//        // set up the onclick event to remove the alert when the anchor is clicked
//	    btn1.onclick = function() 
//	    { 
//	        btnValue = '1';
//	        removeCustomAlert();
//	        return false;
//	    }
//    }
//	else
//    {   
//        //temporary fix for non IE
//        alert("Important! You have a pop-up blocker turned on, which means that some e-timecard screens are blocked from appearing, and the application will not function properly. After you click OK, please turn off all pop-up blockers for the e-timecard website.");
//    }
//}

////var btnValue;
//function removeCustomAlert() 
//{   
//	document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalContainer"));
//}

var l = (screen.width + 100);
var popUpBlocked;
var winOpen;
function CheckPopUp(tb)
{
    winOpen = window.open('','TestWin','width=1, height=1, top=0,left=' + l + 'px;' + ', scroolbars=no');

    if(winOpen)
    {
        popUpBlocked = false;
        setTimeout("winOpen.close();",0);
       
    }
    else
    {
        popUpBlocked = true;
    }

    if(popUpBlocked)
    {
        alert("Important! You have a pop-up blocker turned on, which means that some e-timecard screens are blocked from appearing, and the application will not function properly. After you click OK, please turn off all pop-up blockers for the e-timecard website.");
    }
}
