isOK = false;
UserOK = false;
PassOK = false;
EmailOK = false;

function CheckName2(t) {
	UserOK = true;
	
    if (t.value.length < 3){
    	t.style.color="red";
        $("#UserS").html("<font color='red'>Please enter a username at least 3 characters long!</font>");
        isOK = false;
        UserOK= false;
    }

    if (t.value.length > 18){
    	t.style.color="red";
        $("#UserS").html("<font color='red'>Username cannot be longer than 18 characters!</font>");
        isOK = false;
        UserOK= false;
    }
    
	var handlerFunc = function(r) {
		result = r;
		
		if (result == 2){
			t.style.color="red";
			$("#UserS").html("<font color='red'>Username already exists! If you have forgotten your password <a href='/forgotpass.php'>click here</a>.</font>");
			UserOK= false;
		}else if (result == 1) {
			t.style.color="red";
			$("#UserS").html("<font color='red'>Username contains invalid characters!</font>");
		    UserOK= false;
		}else{
			t.style.color="green";
			$("#UserS").html("<font color='green'>Username OK!</font>");
			isOK = true;
			UserOK = true;
		}
	}
	
	if (UserOK){
		$.ajax({type: "POST",url: "/siteadmin/aFunctions.php",data: "uName="+t.value,success:handlerFunc});
	}
}



function CheckEMail(t) {
	EmailOK = true;
	
    if (!isValidEmail(t.value)){ 
         $("#EmailS").html("<font color='red'>Please enter a valid Email address!</font>");
		 t.style.color="red";
         isOK = false;
         EmailOK = false;
    }
	
	var handlerFunc = function(r) {
		result = r;
		
		if (result == 2){
			t.style.color="red";
			$("#EmailS").html("<font color='red'>Email already exists! If you havent recieved an activation email yet <a href='/resend.php?EMail1=" + t.value + "'>click here</a>.</font>");
			EmailOK = false;
		}else if (result == 1) {
			t.style.color="red";
			$("#EmailS").html("<font color='red'>Email contains invalid characters!</font>");
			EmailOK = false;
		}else{
			t.style.color="green";
			$("#EmailS").html("<font color='green'>Email OK!</font>");
			EmailOK = true;
		}
	}
	
	if (EmailOK){
		$.ajax({type: "POST",url: "/siteadmin/aFunctions.php",data: "email="+t.value,success:handlerFunc});
	}
}

function CheckPass2(t,stat) {
	PassOK = true;
	
    if (t.value.length < 5){
    	t.style.color="red";
        $(stat).html("<font color='red'>Please enter a password at least 5 characters long!</font>");
        isOK = false;
        PassOK = false; 
    }

    if (t.value.length > 18){
    	t.style.color="red";
        $(stat).html("<font color='red'>Password cannot be longer than 18 characters!</font>");
        isOK = false;
        PassOK = false;
	}
	
	if (PassOK){
		if (isLegal(t.value)) {
			t.style.color="green";
			$(stat).html("<font color='green'>Password OK!</font>");
			if ($("#RegPass1").val() == $("#RegPass2").val()){
				$("#RegPass2").css("color", "green");
				$("#PassS2").html("<font color='green'>Password OK!</font>");
			}
	        PassOK = true;
	        isOK = true;
		}else{
			t.style.color="red";
			$(stat).html("<font color='red'>Password contains invalid characters!</font>");
			isOK = false;
	        PassOK = false; 
		}
	}
	
	if ($("#RegPass1").val() != $("#RegPass2").val()){
		$("#RegPass2").css("color", "red");
		$("#PassS2").html("<font color='red'>Passwords do not match!</font>");
		isOK = false;
	    PassOK = false; 
	}
}

function CheckIt(){
	isOK = false;
	UserOK = false;
	PassOK = false;
	EmailOK = false;
	
    CheckName2(document.getElementById("RegUser1"))
    CheckPass2(document.getElementById("RegPass1"),"#PassS1")
    CheckPass2(document.getElementById("RegPass2"),"#PassS2")
	CheckEMail(document.getElementById("RegEmail1"))
	
    if (document.getElementById("Hear").value=="-- Please Select One --"){
       alert('Please let us know where you heard from us.');
       isOK = false;
    }
    
    if (!document.getElementById("Terms").checked){
       alert('You must agree to our terms of use by checking the box.');
       isOK = false;
    }
    
	if (UserOK && PassOK && EmailOK && isOK){
		return true;
	}else{
		alert("Please fill out the form properly!");
		return false;
	}
}

function isLegal(txt) {
	var invalids = "!£$%^&*()+={}]\|[~@:;'#?><,/ ";
	for(i=0; i<invalids.length; i++) {
		if(txt.indexOf(invalids.charAt(i)) >= 0 ) {
			return false;
		}
    }
	return true;
}
function isValidEmail(emailAddress) {
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
    return re.test(emailAddress);
}