var originalFormAction = '';

function customRange(input) {
    return {minDate: (input.id == "search_enddate" ? $("#search_startdate").datepicker("getDate") : null),
        maxDate: (input.id == "search_startdate" ? $("#search_enddate").datepicker("getDate") : null)};
}

$(document).ready(function() {
	if($('#search_startdate').exists() && $('#search_enddate').exists()){
		$("#search_startdate,#search_enddate").datepicker({  dateFormat: iwp_short_date_format,   beforeShow: customRange,     showOn: "both",     buttonImage: iwp_global_tpl_path + "/images/calendar.gif",     buttonImageOnly: true });
	}
/*
	$('.datepick-startdate').bind(
		'dpClosed',
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			if (d) {
				d = new Date(d);
				$('.datepick-enddate').dpSetStartDate(d.addDays(1).asString());
			}
		}
	);
	$('.datepick-enddate').bind(
		'dpClosed',
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			if (d) {
				d = new Date(d);
				$('.datepick-startdate').dpSetEndDate(d.addDays(-1).asString());
			}
		}
	);*/

	originalFormAction = $('#advancedSearchForm').attr('action');
});



function CheckSearchForm(){
	var search = $('#search_Query').val();
	search = search.replace(/ /g, '+');
	search = escape(search);

	$('#advancedSearchForm').attr('action',  originalFormAction + '/' + search);
	$('#advancedSearchForm').submit();
}

function ToggleSearchForm(action){
	if(action == 'show'){
		$('.tplflag-searchAdvancedOption').show('fast');
		$('#toggleSearchOptionsLinkShow').hide();
		$('#toggleSearchOptionsLinkHide').show();
		$('#advancedSearchForm_searchadvanced').val(1);
	}else if(action == 'hide'){
		$('.tplflag-searchAdvancedOption').hide('fast');
		$('#toggleSearchOptionsLinkShow').show();
		$('#toggleSearchOptionsLinkHide').hide();
		$('#advancedSearchForm_searchadvanced').val(0);
	}
}


function ShowSearchOperators(){
	$('#search_operators').show('fast');
	$('#hideoperators').show();
	$('#showoperators').hide();
	HideSearchExamples();
}

function HideSearchOperators(){
	$('#search_operators').hide('fast');
	$('#showoperators').show();
	$('#hideoperators').hide();
}

function ShowSearchExamples(){
	$('#search_examples').show('fast');
	$('#hideexamples').show();
	$('#showexamples').hide();
	HideSearchOperators();
}

function HideSearchExamples(){
	$('#search_examples').hide('fast');
	$('#showexamples').show();
	$('#hideexamples').hide();
}
