
/* Functions to adjust the arrival & departure dates for LaQuinta.com
	The goal is that the arrival date should always be at least one day before the departure date
 	(and the departure date should always be at least one day after the arrival date) */

/* This is called after setting the Arrival time; it adjusts the
	departure time if needed to be at least one day after the arrival time */
function checkDeparture() {
    var month = getListBoxValue(eval(arrM.ToList));
    var year = eval(arrY.ToList).value;
    var day = getListBoxValue(eval(arrD.ToList));
    if(month == '' || day == '') {
        return;
    }

    var today = new Date();

	/**
		This condition is used to find out the year. if Departure Month is 
		greater than or equal to current Month then it is current Year otherwise
		it is a next year.
		After this we are setting the value in form paramteres.
		
		@author Alok Sarawat
	*/
	/*****************************************/
    if(month-1 >= today.getMonth()){
    	year = today.getFullYear();
    }else
	{
    	year = today.getFullYear()+1;
	}
	var arrDate = new Date(year, month-1, day);
    if ( arrDate - today < 0) {
		arrDate=today;
	}
    setArrivalDate(arrDate);
    /******************************************/ 
    month = getListBoxValue(eval(depM.ToList));
    year = eval(depY.ToList).value;
    day = getListBoxValue(eval(depD.ToList));

    if(month == '' || day == '') {
        incDepartDate(arrDate);
        return;
    }
    else {
        var depDate = new Date(year, month-1, day);
        if(arrDate >= depDate) {
            incDepartDate(arrDate);
            return;
        }
    }    
}

function incDepartDate(arrDate) {
    var newDate = Date.addDate(arrDate, Date.DAY, 1);
    depD.MatchVal(newDate.getDate().toString());
    depM.MatchVal((newDate.getMonth() + 1).toString());
    eval(depY.ToList).value = newDate.getFullYear().toString();
	return newDate;
}

function incArriveDate(depDate) {
    var newDate = Date.addDate(depDate, Date.DAY, -1);
    arrD.MatchVal(newDate.getDate().toString());
    arrM.MatchVal((newDate.getMonth() + 1).toString());
    eval(arrY.ToList).value = newDate.getFullYear().toString();
}
/**
	This function set the form parameter (Arrival Dates) from
	date passed as argument.
*/
function setArrivalDate(newDate){
    arrD.MatchVal(newDate.getDate().toString());
    arrM.MatchVal((newDate.getMonth() + 1).toString());
    eval(arrY.ToList).value = newDate.getFullYear().toString();
}
/**
	This function set the form parameter (Depart Dates) from
	date passed as argument.
*/
function setDepartDate(newDate){
    depD.MatchVal(newDate.getDate().toString());
    depM.MatchVal((newDate.getMonth() + 1).toString());
    eval(depY.ToList).value = newDate.getFullYear().toString();
}

/* This is called after setting the Departure time; it adjusts the
	Arrival time if needed to be at least one day after the arrival time */
function checkArrival() {
    var month = getListBoxValue(eval(depM.ToList));
    var year = eval(depY.ToList).value;
    var day = getListBoxValue(eval(depD.ToList));
    if(month == '' || day == '') {
        return;
    }
    var today = new Date();
	/**
		This condition is used to find out the year. if Departure Month is 
		greater than or equal to current Month then it is current Year otherwise
		it is a next year.
		After this we are setting the value in form paramteres.
		
		@author Alok Sarawat 
	*/
	/*****************************************/
    if(month-1 >= today.getMonth()){
    	year = today.getFullYear();
    }else
	{
    	year = today.getFullYear()+1;
	}
    /*******************************************/
	var depDate = new Date(year, month-1, day);
    if ( (depDate - today)<=0 ){
		depDate = incDepartDate(today);
	}else{	
		setDepartDate(depDate);
	}

    month = getListBoxValue(eval(arrM.ToList));
    year = eval(arrY.ToList).value;
    day = getListBoxValue(eval(arrD.ToList));

    if(month == '' || day == '') {
        incArriveDate(depDate);
        return;
    }
    else {
        var arrDate = new Date(year, month-1, day);
		var nowDepDate = new Date(depDate.getFullYear(),depDate.getMonth(),depDate.getDate());
        if(arrDate >= nowDepDate) {
            incArriveDate(nowDepDate);
            return;
        }
    }    
}


function DefineMatchList(form, field) {
    this.ToList = "document.forms[" + form + "].elements['" + field + "']";
    this.MatchVal = CLMatch;
}

function DefineMatchListUsingFormName(form, field) {
    this.ToList = "document.forms['" + form + "'].elements['" + field + "']";
    this.MatchVal = CLMatch;
}

function CLMatch(matchVal) {
  if (matchVal.length == 1)
    matchVal = "0"+matchVal;
  ToObj = eval(this.ToList);
  for (elem = 0; elem < ToObj.options.length; elem++) {
    if (ToObj.options[elem].value == matchVal) 
		ToObj.options[elem].selected = true
	else
		ToObj.options[elem].selected = false
  }
}

function getListBoxValue(list) {
    var retVal = list.options[list.selectedIndex].value;
    return retVal;
}
