/**
 * Syst�me d'agrandissement/reduction des photos d'hotels
 * @author anguyenvan
 **/
function Photo(url, id, width, height, attr) {
	
	this.url = url;
	this.id = id;
	this.width = width;
	this.height = height;
	this.attr = attr;
	this.normalSize = true;
	
	this.changeSize = function() {
		if (this.normalSize) {
			this.normalSize = false;
			this.grow();
		}	else {
			this.normalSize = true;
			this.shrink();
		}
	}
	
	this.grow = function() {
		photo = document.getElementById('photo'+this.id);
		photo.width = this.width * 2;
		photo.height = this.height * 2;
	}
	
	this.shrink = function() {
		photo = document.getElementById('photo'+this.id);
		photo.width = this.width;
		photo.height = this.height;
	}
	
	this.show = function() {
		photo = document.getElementById('photo'+this.id);
		photo.style.display = 'block';
		photo.style.visibility = 'visible';
	}
			
	this.hide = function() {
		photo = document.getElementById('photo'+this.id);
		photo.style.display = 'none';
		photo.style.visibility = 'hidden';
	}
			
}
// 	Param�tre de l'API s�jours necessaires � l'execution d'une requ�te. 
var lang       = "fr_FR";
var formule    = new Array("sejours","circuits","croisieres","locations","ski","thalasso");
var typeFiltre = new Array("CONTINENT","PAYS","DATE_BY_MONTH","DATE_DEPART","NB_JOURS","VILLE_DEPART","PARTENAIRE","PRIX_DIV","PAGE");
var nbJours    = new Array("2", "5", "7", "14", "30");
// Objets prix et produit.
var priceCriterias;		
var productCriterias;	
/**
*	Objet clientCriteria  : param�tres client.
**/
function clientCriteria(lang, idCompte, ipClient, codeSite, codeClient) {
	this.lang       = lang;
	this.idCompte   = idCompte;
	this.ipClient   = ipClient;
	this.codeSite   = codeSite;
	this.codeClient = codeClient;
}
/**
*	Objet productCriteria  : crit�res de  produit.
**/
//****************************
//****     L'original     ****
//****************************
//function productCriteria(idMinResult, idMaxResult, idFormula, idPartner, idCountry, idContinent) {
//	this.idMinResult = idMinResult;
//	this.idMaxResult = idMaxResult;
//	this.idFormula   = idFormula;
//	this.idPartner   = idPartner;
//	this.idCountry   = idCountry;
//	this.idContinent = idContinent;
//	this.String = function() {
//		return "ProductCriteria :: idMinResult => " + this.idMinResult 
//			+ ", idMaxResult => " + this.idMaxResult 
//			+ ", idFormula => " + this.idFormula
//			+ ", idPartner => " + this.idPartner
//			+ ", idCountry =>" + this.idCountry
//			+ ", idContinent =>" + this.idContinent;
//	}
//}
/**
*	Objet productCriteria  : crit�res de  produit.
**/
function productCriteria(formule, partenaire, villea, pays, continent, page) {
	this.formule    = formule;
	this.partenaire = partenaire;
	this.villea   	= villea;
	this.pays  		= pays;
	this.continent  = continent;
	this.page 		= page;
	this.String = function() {
		return "ProductCriteria :: formule => " + this.formule 
			+ ", partenaire => " + this.partenaire 
			+ ", villea => " + this.villea
			+ ", pays => " + this.pays
			+ ", continent =>" + this.continent
			+ ", page =>" + this.page;
	}
}
//****************************
//****     L'original     ****
//****************************
/**
*	Objet priceCriteria  : crit�res de prix.
**/
//function priceCriteria(minDate, maxDate, minPrice, maxPrice, duration, durationInterval, idDepartureCity) {
//	this.minDate  = minDate;
//	this.maxDate  = maxDate;
//	this.minPrice = minPrice;
//	this.maxPrice = maxPrice;
//	this.duration = duration;
//	this.durationInterval = durationInterval;
//	this.idDepartureCity  = idDepartureCity;
//	this.String=function() {
//		return "PriceCriteria :: minDate => " + this.minDate
//			+ ", maxDate => "  + this.maxDate 
//			+ ", minPrice => " + this.minPrice 
//			+ ", maxPrice => " + this.maxPrice
//			+ ", duration => " + this.duration
//			+ ", durationInterval => " + this.durationInterval
//			+ ", idDepartureCity =>"   + this.idDepartureCity;
//	}
//}	
/**
*	Objet priceCriteria  : crit�res de prix.
**/
function priceCriteria(dateDepart, duree, montant, villed) {
	this.dateDepart  = dateDepart;
	this.duree  = duree;
	this.montant = montant;
	this.villed = villed;
	this.String=function() {
		return "PriceCriteria :: dateDepart => " + this.dateDepart
			+ ", duree => "  + this.duree
			+ ", montant => " + this.montant 
			+ ", villed =>"   + this.villed;
	}
}	
/**
*	Fonction qui retourne un mois convertie  en cha�nes
**/
function GetVarToString(myvar){
	return myvar=myvar+"";
}
/**
*	Fonction qui retourne un mois convertie  en cha�nes
**/
function GetDayToString(day){
	day=GetVarToString(day);
	if(day.length < 2) day="0"+day;
	return day;
}
/**
*	Fonction qui retourne un mois convertie  en cha�nes
**/
function GetMonthToString(month){
	month=GetVarToString(month);
	if(month.length < 2) return ("0"+month);
	return month;
}
/**
*	Fonction qui retourne une ann�e convertie  en cha�nes
**/
function GetYearToString(year){
	year=GetVarToString(year);
	if(year.length < 4) year="20"+year;
	return year;
}
/**
*	Fonction qui retourne une date au format sp�cifi�
**/
function formatDate(date,format){
	var day=date.getDate();
	var month=date.getMonth()+1;
	var year=date.getFullYear();
	if(format=="dd/mm/YYYY"){
		return GetDayToString(day)+"/"+GetMonthToString(month)+"/"+year;
	}
	if(format=="YYYY-mm-dd"){
		return year+"-"+GetMonthToString(month)+"-"+day;
	}
	if(format=="mmYY"){
		return (month+""+year);
	}else return;
}
/**
*	Fonction qui retourne la date de d'arriv�e 
**/
function GetMinDate(date){
	var minDate=GetDayOf(date);
	var today = new Date();
	var tomorrow = GetDayOfTomorrow(today);
	if(minDate.getMonth()==tomorrow.getMonth()){	
		return tomorrow;
	}
	else{
		return minDate;
	}
}
/**
*	Fonction qui retourne la date de d�part ou de retour
**/
function GetMaxDate(date){
	var maxDate=GetDayOf(date);
	var today = new Date();
	var tomorrow = GetDayOfTomorrow(today);
	// On v�rifie s'il s'agit du mois en cours
	if(maxDate.getDate()==1 || maxDate.getDate()==01){
		return GetLastDayOfThisMonth(maxDate.getMonth(),maxDate.getFullYear());
	}
	else{
		return maxDate;
	}
}

/**
*	Fonction retourne la date de
**/
function GetDayOf(date){
	var myDate = new Date();
	var mmYY=new RegExp("^[0-9]{4}$","g");
	var mmYYYY=new RegExp("^[0-9]{2}[-][0-9]{4}$","g");
	var YYYYmmdd=new RegExp("^[0-9]{4}[-]{1}[0-9]{2}[-]{1}[0-9]{2}$","g");
	var ddmmYYYY=new RegExp("^[0-9]{2}[/]{1}[0-9]{2}[/]{1}[0-9]{4}$","g");
	//	format : mmYY
	if(mmYY.test(date)){
		myDate = new Date(GetYearToString(date.substr(2,3)),date.substr(0,2)-1,1);
	}
	//	format : mm-YYYY
	if(mmYYYY.test(date)){
		myDate = new Date(GetYearToString(date.substr(3,3)),date.substr(1,1)-1,1);
	}
	//	format : YYYY-mm-dd
	if(YYYYmmdd.test(date)){
		myDate = new Date(date.substr(0,4),date.substr(6,1)-1,date.substr(8,2));
	}
	//	format : dd/mm/YYYY
	if(ddmmYYYY.test(date)){
		myDate = new Date(date.substr(6,4),date.substr(3,2)-1,date.substr(0,2));
	}
	return myDate;
}
/**
*	Fonction retourne la date de demain
**/
function GetDayOfTomorrow(day){
	day.setTime(day.getTime() + 24 * 3600 * 1000);
	var tomorrow = new Date(day.getFullYear(),day.getMonth(),day.getDate());
	return tomorrow;
}
/**
*	Fonction qui retourne le premier jour du mois
**/
function GetFirstDayOfThisMonth(month,year){
	var firstDay = new Date(year,month,01);
	return firstDay;
}
/**
*	Fonction qui retourne le dernier jour du mois
**/
function GetLastDayOfThisMonth(month,year){
	var lastDate = new Date(year,month,1);
	lastDate.setMonth(lastDate.getMonth() + 1);
	lastDate.setDate(0);
	return lastDate;
}
/**
*	Fonction qui retourne le prix minimal au format xxxxx_yyyyy => xxxxx
**/
function GetMinPriceFormat(prix){
	return prix.substr(0,prix.indexOf("_"));
}
/**
*	Fonction qui retourne le prix maximal au format xxxxx_yyyyy => yyyyy
**/
function GetMaxPriceFormat(prix){
	return prix.substr(prix.indexOf("_")+1, prix.length);
}
// Table de prix en cours d'affichage
var priceTable = null;
/**
*	Fonction qui affiche le tableau des prix
**/			
function ShowMorePrice(id) {
  var elt = document.getElementById('product'+id);
	//document.getElementById('href'+id).blur();
	if (priceTable != null && priceTable != elt) {
		priceTable.style.display = 'none';
		priceTable.style.visibility= 'hidden';
	}
	if (elt.style.visibility == "hidden") {
		elt.style.visibility = "visible";
		elt.style.display = "block";
		priceTable = elt;
	} else {
		elt.style.visibility = "hidden";
		elt.style.display = "none";
		priceTable = null;
	}
}

/**
*	Fonction qui donne l'url de la page courante sans les param�tres
* 	 ex: http://www.toto.com/index.php? 
**/
function GetUrlPage(){
	var url=document.location;
	url=url.toString();
	var pageName=url;
	var regexp=new RegExp("[?&]");
	var tab=url.split(regexp);
	if(tab!='null'||tab!='undefined') pageName=tab[0];
	return pageName; 
}

//****************************
//****     L'original     ****
//****************************
/**
*	Fonction qui teste les differentes valeurs de filtres en affectant � chaque param�tre la valeur correspondante 
*	et recharge la page avec les donn�es de l'API
**/
function GetCriteresByPage(type,valeur,denomination,valeurMax){
	switch (type){
		case typeFiltre[0]: productCriterias.idContinent = valeur; 
							break;
		case typeFiltre[1]: productCriterias.idCountry = valeur; 
							productCriterias.countryName = denomination; 
							break;
		case typeFiltre[2]: priceCriterias.minDate = formatDate(GetMinDate(valeur),"dd/mm/YYYY"); 
							priceCriterias.maxDate = formatDate(GetMaxDate(valeur),"dd/mm/YYYY");
							break;				
		case typeFiltre[3]: var val;
							priceCriterias.minDate = formatDate(GetMinDate(valeur),"dd/mm/YYYY");				
							if(valeurMax===undefined || valeurMax===null) {
								val=valeur;
							}else{
								val=valeurMax;
							}
							priceCriterias.maxDate = formatDate(GetMaxDate(val),"dd/mm/YYYY");
							break;
		case typeFiltre[4]: priceCriterias.duration=nbJours[parseInt(valeur)]; 
							break;
		case typeFiltre[5]: priceCriterias.idDepartureCity = valeur; 
							break;
		case typeFiltre[6]: productCriterias.idPartner = valeur; 
							break;
		case typeFiltre[7]: priceCriterias.minPrice = GetMinPriceFormat(valeur); 
							priceCriterias.maxPrice = GetMaxPriceFormat(valeur); 
							break;
//		case typeFiltre[8]: productCriterias.idMinResult = valeur; 
//							productCriterias.idMaxResult = valeurMax; 
//							break;
		case typeFiltre[8]: productCriterias.page = valeur; 
							break;
	}
	redirectTo(clientCriterias.lang,clientCriterias.idCompte,clientCriterias.ipClient,clientCriterias.codeSite,clientCriterias.codeClient,
		productCriterias.idMinResult,productCriterias.idMaxResult,productCriterias.idFormula,productCriterias.idPartner,productCriterias.idCountry,productCriterias.idContinent,
		priceCriterias.minDate,priceCriterias.maxDate,priceCriterias.minPrice,priceCriterias.maxPrice,priceCriterias.duration,priceCriterias.durationInterval,priceCriterias.idDepartureCity
	);
}

/**
* 	Fonction qui affiche la fiche d'un produit dont l'identifiant est pass� en param�tre 
**/
function GetProduct(idProduct,jour,mois,duree,villed,montant,mois2,jour2){
	var s="";
	//on vérifie que le mois est différent de 0 avant de gérer le mois2.
	//Meme principe pour le jour et jour2. Ces derniers doivent d'ailleurs etre gérer si mois et mois2 sont renseignés.
	if (mois!=0 && mois2 != 0) {
		s = s + "&mois2=" + mois2;
		if (jour!=0 && jour2 != 2) {
			s = s + "&jour2=" + jour2;
		}
	}

	//top.document.location
	window.open(GetUrlPage()+"?idProduct="+idProduct
		+"&mois="+mois
		+"&jour="+jour
		+"&duree="+duree
		+"&villed="+villed
		+"&montant="+montant
		+s, 'produit');
	
}

//******************************
//*****     L'Original     *****
//******************************
/**
*	Fonction de redirection 
**/
function redirectTo(lang, idCompte, ipClient, codeSite, codeClient,  
	formule,partenaire,villea,pays,continent,page,
	dateDepart, duree, montant, villed){
	top.document.location ="index.php?lang="+lang
		+"&idCompte="+idCompte
		+"&ipClient="+ipClient
		+"&codeSite="+codeSite
		+"&codeClient="+codeClient		
		+"&formule="+formule		
		+"&partenaire="+partenaire
		+"&villea="+villea		
		+"&pays="+pays
		+"&continent="+continent
		+"&page="+page
		+"&dateDepart="+dateDepart
		+"&duree="+duree
		+"&montant="+montant
		+"&villed="+villed;
}

// Defines that xmlHttp is a new variable.
var xmlHttp; 

// Mozilla, Safari, Konqueror, Opera
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
	try {
		xmlHttp = new XMLHttpRequest();
	} catch (e) {
		xmlHttp = false;
	}
}

xmlHttp.onreadystatechange = function() {
if(xmlHttp.readyState==4)
	if (xmlHttp.status == 200) { 
		document.location.href = xmlHttp.responseText;
		//window.open(xmlHttp.responseText, 'externe');
	} 
} 
			
function GetUrl(idCompte, idProduct, idPrice) {
	xmlHttp.open("get","../easyvoyage/package/url.php?idCompte="+idCompte+"&idProduct="+idProduct+"&idPrice="+idPrice);
	xmlHttp.send(null); // Since there is no supplied form, null takes its place as a new form.
}

// Constantes MEP

// Nom de la classe des l'�l�ments qui affichent et masquent les filtres 
var displayFilterClass="bouton_afficher";
var hiddenFilterClass="bouton_masquer";
// ID del'�l�ment qui contient les filtres suppl�mentaires
var dataRange="plageDonnee";

mesMois=new Array();
mesUrls=new Array();

function clik(parametre){window.open(parametre, '_blank', '');}
function bOver(t){t.style.background='#daffff';}
function bOut(t){t.style.background='#FFFFFF';}

/**
* 	Fonction affiche et masque le tableaude prix d'un produit (id)
**/
function Close(id){
	document.getElementById("tabPrice"+id).style.visibility="hidden";
}

/**
*	Fonction affiche et masque tous les prix d'un produit pour une ville de d�part donn�e (id) 
**/
function ShowMorePriceByVille(id){
	allPrices =  document.getElementsByName("tous_les_prix");
	var tab="tabPrice"+id;
	for (var i = 0; i < allPrices.length; i++){
		if(allPrices[i].id===tab){
			document.getElementById(allPrices[i].id).style.visibility="visible";
		}else{
			document.getElementById(allPrices[i].id).style.visibility="hidden";
		}
	}
}
/**
*	Fonction qui retourne l'ensemble des plages de filtres pr�alablement masqu�s
*	en fonction de l'attribut "class" du type de filtre
*	(ex: le filtre  CONTINENT, les plages seront les suivantes : Asie, Afriqur , Oc�anie , etc...
**/
function GetElementByClassName(strTag,strClass,strText){
	var elts = new Array();
	for (i=0;i<document.getElementsByTagName(strTag).length; i++) {
		if (document.getElementsByTagName(strTag).item(i).className == strClass) {
			elts[i]=document.getElementsByTagName(strTag).item(i);
			document.getElementsByTagName(strTag).item(i).className ="bouton_afficher";
		}
	}
	return elts;
}
/**
*
**/
function GetElementsByNodeAndClassName(nodeElt,tag,className){
	var elts = new Array();
	var j=0;
		for (i=0;i<nodeElt.getElementsByTagName(tag).length; i++) {
			if (nodeElt.getElementsByTagName(tag).item(i).className==className){
			mynode=nodeElt.getElementsByTagName(tag).item(i);
				filtre = new Filtre(mynode.firstChild.firstChild.data,
				mynode.lastChild.lastChild.firstChild.data,mynode.firstChild.href)
				elts[j]=filtre;
				j++;
			}
		}
	return elts;
}
/**
*	Objet filtre
**/
function Filtre(libelle,nbprice,href){
	this.libelle=libelle;
	this.nbprice=nbprice;
	this.href=href;
}
/**
*	Fonction qui masque/d�masque les plages de filtres suppl�mentaires 
**/
function FiltersDisplayer(filtersArray, actionEltClass) {
	// si le bouton a �t� cliqu�, la classe de l'�l�ment qui g�re l'affiche des filters passe de invisible � visible
	if (actionEltClass.className==hiddenFilterClass) {
		// on nettoie la plage de donn�e
		while (document.getElementById(dataRange).firstChild) {
			list=document.getElementById(dataRange);
			last=list.lastChild;
			list.removeChild(last);
		}
		// on injecte la liste de filtres suppl�mentaires dans l'element DOM DataRange
		if (filtersArray.length>0) {
			for (i=0;i<filtersArray.length;i++) {
				plaGetext = document.createTextNode(filtersArray[i].libelle);
				prixText = document.createTextNode(filtersArray[i].nbprice);
				list = document.createElement('LI');
				link = document.createElement('A');
				span = document.createElement('SPAN');
				span.className="nbtexte";
				span.appendChild(prixText);
				link.href=filtersArray[i].href;
				link.appendChild(plaGetext);
				link.appendChild(span);
				list.appendChild(link);
				document.getElementById(dataRange).appendChild(list);
			}
			document.getElementById(dataRange).style.display='block';
			document.getElementById(dataRange).style.visibility='visible';
			actionEltClass.className=displayFilterClass;
		}
	}
	// sinon la classe de l'�l�ment qui g�re l'affichage 
	//des filtres suppl�mentaires passe de visible � invisible 
	else{
		if(actionEltClass.className==displayFilterClass){
			document.getElementById(dataRange).style.display='none';
			document.getElementById(dataRange).style.visibility='hidden';
			actionEltClass.className=hiddenFilterClass;
		}
	}
	
}

