/*******************************************************************************
' Function Name		- 
'
' Description		Generic function to handle the Enter key press
'*******************************************************************************/
var ns = (!document.all) ? true : false
var ie = (document.all) ? true : false

var enterKey = 13;

if (ie) 
{
	var DOMItem = "document.all";
} 
else 
{
	var DOMItem = "document.forms[0]";
	window.captureEvents(Event.KEYPRESS);
}

document.onkeydown = keyPressed;

function keyPressed(e)
{
	var n;
	(ns) ? n=e.which : n=event.keyCode
	
	if (n==enterKey)
	{
		return fnSetCtrlEnterClick('ctl00_PlaceHolderMain_ccLogin_','hlLogin', n);
	}
}


/*******************************************************************************
' Function Name		fnCheckOS 
'
' Description		This function checks the operating system and browser
'*******************************************************************************/
function fnCheckOS()
{
	os = navigator.platform.substr(0,3);

	if (os != "Win" && os != "Mac") 
	{
		alert ("Our Eastman website works best with a Windows Operating System or a Macintosh System.");
	}
	else
	{
		browsername = navigator.appName.substr(0,5);

		if (browsername != "Micro")
		{
			alert ("Our Eastman website works best with Microsoft Internet Explorer version 5.0 or higher. Using any other browser may give unexpected results.");
		} 
	}
}

/*******************************************************************************
' Function Name		fnUpdateParent 
'
' Description		This function sets the window opener location
'*******************************************************************************/
function fnUpdateParent(newURL)
{
	opener.location.href = newURL;
}
 

/*******************************************************************************
' Function Name		fnFormValidation 
'
' Description		This function handles the validation of the form controls
'					and submits the user credentials.
'*******************************************************************************/
function fnFormValidation(obj,cookieDomain,CustomerCenterURL,containerElement)
{
	var browserName = navigator.appName

	//-- cookieEnabled only works in MSIE.
	if(browserName == "Microsoft Internet Explorer")
	{
		if (navigator.cookieEnabled==false)
		{
			alert("Your web browser must have cookies enabled to login to the Customer Center.");
			document.getElementById(obj+"txtUserName").value = "";
			document.getElementById(obj+"txtPassWord").value = "";
			document.getElementById(obj+"txtUserName").focus();
			return false;
		}
	}  
	
	
	if(fnTrimString(document.getElementById(obj+"txtUserName").value) == "")
	{
		alert("Please enter your UserID.");
		document.getElementById(obj+"txtUserName").focus();
		return false;
	}

	if(fnTrimString(document.getElementById(obj+"txtPassWord").value) == "")
	{
		alert("Please enter your Password.");
		document.getElementById(obj+"txtPassword").focus();
		return false;
	}

	if(document.getElementById(obj+"txtUserName").value.length > 20)
	{
		document.getElementById(obj+"txtUserName").value = "";
		document.getElementById(obj+"txtPassword").value = "";
		document.getElementById(obj+"txtUserName").focus();
		return false;
	}

	fnSetCookie(obj,cookieDomain);	
	var theForm = document.createElement("form");
	document.forms[0].USER.value = document.getElementById(obj+"txtUserName").value;
	document.forms[0].PASSWORD.value = document.getElementById(obj+"txtPassword").value;
	document.forms[0].CustURL.value = CustomerCenterURL;
		
    var CCWindow = window.open('/_layouts/EastmanGlobal/OpenCustCenter.aspx');
	if (!CCWindow)
		alert('Please disable the popup blocker to open up the Customer Center');
	else
		setTimeout('refresh()',3000);
	return false;
}

function refresh()
{
    window.location.href = window.location.href;
}


/*******************************************************************************
' Function Name		fnEncrypt 
'
' Description		This function encrypts text using the RSA encoder
'*******************************************************************************/
function fnEncrypt(text) 
{
	return rsaEncode([17],[148299941,57683965,5687041],text)
}

/*******************************************************************************
' Function Name		fnGetCookie 
'
' Description		This function gets a cookie from the request
'*******************************************************************************/
function fnGetCookie(sName)
{
	// cookies are separated by semicolons
	var aCookie = document.cookie.split("; ");
	
	for (var i=0; i < aCookie.length; i++)
	{
		// a name/value pair (a crumb) is separated by an equal sign
		var aCrumb = aCookie[i].split("=");
		if (sName == aCrumb[0]) 
		return unescape(aCrumb[1]);
	}

	// a cookie with the requested name does not exist
	return null;
}


/*******************************************************************************
' Function Name		fnSetCookie 
'
' Description		This function sets a cookie with the user profile
'*******************************************************************************/
function fnSetCookie(obj,cookieDomain)
{
	
	var intCtrValue; 

	strDomain = cookieDomain;
	
	intCtrValue = fnGetCookie("LoginCount");
	
	if (intCtrValue == null)
		{intCtrValue = 0}
	
	intCtrValue = parseInt(intCtrValue)
	intCtrValue = intCtrValue + 1;
	
	strNameValue = document.getElementById(obj+"txtUserName").value;
	strPasswordValue = document.getElementById(obj+"txtPassWord").value;
	strText = strNameValue + ";" + strPasswordValue;
	
	strEnc = fnEncrypt(strText);
		
	//Set the cookie
	document.cookie = "Creds="+ escape(strEnc) + "; " +
	"; path=" +
	"; domain=" + strDomain + "; " + "secure";

	document.cookie = "USERID="+ escape(strNameValue) + "; " +
	"; path=" +
	"; domain=" + strDomain + "; "+"secure";

	document.cookie = "LoginCount="+ escape(intCtrValue) + "; " +
	"; path=" +
	"; domain=" + strDomain + "; "+"secure";

	document.cookie = "BLCOUNTER=" + escape(1) +
	"; path=" +
       "; domain=" + strDomain + "; "+"secure";

	return false;
}


/*******************************************************************************
' Function Name		fnSetEnterClick				
'
' Description		This function handles the enter key press. If enter key is 
'					pressed, it calls the click event of the control passed to it
'*******************************************************************************/
function fnSetCtrlEnterClick(obj,ControlToHandle, keyCode)
{
	var ctl = ControlToHandle;

   if(keyCode == 13)
    {
    	if (document.getElementById(obj+"txtUserName").value != "" && document.getElementById(obj+"txtPassWord").value !="")
    		document.getElementById(obj+ctl).onclick();
    	event.returnValue = false;
    }
}

/*******************************************************************************
' Function Name		fnTrimString				
'
' Description		Generic function to trim string
'*******************************************************************************/
function fnTrimString(str)
{
	
	var x;
	var TrimmedString = new String("");
	
	x=0;
	
	while(str.charAt(x) == ' ') x++;
	
	for(;x<str.length;x++) 
	{
		TrimmedString = TrimmedString + str.charAt(x);
	}
	
	str = TrimmedString;
	TrimmedString = "";
	
	x=str.length-1;
	
	while(str.charAt(x) == ' ') x--;
	
	for(var y=0;y<=x;y++) 
	{
		TrimmedString = TrimmedString + str.charAt(y);
	}
	return TrimmedString;
}
