var today = new Date();
var cost = 351;
var cost_child = 316;

function writeLodgingFormTable(divID) {
	var f = document.forms.form1;
	switch(divID) {
		case "lodging_night_of" :
			var n = f.lodging_night_of_qty[f.lodging_night_of_qty.selectedIndex].value - 0;
			break;
		case "lodging_night_before" :
			var n = f.lodging_night_before_qty[f.lodging_night_before_qty.selectedIndex].value - 0;
			break;
		default :
			return false;
	}
	
	if(n == 0) {
		getRef(divID).innerHTML = "";
		return true;
	}
	var ar = new Array();
	ar.push('<blockquote><table cellpadding="1" cellspacing="0" border="0" class="lodgingFormTable">');
	ar.push('<tr><th>Room #</th><th>Room Type</th><th>Adults</th><th>Under 16</th><th>Rollaway</th></tr>');
	
	var i;
	for(i = 0; i < n; i++) {
		ar.push('<tr align="center">');
		ar.push('<td>' + (i+1) + '</td>');
		ar.push('<td><select name="' + divID + '_room_type_' + (i+1) + '" size="1"><option value="Double Non-smoking">Double Non-smoking</option><option value="Double Smoking">Double Smoking</option><option value="King Non-smoking">King Non-smoking</option><option value="King Smoking">King Smoking</option></select></td>');
		ar.push('<td><input type="text" class="textbox" name="' + divID + '_num_adults_' + (i+1) + '" value="1" size="2" maxlength="2" onblur="this.value=(this.value==\'\'||isNaN(this.value) ? 2 : this.value);" /></td>');
		ar.push('<td><input type="text" class="textbox" name="' + divID + '_num_children_' + (i+1) + '" value="0" size="2" maxlength="2" onblur="this.value=(this.value==\'\'||isNaN(this.value) ? 0 : this.value);" /></td>');
		ar.push('<td><input type="checkbox" name="' + divID + '_rollaway_' + (i+1) + '" value="1" /></td>');
		ar.push('</tr>');
	}
	ar.push('</table></blockquote>');
	getRef(divID).innerHTML = ar.join("\n");
	return true;

}


function updateMonthDays(month, year) {
	var day = new Day();
	var firstDay = new Date(month + "/01/" + year);

	var selectItem = document.forms.form1.tripdate_day;
	selectItem.options.length = 0;
	selectItem.options[0] = new Option("DAY", "#");
	

	
	
	if(month == "#") { return false; }
	else if(month == "3") {
		firstDay.setDate(20);
		firstDay = firstDay.getDay();
		var m = new Month();
		var n = m.getDays(month-1, year) -19;
		var i = 1;
		var s = "";
		var cntr = 19;
		
		for(i = 1; i <= n; i++) {
			s = (i + cntr);
			s += " " + day.nameAbbr(firstDay);
			s = s.toUpperCase();
			selectItem.options[i] = new Option(s, (i+cntr));
			firstDay = (firstDay == 6 ? 0 : firstDay + 1);
		}
	} else {
	
		firstDay = firstDay.getDay();
		var m = new Month();
		var n = m.getDays(month-1, year);
		var i = 1;
		var s = "";
		
		for(i = 1; i <= n; i++) {
			s = (i < 10 ? "0" + i : i);
			s += " " + day.nameAbbr(firstDay);
			s = s.toUpperCase();
			selectItem.options[i] = new Option(s, i);
			firstDay = (firstDay == 6 ? 0 : firstDay + 1);
		}
	}
		
};

function updateTotal() {
	var f = document.forms.form1;
	var adultTotal = cost;
	var childTotal = cost_child;
	adultTotal *= (f.participants.value-0);
	childTotal *= (f.participants_children.value-0);
	getRef("total_cost").innerHTML = FormatNumber(adultTotal, 2, 1, 0, 1);
	getRef("total_cost_children").innerHTML = FormatNumber(childTotal, 2, 1, 0, 1);
	getRef("total_trip_cost").innerHTML = FormatNumber(adultTotal -0 + childTotal - 0, 2, 1, 0, 1);
	f.total.value = adultTotal -0 + childTotal - 0;
}

function initForm() {
	
	var month = new Month();
	
	var f = document.forms.form1;
	f.onsubmit = function() { return false; }
	f.btnSubmit.onclick = doSubmit;
	
	f.tripdate_year.options[f.tripdate_year.options.length] = new Option(today.getFullYear(), today.getFullYear());
	f.tripdate_year.options[f.tripdate_year.options.length] = new Option(today.getFullYear()+1, today.getFullYear()+1);
	
	f.tripdate_month.onchange = function() { updateMonthDays(this.value, f.tripdate_year[f.tripdate_year.selectedIndex].value); }
	f.tripdate_year.onchange = function() { updateMonthDays(f.tripdate_month[f.tripdate_month.selectedIndex].value, this.value); }
	
	f.cc_month.options.length = 0;
	var str = "";
	f.cc_month.options[0] = new Option("MONTH", "#");
	f.cc_year.options[0] = new Option("YEAR", "#");
	
	var i, n;
	for(i = 1; i <= 12; i++) {
		str = (i < 10 ? "0" + i : i);
		str += " " + month.name(i-1);
		f.cc_month.options[i] = new Option(str, i);
	}
	n = today.getFullYear();
	for(i = 0; i < 10; i++) {
		f.cc_year.options[i+1] = new Option(n, n);
		n++;
	}
	n = arCountryList.length;
	f.country.options.length = 0;
	f.cc_country.options.length = 0;
	for(i = 0; i < n; i++) {
		s = arCountryList[i];
		switch(s) {
			case "United States" :
				f.country.options[i] = new Option(s, s, true, true);
				f.cc_country.options[i] = new Option(s, s, true, true);
				break;
			default :
				f.country.options[i] = new Option(s, s);
				f.cc_country.options[i] = new Option(s, s);
		}
	}
	
	f.lodging_night_before_qty.onchange = function() {
		writeLodgingFormTable("lodging_night_before");
	}
	f.lodging_night_of_qty.onchange = function() {
		writeLodgingFormTable("lodging_night_of");
	}
	
	updateMonthDays(f.tripdate_month[f.tripdate_month.selectedIndex].value, f.tripdate_year[f.tripdate_year.selectedIndex].value);
	writeLodgingFormTable("lodging_night_of");
	writeLodgingFormTable("lodging_night_before");
	f.participants.onchange = function() { updateTotal(); };
	f.participants_children.onchange = function() { updateTotal(); };
	updateTotal();
};

function doSubmit() {
	var f = document.forms.form1;
	
	if(f.tripdate_month.selectedIndex == 0 || f.tripdate_day.selectedIndex == 0) {
		window.alert("You must first select your Trip Date.");
		window.location.href = "#tripdate";
		return false;
	}
	
	var v = new Validation(f);
	if(!f.cc_address_same.checked) {
		v.addItem("cc_address", "Billing Address");
		v.addItem("cc_city", "Billing City");
		v.addItem("cc_state", "Billing State");
		v.addItem("cc_zip", "Billing Zip");
	}
	v.addItem("cc_cvv", "Credit Card Verification Code");
	v.addItem("cc_name", "Name on Credit Card");
	v.addItem("cc_number", "Credit Card Number");
	v.addItem("email", "E-mail", "email");
	if(!validateField("phone", f.phone_home.value) && !validateField("phone", f.phone_bus.value) && !validateField("phone", f.phone_cell.value)) {
		v.addItem("phone_home", "You must provide at least 1 Phone Number.");
	}
	v.addItem("zip", "Zip code");
	v.addItem("state", "State (or Province)");
	v.addItem("city", "City");
	v.addItem("address", "Address");
	v.addItem("last_name", "Last Name");
	v.addItem("first_name", "First Name");
	
	if(!v.validate()) {
		return false;
	}
	if(f.cc_month.selectedIndex == 0 || f.cc_year.selectedIndex == 0) {
		window.alert("Please provide Credit Card Expiration.");
		return false;
	} else if(	f.cc_year[f.cc_year.selectedIndex].value < today.getFullYear() || 
				f.cc_year[f.cc_year.selectedIndex].value == today.getFullYear() &&
				f.cc_month[f.cc_month.selectedIndex].value < (today.getMonth()+1)
			)
	{
		window.alert("Your Credit Card appears to be expired.");
		return false;
	}
	
	
	
	var confirmStr = "Your Total comes to $" + f.total.value + "\n" +
					"Please make sure you have read and understand our Cancellation and Refund policy.\n" +
					"Your trip is NOT confirmed until you have received confirmation by Rivers-Oceans.\n\n" +
					"Do you wish to Submit this Reservation?";
	if( window.confirm(confirmStr) ) {
		f.cc_number.value = f.cc_type.value + " " + f.cc_number.value;
		f.reservation_notes.value = (f.reservation_notes.value + "").substring(0, 499);
		f.action = "https://secure.rivers-oceans.com/1_day_canyon_reservation/processReservation.php";
		f.btnSubmit.disabled = true;
		f.btnSubmit.value = "Processing Reservation...";
		f.submit();
		return true;
	}
	
	return false;
};






