// utility function to validate dates
/*function validDate(field) {
	this.field = field;
	_s  = field.value;
	_d = getDateFromField(field, dateFormatString);
	this.valid = true;
	if ( isNaN(_d.getDate() ) ) {  // Basically, this allows the functions to gracefully return to the format string instead of putting a date in the field.
			this.valid = false;
	}
	this.d = _d;
	this.setD = function(_d) {this.d = _d}
	this.setNonD = function(_s) {this.s = _s}
	this.getM = function() {
		var month = this.d.getMonth();
		return month;
	}
	this.getD = function() {
		var day = this.d.getDay();
		return day;
	}
	this.getY = function() {
		var y = (this.d.getYear() + 10000) % 100;
		y += (y < 38) ? 2000 : 1900;
		return y
	}
	this.setField = function() {
		writeDateToField(this.field, this.d, dateFormatString);
	}
	this.setAltField = function() {
		s = this.getY() +"-"+ lZero(this.d.getMonth() + 1 ) + "-" + lZero(this.d.getDate());
		field.value = s
		return s;
	}
	this.nextDay = function() {
		return this.d.setDate(this.d.getDate()+1);
	}
	this.diffDate = function(dd) {
		return (makeDate(this.d).getTime() - makeDate(dd).getTime()) / (1000*60*60*24);
	}
	function makeDate(md) {
		return new Date(md.getMonth() + 1 + "/" + md.getDate() + "/" + md.getYear());
	}

	function lZero(nr) {if (nr < 10) nr = "0" + nr;return nr;}
	return this;
}*/

function ciDate_onBlur(defaultValue) {
	var checkIn = new validDate(f.ciDate);

	if ( checkIn.valid ) {
		checkIn.setField();
		f.arrivalDate.value = f.ciDate.value;

		var checkInAlt = new validDate(f.arrivalDate);
		checkInAlt.setAltField(checkInAlt.d);

		var checkOut = new validDate(f.coDate);

		if ( !(checkOut.valid) || (checkOut.d.getTime() < checkIn.d.getTime())) {

			checkOut.setD(checkIn.d);
			checkOut.nextDay();
			checkOut.setField();

			f.departureDate.value = f.coDate.value;

			var checkOutAlt = validDate(f.departureDate);
			checkOutAlt.setAltField(checkOutAlt.d);
		}
	} else {
		f.ciDate.value = defaultValue;
		f.arrivalDate.value = '';
	}
}

function coDate_onBlur(defaultValue) {
	var checkOut = new validDate(f.coDate);
	if ( checkOut.valid ) {
		checkOut.setField();

		f.departureDate.value = f.coDate.value;

		var checkOutAlt = validDate(f.departureDate);
		checkOutAlt.setAltField(checkOutAlt.d);
	} else {
		f.coDate.value = defaultValue;
		f.departureDate.value = '';
	}
}

// default the checkin date to today's date
function setDateToToday(isDefaultDates,defaultValue)
{
	if (startCIDate == '' && isDefaultDates == false){
		f.ciDate.value = dateFormatString;
		f.coDate.value = dateFormatString;
		f.arrivalDate.value = '';
		f.departureDate.value = '';
	} else if (startCIDate == '' && isDefaultDates == true) {
		function lZero(nr) {if (nr < 10) nr = "0" + nr;return nr;}
	
		var today = new Date();
		writeDateToField(f.ciDate, today, dateFormatString);
		var tomorrow = new Date();
		tomorrow.setDate(tomorrow.getDate() + 1);
		writeDateToField(f.coDate, tomorrow, dateFormatString);
		
		var checkIn = new validDate(f.ciDate);
		
		if ( checkIn.valid ) {
			checkIn.setField();
			f.arrivalDate.value = f.ciDate.value;
			
			var checkInAlt = new validDate(f.arrivalDate);
			checkInAlt.setAltField(checkInAlt.d);
	
			var checkOut = validDate(f.coDate);
			
			if ( (! (checkOut.valid)) || (checkOut.d.getTime() < checkIn.d.getTime())) {
				checkOut.setD(checkIn.d);
				checkOut.nextDay();
				checkOut.setField();
				
				f.departureDate.value = f.coDate.value;
				
				var checkOutAlt = validDate(f.departureDate);
				checkOutAlt.setAltField(checkOutAlt.d);
			}
		}		
		
	} else {
		writeDateToField(f.ciDate, new Date(startCIDate), dateFormatString);

	    if (startCODate == false) {
            startCODate = new validDate(startCIDate);
            startCODate.nextDay();
    		writeDateToField(f.coDate, startCODate.d, dateFormatString);
	    } else {
    		writeDateToField(f.coDate, new Date(startCODate), dateFormatString);
	    }

		f.arrivalDate.value = startCIDateBE;
		f.departureDate.value = startCODateBE;
	}
}

function correctDate(incFormat) //incorrect format expected: yyyy-mm-dd
{
	if (incFormat != ''){
		var tempArray = incFormat.split("-");
		
		var tempYear = tempArray[0];
		var tempMonth = tempArray[1];
		var tempDay = tempArray[2];
		
		var corFormat = tempMonth + "/" + tempDay + "/" + tempYear;	
		return corFormat;
	} else {
		return false;
	}
}

function checkDates() {
	var ci = (f.ciDate==undefined) ? f.arrivalDate : f.ciDate;
	var co = (f.coDate==undefined) ? f.departureDate : f.coDate;
	if ((ci.value == dateFormatString) && (co.value == dateFormatString)) {
		return "DATES_NOT_SET";
	}

	var checkIn = new validDate(ci);
	var checkOut = new validDate(co);
	
	if (!(checkIn.valid) && (!checkOut.valid) ) {
		searchForm.addError("~ciAnddepartureDatesInvalidError", "arrivalDateLabel","departureDateLabel")
	} else if (!checkIn.valid ) {
		searchForm.addError("~arrivalDateInvalidError","arrivalDateLabel")
	} else if (!checkOut.valid ) {
		searchForm.addError("~departureDateInvalidError","departureDateLabel")
	} else 	if ( (checkIn.valid) & (checkOut.valid)  ) {
		var datesDelta = Math.round(checkIn.diffDate( checkOut.d)); // round to account for DST
        var nowDelta = Math.round(checkIn.diffDate(new Date())); // round to account for DST
        if (nowDelta > 0 ) {
        	searchForm.addError("~checkInEarlierThanTodayError","arrivalDateLabel");
        } else if (nowDelta < -551) {
            searchForm.addError("~bookToFarInFutureError","arrivalDateLabel");
            searchForm.addError()
        } else if (datesDelta <  0) {
            searchForm.addError("~departureBeforeArrivalError","departureDateLabel");
        } else if (datesDelta > 90) {
        	searchForm.addError("~maximumLengthStayExceededError", "arrivalDateLabel","departureDateLabel");
        } else if (datesDelta < 1) {
            searchForm.addError("~arrivalEqualsDepartureError", "arrivalDateLabel","departureDateLabel");
		}
	}
	return "";
}


