
// This is part of a set of form validation functions adapted by ED ///////////////////////////
// found at http://javascript.about.com

// This validation will only work if it is called
// EG: do that in the form tag -  id="form1" onsubmit="return validateForm(this)"

// MANDATORY FIELDS - This is a stsndard function called by the individuls field checks -------------

function validRequired(formField,fieldLabel)
{
  var result = true;
  
	if (!formField.value)
	 {
		alert('Please enter a value for the "' + fieldLabel +'" field.');
		formField.focus();
		result = false;
	 }
   
  return result;
}

function validDropdown(formField,fieldLabel)
{
  var result = true;
  
	if (formField.value==0)
	 {
		alert('Please choose a "' + fieldLabel +'"');
		formField.focus();
		result = false;
	 }
   
  return result;
}

function validTerms(formField)
{
  var result = true;
  
	if (formField.checked==false )
	 {
		alert('You need to agree with ourTerms & Conditions\rto proceed with your booking.');
		formField.focus();
		result = false;
	 }
   
  return result;
}

// EMAIL - IS VALID -----------------------------------------------------

function validEmail(formField,fieldLabel,required)
{
  var result = true;
  
  if (required && !validRequired(formField,fieldLabel))
    result = false;

  if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) )
  {
    alert("Please enter a complete email address in the form: yourname@yourdomain.com");
    formField.focus();
    result = false;
  }
   
  return result;

}

// SUB-ROUTINE for email check -----------------------------------------------

function isEmailAddr(email)
{
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
  result = true;
  }
  return result;
}

//SUB-ROTINE to verify numbers
// http://www.webdeveloper.com/javascript/js_form_example.html

function VerifyTelephone(formField,fieldLabel) {
	
	var valid = 1
	if (!CheckPhoneNumber(formField.value)) { // call the routine that actually checks for numbers
			valid = 0
		}
	// Here we decide whether to submit the form.
		if (!valid) {
	alert('Please enter a valid "' + fieldLabel +'" number.')
		}
		return valid
		}
	
	function CheckPhoneNumber(TheNumber) {
		var valid = 1
		var GoodChars = "0123456789()-+ "
		var i = 0
		if (TheNumber=="") {
		// Return false if number is empty
		valid = 0
			}
		for (i =0; i <= TheNumber.length -1; i++) {
		if (GoodChars.indexOf(TheNumber.charAt(i)) == -1) {
		// Note: Remove the comments from the following line to see this
		// for loop in action.
		// alert(TheNumber.charAt(i) + " is no good.")
		valid = 0
		} // End if statement
			} // End for loop
		return valid
}

//// DATE verification routine
/*
-----------------------------------------
|     By Mattias Sjöberg 28/11-96       |
|You're welcome to use/edit this script.|
| Keep the comments and drop me a note. |
-----------------------------------------
|      mattias.sjoberg@swipnet.se       |
| www.geocities.com/SiliconValley/7116  |
|     Visit  The JavaScript Planet      |
-----------------------------------------
*/

function checkDate(formField, fieldLabel){
//      window.onerror=null // for all other strange errors
        var err=0;
        var psj=0;
		var result = false;	
        a=formField.value

        // split the date up into component parts
        if (a.length != 10) err=1
        d = a.substring(0, 2)// day DD
        c = a.substring(2, 3)// '/' or '-'
        m = a.substring(3, 5)// month MM
        e = a.substring(5, 6)// '/' or '-'
        y = a.substring(6, 10)// year YYYY

        //basic error checking
        if (d<1 || d>31) err = 1
        if (c != '/' && c != '-') err = 1
        if (m<1 || m>12) err = 1
        if (e != '/' && e != '-') err = 1
        if (y<1925 || y>2000) err = 1 // maybe we should set some tighter/realistic limits?
        
        //advanced error checking

        // months with 30 days
        if (m==4 || m==6 || m==9 || m==11){
                if (d==31) err=1
        }

        // february, leap year
        if (m==2){
                // feb
                var g=parseInt(y/4)
                if (isNaN(g)) {
                        err=1
                }

                if (d>29) err=1
                if (d==29 && ((y/4)!=parseInt(y/4))) err=1
        }

        if (err==1){
				var message = "Please enter a valid " + fieldLabel + " \nusing the format \nDD/MM/YYYY or DD-MM-YYYY";
                alert(message);
				result = false;
				return result;
        } else {
                //alert('OK!');
				result = true;
				return result;
        }

} /////////// End of Date verification ////////////////////////////////////////

//------------------------- INDIVIDUAL Field validation calls --------------------------------------------

// customize these calls for your form - FORMNAME.FIELDNAME,"FIELD DESCRIPTION FOR ALERT"
  
function validateForm(form1)
{
	  if (!validRequired(form1.name,"Full Name"))
		return false;
	
	  if (!validRequired(form1.address,"Address"))
		return false;
	
	  if (!validRequired(form1.postcode,"Postcode"))
		return false;
	
	  if (!validEmail(form1.email,"Email",true))
		return false;

	  if (!VerifyTelephone(form1.telephone,"Telephone")) // test for only numbers
	    return false; 
		
 	  if (!checkDate(form1.date_of_birth, "Date of birth")) 
		return false;
	
 	  return true; // if they are all OK
}
	
 // <--------- End of validation functions
 /////////////////////////////////////////////////////////////////////////////////////////////////// 
