function validate_form(formname) {
	valid = true;
	var frm = eval("document."+formname);
	if (frm.arrivaldate.value == "" || frm.arrivaldate.value == "mm/dd/yyyy" || frm.arrivaldate.value == "Arrival") {
		alert ( "Please fill in the 'Arrival' box." );
		valid = false;
	}
	if (frm.departuredate.value == "" || frm.departuredate.value == "mm/dd/yyyy" || frm.arrivaldate.value == "Depaarture") {
		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() {
	// This function auto-loads the child ages fields for the Vacation Specials page
	var myRegExp = /vacation-specials/;
	var matchPos1 = document.location.href.search(myRegExp);
	if(matchPos1 != -1) {
		var x=document.getElementsByName("packageid");
		var pkgIdArray = new Array();
		for (i=0;i<x.length;i++) {
			pkgIdArray.push(x[i].value);
		} 
		for (var i=0; i<pkgIdArray.length; i++) {
			specials_display_childages(pkgIdArray[i]);
		}
	}
}


var selectStyled = false;

function display_childages() {
	if (document.demoform) { // if widget is on screen and active
		// run styling once
		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');
				}
			}
		}
		if (!selectStyled) {
			$('select.pulldown').customStyle();
			selectStyled = true;
		}

	}
}

function specials_display_childages(pkgId) {
	var childcount = eval("document.search_package"+pkgId+".childcount.value");
	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');
			}
		}
	}
}

var custom_selectStyled = false;

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');
			}
		}
	}
	if (!custom_selectStyled) {
		$('select.custom_pulldown').customStyle2();
		custom_selectStyled = true;
	}

}




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;
}

