$(function () {
	/*
	$('#startdate').datepicker({ defaultDate: +0, changeMonth: false, changeYear: false, showOn: 'both', buttonImageOnly: true, buttonImage: '/images/calendar.png', yearRange: '-0:+3', dateFormat: 'dd/mm/yy',  constrainInput: true });
        $('#enddate').datepicker({ defaultDate: +1, changeMonth: false, changeYear: false, showOn: 'both', buttonImageOnly: true, buttonImage: '/images/calendar.png', yearRange: '-0:+3', dateFormat: 'dd/mm/yy',  constrainInput: true });
	$('#startdate').bind(
		'dpClosed',
		function(e, selectedDates)
		{
		alert(d.addDays(1).asString());
			var d = selectedDates[0];
			if (d) {
				d = new Date(d);
				$('#enddate').dpSetStartDate(d.addDays(1).asString());
				
			}
		}
	);
	$('#enddate').bind(
		'dpClosed',
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			if (d) {
				d = new Date(d);
				$('#startdate').dpSetEndDate(d.addDays(-1).asString());
			}
		}
	);
	*/

	$('#startdate,#enddate').datepick({
		onSelect: customRange,
		showTrigger: '<img src="/images/calendar.png" alt="Calendar" />',
		pickerClass: 'noPrevNext',
		changeMonth: false
		}); 
	function customRange(dates) { 
		if (this.id == 'startdate') { 
			$('#enddate').datepick('option', 'minDate', dates[0] || null); 
		}else {
			$('#startdate').datepick('option', 'maxDate', dates[0] || null); 
		}
	}

	var optionsVillaSearch = {
		target: '.errorSearchBox',   // target element(s) to be updated with server response 
		beforeSubmit: showRequestVillaSearch,  // pre-submit callback 
		success: showResponseVillaSearch,  // post-submit callback 
		error: showErrorVillaSearch
	};

	var optionsRefSearch = {
		//target: '.errorBox',   // target element(s) to be updated with server response 
		beforeSubmit: showRequestVillaRefSearch,  // pre-submit callback 
		success: showResponseVillaRefSearch,  // post-submit callback 
		error: showErrorVillaRefSearch
	};
	
	$("#frmVillaRefSearch").validate({
		errorLabelContainer: $(".errorRefSearchBox"),
		focusInvalid: false,
		wrapper: 'span',
		rules: {
			reference_no: { required: true }
		},
		messages: {
			reference_no: { required: "Please enter a Reference Number" }
			
		},
		submitHandler: function (form) {
			$('#frmVillaRefSearch').ajaxSubmit(optionsRefSearch);
		}
	});

	function showRequestVillaRefSearch(formData, jqForm, options) {
		//var queryString = $.param(formData);
		//alert('About to submit: \n\n' + queryString);
		//$('#boxSearchVilla').fadeOut();
		//$('.errorBox').html('please wait whilst searching...<img src="/images/ajax-loader.gif" />');
		$.prompt(villaSearchRefPopUp);
		//$.prompt('boo');
		return true;
	}
	function showResponseVillaRefSearch(responseText, statusText, xhr, $form) {
		//alert(responseText);
		var arrResponse = responseText.split("|");
		var bResponse = arrResponse[0];
		var villaID = arrResponse[1];
		if (bResponse == "success") {
			$.prompt.goToState('state1');
			location.href='/details.asp?id=' + villaID;
			//$.prompt.goToState('state1');
			//alert(responseText);
			//$.ajax({
			//	type: "POST",
			//	url: "/results.asp,
			//	data: { villas : responseText },
			//	success: function (theResponse) {
			//		if(theResponse=="success"){
			//			$.prompt.goToState('state2');
			//		}else{
			//			$.prompt.goToState('state4');
			//		}
			//	}
			//});
		} else {
			$.prompt.goToState('state2');
			//$('#boxSearchRefVilla').fadeIn();
		}
		return true;
	}
	function showErrorVillaRefSearch(responseText, statusText, xhr, $form) {
		alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + '\n\n');
	}
	var villaSearchRefPopUp = {
		state0: {
			html:'Searching...<img src="/images/ajax-loader.gif" />',
			focus: 1
		},
		state1: {
			html:'<p>Search completed. Please wait...<img src="/images/ajax-loader.gif" /></p>',
			focus: 1
		},
		state2: {
			html:'<p>The selected villa could not be found.</p>',
			focus: 1
		}
	};

	var villaSearchPopUp = {
		state0: {
			html:'Searching...<img src="/images/ajax-loader.gif" />',
			focus: 1
		},
		state1: {
			html:'<p>Search completed. Please wait...<img src="/images/ajax-loader.gif" /></p>',
			focus: 1
		}
	};
	$('.btnDetails').click(function(){
		var villaInfo = (this.id).split('_');
		var villaID = villaInfo[1];
		location.href = '/details.asp?id=' + villaID;
	});
	$("#frmVillaSearch").validate({
		errorLabelContainer: $(".errorSearchBox"),
		focusInvalid: false,
		wrapper: 'span',
		rules: {
			//startdate: { required: true },
			//enddate: { required: true }
		},
		messages: {
			//startdate: { required: "Please select a Date From" },
			//enddate: { required: "Please Select a Date To" }
			
		},
		submitHandler: function (form) {
			$('#frmVillaSearch').ajaxSubmit(optionsVillaSearch);
		}
	});
	function showRequestVillaSearch(formData, jqForm, options) {
		//var queryString = $.param(formData);
		//alert('About to submit: \n\n' + queryString);
		//$('#boxSearchVilla').fadeOut();
		//$('.errorBox').html('please wait whilst searching...<img src="/images/ajax-loader.gif" />');
		$.prompt(villaSearchPopUp);
		return true;
	}
	function showResponseVillaSearch(responseText, statusText, xhr, $form) {
		if (responseText == "success") {
			$.prompt.goToState('state1');
			location.href='/results.asp';
		} else {
			$.prompt.goToState('state1');
			$('#boxSearchVilla').fadeIn();
		}
		return true;
	}
	function showErrorVillaSearch(responseText, statusText, xhr, $form) {
		alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + '\n\n');
	}
});
