// validate form fields for refine search
function checkFields(submit) {
	//Validate start and end dates.
	var startDate, endDate;
	var startString=document.form.start_date.value;
	var isStartDate= !startString=="";
	if (isStartDate) {
		try {
			startDate=Date.fromUKFormat(startString); //Parse start date.
		} catch (e) { //Unparsable date.
			startDate=0;
		}
	}
	var endString=document.form.end_date.value;
	var isEndDate= !endString=="";
	if (isEndDate) {
		try {
			endDate=Date.fromUKFormat(endString); //Parse end date.
		} catch (e) { //Unparable date.
			endDate=0;
		}
	}
	
	var now=new Date();
	if (isStartDate && startDate<now) {
		alert("The start date you have provided is either not a valid date or is in the past.\nThe date format is day/month/year.");
		return false;
	}
	if (isEndDate && ((isStartDate && endDate<startDate) || (!isStartDate && endDate<now))) {
		alert("The end date you have provided is either not a valid date, pre-dates the start date or is in the past.\nThe date format is day/month/year.");
		return false;
	}
	if (submit) document.form.submit();
	return true;
} //ef


// validate form fields for a property request in search_refine.php
function checkRequestFields() {
	//Validate normal refine search fields.
	if (!checkFields(false)) return;
	//Validate e-mail address.
	if (check_email(document.form)) {
		var emailConfirm=document.form.emailConfirm.value;
		if (emailConfirm=="") {
			alert("Please confirm your email address.");
		} else if (document.form.email.value!=emailConfirm) {
			alert("The email address initially entered does not match the confirmation of the email address.");
		} else {
			//Everything OK.  Submit property request.
			document.form.submit();
		}
	}
} //ef

function check_email(form) {
	var email=form.email.value;
	if (email.length<1) {
		alert("Please provide an email address so that we can contact you.");
		return false;
	} else {
		var index=email.indexOf("@");
		var index2=email.lastIndexOf(".");
		if (index>0 && index2>index+1 && index2<email.length-2) {
			return true;
		} else { //Bad e-mail addess.
			alert("The email address you have entered is invalid because it is either missing an @ sign, a user name, a valid site name or a dot.");
			return false;
		}
	}
}

function propertyType(form,findValue) {
	if (document.getElementById('min_sleeps')!=null) {
		if (form.hotels.checked) {
			document.getElementById('min_sleeps_note').innerHTML='Not applicable to hotels';
		} else {
			document.getElementById('min_sleeps_note').innerHTML='';
		}
	}
		
	if (form.hotels.checked && !form.is_shared.checked && !form.selfcatering.checked) {
		if (document.getElementById('pricing_period')!=null && document.getElementById('min_price').innerHTML.toLowerCase()!=minPriceDaily.toLowerCase()) {
			document.getElementById('pricing_period').innerHTML='per night (double room)';		
			var newMinPrice=Math.round((parseFloat(form.min_price.options[Math.max(form.min_price.selectedIndex,0)].value)/7)/5)*5;
			var newMaxPrice=Math.round((parseFloat(form.max_price.options[Math.max(form.max_price.selectedIndex,0)].value)/7)/5)*5;
			document.getElementById('min_price').innerHTML=minPriceDaily;
			document.getElementById('max_price').innerHTML=maxPriceDaily;
			if (findValue) setPrices(form,newMinPrice,newMaxPrice);
		}
		disableControl(form,'min_sleeps');
	} else {
		var oldMinPrice=parseFloat(form.min_price.options[Math.max(form.min_price.selectedIndex,0)].value);
		if (document.getElementById('pricing_period')!=null && document.getElementById('min_price').innerHTML.toLowerCase()!=minPriceWeekly.toLowerCase() && oldMinPrice<200) {
			document.getElementById('pricing_period').innerHTML='per week';
			var newMinPrice=Math.round((oldMinPrice*7)/100)*100;
			var newMaxPrice=Math.round((parseFloat(form.max_price.options[Math.max(form.max_price.selectedIndex,0)].value)*7)/100)*100;
			document.getElementById('min_price').innerHTML=minPriceWeekly;
			document.getElementById('max_price').innerHTML=maxPriceWeekly;
			if (findValue) setPrices(form,newMinPrice,newMaxPrice);
		}
		enableControl(form,'min_sleeps');	
	}
}

function setPrices(form,minPrice,maxPrice) {
	var i;
	for (i=0; i<form.min_price.length; i++) {
		if (parseFloat(form.min_price.options[i].value)>=minPrice) break;
	}
	if (i==form.min_price.length) i--;
	form.min_price.selectedIndex=i;
	for (i=1; i<form.max_price.options.length; i++) {
		if (parseFloat(form.max_price.options[i].value)>=maxPrice) break;
	}
	if (i==form.max_price.options.length) i=0;
	form.max_price.selectedIndex=i;
}
