




var languageString="es_ES";
// Dynamic Variables based on Session Locale
var dateFormatString = "DD/MM/AAAA"; // Logical description of the meaning of elements in a Date String
var dateFormatDisplay = "DD/MM/AAAA"; // Language/Locale-Specific string to describe the meaning of the elements in a date string. Meant for display only

// Module variables
var stay; // Where we're putting the custom stay object


// DEFINE stayDate OBJECT
function stayDate(ci_field, co_field) { // ci_field and co_field are REQUIRED

	// STAY CONSTANTS
	DAY_MLLSCNDS = 1000*60*60*24;

	// FORM FIELD PROPERTIES
	this.ci_field = ci_field; // This is a required parameter
	this.co_field = co_field; // This is a required parameter

	// Write out stay values to fields
	this.display_ci_field = function() { this.ci_field.value = get_display_string(this.ci_date); };
	this.display_co_field = function() { this.co_field.value = get_display_string(this.co_date); };

	// Set check-in date in the object - it will then update the display automatically
	this.set_ci_date = function(date) {
		this.ci_date = date;
		if ((!isValidDate(this.co_date)) && (isValidDate(this.ci_date))) {
			this.set_co_date(new Date(this.ci_date.getTime() + (DAY_MLLSCNDS))); // If there is no check-out date, it is set to one day after check-in
		}
		this.display_ci_field(); // Be sure to update the display
	};

	// Set check-out date in the object - it will then update the display automatically
	this.set_co_date = function(date) {
		this.co_date = date;
		this.display_co_field(); // Be sure to update the display
	};

	this.set_ci_date_string = function(dateString) {
			this.set_ci_date(new Date(dateString));
	};

	this.set_co_date_string = function(dateString) {
			this.set_co_date(new Date(dateString));
	};

	// Functions for returning current date values
	this.get_ci_date = function() { return this.ci_date; };
	this.get_co_date = function() { return this.co_date; };
	this.get_ci_field = function() { return this.ci_field; };
	this.get_co_field = function() { return this.co_field; };

	// One stop shop for reading a date from a field, putting it into memory, and writing it out to the field again.
	this.read_ci_field = function() { this.set_ci_date(get_field_date(this.ci_field)); };
	this.read_co_field = function() { this.set_co_date(get_field_date(this.co_field)); };

	this.get_los = function() {
		if ((this.ci_date == null) || (this.co_date == null)) return null;
		return getDateDiff(this.ci_date, this.co_date);
	};

	// Validates stay data
	// REQURES THAT JSP INCLUDES error_constants.js FOR THE CURRENT BRAND
	// Returns an array of errors if there are any - null otherwise
	// isValidDate protects against non-date objects caused by empty values in the initial HTML
	this.getErrorList = function() {
			stayErrors = new Array();
			today = new Date();
			// Does Arrival Date Exist?
			if (!isValidDate(this.get_ci_date())) stayErrors[stayErrors.length] = new error(NO_ARRIVAL_DATE, this.get_ci_field());
			// Does Departure Date Exist?
			if (!isValidDate(this.get_co_date())) stayErrors[stayErrors.length] = new error(NO_DEPARTURE_DATE, this.get_co_field());
			// Is LOS less than 30 days?
			if ((isValidDate(this.get_ci_date())) && (this.get_los() > 30)) stayErrors[stayErrors.length] = new error(STAY_OVER_30_DAYS, this.get_co_field());
			// Is Check-Out less than 550 days away?
			if ((isValidDate(this.get_co_date())) && (!(getDateDiff(today, this.co_date) < 550))) stayErrors[stayErrors.length] = new error(STAY_OVER_550_DAYS_AWAY, this.get_ci_field());
			// Is Check-Out and Check-In on Different Days?
			if ((isValidDate(this.ci_date)) && (isSameDate(this.ci_date, this.co_date))) stayErrors[stayErrors.length] = new error(ARRIVAL_EQUALS_DEPARTURE, this.get_co_field());
			// Is Arrival today or later?
			if ((isValidDate(this.get_ci_date())) && (getDateDiff(today, this.get_ci_date()) < 0)) stayErrors[stayErrors.length] = new error(ARRIVAL_BEFORE_TODAY, this.get_ci_field());
			// Is Departure date after the Arrival date?
			if ((isValidDate(this.get_co_date())) && (getDateDiff(this.get_ci_date(), this.get_co_date()) < 0)) stayErrors[stayErrors.length] = new error(DEPARTURE_BEFORE_ARRIVAL, this.get_co_field());

			// Check if there are any errors - if so, return the array
			if (stayErrors.length > 0) { return stayErrors; } else {	return null; }
		};

	// Read the dates in from the HTML Content
	this.read_co_field(); // Read co_field first, as ci_field has overwrite functions in it if co_field is empty
	this.read_ci_field();

}


// GENERAL FUNCTIONS FOR USE WITH STAYS AND DATES
// ----------------------------------------------

// Returns a internationalized string suitable for display in a form field
function get_display_string(date) {
	if (date == null) return dateFormatDisplay; // Return the default date prompt if there is no date yet
	if ((isNaN(date.getMonth())) || (isNaN(date.getDate())) || (isNaN(date.getFullYear()))) return dateFormatDisplay; // Return the default date prompt if there is an invalid date.
	// Get the string to work with
	fieldString = new String(dateFormatString);
	month = new String(date.getMonth() + 1);
	day = new String(date.getDate());
	year = new String(date.getFullYear());
	// Check and fix the length of each component
	if (month.length == 1) month = "0" + month;
	if (day.length == 1) day = "0" + day;

	if(languageString=="en_US" || languageString=="ja_JP" || languageString=="zh_CN"){
	// Create regular expressions to find MM, DD, and YYYY in a string representing the date format
	reMM = /MM/gi; reDD = /DD/gi;
	var yearIndex = dateFormat.indexOf("yyyy");
	reYYYY = /YYYY/gi;
	var isChineseJapanese = false;
	if(yearIndex==-1) {
        reYYYY = /YY/gi;
        year = new String(year.substring(2,4));
    }
    }
    if(languageString=="fr_FR"){
	// Create regular expressions to find JJ, MM, and AAAA in a string representing the date format
	reMM = /MM/gi; reDD = /JJ/gi;
	var yearIndex = dateFormat.indexOf("aaaa");
	reYYYY = /AAAA/gi;
	var isChineseJapanese = false;
	if(yearIndex==-1) {
        reYYYY = /AA/gi;
        year = new String(year.substring(2,4));
    }
    }
    if(languageString=="es_ES"){
	// Create regular expressions to find MM, DD, and AAAA in a string representing the date format
	reMM = /MM/gi; reDD = /DD/gi;
	var yearIndex = dateFormat.indexOf("aaaa");
	reYYYY = /AAAA/gi;
	var isChineseJapanese = false;
	if(yearIndex==-1) {
        reYYYY = /AA/gi;
        year = new String(year.substring(2,4));
    }
    }
    if(languageString=="de_DE"){
	// Create regular expressions to find MM, TT, and JJJJ in a string representing the date format
	reMM = /MM/gi; reDD = /TT/gi;
	var yearIndex = dateFormat.indexOf("jjjj");
	reYYYY = /JJJJ/gi;
	var isChineseJapanese = false;
	if(yearIndex==-1) {
        reYYYY = /yy/gi;
        year = new String(year.substring(2,4));
    }
    }
    if(languageString=="it_IT"){
	// Create regular expressions to find MM, GG, and AAAA in a string representing the date format
	reMM = /MM/gi; reDD = /GG/gi;
	var yearIndex = dateFormat.indexOf("aaaa");
	reYYYY = /AAAA/gi;
	var isChineseJapanese = false;
	if(yearIndex==-1) {
        reYYYY = /AA/gi;
        year = new String(year.substring(2,4));
    }
    }

	// Replace the placeholders for date components with the actual values
	fieldString = fieldString.replace(reMM, month);
	fieldString = fieldString.replace(reDD, day);
	fieldString = fieldString.replace(reYYYY, year);
	// Return the constructed field
	return fieldString;
}


// Returns a date object from a field...Returns null if the field cannot be read as a valid date
function get_field_date(field) {
	dateFormat = dateFormatString.toLowerCase(); // Get rid of any possible capitalization problems.
	dateString = field.value; // Get the string in the field...
	if (dateString == null) return null; // get out of there was nothing in the field
	if(languageString=="en_US"|| languageString=="ja_JP" || languageString=="zh_CN"){
	var monthSection = "mm"; var daySection = "dd"; var yearSection = "yyyy"; // Set the indentifiers expected in a dateFormatString - these should be the same no matter the locale.
    }
    if(languageString=="fr_FR"){
	var monthSection = "mm"; var daySection = "jj"; var yearSection = "aaaa"; // Set the indentifiers expected in a dateFormatString - these should be the same no matter the locale.
    }
    if(languageString=="es_ES"){
	var monthSection = "mm"; var daySection = "dd"; var yearSection = "aaaa"; // Set the indentifiers expected in a dateFormatString - these should be the same no matter the locale.
    }
    if(languageString=="de_DE"){
	var monthSection = "mm"; var daySection = "tt"; var yearSection = "jjjj"; // Set the indentifiers expected in a dateFormatString - these should be the same no matter the locale.
    }
    if(languageString=="it_IT"){
	var monthSection = "mm"; var daySection = "gg"; var yearSection = "aaaa"; // Set the indentifiers expected in a dateFormatString - these should be the same no matter the locale.
    }
	// Get the start positions of each format section to map to the field
	var monthIndex = dateFormat.indexOf(monthSection);
	var dayIndex = dateFormat.indexOf(daySection);
	var yearIndex = dateFormat.indexOf(yearSection);
	var isChineseJapanese = false;
	if(yearIndex==-1) {
	    yearIndex = dateFormat.indexOf("yy");
	    isChineseJapanese = true;
    }
	// Get the variables from the field.
	var MM = dateString.substr(monthIndex, 2);
	var DD = dateString.substr(dayIndex, 2);
	var YYYY = "";
	if(isChineseJapanese) {
	    YYYY = "20" + dateString.substr(yearIndex, 2);
	} else {
	    YYYY = dateString.substr(yearIndex, 4);
    }
	// Check that the date components are actually populated and set the fieldDate value for return
	if ((MM != "") && (DD != "") && (YYYY != "") && (YYYY.length == 4)) {
	var fieldDate = new Date(YYYY, MM - 1, DD); // The date field is in text format, need to convert the month to adjust for the zero-based integer in the constructor.
	} else { 
	  var fieldDate = new Date(dateFormatString); // Otherwise just create an invalid date. The receiving function will have to handle this case.
	}
	// Check that we got actual numbers in the date object - return null if it is garbage
	if (!isValidDate) return null;
	// Check to see if the date in memory matches the date string entered - if not (as it would with the string "02/30/2005"), return null
	if (dateString != get_display_string(fieldDate)) return null;
	// Otherwise return our valid date
	return fieldDate;
}


function getDateDiff(earlyDate, lateDate) {
		var difference = Date.UTC(lateDate.getFullYear(),lateDate.getMonth(),lateDate.getDate(),0,0,0) - Date.UTC(earlyDate.getFullYear(),earlyDate.getMonth(),earlyDate.getDate(),0,0,0);
		return difference/1000/60/60/24;

}

function isSameDate(date1, date2) {
	date1Month = date1.getMonth();
	date1Year = date1.getYear();
	date1Day = date1.getDate();
	
	date2Month = date2.getMonth();
	date2Year = date2.getYear();
	date2Day = date2.getDate();
	
	date1String = date1Month+date1Year+date1Day;
	date2String = date2Month+date2Year+date2Day;
	
	if (date1String == date2String) return true;
	
	return false;
}


function isValidDate(date) {
	if (date == null) return false;
	if ((isNaN(date.getMonth())) || (isNaN(date.getDate())) || (isNaN(date.getFullYear()))) return false; 
	return true;
}


// EVENT HANDLERS FOR DATE FORMS
// -----------------------------

function onDateFocus() {
	// Clear the field if there is not a good date in it
	if (!isValidDate(get_field_date(this))) this.value = "";
}

function onDateBlur() {
	// If there is not a good date in the field, reset it to the date format indicator
	if (!isValidDate(get_field_date(this))) this.value = dateFormatDisplay;
	// If this is the check-in field, fire the ci read method
	if (this.id == "ciDate") stay.read_ci_field();
	// If this is the co field, fire the co read method
	if (this.id == "coDate") stay.read_co_field();
}


// REGISTER EVENT HANDLERS
// -----------------------
addLoadEvent(function() {
	// Find references to the basic elements of the form
	var ci = document.getElementById("ciDate");
	var co = document.getElementById("coDate");
	var form = document.getElementById("search");
	
	// Set the event handlers for each element
	ci.onfocus = onDateFocus; ci.onblur = onDateBlur;
	co.onfocus = onDateFocus; co.onblur = onDateBlur;
	
	// Create the stay object
	stay = new stayDate(ci, co);
});
