var url_reg = "reg_val.php?"; // The server-side script
var url_bio = "get_bio.php?bio="; // The server-side script

function getHTTPObject() {
	var xmlhttp;

	if(window.XMLHttpRequest){
		xmlhttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject){
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		if (!xmlhttp){
			xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
	}
	return xmlhttp;	
}

var http = getHTTPObject(); // We create the HTTP Object

function handleHttpResponse() {	
	if (http.readyState == 4) {
		if(http.status==200) {
			// Get response from PHP script
			var results=http.responseText;
			// Split response into multiple values, ie: DIV id, class name
			var results_array = results.split("|");

			// If password also manipulate 2nd input field
			if (results_array[0] == "about_bio_popup") {
				document.getElementById(results_array[0]).innerHTML = results_array[2];			
			}
			else {
				
	
				// If password also manipulate 2nd input field
				if (results_array[1] == "password") {
					// Change DIV to confirmation
					document.getElementById(results_array[0]+"_text").innerHTML = results_array[2];
					// Change DIV to icon confirmation
					document.getElementById(results_array[0]+"_icon").innerHTML = results_array[4];
					// Change input field class to colour
					document.getElementById(results_array[1]).className = results_array[3];
					
					document.getElementById(results_array[0]+"_text").innerHTML = results_array[2];
					document.getElementById("password2").className = results_array[3];				
				}
				// Reload image verification
				else if (results_array[0] == "reload_verification") {
					document.getElementById(results_array[0]).innerHTML = results_array[2];
				}
				else {
					// Change DIV to confirmation
					document.getElementById(results_array[0]+"_text").innerHTML = results_array[2];
					// Change DIV to icon confirmation
					document.getElementById(results_array[0]+"_icon").innerHTML = results_array[4];
					// Change input field class to colour
					document.getElementById(results_array[1]).className = results_array[3];
				}
			}
		}
	}
}



/* Checks usernames existence */
function requestUsername(field) {
	// Loading indicator
	document.getElementById('res_user_icon').innerHTML = "<img src=\"images/icons/indicator.gif\">";
	// Get value from input
	var sId = document.getElementById("username").value;
	// Send input value to PHP script
	http.open("GET", url_reg + field + "=" + escape(sId), true);
	http.onreadystatechange = handleHttpResponse;
	http.send(null);
}

/* Checks email validity */
function requestEmail(field) {
	// Loading indicator
	document.getElementById('res_email_icon').innerHTML = "<img src=\"images/icons/indicator.gif\">";
	// Get value from input
	var sId = document.getElementById("email").value;
	// Send input value to PHP script
	http.open("GET", url_reg + field + "=" + escape(sId), true);
	http.onreadystatechange = handleHttpResponse;
	http.send(null);
}

/* Checks the passwords match */
function checkPassword(field) {
	// Loading indicator
	document.getElementById('res_password_icon').innerHTML = "<img src=\"images/icons/indicator.gif\">";
	// Get value from input
	var sId = document.getElementById("password").value;
	var sId2 = document.getElementById("password2").value;
	// Send input value to PHP script
	http.open("GET", url_reg + field + "=" + escape(sId) + "|" + escape(sId2), true);
	http.onreadystatechange = handleHttpResponse;
	http.send(null);
}

/* Checks verification */
function checkVerification(field) {
	// Loading indicator
	document.getElementById('res_verify_icon').innerHTML = "<img src=\"images/icons/indicator.gif\">";
	// Get value from input
	var sId = document.getElementById("verify").value;
	// Send input value to PHP script
	http.open("GET", url_reg + field + "=" + escape(sId), true);
	http.onreadystatechange = handleHttpResponse;
	http.send(null);
}

/* Reloads Verification */
function reloadVerification(field) {
	// Loading indicator
	document.getElementById('reload_verification').innerHTML = "<div style=\"width: 200px; height: 28px; background: #005aa6; text-align: center; padding-top: 12px;\"><img src=\"images/icons/indicator_blue.gif\"></div>";
	// Send input value to PHP script
	http.open("GET", url_reg + field + "=1", true);
	http.onreadystatechange = handleHttpResponse;
	http.send(null);
}

/* Reloads Verification */
function requestBio(sId) {
	document.getElementById('about_bio_popup').innerHTML = "<img src=\"images/icons/indicator.gif\">";
	//var sId = document.getElementById("message_id").value;
	http.open("GET", url_bio + escape(sId), true);
	http.onreadystatechange = handleHttpResponse;
	http.send(null);
}

/* AJAX Popup for approvals */
function openAJAXPopup(sId) {
	document.getElementById('about_bio_popup').innerHTML = "<img src=\"images/icons/indicator.gif\">";
	//var sId = document.getElementById("message_id").value;
	http.open("GET", url_bio + escape(sId), true);
	http.onreadystatechange = handleHttpResponse;
	http.send(null);
}