//Email Validation
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;
}



// does the confirmation email match
function confirmEmail(email,email2,mesg){
  if (email.value != email2.value){
  alert (mesg);
  
email2.value="";
email2.focus();
}
else{
  return true
  }
}
  



// required field Validation
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 allDigits(str)
{
	return inValidCharSet(str,"0123456789");
}

function inValidCharSet(str,charset)
{
	var result = true;

	// Note: doesn't use regular expressions to avoid early Mac browser bugs	
	for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0)
		{
			result = false;
			break;
		}
	
	return result;
}

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;

}
// Number Validation
function validNum(formField,fieldLabel,required)
{
	var result = true;

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

// Int Validation
function validInt(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		var num = parseInt(formField.value,10);
 		if (isNaN(num))
 		{
 			alert('Please enter a number for the "' + fieldLabel +'" field.');
			formField.focus();		
			result = false;
		}
	} 
	
	return result;
}

// Date Validation

function validDate(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		var elems = formField.value.split("/");
 		result = (elems.length == 3); // should be three components
 		if (result) {
 			var month = parseInt(elems[0],10);
  			var day = parseInt(elems[1],10);
 			var year = parseInt(elems[2],10);
			result = allDigits(elems[0]) && (month > 0) && (month < 13) &&
					 allDigits(elems[1]) && (day > 0) && (day < 32) &&
					 allDigits(elems[2]) && ((elems[2].length == 2) || (elems[2].length == 4));
 		}
  		if (!result) {
 			alert('Please enter a date in the format MM/DD/YYYY for the "' + fieldLabel +'" field.');
			formField.focus();		
		}
	} 
	return result;
}

/* Edwin Martinez COPA ------------------------------------------------------------------------*/

function makeArray() {
  var args = makeArray.arguments;
  for (var i = 0; i < args.length; i++) {
    this[i] = args[i];
  }
  this.length = args.length;
}


function getString(date) {
  var months = new makeArray("January", "February", "March", "April", "May", "June", "July", "August","September", "October", "November", "December");
  return months[date.getMonth()] + " " + date.getDate() + ", " + ((date.c < 100) ? "19" : "") + date.getFullYear();
}

 function COPACheck(year,month,day) {
    checkexpires = 48; //hours
	
    today  = new Date();
	copadate = new Date();
	//dmonth = year.value - 1;
	bd2check = new Date(year.value,month.value - 1,day.value);
	//Set 1 day in milliseconds
    var one_day=1000*60*60*24;
	copadate = today;
	copadate.setYear(today.getYear() - 13); 
	var str = getString(copadate);
	//alert(getString(bd2check));
	if(bd2check > copadate) {
	  if(getCookie("copa")) { var cookieset=getCookie("copa"); }
	  cookieexpires = new Date();
	  cookieexpires.setDate( today.getDate() + 2);
	  setCookie("copa",bd2check,cookieexpires);
	  //alert ("we have a minor: cookieexpires " + cookieexpires);
	  location.href("unable_to_register.htm");
	  return 0;
	}
	else {
	  //alert ("all is cool");
	  return 1;
	}
}



/*============================================================================*/

/*

This routine checks the credit card number. The following checks are made:

1. A number has been provided
2. The number is a right length for the card
3. The number has an appropriate prefix for the card
4. The number has a valid modulus 10 number check digit if required

If the validation fails an error is reported.

The structure of credit card formats was gleaned from a variety of sources on 
the web.

Parameters:
            cardnumber           number on the card
            cardname             name of card as defined in the card list below

Author:     John Gardner
Date:       1st November 2003
Updated:    26th Feb. 2005      Additional cards added by request

*/

/*
   If a credit card number is invalid, an error reason is loaded into the 
   global ccErrorNo variable. This can be be used to index into the global error  
   string array to report the reason to the user if required:
   
   e.g. if (!checkCreditCard (number, name) alert (ccErrors(ccErrorNo);
*/

var ccErrorNo = 0;
var ccErrors = new Array ()

ccErrors [0] = "Unknown card type";
ccErrors [1] = "No card number provided";
ccErrors [2] = "Credit card number is in invalid format";
ccErrors [3] = "Credit card number is invalid";
ccErrors [4] = "Credit card number has an inappropriate number of digits";

function checkCreditCard (cardnumber, cardname) {

     
  // Array to hold the permitted card characteristics
  var cards = new Array();

  // Define the cards we support. You may add addtional card types.
  
  //  Name:      As in the selection box of the form - must be same as user's
  //  Length:    List of possible valid lengths of the card number for the card
  //  prefixes:  List of possible prefixes for the card
  //  checkdigit Boolean to say whether there is a check digit
  
  cards [0] = {name: "Visa", 
               length: "13,16", 
               prefixes: "4",
               checkdigit: true};
  cards [1] = {name: "MasterCard", 
               length: "16", 
               prefixes: "51,52,53,54,55",
               checkdigit: true};
  cards [2] = {name: "DinersClub", 
               length: "14,", 
               prefixes: "300,301,302,303,304,305,36,38",
               checkdigit: true};
  cards [3] = {name: "CarteBlanche", 
               length: "14", 
               prefixes: "300,301,302,303,304,305,36,38",
               checkdigit: true};
  cards [4] = {name: "AmEx", 
               length: "15", 
               prefixes: "34,37",
               checkdigit: true};
  cards [5] = {name: "Discover", 
               length: "16", 
               prefixes: "6011",
               checkdigit: true};
  cards [6] = {name: "JCB", 
               length: "15,16", 
               prefixes: "3,1800,2131",
               checkdigit: true};
  cards [7] = {name: "Enroute", 
               length: "15", 
               prefixes: "2014,2149",
               checkdigit: true};
               
  // Establish card type
  var cardType = -1;
  for (var i=0; i<cards.length; i++) {

    // See if it is this card (ignoring the case of the string)
    if (cardname.toLowerCase () == cards[i].name.toLowerCase()) {
      cardType = i;
      break;
    }
  }
  
  // If card type not found, report an error
  if (cardType == -1) {
     ccErrorNo = 0;
     return false; 
  }
   
  // Ensure that the user has provided a credit card number
  if (cardnumber.length == 0)  {
     ccErrorNo = 1;
     return false; 
  }
  
  // Check that the number is numeric, although we do permit a space to occur  
  // every four digits. 
  var cardNo = cardnumber
  var cardexp = /^([0-9]{4})\s?([0-9]{4})\s?([0-9]{4})\s?([0-9]{1,4})$/;
  if (!cardexp.exec(cardNo))  {
     ccErrorNo = 2;
     return false; 
  }
    
  // Now remove any spaces from the credit card number
  cardexp.exec(cardNo);
  cardNo = RegExp.$1 + RegExp.$2 + RegExp.$3 + RegExp.$4;
       
  // Now check the modulus 10 check digit - if required
  if (cards[cardType].checkdigit) {
    var checksum = 0;                                  // running checksum total
    var mychar = "";                                   // next char to process
    var j = 1;                                         // takes value of 1 or 2
  
    // Process each digit one by one starting at the right
    var calc;
    for (i = cardNo.length - 1; i >= 0; i--) {
    
      // Extract the next digit and multiply by 1 or 2 on alternative digits.
      calc = Number(cardNo.charAt(i)) * j;
    
      // If the result is in two digits add 1 to the checksum total
      if (calc > 9) {
        checksum = checksum + 1;
        calc = calc - 10;
      }
    
      // Add the units element to the checksum total
      checksum = checksum + calc;
    
      // Switch the value of j
      if (j ==1) {j = 2} else {j = 1};
    } 
  
    // All done - if checksum is divisible by 10, it is a valid modulus 10.
    // If not, report an error.
    if (checksum % 10 != 0)  {
     ccErrorNo = 3;
     return false; 
    }
  }  

  // The following are the card-specific checks we undertake.
  var LengthValid = false;
  var PrefixValid = false; 
  var undefined; 

  // We use these for holding the valid lengths and prefixes of a card type
  var prefix = new Array ();
  var lengths = new Array ();
    
  // Load an array with the valid prefixes for this card
  prefix = cards[cardType].prefixes.split(",");
      
  // Now see if any of them match what we have in the card number
  for (i=0; i<prefix.length; i++) {
    var exp = new RegExp ("^" + prefix[i]);
    if (exp.test (cardNo)) PrefixValid = true;
  }
      
  // If it isn't a valid prefix there's no point at looking at the length
  if (!PrefixValid) {
     ccErrorNo = 3;
     return false; 
  }
    
  // See if the length is valid for this card
  lengths = cards[cardType].length.split(",");
  for (j=0; j<lengths.length; j++) {
    if (cardNo.length == lengths[j]) LengthValid = true;
  }
  
  // See if all is OK by seeing if the length was valid. We only check the 
  // length if all else was hunky dory.
  if (!LengthValid) {
     ccErrorNo = 4;
     return false; 
  };   
  
  // The credit card is in the required format.
  return true;
}

/*============================================================================*/


function testCreditCard () {
  myCardNo = document.form.CardNumber.value;
  myCardType = document.form.CardType.value;
  if (checkCreditCard (myCardNo,myCardType)) {

  } 
  else {alert (ccErrors[ccErrorNo])};
}



/*---------------------------------------Copa ends ------------------------------*/


/*  Main Function -----------------------------------------------------------------------*/
function validateForm(theForm) {
// Form specific calls
	if (!validRequired(theForm.firstName,"First Name")) return false;
	if (!validRequired(theForm.lastName,"Last Name")) return false;
	if (!validRequired(theForm.address,"Address")) return false;
	if (!validRequired(theForm.city,"City")) return false;
	if (!validRequired(theForm.state,"State")) return false;
	if (!validRequired(theForm.country,"Country")) return false;
	if (!validRequired(theForm.phone,"Phone")) return false;
	
	//if(theForm.phone.value.length !=12){
		//	alert("Please enter your10 digit phone number ex 555-555-5555");
		//	theForm.phone.value=""
			//theForm.phone.focus()
			//return false;
		//}
	//if (!validNum(theForm.fax,"Fax",true)) return false;
	if (!validEmail(theForm.email,"Email Address",true)) return false;
	if (!validRequired(theForm.age,"Age")) return false;
	if (!validRequired(theForm.zipCode,"Zip Code")) return false;
	if (!validRequired(theForm.vehicleType,"Vehicle Type")) return false;
	//if (!validRequired(theForm.pudm,"Rental Pickup Date Month")) return false;
	//if (!validRequired(theForm.pudd,"Rental Pickup Date Day")) return false;
	//if (!validRequired(theForm.puth,"Rental Pickup Time Hour")) return false;
	//if (!validRequired(theForm.putm,"Rental Pickup Time Hour")) return false;
	//if (!validRequired(theForm.putampm,"Rental Pickup Time AM/PM")) return false;
	//if (!validRequired(theForm.rdm,"Return Date Month")) return false;
	//if (!validRequired(theForm.rdd,"Return Date Day")) return false;
	//if (!validRequired(theForm.rdh,"Return Date Hour")) return false;
   // if (!validRequired(theForm.rdm,"Return Date Minute")) return false;
	//if (!validRequired(theForm.rtampm,"Return Date AM/PM")) return false;
	if (!validRequired(theForm.arrivingLocation,"Arriving Location")) return false;
	if (!validRequired(theForm.hotel,"Hotel")) return false;
	//if (!validRequired(theForm.airline,"Airline")) return false;
	//if (!validRequired(theForm.eta,"Estimated TIme of Arrival")) return false;
	//if (!validRequired(theForm.flightnumber,"Flight Number")) return false;
	//if (!validRequired(theForm.dlic,"Driver's License #")) return false;
	//if (!validRequired(theForm.dlicdate,"Driver's License Date")) return false;
	//if (!validRequired(theForm.card,"Credit Card Type")) return false;
	//if (!validNum(theForm.cardnumber,"Credit Card Number",true)) return false;
	 myCardNo = document.form.CardNumber.value;
  myCardType = document.form.CardType.value;
  if (checkCreditCard (myCardNo,myCardType)) {
return true;
  } 
  else {alert (ccErrors[ccErrorNo]);return false;}
  
	if (!validRequired(theForm.cdate,"Credit Card Expiry Date"))return true;
}


		  function tabToNext(obj,obj2){
if (obj.value.length >=3){
obj2.focus()
}
}