//If machindID in url authenticate as managed service.
if (location.href.indexOf('machineID=') != -1 || location.href.indexOf('txtemail=') != -1)
	self.location = "http://cw.cmpcs.com/v4_6_release/services/system_io/Portal/processLogin.rails?" + location.href.split('?')[1];
var gotoParams = "";
if (top.location != self.location)
	top.location = "http://cw.cmpcs.com/v4_6_release/services/system_io/Portal/Default.aspx";

function emailPassword() {
	var strEmail = document.frmLogin.txtemail.value;

	if (strEmail.length == 0 || strEmail.indexOf("@") < 0)
		alert("Please enter your email address.");
	else
		self.location = "http://cw.cmpcs.com/v4_6_release/services/system_io/Portal/EmailPassword.aspx?e=" + strEmail;
}

function testCookies() {
	document.cookie = 'WM_acceptsCookies=yes';
	if(document.cookie == '') {
		alert("This site requires cookies to be enabled.\nPlease enable cookies in your browser's options page.");
		document.frmLogin.btnSubmit.disabled = true;
		return;
	}
	document.cookie = 'WM_acceptsCookies=yes; expires=now';
	document.getElementById("txtemail").focus();

	if (self.location.href.indexOf("goto=") > 0)
	{
		var whereTo = self.location.href.substring(self.location.href.indexOf("goto=") + 5);
		if (whereTo.indexOf("&") > 0)
			whereTo = whereTo.substring(0, whereTo.indexOf("&"));

		if (whereTo == "newsr")
			gotoParams = "newsr";
		if (!isNaN(parseInt(whereTo, 10)))
			gotoParams = parseInt(whereTo, 10);
	}

	document.frmLogin.gotoparams.value = gotoParams;
	parseCookie();
}

function parseCookie() {
	if (!document.cookie || self.location.href.indexOf("newuser") > 0)
		return;

	var aryText = (unescape(document.cookie)).split(";");
	var strEmail = "";
	var strPassword = "";

	for (var idx = 0; idx < aryText.length; idx++)
	{
		var temp = aryText[idx].split("=");

		if (temp[0].toLowerCase().replace(/ /g, "") == "email" && temp[1])
			strEmail = temp[1];
		else if (temp[0].toLowerCase().replace(/ /g, "") == "password" && temp[1])
			strPassword = temp[1];

		if (strEmail.length > 0 && strPassword.length > 0)
		{
			if (strEmail == "undefined" || strPassword == "undefined")
				return;
			self.location = "http://cw.cmpcs.com/v4_6_release/services/system_io/Portal/ProcessLogin.aspx?" + (gotoParams == "" ? "" : "goto=" + gotoParams) + "&d="+ Math.random();
			return;
		}
	}
}


