function validate_form(formname) {
	valid = true;
	var frm = eval("document."+formname);
	if (frm.arrivaldate.value == "" || frm.arrivaldate.value == "Arrival") {
		alert ( "Please fill in the 'Arrival' box." );
		valid = false;
	}
	if (frm.departuredate.value == "" || frm.departuredate.value == "Departure") {
		alert ( "Please fill in the 'Departure' box." );
		valid = false;
	}
	var childcount = frm.childcount.value;
	if (childcount != 0) {
		for (var i=1; i<=childcount; i++) {
			if (eval("frm.child"+i+"age.value") == "") {
				alert("Please enter the child's age.");
				valid = false;
				break;
			}
		}
	}
	return valid;
}

function getValueFromIFrame(p_frame, p_fieldname) {
	var frame = document.getElementById(p_frame);
	return (frame.contentDocument.forms[0].elements[p_fieldname].value);
}

window.onload = function() {
	display_childages();
	var myRegExp = /vacation-specials/;
	var matchPos1 = document.location.href.search(myRegExp);
	if(matchPos1 != -1) {
		var pkgIdArray = new Array("7002","7005","7007");
		for (var i=0; i<pkgIdArray.length; i++) {
			specials_display_childages(pkgIdArray[i]);
		}
	}
}

function calculate_departuredate() {
	var startdate= new Date();
	startdate.setTime(Date.parse(document.customwidget.arrivaldate.value) + (12*60*60*1000));	
	var numberdays = document.customwidget.nights.value;
	if (isNaN(numberdays)) {
		alert('You must enter a number in the Nights field.');
	} else {
		var msPerDay = (24 * 60 * 60 * 1000);
		var enddate = new Date();
		enddate.setTime(startdate.getTime() + (msPerDay * numberdays));	
		endday = enddate.getDate();
		endmonth = enddate.getMonth();
		endyear = enddate.getYear();
		if ((endyear  <  200)   &&   (endyear  >  100))
			var endyear = endyear + 1900;
		if (endyear  <  100)
			var endyear = endyear + 2000;
		if (endmonth == 12)
			var endmonth = 1;
		else
			var endmonth = (endmonth + 1);
		if (endday  <  10)
			{var endday = '0' + Number(endday);}
		//if (endmonth  <  10)
		//	{var endmonth = '0' + Number(endmonth);}
		if (document.customwidget.arrivaldate.value != '') {
			document.customwidget.departuredate.value = endmonth + '/' + endday + '/' + endyear;
		}
	}
}


function display_childages() {
	if (document.demoform) { // if widget is on screen and active
		var childcount = document.demoform.childcount.value;
		for (var i=1; i<=8; i++) {
			if (childcount == 0) {
				change('childsagetable', 'cwAgesTable_hidden');
			} else {
				change('childsagetable', 'cwAgesTable_visible');
				change('childrow1', 'cwAgesRow_visible');
				if (i>4) {
					change('childrow2', 'cwAgesRow_visible');
				}else{
					change('childrow2', 'cwAgesRow_hidden');
				}
				if (i>childcount) {
					change('child'+i, 'cwAgesCell_hidden');
				}else{
					change('child'+i, 'cwAgesCell_visible');
				}
			}
		}
	}
}

function specials_display_childages(pkgId) {
	try {
	var childcount = eval("document.search_package"+pkgId+".childcount.value");
	}catch(err){ var childcount=0; }
	for (var i=1; i<=9; i++) {
		if (childcount == 0) {
			change('specials_childsagetable_'+pkgId, 'cwSpecialsAgesTable_hidden');
		} else {
			change('specials_childsagetable_'+pkgId, 'cwSpecialsAgesTable_visible');
			change('specials_childrow1_'+pkgId, 'cwAgesRow_visible');
			if (i>5) {
				change('specials_childrow2_'+pkgId, 'cwAgesRow_visible');
			}else{
				change('specials_childrow2_'+pkgId, 'cwAgesRow_hidden');
			}
			if (i>childcount) {
				change('specials_child'+i+'_'+pkgId, 'cwAgesCell_hidden');
			}else{
				change('specials_child'+i+'_'+pkgId, 'cwAgesCell_visible');
			}
		}
	}
}

function custom_display_childages() {
	var childcount = eval("document.customwidget.childcount.value");
	
	for (var i=1; i<=8; i++) {
		if (childcount == 0) {
			change('custom_ChildAgesTitle', 'cwAgesCell_hidden');
			change('custom_childsagetable', 'cwCustomAgesTable_hidden');
		} else {
			change('custom_ChildAgesTitle', 'cwAgesCell_visible');
			change('custom_childsagetable', 'cwCustomAgesTable_visible');
			change('custom_childrow1', 'cwAgesRow_visible');
			if (i>5) {
				change('custom_childrow2', 'cwAgesRow_visible');
			}else{
				change('custom_childrow2', 'cwAgesRow_hidden');
			}
			if (i>childcount) {
				change('custom_child'+i, 'cwAgesCell_hidden');
			}else{
				change('custom_child'+i, 'cwAgesCell_visible');
			}
		}
	}
}




function change(id, newClass) {
	if (newClass == "cwAgesCell_hidden") {
		// reset all hidden age fields
		var selectBox = identity.getElementsByTagName("select");
		if (selectBox[0]) {
			selectBox[0].selectedIndex = 0;
		}
	}
	identity=document.getElementById(id);
	identity.className=newClass;
}

function createarray(formname) {
	var theForm = eval("document."+formname);
	
	arrayvalue = '';
	for(var i = 0; i < document.attributes.length; i++) {
		if (document.attributes.elements[i].type == 'checkbox') {
			if (document.attributes.elements[i].checked == true) {
				arrayvalue = arrayvalue  + document.attributes.elements[i].value;
			}
		} else if (document.attributes.elements[i].name == 'numrooms') {
			if (document.attributes.elements[i].value != '') {
				arrayvalue = arrayvalue  + 'P,501,1,' + document.attributes.elements[i].value + ',';
			}
		} else if (document.attributes.elements[i].type == 'text') {
			arrayvalue = arrayvalue  + document.attributes.elements[i].name + ',' + document.attributes.elements[i].value;
		}else if (document.attributes.elements[i].type == 'radio') {
			if (document.attributes.elements[i].checked == true) {
				arrayvalue = arrayvalue  + document.attributes.elements[i].value;
			}
		} else {
			arrayvalue = arrayvalue  + document.attributes.elements[i].value;
		}	
	}	
	theForm.features.value =  arrayvalue;
	var domain = document.domain;
	var pos = domain.indexOf(".");
	pos = pos + 1;
	domain = domain.substring(pos);
	document.cookie = "features=" + arrayvalue + ";domain=" + domain + ";path=/";
	var childagearray;
	childagearray = theForm.child1age.options[theForm.child1age.selectedIndex].value + ',';
	childagearray = childagearray + theForm.child2age.options[theForm.child2age.selectedIndex].value + ',';
	childagearray = childagearray + theForm.child3age.options[theForm.child3age.selectedIndex].value + ',';
	childagearray = childagearray + theForm.child4age.options[theForm.child4age.selectedIndex].value + ',';
	childagearray = childagearray + theForm.child5age.options[theForm.child5age.selectedIndex].value + ',';
	childagearray = childagearray + theForm.child6age.options[theForm.child6age.selectedIndex].value + ',';
	childagearray = childagearray + theForm.child7age.options[theForm.child7age.selectedIndex].value + ',';
	childagearray = childagearray + theForm.child8age.options[theForm.child8age.selectedIndex].value + ',';
	theForm.childagearray.value = childagearray;
}

