$(document).ready(function() {
	
	// submit page with url get vars based on shortcode finder fields
	$("#service_quick_find_button").click(function() {
		var action = $("#service_quick_find").attr('action');
		var append = "&search_page_4825_submit_button=Submit&current_result_page=1&results_per_page=1&submitted_search_category=&mode=";		
		var shortcode = $("#quickfind_shortcode").val();
		var date = $("#quickfind_date").val();
		var url = action + "?queries_shortcode_query=" + shortcode + "&search_date=" + Url.encode(date) + append;
		window.location = url;
	});
	
	// if search start and end dates exist in dom then manipulate content based on date range
	if ( $("#search_start_date").length > 0 && $("#search_end_date").length > 0 ) {
		// convert start  and end date into milliseconds since midnight of January 1, 1970
		var start = Date.parse($('#search_start_date').text()+"");
		var end = Date.parse($('#search_end_date').text()+"");
		// get the date entered from the url
		var date = getParameterByName('search_date'+"");
		// convert date entered into milliseconds since midnight of January 1, 1970
		var submit_date = Date.parse(Url.decode(date)+"");
		$('#search_date').remove(); // Matrix adds this hidden field,remove to avoid conflict
		if ( submit_date != 0 && submit_date != "" && submit_date <= end && submit_date >= start ) {
			// valid date and in range so results will show by default
		} else {
			$('#service_finder_results').css('display','none');
			$('#service_finder_no_date_match').css('display','block');
			$('#search_date_field').val(Url.decode(date));
		}	
	}
	
	$('#js_calendar_main').datepicker({
			altField: '#search_date_field',
			altFormat: 'dd/mm/yy',
			onSelect: function() {
				$('#js_calendar_rhs').css('display','none');
			}
	});
	
	$('#js_calendar_main').css('display','none');
	
	$('#js_calendar_main_button').click( function() {
		if ( $('#js_calendar_main').css('display') == 'none' ) {
			$('#js_calendar_main').css('display','block');
		} else {
			$('#js_calendar_main').css('display','none');
		}
	});
	
	$('#js_calendar_rhs').datepicker({
			altField: '#quickfind_date',
			altFormat: 'dd/mm/yy',
			onSelect: function() {
				$('#js_calendar_rhs').css('display','none');
			}
	});
	
	$('#js_calendar_rhs').css('display','none');
	
	$('#js_calendar_rhs_button').click( function() {
		if ( $('#js_calendar_rhs').css('display') == 'none' ) {
			$('#js_calendar_rhs').css('display','block');
		} else {
			$('#js_calendar_rhs').css('display','none');
		}
	});
	
	$('#search_date_field').val("");
	
	$('#quickfind_date').val("");
	
});
		
function getParameterByName(name) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/
 
var Url = {
 
	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},
 
	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}
