function getMethod(str) {
				$.ajax({
				type: 'GET',
				url: '/getuser.php',
				data:{q:str, r:(new Date().getTime())},
						  dataType:'xml',
						  success: function(xml) {
							$(xml).find("stat").each(function()
							{
		var li = parseInt($(this).find("a").text());
		if (li == 0) {
			alert("username already in use");
			$("#check1").html("<img src=\"/images/cross.gif\" alt=\"\" height=\"16\"/ width=\"16\"\/>");
		} else {
			validusername = true;
			$("#check1").html("<img src=\"/images/tick.gif\" alt=\"\" height=\"16\"/ width=\"16\"\/>");
		}
		enableSignupBtn();
		});
	  }
	});	
}

var pass = "";
var validusername = false;
var validpassword = false;
var validpassword2 = false;
var validfirstname = false;
var validsurname = false;
var validemail = false;
var validtcs = false;
	
function userCheck() {
	$("#check1").html("<img src=\"/images/loading.gif\" alt=\"\" height=\"16\"/ width=\"16\"\/>");
	var qa = $('#username').val();
	var qq = qa.match(/^[a-zA-z0-9_]{6,16}$/);
	if (qq) {
		getMethod(qa.toLowerCase());
	} else {			
		$("#check1").html("<img src=\"/images/cross.gif\" alt=\"\" height=\"16\"/ width=\"16\"\/>");
		if (qa) {
			alert("Username must be made up of 6-16 letters, numbers and underscores");
		}
		validusername = false;
	}
	enableSignupBtn();
}

function pwCheck() {
	$("#check2").html("<img src=\"/images/loading.gif\" alt=\"\" height=\"16\"/ width=\"16\"\/>");
	var qa = $('#password').val();
	pass = qa;
	var qq = qa.match(/^[a-zA-z0-9]{6,10}$/);
	if (qq) {
		$("#check2").html("<img src=\"/images/tick.gif\" alt=\"\" height=\"16\"/ width=\"16\"\/>");
		validpassword = true;
	} else {
		$("#check2").html("<img src=\"/images/cross.gif\" alt=\"\" height=\"16\"/ width=\"16\"\/>");
		if (qa) {
			alert("Password must be 6-10 letters and/or numbers");
		}
		validpassword = false;
	}
	enableSignupBtn();
}

function pwCheck2() {
	$("#check3").html("<img src=\"/images/loading.gif\" alt=\"\" height=\"16\"/ width=\"16\"\/>");
	var qa = $('#password2').val();
	var qq = qa.match(/^[a-zA-z0-9]{6,10}$/);
	if (qq && (qa == pass)) {			
		$("#check3").html("<img src=\"/images/tick.gif\" alt=\"\" height=\"16\"/ width=\"16\"\/>");			
		validpassword2 = true;
	} else {
		$("#check3").html("<img src=\"/images/cross.gif\" alt=\"\" height=\"16\"/ width=\"16\"\/>");
		if (qa == pass) {
			if (qa) {
				alert("Password must be 6-10 letters and/or numbers");
			}
		} else {
		alert("Passwords don't match");
		}
		validpassword2 = false;
	}
	enableSignupBtn();
}

function emailCheck() {
	$("#check4").html("<img src=\"/images/loading.gif\" alt=\"\" height=\"16\"/ width=\"16\"\/>");
	var qa = $('#email').val();
	var qq = qa.match(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);//fix
	if (qq) {			
		$("#check4").html("<img src=\"/images/tick.gif\" alt=\"\" height=\"16\"/ width=\"16\"\/>");			
		validemail = true;
	} else {
		$("#check4").html("<img src=\"/images/cross.gif\" alt=\"\" height=\"16\"/ width=\"16\"\/>");
		validemail = false;
	}
	enableSignupBtn();
}

function firstnameCheck() {
	$("#check10").html("<img src=\"/images/loading.gif\" alt=\"\" height=\"16\"/ width=\"16\"\/>");
	var firstname = $('#firstname').val();
	var qq = firstname.match(/^[a-zA-z]+$/);//fix chinese?
	if (qq && (firstname != "")) {			
		$("#check10").html("<img src=\"/images/tick.gif\" alt=\"\" height=\"16\"/ width=\"16\"\/>");			
		validfirstname = true;
	} else {
		$("#check10").html("<img src=\"/images/cross.gif\" alt=\"\" height=\"16\"/ width=\"16\"\/>");
		validfirstname = false;
	}
	enableSignupBtn();
}

function surnameCheck() {
	$("#check11").html("<img src=\"/images/loading.gif\" alt=\"\" height=\"16\"/ width=\"16\"\/>");
	var surname = $('#surname').val();
	var qq = surname.match(/^[a-zA-z\. \'\-]+$/);//fix chinese?
	if (qq && (surname != "")) {			
		$("#check11").html("<img src=\"/images/tick.gif\" alt=\"\" height=\"16\"/ width=\"16\"\/>");			
		validsurname = true;
	} else {
		$("#check11").html("<img src=\"/images/cross.gif\" alt=\"\" height=\"16\"/ width=\"16\"\/>");
		validsurname = false;
	}
	enableSignupBtn();
}

function tcsCheck() {
	if ($('#check9').attr("checked")) {
		validtcs = true;
	} else {
		validtcs = false;
	}
	enableSignupBtn();
}

function enableSignupBtn() {
	if (validusername && validpassword && validpassword2 && validemail && validtcs && validfirstname && validsurname && validdc && validchurchname) {
		$("#submit").removeAttr("disabled");
	} else {
		$("#submit").attr("disabled", true);
	}
}

function showBoxes() {
	alert("If your church is not listed, please get in touch and we will most likely add it very quickly!");
}

