var req;
var formValid = true;

function loadXMLDoc(url) {
	req = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest && !(window.ActiveXObject)) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
	if(req) {
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.send("");
	}
}

function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            if (req.responseText == "true"){
				var emailAddress = document.getElementById("email").value;
				setMsg("email-msg", "<b>Email address in use.</b> Please choose another.");
				formValid = false;
				emailValid = false;
			}else{
				emailValid = true;
			}
        } else {
            //alert("There was a problem retrieving the XML data:\n" + req.statusText);
        }
    }
}

function checkEmailExists(){
	var emailAddress = document.getElementById("email").value;
	if (checkEmail(emailAddress) != false){
		emailAddress = emailAddress.replace("@", "~~");
		loadXMLDoc(base_url + "login/checkuser/" + emailAddress);
		setMsg("email-msg", "Email address:");
	}
}
	

function setMsg(idName, msg){
	if (idName == ""){
		return false;
	}else{
		document.getElementById(idName).innerHTML = msg;
	}
}

function checkPassword(){
	var password  = document.getElementById("password").value;
	if (password == ""){
		setMsg("password-msg", "<b>Password is required</b>");
		formValid = false;
		return false;
	}
	if (password.length <= 4){
		setMsg("password-msg", "<b>Password should be more than 4 characters</b>");
		formValid = false;
		return false;
	}else{
		setMsg("password-msg", "Password: ");
	}
}

function checkPassword2(){
	var password  = document.getElementById("password").value;
	var password2  = document.getElementById("password2").value;
	if (password2 == ""){
		setMsg("password2-msg", "<b>Please repeat password</b>");
		formValid = false;
		return false;
	}
	if (password2.length <= 4){
		setMsg("password2-msg", "<b>Password should be more than 4 characters</b>");
		formValid = false;
		return false;
	}
	if (password != password2){
		setMsg("password2-msg", "<b>Passwords do not match</b>.");
		formValid = false;
		return false;
	}else{
		if (password2 == ""){
			setMsg("password2-msg", "<b>Password should be more than 4 characters</b>");
		}else{
			setMsg("password2-msg", "Password again:");
		}
	}
}

function verifyEmail(email){
	var status = false;
	var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
	if (email.search(emailRegEx) == -1) {
		return false;
	}
}

function checkEmail(){
	var email =  document.getElementById("email").value;
	if (email == ""){
		setMsg("email-msg", "<b>Email address required</b>");
		formValid = false;
		return false;
	}
	if (verifyEmail(email) == false){
		setMsg("email-msg", "<b>Email address invalid</b>");
		formValid = false;
		return false;
	}
	setMsg("email-msg", "Email address: ");
}
function checkFirstname(){
	var forename = document.getElementById("firstname").value;
	if (forename == ""){
		setMsg("firstname-msg", "<b>Firstname is required</b>");
		formValid = false;
	}else{
		setMsg("firstname-msg", "Firstname: ");
	}
}

function checkSurname(){
	var surname =  document.getElementById("surname").value;
	if (surname == ""){
		setMsg("surname-msg", "<b>Surname is required</b>");
		formValid = false;
	}else{
		setMsg("surname-msg", "Surname: ");
	}
}

function checkAddress(){
	var add1 =  document.getElementById("address").value;
	if (add1 == ""){
		setMsg("address-msg", "<b>Address is required</b>");
		formValid = false;
	}else{
		setMsg("address-msg", "Address line 1: ");
	}
}

function checkAddress2(){
	var add2 =  document.getElementById("address2").value;
	if (add2 == ""){
		setMsg("address2-msg", "<b>Address 2 is required</b>");
		formValid = false;
	}else{
		setMsg("address2-msg", "Address line 2: ");
	}
}

function checkCity(){
	var city =  document.getElementById("city").value;
	if (city == ""){
		setMsg("city-msg", "<b>Town/City is required</b>");
		formValid = false;
	}else{
		setMsg("city-msg", "Town/City: ");
	}
}

function checkCounty(){
	var county =  document.getElementById("county").value;
	if (county == ""){
		setMsg("county-msg", "<b>County is required</b>");
		formValid = false;
	}else{
		setMsg("county-msg", "County: ");
	}
}

function checkCountry(){
	return;
	var country = document.getElementById("country").options[document.getElementById("country").selectedIndex].value;
	if (country == ""){
		setMsg("country-msg", "<b>Country is required</b>");
		formValid = false;
	}else{
		setMsg("country-msg", "Country: ");
	}
}

function checkTitle(){
	var title = document.getElementById("title").options[document.getElementById("title").selectedIndex].value;
	if (title == ""){
		setMsg("title-msg", "<b>Title is required</b>");
		formValid = false;
	}else{
		setMsg("title-msg", "Title: ");
	}
}

function checkPostcode(){
	var postcode =  document.getElementById("postcode").value;
	if (postcode == ""){
		setMsg("postcode-msg", "<b>Postcode is required</b>");
		formValid = false;
	}else{
		setMsg("postcode-msg", "Postcode: ");
	}
}

function resetMessages(){
	setMsg("title-msg", "Title:");
	setMsg("firstname-msg", "Firstname:");
	setMsg("surname-msg", "Surname:");
	setMsg("password-msg", "Password:");
	setMsg("password2-msg", "Password again:");
	setMsg("email-msg", "Email address:");
	//setMsg("country-msg", "Country: ");
	setMsg("address-msg", "Address line 1: ");
	setMsg("postcode-msg", "Postcode: ");
	setMsg("address2-msg", "Address line 2: ");
	setMsg("city-msg", "Town/City: ");
	setMsg("county-msg", "County: ");
}


function verifyLoginForm(){
	var email =  document.getElementById("login-email").value;
	var password =  document.getElementById("login-password").value;
	var l = true;
	if (email == ""){
		l = false;
		setMsg("login-email-msg", "<font color=\"#ff0000\"><b>Email address: </b></font>");
	}
	if (password == ""){
		l = false;
		setMsg("login-password-msg", "<font color=\"#ff0000\"><b>Password: </b></font>");
	}
	if (l){
		//if(document.loginform.onsubmit()){//this check triggers the validations
		//	document.loginform.submit();
		//}
	}else{
		return false;
	}
}

function forgotpass(){
	window.open(base_url + "login/forgotpass/", "Forgotten password","menubar=no,location=no,width=620,height=220,toolbar=no");
	return;
}

function sendtofriend(){
	window.open(base_url + "login/sendtofriend/true/", "Send to friends","menubar=no,location=no,width=620,height=430,toolbar=no");
	return;
}

//function changePass(){
//	window.open(base_url + "login/changepass/true/", "Change your password","menubar=no,location=no,width=620,height=430,toolbar=no");
//	return;
//}

function verifyRegForm(){
	resetMessages();
	formValid = true;
	var agreeToTerms = document.getElementById("terms-opt-in").checked;
	
	checkEmail();
	if (!emailValid){
		setMsg("email-msg", "<b>Email address in use.</b> Please choose another.");
		formValid = false;
		
	}
	
	checkFirstname();
	checkSurname();
	checkPassword();
	checkPassword2();
	checkTitle();
	checkAddress();
	//checkAddress2();
	checkCity();
	//checkCounty();
	checkPostcode();
	//checkCountry();
	
	if (!agreeToTerms){
		alert("You must agree to the terms and conditions.");
		return false;
	}
	
	if (formValid == false){
		alert("There was an error with some of the form entries, please try again.");
		return false;
	}else{
		//everthing checks out..
		// proceed with form submission
	}
}

function setValue(idName, msg){
	if (idName == ""){
		return false;
	}else{
		document.getElementById(idName).value = msg;
	}
}

function initRegForm(){
	if (error){
		//setMsg("error-msg", error_msg + "<br />");
		setValue("firstname", firstname);
		setValue("surname", surname);
		setValue("housenum", housenum);
		setValue("address", address);
		setValue("address2", address2);
		setValue("city", city);
		setValue("county", county);
		setValue("postcode", postcode);
		setValue("mobile", mobile);
		if (guardian_opt_in == 1){
			document.getElementById("guardian-opt-in").checked = true;
		}
		if (third_party_opt_in == 1){
			document.getElementById("third-party-opt-in").checked = true;
		}
		document.getElementById("title").options[0].selected;
		document.getElementById("title").options[0].value = title;
		document.getElementById("title").options[0].text = title;
		//document.getElementById("country").options[0].selected;
		//document.getElementById("country").options[0].value = country;
		//document.getElementById("country").options[0].text = country;
	}
}