// 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=document.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 ValidateNotBlanksPayment(){
	
	var fname=document.form.fname;
	var lname=document.form.lname;	
	var amount=document.form.amount;
	var reason=document.form.reason;	
	var villa=document.form.villa;	

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

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

	// Check if PAYMENT REASON field have values
	if ((reason.value==null)||(reason.value=="")){
		alert("You must say what the payment is for");
		reason.focus();
		return false;
	}

	// Check if VILLA field have values
	if ((villa.value==null)||(villa.value=="")){
		alert("Please specify the villa or use 'other' or 'not applicable'");
		villa.focus();
		return false;
	}

	// Check if AMOUNT field have values
	if ((amount.value==null)||(amount.value=="")){
		alert("You must enter the Amount in the box provided");
		amount.focus();
		return false;
	}
	
	if (isInteger(amount.value)==false) {
		alert("The Amount must be digits only");	
		amount.value="";
		amount.focus();
		return false;
	}
	
return true;

}

function ValidateNotBlanksEnquiry(){
	
	var name=document.form.name;
	var howhear=document.form.howhear;
	var captcha=document.form.captcha_code;

	// Check if FNAME 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 HOWHEAR field has a value
	if ((howhear.value==null)||(howhear.value=="")){
		alert("Please tell us how you heard of us. Thank you.");
		howhear.focus();
		return false;
	}
	
	// Check if CAPTCHA Code field has a value
	if ((captcha.value==null)||(captcha.value=="")){
		alert("Please input the code in the image to verify you're human. The letters and numbers are NOT case-sensitive.");
		captcha.focus();
		return false;
	}
	
return true;

}

function ValidateNotBlanksCarHire(){
	
	var name=document.form.name;
	//var howhear=document.form.howhear;
	var captcha=document.form.captcha_code;

	// Check if FNAME 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 HOWHEAR field has a value
	//if ((howhear.value==null)||(howhear.value=="")){
	//	alert("Please tell us how you heard of us. Thank you.");
	//	howhear.focus();
	//	return false;
	//}
	
	// Check if CAPTCHA Code field has a value
	if ((captcha.value==null)||(captcha.value=="")){
		alert("Please input the code in the image to verify you're human. The letters and numbers are NOT case-sensitive.");
		captcha.focus();
		return false;
	}
	
return true;

}

function ValidateNotBlanksLuggage(){
	
	var name=document.form.name;
	//var howhear=document.form.howhear;
	var captcha=document.form.captcha_code;

	// Check if FNAME 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 HOWHEAR field has a value
	//if ((howhear.value==null)||(howhear.value=="")){
	//	alert("Please tell us how you heard of us. Thank you.");
	//	howhear.focus();
	//	return false;
	//}
	
	// Check if CAPTCHA Code field has a value
	if ((captcha.value==null)||(captcha.value=="")){
		alert("Please input the code in the image to verify you're human. The letters and numbers are NOT case-sensitive.");
		captcha.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 containers to check all fields for PAYMENT SCREENS

function ValidatePayment1() {

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

form.submit();
return true;

}

function ValidatePayment2() {

alert("IMPORTANT NOTE: This site uses a \"Shared SSL\" certificate, when you are returned to this site from HSBC secure pages you may receive a security warning. This is normal and nothing to be concerned about, please click YES/OK to continue to the \"Transaction Status Page\". Thank you, ibizavillas2000.com");
form.TimeStamp.value=new Date().valueOf();
form.submit();
return true;

}

// Global container to check all elements for ENQUIRY PAGE

function ValidateEnquiry() {

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

form.submit();
return true;

}

// Global container to check all elements for CAR HIRE REQUEST PAGE

function ValidateCarHire() {

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

document.form.submit();
return true;

}


// Global container to check all elements for GENERIC SERVUCE REQUEST PAGES

function ValidateSecGuard() {

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

form.submit();
return true;

}

// Global container to check all elements for CAR HIRE REQUEST PAGE

function ValidateLuggage() {

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

form.submit();
return true;

}
