var typedate = "";
var formName = "";
var calDateFormat = "dd/MM/yyyy";
var calDate = new Date();
var suffix = "";
function updateFromText(suffix) {
	if (eval("document.getElementById('dayDateFromText" + suffix + "')") != null) {
	day = eval("document."+formName+".elements['dayDateFrom"+suffix+"'].options[document."+formName+".elements['dayDateFrom"+suffix+"'].selectedIndex].value");
	month = eval("document."+formName+".elements['monthDateFrom"+suffix+"'].options[document."+formName+".elements['monthDateFrom"+suffix+"'].selectedIndex].text");
	year= eval("document."+formName+".elements['yearDateFrom"+suffix+"'].options[document."+formName+".elements['yearDateFrom"+suffix+"'].selectedIndex].value");
	eval("document.getElementById('dayDateFromText" + suffix + "').innerHTML= day");
	eval("document.getElementById('monthDateFromText" + suffix + "').innerHTML= month");
	eval("document.getElementById('yearDateFromText" + suffix + "').innerHTML= year");
	}
}
function updateFromInput(suffix,pattern)
{
	var strFromInputId = "inputDateFrom"+suffix;
	if (document.getElementById(strFromInputId)) {
		var dayId = "dayDateFrom"+suffix;
		var monthId = "monthDateFrom"+suffix;
		var yearId = "yearDateFrom"+suffix;
        	day = document.getElementsByName(dayId)[0].options[document.getElementsByName(dayId)[0].selectedIndex].value;
	        month = document.getElementsByName(monthId)[0].options[document.getElementsByName(monthId)[0].selectedIndex].value;
	        year= document.getElementsByName(yearId)[0].options[document.getElementsByName(yearId)[0].selectedIndex].value;

		if (day.length==1)
			day = "0"+day;
		if (month.length==1)
			month = "0"+month;	

		var patternToUse = "dd/MM/yyyy";	        
		if (pattern)
			patternToUse = pattern;

	 	patternToUse =	patternToUse.replace("dd",day);
		patternToUse = 	patternToUse.replace("MM",month);
		patternToUse =	patternToUse.replace("yyyy",year);
		patternToUse = 	patternToUse.replace("YYYY",year);		
		patternTouse = 	patternToUse.replace("YY",year);
			
		document.getElementById(strFromInputId).value= patternToUse;
        }
	updateToInput(suffix,pattern);
}


function updateToInput(suffix,pattern)
{
	var strToInputId = "inputDateTo"+suffix;
        if (document.getElementById(strToInputId)) {

		var dayId = "dayDateTo"+suffix;
                var monthId = "monthDateTo"+suffix;
                var yearId = "yearDateTo"+suffix;
                day = document.getElementsByName(dayId)[0].options[document.getElementsByName(dayId)[0].selectedIndex].value;
                month = document.getElementsByName(monthId)[0].options[document.getElementsByName(monthId)[0].selectedIndex].value;
                year= document.getElementsByName(yearId)[0].options[document.getElementsByName(yearId)[0].selectedIndex].value;

                if (day.length==1)
                        day = "0"+day;
                if (month.length==1)
                        month = "0"+month;

                var patternToUse = "dd/MM/yyyy";
                if (pattern)
                        patternToUse = pattern;

                patternToUse =  patternToUse.replace("dd",day);
                patternToUse =  patternToUse.replace("MM",month);
                patternToUse =  patternToUse.replace("yyyy",year);
                patternToUse =  patternToUse.replace("YYYY",year);
                patternTouse =  patternToUse.replace("YY",year);

                document.getElementById(strToInputId).value= patternToUse;
        }
}

// ================================================================
//Bloque la date From minimum par rapport a la date To de l'element FromToDate precedent
// ================================================================
function blockTo_FromFrom(suffix,nbNightMin) {
	if (nbNightMin == null) { nbNightMin = 0; }
    if (suffix == null || suffix == "none") { suffix = ""; }
		fromDay = eval("document."+formName+".elements['dayDateFrom"+suffix+"'].options[document."+formName+".elements['dayDateFrom"+suffix+"'].selectedIndex].value");
		fromMonth = eval("document."+formName+".elements['monthDateFrom"+suffix+"'].options[document."+formName+".elements['monthDateFrom"+suffix+"'].selectedIndex].value");
		fromYear= eval("document."+formName+".elements['yearDateFrom"+suffix+"'].options[document."+formName+".elements['yearDateFrom"+suffix+"'].selectedIndex].value");
		toDay = eval("document."+formName+".elements['dayDateTo"+suffix+"'].options[document."+formName+".elements['dayDateTo"+suffix+"'].selectedIndex].value");
		toMonth = eval("document."+formName+".elements['monthDateTo"+suffix+"'].options[document."+formName+".elements['monthDateTo"+suffix+"'].selectedIndex].value");
		toYear= eval("document."+formName+".elements['yearDateTo"+suffix+"'].options[document."+formName+".elements['yearDateTo"+suffix+"'].selectedIndex].value");
		date1 = new Date(fromYear,fromMonth,fromDay,6,0,0);
		date2 = new Date(toYear,toMonth,toDay,6,0,0);
	    var difference = Date.UTC(y2k(date2.getYear()),date2.getMonth(),date2.getDate(),6,0,0)
                     - Date.UTC(y2k(date1.getYear()),date1.getMonth(),date1.getDate(),6,0,0);
        diff = difference/1000/60/60/24;
        if (diff<=0) {
        	newDate = new Date(date1.getTime() + nbNightMin*24*60*60*1000);
        	CurDay = newDate.getDate();
    		CurMonth = newDate.getMonth();
    		CurYear = y2k(newDate.getYear()); 
        	if (CurMonth<10) CurMonth = "0"+CurMonth;
    		if (CurDay<10) CurDay = "0"+CurDay;
    		if ((CurDay > 0) && (CurMonth > 0) && (CurYear >0)) {
        		eval("document."+formName+".elements['dayDateTo"+(suffix)+"'].selectedIndex="+(CurDay - 1));
        		eval("document."+formName+".elements['monthDateTo"+(suffix)+"'].selectedIndex="+(CurMonth-1));
    		}
    		size =  eval("document."+formName+".yearDateTo"+(suffix)+".options.length");
    		for (i=0;i<size;i++) {
        		if (CurYear == eval("document."+formName+".yearDateTo"+(suffix)+".options[i].value")) eval("document."+formName+".yearDateTo"+(suffix)+".selectedIndex = i");
    		}
		}
   updateNbNight(
        formName,
        eval("document."+formName+".elements['dayDateTo"+suffix+"'].options[document."+formName+".elements['dayDateTo"+suffix+"'].selectedIndex].value"),
        eval("document."+formName+".elements['monthDateTo"+suffix+"'].options[document."+formName+".elements['monthDateTo"+suffix+"'].selectedIndex].value"),
        eval("document."+formName+".elements['yearDateTo"+suffix+"'].options[document."+formName+".elements['yearDateTo"+suffix+"'].selectedIndex].value"),
        eval("document."+formName+".elements['dayDateFrom"+suffix+"'].options[document."+formName+".elements['dayDateFrom"+suffix+"'].selectedIndex].value"),
        eval("document."+formName+".elements['monthDateFrom"+suffix+"'].options[document."+formName+".elements['monthDateFrom"+suffix+"'].selectedIndex].value"),
        eval("document."+formName+".elements['yearDateFrom"+suffix+"'].options[document."+formName+".elements['yearDateFrom"+suffix+"'].selectedIndex].value"),
        suffix);
}
// ================================================================
function onChangeFrom(suffix) {
    if (suffix == null || suffix == "none") { suffix = ""; }
    if (checkEnddatePartner('from')) {
      elem = eval("document."+formName+".nbnight"+suffix);

      	if (elem)
			{
	  			nbnight_type = elem.type;
	    if (nbnight_type == "select-one")
	      nbnight = eval("document."+formName+".nbnight"+suffix+".selectedIndex")+1;
	    else
	      nbnight =  eval("document."+formName+".nbnight"+suffix+".value");
	  updateEndDate(formName,
			eval("document."+formName+".elements['dayDateFrom"+suffix+"'].options[document."+formName+".elements['dayDateFrom"+suffix+"'].selectedIndex].value"),
			eval("document."+formName+".elements['monthDateFrom"+suffix+"'].options[document."+formName+".elements['monthDateFrom"+suffix+"'].selectedIndex].value"),
			eval("document."+formName+".elements['yearDateFrom"+suffix+"'].options[document."+formName+".elements['yearDateFrom"+suffix+"'].selectedIndex].value"),
			nbnight,suffix
			);
if (suffix>0) {
		updateFromText(suffix);
}
			}
    	}
}
 // ================================================================
function onChangeTo(suffix) {
    if (suffix == null || suffix == "none") { suffix = ""; }
    if (checkEnddatePartner('to')) {
        updateNbNight(
        formName,
        eval("document."+formName+".elements['dayDateTo"+suffix+"'].options[document."+formName+".elements['dayDateTo"+suffix+"'].selectedIndex].value"),
        eval("document."+formName+".elements['monthDateTo"+suffix+"'].options[document."+formName+".elements['monthDateTo"+suffix+"'].selectedIndex].value"),
        eval("document."+formName+".elements['yearDateTo"+suffix+"'].options[document."+formName+".elements['yearDateTo"+suffix+"'].selectedIndex].value"),
        eval("document."+formName+".elements['dayDateFrom"+suffix+"'].options[document."+formName+".elements['dayDateFrom"+suffix+"'].selectedIndex].value"),
        eval("document."+formName+".elements['monthDateFrom"+suffix+"'].options[document."+formName+".elements['monthDateFrom"+suffix+"'].selectedIndex].value"),
        eval("document."+formName+".elements['yearDateFrom"+suffix+"'].options[document."+formName+".elements['yearDateFrom"+suffix+"'].selectedIndex].value"),
		suffix
        );
if (suffix>0) {
	updateFromText(suffix);
}
    }
}
// ================================================================
setDateParams = function(i_typedate,i_formName) {
    typedate = i_typedate;
    formName = i_formName;
    if (document.forms[0].checkavail)
      {
	document.forms[0].checkavail.value = 1;
      }
    else
      {
	for (idx=1; idx<document.forms.length; idx++)
	  {
	    if (document.forms[idx].checkavail)
	      {
		document.forms[idx].checkavail.value = 1;
	      }
	  }
      }
}
  // ================================================================      
function setFields(dayField,monthField,yearField) {
      caldayfield = dayField;
      calmonthfield = monthField;
      calyearfield = yearField;
}
        
// ================================================================
// fonction permettant de creer une date
// ================================================================
function createOutDateCal(dateFormat) {
    // SET THE DATE RETURNED TO THE USER
    day           = calDate.getDate()+"";
    if (day.length == 1)
        day = 0+day;

    month         = calDate.getMonth()+1+"";
    if (month.length == 1)
        month = 0+month;
    
    year          = calDate.getFullYear();
    outDate = dateFormat;

    // RETURN ONE OR TWO DIGIT DAY
    if (dateFormat.indexOf("dd") != -1) {
        outDate = jsReplaceCal(outDate, "dd", day);
    }
    // RETURN ONE OR TWO DIGIT MONTH
    if (dateFormat.indexOf("MM") != -1) {
        outDate = jsReplaceCal(outDate, "MM", month);
    }
    // RETURN FOUR-DIGIT YEAR
    if (dateFormat.indexOf("yyyy") != -1) {
        outDate = jsReplaceCal(outDate, "yyyy", year);
    }
    // RETURN TWO-DIGIT YEAR
    else if (dateFormat.indexOf("yy") != -1) {
        var yearString = "" + year;
        var yearString = yearString.substring(2,4);
        outDate = jjsReplaceCal(outDate, "yy", yearString);
    }

    return outDate;
}
// ================================================================

// ================================================================
// Permet de faire des replace de string
// ================================================================
function jsReplaceCal(inString, find, replace) {

    var outString = "";

    if (!inString) {
        return "";
    }

    // REPLACE ALL INSTANCES OF find WITH replace
    if (inString.indexOf(find) != -1) {
        // SEPARATE THE STRING INTO AN ARRAY OF STRINGS USING THE VALUE IN find
        t = inString.split(find);

        // JOIN ALL ELEMENTS OF THE ARRAY, SEPARATED BY THE VALUE IN replace
        return (t.join(replace));
    }
    else {
        return inString;
    }
}
// ================================================================

// ================================================================
// fonction qui permetde recuperer une partie d'une 
// String corespondant a un format
// ================================================================
function getPartOfDateCal(dateSt,dateFormat,whose) {
    var ret = false;

    if (dateFormat.indexOf("/") > 0)
      {
	tabDateFormat = dateFormat.split('/');
	tabDateSt = dateSt.split('/');
      }
    else
      {
	tabDateFormat = dateFormat.split('-');
	tabDateSt = dateSt.split('-');
      }
    
    if (tabDateFormat.length != tabDateSt.length)
      {
        return false;
      }
    else {
        for (i=0;i<tabDateFormat.length;i++) {
            if (whose == "DAY" && tabDateFormat[i] == "dd") {
                ret = tabDateSt[i];
                break;
            }
            else {
                if (whose == "MONTH" && tabDateFormat[i] == "MM") {
                    ret = tabDateSt[i];
                    break;
                }
                else {
                    if (whose == "YEAR" && tabDateFormat[i] == "yyyy") {
                        ret = tabDateSt[i];
                        break;
                    }
                    else {
                        if (whose == "YEAR" && tabDateFormat[i] == "yyyy") {
                            ret = tabDateSt[i].substring(2,4);
                            break;
                        }
                    }
                }
            }
        }
    }
    return ret;
}
// ================================================================

//*******************************************************************
// SET FIELD VALUE TO THE DATE SELECTED
//*******************************************************************
function returnDate(cal,suffix)
{
    if (suffix == null || suffix == "none") { suffix = ""; }
    calDate = cal.date;
    outDate = createOutDateCal(calDateFormat);
    day = getPartOfDateCal(outDate,calDateFormat,"DAY");
    month = getPartOfDateCal(outDate,calDateFormat,"MONTH");
    year = getPartOfDateCal(outDate,calDateFormat,"YEAR");
    caldayfield.selectedIndex = day-1;
    calmonthfield.selectedIndex = month-1;
    nowDate = new Date();
    for (idxOpt = 0; idxOpt < calyearfield.options.length;idxOpt++)
    {   
	if (calyearfield.options[idxOpt].value == year)
	{
		calyearfield.selectedIndex = idxOpt;
		break;
	}
    }

    if (typedate != '' && formName != '')
        if (typedate == 'from') {
            onChangeFrom(suffix);
			}
        else {
            onChangeTo(suffix);
		}
}
//*******************************************************************
