function getXMLHTTPRequest() {
	var xRequest = null;
	if (window.XMLHttpRequest) {
		xRequest = new XMLHttpRequest(); //Mozilla/Safari
	} else if (window.ActiveXObject) {
		xRequest = new ActiveXObject("Microsoft.XMLHTTP"); //IE
	}

	return xRequest;
}			
			
function ajaxLogin() {
	var xmlHttp = getXMLHTTPRequest();
	var params = "userId=" + encodeURI( document.getElementById("UserID").value ) +
                  "&password=" + encodeURI( document.getElementById("Password").value ) +
                  "&loginUrl=" + encodeURI( document.location.href );
	xmlHttp.onreadystatechange= function() {
		stateChanged(xmlHttp);
	}
	xmlHttp.open("POST","ajaxLogin.html",true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.send(params);	
}
			
function stateChanged(req) {
	if (req.readyState == 4) {
		if (req.status == 200) {			
			var response  = trimString(req.responseText);
			var showService = null;
			var surveyIds = null;
			
			if(response.indexOf(",") != -1){
				var array = response.split(",");
				
				response = array[0];
				showService = array[1];		
				surveyIds = array[2];
			}
			
			if (response == "SESSION_CREATED_OK") {
				
		   		var newLoc = document.getElementById('RedirectUrl');
		   		
		   		if(showService == "Y"){
		   		// need to tag on redirect
		   			(document.location).href = "customPage.html?group=SURVEY&surveyIds=" + surveyIds + "&redirectTo=" + newLoc.value;		   			
		   		}		   		
		   		else{
		   			(document.location).href = newLoc.value;
		   		}		   		
			} else {
	  			document.write(req.responseText);
			}
        } else {
  			alert ("Error: " + req.responseText);
        }
	}
}

function goUserRegister(params) {
	var url = "register.html";
	if (params) {
		url += "?" + params;
	}	
	var width = 500; var height = 600;
	var coords = getCentreCoords(width, height);
	var props = "left=" + coords[0] + ",top=" + coords[1] + ",width=" + width + ",height=" + height;
	var win = window.open(url, '', props);
	win.focus();
}

function goForgottenPassword(params) {
	var url = "forgottenUP.html";
	if (params) {
		url += "?" + params;
	}	
	var width = 500; var height = 200;
	var coords = getCentreCoords(width, height);
	var props = "left=" + coords[0] + ",top=" + coords[1] + ",width=" + width + ",height=" + height;
	var win = window.open(url, '', props);
	win.focus();
}

function goLogout() {
	var xmlHttp = getXMLHTTPRequest();
	var params = "";
	xmlHttp.onreadystatechange= function() {
		logoutStateChanged(xmlHttp);
	}
	xmlHttp.open("POST","ajaxLogout.html",true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.send(params);
}

function logoutStateChanged(req) {
	// But are we bothered what req contains?
	var newLoc = document.getElementById('RedirectUrl');
	document.location.href = newLoc.value;
}
