// these are the allowable characters on Cosnet
var validationAlphaUC = Array("the letters A-Z", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
var validationAlphaLC = Array("the letters a-z", "abcdefghijklmnopqrstuvwxyz");
var validationNum = Array("the digits 0-9", "0123456789");
var validationPunc = Array("Hyphen, Apostrophe, Full-Stop, Comma, Space characters", "\\-'\\.\\, ");
// specific variants for some fields
var validationPostCodePunc = Array("Space character", " ");
var validationTelNum = Array("the digits 0-9, Space character", "0123456789 ");

// LUHN Check function
function luhnCheckCC(s) {

  var i, n, c, r, t;

  // First, reverse the string and remove any non-numeric characters.

  r = "";
  for (i = 0; i < s.length; i++) {
    c = parseInt(s.charAt(i), 10);
    if (c >= 0 && c <= 9)
      r = c + r;
  }

  // Check for a bad string.

  if (r.length <= 1)
    return false;

  // Now run through each single digit to create a new string. Even digits
  // are multiplied by two, odd digits are left alone.

  t = "";
  for (i = 0; i < r.length; i++) {
    c = parseInt(r.charAt(i), 10);
    if (i % 2 != 0)
      c *= 2;
    t = t + c;
  }

  // Finally, add up all the single digits in this string.

  n = 0;
  for (i = 0; i < t.length; i++) {
    c = parseInt(t.charAt(i), 10);
    n = n + c;
  }

  // If the resulting sum is an even multiple of ten (but not zero), the
  // card number is good.

  if (n != 0 && n % 10 == 0)
    return true;
  else
    return false;
}
//end of Luhn Check

function check_firstname(i_form)
{
	var valid_chars = validationAlphaUC[0] + ", " + validationAlphaLC[0] + ", " + validationPunc[0];
	var pattern = '[^' + validationAlphaUC[1] + validationAlphaLC[1] + validationPunc[1] + ']+';
	var re = new RegExp(pattern);
	
	for(i=0; i<i_form.no_adult_pax.value; i++){

		x = i;
		x +=1; 
		if (eval("i_form.adult_"+i+"_forename.value") == null || eval("i_form.adult_"+i+"_forename.value") == ""){
	     	alert("The Adult "+x+" Forename field has not been completed.");
	    	eval("i_form.adult_"+i+"_forename.focus()");
	     	return false;
	     }
	     else if (eval("i_form.adult_"+i+"_forename.value.match(re)")){
	     	alert("The Adult "+x+" Forename field contains invalid characters. Only " + valid_chars + " are allowed.");
	    	eval("i_form.adult_"+i+"_forename.focus()");
	     	return false;
	     }
	}
    return true;
}

function check_lastname(i_form)
{
	var valid_chars = validationAlphaUC[0] + ", " + validationAlphaLC[0] + ", " + validationPunc[0];
	var pattern = '[^' + validationAlphaUC[1] + validationAlphaLC[1] + validationPunc[1] + ']+';
	var re = new RegExp(pattern);
	
	for(i=0; i<i_form.no_adult_pax.value; i++){

		x = i;
		x +=1;
	    if (eval("i_form.adult_"+i+"_surname.value") == null || eval("i_form.adult_"+i+"_surname.value") == ""){
	     	alert("The Adult "+x+" Surname field has not been completed.");
	    	eval("i_form.adult_"+i+"_surname.focus()");
	     	return false;
	    }
	    else if (eval("i_form.adult_"+i+"_surname.value.match(re)")){
	     	alert("The Adult "+x+" Surname field contains invalid characters. Only " + valid_chars + " are allowed.");
	    	eval("i_form.adult_"+i+"_surname.focus()");
	     	return false;
	    }
	}
    return true;
}

function check_dob(i_form)
{
	// regular expression to match required date format
	re = /^\d{1,2}\/\d{1,2}\/\d{4}$/;

	for(i=0; i<i_form.no_adult_pax.value; i++){

		x = i;
		x +=1;
		if (eval("i_form.adult_"+i+"_dob.value") == null || eval("i_form.adult_"+i+"_dob.value") == "" || eval("i_form.adult_"+i+"_dob.value") == "dd/mm/yyyy" || eval("!i_form.adult_"+i+"_dob.value.match(re)") ){
	     	alert("The Adult "+x+" Date of birth field is not valid. It must be in the format dd/mm/yyyy");
	    	eval("i_form.adult_"+i+"_dob.focus()");
	     	return false;
	    }
	}
    return true;
}

function check_child_firstname(i_form)
{
	var valid_chars = validationAlphaUC[0] + ", " + validationAlphaLC[0] + ", " + validationPunc[0];
	var pattern = '[^' + validationAlphaUC[1] + validationAlphaLC[1] + validationPunc[1] + ']+';
	var re = new RegExp(pattern);
	
	for(i=0; i<i_form.no_child_pax.value; i++){

		x = i;
		x +=1; 
		if (eval("i_form.child_"+i+"_forename.value") == null || eval("i_form.child_"+i+"_forename.value") == ""){
	     	alert("The Child "+x+" Forename field has not been completed.");
	    	eval("i_form.child_"+i+"_forename.focus()");
	     	return false;
	     }
	     else if (eval("i_form.child_"+i+"_forename.value.match(re)")){
	     	alert("The Child "+x+" Forename field contains invalid characters. Only " + valid_chars + " are allowed.");
	    	eval("i_form.child_"+i+"_forename.focus()");
	     	return false;
	     }
	}
    return true;
}

function check_child_lastname(i_form)
{
	var valid_chars = validationAlphaUC[0] + ", " + validationAlphaLC[0] + ", " + validationPunc[0];
	var pattern = '[^' + validationAlphaUC[1] + validationAlphaLC[1] + validationPunc[1] + ']+';
	var re = new RegExp(pattern);
	
	for(i=0; i<i_form.no_child_pax.value; i++){

		x = i;
		x +=1;
	    if (eval("i_form.child_"+i+"_surname.value") == null || eval("i_form.child_"+i+"_surname.value") == ""){
	     	alert("The Child "+x+" Surname field has not been completed.");
	    	eval("i_form.child_"+i+"_surname.focus()");
	     	return false;
	    }
	     else if (eval("i_form.child_"+i+"_surname.value.match(re)")){
	     	alert("The Child "+x+" Surname field contains invalid characters. Only " + valid_chars + " are allowed.");
	    	eval("i_form.child_"+i+"_surname.focus()");
	     	return false;
	     }
	}
    return true;
}

function check_child_dob(i_form)
{
	// regular expression to match required date format
	re = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
	max_age = 16;
	
	for(i=0; i<i_form.no_child_pax.value; i++){

		x = i;
		x +=1;
		if (eval("i_form.child_"+i+"_dob.value") == null || eval("i_form.child_"+i+"_dob.value") == "" || eval("i_form.child_"+i+"_dob.value") == "dd/mm/yyyy" || eval("!i_form.child_"+i+"_dob.value.match(re)") ){
	     	alert("The Child "+x+" Date of birth field is not valid. It must be in the format dd/mm/yyyy");
	    	eval("i_form.child_"+i+"_dob.focus()");
	     	return false;
	    }
	    else if (isOverAge(eval("i_form.child_"+i+"_dob.value"), max_age)){
	    	alert("Child "+x+" will be over "+max_age+" years of age on the return travel date. Please re-book this child as an adult passenger.");
	    	return false;
	    }
	}
    return true;
}

function check_infant_firstname(i_form)
{
	var valid_chars = validationAlphaUC[0] + ", " + validationAlphaLC[0] + ", " + validationPunc[0];
	var pattern = '[^' + validationAlphaUC[1] + validationAlphaLC[1] + validationPunc[1] + ']+';
	var re = new RegExp(pattern);
	
	for(i=0; i<i_form.no_infant_pax.value; i++){

		x = i;
		x +=1; 
		if (eval("i_form.infant_"+i+"_forename.value") == null || eval("i_form.infant_"+i+"_forename.value") == ""){
	     	alert("The Infant "+x+" Forename field has not been completed.");
	    	eval("i_form.infant_"+i+"_forename.focus()");
	     	return false;
	     }
	     else if (eval("i_form.infant_"+i+"_forename.value.match(re)")){
	     	alert("The Infant "+x+" Forename field contains invalid characters. Only " + valid_chars + " are allowed.");
	    	eval("i_form.infant_"+i+"_forename.focus()");
	     	return false;
	     }
	}
    return true;
}

function check_infant_lastname(i_form)
{
	var valid_chars = validationAlphaUC[0] + ", " + validationAlphaLC[0] + ", " + validationPunc[0];
	var pattern = '[^' + validationAlphaUC[1] + validationAlphaLC[1] + validationPunc[1] + ']+';
	var re = new RegExp(pattern);
	
	for(i=0; i<i_form.no_infant_pax.value; i++){

		x = i;
		x +=1;
	    if (eval("i_form.infant_"+i+"_surname.value") == null || eval("i_form.infant_"+i+"_surname.value") == ""){
	     	alert("The Infant "+x+" Surname field has not been completed.");
	    	eval("i_form.infant_"+i+"_surname.focus()");
	     	return false;
	    }
	    else if (eval("i_form.infant_"+i+"_surname.value.match(re)")){
	     	alert("The Infant "+x+" Surname field contains invalid characters. Only " + valid_chars + " are allowed.");
	    	eval("i_form.infant_"+i+"_surname.focus()");
	     	return false;
	    }
	}
    return true;
}

function check_infant_dob(i_form)
{
	// regular expression to match required date format
	re = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
	max_age = 2;

	for(i=0; i<i_form.no_infant_pax.value; i++){

		x = i;
		x +=1;
		if (eval("i_form.infant_"+i+"_dob.value") == null || eval("i_form.infant_"+i+"_dob.value") == "" || eval("i_form.infant_"+i+"_dob.value") == "dd/mm/yyyy" || eval("!i_form.infant_"+i+"_dob.value.match(re)") ){
	     	alert("The Infant "+x+" Date of birth field is not valid. It must be in the format dd/mm/yyyy");
	    	eval("i_form.infant_"+i+"_dob.focus()");
	     	return false;
	    }
	    else if (isOverAge(eval("i_form.infant_"+i+"_dob.value"), max_age)){
	    	alert("Infant "+x+" will be over "+max_age+" years of age on the return travel date. Please re-book this infant as a child passenger.");
	    	return false;
	    }
	}
    return true;
}

function check_for_middle_name(i_form)
{
	var reWhiteSpace = new RegExp(/\s/g);
	var isThereWhiteSpace = false;
	
	for(i=0; i<i_form.no_adult_pax.value; i++){
	    if (reWhiteSpace.test(eval("i_form.adult_"+i+"_surname.value"))) {
            isThereWhiteSpace = true;
	    }
	}
	
	for(i=0; i<i_form.no_child_pax.value; i++){
	    if (reWhiteSpace.test(eval("i_form.child_"+i+"_surname.value"))) {
            isThereWhiteSpace = true;
	    }
	}	
	
	for(i=0; i<i_form.no_infant_pax.value; i++){
	    if (reWhiteSpace.test(eval("i_form.infant_"+i+"_surname.value"))) {
            isThereWhiteSpace = true;
	    }
	}
	
	if (isThereWhiteSpace)
	{
		input_box=confirm("Please note the Surname field must be the passenger SURNAME ONLY as written on your passport and not including any middle names. Do you want to continue?");
		if (input_box==true)
		{ 
			return true
		}
		else
		{
			return false;
		}		
	}
	return true;
}

function check_ccname(i_form)
{
	var valid_chars = validationAlphaUC[0] + ", " + validationAlphaLC[0] + ", " + validationPunc[0];
	var pattern = '[^' + validationAlphaUC[1] + validationAlphaLC[1] + validationPunc[1] + ']+';
	var re = new RegExp(pattern);
	
     if (i_form.cc_name.value == null || i_form.cc_name.value == ""){
     	alert("The Cardholder Name field has not been completed.");
    	i_form.cc_name.focus();
     	return false;
     }
	 else if (i_form.cc_name.value.match(re)){
	 	alert("The Cardholder Name field contains invalid characters. Only " + valid_chars + " are allowed.");
	    i_form.cc_name.focus();
	    return false;
	 }
     return true;
}

function check_ccpostcode(i_form)
{
	var valid_chars = validationAlphaUC[0] + ", " + validationAlphaLC[0] + ", " + validationNum[0] + ", " + validationPostCodePunc[0];
	var pattern = '[^' + validationAlphaUC[1] + validationAlphaLC[1] + validationNum[1] + validationPostCodePunc[1] + ']+';
	var re = new RegExp(pattern);
	
    if (i_form.lead_postcode.value == null || i_form.lead_postcode.value == ""){
		alert("The Credit Card Holder Postcode field has not been completed.");
    	i_form.lead_postcode.focus();
     	return false;
    }
	else if (i_form.lead_postcode.value.match(re)){
	 	alert("The Credit Card Holder Postcode field contains invalid characters. Only " + valid_chars + " are allowed.");
	    i_form.lead_postcode.focus();
	    return false;
	}	
    return true;
}


function check_ccaddress(i_form)
{
	var valid_chars = validationAlphaUC[0] + ", " + validationAlphaLC[0] + ", " + validationNum[0] + ", " + validationPunc[0];
	var pattern = '[^' + validationAlphaUC[1] + validationAlphaLC[1] + validationNum[1] + validationPunc[1] + ']+';
	var re = new RegExp(pattern);
	
     if (i_form.lead_address_1.value == null || i_form.lead_address_1.value == ""){
     	alert("The Credit Card Holder Address Line 1 field has not been completed.");
    	i_form.lead_address_1.focus();
     	return false;
     }
	 else if (i_form.lead_address_1.value.match(re)){
	 	alert("The Credit Card Holder Address Line 1 field contains invalid characters. Only " + valid_chars + " are allowed.");
	    i_form.lead_address_1.focus();
	    return false;
	 }
	 else if ((i_form.lead_address_1.value != null || i_form.lead_address_2.value != "") && i_form.lead_address_2.value.match(re)){
	 	alert("The Credit Card Holder Address Line 2 field contains invalid characters. Only " + valid_chars + " are allowed.");
	    i_form.lead_address_1.focus();
	    return false;
	 }
     return true;
}

function check_cctown(i_form)
{
	var valid_chars = validationAlphaUC[0] + ", " + validationAlphaLC[0] + ", " + validationPunc[0];
	var pattern = '[^' + validationAlphaUC[1] + validationAlphaLC[1] + validationPunc[1] + ']+';
	var re = new RegExp(pattern);
	
     if (i_form.lead_town.value == null || i_form.lead_town.value == ""){
     	alert("The Credit Card Holder Town/City field has not been completed.");
    	i_form.lead_town.focus();
     	return false;
     }
	 else if (i_form.lead_town.value.match(re)){
	 	alert("The Credit Card Holder Town/City field contains invalid characters. Only " + valid_chars + " are allowed.");
	    i_form.lead_town.focus();
	    return false;
	 }
     return true;
}


function check_ccExpiry(i_form, currentMonth, currentYear)
{

     if (i_form.cc_expiry_month.value < currentMonth && i_form.cc_expiry_year.value == currentYear){
     	alert("The credit card expiry date has already passed.");
    	i_form.cc_expiry_month.focus();
     	return false;
     }
	
     return true;
}



function check_phoneDay(i_form)
{
	var valid_chars = validationTelNum[0];
	var pattern = '[^' + validationTelNum[1] + ']+';
	var re = new RegExp(pattern);
	
    if (i_form.phoneDay.value == null || i_form.phoneDay.value == ""){
    	alert("The Credit Card Holder Daytime Telephone Number field has not been completed.");
    	i_form.phoneDay.focus();
     	return false;
     }
	 else if (i_form.phoneDay.value.match(re)){
	 	alert("The Credit Card Holder Daytime Telephone Number field contains invalid characters. Only " + valid_chars + " are allowed.");
	    i_form.phoneDay.focus();
	    return false;
	 }
     return true;
}

function check_phoneEvening(i_form)
{
	var valid_chars = validationTelNum[0];
	var pattern = '[^' + validationTelNum[1] + ']+';
	var re = new RegExp(pattern);
	
    if (i_form.phoneEvening.value == null || i_form.phoneEvening.value == ""){
		alert("The Credit Card Holder Evening Telephone Number field has not been completed.");
    	i_form.phoneEvening.focus();
     	return false;
     }
	 else if (i_form.phoneEvening.value.match(re)){
	 	alert("The Credit Card Holder Evening Telephone Number field contains invalid characters. Only " + valid_chars + " are allowed.");
	    i_form.phoneEvening.focus();
	    return false;
	 }
     return true;
}

function check_email(i_form)
{
     if (i_form.email.value == null || i_form.email.value == ""){
     	alert("The Credit Card Holder Email address field has not been completed.");
    	i_form.email.focus();
     	return false;
     }

     if(i_form.email.value != i_form.emailConfirm.value){
    	 alert("Email addresses do not match.");
	 	 i_form.email.focus();
	 	 return false;     
     }
     
     var filter = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
     
     if (!filter.test(i_form.email.value)) {
	     alert('Please provide a valid email address');
	     i_form.email.focus();    
	 	 return false; 	     
     }
     
     return true;
}

function check_ccnumber(i_form)
{
	var valid_chars = validationNum[0];
	var pattern = '[^' + validationNum[1] + ']+';
	var re = new RegExp(pattern);
	
     if (i_form.cc_number.value == null || i_form.cc_number.value == ""){
     	alert("The Card Number field has not been completed.");
    	i_form.cc_number.focus();
     	return false;
     }
	 else if (i_form.cc_number.value.match(re)){
	 	alert("The Card Number field contains invalid characters. Only " + valid_chars + " are allowed.");
	    i_form.cc_number.focus();
	    return false;
	 }
     return true;
}

function luhn_check(i_form)
{
	// Luhn Check
	var s = i_form.cc_number.value;

	if (!luhnCheckCC(s)){
		alert("Invalid Credit Card Number.");
	    i_form.cc_number.focus();
	 	return false;
	  }
	  return true;
}

function check_cvs(i_form)
{
	var valid_chars = validationNum[0];
	var pattern = '[^' + validationNum[1] + ']+';
	var re = new RegExp(pattern);
	
     if (i_form.cc_security_no.value == null || i_form.cc_security_no.value == ""){
     	alert("The Card Security Number field has not been completed. This number is a unique three or four-digit number printed on the reverse of your debit/credit card.");
    	i_form.cc_security_no.focus();
     	return false;
     }
	 else if (i_form.cc_security_no.value.match(re)){
	 	alert("The Card Security Number field contains invalid characters. Only " + valid_chars + " are allowed.");
	    i_form.cc_security_no.focus();
	    return false;
	 }
     return true;
}

function check_cc_type(i_form)
{
    if (i_form.cc_type == undefined)
        return true;

    if (i_form.cc_type.value.substr(0,1) != "|")
        return true;

    alert("Please select a type of card before proceeding.");

    i_form.cc_type.focus();

    return false;
}

function check_issue_number(i_form)
{
	var valid_chars = validationNum[0];
	var pattern = '[^' + validationNum[1] + ']+';
	var re = new RegExp(pattern);
	
	if (i_form.cc_issue_no.value != "" && i_form.cc_issue_no.value.match(re)){
		alert("The Card Issue Number field contains invalid characters. Only " + valid_chars + " are allowed.");
		i_form.cc_issue_no.focus();
		return false;
	}
    return true;
}

function check_accept_tandc(i_form, brandName)
{
    if ((l_accept_tandc = i_form.accept_tandc.checked))
        return true;
	
        if (brandName == "Avro")
			alert("Please accept Avro's terms and conditions before proceeding.");
		else
    		alert("Please accept Cosmos Flights' terms and conditions before proceeding.");

    i_form.accept_tandc.focus();

    return false;
}

function check_accept_dacp(i_form, brandName)
{
    if (i_form.accept_dacp.checked){return true;}
    else{

	    if (brandName == "Avro")
    		alert("Please accept Avro's Deposit & Cancellation Policy before proceeding.");
    	else
    		alert("Please accept Cosmos Flights' Deposit & Cancellation Policy before proceeding.");

    i_form.accept_dacp.focus();

    return false;
    }
}

function validate_form(i_form, currentMonth, currentYear, brandName)
{

	if (!check_firstname(i_form))
        return false;
        
    if (!check_lastname(i_form))
        return false; 
        
    if (!check_dob(i_form))
        return false;   
        
    if (!check_child_firstname(i_form))
        return false;
        
    if (!check_child_lastname(i_form))
        return false;
       
    if (!check_child_dob(i_form))
        return false;
        
    if (!check_infant_firstname(i_form))
        return false; 
        
    if (!check_infant_lastname(i_form))
        return false;  
        
    if (!check_infant_dob(i_form))
        return false;
    
    if (!check_ccname(i_form))
        return false; 
        
    if (!check_ccpostcode(i_form))
        return false;     

    if (!check_ccaddress(i_form))
        return false; 
        
    if (!check_cctown(i_form))
        return false; 
        
    if (!check_phoneDay(i_form))
        return false; 
        
    if (!check_phoneEvening(i_form))
        return false; 
        
    if (!check_email(i_form))
        return false; 
        
    if (!check_ccnumber(i_form))
        return false; 
        
    if (!luhn_check(i_form))
        return false;
        
    if (!check_ccExpiry(i_form, currentMonth, currentYear))
        return false;
        
    if (!check_cvs(i_form))
        return false; 
        
    if (!check_cc_type(i_form))
        return false;
    
    if (!check_issue_number(i_form))
        return false;
        
    if (!check_accept_tandc(i_form, brandName))
        return false;
    
    if (!check_accept_dacp(i_form, brandName))
        return false;
    
    if (!check_for_middle_name(i_form))
    	return false;

	if (!isSurnameMatchCardHolder(i_form))
       return false;

    return true;
}

function loader(i_form, currentMonth, currentYear, brandName)
{
	if (! validate_form(i_form, currentMonth, currentYear, brandName))
        return false;
	if(document.getElementById('loaderTrans')!=null){
		document.getElementById('loaderTrans').style.display = 'block';
		document.getElementById('loaderTrans').style.width = document.body.offsetWidth + 'px';
		// document.getElementById('loaderTrans').style.height = _docHeight;
	}
	
	document.getElementById('loaderImage').style.display = 'block';
	document.getElementById('loaderImage').style.width = document.body.offsetWidth + 'px';
	document.getElementById('loaderImage').style.offsetTop = '250px';
	
	scroll(0,0);
	
	var x = document.getElementsByTagName("select");

	for (i = 0; i < x.length; i++) {
	   x[i].style.display = "none";
	}
	
	return true;
}

//This function is to remove the loading div if back button is pressed
//on the ACS page.
function removeLoading(){
	
	if(document.getElementById('loaderTrans')!=null){
		document.getElementById('loaderTrans').style.display = 'none';
		document.getElementById('loaderImage').style.display = 'none';
	}

}

function loaderThreeD()
{	
	document.getElementById('loaderTrans').style.display = 'block';
	document.getElementById('loaderTrans').style.width = document.body.offsetWidth + 'px';
		
	document.getElementById('loaderImage').style.display = 'block';
	document.getElementById('loaderImage').style.width = document.body.offsetWidth + 'px';
	document.getElementById('loaderImage').style.offsetTop = '250px';
	
	scroll(0,0);
	
	var x = document.getElementsByTagName("select");

	for (i = 0; i < x.length; i++) {
	   x[i].style.display = "none";
	}
	
	return true;
}

function validate_form2(i_form, i_page, currentMonth, currentYear, brandName)
{
    if (! validate_form(i_form, currentMonth, currentYear, brandName))
        return false;
    l_opera = (navigator.userAgent.indexOf('Opera') != -1);
    var l_width = 800;
    var l_height = 300;
    var l_top;
    var l_left;
    if (l_opera)
        l_top = l_left = 50;
    else
    {
        l_top = screen.height / 2 - l_height / 2;
        l_left = screen.width / 2 - l_width / 2;
    }
    l_str =  "height=" + l_height + ",width=" + l_width + ",left=" + l_left + ",top="
      + l_top +",location=no,menubar=no,resizable=no,status=no,toolbar=no;";
    window.l_win = window.open(i_page, "please_wait", l_str);
    window.onunload=close_wait;
    document.all.prc.disabled = true;
    return true;
}



function validate_form_agent(i_form, brandName)
{
	if (!check_firstname(i_form))
        return false;
        
    if (!check_lastname(i_form))
        return false; 

    if (!check_child_firstname(i_form))
        return false;
        
    if (!check_child_lastname(i_form))
        return false;
       
    if (!check_child_dob(i_form))
        return false;
        
    if (!check_infant_firstname(i_form))
        return false; 
        
    if (!check_infant_lastname(i_form))
        return false;  
        
    if (!check_infant_dob(i_form))
        return false;   

    if (!check_accept_tandc(i_form, brandName))
        return false;
    
    if (!check_for_middle_name(i_form))
    	return false;    

    return true;
}

function validate_form2_agent(i_form, i_page, brandName)
{
    if (! validate_form_agent(i_form, brandName))
        return false;
   	
	return true;
}


function close_wait()
{
    window.l_win.close();
}

function updateDiv(id,text)
{
    if(document.getElementById(id)!= null)
    {
        //newText = document.getElementById(id).innerHTML.replace(/.[0-9]+\.[0-9][0-9]/,text);
    
        document.getElementById(id).innerHTML = text;
    }
}

function updateValue(id,text)
{
    if (document.getElementById(id)!= null)
    {
        document.getElementById(id).value = text;
    }

}

function getValue(id)
{
	if (document.getElementById(id)!= null) {return document.getElementById(id).value;}
	else {return false;}
}

function idExists(id)
{
	if (document.getElementById(id)!= null) {return true;}
	else {return false;}
}

function cc_select(Sel)
{
   args = Sel.split('|');

   ccNoFee="&pound;0.00";
   ccFee= "&pound;"+args[1];
   ccEach= "&pound;"+args[1];
   TotalWith=  "&pound;"+args[2];
   TotalWithout= "&pound;"+args[3];

   if (ccFee == 0)
   {
      if (idExists('cc_fee_1'))
         updateDiv('cc_fee_1',ccNoFee);
   
      if (idExists('cc_fee_2'))
         updateDiv('cc_fee_2',ccNoFee);

      if (idExists('cc_js_active'))
         updateValue('cc_js_active','Debit');

      if (idExists('total_price_1'))
         updateDiv('total_price_1',TotalWithout);

      if (idExists('total_price_2'))
         updateDiv('total_price_2',TotalWithout);

   } else {
   
      if (idExists('cc_fee_1'))
         updateDiv('cc_fee_1',ccFee);
   
      if (idExists('cc_fee_2'))
         updateDiv('cc_fee_2',ccFee);

      if (idExists('cc_js_active'))
         updateValue('cc_js_active','Credit');

      if (idExists('total_price_1'))
         updateDiv('total_price_1',TotalWith);

      if (idExists('total_price_2'))
         updateDiv('total_price_2',TotalWith);
   }

   updateValue('cc_total_price',args[2]);
}

// calculates if person is over the specified age
function isOverAge(dob, aged_over)
{
	over_age = false;
	// first we need the reference Dob before which the person must not have been born
	reference_dob = new Date();
	actual_dob = new Date(Date.parse(dob));
	reference_dob.setFullYear(reference_dob.getFullYear() - aged_over);
	
	// now we compare the actual DoB to the reference Dob
	if (actual_dob.getFullYear() <= reference_dob.getFullYear()){
		if (actual_dob.getMonth() <= reference_dob.getMonth()){
			if (actual_dob.getDate() < reference_dob.getDate()){
				over_age = true;
			}
		}
	}
	
	return over_age;
}

// determines if one of the customer's name matches the card holder
function isSurnameMatchCardHolder(i_form)
{   
	var surname = "";
	var card_holder = i_form.cc_name.value.toLowerCase();
	i = 0;

	while (eval("i_form.adult_"+i+"_surname")) {
		eval("surname = i_form.adult_"+i+"_surname.value.toLowerCase()");
	 	if (card_holder.indexOf(surname) != -1){
			//found at least one match for the customer's surname and card used.  therefore can return true
			return true;
		}
		i = i + 1;
 	}
	
	//if nothing was found while looping through the results then no passengers with their surnames
	//matched the card details.  Therefore return false.
	alert("Payment card must be in the name of one of the passengers travelling");
	eval("i_form.cc_name.focus()");
    return false;

}
