function createXMLHttp() {
	if (typeof XMLHttpRequest != 'undefined')
		return new XMLHttpRequest();
	else if (window.ActiveXObject) {
		var avers = ["Microsoft.XmlHttp", "MSXML2.XmlHttp","MSXML2.XmlHttp.3.0", "MSXML2.XmlHttp.4.0","MSXML2.XmlHttp.5.0"];
		for (var i = avers.length -1; i >= 0; i--) {
			try {
				httpObj = new ActiveXObject(avers[i]);
				return httpObj;
			} catch(err) {
			
			}
		}
	}
	throw new Error('XMLHttp (AJAX) not supported');
}

function processRequest(ajaxObj,sDisplayField) {
	if (ajaxObj.readyState == 4) {
		if (ajaxObj.status != 200) {
			alert('Unable to access the server currently. Please try again later');
		} else {
			document.getElementById(sDisplayField).innerHTML = ajaxObj.responseText.substring(1);
			if (ajaxObj.responseText.substring(0,1) == "0" || ajaxObj.responseText.substring(0,1) == 0) {
				document.getElementById(sDisplayField).className = "error";
			}	else if (ajaxObj.responseText.substring(0,1) == "1" || ajaxObj.responseText.substring(0,1) == 1) {
				document.getElementById(sDisplayField).className = "success";
			} else {
				document.getElementById(sDisplayField).className = "notice";
			}
		}
	}
}


function sendCode() {
	if (document.getElementById("CellNumber").value == "") {
		alert("Please specify your Cell Number");
		return;
	}	

	if (document.getElementById("Network").value == "") {
		alert("Please select your network");
		return;
	}	

	sendRequest("CellNumber",document.getElementById("CellNumber").value+"||"+document.getElementById("Network").value,"joinCheckerStatus");
}

function checkCode() {
	sendRequest("Code",document.getElementById("Code").value+"||"+document.getElementById("CellNumber").value,"joinCheckerStatus2");
}

function checkNickname() {
sendRequest("Nickname",document.getElementById("Nickname").value,"NicknameStatus");
}

function checkIDNumber() {
	sendRequest("IDNumber",document.getElementById("IDNumber").value,"IDNumberStatus");
}

function sendRequest(sType, sRequestValue, sDisplayField) {
	var ajaxObj = createXMLHttp();
	ajaxObj.open("POST", 'checkserver.php', true);	
	ajaxObj.onreadystatechange = function() {processRequest(ajaxObj, sDisplayField);}
	ajaxObj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	ajaxObj.send("type="+sType+"&value="+sRequestValue);

}

function checkForm() {
	if (document.getElementById("CellNumber").value == "") {
		alert("Please specify your Cell Number");
		return;
	}	

	if (document.getElementById("Network").value == "") {
		alert("Please select your network");
		return;
	}	

	if (document.getElementById("Code").value == "") {
		alert("Please specify the registration code you were sent");
		return;
	}

	if (document.getElementById("Nickname").value == "") {
		alert("Please specify your Nickname");
		return;
	}

	if (document.getElementById("IDNumber").value == "") {
		alert("Please specify your ID number");
		return;
	}

	if (document.getElementById("JoinPassword").value == "") {
		alert("Please specify your password");
		return;
	}

	if (document.getElementById("PasswordConfirm").value == "") {
		alert("Please confirm your password");
		return;
	}

	if (document.getElementById("JoinPassword").value != document.getElementById("PasswordConfirm").value) {
		alert("Please ensure that your passwords match");
		return;
	}

	if (document.getElementById("FirstName").value == "") {
		alert("Please specify your name");
		return;
	}

	if (document.getElementById("LastName").value == "") {
		alert("Please specify your surname");
		return;
	}

	if (document.getElementById("Description").value == "" || document.getElementById("Description").value == " ") {
		alert("Please specify a short description about yourself");
		return;
	}

	if (document.getElementById("Email").value != document.getElementById("EmailConfirm").value) {
		alert("Please ensure that your email addresses match");
		return;
	}

	if ((document.getElementById("GenderMale") && document.getElementById("GenderMale").checked == false) && (document.getElementById("GenderFemale") && document.getElementById("GenderFemale").checked == false) && (document.getElementById("GenderCouple") && document.getElementById("GenderCouple").checked == false)) {
		alert("Please specify your sex");
		return;
	}

	if (document.getElementById("PostalCode").value == "") {
		alert("Please specify your postal code");
		return;
	}

	if (document.getElementById("Terms").checked != true) {
		alert("You need to accept to the terms and conditions to complete the registration");
		return;
	}

	
	document.getElementById("joinForm").submit();
}
