function processXML(xml)
{
	var sMessage = "";
	//Login Fails
	if (xml.getElementsByTagName("connexionResult")[0].attributes.getNamedItem("success").nodeValue == "0")
	{
		sMessage += 'Wrong login or password !';

		if (parseInt(xml.getElementsByTagName("connexionResult")[0].attributes.getNamedItem("wrongLogin").nodeValue) == "1")
		{
			alert(sMessage);
			return;
		}
		var iAttemptsLeft = parseInt(xml.getElementsByTagName("connexionResult")[0].attributes.getNamedItem("attemptsLeft").nodeValue);
		if (iAttemptsLeft > 0)
		{
			sMessage += '\n' + iAttemptsLeft + ' attempts left before this account will be locked.';
		}
		if (xml.getElementsByTagName("connexionResult")[0].attributes.getNamedItem("accountLocked").nodeValue == "1")
		{
			sMessage += '\nYour user account has been locked following 3 unsuccessful login attempts.\nPlease contact Masternaut Customer Service on 08444 150150 or your Administrator to unlock your account.';
		}
		
		if (sMessage != '')
		{
			alert(sMessage);
			document.getElementById("password").value = "";
		}
		return;
	}
	
	createCookie_("login", document.getElementById("login").value, 365);
	
	if (xml.getElementsByTagName("connexionResult")[0].attributes.getNamedItem("multiSiteLogin").nodeValue == "1")
	{
		window.location = 'Login_Site.aspx';
		return;
	}

	window.location = 'cc_greenscore.aspx';
}

function login()
{
	loadXMLDoc("login_xml.aspx?LOGIN=" + encodeURI(document.getElementById("login").value) + "&PASSWORD=" + encodeURI(document.getElementById("password").value), null);
}

function PressEnter()
{
	if (window.event.keyCode==13)
		login();
}

function getPassword() 
{
	window.open('http://www.masternaut.co.uk/mweb/login_getPass.aspx','Edit','toolbar=no,location=no,status=no,directories=no,menubar=no,resizable=no,scrollbars=no,width=400,height=250');
}

function readCookie_(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return "";
}

function createCookie_(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

var tmrRetrieveLoginTimer;

function retrieveLogin()
{
	if (document.getElementById("login") == null)
	{
		tmrRetrieveLoginTimer = setTimeout(retrieveLogin, 1000);
		return;
	}
	clearTimeout(tmrRetrieveLoginTimer);
	if ((document.getElementById("login").value == "") && (readCookie_("login") != "") && (readCookie_("login") != "null"))
		document.getElementById("login").value = readCookie_("login");
	
	if (document.getElementById("password") != null)
		document.getElementById("password").focus();
}

window.onload = retrieveLogin;
