function checkName() {
	if($("#name").val() == "") {
		$("#errorName").text("Please enter your name.");
		$.ajax({
		   type: "GET",
		   url: homelink+"/otto/Utility/AjaxValidate.php",
		   data: "type=code&value=1",	   
		   success: function(msg){
			   $("#name").select();
			   $("#name").focus();
		   }
		 });		
		return false;
	} else {
		$("#errorName").text("");
		return true;
	}	
}
function checkEmail() {
	value = $("#email").val();
	if(value == "" || validateEmail(value) == false) {
		$("#errorEmail").text("Please enter a valid email.");
		$.ajax({
		   type: "GET",
		   url: homelink+"/otto/Utility/AjaxValidate.php",
		   data: "type=code&value=1",	   
		   success: function(msg){
			   $("#email").select();
			   $("#email").focus();
		   }
		 });		
		return false;
	} else {
		$("#errorEmail").text("");
		return true;
	}
}
function checkPhone() {
	if($("#phone").val() == "" || isNaN($("#phone").val())) {
		$("#errorPhone").text("Please enter a valid phone.");
		$.ajax({
		   type: "GET",
		   url: homelink+"/otto/Utility/AjaxValidate.php",
		   data: "type=code&value=1",	   
		   success: function(msg){
			   $("#phone").select();
			   $("#phone").focus();
		   }
		 });		
		return false;
	} else {
		$("#errorPhone").text("");
		return true;
	}
}

function checkPostcode() {	
	var value = $("#postcode").val();
	if($("#country").val() == "Australia" || $("#country").val() == "Aus" || $("#country").val() == "australia") {
		if((isNaN(value) || 999>value || value>9999 || value == "")) {			
			$("#errorPostcode").text("Please enter a valid postcode.");	
			$.ajax({
			   type: "GET",
			   url: homelink+"/otto/Utility/AjaxValidate.php",
			   data: "type=code&value=1",	   
			   success: function(msg){
				   $("#postcode").select();
				   $("#postcode").focus();
			   }
			 });							
			return false;
		} else {
			$("#errorPostcode").text("");
			return true;
		}
	} else {
		if(value == "") {
			$("#errorPostcode").text("Please enter a valid postcode.");	
			$.ajax({
			   type: "GET",
			   url: homelink+"/otto/Utility/AjaxValidate.php",
			   data: "type=code&value=1",	   
			   success: function(msg){
				   $("#postcode").select();
				   $("#postcode").focus();
			   }
			 });						
			return false;
		} else {			
			$("#errorPostcode").text("");
			return true;
		}
	}	
}
function checkCountry() {
	if($("#country").val() == "") {
		$("#errorCountry").text("Please enter your country.");
		$.ajax({
		   type: "GET",
		   url: homelink+"/otto/Utility/AjaxValidate.php",
		   data: "type=code&value=1",	   
		   success: function(msg){
			   $("#country").select();
			   $("#country").focus();
		   }
		 });		
		return false;
	} else {
		$("#errorCountry").text("");
		return true;		
	}
}
function checkCompany() {
	if($("#company").val() == "") {
		$("#errorCompany").text("Please enter your company.");
		$.ajax({
		   type: "GET",
		   url: homelink+"/otto/Utility/AjaxValidate.php",
		   data: "type=code&value=1",	   
		   success: function(msg){
			   $("#company").select();
			   $("#company").focus();
		   }
		 });		
		return false;
	} else {
		$("#errorCompany").text("");
		return true;
	}
}
function checkState() {
	if($("#state").val() == "") {
		$("#errorState").text("Please enter your state.");
		$.ajax({
		   type: "GET",
		   url: homelink+"/otto/Utility/AjaxValidate.php",
		   data: "type=code&value=1",	   
		   success: function(msg){
			   $("#state").select();
			   $("#state").focus();
		   }
		 });		
		return false;
	} else {
		$("#errorState").text("");
		return true;
	}
}
function checkCode() {
	code1 = $("#authinput").val();		
	if(code1 == "") {
		$("#errorCode").text("Please enter security code.");
	} else {
		$.ajax({
		   type: "GET",
		   url: homelink+"/otto/Utility/checkCode.php",
		   data: "authinput="+code1,	   
		   success: function(msg){
			   if(msg == "same") {
				   $("#errorCode").text("");
			   } else {
				   $("#errorCode").text("Incorrect! Please try again.");
				   $("#authinput").select();
			       $("#authinput").focus();
			   }
			   
		   }
		 });
	}	
}

$(document).ready(function() {
	$("#submitLink").click(function(){
		var tag = true;		
		if(!checkPostcode()) {
			tag = false
		}
		if($("#state").val() == "") {
			$("#errorState").text("Please enter your state.");
			$.ajax({
			   type: "GET",
			   url: homelink+"/otto/Utility/AjaxValidate.php",
			   data: "type=code&value=1",	   
			   success: function(msg){
				   $("#state").select();
				   $("#state").focus();
			   }
			 });		
			tag = false;
		}
		if($("#country").val() == "") {
			$("#errorCountry").text("Please enter your country.");
			$.ajax({
			   type: "GET",
			   url: homelink+"/otto/Utility/AjaxValidate.php",
			   data: "type=code&value=1",	   
			   success: function(msg){
				   $("#country").select();
				   $("#country").focus();
			   }
			 });		
			tag = false;
		}
		if(!checkCompany()) {
			tag = false
		}
		if(!checkPhone()) {
			tag = false
		}
		if(!checkEmail()) {
			tag = false
		}												
		if(!checkName()) {
			tag = false
		}	
		code1 = $("#authinput").val();		
		if(code1 == "") {
			$("#errorCode").text("Please enter security code.");
		} else {
			$.ajax({
			   type: "GET",
			   url: homelink+"/otto/Utility/checkCode.php",
			   data: "authinput="+code1,	   
			   success: function(msg){				   
				   if(msg == "same") {
					   $("#errorCode").text("");
					   if(tag == true) {	
					   		
							$("#registrationForm").submit();
						}
				   } else {
					   $("#errorCode").text("Incorrect! Please try again.");
					   $("#authinput").select();
					   $("#authinput").focus();
				   }
				   
			   }
			 });
		}					
	});
	$("#formsubmit").click(function(){
		var tag = true;				
		if(!checkPostcode()) {
			tag = false
		}
		if($("#state").val() == "") {
			$("#errorState").text("Please enter your state.");
			$.ajax({
			   type: "GET",
			   url: homelink+"/otto/Utility/AjaxValidate.php",
			   data: "type=code&value=1",	   
			   success: function(msg){
				   $("#state").select();
				   $("#state").focus();
			   }
			 });		
			tag = false;
		}
		if($("#country").val() == "") {
			$("#errorCountry").text("Please enter your country.");
			$.ajax({
			   type: "GET",
			   url: homelink+"/otto/Utility/AjaxValidate.php",
			   data: "type=code&value=1",	   
			   success: function(msg){
				   $("#country").select();
				   $("#country").focus();
			   }
			 });		
			tag = false;
		}
		if(!checkCompany()) {
			tag = false
		}
		if(!checkPhone()) {
			tag = false
		}
		if(!checkEmail()) {
			tag = false
		}												
		if(!checkName()) {
			tag = false
		}		
		code1 = $("#authinput").val();		
		if(code1 == "") {
			$("#errorCode").text("Please enter security code.");
		} else {
			$.ajax({
			   type: "GET",
			   url: homelink+"/otto/Utility/checkCode.php",
			   data: "authinput="+code1,	   
			   success: function(msg){				   
				   if(msg == "same") {
					   $("#errorCode").text("");
					   if(tag == true) {	
					   		
							$("#registrationForm").submit();
						}
				   } else {
					   $("#errorCode").text("Incorrect! Please try again.");
					   $("#authinput").select();
					   $("#authinput").focus();
				   }
				   
			   }
			 });
		}	
		
		return false;				 
	});
});

function submitvalidate() {
	$("#submitLink").click(function(){
		var tag = true;		
		if(!checkPostcode()) {
			tag = false
		}
		if($("#state").val() == "") {
			$("#errorState").text("Please enter your state.");
			$.ajax({
			   type: "GET",
			   url: homelink+"/otto/Utility/AjaxValidate.php",
			   data: "type=code&value=1",	   
			   success: function(msg){
				   $("#state").select();
				   $("#state").focus();
			   }
			 });		
			tag = false;
		}
		if($("#country").val() == "") {
			$("#errorCountry").text("Please enter your country.");
			$.ajax({
			   type: "GET",
			   url: homelink+"/otto/Utility/AjaxValidate.php",
			   data: "type=code&value=1",	   
			   success: function(msg){
				   $("#country").select();
				   $("#country").focus();
			   }
			 });		
			tag = false;
		}
		if(!checkCompany()) {
			tag = false
		}
		if(!checkPhone()) {
			tag = false
		}
		if(!checkEmail()) {
			tag = false
		}												
		if(!checkName()) {
			tag = false
		}	
		code1 = $("#authinput").val();		
		if(code1 == "") {
			$("#errorCode").text("Please enter security code.");
		} else {
			$.ajax({
			   type: "GET",
			   url: homelink+"/otto/Utility/checkCode.php",
			   data: "authinput="+code1,	   
			   success: function(msg){				   
				   if(msg == "same") {
					   $("#errorCode").text("");
					   if(tag == true) {	
					   		
							$("#registrationForm").submit();
						}
				   } else {
					   $("#errorCode").text("Incorrect! Please try again.");
					   $("#authinput").select();
					   $("#authinput").focus();
				   }
				   
			   }
			 });
		}					
	});
	$("#formsubmit").click(function(){
		var tag = true;				
		if(!checkPostcode()) {
			tag = false
		}
		if($("#state").val() == "") {
			$("#errorState").text("Please enter your state.");
			$.ajax({
			   type: "GET",
			   url: homelink+"/otto/Utility/AjaxValidate.php",
			   data: "type=code&value=1",	   
			   success: function(msg){
				   $("#state").select();
				   $("#state").focus();
			   }
			 });		
			tag = false;
		}
		if($("#country").val() == "") {
			$("#errorCountry").text("Please enter your country.");
			$.ajax({
			   type: "GET",
			   url: homelink+"/otto/Utility/AjaxValidate.php",
			   data: "type=code&value=1",	   
			   success: function(msg){
				   $("#country").select();
				   $("#country").focus();
			   }
			 });		
			tag = false;
		}
		if(!checkCompany()) {
			tag = false
		}
		if(!checkPhone()) {
			tag = false
		}
		if(!checkEmail()) {
			tag = false
		}												
		if(!checkName()) {
			tag = false
		}		
		code1 = $("#authinput").val();		
		if(code1 == "") {
			$("#errorCode").text("Please enter security code.");
		} else {
			$.ajax({
			   type: "GET",
			   url: homelink+"/otto/Utility/checkCode.php",
			   data: "authinput="+code1,	   
			   success: function(msg){				   
				   if(msg == "same") {
					   $("#errorCode").text("");
					   if(tag == true) {	
					   		
							$("#registrationForm").submit();
						}
				   } else {
					   $("#errorCode").text("Incorrect! Please try again.");
					   $("#authinput").select();
					   $("#authinput").focus();
				   }
				   
			   }
			 });
		}	
		
		return false;				 
	});
}

function validateEmail(email)
{
// a very simple email validation checking. 
// you can add more complex email checking if it helps 
    if(email.length <= 0)
	{
	  return true;
	}
    var splitted = email.match("^(.+)@(.+)$");
    if(splitted == null) return false;
    if(splitted[1] != null )
    {
      var regexp_user=/^\"?[\w-_\.]*\"?$/;
      if(splitted[1].match(regexp_user) == null) return false;
    }
    if(splitted[2] != null)
    {
      var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
      if(splitted[2].match(regexp_domain) == null) 
      {
	    var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
	    if(splitted[2].match(regexp_ip) == null) return false;
      }// if
      return true;
    }
return false;
}