// JavaScript Document

function formValidator(){
	// Make quick references to our fields
	var first_mame = document.getElementById('first_name');
	var last_name = document.getElementById('last_name');
	var email = document.getElementById('email');
	var mobile = document.getElementById('mobile');
	var phone = document.getElementById('phone');
	
	var username = document.getElementById('usr');
	var password = document.getElementById('pass');
	var repassword = document.getElementById('repass');
	
	var addressUrl = document.getElementById('address_url');
	
	
	// Check each input in the order that it appears in the form!
	if(isAlphabet(first_mame, "Please enter only letters for your first name")){
     if(isAlphabet(last_name, "Please enter only letters for your last name")){
		if(emailValidator(email, "Please enter a valid email address")){
		  if(isNumeric(phone, "Please enter a valid telephone number (only numbers)")){
		   if(isNumeric(mobile, "Please enter a valid mobile phone (only numbers)")){
		  	if(isAlphanumeric(username, "Please enter only letters and numbers for your username")){
			   if (username.value.length>=4){ 
			     	if(isAlphanumeric(password, "Please enter only letters and numbers for your password")){
						if (password.value==repassword.value){
							window.location = addressUrl.value+"/icontact";
						 	return true;
						}
						else { alert ("The password are not the same"); } 
				    }//password
			   }//username.value.length
			   else { alert ("Username too short"); }
			 }//username
		    }//mobile
		   }//phone
		  }//email
	   }//last_name
	}//first_name
	
	return false;
	
}
/*function NewWindow(strURL){
  window.open(strURL, "_blank", "width=460, height=285, scrollbars=no, resizable=no, toolbar=no, location=no, directories=no, status=no, menubar=no, copyhistory=no,
			  left=350, top=150, screenX=0, screenY=0");
}*/
function newsletterValidator(){
	// Make quick references to our fields
	var newsEmail = document.getElementById('newsletter');
	var addressUrl = document.getElementById('address_url');
	
	// Check each input in the order that it appears in the form!
	if(emailValidator(newsEmail, "Please enter a valid email address")){
	 	 window.open (addressUrl.value+"/pop_up_news.php", "_blank","width=460, height=285, scrollbars=no, resizable=no, toolbar=no, location=no, directories=no, status=no, menubar=no, copyhistory=no, left=350, top=150, screenX=0, screenY=0"); 
	return true;
	}//email
	
	return false;
	
}

function isEmpty(elem, helperMsg){
	if(elem.value.length == 0){
		alert(helperMsg);
		elem.focus(); // set the focus to this input
		return true;
	}
	return false;
}

function isNumeric(elem, helperMsg){
	var numericExpression = /^[0-9\s]+$/;
	if(elem.value.match(numericExpression)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isAlphabet(elem, helperMsg){
	var alphaExp = /^[a-zA-Z\s]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isAlphanumeric(elem, helperMsg){
	var alphaExp = /^[0-9a-zA-Z\s]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function lengthRestriction(elem, min, max){
	var uInput = elem.value;
	if(uInput.length >= min && uInput.length <= max){
		return true;
	}else{
		alert("Please enter between " +min+ " and " +max+ " characters");
		elem.focus();
		return false;
	}
}

function madeSelection(elem, helperMsg){
	if(elem.value == "Please Choose"){
		alert(helperMsg);
		elem.focus();
		return false;
	}else{
		return true;
	}
}

function emailValidator(elem, helperMsg){
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(elem.value.match(emailExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}
