var CHILDREN_SELECTS_ROW_DIV_NAME             = "children_selects_row";
var CHILDREN_SELECTS_AGE_DESCRIPTION_DIV_NAME = "child_age_description";
var CHILDREN_SELECTS_AGE_NOTE_DIV_NAME        = "child_age_note";

// Hide a select
function hideSelect(id)
{
  selectObj = document.getElementById(id);
  
  if (!selectObj) {
    //alert(id);
    return;
  }
  
  selectObj.style.visibility = 'hidden';
  //selectObj.style.display    = 'block';
}

// Show a select
function showSelect(id)
{
  selectObj = document.getElementById(id);
  
  if (!selectObj) {
    //alert(id);
    return;
  }
  
  selectObj.style.visibility = 'visible';
  //selectObj.style.display    = 'block';
}

// Show the appropriate child select boxes based on the number of children selected.
function showChildSelects(form_object) {
  number_of_children = 0;
  
  if (!form_object.cruise_child) {
    return;
  }
  
  number_of_children    = form_object.cruise_child.selectedIndex;
  total_number_children = form_object.cruise_child.options.length - 1;
  
  for (count = number_of_children; count <= total_number_children; count++) {
    hideSelect('child_age_' + count);
  }
  
  if (!number_of_children) {
       
    if (document.getElementById(CHILDREN_SELECTS_AGE_DESCRIPTION_DIV_NAME)) {
      hideDiv(CHILDREN_SELECTS_AGE_DESCRIPTION_DIV_NAME,!CHILDREN_ROW_HIDE_COMPLETE);
    }
    
    if (document.getElementById(CHILDREN_SELECTS_AGE_NOTE_DIV_NAME)) {
      hideDiv(CHILDREN_SELECTS_AGE_NOTE_DIV_NAME,!CHILDREN_ROW_HIDE_COMPLETE);
    }
    
    return;
  }
  
  for (count = 1; count <= number_of_children; count++) {
    showSelect('child_age_' + count);
  }
  
  
  if (document.getElementById(CHILDREN_SELECTS_AGE_DESCRIPTION_DIV_NAME)) {
    showDiv(CHILDREN_SELECTS_AGE_DESCRIPTION_DIV_NAME);
  }
  
  if (document.getElementById(CHILDREN_SELECTS_AGE_NOTE_DIV_NAME)) {
    showDiv(CHILDREN_SELECTS_AGE_NOTE_DIV_NAME);
  }
}

// Checks the children age selects and adds input fields to the form for submission
function childrenSelectChecks(form_object)
{
  if (!form_object.cruise_child) {
    return true;
  }
  
  // Loop over each of the child selects to verify they have valid ages selected
  for (count = 1; count <= form_object.cruise_child.selectedIndex; count++) {
    // Do the checks to make sure that each child was selected an age
    child_age_select = document.getElementById('child_age_' + count);
    if (child_age_select && !child_age_select.selectedIndex) {
      alert('Please select the age of child ' + count);
      child_age_select.focus();
      return false;
    }
  }
  
  return true;
}


function CheckPaxCount(elm) {
	
	var totalPax = 0;
	
	totalPax = parseInt(document.getElementById('cruise_seniors').value) +  parseInt(document.getElementById('cruise_adults').value) + parseInt(document.getElementById('cruise_child').value);
	
	if (totalPax > 4) {
		
	alert("Sorry there are no available cabins that can accomodate more than 4 passengers, you will require multiple cabins.");
	return false;
	
}


 if (totalPax < 2) {
        
    alert("Sorry there are no available cabins that can accomodate 1 passenger.");
    return false;
 }

return true;

	
}	




function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}



function populateFirstPax() {
	document.paymentForm.Ntitle.selectedIndex = document.paymentForm.FirstNameTitle1.selectedIndex;
	document.paymentForm.FIRSTNAME.value = document.paymentForm.FirstNamePax1.value;
	document.paymentForm.LASTNAME.value = document.paymentForm.LastNamePax1.value;
	document.paymentForm.cc_number01.value =  document.paymentForm.FirstNamePax1.value + " " + document.paymentForm.LastNamePax1.value;
	document.getElementById("guest1Div").innerHTML = document.paymentForm.FirstNameTitle1.options[document.paymentForm.FirstNameTitle1.selectedIndex].value + " " + document.paymentForm.FirstNamePax1.value + " " + document.paymentForm.LastNamePax1.value + " :&nbsp;";
	
	}
	
	
	
function populateSecondPax() {
	document.getElementById("guest2Div").innerHTML = document.paymentForm.FirstNameTitle2.options[document.paymentForm.FirstNameTitle2.selectedIndex].value + " " + document.paymentForm.FirstNamePax2.value + " " + document.paymentForm.LastNamePax2.value + " :&nbsp;";
	
}


function populateThirdPax() {
	document.getElementById("guest3Div").innerHTML = document.paymentForm.FirstNameTitle3.options[document.paymentForm.FirstNameTitle3.selectedIndex].value + " " + document.paymentForm.FirstNamePax3.value + " " + document.paymentForm.LastNamePax3.value + " :&nbsp;";
	
}


function populateForthPax() {
	document.getElementById("guest4Div").innerHTML = document.paymentForm.FirstNameTitle4.options[document.paymentForm.FirstNameTitle4.selectedIndex].value + " " + document.paymentForm.FirstNamePax4.value + " " + document.paymentForm.LastNamePax4.value + " :&nbsp;";
	
}



function validatepaymentForm()
			{
	
				  	document.paymentForm.action = "confirmation.php";
				
			// define vars
			// remember to change form name after 'document'
			var valid = true; 
			var errMsg = 'The following items need your attention:\n\n';
			
			var firstName = document.paymentForm.FIRSTNAME.value; 
			var lastName = document.paymentForm.LASTNAME.value; 
			var emailAddress = document.paymentForm.EMAIL.value;
			var dayPhone = document.paymentForm.DAY_PHONE.value;
			var cc_number01 	= document.paymentForm.cc_number01.value;	
			var cc_number02 	= document.paymentForm.cc_number02.value;
			var cc_number04 	= document.paymentForm.cc_number04.value;
			var cc_number05 	= document.paymentForm.cc_number05.value;
			var cc_number06 	= document.paymentForm.cc_number06.value;
			var cc_number07 	= document.paymentForm.cc_number07.value;
			var numGuests =  parseInt(document.paymentForm.cruise_seniors.value) + parseInt(document.paymentForm.cruise_child.value) +  parseInt(document.paymentForm.cruise_adults.value);
									
			var PaymentMethod 	= document.paymentForm.PaymentMethod.value;
			var CreditCardNumber = document.paymentForm.CreditCardNumber.value;
			var ExpireMonth 	= document.paymentForm.ExpireMonth.value;
			var ExpireYear 		= document.paymentForm.ExpireYear.value;
			var CCVNum 			= document.paymentForm.CCVNum.value;
			var PaymentAmount 	= document.paymentForm.PaymentAmount.value;
			var terms 	= document.paymentForm.terms.checked;			
			
			
			var ccNumb = document.paymentForm.CreditCardNumber.value;
			//function Mod10(ccNumb) {  // v2.0
			var valid = "0123456789"  // Valid digits in a credit card number
			var len = ccNumb.length;  // The length of the submitted cc number
			var iCCN = parseInt(ccNumb);  // integer of ccNumb
			var sCCN = ccNumb.toString();  // string of ccNumb
			sCCN = sCCN.replace (/^\s+|\s+$/g,'');  // strip spaces
			var iTotal = 0;  // integer total set at zero
			var bNum = true;  // by default assume it is a number
			var bResult = false;  // by default assume it is NOT a valid cc
			var temp;  // temp variable for parsing string
			var calc;  // used for calculation of each digit
			
			
			var CruiseLineResNo = document.paymentForm.CruiseLineResNo.value;
			var StateRoom = document.paymentForm.StateRoom.value;
			var AmountDueVendor = document.paymentForm.AmountDueVendor.value;
			
	
			// Determine if the ccNumb is in fact all numbers
			for (var j=0; j<len; j++) {
			  temp = "" + sCCN.substring(j, j+1);
			  if (valid.indexOf(temp) == "-1"){bNum = false;}
			}
			
			// if it is NOT a number, you can either alert to the fact, or just pass a failure
			if(!bNum){
			  /*alert("Not a Number");*/bResult = false;
			}
			
			// Determine if it is the proper length 
			if((len == 0)&&(bResult)){  // nothing, field is blank AND passed above # check
			  bResult = false;
			} else{  // ccNumb is a number and the proper length - let's see if it is a valid card number
			  if(len >= 15){  // 15 or 16 for Amex or V/MC
			    for(var i=len;i>0;i--){  // LOOP throught the digits of the card
			      calc = parseInt(iCCN) % 10;  // right most digit
			      calc = parseInt(calc);  // assure it is an integer
			      iTotal += calc;  // running total of the card number as we loop - Do Nothing to first digit
			      i--;  // decrement the count - move to the next digit in the card
			      iCCN = iCCN / 10;                               // subtracts right most digit from ccNumb
			      calc = parseInt(iCCN) % 10 ;    // NEXT right most digit
			      calc = calc *2;                                 // multiply the digit by two
			      // Instead of some screwy method of converting 16 to a string and then parsing 1 and 6 and then adding them to make 7,
			      // I use a simple switch statement to change the value of calc2 to 7 if 16 is the multiple.
			      switch(calc){
			        case 10: calc = 1; break;       //5*2=10 & 1+0 = 1
			        case 12: calc = 3; break;       //6*2=12 & 1+2 = 3
			        case 14: calc = 5; break;       //7*2=14 & 1+4 = 5
			        case 16: calc = 7; break;       //8*2=16 & 1+6 = 7
			        case 18: calc = 9; break;       //9*2=18 & 1+8 = 9
			        default: calc = calc;           //4*2= 8 &   8 = 8  -same for all lower numbers
			      }                                               
			    iCCN = iCCN / 10;  // subtracts right most digit from ccNum
			    iTotal += calc;  // running total of the card number as we loop
			  }  // END OF LOOP
			  if ((iTotal%10)==0){  // check to see if the sum Mod 10 is zero
			    bResult = true;  // This IS (or could be) a valid credit card number.
			  } else {
			    bResult = false;  // This could NOT be a valid credit card number
			    }
			  }
			}


			
				if (firstName == "")
				{
					valid = false;
					errMsg += 'Please provide your First Name.\n';
				}
			
				if (lastName == "")
				{
					valid = false;
					errMsg += 'Please provide your Last Name.\n';
				}
			


				if (emailAddress == "")
				{
					valid = false;
					errMsg += 'Please enter your Email Address.\n';
				}
			
			
			if (dayPhone == "")
				{
					valid = false;
					errMsg += 'Please provide your Day Phone Number.\n';
				}
			

			
			if (numGuests == 0)
				{
					valid = false;
					errMsg += 'Please select the Number of Guests.\n';
				} else {
					
					
				
					
				for(var i=1; i<numGuests+1; i++) {
					
						vfirstNameTitle = "FirstNameTitle" + i.toString();
						vFirstNamePax = "FirstNamePax" + i.toString();
						vLastNamePax = "LastNamePax" + i.toString();
						vmonthBirthDate = "monthBirthDate" + i.toString();
						vdayBirthDate = "dayBirthDate" + i.toString();
						vyearBirthDate = "yearBirthDate" + i.toString();
	
						
						vCitizenship = "Citizenship" + i.toString();
						
					 if (eval("document.paymentForm." + vfirstNameTitle + ".selectedIndex") == 0)
					{
					valid = false;
					errMsg += 'Please provide the Title of Guest ' + i.toString() +  '.\n';
					}
					
					
					
					
					 if (eval("document.paymentForm." + vFirstNamePax + ".value") == "")
					{
					valid = false;
					errMsg += 'Please provide the First Name of Guest ' + i.toString() +  '.\n';
					}
			
					 if (eval("document.paymentForm." + vLastNamePax + ".value") == "")
					{
					valid = false;
					errMsg += 'Please provide the Last Name of Guest ' + i.toString() +  '.\n';
					}
					
					
					
					
					
					if (eval("document.paymentForm." + vmonthBirthDate + ".selectedIndex") == 0)
					{
					valid = false;
					errMsg += 'Please provide the Birth Date Month of Guest ' + i.toString() +  '.\n';
					} 
					
					
						if (eval("document.paymentForm." + vdayBirthDate + ".selectedIndex") == 0)
					{
					valid = false;
					errMsg += 'Please provide the Birth Date Day of Guest ' + i.toString() +  '.\n';
					} 
					
					
					if (eval("document.paymentForm." + vyearBirthDate + ".selectedIndex") == 0)
					{
					valid = false;
					errMsg += 'Please provide the Birth Date Year of Guest ' + i.toString() +  '.\n';
					} 
					
						
					
				
					
		
				}
				
						

					
					}


			
			
			if (cc_number01 == "")
				{
					valid = false;
					errMsg += 'Please provide the Name on the Credit Card.\n';
				}
			
			if (cc_number02 == "")
				{
					valid = false;
					errMsg += 'Please provide the Billing Address.\n';
				}
			
			if (cc_number04 == "")
				{
					valid = false;
					errMsg += 'Please provide the Billing City.\n';
				}
			
			if (cc_number05 == "" && cc_number07 == "US")
				{
					valid = false;
					errMsg += 'Please provide the Billing State.\n';
				}
			
			if (cc_number06 == "" && cc_number07 == "US")
				{
					valid = false;
					errMsg += 'Please provide the Billing Zip or Postal Code.\n';
				}
			
			if (cc_number07 == "")
				{
					valid = false;
					errMsg += 'Please provide the Billing Country.\n';
				}
			

// Validate Agent Fields
if (ReadCookie("Agent") !="") {
	
	if (document.paymentForm.AgentCode.selectedIndex == 0) {
		valid = false;
	   errMsg += 'Please select the Agent Name.\n';
		}
	
	
	if (CruiseLineResNo == "")
				{
					valid = false;
					errMsg += 'Please provide the Cruise Line Res Number.\n';
				}
	
	
		if (StateRoom == "")
				{
					valid = false;
					errMsg += 'Please provide the Cabin Number.\n';
				}
	
	
	
		
				
					

					
	
	
	
		if (AmountDueVendor == "")
				{
					valid = false;
					errMsg += 'Please provide the Amount Due to the Cruise Line.\n';
				}
	
	
	
}


	
			if (PaymentMethod == "")
				{
					valid = false;
					errMsg += 'Please choose a Credit Card Type.\n';
				}
			
			if (CreditCardNumber == "")
				{
					valid = false;
					errMsg += 'Please enter a Credit Card Number.\n';
				} else if (!bResult)
					{
						valid = false;
						errMsg += 'The Credit Card Number you entered is not valid.\n';
					}
			
			if (ExpireMonth == "")
				{
					valid = false;
					errMsg += 'Please choose an Expiration Month.\n';
				}
			
			
			
			if (ExpireYear == "")
				{
					valid = false;
					errMsg += 'Please choose an Expiration Year.\n';
				}
			
			if (CCVNum == "")
				{
					valid = false;
					errMsg += 'Please provide a CVV Security Code.\n';
				}
			
			if (PaymentAmount == "")
				{
					valid = false;
					errMsg += 'Please enter a Payment Amount.\n';
				}
				
					

				
				if (terms != true)
				{
					valid = false;
					errMsg += 'You must agree with the terms and conditions.\n';
				}
				

					
				
	
		
			// errors?, show them to user and stop processing form		
			if(!valid) 
				alert(errMsg); 
			return valid;
			}


function validateSaveForm()
			{
	

   	document.paymentForm.action = "savePage.php";
			var firstName = document.paymentForm.FIRSTNAME.value; 
			var lastName = document.paymentForm.LASTNAME.value; 
      var emailAddress = document.paymentForm.EMAIL.value;
				
				
			// define vars
			// remember to change form name after 'document'
			var valid = true; 
			var errMsg = "";

			

				if (firstName == "")
				{
					valid = false;
					errMsg += 'Please provide the client\'s First Name.\n';
				}
			
				if (lastName == "")
				{
					valid = false;
					errMsg += 'Please provide the client\'s Last Name.\n';
				}
	

				if (emailAddress == "")
				{
					valid = false;
					errMsg += 'Please enter the client\'s  Email Address.\n';
				}
			
			
	

					

	
		
			// errors?, show them to user and stop processing form		
			if(!valid) 
				alert(errMsg); 
			return valid;
			}



function changeStates(elm) {

	
	if (elm.value == "US") {
			document.getElementById("STATEDIVUS").style.visibility = 'visible';
			document.getElementById("STATEDIVCA").style.visibility = 'hidden';
		
		} else{
			
			if	(elm.value == "CA") {		
			    document.getElementById("STATEDIVCA").style.visibility = 'visible';
			    document.getElementById("STATEDIVUS").style.visibility = 'hidden';
       } else {
			
        document.getElementById("STATEDIVUS").style.visibility = 'hidden';
			  document.getElementById("STATEDIVCA").style.visibility = 'hidden';
			}
			
			
		}
		
		
	

	}
	




function toggleVis(currElem){
if(document.layers){ // Netscape 4+
dom = document.layers[currElem].style;
}else if(document.getElementById){ // Netscape 6+, gecko, IE 5+
dom = document.getElementById(currElem).style;
}else if(document.all){ // IE 4+
dom = document.all[currElem].style;
}else{ // Browser unknown; do nothing
return ;
}

if (dom.display == "" || dom.display == "block" || dom.display == "table-row"){
dom.display = "none";
dom.visibility = "hidden";
}else{
dom.display = "";
dom.visibility = "visible";

}
}



function declineInsurance(price,totalprice,totalpricewinsurance,isdeposit) {
	
	
	document.getElementById("insuranceCell").innerHTML = "Declined&nbsp;";
	
	document.getElementById("insuranceCell2").innerHTML = "&nbsp;&nbsp;&nbsp;<a href=\"JavaScript:AddInsurance('" + price + "','" + totalprice + "','" + totalpricewinsurance + "','" + isdeposit + "');\">(Click to Add Insurance)</a>";
	
	document.getElementById("totalPricewInsurance").innerHTML = totalprice;
	
	if (isdeposit == "") {
		document.getElementById("DIVPaymentAmount").innerHTML = totalprice;
}
	
	document.getElementById("insurance").value = "N"; 


	
	}
	
	

function AddInsurance(price,totalprice,totalpricewinsurance,isdeposit) {
	
	document.getElementById("insuranceCell").innerHTML = "$<span id=\"totalInsurance\">" + price + "</span>&nbsp;";
	
	document.getElementById("insuranceCell2").innerHTML = "&nbsp;&nbsp;<a href=\"JavaScript:declineInsurance('" + price + "','" + totalprice + "','" + totalpricewinsurance + "','" + isdeposit +  "');\">(Click to Decline Insurance)</a>";
	
	document.getElementById("totalPricewInsurance").innerHTML = totalpricewinsurance;
	if (isdeposit == "") {
		document.getElementById("DIVPaymentAmount").innerHTML = totalpricewinsurance;
}
	document.getElementById("insurance").value = "Y"; 

	
	}	
	
	

var dtCh= "/";
var minYear=1900;
var maxYear=2100;

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 stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){

	var DateError = "";
	var daysInMonth = DaysArray(12);
	var pos1=dtStr.indexOf(dtCh);
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	var strMonth=dtStr.substring(0,pos1);
	var strDay=dtStr.substring(pos1+1,pos2);
	var strYear=dtStr.substring(pos2+1);
	strYr=strYear;
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1);
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1);
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);
	if (pos1==-1 || pos2==-1){
		DateError = "Birthday: The date format should be : mm/dd/yyyy.\n";
	}
	if (strMonth.length<1 || month<1 || month>12){
		DateError = "Birthday: Please enter a valid month.\n";
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		DateError = "Birthday: Please enter a valid day.\n";
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		DateError = "Birthday: Please enter a valid 4 digit year between "+minYear+" and "+maxYear + ".\n";

	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		DateError = "Birthday: Please enter a valid date.\n";
	}
return DateError;
}



