//======================================================================
// Fonction qui permet de verifier qu'au moins un jour a ete selectionne
// Elle renvoie true si un moins un jour est selectionne
//======================================================================
function CheckDays ()
{
    var response = false;
    // Attention !!! Les champs du formulaire doivent se nommer dayallowed0,dayallowed1,...
    for (i=0; i<=6; i++) {
        if (eval("document.forms[0].dayallowed"+i+".checked") == true) {
            response = true;
        }
    }
    return response;
}


// ================================================================
// GetYear: Get the year.
// Redefinition de la fonction GetYear car:
// Avec Netscape: 2000 => 100
// Avec IE:       2000 => 2000
// ================================================================
function GetTheYear(date) {  
    year = date.getYear(); 
    if (year <= 1900) year += 1900; 
    return year;
}     
// ================================================================

// ================================================================
// Récupère l'année sur 4 digits
// ================================================================
function y2k(number) { 
    return (number < 1000) ? number + 1900 : number; 
}
// ================================================================

// ================================================================
// compare 2 dates:
// vrai si date2 >= date1;
// ================================================================
function Date2_sup_Date1 (Day1, Month1, Year1, Day2, Month2, Year2) {
    if (Day1<=9) Day1 = '0'+parseInt(Day1,10);
    if (Day2<=9) Day2 = '0'+parseInt(Day2,10);
    if (Month1<=9) Month1 = '0'+parseInt(Month1,10);
    if (Month2<=9) Month2 = '0'+parseInt(Month2,10);
    if (parseInt(Year2,10)>parseInt(Year1,10)) 
    {
    	return true;
    }
    else 
    {
        if (parseInt(Year2,10) == parseInt(Year1,10))
        {
            if (parseInt(Month2,10)>parseInt(Month1,10))
            {
            	return true;
        	}
            else 
            {
                if (parseInt(Month2,10) == parseInt(Month1,10)) 
                {
                    if (parseInt(Day2,10)>=parseInt(Day1,10)) 
                    	return true;
                    else 
                        return false;
                }
                else 
                    return false;
            }
        }
        else 
            return false;
    }
}
// ================================================================
//================================================================
//compare 2 dates:
//vrai si date2 >= date1;
//================================================================
function Date2_strict_sup_Date1 (Day1, Month1, Year1, Day2, Month2, Year2) {
 if (Day1<=9) Day1 = '0'+parseInt(Day1,10);
 if (Day2<=9) Day2 = '0'+parseInt(Day2,10);
 if (Month1<=9) Month1 = '0'+parseInt(Month1,10);
 if (Month2<=9) Month2 = '0'+parseInt(Month2,10);
 if (parseInt(Year2,10)>parseInt(Year1,10)) 
 {
 	return true;
 }
 else 
 {
     if (parseInt(Year2,10) == parseInt(Year1,10))
     {
         if (parseInt(Month2,10)>parseInt(Month1,10))
         {
         	return true;
     	}
         else 
         {
             if (parseInt(Month2,10) == parseInt(Month1,10)) 
             {
                 if (parseInt(Day2,10)>parseInt(Day1,10)) 
                 	return true;
                 else 
                     return false;
             }
             else 
                 return false;
         }
     }
     else 
         return false;
 }
}


// ================================================================
// Permet de vérifier si une date est valide
// ================================================================
function checkdate(d,m,y) {
    if (y < 100) y = y+1900;
    var yl=1900;   // least year to consider
    var ym=2200;   // most year to consider
    if (m<1 || m>12)  return(false);
    if (d<1 || d>31)  return(false);
    if (y<yl || y>ym) return(false);
    if (m==4 || m==6 || m==9 || m==11)
        if (d==31) 
            return(false);
        if (m==2) {
            var b=parseInt(y/4,10);
            if (isNaN(b)) 
                return(false);
            if (d>29)
                return(false);
            if (d==29 && ((y/4)!=parseInt(y/4,10))) 
                return(false);
        }
    return(true);
}
// ================================================================

// ================================================================
// Permet de mettre à jour le nombre de nuits
// ================================================================
function updateNbNight(formName,day1,month1,year1,day2,month2,year2,suffix,nbnightsfieldname){
		
    if (suffix == null || suffix == "none") { suffix = ""; }
    
    var nbnightsfield = "";
    if (nbnightsfieldname != undefined)
    	nbnightsfield = "document."+formName+"."+nbnightsfieldname;
    else
    	nbnightsfield = "document."+formName+".nbnight"+suffix;
    
    if(eval(nbnightsfield) != undefined)
    {
    	date1 = new Date(year1,month1-1,day1,6,0,0);
        date2 = new Date(year2,month2-1,day2,6,0,0);

        var difference = Date.UTC(y2k(date1.getYear()),date1.getMonth(),date1.getDate(),6,0,0)
                         - Date.UTC(y2k(date2.getYear()),date2.getMonth(),date2.getDate(),6,0,0);

        diff = difference/1000/60/60/24;
        
        if(diff > 0) {
           nbnight_type = eval(nbnightsfield+".type");
          if (nbnight_type == "select-one")
    	eval(nbnightsfield+".selectedIndex=\""+(diff-1)+"\"");
          else
    	eval(nbnightsfield+".value=\""+diff+"\"");
         }
    }
}
// ================================================================

// ================================================================
// Permet d'updater la date de fin
// ================================================================
function updateEndDate(formName,myDay,myMonth,myYear,daysToAdd,suffix) {
	if((document.forms[formName].elements['dayDateTo'+suffix] == undefined) || 
			(document.forms[formName].elements['monthDateTo'+suffix] == undefined) || 
			(document.forms[formName].elements['yearDateTo'+suffix] == undefined)
			) return;
	
    if (suffix == null || suffix == "none") { suffix = ""; }
    savemyYear = myYear;

    myMonth = myMonth - 1;
    convertDate = new Date(myYear,myMonth,myDay,6,0,0);
    newDate = new Date(convertDate.getTime() + daysToAdd*24*60*60*1000);

    CurDay = newDate.getDate();
    CurMonth = newDate.getMonth();
    CurYear = y2k(newDate.getYear()); 

    CurMonth = CurMonth +1;

    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");
    }
}
// ================================================================
// Permet d'updater la date de debut
// ================================================================
function updateNextStartDate(suffix) {
    if (suffix == null || suffix == "none") { suffix = ""; }
    
    var myYearElem = eval("document."+formName+".elements['yearDateTo"+suffix+"']");
    var myMonthElem = eval("document."+formName+".elements['monthDateTo"+suffix+"']");
    var myDayElem = eval("document."+formName+".elements['dayDateTo"+suffix+"']");
    
    if (myYearElem && myMonthElem && myDayElem)
    {
	    myYear = myYearElem.options[myYearElem.selectedIndex].value;
	    myMonth = myMonthElem.options[myMonthElem.selectedIndex].value;
	    myDay = myDayElem.options[myDayElem.selectedIndex].value;
	    convertDate = new Date(myYear,myMonth,myDay,6,0,0);
		newDate = convertDate;
	    CurDay = newDate.getDate();
	    CurMonth = newDate.getMonth();
	    CurYear = y2k(newDate.getYear()); 
	
	    //CurMonth = CurMonth +1;
		if (eval("document."+formName+".elements['dayDateFrom"+(suffix+1)+"']")) 
		{
		    if ((CurDay > 0) && (CurMonth > 0) && (CurYear >0)) {
		        eval("document."+formName+".elements['dayDateFrom"+(suffix+1)+"'].selectedIndex="+(CurDay - 1));
		        eval("document."+formName+".elements['monthDateFrom"+(suffix+1)+"'].selectedIndex="+(CurMonth-1));
		    }
		    
		    if (CurMonth<10) CurMonth = "0"+CurMonth;
		    if (CurDay<10) CurDay = "0"+CurDay;
		
		    size =  eval("document."+formName+".yearDateFrom"+eval(suffix+1)+".options.length");
		    for (i=0;i<size;i++) {
		        if (CurYear == eval("document."+formName+".yearDateFrom"+(suffix+1)+".options[i].value")) eval("document."+formName+".yearDateFrom"+(suffix+1)+".selectedIndex = i");
		    }
			if (eval("document."+formName+".yearDateFrom"+(suffix+1))) 
			{
				onChangeFrom(eval(suffix+1));
				if (eval("document."+formName+".yearDateFrom"+(suffix+2))) 
				{
					updateNextStartDate(eval(suffix+1));
				}
			}
		 }
    }
}

// ================================================================
// Permet d'updater les dates precedentes
// ================================================================
function updatePreviousDate(suffix) {
    if (suffix == null || suffix == "none") { suffix = ""; }
  if ((suffix-1)>=0) {
    myYear = eval("document."+formName+".elements['yearDateFrom"+suffix+"'].options[document."+formName+".elements['yearDateFrom"+suffix+"'].selectedIndex].value");
    myMonth = eval("document."+formName+".elements['monthDateFrom"+suffix+"'].options[document."+formName+".elements['monthDateFrom"+suffix+"'].selectedIndex].value")-1;
    myDay = eval("document."+formName+".elements['dayDateFrom"+suffix+"'].options[document."+formName+".elements['dayDateFrom"+suffix+"'].selectedIndex].value");
    convertDate = new Date(myYear,myMonth,myDay,6,0,0);
	 newDate = convertDate;
    CurDay = newDate.getDate();
    CurMonth = newDate.getMonth();
    CurYear = y2k(newDate.getYear()); 

    CurMonth = CurMonth +1;

    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-1)+"'].selectedIndex="+(CurDay - 1));
        eval("document."+formName+".elements['monthDateTo"+(suffix-1)+"'].selectedIndex="+(CurMonth-1));
    }
    size =  eval("document."+formName+".yearDateTo"+(suffix-1)+".options.length");
    for (i=0;i<size;i++) {
        if (CurYear == eval("document."+formName+".yearDateTo"+(suffix-1)+".options[i].value")) eval("document."+formName+".yearDateTo"+(suffix-1)+".selectedIndex = i");
    }
	if (eval("document."+formName+".yearDateTo"+(suffix-1))) {
			onChangeTo((suffix-1));
		if (eval("document."+formName+".yearDateTo"+(suffix-2))) {
			updatePreviousDate((suffix-1));
		}
	}
  }
}

// ================================================================

// ================================================================
// Permet de récupérer la taille d'un tableau
// ================================================================
function makeArray(n) {
    this.length = n;return this;
}

// ================================================================
// Permet d'extraire une date d'une succession de select
// ================================================================
function ExtractDate(formName,field) {
    var date = new String(eval('document.'+formName+'.elements[\"'+field+'\"].options[document.'+formName+'.elements[\"'+field+'\"].selectedIndex].value'));
    return date;
}
// ================================================================

// ================================================================
// fonction qui permetde recuperer une partie d'une 
// String corespondant a un format
// ================================================================
function getPartOfDate(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;
}
// ================================================================

// ================================================================
// fonction permettant de creer une date
// ================================================================
function createOutDate(dateFormat,paramDate) {

	calToUse = calDate;
	if (paramDate)
		calToUse = paramDate; 
	
    // SET THE DATE RETURNED TO THE USER
    var day = calToUse.getDate()+"";
    if (day.length == 1)
        day = 0+day;

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

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

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

// ================================================================
// Permet de faire des replace de string
// ================================================================
function jsReplace(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;
    }
}
// ================================================================

// ================================================================
// ENSURE THAT VALUE IS TWO DIGITS IN LENGTH
// ================================================================
function makeTwoDigit(inValue) {

    var numVal = parseInt(inValue, 10);

    // VALUE IS LESS THAN TWO DIGITS IN LENGTH
    if (numVal < 10) {

        // ADD A LEADING ZERO TO THE VALUE AND RETURN IT
        return("0" + numVal);
    }
    else {
        return numVal;
    }
}
// ================================================================

// ================================================================
// CHECK TO SEE IF YEAR IS A LEAP YEAR
// ================================================================
function isLeapYear (Year) {

    if (((Year % 4)==0) && ((Year % 100)!=0) || ((Year % 400)==0)) {
        return (true);
    }
    else {
        return (false);
    }
}
// ================================================================

