var logInFormName = '';
var logInFunction = '';
function submitLogInDetails() {
	if ( !document.getElementById("email").value.match("^(.+)@(.+)\\.(.+)$") ) {
		alert('Please enter a valid email address');
		document.getElementById("email").focus();
	} else if ( document.getElementById("loginPassword").value.length < 5 ) {
		alert('Please enter a valid password');
		document.getElementById("loginPassword").focus();
	} else {
		if (typeof logInFormName == 'undefined' || !logInFormName ) {
			logInFormName = 'londonProForm';
		}
		AjaxSearches.LogIn(document.getElementById("email").value,document.getElementById("loginPassword").value,logInFormName,logInFunction);
	}
}

function submitAccountHoldersDetails() {
	if (typeof logInFormName == 'undefined' || !logInFormName ) {
		logInFormName = 'londonProForm';
	}
	AjaxSearches.LogIn(document.getElementById("email").value,document.getElementById("pass").value,logInFormName,logInFunction);
}

function _utf8_encode(string) {
	string = string.replace(/\r\n/g,"\n");
	var utftext = "";

	for (var n = 0; n < string.length; n++) {

		var c = string.charCodeAt(n);

		if (c < 128) {
		utftext += String.fromCharCode(c);
		}
		else if((c > 127) && (c < 2048)) {
			utftext += String.fromCharCode((c >> 6) | 192);
			utftext += String.fromCharCode((c & 63) | 128);
		}
		else {
			utftext += String.fromCharCode((c >> 12) | 224);
			utftext += String.fromCharCode(((c >> 6) & 63) | 128);
			utftext += String.fromCharCode((c & 63) | 128);
		}
	}
	return escape(utftext);
}

function postAjaxLogin(response)
{
	var userLoggedInColl = response.getElementsByTagName("userLoggedIn");
	var action = '';

	if(userLoggedInColl && userLoggedInColl.length > 0 && (userLoggedInColl[0]).firstChild.nodeValue == '1' )
	{
		var runFunctionName = response.getElementsByTagName("runFunction");

		if(runFunctionName && runFunctionName.length > 0 && runFunctionName[0].firstChild)
		{
			// As the page may not be being refreshed, update the user details on the page header first
			document.getElementById('homeLink').href = '/LondonPro';

			var userName = response.getElementsByTagName("Name");
			var welcomeString = 'Welcome';

			if(userName)
			{
				welcomeString += ', ' + userName[0].firstChild.nodeValue;
			}

			welcomeString += ' | ';

			// If this isn't set, there ain't much we can do so just set it to some arbitrary link
			var logOut = document.getElementById('logOutURI').value;
			var linkText = (logOut ? logOut : '/LondonPro');
			document.getElementById('welcomeText').innerHTML = welcomeString + "<a href='" + linkText + "' class='topNav'>Sign Out</a>";

			var runFunction = new Function(runFunctionName[0].firstChild.nodeValue);
			//swapLayers('newBlocked', 'loginLayer', 'wrapLogins');
			runFunction();
			logInFunction = '';
		}
		else
		{
			var submitFormName = 'londonProForm';
			var submitForm = response.getElementsByTagName("formName");

			if(submitForm && submitForm.length > 0 && submitForm[0].firstChild)
			{
				submitFormName = submitForm[0].firstChild.nodeValue;
			}

			if(document.forms[submitFormName])
			{
				document.forms[submitFormName].submit();
			}
		}
	}
	else
	{
		var actionColl = response.getElementsByTagName("action");

		if(actionColl && actionColl.length > 0 && actionColl[0].firstChild)
		{
			action = actionColl[0].firstChild.nodeValue;
		}

		var errorColl = response.getElementsByTagName("processingResult");

		if(errorColl && errorColl.length > 0)
		{
			alert(errorColl[0].firstChild.nodeValue);
		}
		else
		{
			alert('An unknown error has occurred');
		}

		if(action == 'sendMessage' && errorColl[0].firstChild.nodeValue == 'Email sent')
		{
			document.getElementById("userName").value='';
			document.getElementById("userMail").value='';
			document.getElementById("userTitle").value='';
			document.getElementById("userMessage").value='';
		}
	}
}

function setHiddenCheckbox(checkboxId,hiddenId) {
	var hidden = document.getElementById(hiddenId);
	var checkbox = document.getElementById(checkboxId);
	if ( checkbox && hidden ) {
		if ( checkbox.checked ) {
			hidden.value = '1';
		} else {
			hidden.value = '';
		}
	}
}

function windowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
	//Non-IE
	myWidth = window.innerWidth;
	myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	//IE 6+ in 'standards compliant mode'
	myWidth = document.documentElement.clientWidth;
	myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	//IE 4 compatible
	myWidth = document.body.clientWidth;
	myHeight = document.body.clientHeight;
  }
  return [myWidth, myHeight];
}

function positionLogin(wrapper){
		var windowParam = windowSize();
		var wrapperWidth = (wrapper.style.width.indexOf('px') ? wrapper.style.width.substring(0, wrapper.style.width.length - 2) : wrapper.style.width);
		wrapper.style.left = '' + (Math.floor(windowParam[0] / 2) - (wrapperWidth / 2)) + 'px';
		wrapper.style.top = '' + (Math.floor(windowParam[1] / 2) - 60) + 'px';
		wrapper.style.display = "block";
}

var CheckPressedKey = {
	keyused : function(e){
		if(!e){
			e = window.event;
		}

		if(e.keyCode == 13){
			submitLogInDetails();
		}

	}
}