////////////////////////////////
///// DATES SELECT METHODS /////
////////////////////////////////

// Function to initialize the given lists with first and preselected values defined in global variables :
// NEEDED GLOBAL VARS : firstDayNumberGlobal, firstMonthNumberGlobal, firstYearNumberGlobal, 
//                      nbMonthsGlobal,
//                      preselectedDayValueGlobal, preselectedMthYearValueGlobal
//
// Usually used for initializing alone date select or departure date.
//
// dayField : the day form select id. Example : document.searchForm.day
// monthYearField : the month + year form select id. Example : document.searchForm.monthYear
function initDateSelectWithGlobalVars(dayField, monthYearField) {
    buildDatesSelect(dayField, monthYearField, firstDayNumberGlobal, firstMonthNumberGlobal, firstYearNumberGlobal, nbMonthsGlobal, preselectedDayValueGlobal, preselectedMthYearValueGlobal);
}

// Function to build the given date selects with begining values defined relatively from other given date selects.
// NEEDED GLOBAL VARS : nbMonthsGlobal, daysDiffGlobal, defaultAddedDaysGlobal
//                      firstRelativeDayNumberGlobal, firstRelativeMonthNumberGlobal, firstRelativeYearNumberGlobal  (just initialized)
//
// Usually used for return date select updating.
//
// dayField : the day form select id. Example : document.searchForm.day
// monthYearField : the month + year form select id. Example : document.searchForm.monthYear
// fromDayField : the relative day form select id.
// fromMonthYearField : the relative month + year form select id.
function buildRelativeDatesSelectWithGlobalVars(dayField, monthYearField, fromDayField, fromMonthYearField) {
    if (defaultAddedDaysGlobal == null) {
        buildRelativeDatesSelect(dayField, monthYearField, fromDayField, fromMonthYearField, daysDiffGlobal, nbMonthsGlobal, null, null);
    } else {
        var fromDate = getDateFromFields(fromDayField, fromMonthYearField);
        var firstDateTime = fromDate.getTime() + (defaultAddedDaysGlobal * 86400000);
        var firstDate = new Date(firstDateTime);
    
        var yearNum = firstDate.getYear();
        if (yearNum < 999) {
            yearNum+=1900;
        }
        
        var preselectedDayValue = "";
        var dayNum = firstDate.getDate();
        if (dayNum < 10) {
          preselectedDayValue = "0";
        }
        preselectedDayValue = preselectedDayValue + dayNum;
        
        var preselectedMthYearValue = "";
        var monthNum = MonthNumberGlobal = firstDate.getMonth() + 1;
        if (monthNum < 10) {
          preselectedMthYearValue = "0";
        }
        preselectedMthYearValue = preselectedMthYearValue + monthNum + "/" + yearNum;
        
        buildRelativeDatesSelect(dayField, monthYearField, fromDayField, fromMonthYearField, daysDiffGlobal, nbMonthsGlobal, preselectedDayValue, preselectedMthYearValue);
    }
}

// Function to init the given date selects with begining values defined relatively from other given date selects.
// NEEDED GLOBAL VARS : nbMonthsGlobal, daysDiffGlobal, preselectedReturnDayValueGlobal, preselectedReturnMthYearValueGlobal, defaultAddedDaysGlobal
//                      firstRelativeDayNumberGlobal, firstRelativeMonthNumberGlobal, firstRelativeYearNumberGlobal  (just initialized)
//
// Usually used for return date select initializing.
//
// dayField : the day form select id. Example : document.searchForm.day
// monthYearField : the month + year form select id. Example : document.searchForm.monthYear
// fromDayField : the relative day form select id.
// fromMonthYearField : the relative month + year form select id.
function initRelativeDatesSelectWithGlobalVars(dayField, monthYearField, fromDayField, fromMonthYearField) {

   if (preselectedReturnDayValueGlobal != null && preselectedReturnMthYearValueGlobal != null) {
        buildRelativeDatesSelect(dayField, monthYearField, fromDayField, fromMonthYearField, daysDiffGlobal, nbMonthsGlobal, preselectedReturnDayValueGlobal, preselectedReturnMthYearValueGlobal);
   } else {
     if (defaultAddedDaysGlobal == null) {
        buildRelativeDatesSelect(dayField, monthYearField, fromDayField, fromMonthYearField, daysDiffGlobal, nbMonthsGlobal, null, null);
     } else {
        var fromDate = getDateFromFields(fromDayField, fromMonthYearField);
        var firstDateTime = fromDate.getTime() + (defaultAddedDaysGlobal * 86400000);
        var firstDate = new Date(firstDateTime);
    
        var yearNum = firstDate.getYear();
        if (yearNum < 999) {
            yearNum+=1900;
        }
        
        var preselectedDayValue = "";
        var dayNum = firstDate.getDate();
        if (dayNum < 10) {
          preselectedDayValue = "0";
        }
        preselectedDayValue = preselectedDayValue + dayNum;
        
        var preselectedMthYearValue = "";
        var monthNum = MonthNumberGlobal = firstDate.getMonth() + 1;
        if (monthNum < 10) {
          preselectedMthYearValue = "0";
        }
        preselectedMthYearValue = preselectedMthYearValue + monthNum + "/" + yearNum;
        
        buildRelativeDatesSelect(dayField, monthYearField, fromDayField, fromMonthYearField, daysDiffGlobal, nbMonthsGlobal, preselectedDayValue, preselectedMthYearValue);
      }
   }

}


// Funtion used to update days list field when monthYear field is change.
// NEEDED GLOBAL VARS : firstDayNumberGlobal, firstMonthNumberGlobal, firstYearNumberGlobal.
//
// Usually used for alone date select or departure date.
//
// dayField : the day form select id. Example : document.searchForm.day
// monthYearField : the month + year form select id. Example : document.searchForm.monthYear
function updateDaysList(dayField, monthYearField) {
    var preselectedDay = parseInt(dayField.options[dayField.selectedIndex].value, 10);
    buildDaysList(dayField, monthYearField, firstDayNumberGlobal, firstMonthNumberGlobal, firstYearNumberGlobal, preselectedDay);
}

// Funtion used to update days list field when monthYear field is change.
// NEEDED GLOBAL VARS : firstRelativeDayNumberGlobal, firstRelativeMonthNumberGlobal, firstRelativeYearNumberGlobal  (just initialized).
//
// Usually used for return date select updating.
//
// dayField : the day form select id. Example : document.searchForm.day
// monthYearField : the month + year form select id. Example : document.searchForm.monthYear
function updateRelativeDaysList(dayField, monthYearField) {
    var preselectedDay = parseInt(dayField.options[dayField.selectedIndex].value, 10);
    buildDaysList(dayField, monthYearField, firstRelativeDayNumberGlobal, firstRelativeMonthNumberGlobal, firstRelativeYearNumberGlobal, preselectedDay);
}


///////////////////////////
///// WEEK DAY METHOD /////
///////////////////////////

// Function used to write in a given field the week day name obtained from given input fields.
//
// inputField : the input text field where to write the week day name.
// fromDayField : the input day form select id.
// fromMonthYearField : the input month + year form select id.
function writeWeekDayNameInField(inputField, fromDayField, fromMthYearField) {
    inputField.value = getWeekDayName(fromDayField, fromMthYearField);
}



///////////////////////////////////
//////// PRIVATE METHODS //////////
///////////////////////////////////

// Util 'private' function to get the week day name from date select fields.
// dayField : the day form select id. Example : document.searchForm.day
// monthYearField : the month + year form select id. Example : document.searchForm.monthYear
function getWeekDayName(dayField, monthYearField) {
    var selectDate = getDateFromFields(dayField, monthYearField);

    return weekDayTitles[selectDate.getDay()];
}


// Function to build the given date selects with begining values defined relatively from other given date selects.
// NEEDED GLOBAL VARS : firstRelativeDayNumberGlobal, firstRelativeMonthNumberGlobal, firstRelativeYearNumberGlobal  (just initialized)
//
// Usually used for return date select updating and initializing.
//
// dayField : the day form select id. Example : document.searchForm.day
// monthYearField : the month + year form select id. Example : document.searchForm.monthYear
// fromDayField : the relative day form select id.
// fromMonthYearField : the relative month + year form select id.
// nbAddedDays : the number of days to add to the relative date.
// nbMonthsParam : the number of month to display.
// preselectedDayParam : the day value (string with two chars!!) to select in list (if not present, select the closest one)
// preselectedMthYearParam : the monthYear value to preselect
function buildRelativeDatesSelect(dayField, monthYearField, fromDayField, fromMonthYearField, nbAddedDays, nbMonthsParam, preselectedDayParam, preselectedMthYearParam) {
    var fromDate = getDateFromFields(fromDayField, fromMonthYearField);
    var firstDateTime = fromDate.getTime() + (nbAddedDays * 86400000);
    var firstDate = new Date(firstDateTime);

    var firstYear = firstDate.getYear();
    if(firstYear < 999) {
        firstYear+=1900;
    }
    
    firstRelativeDayNumberGlobal = firstDate.getDate();
    firstRelativeMonthNumberGlobal = firstDate.getMonth() + 1;
    firstRelativeYearNumberGlobal = firstYear;
    
    buildDatesSelect(dayField, monthYearField, firstRelativeDayNumberGlobal, firstRelativeMonthNumberGlobal, firstRelativeYearNumberGlobal, nbMonthsParam, preselectedDayParam, preselectedMthYearParam);
}


// Function to build the given date fields with given first date and number of months to display.
// Then preselect the date fields with the given preselected date.
//
// dayField : the day form select id. Example : document.searchForm.day
// monthYearField : the month + year form select id. Example : document.searchForm.monthYear
// firstDayNumber : the first day for date selects
// firstMonthNumber : the first month for date selects
// firstYearNumber : the first year for date selects
// nbMonthsParam : the number of month to display
// preselectedDayParam : the day value (string with two chars!!) to select in list (if not present, select the closest one)
// preselectedMthYearParam : the monthYear value to preselect
function buildDatesSelect(dayField, monthYearField, firstDayNumber, firstMonthNumber, firstYearNumber, nbMonthsParam, preselectedDayParam, preselectedMthYearParam) {
    var lastMonthNumberTemp = firstMonthNumber + nbMonthsParam;
    var lastMonthNumber = lastMonthNumberTemp % 12;
    var lastYearNumber = firstYearNumber + parseInt(lastMonthNumberTemp/12);
    
    buildMonthYearSelect(monthYearField, firstMonthNumber, firstYearNumber, lastMonthNumber, lastYearNumber, preselectedMthYearParam);
    buildDaysList(dayField, monthYearField, firstDayNumber, firstMonthNumber, firstYearNumber, preselectedDayParam);
}



// Util 'private' function to build the monthYear select
//
// monthYearField : the month + year form select id. Example : document.searchForm.monthYear
// firstMonthNumber : the first month for date selects
// firstYearNumber : the first year for date selects
// lastMonthNumber : the month (1-12) of the last date to build
// lastYearNumber : the year (yyyy) of the last date to build
// preselectedMthYearParam : the monthYear value to preselect. takes first month if null.
function buildMonthYearSelect(monthYearField, firstMonthNumber, firstYearNumber, lastMonthNumber, lastYearNumber, preselectedMthYearParam ) {

    var index = 0;
    var i = 0;

    monthYearField.options.length = 0;
    var currMonthNum = firstMonthNumber;
    var currYearNum = firstYearNumber;
    
    while (currMonthNum != lastMonthNumber+1 || currYearNum != lastYearNumber) {

        var currMthYearValueStr = "";
        if (currMonthNum < 10) {
            currMthYearValueStr = "0";
        }
        currMthYearValueStr = currMthYearValueStr + currMonthNum + "/" + currYearNum;
        var currMthYearTitleStr = monthTitles[currMonthNum] + " " + currYearNum;
        
        monthYearField.options[monthYearField.length] = new Option(currMthYearTitleStr, currMthYearValueStr);
        
        if (preselectedMthYearParam == currMthYearValueStr) {
            index = i;
        }
        
        if (currMonthNum == 12) {
            currMonthNum = 1;
            currYearNum = currYearNum + 1;
        } else {
            currMonthNum = currMonthNum + 1;
        }
        i++;
    }

    monthYearField.selectedIndex = index;
}

// Util 'private' function to build the day select list
//
// dayField : the day form select id. Example : document.searchForm.day
// mthYearField : the month + year form select id. Example : document.searchForm.monthYear
// firstDayNumber : the first day for date selects
// firstMonthNumber : the first month for date selects
// firstYearNumber : the first year for date selects
// preselectedDayParam : the day value (string with two chars!!) to select in list (if not present, select the closest one). Takes 1 if null.
function buildDaysList(dayField, mthYearField, firstDayNumber, firstMonthNumber, firstYearNumber, preselectedDayParam) {

    var currMthYearStr = mthYearField.options[mthYearField.selectedIndex].value;
    
    var slashIndex = currMthYearStr.indexOf("/");
    var currMth = parseInt(currMthYearStr.substring(0,slashIndex), 10);
    var currYear = parseInt(currMthYearStr.substring(slashIndex+1), 10);

    buildDaysListFromValues(dayField, currMth, currYear, firstDayNumber, firstMonthNumber, firstYearNumber, preselectedDayParam);
}


// 'Private' util function to get a javascript Date object from date select fields.
//
// dayField : the day form select id. Example : document.searchForm.day
// monthYearField : the month + year form select id. Example : document.searchForm.monthYear
function getDateFromFields(dayField, monthYearField) {
    var dayNum = parseInt(dayField.options[dayField.selectedIndex].value, 10);
    var mthYearStr = monthYearField.options[monthYearField.selectedIndex].value;
        
    var slashIndex = mthYearStr.indexOf("/");
    var mthNum = parseInt(mthYearStr.substring(0,slashIndex), 10);
    var yearNum = parseInt(mthYearStr.substring(slashIndex+1), 10);

    var resultDate = new Date(yearNum, mthNum-1, dayNum);
    return resultDate;
}