	var MsgWindow = null;

	function encode(strInput) {
		var strOutput = strInput;
		
		strOutput = strOutput.replace(/&/g,"&amp;");
		strOutput = strOutput.replace(/'/g,"&#39;");
		strOutput = strOutput.replace(/"/g,"&quot;");		
		strOutput = strOutput.replace(/</g,"&lt;");
		strOutput = strOutput.replace(/>/g,"&gt;");
		
		return strOutput;
	}

	function OpenMsgWindow(Msg) {
		try {
			MsgWindow = window.open("","msg", "height=50, width=400, resizable=no, left=" + (window.screen.availWidth - 400)/2 + ", top=200");
			MsgWindow.document.write("<center>" + Msg + "</center>");
		} catch (e) {;}		
	}

	function CloseMsgWindow() {
		try {
			MsgWindow.close();
		} catch (e) {;}
		try {
			MsgWindow = null;
		} catch (e) {;}
	}

	function validateLogin() {		
		var sRequest = "<Request>";	
		sRequest = sRequest + "<ClientWebName>" + encode(document.all("txtClientWebName").value) + "</ClientWebName>";			
		sRequest = sRequest + "<LoginName>" + encode(document.all("txtUserLoginName").value) + "</LoginName>";		
		sRequest = sRequest + "<Password>" + encode(document.all("txtUserPassword").value) + "</Password>";
		sRequest = sRequest + "<IPAddress>" + encode(document.all("txtIPAddress").value) + "</IPAddress>";
		sRequest = sRequest + "<Database>" + document.all("cboDatabase").value + "</Database>";	
		sRequest = sRequest + "</Request>";
		
		var httpRequest = null;
			
		try {
			httpRequest = new XMLHttpRequest;
		} catch (e) {
			CloseMsgWindow();
			alert ("Unable to create XMLHttpRequest component.  Please ensure you are running the latest version of your browser\n" + e.description);
			return 0;
		}		
		
		try {	
			httpRequest.open ("POST", "validateclientlogin.aspx", false);		
			httpRequest.send (sRequest);
		} catch (e) {CloseMsgWindow(); alert("A communications error occurred trying to validate your login.\n" + e); return 0;}
			
		if (httpRequest.status == 200) {
			//handle com return error here.
			var xmlDoc = httpRequest.responseXML;
			
			var node = xmlDoc.selectSingleNode("//Error");

			if (node != null) {
				CloseMsgWindow();
				alert(Sarissa.getText(node));
				return 0;
			}
				
			var node = xmlDoc.selectSingleNode("//Success");
					
			if (node == null) {
				CloseMsgWindow();
				alert("Unexpected Error: No return value.");
				return 0;
			} else {			
				return 1;
			}
		} else {
			//handle http error here
			CloseMsgWindow();
			alert ("A server error occurred trying to validate your login\n" + httpRequest.statusText);
			return 0;
		}
		return 1;			
	}

	function txtClientWebName_OnKeyPress(){
		if (event.keyCode ==13){ 
			document.getElementById("txtUserLoginName").focus();
		}
	}
			
	function txtUserLoginName_OnKeyPress(){
		if (event.keyCode ==13){ 
			document.getElementById("txtUserPassword").focus();
		}
	}
			
	function txtUserPassword_OnKeyPress(){
		if (event.keyCode ==13){ 
			document.getElementById("Submit").focus();
		}
	}
		
	function txtLogin_OnClick(){
		
		var lngDatabase = document.getElementById("cboDatabase").value;
		
		if (eval(lngDatabase) == 0){
			alert("Please select a country");
			return;
		}
		
		if (document.getElementById("txtClientWebName").value == "") {
			alert("Please Enter the Client Name.");
			return;
		}
		
		if (document.getElementById("txtUserLoginName").value == "") {
			alert("Please Enter Your Login Name.");
			return;
		}
		
		OpenMsgWindow('Please Wait While Your Login Is Being Processed.');
		document.getElementById("divWelcome").innerText = "Please wait while validating your login";	
		window.status = "Please wait while validating your login";	
				
		if (validateLogin() == 0) {
			document.getElementById("divWelcome").innerText = "Welcome to Market Pulse Web Reporting\nPlease enter your Company, User Name and Password";
			CloseMsgWindow();
			window.status = "Done.";
			return;
		}
		
		CloseMsgWindow();
		window.status = "Done.";
		
		var toDay = new Date();
				
		toDay.setYear(toDay.getFullYear() + 1);	
					
		document.cookie = "database=" + lngDatabase + ";expires=" + toDay + ";";			
		
		if ((document.getElementById("txtUserPassword").value.toUpperCase() == document.getElementById("txtUserLoginName").value.toUpperCase()) || (document.getElementById("txtUserPassword").value.toUpperCase == "PASS") || (document.getElementById("txtUserPassword").value.toUpperCase == "PASSWORD")){
			window.location.href = "changepassword.aspx?status=1";
		}
		else {				
			window.location.href = "menu.aspx";
		}	
	}
		
	function body_onload() {
		try {
			document.getElementById("txtClientWebName").focus();
		} catch (e) {;}
	}	