
// Array of month titles
var monthTitles = new Array("", "Janv.", "Févr.", "Mars", "Avr.", "Mai", "Juin", "Juil.", "Août", "Sept.", "Oct.", "Nov.", "Déc.");

// Array of week day names
var weekDayTitles = new Array("Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi");


// only blanks or empty pattern
var onlyBlanksPattern = /^\s*$/;

// contains numbers pattern test
var containsNumberPattern = /[0-9]+/;

// contains letters pattern test
var containsLetterPattern = /[a-zA-Z]+/;

// email pattern
var emailPattern = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/ ;
// email domains list
var domainExtList='ac, ad, ae, af, ag, ai, al, am, an, ao, aq, ar, as, at, au, aw, az, ba, bb, bd, be, bf, bg, bh, bi, bj, bm, bn, bo, br, bs, bt, bv, bw, by, bz, ca, cc, cd, cf, cg, ch, ci, ck, cl, cm, cn, co, cr, cs, cu, cv, cx, cy, cz, de, dj, dk, dm, do, dz, ec, ee, eg, eh, er, es, et, fi, fj, fk, fm, fo, fr, fx, ga, gb, gd, ge, gf, gg, gh, gi, gl, gm, gn, gp, gq, gr, gs, gt, gu, gw, gy, hk, hm, hn, hr, ht, hu, id, ie, il, im, in, io, iq, ir, is, it, je, jm, jo, jp, ke, kg, kh, ki, km, kn, kp, kr, kw, ky, kz, la, lb, lc, li, lk, lr, ls, lt, lu, lv, ly, ma, mc, md, mg, mh, mk, ml, mm, mn, mo, mp, mq, mr, ms, mt, mu, mv, mw, mx, my, mz, na, nc, ne, nf, ng, ni, nl, no, np, nr, nt, nu, nz, om, pa, pe, pf, pg, ph, pk, pl, pm, pn, pr, ps, pt, pw, py, qa, re, ro, ru, rw, sa, sb, sc, sd, se, sg, sh, si, sj, sk, sl, sm, sn, so, sr, st, su, sv, sy, sz, tc, td, tf, tg, th, tj, tk, tm, tn, to, tp, tr, tt, tv, tw, tz, ua, ug, uk, um, us, uy, uz, va, vc, ve, vg, vi, vn, vu, wf, ws, ye, yt, yu, za, zm, zr, zw, com, edu, gov, int, mil, net, org, biz, pro, info, aero, name, coop, arpa, nato, museum, EoF';

// Util 'private' function to build the day select list
//
// dayField : the day form select id. Example : document.searchForm.day
// monthNumber : the month number (1-12).
// yearNumber : the year number (yyyy).
// 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 buildDaysListFromValues(dayField, monthNumber, yearNumber, firstDayNumber, firstMonthNumber, firstYearNumber, preselectedDayParam) {

	var selectedDayNumber = 1;
	if (preselectedDayParam != null && preselectedDayParam != "") {
	    selectedDayNumber = parseInt(preselectedDayParam, 10);
    }

    var currMth = monthNumber;
    var currYear = yearNumber;

    var nextFirstDayMth = currMth == 12 ? 1 : currMth+1;
    var nextFirstDayYear = currMth == 12 ? currYear+1 : currYear;
    var nextFirstDayDate = new Date(nextFirstDayYear, nextFirstDayMth-1, 1);

    var lastDayListDate = new Date(nextFirstDayDate - 86400000);
    var lastDayList = lastDayListDate.getDate();

    var firstDayList = currMth == firstMonthNumber && currYear == firstYearNumber ? firstDayNumber : 1;

    var index = 0;
    dayField.options.length = 0;

    var closestDayDiff = 35;

    for (var i = firstDayList; i < lastDayList + 1; i++) {
        var optionValue = i < 10 ? "0" + i : i;
        dayField.options[dayField.length] = new Option(i, optionValue);
        var dayDiff = Math.abs(i-selectedDayNumber);
        if (dayDiff < closestDayDiff) {
            closestDayDiff = dayDiff;
            index  = i-firstDayList;
        }
    }

    dayField.options[index].selected = true;
}


// Function used to test if the given string is a correct name (not only spaces and without numbers)
// NEEDED GLOBAL CONSTANTS : onlyBlanksPattern, containsNumberPattern.
//
// nameStr : the string to test.
function isCorrectName(nameStr) {
    return !onlyBlanksPattern.test(nameStr) && !containsNumberPattern.test(nameStr);
}

// Function used to test if the given string is a correct name (not only spaces)
// NEEDED GLOBAL CONSTANTS : onlyBlanksPattern.
//
// nameStr : the string to test.
function isCorrectString(str) {
	return !onlyBlanksPattern.test(str);
}

// Function used to test if the given string is a correct number (contains number and no letters)
//
// nameStr : the string to test.
function isCorrectNumber(numStr) {
	return !onlyBlanksPattern.test(numStr) && containsNumberPattern.test(numStr) && !containsLetterPattern.test(numStr);
}

// Function used to check if the given string is a correct email.
// NEEDED GLOBAL CONSTANTS : emailPattern, domainExtList.
//
// emailStr the string to test.
function isCorrectEMail(emailStr) {
    var validMail = emailPattern.test(emailStr);

    var validDomainName = false;
    if (validMail) {
      var DotPos= emailStr.lastIndexOf(".");
      if (DotPos > 0) {
          var domainExt=domainExtList.split(", ");
          var emailExt=emailStr.substr(DotPos+1);
          emailExt=emailExt.toLowerCase();
          for(i=0; domainExt.length; i++) {
              if (domainExt[i]=='EoF') {
                  break; //infinite loop else
              }
              if (emailExt==domainExt[i]) {
                  validDomainName=true;
                  break;
              }
          }
      }
    }
    return(validMail && validDomainName);
}

// Util function used to get a completed with zero number String.
// Example : 1 -> "01" ; 9 -> "09" ; 12 -> "12"
//
// inputNumber : the number to treat.
function completeNumberWithZero(inputNumber) {
        if (inputNumber < 10) {
            return "0" + String(inputNumber);
        } else {
            return String(inputNumber);
        }
}

// Util 'private' function used to get a message with arguments inside.
//
// mesg : the string message with #0, #1, ...
// arg0, arg1, arg2, arg3 : the args to put in string.
function getArgMessage(mesg, arg0, arg1, arg2, arg3) {
    var msg = mesg;
    if (arg0 != null && arg0 != "") {
        msg = msg.replace(/#0/ , arg0);
    }
    if (arg1 != null && arg1 != "") {
        msg = msg.replace(/#1/ , arg1);
    }
    if (arg2 != null && arg2 != "") {
        msg = msg.replace(/#2/ , arg2);
    }
    if (arg3 != null && arg3 != "") {
        msg = msg.replace(/#3/ , arg3);
    }
    return msg;
}



// opens a pop up for the calendar
function popupCalendar (url,title) {
      show = window.open(url,title,"resizable=no,height=180,width=185,scrollbars=no,screenX=300,screenY=400,top=400,left=300");
}

// Util method to open a popup
function openPopupWindow(url, windowName, width, height, left, top) {
    popup = window.open(url,
                        windowName,
                        "resizable=no,width="+width+",height="+height+",left="+left+",top="+top+",dependent=no,scrollbars=yes");
}

// Util method to redirect to an url
function redirect(url) {
  window.location = url;
}

// Util method to redirect to an url
function redirectWithAnchorReserver(url) {
  window.location = url + "#reserver"; ;
}

// Util method to check if a string is in an array
function isInArray(strArray, strText) {
   var found = false;
   var j;
   for (j=0; j < strArray.length && !found; j++) {
     if (strArray[j] == strText) {
       found = true;
     }
   }

   return found;
}

// this function redirects to action confirmOtherPrebooking
// NEEDED GLOBAL VAR : incorrectCommentsMsgGlobal
function validateAndSubmitOtherPrebooking() {
    if (document.forms['preBookingForm'].elements['comments'].value == "") {
        alert(incorrectCommentsMsgGlobal);
    } else {
        document.forms['preBookingForm'].submit();
    }
}

//this function redirects to action storeFormulaInSession?formulaKey=ALL
function returnAllOferts() {
    window.location = "storeFormulaInSession.do?formulaKey=ALL";
}