/**
* Adds the trim function to String objects.
*/
String.prototype.trim = function() {

// skip leading and trailing whitespace
// and return everything in between
var x=this;
x=x.replace(/^\s*(.*)/, "$1");
x=x.replace(/(.*?)\s*$/, "$1");
return x;
}

/**
* Checks if the specified value is null or empty.
* @param val The value to check.
*/
function isBlank(val){
if(val == null){
return true;
}
for(var i=0;i<val.length;i++) {
if ((val.charAt(i) != ' ') && (val.charAt(i) != "\t")
&& (val.charAt(i) != "\n") && (val.charAt(i) != "\r")){
return false;
}
}
return true;
}

/**
* Converts the specified number in a two digits number.
* @param number The number to format.
*/
function formatTo2Digits(number) {
if (number < 10) {
return ("0" + number);
}
return ("" + number);
}

var daysOfWeek = ["Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"];

/**
* Writes the name of the day selected in the drop-down boxes in the specified element.
* @param id the id of the element in which the day name must be written.
* @param formName the name of the form.
* @param monthFieldName the name of the month and year drop-down box.
* @param dayFieldName the name of the day drop-down box.
*/
function printDayOfWeek(id, formName, monthFieldName, dayFieldName) {
var time = getSelectedTime(formName, monthFieldName, dayFieldName);
var node = document.getElementById(id);
var children = node.childNodes;
node.replaceChild(document.createTextNode(daysOfWeek[time.getDay()]), children[0]);

}

/**
* Returns the value selected in the specified drop-down box.
* @param formName the name of the form.
* @param fieldName the name of the drop-down box.
*/
function getSelectedValue(formName, fieldName) {
var form = document.forms[formName];
return (form.elements[fieldName].options[form.elements[fieldName].selectedIndex].value)
}

/**
* Returns the date specified in the drop down boxes.
* @param formName the name of the form.
* @param monthFieldName the name of the month and year drop-down box.
* @param dayFieldName the name of the day drop-down box.
*/
function getSelectedTime(formName, monthFieldName, dayFieldName) {

var year = getSelectedYear(formName, monthFieldName);
var month = getSelectedMonth(formName, monthFieldName);
return (new Date(year, month, getSelectedValue(formName, dayFieldName)));
}

/**
* Returns the year selected in the specified dropdown list.
* The year must be the 4 first digits of the value selected in the specified dropdown list.
* @param formName the name of the form.
* @param fieldName the name of the drop-down box.
*/
function getSelectedYear(formName, fieldName) {
return getSelectedValue(formName, fieldName).substring(0, 4);
}

/**
* Returns the month selected in the specified dropdown list.
* The month must be the fifth and the sixth digits of the value selected in the specified dropdown list.
* @param formName the name of the form.
* @param fieldName the name of the drop-down box.
*/
function getSelectedMonth(formName, fieldName) {
var monthNumber = getSelectedValue(formName, fieldName).substring(4, 6);
return (monthNumber - 1);
}

/**
* Populates the day drop-down box with the number of days corresponding
* to the month specified by the month drop-down box.
* @param formName the name of the form.
* @param monthFieldName the name of the month drop-down box.
* @param dayFieldName the name of the day drop-down box.
*/
function populateDayField(formName, monthFieldName, dayFieldName) {

timeA = new Date(getSelectedYear(formName, monthFieldName), getSelectedMonth(formName, monthFieldName) + 1, 1);
timeDifference = timeA - 86400000;
timeB = new Date(timeDifference);
var daysInMonth = timeB.getDate();
var form = document.forms[formName];

var selectedDay = form.elements[dayFieldName].value;
if (selectedDay > daysInMonth) {
selectedDay = daysInMonth;
}

for (var i = 0; i < form.elements[dayFieldName].length; i++) {
form.elements[dayFieldName].options[0] = null;
}
for (var i = 0; i < daysInMonth; i++) {
form.elements[dayFieldName].options[i] = new Option(i + 1, formatTo2Digits(i + 1));
}
form.elements[dayFieldName].options[selectedDay - 1].selected = true;

}

var errors = new Array();

/**
* Validates the form. This function calls the validateFields()
* function and expects that its return an array which contains for each
* error an array containing the error message and a list of the elements id to highlight.
* @return true if the form is valide.
*/
function validateForm() {

//for (var i = 0; i < errors.length; i++) {
// modif olivier suite probleme resa
//for (var j = 1; j < errors[i].length; j++) {
//i=1;
//document.getElementById(errors[i][j]).className = null;
//}
//}

errors = validateFields();

if (errors.length > 0) {
document.getElementById("errorDiv").style.display = "block";
var cell = document.getElementById("errorMessages");
var children = cell.childNodes;
var nbChildren = children.length;
for (var i = nbChildren - 1; i >=0; i--) {
cell.removeChild(children[i]);
}
var ul = document.createElement("ul");
for (var i = 0; i < errors.length; i++) {
var li = document.createElement("li");
li.appendChild(document.createTextNode(errors[i][0]));
ul.appendChild(li);
// modif olivier suite probleme resa
//for (var j = 1; j < errors[i].length; j++) {
//i=1;
//document.getElementById(errors[i][j]).className = "errorHighlight";
//}
}
cell.appendChild(ul);
return false;
}

return true;
}
function showTimeSelection(formName, dateRangefieldName, timeFieldName, cellId) {
if (document.forms[formName].elements[dateRangefieldName].value == "0") {
document.getElementById(cellId).style.display = "block";
document.getElementById(cellId + "2").style.display = "none";
} else {
document.forms[formName].elements[timeFieldName].value = "ANY";
document.getElementById(cellId).style.display = "none";
document.getElementById(cellId + "2").style.display = "block";
}
}
