/************************************************************/
/************************************************************/
$(document).ready(function () {
	
	/************************************************************/
	//Ajax populate town drop down when entering page
	if( $("select#searchprovince").val() != "" ) {		
		setTown ( $("select#searchprovince").val(), $("#returnsearchtown").val() );
	}
	
	/************************************************************/
	//Ajax populate town drop down when province selected
	$("select#searchprovince").change(function(){
		setTown ( $(this).val(), "-1" );
	})				
	
	$("#searchprovince").change(function() { $("#searchprovince").blur(); });
	
})

/************************************************************/
/************************************************************/

function setTown ( selectedProvince, selectedTown ) {
	
	if( selectedProvince == "" ) {			
		var options = '<option value="">Select Town</option>';		
		$("#searchtown").html(options);			
		$("#searchtown").attr("disabled", true);
		$('#searchtown').disabled = true; 
	} else {
						 
		$("#submitregionsearch").attr("value", "Loading...");
		$("#submitregionsearch").attr("disabled", "true"); 
		$("#searchtown").removeAttr("disabled");
		
		var options = '<option value="">Loading...</option>';
		$("#searchtown").html(options);
		$.getJSON("/includes/wheretostay/getregionsfromprovince.php",{province: selectedProvince}, function(j){
			var options = '';
			options += '<option value="-1">All Towns</option>';				
	
			for (var i = 0; i < j.length; i++) {			
				if( selectedTown.toLowerCase() == j[i].optionDisplay.toLowerCase() ) {
					options += '<option value="' + j[i].optionValue + '" selected="selected">' + j[i].optionDisplay + '</option>';					
				} else {
					options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
				}
			}
			$("#searchtown").html(options);
			
			$("#submitregionsearch").attr("value", "Search");
			$("#submitregionsearch").removeAttr("disabled");
		})
	}	
	
}

/************************************************************/
/************************************************************/
function setValidation( type ) {

	$("#searchprovince").blur();
	$("#searchtown").blur();
	$("#namesearch").blur();	
	
	var validator = $("#wheretostayform").validate({
		messages: {
			searchprovince: { required: "Please select a province" },
			namesearch: { required: "Please enter an establishment name" }
		}
	});	
	
	validator.hideErrors();
	
	$("#searchprovince").rules("remove");
	$("#namesearch").rules("remove");
		
	if( type == "region" ) {
		$("#searchprovince").rules("add", { required: true });		
	} else if( type == "name" ) {		
		$("#namesearch").rules("add", { required: true });
		
		
	}
}

/************************************************************/
/************************************************************/
function setSearchType( value ) {
	setValidation( value );
	with( document.wheretostayform ) {
		searchtype.value = value;
		if( value == "region" ) {					
			namesearch.value = "";
		}else if( value == "name" ) {
			searchprovince.selectedIndex = 0;
			
			var options = '<option value="0">Select Town</option>';		
			$("#searchtown").html(options);			
			$("#searchtown").attr("disabled", true); 	
			
			el = getElementsByName("accomodationtype[]");
			if( el ) {
				for(i=0;i<el.length;i++) el[i].checked = false;
			}
		}
	}
}

/************************************************************/
/************************************************************/
function resetSearch( ) {
	document.wheretostayform.searchstatus.value = "reset";
	document.wheretostayform.submit();
}

/************************************************************/
/************************************************************/

/************************************************************/
/************************************************************/

/************************************************************/
/************************************************************/