/*Form Validation*/
function validateforms(theform) {
	var requiredFields = new Array();
	var intCounter = 0;
	var strWarningMessage = "Please enter the following compulsory fields:\n";
	var bgColour = "#E9CDD0";
	
	if(theform.title != null) {
		if(theform.title.value == '') {
			intCounter = intCounter + 1;
			requiredFields[intCounter] = "Title";
			document.getElementById("title").style.backgroundColor = bgColour;
		}
	}
	
	if(theform.first_name != null) {
		if(theform.first_name.value.length < 1) {
			intCounter = intCounter + 1;
			requiredFields[intCounter] = "First name";
			document.getElementById("first_name").style.backgroundColor = bgColour;
		}
	}
	
	if(theform.last_name != null) {
		if(theform.last_name.value.length < 1) {
			intCounter = intCounter + 1;
			requiredFields[intCounter] = "Last name";
			document.getElementById("last_name").style.backgroundColor = bgColour;
		}
	}
	
	
	if (theform.email != null) {
		var strEmail = theform.email.value;
		var isEmail = ((strEmail.indexOf("@") != -1) && (strEmail.indexOf(".") != -1));
		
		if (theform.email.value.length < 1) {
			intCounter = intCounter + 1;
			requiredFields[intCounter] = "Email address";
			document.getElementById("email").style.backgroundColor = bgColour;
		}
		if (!isEmail && theform.email.value.length > 1) {
			intCounter = intCounter + 1;
			requiredFields[intCounter] = "A valid email address";
			document.getElementById("email").style.backgroundColor = bgColour;
		}
	}
	
	if(theform.day_tel != null) {
		if(theform.day_tel.value.length < 1) {
			intCounter = intCounter + 1;
			requiredFields[intCounter] = "Daytime tel no.";
			document.getElementById("day_tel").style.backgroundColor = bgColour;
		}
	}
	
	if(theform.address1 != null) {
		if(theform.address1.value.length < 1) {
			intCounter = intCounter + 1;
			requiredFields[intCounter] = "Home address";
			document.getElementById("address1").style.backgroundColor = bgColour;
		}
	}
	
	if(theform.town != null) {
		if(theform.town.value.length < 1) {
			intCounter = intCounter + 1;
			requiredFields[intCounter] = "Town";
			document.getElementById("town").style.backgroundColor = bgColour;
		}
	}
	
	if(theform.postcode != null) {
		if(theform.postcode.value.length < 1) {
			intCounter = intCounter + 1;
			requiredFields[intCounter] = "Postcode";
			document.getElementById("postcode").style.backgroundColor = bgColour;
		}
	}

	/*if(theform.contact_method != null) {
		if(theform.contact_method.selectedIndex == 0) {
			intCounter = intCounter + 1;
			requiredFields[intCounter] = "Contact method";
			document.getElementById("contact_method").style.backgroundColor = bgColour;
		}
	}*/
	
	if(theform.mobile_tel != null && theform.contact_method != null) {
		if(theform.contact_method.value == 'Mobile' && theform.contact_method != null && theform.mobile_tel.value.length < 1) {
			intCounter = intCounter + 1;
			requiredFields[intCounter] = "Mobile no.";
			document.getElementById("mobile_tel").style.backgroundColor = bgColour;
		}
	}
	
	if(theform.hear_about != null) {
		if(theform.hear_about.selectedIndex == 0) {
			intCounter = intCounter + 1;
			requiredFields[intCounter] = "Hear about";
			document.getElementById("hear_about").style.backgroundColor = bgColour;
		}
	}

	if(theform.number_in_party != null) {
		if(theform.number_in_party.selectedIndex == 0) {
			intCounter = intCounter + 1;
			requiredFields[intCounter] = "Number in party";
			document.getElementById("number_in_party").style.backgroundColor = bgColour;
		}
	}
	
	if(theform.number_in_party != null) {
		for (i = 1;i <= theform.number_in_party.value;i++) {
			if(document.getElementById("title" + i).value.length < 1) {
				intCounter = intCounter + 1;
				requiredFields[intCounter] = "Traveller " + i + "'s title";
				document.getElementById("title" + i).style.backgroundColor = bgColour;
			}
			if(document.getElementById("first_name" + i).value.length < 1) {
				intCounter = intCounter + 1;
				requiredFields[intCounter] = "Traveller " + i + "'s first name";
				document.getElementById("first_name" + i).style.backgroundColor = bgColour;
			}
			if(document.getElementById("last_name" + i).value.length < 1) {
				intCounter = intCounter + 1;
				requiredFields[intCounter] = "Traveller " + i + "'s last name";
				document.getElementById("last_name" + i).style.backgroundColor = bgColour;
			}
			
			if(document.getElementById("day" + i).value.length < 1) {
				intCounter = intCounter + 1;
				requiredFields[intCounter] = "Traveller " + i + "'s birth day";
				document.getElementById("day" + i).style.backgroundColor = bgColour;
			}
			else if(document.getElementById("day" + i).value < 1 || document.getElementById("day" + i).value > 31 || isNaN(document.getElementById("day" + i).value)) {
				intCounter = intCounter + 1;
				requiredFields[intCounter] = "A valid birth day for Traveller " + i;
				document.getElementById("day" + i).style.backgroundColor = bgColour;
			}
			
			if(document.getElementById("month" + i).value.length < 1) {
				intCounter = intCounter + 1;
				requiredFields[intCounter] = "Traveller " + i + "'s birth month";
				document.getElementById("month" + i).style.backgroundColor = bgColour;
			}
			else if(document.getElementById("month" + i).value < 1 || document.getElementById("month" + i).value > 12 || isNaN(document.getElementById("month" + i).value)) {
				intCounter = intCounter + 1;
				requiredFields[intCounter] = "A valid birth month for Traveller " + i;
				document.getElementById("month" + i).style.backgroundColor = bgColour;
			}
			
			if(document.getElementById("year" + i).value.length < 1) {
				intCounter = intCounter + 1;
				requiredFields[intCounter] = "Traveller " + i + "'s birth year";
				document.getElementById("year" + i).style.backgroundColor = bgColour;
			}
			else if(document.getElementById("year" + i).value.length > 4 || document.getElementById("year" + i).value.length < 4 || isNaN(document.getElementById("year" + i).value)) {
				intCounter = intCounter + 1;
				requiredFields[intCounter] = "A valid birth year for Traveller " + i;
				document.getElementById("year" + i).style.backgroundColor = bgColour;
			}
			
			if(document.getElementById("room_type" + i).selectedIndex == 0) {
				intCounter = intCounter + 1;
				requiredFields[intCounter] = "Traveller " + i + "'s room type";
				document.getElementById("room_type" + i).style.backgroundColor = bgColour;
			}
			if(document.getElementById("insurance" + i).selectedIndex == 0) {
				intCounter = intCounter + 1;
				requiredFields[intCounter] = "Traveller " + i + "'s insurance";
				document.getElementById("insurance" + i).style.backgroundColor = bgColour;
			}
		}
	}
	
	if(theform.card_name != null) {
		if(theform.card_name.value.length < 1) {
			intCounter = intCounter + 1;
			requiredFields[intCounter] = "Card name";
			document.getElementById("card_name").style.backgroundColor = bgColour;
		}
	}
	
	if(theform.card_number != null) {
		if(theform.card_number.value.length < 1) {
			intCounter = intCounter + 1;
			requiredFields[intCounter] = "Card number";
			document.getElementById("card_number").style.backgroundColor = bgColour;
		}
	}
	
	if(theform.card_type != null) {
		if(theform.card_type.selectedIndex == 0) {
			intCounter = intCounter + 1;
			requiredFields[intCounter] = "Card type";
			document.getElementById("card_type").style.backgroundColor = bgColour;
		}
	}
	
	if(theform.expiry_month != null) {
		if(theform.expiry_month.selectedIndex == 0) {
			intCounter = intCounter + 1;
			requiredFields[intCounter] = "Card expiry month";
			document.getElementById("expiry_month").style.backgroundColor = bgColour;
		}
	}
	
	if(theform.expiry_year != null) {
		if(theform.expiry_year.selectedIndex == 0) {
			intCounter = intCounter + 1;
			requiredFields[intCounter] = "Card expiry year";
			document.getElementById("expiry_year").style.backgroundColor = bgColour;
		}
	}
	
	if(theform.bill_address1 != null) {
		if(theform.bill_address1.value.length < 1) {
			intCounter = intCounter + 1;
			requiredFields[intCounter] = "Billing address 1";
			document.getElementById("bill_address1").style.backgroundColor = bgColour;
		}
	}
	
	if(theform.bill_town_city != null) {
		if(theform.bill_town_city.value.length < 1) {
			intCounter = intCounter + 1;
			requiredFields[intCounter] = "Billing town";
			document.getElementById("bill_town_city").style.backgroundColor = bgColour;
		}
	}
	
	if(theform.bill_postcode != null) {
		if(theform.bill_postcode.value.length < 1) {
			intCounter = intCounter + 1;
			requiredFields[intCounter] = "Billing postcode";
			document.getElementById("bill_postcode").style.backgroundColor = bgColour;
		}
	}
	
	if(theform.confirm_read_conditions != null) {
		if(theform.confirm_read_conditions.checked == false) {
			intCounter = intCounter + 1;
			requiredFields[intCounter] = "Read booking condtions";
			document.getElementById("confirm_read_conditions").style.backgroundColor = bgColour;
		}
	}

	if(theform.confirm_read_privacy_policy != null) {
		if(theform.confirm_read_privacy_policy.checked == false) {
			intCounter = intCounter + 1;
			requiredFields[intCounter] = "Read privacy policy";
			document.getElementById("confirm_read_privacy_policy").style.backgroundColor = bgColour;
		}
	}
	
	if(theform.travel_insurance != null) {
		if(theform.travel_insurance.checked == false) {
			intCounter = intCounter + 1;
			requiredFields[intCounter] = "insurance condtions";
			document.getElementById("travel_insurance").style.backgroundColor = bgColour;
		}
	}
	
	if (theform.tour_theme != null && theform.tour_associated_with != null && theform.tour_departure_month != null) {
		if (theform.tour_theme.value.length < 1 && theform.tour_associated_with.value.length < 1 && theform.tour_departure_month.value.length < 1) {
			intCounter = intCounter + 1;
			requiredFields[intCounter] = "At least one of the list options must be selected";
			document.getElementById("tour_theme").style.backgroundColor = bgColour;
			document.getElementById("tour_associated_with").style.backgroundColor = bgColour;
			document.getElementById("tour_departure_month").style.backgroundColor = bgColour;
			}
		else  {
			createCookie("tour_theme",theform.tour_theme.options[theform.tour_theme.selectedIndex].text,"14");
			createCookie("tour_associated_with",theform.tour_associated_with.options[theform.tour_associated_with.selectedIndex].text,"14");
			createCookie("tour_departure_month",theform.tour_departure_month.options[theform.tour_departure_month.selectedIndex].text,"14");
			}
		}
	
	// these params used by hotel search, special offers page and events page
	if (theform.areaid != null && theform.destinationid != null)  {
		createCookie("areaid",theform.areaid.options[theform.areaid.selectedIndex].text,"14");
		createCookie("destinationid",theform.destinationid.options[theform.destinationid.selectedIndex].text,"14");
		// these params used by hotel page
		if (theform.hotel_activity != null)  {
			createCookie("hotel_activity",theform.hotel_activity.options[theform.hotel_activity.selectedIndex].text,"14");
			createCookie("starrating",theform.starrating.options[theform.starrating.selectedIndex].text,"14");
			}
		// these used by events page
		if (theform.event_activity != null)  {
			createCookie("event_activity",theform.event_activity.options[theform.event_activity.selectedIndex].text,"14");
			createCookie("event_start_month",theform.event_start_month.options[theform.event_start_month.selectedIndex].text,"14");
			}
		// search BL can't cope with an areaID and destID so make areaID nothing if there is a dest ID 
		if (theform.destinationid.selectedIndex > 0)  {
			theform.areaid.selectedIndex = 0;
			}
		}

	// event departure month
	if(theform.event_start_month != null) {
		if(theform.event_start_month.selectedIndex == 0) {
			intCounter = intCounter + 1;
			requiredFields[intCounter] = "Departure month";
			document.getElementById("event_start_month").style.backgroundColor = bgColour;
		}
	}

	if(theform.brochure1 != null) {
		if(theform.brochure1.checked == false && theform.brochure2.checked == false && theform.brochure3.checked == false && theform.brochure4.checked == false && theform.brochure5.checked == false && theform.brochure6.checked == false && theform.brochure7.checked == false && theform.brochure8.checked == false) {
			intCounter = intCounter + 1;
			requiredFields[intCounter] = "Select a brochure";
			document.getElementById("brochuretitle1").style.backgroundColor = bgColour;
			document.getElementById("brochuretitle2").style.backgroundColor = bgColour;
			document.getElementById("brochuretitle3").style.backgroundColor = bgColour;
			document.getElementById("brochuretitle4").style.backgroundColor = bgColour;
			document.getElementById("brochuretitle5").style.backgroundColor = bgColour;
			document.getElementById("brochuretitle6").style.backgroundColor = bgColour;
			document.getElementById("brochuretitle7").style.backgroundColor = bgColour;
			document.getElementById("brochuretitle8").style.backgroundColor = bgColour;
		}		
	}
	
	//validate ebrochure sections
	/*if(theform.all_sections != null) {
		var numberboxes = theform.brochureboxes.length;
		var txt = "";
		
		if(theform.brochureboxes.value == undefined)
		{
			for (i = 0; i < numberboxes; i++) 
			{
				if (theform.brochureboxes[i].checked) 
				{
					txt = txt + theform.brochureboxes[i].value + " "
				}
			}

			if (txt == '') 
			{
				intCounter = intCounter + 1;
				requiredFields[intCounter] = "Select a brochure section";
			}
		}
		else
		{
			if (!theform.brochureboxes.checked) 
			{
				intCounter = intCounter + 1;
				requiredFields[intCounter] = "Select a brochure section";
			}
		}
	}*/

	if (theform.tour_theme_form != null) {
		if(theform.tour_departure_month.value == '') {
			intCounter = intCounter + 1;
			requiredFields[intCounter] = "Select a departure month";
			document.getElementById("tour_departure_month").style.backgroundColor = bgColour;
		}
	}
	
	//Display warning message
	if (intCounter > 0) {
		for (i = 1; i <= intCounter; i++) {
			strWarningMessage = strWarningMessage + "\n" + requiredFields[i];
		}
		alert(strWarningMessage);
		return false;
	}
	return true;
}

/* CALENDAR/DATE FUNCTIONS */
// global variable
var monthName = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

// initially sets days, monoths and year dropdown for a DAY and MONTHYEAR <select> box pairing
function initializeDaysMonthsYear (theform, dayselement, monthsyearelement, selectedDate) 
{
	var objMonthsYearSelect, i, startmonth, theyear, fstartmonth, selMonth, selYear, selIndex;
	var dtToday = new Date();

	// set variables to current month and year
	theday = Number(dtToday.getDate());
	startmonth = dtToday.getMonth() + 1;
	theyear = dtToday.getFullYear();
	
	if (selectedDate != "") {
		theday = Number(selectedDate.substring(8,10));
		selMonth = Number(selectedDate.substring(5,7));
		selYear = Number(selectedDate.substring(0,4));
		theday = theday++;
		startmonth = selMonth;
		theyear = selYear;
		populateDays(theform, dayselement, theday, startmonth, theyear);
	}
	
	// populate days dropdown, dependent on current month and possibility of leap year (current year)
	if (dayselement != "") {
		populateDays(theform, dayselement, theday, startmonth, theyear);
	}
	
	eval("objMonthsYearSelect = document."+theform+"."+monthsyearelement);
	
	// clear any current option entries
	objMonthsYearSelect.length = 0;
	
	for (i = 1; i <= 12; i++) 
	{
		if (startmonth == 13) 
		{
			fstartmonth = "01";
			startmonth = 1;
			theyear++;
		}
		else
		{
			if (startmonth < 10) 
			{
				fstartmonth = "0" + startmonth;
			}
			else 
				fstartmonth = startmonth;
			}
			theval = theyear + "-" + fstartmonth;
			startmonthname = monthName[startmonth - 1];
			thetext = startmonthname + " " + theyear;
			objMonthsYearSelect.options[i-1] = new Option(thetext, theval);
			if (selectedDate != "") 
			{
				if (fstartmonth == selMonth && theyear == selYear) {
					selIndex = i - 1;
				}
			}
			startmonth++;
		}
	if (selectedDate != "") 
	{
		objMonthsYearSelect.selectedIndex = selIndex;
	}
}

// changes days dropdown when monthyear select box is changed
function updateDaysList(theform, dayselement, monthyear)
{
	var monthyeararray, arrivalday;
	monthyeararray = monthyear.split("-");	

	eval("arrivalday = document." + theform + "." + dayselement + ".options[document." + theform + "." + dayselement + ".selectedIndex].value;");

	// populate days dropdown, dependent on current month and possibility of leap year (current year)
	populateDays (theform, dayselement, arrivalday, Number(monthyeararray[1]), Number(monthyeararray[0]));
}

// updates days dropdown
function populateDays(theform, dayselement, theday, themonth, theyear)
{
	var objDaysSelect, i, maxdays;

	// need correct max days in month 
	switch(themonth)   {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12: maxdays = 31;
				break;
		case 2: if (theyear % 4 == 0) maxdays = 29;
				else maxdays = 28;
				break;
		case 4:
		case 6:
		case 9:
		case 11: maxdays = 30;
				break;
		}

	eval("objDaysSelect = document."+theform+"."+dayselement+";");


	// clear any current option entries
	objDaysSelect.length = 0;
	for (i = 1; i <= maxdays; i++)
	{
		if (i < 10) fi = "0" + i;
		else fi = i;
		if (i==theday) objDaysSelect.options[i-1] = new Option(i, fi, true, true);
		else objDaysSelect.options[i-1] = new Option(i, fi);
	}
}


/* sets value of select box */
function SetSelectedValue(options, value) 
{
	for (var i=0; i < options.length; i++)
	{ 
		if (options[i].value == value )
		{
			options[i].selected = true;
		}
	}
}
