/***************************************************
	LH main form
***************************************************/
function checkLHForm(theform)   {
	var weeksel, msg="";
	weeksel = theform.sc_week.selectedIndex;
	if (weeksel > 2)   {
		if (theform.sc_flexibility.selectedIndex==0) msg = "Please choose a 'Flexibility' option..\n";
		if (theform.sc_duration.selectedIndex==0) msg = msg + "Please choose a 'Duration' option..\n";
		}
	if (msg!="") {
		alert(msg); 
		return false;		
		}
	else return true;
	}

function proceedLHForm()   {
	return true;
	}

/***************************************************
	week/flexibility/duration code
***************************************************/
	var flexselectvalueslist, flexselecttextlist, durationselectvalueslist,durationselecttextlist;
	flexselectvalueslist = "0|p7|m7";
	flexselecttextlist = "Selected week only|+7 days|-7 days";
	durationselectvalueslist = "7|14|21|28";
	durationselecttextlist = "7 nights|14 nights|21 nights|28 nights";

	function setFlexDurationToNull(sc_flexibility, sc_duration)  {
		sc_flexibility.length = 1;
		sc_flexibility[1] =  new Option("Select week first..", "0");
		sc_duration.length = 1;
		sc_duration[1] =  new Option("Select week first..", "0");
		}

	function setFlexDuration(sc_flexibility, sc_duration, selectedflexvalue, selecteddurationvalue)  {
		// flexibility field
		var selindex;

		thevaluesarray = flexselectvalueslist.split("|");
		thetextarray = flexselecttextlist.split("|");
		sc_flexibility.length = 1;

		for (i = 0; i <	thevaluesarray.length; i++)   {
			sc_flexibility[i + 1] = new Option(thetextarray[i], thevaluesarray[i]);
			if (selectedflexvalue==thevaluesarray[i]) selindex = i;
			}
		if (selectedflexvalue=="null"||selectedflexvalue=="") selindex = -1;
		sc_flexibility.selectedIndex=selindex + 1;

		// duration field
		thevaluesarray = durationselectvalueslist.split("|");
		thetextarray = durationselecttextlist.split("|");
		sc_duration.length = 1;
		for (i = 0; i <	thevaluesarray.length; i++)   {
			sc_duration[i + 1] = new Option(thetextarray[i], thevaluesarray[i]);
			if (selecteddurationvalue==thevaluesarray[i]) selindex = i;
			}		
		if (selecteddurationvalue=="null") selindex = -1;
		sc_duration.selectedIndex=selindex + 1;
		}

	function initialiseFlexDuration()   {
		doc = document.searchform;
		if (doc.sc_week.selectedIndex < 2) setFlexDurationToNull(doc.sc_flexibility, doc.sc_duration); 
		else setFlexDuration(doc.sc_flexibility, doc.sc_duration, document.getElementById("selectedflexvalue").innerHTML, document.getElementById("selecteddurationvalue").innerHTML);
		}

	function filter_flexduration()  {
		doc = document.searchform;
		selval = doc.sc_week.selectedIndex;
		if (selval < 2) setFlexDurationToNull(doc.sc_flexibility, doc.sc_duration); 
		else setFlexDuration(doc.sc_flexibility, doc.sc_duration, "null", "null");
		}

	var monthName=new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

	function change_weeks(theregion)  {
		var intCurrentMilliseconds, regionID, dayofweektest, doc, i, newdate; 

		doc = document.searchform;
		regionID = theregion.options[theregion.selectedIndex].value;

		// test the first week proper in the dropdown...
		//		if weeks CODs are saturdays and we have changed to Menorca then change weeks to Fridays,
		//		... or if we have changed to Monchique then change weeks to Thursdays,
		dayofweektest = doc.sc_week.options[2].text.substring(0,3);
		if (dayofweektest=="Sat")  {
			// Menorca
			if (regionID==43807) change_week_select(doc.sc_week, 1, -1, "Fri");
			if (regionID==44175) change_week_select(doc.sc_week, 2, -1, "Thurs");
			}
		else   {
			// we may be de-selecting Menorca or Monchique, therefore change back to a Saturday
			// Menorca
			if (dayofweektest=="Fri") change_week_select(doc.sc_week, 1, 1, "Sat");
			// Monchique
			if (dayofweektest=="Thu") change_week_select(doc.sc_week, 2, 1, "Sat");
			}
		}

	function change_week_select(sc_week, numdays, addsign, dayname)  {
		var thedate, newdate, newdate2; 
		var intADay = 24 * 60 * 60 * 1000;
		for (i=2;i<sc_week.length;i++)   {
			thedate = sc_week.options[i].value;
			newdate = new Date(thedate.substring(0,4), thedate.substring(5,7), thedate.substring(8,10)) ;
			newdate2 = new Date(newdate.getTime() + intADay * numdays * addsign);
			newvalue = newdate2.getFullYear() + "-" + sanitise_num_tostring(newdate2.getMonth()) + "-" + sanitise_num_tostring(newdate2.getDate());
			newtext = dayname + " " + newdate2.getDate() + " " + monthName[newdate2.getMonth() - 1] + " " + newdate2.getFullYear();
			sc_week.options[i] =  new Option(newtext, newvalue); 
			}		
		}

	function sanitise_num_tostring  (thenum)   {
		thestr=String(thenum);
		if (thestr.length==1) { thestr="0" + thestr; } 		
		return thestr;
		}
		


