//Fonction pour detecter et créer un objet active X pour l'utilsation de certaine fonction Ajax
// creates an XMLHttpRequest instance
function createXmlHttpRequestObject(){
//Récupération de XMLHttpRequest object
var xmlHttp;
	//Cela fonctionnera pour tous les navigateur excepté IE6 et plus vieux
	try{
	//On essaie de créer le XMLHttpRequest object
	xmlHttp = new XMLHttpRequest();
	}
	catch(e){
	// Prend en compte IE6 et plus vieu
	var XmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0',
	'Msxml2.XMLHTTP.5.0',
	'Msxml2.XMLHTTP.4.0',
	'Msxml2.XMLHTTP.3.0',
	'Msxml2.XMLHTTP',
	'Microsoft.XMLHTTP');
	//On essai tout les prog jusqu'a ce qu'il fonctionne
		for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)
		{
	  		try{
	  		// On essaie de créer XMLHttpRequest object
	 		 xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
	 		}
	  		catch (e) {} // On ignore les erreurs potentiel
	  	}
	}
//On retourne l'objet créé ou on affiche un message d'erreur.
	if (!xmlHttp)
	alert("Error creating the XMLHttpRequest object.");
	else
	return xmlHttp;
}

//On définit la variable xmlHttp pour qu'elle puisse être exploitée dans la fonction process, sinon les scripts utilisant AJAX ne fonctionne pas
var xmlHttp = createXmlHttpRequestObject();

/* CORRECTIF POUR LE BACKGROUND FIXED IE6 */
function fixed_back(select,offX,offY){
		if (offX.lastIndexOf("%") != -1){
			var img = new Image();
			img.src = select.currentStyle.backgroundImage.substring(5,Number(select.currentStyle.backgroundImage.length-2));
			var tempX = Number(offX.substring(0,offX.lastIndexOf("%")));
			tempX = Number((((document.documentElement.clientWidth-img.width)*tempX)/100)+document.documentElement.scrollLeft-select.offsetLeft)+'px';
		} else {
			var tempX = Number(offX.split("px")[0]);
			tempX = Number(tempX+document.documentElement.scrollLeft-select.offsetLeft)+'px';
		}
		if (offY.lastIndexOf("%") != -1){
			var img = new Image();
			img.src = select.currentStyle.backgroundImage.substring(5,Number(select.currentStyle.backgroundImage.length-2));
			var tempY = Number(offY.substring(0,offY.lastIndexOf("%")));
			tempY = Number((((document.documentElement.clientHeight-img.height)*tempY)/100)+document.documentElement.scrollTop-select.offsetTop)+'px';
		} else {
			var tempY = Number(offY.split("px")[0]);
			tempY = Number(tempY+document.documentElement.scrollTop-select.offsetTop)+'px';
		}
		return tempX+' '+tempY;
}
/*FIN CORRECTIF POUR BG FIXED IE6 */

/*** ANIMATION DRAPEAUX SUR ROLLOVER ***/
function flagSelect(cible){
flag = document.getElementById(cible);

	if(flag.src = 'img/drapeaux/lang-'+cible+'-on.gif', false){
	flag.src = 'img/drapeaux/lang-'+cible+'-on.gif';
	}

}
function flagUnSelect(cible,langue){
flag = document.getElementById(cible);

	if(cible == langue){
	flag.src = 'img/drapeaux/lang-'+cible+'-on.gif';
	}
	else{
	flag.src = 'img/drapeaux/lang-'+cible+'-off.gif';
	}
	
}
/*** FIN ANIMATION DRAPEAUX SUR ROLLOVER ***/

/*** INTERACTION ICONE BLOC DE RECHERCHE HOME PAGE ***/
function actualiserBlocRechercheHP(value){
	
    var displayRecherche = value.responseText;
    var blocRecherche = document.getElementById('ResultRchEnvie');
    blocRecherche.innerHTML = displayRecherche;
}
function rchEnvie(){
	var xmlHttpEnvie = createXmlHttpRequestObject();
	var allBoxEnvie = document.getElementsByName('envie'); 
	var tableBoxEnvie = new Array();
	
	if(!allBoxEnvie){
        return;
	}
	for(var i=0; i < allBoxEnvie.length; i++) {
		  
		if(allBoxEnvie[i].checked == true){
		tableBoxEnvie.push(allBoxEnvie[i].value);
		}
		
	}
	document.getElementById('facilityid').value = tableBoxEnvie;
	/* Envoi de la requête à la page de traitement */
	xmlHttpEnvie.open('GET','/lib/AjaxRechercheHome.php?params='+tableBoxEnvie,true);
	
	document.getElementById('ResultRchEnvie').innerHTML = 'loading process...';
	
		xmlHttpEnvie.onreadystatechange = function(){
			/* Lorsque l'état est à 4 */
			if(xmlHttpEnvie.readyState == 4){
						
				/* Si on a un statut à 200 */
				if(xmlHttpEnvie.status == 200){
				document.getElementById('ResultRchEnvie').innerHTML = 'Patientez...';
				actualiserBlocRechercheHP(xmlHttpEnvie)
				}
						
			}
		
		}
	xmlHttpEnvie.send(null);
	var tableBoxEnvie = new Array();	
}
function RchHomeOnglet(cible){

	if(cible == 'Loupe'){
	
	document.getElementById('Loupe').src = 'img/elements/rchLoupe-on.jpg';
	document.getElementById('IconeRchLoupe').className = 'spanRechercheIconeOn';
	document.getElementById('IconePositionRchLoupe').className = 'spanRechercheIconePositionOn';
	document.getElementById('IconePositionRchLoupe').innerHTML = '<img src="img/elements/PositionOngletRch.jpg" width="30" height="8" />';
	document.getElementById('BlocRechercheHotel').style.display = 'block';
	
	document.getElementById('Coeur').src = 'img/elements/rchCoeur-off.jpg';
	document.getElementById('IconeRchCoeur').className = 'spanRechercheIconeOff';
	document.getElementById('IconePositionRchCoeur').className = 'spanRechercheIconePositionOff';
	document.getElementById('IconePositionRchCoeur').innerHTML = '';
	document.getElementById('BlocRechercheEnvie').style.display = 'none';
	}
	
	if(cible == 'Coeur'){
	
	document.getElementById('Coeur').src = 'img/elements/rchCoeur-on.jpg';
	document.getElementById('IconeRchCoeur').className = 'spanRechercheIconeOn';
	document.getElementById('IconePositionRchCoeur').className = 'spanRechercheIconePositionOn';
	document.getElementById('IconePositionRchCoeur').innerHTML = '<img src="img/elements/PositionOngletRch.jpg" width="30" height="8" />';
	document.getElementById('BlocRechercheEnvie').style.display = 'block';
	
	document.getElementById('Loupe').src = 'img/elements/rchLoupe-off.jpg';
	document.getElementById('IconeRchLoupe').className = 'spanRechercheIconeOff';
	document.getElementById('IconePositionRchLoupe').className = 'spanRechercheIconePositionOff';
	document.getElementById('IconePositionRchLoupe').innerHTML = '';
	document.getElementById('BlocRechercheHotel').style.display = 'none';
	}
}
/*** FIN INTERACTION ICONE BLOC DE RECHERCHE HOME PAGE ***/

/******** CALENDRIER HOME ********************************/
function GetFocus(field,fieldHidden,datePicked){
var params = document;
							
var jour = datePicked.substr(0,2); 
var mois = datePicked.substr(3,2);
var annee = datePicked.substr(6,4); 
							
params.getElementById('fday').value = jour;
params.getElementById('fmonth').value =  mois;
params.getElementById('fyear').value =  annee;
							
params.getElementById('tday').value = jour;
params.getElementById('tmonth').value =  mois;
params.getElementById('tyear').value = annee;
							
params.getElementById('fday').blur(),
params.getElementById('defineDateAr').blur(),
params.getElementById(field).focus();
							
return false();
}
						
function customRange(input) {  
							
var ParamDate = input.id == "defineDateDe" ? $("#defineDateAr").datepick("getDate") : null;
	//Fonction qui ajoutera des jours à la date definit d'arriver 
	//pour afficher une date de départ cohérente
	addDays = function(d, j){
	return new Date(d.getTime() + (1000 * 60 * 60 * 24 * j));
	}
	//On test si le champ date d'arrivée est vide ou non, 
	//si le champ est null on met la date du jour en rérérence
	if(ParamDate == null){
	var ParamDate = new Date();
	}
//Définition de l'affichage des date de depart du calendrier en fonction de la date d'arrivée
//Pour supprimer un jour addDays(NewParamsDate, -1)
//Pour ajouter un jour addDays(NewParamsDate, 1)
//Pour la date D, addDays(NewParams)
var NewParamsDate = new Date(ParamDate);
var defineDateDepart = addDays(NewParamsDate,1);
		
	if(input.id == 'defineDateAr'){
		return {minDate: -0, maxDate: +360}; 
	}	
	if(input.id == 'defineDateDe'){
		return {minDate: defineDateDepart,  
       maxDate: +360}; 
	} 
}
						
function defineDate(datePicked){
var jour = datePicked.substr(0,2); 
var mois = datePicked.substr(3,2);
var annee = datePicked.substr(6,4);
							
var params = document; 
							
params.getElementById('tday').value = jour;
params.getElementById('tmonth').value =  mois;
params.getElementById('tyear').value = annee;
}
/*************** FIN CALENDRIER HOME PAGE ************/

/* TEST INPUT NEWSLETTER */
function CreateAgeEnfant(value){
	
var destination = document.getElementById('CelluleAgeEnfant');	
var par = document;

		
		destination.innerHTML = "";
		
		if(value == '0'){
		
		var new_input = par.createElement('input');
	    new_input.name = 'age0';
		new_input.id = 'age0';
		new_input.type = 'hidden';
		new_input.value = '0';
		destination.appendChild(new_input);
		
			
		}
		else{

			for(var i=0; i<value; i++){

			var new_select = par.createElement('select');
	    	new_select.name = 'age'+i;
			new_select.id = 'age'+i;
			new_select.className = 'inputNewsletter';
			destination.appendChild(new_select);
			
			var listselect = document.getElementById('age'+i);
			
			opt0 = new Option('0', '0',true, true);
			listselect.options['0'] = opt0;
			opt1 = new Option('1', '1',false, false);
			listselect.options['1'] = opt1;
			opt2 = new Option('2', '2',false, false);
			listselect.options['2'] = opt2;
			opt3 = new Option('3', '3',false, false);
			listselect.options['3'] = opt3;
			opt4 = new Option('4', '4',false, false);
			listselect.options['4'] = opt4;
			opt5 = new Option('5', '5',false, false);
			listselect.options['5'] = opt5;
			opt6 = new Option('6', '6',false, false);
			listselect.options['6'] = opt6;
			opt7 = new Option('7', '7',false, false);
			listselect.options['7'] = opt7;
			opt8 = new Option('8', '8',false, false);
			listselect.options['8'] = opt8;
			opt9 = new Option('9', '9',false, false);
			listselect.options['9'] = opt9;
			opt10 = new Option('10', '10',false, false);
			listselect.options['10'] = opt10;
			opt11 = new Option('11', '11',false, false);
			listselect.options['11'] = opt11;
			opt12 = new Option('12', '12',false, false);
			listselect.options['12'] = opt12;
			opt13 = new Option('13', '13',false, false);
			listselect.options['13'] = opt13;
			opt14 = new Option('14', '14',false, false);
			listselect.options['14'] = opt14;
			opt15 = new Option('15', '15',false, false);
			listselect.options['15'] = opt15;
			opt16 = new Option('16', '16',false, false);
			listselect.options['16'] = opt16;
			opt17 = new Option('17', '17',false, false);
			listselect.options['17'] = opt17;
			opt18 = new Option('18', '18',false, false);
			listselect.options['18'] = opt18;
			opt19 = new Option('19', '19',false, false);
			listselect.options['19'] = opt19;
			opt20 = new Option('20', '20',false, false);
			listselect.options['20'] = opt20;
			
		}	
	
	}
	
}
/* FIN TEST INPUT NEWSLETTER*/




//Pour binder les événements sans écraser le reste
function addEvent(obj, evType, fn){
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, true);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
 } else {
    return false;
 }
}

//Vérifier la validité du moteur de recherche de la homepage
function checkSearchEngineHP(){
	var form = document.getElementById('MoteurRechercheForm');
	var destination = document.getElementById('zoneid');

	if (destination.options[destination.selectedIndex].value == ''){
	
		if (!document.getElementById('errorMsg')){
			var errorMsg = document.createElement('div');
			errorMsg.setAttribute('id', 'errorMsg');
			errorMsg.innerHTML = 'Veuillez indiquez une destination'
			//form.parentNode.insertBefore(errorMsg, form)
			form.insertBefore(errorMsg, form.firstChild)
			
			if (destination.onchange = 'undefined'){
				destination.onchange = function(){
				var errorMsg = document.getElementById('errorMsg');
					if (errorMsg)
						errorMsg.parentNode.removeChild(errorMsg)
				}
			}
		}

	return false;
	}
	
	else{
		if (document.getElementById('defineDateAr').value == ''){
			var fday = document.getElementById('fday');
			var fmonth = document.getElementById('fmonth');
			var fyear = document.getElementById('fyear');
		
			if (fday) fday.parentNode.removeChild(fday);
			if (fmonth) fmonth.parentNode.removeChild(fmonth);
			if (fyear) fyear.parentNode.removeChild(fyear);
		}
			
		if (document.getElementById('defineDateDe').value == ''){
			var tday = document.getElementById('tday');
			var tmonth = document.getElementById('tmonth');
			var tyear = document.getElementById('tyear');
		
			if (tday) tday.parentNode.removeChild(tday);
			if (tmonth) tmonth.parentNode.removeChild(tmonth);
			if (tyear) tyear.parentNode.removeChild(tyear);		
		}
	return true;
	}
}
/***** FONCTION DU MOTEUR DE RECHERCHE DE LA BOUTIQUE *****/
function rchCoffret(value)
{
	var bloc = document.getElementById('rstRechercheCadeau');
    bloc.innerHTML = "veuillez patienter...";
    
	var budget = document.getElementById('budget').value;
	var theme = document.getElementById('theme').value;
	// var occasion = document.getElementById('occasion').value;
	
	// var chaine = 'theme='+theme+'&budget='+budget+'&occasion='+occasion;
	var chaine = 'theme='+theme+'&budget='+budget;
		
	/* On crée l'objet XHR */
	createXmlHttpRequestObject();	
			
	/* Envoi de la requête à la page de traitement */
	xmlHttp.open("GET","../boutique/ajax-rchcoffret.php?"+chaine, true);
		   
	/* On surveille le changement d'état de la requête qui va passer successivement de 1 à 4 */
	xmlHttp.onreadystatechange = function()
	{
	/* Lorsque l'état est à 4 */
		if(xmlHttp.readyState == 4)
		{
			/* Si on a un statut à 200 */
			if(xmlHttp.status == 200)
			{
				/* Mise à jour de l'affichage, on appelle la fonction apropriée */
				var nb = xmlHttp.responseText;
				var bloc = document.getElementById('rstRechercheCadeau');
				bloc.innerHTML = '<b>'+nb+'</b> Coffrets Cadeaux';
			}
		}
				
	}
	xmlHttp.send(null);
}
function verifRchCoffret(){
	
	var budget = document.getElementById('budget').value;
	var theme = document.getElementById('theme').value;
	// var occasion = document.getElementById('occasion').value;
	// var chaine = 'theme='+theme+'&budget='+budget+'&occasion='+occasion;
	var chaine = 'theme='+theme+'&budget='+budget;
	
	// if(theme == '' && budget == '' && occasion == ''){
	if(theme == '' && budget == ''){
	document.getElementById('rstRechercheCadeau').innerHTML = '<font style="color:red; font-size:9px;">Veuillez préciser votre recherche</font>';
	return(false);
	}
	else{
		var xmlHttpVerif = createXmlHttpRequestObject();
		xmlHttpVerif.open("GET","../boutique/ajax-rchcoffret.php?"+chaine, true);
		
		xmlHttpVerif.onreadystatechange = function()
		{		
			if(xmlHttpVerif.readyState == 4)
			{
				if(xmlHttpVerif.status == 200)
				{
					var nb = xmlHttpVerif.responseText;
					var bloc = document.getElementById('rstRechercheCadeau');
					
					if(nb == '0'){
						bloc.innerHTML = '<font style="color:red; font-size:9px;">Il n’y a pas de résultats<br/>correspondant à votre recherche</font>';
						return(false);
					}
					else{
						document.forms['coffretcadeau'].submit();
					}
				}
			}
		}
	xmlHttpVerif.send(null);
	}
	
}
