//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Old Browser detected, You should consider updgrading!");
	}
}

//Our XmlHttpRequest object to get the auto suggest
var searchReq = getXmlHttpRequestObject();



//Starts the AJAX request.
function checkPreviousCust(email) {
	

	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		   if (email != ''){
		   searchReq.open("GET", 'ajax/check_previous_cust.php?email=' + email, true);
		   searchReq.onreadystatechange = handleUserDetails; 
		   searchReq.send(null);
		   }else{
		   alert('Please enter your email address');
		   }


	}		
}


//Starts the AJAX request.
function checkUsernameExists(username) {

	if (searchReq.readyState == 4 || searchReq.readyState == 0) {

		   searchReq.open("GET", 'ajax/check_previous_cust.php?username=' + username, true);
		   searchReq.onreadystatechange = handleUserDetails; 
		   searchReq.send(null);

	}		
}



//Starts the AJAX request.
function retrieveQuestion(username) {

	if (searchReq.readyState == 4 || searchReq.readyState == 0) {

		   if (username != ''){
		   var str = escape(document.getElementById('username').value);
		   searchReq.open("GET", 'ajax/check_previous_cust.php?question=' + str, true);
		   searchReq.onreadystatechange = handleUserDetails; 
		   searchReq.send(null);
		   }

	}		
}



//Starts the AJAX request.
function checkEmailExists(email) {
    //alert("Please ensure your email address  has been entered correctly\n" + email);
	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		   if (email != ''){
		   searchReq.open("GET", 'ajax/check_previous_cust.php?email=' + email, true);
		   searchReq.onreadystatechange = handleUserDetails; 
		   searchReq.send(null);
		   }else{
		   alert('Please enter your email address');
		   }


	}		
}



//Called when the AJAX response is returned.
function handleUserDetails() {

if (searchReq.readyState == 4) {
			var str = searchReq.responseText;
			if(str == "username_exists"){
				alert("Sorry, this username already exists in our database, please try another name or Login if you have previously registered this username with us.");				
			}else if(str == "email_exists"){
				alert("Sorry, this email address is already registered, please Login to access your account or use the password reset form to retrieve your password.");				
			}else if(str.substr(0,14) == "question_found"){
				document.getElementById('showquestion').style.display = 'inline'; 
				document.getElementById("question_display").innerHTML = "none"; 
				document.getElementById("question_display").innerHTML = str.substr(14); 
			}else if(str == "question_notfound"){
				alert("Sorry, we could not locate your username in our database, please try again. Note: you can try either your username or email address.");
				document.getElementById('showquestion').style.display = 'none'; 
			}else {
			exit;
			}
	
	}
}
