/**
*
* Check if the form is filled in login page.
*/

function submitFormular(value1,value2) {

    if (value1.value!="" && value2.value!="") {
        return true;
    }
    else {
        alert("Les champs PCC et AgentId sont obligatoires!");
        return false;
    }

}
var checked ='price';

var monthArray = new Array('','Jan','Fév','Mars','Avr','Mai','Juin','Juil','Août','Sept','Oct','Nov','Déc');

/**
*
* Put the kind of sort to make to engine results.
*/
function setSortType(value) {
    document.forms[0].sortType.value = value;
    document.forms[0].submit();

}

/******************************************/
/** Classe representant une disponibilité */
/******************************************/
/**
* Constructeur
*/
function Dispo(depCityCode, depDate, depDate2, price, nbDays, nbNigths, oldPrice, percentage) {
    this.depCityCode = depCityCode;
    // Pour l'affichage dans la combobox
    var date = depDate.split("-");
    var day = date[2];
    var month = date[1];
    var monthFr = monthArray[Number(month)];
    var year = date[0];
    this.depDate = new Array(day+" "+monthFr+" "+year);
    this.depDate2 = new Array(day+"-"+month+"-"+year);
    this.price = price;
    this.oldPrice = oldPrice;
    this.percentage = percentage;
    this.nbDays = nbDays;
    this.nbNigths = nbNigths;
}
/**
* Methode toString
*/
Dispo.prototype.toString = function() {
	if (this.price != this.oldPrice) {
		return  'le ' + this.depDate 
			+ ' pour ' + this.price + '€  au lieu de ' + this.oldPrice + '€ '
			+ '(- ' + this.percentage + '%)' 
			+ ' - ' + this.nbDays + ' j / ' + this.nbNigths + ' n';
	} else {
		return  'le ' + this.depDate 
			+ ' pour ' + this.price + '€ - ' 
			+ this.nbDays + ' j / ' + this.nbNigths + ' n';	
	}
}
/**
* Methode stringInValue
*/
Dispo.prototype.stringInValue = function() {
    return  this.depDate2 + '-' + this.nbDays + '-' + this.nbNigths;
}
/******************************************/

/**
* Fonction qui cherche un control recursivement a partir d'un autre control *UNIQUE* dans la page .
* Attention: l'id du control parent d'ou la recherche commence doit etre unique dans la page.
* @param parentId control parent d'ou la recherche recursive commence
* @param controlId l'id du control qu'on cherche
*/
function findControlByParentAndControlId(parentId, controlId)
{
	// On recupere le control parent d'ou la recherche va commencer
	// Attention: l'id doit etre unique ou sur IE, ca va pas marcher
	var result;
	var children = document.getElementById(parentId).childNodes;

	// Si le control de depart a des controls enfants, on cherche parmi eux
	if(children.length > 0)
	{
	   	var i = 0;

	   	// On cherche dans les controls enfants du premier niveau
	   	for(i = 0; i < children.length; i++)
	   	{   	   	   	
	   	   	if (children[i].id == controlId)
	   	   	   	result = children[i];
	   	}

	   	// Si on trouve le control, on sort
	   	if (result)
	   		return result;

	   	// Si le control n'est pas trouve dans le premier niveau, on cherche en profondeur
	   	for(i = 0; i < children.length; i++)
	   	{
	   	   findControlByParentAndControlId(children[i], controlId);
	   	}   		
	}
	else
	   	result = null;

	return result;
}


/**
* Remplissage de la select box des dispos sur la fiche produit.
* @param dispoSelectBoxName dispo select box name
* @param citySelectBoxName city select box name
* @param disposArray array of all js dispos objects
* @param selectedDispo option dispo to select (from salesprocess)
*/
function fillSelectBoxDispo(dispoSelectBoxName, citySelectBoxName, disposArray, selectedDispo) {

    // On recupere le code de la ville selectionnée
	//var controls[] = document.getElementById(citySelectBoxName);
	var departureCityCombo = findControlByParentAndControlId('formulaireReservation', citySelectBoxName);
	
	
		//alert('combo found: ' + departureCityCombo.id + " " + departureCityCombo.innerHTML);
	
	var selectedDepCityCode = departureCityCombo.options[departureCityCombo.selectedIndex].value;


    // Suppression des anciennes Options
    document.getElementById(dispoSelectBoxName).length = 0;
    
    // Ajout des nouvelles Options
    var j = 0;
    for(var i = 0; i < disposArray.length; i++) {
        if (disposArray[i].depCityCode == selectedDepCityCode) {
            document.getElementById(dispoSelectBoxName).options[j] =
                        new Option(disposArray[i].toString(),
                                   disposArray[i].stringInValue());
            j++;
        }
    }

            }

/**
* Remplissage de la select box des dispos opposé afin d'avoir la meme ville en haut et en bas.
* @param position the number of the select box
* @param disposArray array of all js dispos objects
*/
function updateDispo(position, disposArray) {

    if (position == '1') {
        // On recupere le code de la ville selectionnée
        document.getElementById('dpci2').options[document.getElementById('dpci1').selectedIndex].selected = true;
        fillSelectBoxDispo('departure-dates2','dpci2', disposArray, 'null');

    } else {
        document.getElementById('dpci1').options[document.getElementById('dpci2').selectedIndex].selected = true;
        fillSelectBoxDispo('departure-dates1','dpci1', disposArray, 'null');
    }

}


/**
* Remet a jour les selectboxes opposés lors d'une modification
* @param position indique quel selct boxes prendre en compte.
* @param selectBoxName l'id de la selectBox
*/

function updateSelectBox(position,selectBoxName) {
     if (position == 1) {
        var selectBoxCur = selectBoxName + '1';
        var selectBoxTarget = selectBoxName + '2';
        document.getElementById(selectBoxTarget).options[document.getElementById(selectBoxCur).selectedIndex].selected = true;
     } else {
        var selectBoxCur = selectBoxName + '2';
        var selectBoxTarget = selectBoxName + '1';
        document.getElementById(selectBoxTarget).options[document.getElementById(selectBoxCur).selectedIndex].selected = true;
     }

}



/**
* Remplit les champs hidden qui servent pour la réservation
* @param position indique quel selct boxes prendre en compte.
*/

function fillHiddenParameters(position) {

    //var Dispo = new Dispo('','','','','');
    var dateDep = document.getElementById('dateDep');
    //var dayDuration = document.getElementById('dayDuration');
    //var nightDuration = document.getElementById('nightDuration');
    //var city = '';

    //var listDispo = new Array;

    //var j = 0;

    //Dispo = disposArray[document.getElementById('departure-dates').selectedIndex];
    //dateDep.value = Dispo.depDate2[0];
    //dayDuration.value = Dispo.nbDays;
    //nightDuration.value = Dispo.nbNigths;
    
}

/**
* Affichage du bon nombre de select box en fonction du nombre d'enfant sélectionné
* @param childrenSelectBoxName children select box name
* @param maxNbOfChildren max number of children
*/
function displayAgeBox(childrenSelectBoxName, maxNbOfChildren) {

    // Number of children pax
    var value = document.getElementById(childrenSelectBoxName).options[document.getElementById(childrenSelectBoxName).selectedIndex].value;
    // Affichage du nombre de selectbox necessaires
    for (var i = 1; i <= value; i++) {
        var divID = "ageChild" + i;
        ////alert(divID+" block");
        document.getElementById(divID).style.display = "block";
    }
    // Non affichage des derni�res selectbox inutiles
    for (var i = Number(value)+1; i <= maxNbOfChildren; i++) {
        var divID = "ageChild" + i;
        ////alert(divID+" none");
        document.getElementById(divID).style.display = "none";
    }
}

/**
* Remet a jour les selectboxes opposés lors d'une modification
* @param position indique quel selct boxes prendre en compte.
* @param selectBoxName l'id de la selectBox
* @param number the number of the ageChildSelectBox
*/

function updateSelectBoxAgeChild (position,selectBoxName, number) {
     if (position == 1) {
        var selectBoxCur = selectBoxName + '11' + number;
        var selectBoxTarget = selectBoxName + '22' + number;
        document.getElementById(selectBoxTarget).options[document.getElementById(selectBoxCur).selectedIndex].selected = true;
     } else {
        var selectBoxCur = selectBoxName + '22' + number;
        var selectBoxTarget = selectBoxName + '11' + number;
        document.getElementById(selectBoxTarget).options[document.getElementById(selectBoxCur).selectedIndex].selected = true;
     }

}

/**
* Links the Google Analytics cookies.
*/
function linkGAData() {
	if (window.pageTracker) {
		var inputElement = document.getElementById("reservationProfileChannelName");
		
		if (inputElement) {
			inputElement.value = pageTracker._getLinkerUrl("http://www.promosejours.com").replace("http://www.promosejours.com?", "");
		}
	}
}

