
/* 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 arrDate = new Date(year, month-1, day);

    var today = new Date();
    today =  new Date(today.getFullYear(),today.getMonth(),today.getDate());
    if ( (arrDate - today)<0 ) year = today.getFullYear();
    arrDate = new Date(year, month-1, day);
    if ( ((arrDate - today)<0) ) year = today.getFullYear()+1;
    arrDate = new Date(year, month-1, day);
    
    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();
}

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();
}


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 depDate = new Date(year, month-1, day);

    var today = new Date();
    today =  new Date(today.getFullYear(),today.getMonth(),today.getDate());
    if ( (depDate - today)<=0 ) year = today.getFullYear();
    depDate = new Date(year, month-1, day);
    if ( (depDate - today)<=0 ) year = today.getFullYear()+1;
    depDate = new Date(year, month-1, day);

    
    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);
        if(arrDate >= depDate) {
            incArriveDate(depDate);
            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;
}
