// JavaScript Document
function ShowHideLayers(divName,ShowHide) {
	if (ShowHide == 'show') {
		document.getElementById(divName).style.display = '';
	}
	else if (ShowHide == 'hide') {
		document.getElementById(divName).style.display = 'none';
	}
}

function ShowTooltip(show){

	if(show){
		var show = document.getElementById('tt1').style.display='block';
	}else{
		var hide = document.getElementById('tt1').style.display='none';
	}

}

function ReturnSiteCode(url_page)
{
if (String(url_page).indexOf('?') != -1){
    var code = String(url_page).split("?");
	code=String(code[1]).split("=");
	var code_chaine=code[1];
	if(code_chaine!="")
		return code_chaine;
  }
  else
    return false;
}

function replaceCodeChaine(code_chaine){
	if(code_chaine){
		document.forms.moteur_groupe_loisir.code_chaine.value=code_chaine;
	}
}


function showHideContratField(){
	if(document.moteur_groupe_loisir.t_fich[2].checked){
		ShowHideLayers('label_SC','show');
		ShowHideLayers('contractLabel','show');
		ShowHideLayers('contractField','show');
		ShowHideLayers('bt_valider_contrat1','hide');
	} else {
		ShowHideLayers('label_SC','hide');
		ShowHideLayers('contractLabel','hide');
		ShowHideLayers('contractField','hide');
		ShowHideLayers('bt_valider_contrat1','show');
	}
}

function initFromRequest(){
	if ((request_iata_type!=null) && (request_iata_type!="")){
		if (request_iata_type=="SC" || request_iata_type=="sc")
			document.forms.moteur_groupe_loisir.t_fich[2].checked=true;
		else if (request_iata_type=="RT" || request_iata_type=="rt")
			document.forms.moteur_groupe_loisir.t_fich[1].checked=true;
		else
			document.forms.moteur_groupe_loisir.t_fich[0].checked=true;
			
		showHideContratField();
	}			
			
	if ( (request_num_iata!=null) &&(request_num_iata!="") )
		document.forms.moteur_groupe_loisir.client.value=request_num_iata;
		
	if ( (request_num_contrat!=null) &&(request_num_contrat!="") )
		document.forms.moteur_groupe_loisir.num_contrat.value=request_num_contrat;	 	 	
}



function calcule_nb_chambre(champs) {

	somme = (Number(document.moteur_groupe_loisir.chambre_doubles.value) + Number(document.moteur_groupe_loisir.chambre_triples.value) + Number(document.moteur_groupe_loisir.chambre_simples.value) + Number(document.moteur_groupe_loisir.chambre_twins.value))
	if(isNaN(somme)){ somme = 0; champs.value=0;}
	if(somme < 0){ somme = 0; champs.value=0;}
	
	// nombre de chambres
	document.getElementById('nb_chambres').innerHTML = somme;
	
	// nombre de pax
	//document.getElementById('nb_pax').innerHTML = calcule_nb_pax();

}
function retourne_nb_chambre() {

	somme = (Number(document.moteur_groupe_loisir.chambre_doubles.value) + Number(document.moteur_groupe_loisir.chambre_triples.value) + Number(document.moteur_groupe_loisir.chambre_simples.value) + Number(document.moteur_groupe_loisir.chambre_twins.value))
	return somme;
}

function calcule_nb_pax() {

	sommePax = (Number(document.moteur_groupe_loisir.chambre_doubles.value)*2 + Number(document.moteur_groupe_loisir.chambre_triples.value)*3 + Number(document.moteur_groupe_loisir.chambre_simples.value) + Number(document.moteur_groupe_loisir.chambre_twins.value)*2)
	return sommePax;
}

function submitThisForm(){

	document.getElementById('message').className = "visible";
	var destination = document.getElementById('destination').value;
	var ref_tour = document.getElementById('ref_tour').value;
		
	// var pour les chambres
	var chambre_doubles=Number(document.moteur_groupe_loisir.chambre_doubles.value);
	var chambre_triples=Number(document.moteur_groupe_loisir.chambre_triples.value);
	var chambre_simples=Number(document.moteur_groupe_loisir.chambre_simples.value);
	var chambre_twins=Number(document.moteur_groupe_loisir.chambre_twins.value);

	var somme = retourne_nb_chambre();
	var num_pax = calcule_nb_pax();
		
	//verification qu'aucun champ obligatoire n'est rempli
	if (destination=="" && ref_tour=="" && somme<8 && num_pax < 15){
			
		document.getElementById('message').innerHTML = messageAllMandatoryField;
		document.getElementById('destination').style.backgroundColor = ISerreurfond;
		document.getElementById('ref_tour').style.backgroundColor = ISerreurfond;
		document.getElementById('chambre_simples').style.backgroundColor = ISerreurfond;
		document.getElementById('chambre_twins').style.backgroundColor = ISerreurfond;

		document.getElementById('chambre_triples').style.backgroundColor = ISerreurfond;
		document.getElementById('chambre_doubles').style.backgroundColor = ISerreurfond;
		return false;	
	}else{
			//alert("TOTAL PAX: " + num_pax);
		document.getElementById('destination').style.backgroundColor = ISorigfond;
		document.getElementById('ref_tour').style.backgroundColor = ISorigfond;
		document.getElementById('chambre_simples').style.backgroundColor = ISorigfond;
		document.getElementById('chambre_twins').style.backgroundColor = ISorigfond;
		document.getElementById('chambre_triples').style.backgroundColor = ISorigfond;
		document.getElementById('chambre_doubles').style.backgroundColor = ISorigfond;
	}
	calcule_nb_chambre();
	
	var limiteNbreChambresSimples = 30*Number(document.getElementById('nb_chambres').innerHTML);
	var totalChambresSimple = 100*Number(document.moteur_groupe_loisir.chambre_simples.value);
	
	//verification nombre chambre >=8		 
	if(somme<8 || num_pax < 15){
	
		if(destination==null || destination=="") { 
			if (ref_tour==null || ref_tour==""){
				document.getElementById('message').innerHTML = messageMandatoryField+messageDestination+messageRef+messageTotalSimples;
				document.getElementById('destination').style.backgroundColor = ISerreurfond;
				document.getElementById('ref_tour').style.backgroundColor = ISerreurfond;
				document.getElementById('chambre_simples').style.backgroundColor = ISerreurfond;
				document.getElementById('chambre_twins').style.backgroundColor = ISerreurfond;
				document.getElementById('chambre_triples').style.backgroundColor = ISerreurfond;
				document.getElementById('chambre_doubles').style.backgroundColor = ISerreurfond;
			}
			else {
				document.getElementById('message').innerHTML = messageMandatoryField+messageDestination+messageTotalSimples;
				document.getElementById('destination').style.backgroundColor = ISerreurfond;
				document.getElementById('chambre_simples').style.backgroundColor = ISerreurfond;
				document.getElementById('chambre_twins').style.backgroundColor = ISerreurfond;
				document.getElementById('chambre_triples').style.backgroundColor = ISerreurfond;
				document.getElementById('chambre_doubles').style.backgroundColor = ISerreurfond;
			}
		}
		else if(ref_tour==null || ref_tour=="") {
			document.getElementById('message').innerHTML = messageMandatoryField+messageRef+messageTotalSimples;
			document.getElementById('ref_tour').style.backgroundColor = ISerreurfond;
			document.getElementById('chambre_simples').style.backgroundColor = ISerreurfond;
			document.getElementById('chambre_twins').style.backgroundColor = ISerreurfond;
			document.getElementById('chambre_triples').style.backgroundColor = ISerreurfond;
			document.getElementById('chambre_doubles').style.backgroundColor = ISerreurfond;
		}
		else {
			isErrorTotalChambre = true;
			errorTotalChambre();
			document.getElementById('chambre_simples').style.backgroundColor = ISerreurfond;
			document.getElementById('chambre_twins').style.backgroundColor = ISerreurfond;
			document.getElementById('chambre_triples').style.backgroundColor = ISerreurfond;
			document.getElementById('chambre_doubles').style.backgroundColor = ISerreurfond;
		}
		return false;
	}else{
		document.getElementById('destination').style.backgroundColor = ISorigfond;
		document.getElementById('ref_tour').style.backgroundColor = ISorigfond;
		document.getElementById('chambre_simples').style.backgroundColor = ISorigfond;
		document.getElementById('chambre_twins').style.backgroundColor = ISorigfond;
		document.getElementById('chambre_triples').style.backgroundColor = ISorigfond;
		document.getElementById('chambre_doubles').style.backgroundColor = ISorigfond;
	}
	
	// verification ratio chambres simple vs total chambres
	if(totalChambresSimple>limiteNbreChambresSimples){
		isErrorTropDeChambresSimples = true;
		errorTropDeChambresSimples();
		document.getElementById('chambre_simples').style.backgroundColor = ISerreurfond;
		return false;
	}else{
		document.getElementById('chambre_simples').style.backgroundColor = ISorigfond;
	}
	
	//verification destination remplie 
	if(destination==null || destination==""){ 
	
		if (ref_tour==null || ref_tour==""){
			document.getElementById('message').innerHTML = messageMandatoryField+messageDestination+messageRef;
			document.getElementById('destination').style.backgroundColor = ISerreurfond;
			document.getElementById('ref_tour').style.backgroundColor = ISerreurfond;
		}
		else {
			document.getElementById('message').innerHTML = messageMandatoryField+messageDestination;
			document.getElementById('destination').style.backgroundColor = ISerreurfond;
		}
		return false;	
	} else{
		document.getElementById('destination').style.backgroundColor = ISorigfond;
		document.getElementById('ref_tour').style.backgroundColor = ISorigfond;
	}
	//verification reference remplie
	if(ref_tour==null || ref_tour==""){
		document.getElementById('message').innerHTML = messageMandatoryField+messageRef;
		document.getElementById('ref_tour').style.backgroundColor = ISerreurfond;
		return false;	
	} 
	else{
		isErrorTotalChambre = false;
		brandCriteria_submit();
		document.getElementById('message').className = "hidden";
		document.getElementById('ref_tour').style.backgroundColor = ISorigfond;
		return true;
	}
	if(!isNaN(document.getElementById('destination').value)){
		document.getElementById('code_chaine').value="ALL";	
	}
	
}

function errorTotalChambre(){
	if(isErrorTotalChambre==true){
		document.getElementById('message').innerHTML = messageMandatoryField+messageTotalSimples;
	}
}

function errorTropDeChambresSimples(){
	if(isErrorTropDeChambresSimples==true){
		document.getElementById('message').innerHTML = messageTropDeChambresSimples;
	}
}


function errorTropDePax(){
	if(isErrorTropDePax==true){
		document.getElementById('message').innerHTML = messageTropDePax;
	}
}


var tabBrand = new Array('SOF','MER','NOV','IBI','PUL','SUI','ASE', 'MGA');

function brandCriteria_submit(){	
	var brandCodes='';
	//Cas toutes les cases cochees
	var allb = true ;
	for (var i=0 ; i < tabBrand.length ; i++){
		if (document.getElementById('brand_' + tabBrand[i]).checked==true){
			if (brandCodes != '') { brandCodes += '|' ; }
			brandCodes += tabBrand[i]; 
			
		} else allb =false ;
	}
	if (allb) brandCodes = 'ALL'; // toutes les cases cochées
	if (brandCodes == '') brandCodes = 'ALL'; // aucune case cochée
	document.getElementById('code_chaine').value=brandCodes;
}


function getNumeroAgv(){
   document.getElementById('client').value = d_numeroAgv;
}
function checkRightAgvType(){
	if('SC'==d_typeAgv)	
   		document.moteur_groupe_loisir.t_fich[2].checked = true;
   	else if('RT'==d_typeAgv)
   		document.moteur_groupe_loisir.t_fich[1].checked = true;
   	else
   		document.moteur_groupe_loisir.t_fich[0].checked = true;

}

