var nonums = /^[0-9]*$/;
var xmlhttp=null;

// error message display
function showError(divId,errorText,x,y){
	var error = document.getElementById(divId);
	window.scrollTo(x,y);
	error.innerHTML = errorText;
}

 //trim functions
function jtrim(value) {
	return LTrim(RTrim(value));
}
function trim(value) {
	return LTrim(RTrim(value));
}
// Removes leading whitespaces
function LTrim(value) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}
// Removes ending whitespaces
function RTrim(value) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}

function validateJ1Form(){
	var state=document.myform.state.value;
	var city=document.myform.city.value;
	var otherCity=document.myform.otherCity.value;
	var year=document.myform.year.value;
	var company=document.myform.company.value;
	var radioCompany = false;
	var radioAccomodation = false;
	var radioLiving = false;
	var radioEmployment = false;
	var radioWork = false;
	var radioJ1ers = false;
	var radioLife = false;
	var radioRating = false;
	var jobCompany = document.myform.jobCompany.value;
	
	
	if(trim(state)==""){
		document.myform.state.focus();
		showError("errorRegister","Please choose a state",0,0);
		return false;
	}
	
	if(trim(city)==""){
		showError("errorRegister","Please choose a city from the drop down or enter it into Other if not available",0,0);
		return false;
	}
	
	if(trim(city)=="other" && trim(otherCity)==0){
		showError("errorRegister","Please choose a city from the drop down or enter it into Other if not available",0,0);
		return false;
	}
	
	for (i = 0; i < document.myform.rating.length; i++) {
		if (document.myform.rating[i].checked)
			radioRating = true; 
	}
	if (!radioRating) {
		showError("errorRegister","Please rate your city",0,0);
		return false;
	}
	
	if(year==""){
		showError("errorRegister","Please select the year of your J1",0,0);			
		return false;
	}
	
	if(trim(company)==""){
		showError("errorRegister","Please enter your J1 company",0,0);			
		return false;
	}
	
	if(trim(jobCompany)==0){
		showError("errorRegister","Please enter the name of the company you worked",0,0);			
		return false;
	}
	
	for (i = 0; i < document.myform.recommendCompany.length; i++) {
		if (document.myform.recommendCompany[i].checked)
			radioCompany = true; 
	}
	if (!radioCompany) {
		showError("errorRegister","Please rate your J1 company",0,0);
		return false;
	}

	for (i = 0; i < document.myform.accomodation.length; i++) {
		if (document.myform.accomodation[i].checked)
			radioAccomodation = true; 
	}
	if (!radioAccomodation) {
		showError("errorRegister","Please rate how easy it was to get accomodation",0,0);
		return false;
	}

	for (i = 0; i < document.myform.livingCosts.length; i++) {
		if (document.myform.livingCosts[i].checked)
			radioLiving = true; 
	}
	if (!radioLiving) {
		showError("errorRegister","Please rate the cost of living",0,0);
		return false;
	}
	
	for (i = 0; i < document.myform.easeEmployment.length; i++) {
		if (document.myform.easeEmployment[i].checked)
			radioEmployment = true; 
	}
	if (!radioEmployment) {
		showError("errorRegister","Please rate the availability of jobs",0,0);
		return false;
	}
	
	for (i = 0; i < document.myform.recommendWork.length; i++) {
		if (document.myform.recommendWork[i].checked)
			radioWork = true; 
	}
	if (!radioWork) {
		showError("errorRegister","Please rate the company you worked for",0,0);
		return false;
	}
	
	for (i = 0; i < document.myform.j1ers.length; i++) {
		if (document.myform.j1ers[i].checked)
			radioJ1ers = true; 
	}
	if (!radioJ1ers) {
		showError("errorRegister","Please rate how many J1ers weer around your city",0,0);
		return false;
	}
	
	for (i = 0; i < document.myform.sociallife.length; i++) {
		if (document.myform.sociallife[i].checked)
			radioLife = true; 
	}
	if (!radioLife) {
		showError("errorRegister","Please rate the social life of your city",0,0);
		return false;
	}
}

// user validation
function validateUserRegistrationForm(){
	var Name=document.myform.Name.value;
	var Surname=document.myform.Surname.value;
	var year=document.myform.dobYear.value;
	var month=document.myform.dobMonth.value;
	var date=document.myform.dobDay.value;
	var email=document.myform.email.value;
	var Remail=document.myform.Remail.value;
	var Password=document.myform.Password.value;
	var RPassword=document.myform.RPassword.value;
	var boxCheck = false;
	var username = document.myform.username.value;
	
	if(trim(username)==0){
		document.myform.username.focus();
		showError("errorRegister","Please choose a username",0,0);
		return false;
	}
	// AJAX Code
	
	xmlhttp=null;
	if (window.XMLHttpRequest) {// all modern browsers
	  xmlhttp=new XMLHttpRequest();
	}
	
	else if (window.ActiveXObject) {// for IE5, IE6
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	 }
	 
	if (xmlhttp!=null) {
		  xmlhttp.open('GET', '../js/test.php?username='+username, false);
		  xmlhttp.send(null);
	}
	
  	if (xmlhttp.status==200) {
		if(xmlhttp.responseText.indexOf('true') == -1) {
			document.myform.username.focus();
			showError("errorRegister","That username is already in use, please choose another",0,0);
			return false;
		}
	}
	
	// AJAX Code End
	
	if(trim(Name)==0){
		document.myform.Name.focus();
		showError("errorRegister","Please enter name",0,0);
		return false;
	}
	if(trim(Surname)==0){
		document.myform.Surname.focus();
		showError("errorRegister","Please enter your surname",0,0);
		return false;
	}
	if(date=="" || month=="" || year==""){
		showError("errorRegister","Please select correct date of birth",0,0);			
		return false;
	}
	if(trim(email)==""){
		document.myform.email.focus();
		showError("errorRegister","Please enter email",0,0);
		return false;
	}
	if(trim(Remail)==""){
		document.myform.Remail.focus();
		showError("errorRegister","Please enter email address twice",0,0);
		return false;
	}
	if (email.length >0) {
		 i=email.indexOf("@")
		 j=email.indexOf(".",i)
		 k=email.indexOf(",")
		 kk=email.indexOf(" ")
		 jj=email.lastIndexOf(".")+1
		 len=email.length
		 	if ((i>0) && (j>(1+1)) && (k==-1) && (kk==-1) && (len-jj >=2) && (len-jj<=3)) {
		 	}else {			 		
		 		error = "Please enter a correct email address. <b>" +	email + "</b> is invalid.";
				document.myform.email.focus();
				showError("errorRegister",error,0,0);
				return false;
		 	}
	 }	
	 if (Remail.length >0) {
		 i=Remail.indexOf("@")
		 j=Remail.indexOf(".",i)
		 k=Remail.indexOf(",")
		 kk=Remail.indexOf(" ")
		 jj=Remail.lastIndexOf(".")+1
		 len=Remail.length
		 	if ((i>0) && (j>(1+1)) && (k==-1) && (kk==-1) && (len-jj >=2) && (len-jj<=3)) {
		 	}else {			 		
		 		error = "Please enter a correct email address. <b>" +	Remail + "</b> is invalid.";
				document.myform.Remail.focus();
				showError("errorRegister",error,0,0);
				return false;
		 	}
	 }
	 if(email != Remail) {
		error = "Email addresses don't match";
		document.myform.Remail.focus();
		showError("errorRegister",error,0,0);
		return false;
	 }
	 
	 if(trim(Password)==0){
		document.myform.Password.focus();
		showError("errorRegister","Please enter your password",0,0);
		return false;
	}

	 if(trim(RPassword)==0){
		document.myform.RPassword.focus();
		showError("errorRegister","Please enter your password twice",0,0);
		return false;
	}
	
	if(Password!=RPassword){
		document.myform.RPassword.focus();
		showError("errorRegister","Passwords don't match",0,0);
		return false;
	}
		 
 	if (document.myform.confirm.checked) {
		boxCheck = true; 
	}
	if (!boxCheck) {
		showError("errorRegister","Please Agree to the terms & Conditions",0,0);
		return false;
	}
}
function Reset(){
	document.all.myform.reset();
}
