	// fixing size of input fields for older versions of firefox
	$(document).ready(function() {

		if ($.browser.mozilla && jQuery.browser.version.substr(0,3)<1.9) {
			$('#fieldx_first').css("width", "150px");
			$('#fieldx_last').css("width", "150px");
		}
	});
	
	/*
	* parse date 
	*/
	function parse_cal_date(x, a, b, c)
	{
		if (document.getElementById && (
				document.getElementById(x) &&
				document.getElementById(a) &&
				document.getElementById(b) &&
				document.getElementById(c)
				)
			) {
				xx = document.getElementById(x);
				aa = document.getElementById(a);
				bb = document.getElementById(b);
				cc = document.getElementById(c);
				if (aa.value && bb.value && cc.value){
					xx.value = aa.value + '-' + bb.value + '-' + cc.value;
				}
		}else {
			return false;
		}
	}


	/*
	* parse date 2
	*/
	function parse_sel_date(x, a, b, c)
	{
		if (document.getElementById && (
				document.getElementById(x) &&
				document.getElementById(a) &&
				document.getElementById(b) &&
				document.getElementById(c)
				)
			) {
				mydate = document.getElementById(x).value;
				aa = document.getElementById(a);
				bb = document.getElementById(b);
				cc = document.getElementById(c);
				// re = /[0-9]{2}/g;
				//re = /^  (\d{2})\D(\d{2})\D(\d{4})  $/;
				re = /(\d{2})-(\d{2})-(\d{4})/;
				myArray = mydate.match(re);
				//alert(myArray);
				if (myArray){
					aa.value = myArray[1];
					bb.value = myArray[2];
					cc.value = (myArray[3]);
				}
		}else {
			return false;
		}
	}
	
	/*
	function processDatesInField () {
	   var dField = document.getElementById('s_date_c');
	   var aField = document.getElementById('e_date_c');
	   try {
				re = /(\d{2})-(\d{2})-(\d{4})/;
				smyArray = dField.match(re);
				if (smyArray){
					var sssDate = new Date(Date.UTC(smyArray[3], parseFloat(smyArray[1])-1, parseFloat(smyArray[2]), 0, 0, 0));
					startDate = sssDate;
				}
				emyArray = aField.match(re);
				if (emyArray){
					var eeeDate = new Date(Date.UTC(emyArray[3], parseFloat(emyArray[1])-1, parseFloat(emyArray[2]), 0, 0, 0));
					endDate = eeeDate;
				}
		  //startDate = Zapatec.Date.parseDate(dField.value, "%Y-%m-%d ");
		  //endDate = Zapatec.Date.parseDate(aField.value, "%Y-%m-%d ");
	   }
	   catch (e) {
		  if (console)
			 console.log (e);
	   }
	}
	*/

	var startDate;
	var endDate;
	var callbacks = 0;


	function resetDates() {
		startDate = endDate = null;
	}


	/*
	* Given two dates (in seconds) find out if date1 is bigger, date2 is bigger or
	 * they're the same, taking only the dates, not the time into account.
	 * In other words, different times on the same date returns equal.
	 * returns -1 for date1 bigger, 1 for date2 is bigger 0 for equal
	 */

	function compareDatesOnly(date1, date2) {
		var year1 = date1.getYear();
		var year2 = date2.getYear();
		var month1 = date1.getMonth();
		var month2 = date2.getMonth();
		var day1 = date1.getDate();
		var day2 = date2.getDate();

		if (year1 > year2) {
			return -1;
		}
		if (year2 > year1) {
			return 1;
		}

		//years are equal
		if (month1 > month2) {
			return -1;
		}
		if (month2 > month1) {
			return 1;
		}

		//years and months are equal
		if (day1 > day2) {
			return -1;
		}
		if (day2 > day1) {
			return 1;
		}

		//days are equal
		return 0;


		/* Can't do this because of timezone issues
		var days1 = Math.floor(date1.getTime()/Date.DAY);
		var days2 = Math.floor(date2.getTime()/Date.DAY);
		return (days1 - days2);
		*/
	}


	function filterDates1(cal) {
		startDate = cal.date;
		/* If they haven't chosen an 
		end date before we'll set it to the same date as the start date This
		way if the user scrolls in the start date 5 months forward, they don't
		need to do it again for the end date.
		*/

		if (endDate == null) { 
			  Zapatec.Calendar.setup({
				titleHtml         : "Check-Out Date",
				firstDay          : 1,
				date              : startDate,
				weekNumbers       : false,
				electric          : false,
				singleClick		  : true,
				inputField        : "e_date_c",
				button            : "e_trigger_c",
				daFormat		  : "%m-%d-%Y ",
		        ifFormat          : "%m-%d-%Y ",
				align             : "BR",
				controlMonth	  : 2,
				numberMonths      : 2,
				monthsInRow       : 2,
				//saveDate       	  : 2,  		  // save for two days
				showsTime         : false,        //no time
				noHelp			  : true,
				disableFunc    	  : dateInRange2, //the function to call
				onUpdate          : filterDates2
			  });
	
				parse_cal_date('e_date_c', 's_date_sel_mo', 's_date_sel_da', 's_date_sel_yr');
				parse_sel_date('e_date_c', 'e_date_sel_mo', 'e_date_sel_da', 'e_date_sel_yr');
		}
	}
	
	function filterDates2(cal) {
		endDate = cal.date;
	}
	
	
	/*
	* Both functions disable and hilight dates.
	*/
	
	/* 
	* Can't choose days after the
	* end date if it is choosen, hilights start and end dates with one style and dates between them with another
	*/
	function dateInRange1(date) {
	
		if (endDate != null) {

			// Disable dates after end date
			var compareEnd = compareDatesOnly(date, endDate);
			if  (compareEnd < 0) {
				return (true);
			}

			// Hilight end date with "edges" style
			if  (compareEnd == 0) {
				{return "edges";}
			}


			// Hilight inner dates with "between" style
			if (startDate != null){
				var compareStart = compareDatesOnly(date, startDate);
				if  (compareStart < 0) {
					return "between";
				} 
			} 
		}

		//disable days prior to today
		var today = new Date();
		var compareToday = compareDatesOnly(date, today);
		if (compareToday > 0) {
			return(true);
		}


		//all other days are enabled
		return false;
		//alert(ret + " " + today + ":" + date + ":" + compareToday + ":" + days1 + ":" + days2);
		return(ret);
	}

	/* 
	* Can't choose days before the
	* start date if it is choosen, hilights start and end dates with one style and dates between them with another
	*/

	function dateInRange2(date) {
	
		//processDatesInField();
	
		if (startDate != null) {
			// Disable dates before start date
			var compareDays = compareDatesOnly(startDate, date);
			if  (compareDays < 0) {
				return (true);
			}

			// Hilight end date with "edges" style
			if  (compareDays == 0) {
				{return "edges";}
			}

			// Hilight inner dates with "between" style
			if ((endDate != null) && (date > startDate) && (date < endDate)) {
				return "between";
			} 
		} 

		var now = new Date();
		if (compareDatesOnly(now, date) < 0) {
			return (true);
		}

		//all other days are enabled
		return false;
	}
	// end hiding contents from old browsers  -->
