// JavaScript Document

/**
 * S.Middleton	1/1/2008 (adapted from DHTML validation script SmartWebby.com (http://www.smartwebby.com/dhtml/)
 * 		- Amended to take minYear and maxYear as an argument
 *		- Changed format to yyyy-mm-dd
 *		- Amalgamated with email validation script from smartwebby
 *		- created new function for T&C's ..etc
 *		- added a wrapper (hardcoded some booking form variables to make it work!!) :((
 */

// Check the format of the email address
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;
	}
}

function echeck(str) {

		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail Address");
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail Address");
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail Address");
		    return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail Address");
		    return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail Address");
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail Address");
		    return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail Address");
		    return false;
		 }

 		 return true;				
	}

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function ValidateEmail() {

	var emailID=form.email;
	
	// Check if EMAIL has a value
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email Address");
		emailID.focus();
		return false;
	}
	
	// Check if EMAIL is correct format	
	if (echeck(emailID.value)==false){
		emailID.value="";
		emailID.focus();
		return false;
	}	

return true;

}


function ValidateNotBlanks(){
	
	var name=form.name;
	var pickup=form.pickup;
	var dropoff=form.dropoff;

	// Check if NAME has a value
	if ((name.value==null)||(name.value=="")){
		alert("Please enter your Full Name in the box provided");
		name.focus();
		return false;
	}

	// Check if PICKUP fields have values
	if ((pickup.value==null)||(pickup.value=="")){
		alert("You must enter your Pickup Point in the box provided");
		pickup.focus();
		return false;
	}

	// Check if DROPOFF field have values
	if ((dropoff.value==null)||(dropoff.value=="")){
		alert("You must enter your Dropoff Point in the box provided");
		dropoff.focus();
		return false;
	}

return true;

}

function isPhone(){
	var i;
	var s = form.phone;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if ( ((c < "0") || (c > "9")) ) {
			alert("You must enter only digits for the phone number");
			phone.focus();
			return false;
		}
    }
    // All characters are numbers.
    return true;
}

function ValidatePhone(){
	
	var phone = form.phone;
	var strPhone = phone.value;
	
	if (phone.value == null || phone.value == "") {
	alert("You must enter a contact phone number");	
		phone.value="";
		phone.focus();
		return false;
		}
	
	if (isInteger(phone.value)==false) {
		alert("The phone number must be digits only, for example 441273400525");	
		phone.value="";
		phone.focus();
		return false;
	}

	if (strPhone.length < 7) {
		alert("The phone number you have specified is not long enough, it must be include the international code");	
		phone.focus();
		return false;
	}

return true;
}

// Global container to check all elements

function ValidateForm() {

	if (ValidateNotBlanks()==false){
		return false;
	}
	if (ValidateEmail()==false){
		return false;
	}
	if (ValidatePhone()==false){
		return false;
	}


form.submit();
return true;

}

// Global container for checking confirmation form

function ValidateConfirm() {

	if (ValidateEmail()==false){
		return false;
	}
	if (ValidatePhone()==false){
		return false;
	}

	var howhear=form.howhear;

	// Check if HOWHEAER has a value
	if (	howhear.value==0){
		alert("Please specify where you heard of us, thank you.");
		howhear.focus();
		return false;
	}

form.TimeStamp.value=new Date().valueOf();
form.submit();
return true;

}
