<!--//
/*/////////////////////////////////////////////////////////////////////////
// - search_box-v2.js // version 2.0
///////////////////////////////////////////////////////////////////////////
// - DESCRIPTION
//-------------------------------------------------------------------------
// Dynamic Search Input: Destinations, Hotels, Stars
///////////////////////////////////////////////////////////////////////////
// - DEPENDENCIES SITE : AVC, SVC
// - DEPENDENCIES FILE : search_box_data-v2.php
///////////////////////////////////////////////////////////////////////////
// - HISTORY MODIFICATION
//-------------------------------------------------------------------------
// Benoit	16DEC06  Add comment
// Benoit	02FEV07  Add sb_lang for support different language
///////////////////////////////////////////////////////////////////////////
// - CONTACT INFO
//-------------------------------------------------------------------------
// Benoit Renaud (benoit at voyagesconstellation.com)
/////////////////////////////////////////////////////////////////////////*/

var arrayAlldest = new Array();
var arrayCountrydest = new Array();

function createSelectDest(targetSelectId,selected,withAllDest,withGroupCountry,forHotelUse,multiple)
{
	
	//Variable initialisation
	var destArray = new Array();
	destArray = sortAssoc(arrayDestination,"PAYS");
	var arrayCountryAlreadyAdd = new Array();
	var tempCountrydest = new Array();
	//Traduction
	try { var sb_alldest = _gtext("sb_alldest"); } catch(e) { var sb_alldest = "Toutes destinations"; };
	try { var sb_all = _gtext("sb_all"); } catch(e) { var sb_all = "Tout"; };		

	if(!document.getElementById(targetSelectId)) return false;
	var selectBox = document.getElementById(targetSelectId); //Selectbox
	selectBox.options.length = 0; // Clear this selectbox
	var countOption = 0;
	//check if is a multiple select
	if(multiple == true)
	{
		var sSelected = new String(selected); //Split the selected dest string and make an array
		var tSelected = sSelected.split(',');
	}
	if(withGroupCountry == true)//Group by Country true or false
	{
		//Make the all dest grouping by country
		for (var destcode in destArray)
		{
			var currentCountry = destArray[destcode]["PAYS"];
			var arrayCheck = (tempCountrydest[currentCountry]?tempCountrydest[currentCountry]:"");
			if((!arrayCheck)){tempCountrydest[currentCountry] = new Array();}
			tempCountrydest[currentCountry].push(destcode);//Add Dest code in Country grouping array
		}
	}//withGroupCountry == true
	
	//Loop on the destination
	for (var destcode in destArray)
	{
		if(withAllDest == true)//Include all dest. choice true or false
		{
			arrayAlldest.push(destcode);//Add code in the all dest array
			//First execution print the destination name and a space before
			if(countOption==0)
			{
				selectBox.options[countOption] = new Option(sb_alldest,"#");
				countOption++;
			}
		}//withAllDest == true
		
		if(withGroupCountry == true)//Group by Country true or false
		{
			var currentCountry = destArray[destcode]["PAYS"];
			if(!in_array(currentCountry, arrayCountryAlreadyAdd))
			{
				selectBox.options[countOption] = new Option(sb_all+" "+currentCountry,tempCountrydest[currentCountry].join(","));
				selectBox.options[countOption].className = "country";
				if(multiple == true)//is a multiple selected box
				{
					if(in_array(selectBox.options[countOption].value, tSelected)){selectBox.options[countOption].selected = true;}
				}
				else//just one option cant be selected
				{
					if(selected==selectBox.options[countOption].value){selectBox.options[countOption].selected = true;}
				}
				countOption++;
				arrayCountryAlreadyAdd.push(currentCountry);
			}
		}//withGroupCountry == true
		
		//Concat the valid hotel at the selectbox
		selectBox.options[countOption] = new Option(ucwords(destArray[destcode]['NAME']+", "+destArray[destcode]['PAYS']),destcode);
		selectBox.options[countOption].className = "destination";
		if(multiple == true)//is a multiple selected box
		{
			if(in_array(selectBox.options[countOption].value, tSelected)){selectBox.options[countOption].selected = true;}
		}
		else//just one option cant be selected
		{
			if(selected==selectBox.options[countOption].value){selectBox.options[countOption].selected = true;}
		}
		countOption++;
	}
	//Include all dest. choice true or false
	if(withAllDest == true){
		selectBox.options[0].value = arrayAlldest.join(",");
		if(selected==selectBox.options[0].value){selectBox.options[0].selected = true;}
	}//Update the all destination value
	//If for Hotel use save the country sort by dest array
	if(forHotelUse == true){arrayCountrydest = tempCountrydest;}
	return true;
}
function getCountryByDest(dest_array)
{
	//Loop on the country
	//alert(dest_array);
	//alert(arrayCountrydest["Cuba"].length);
	for (var country in arrayCountrydest)
	{
		if(compare_array(dest_array, arrayCountrydest[country]))
			return country;
	}
	return false;
}
function setDestByHotel(hot_num,dest)
{
	var selectBox = document.getElementById("pDestForfaits"); //Selectbox
	if(selectBox && hot_num && dest)
	{
		var arrayHotelSelect = arrayHotel[dest];
		for(var x=0;x<arrayHotelSelect.length;x++)
		{
			if(arrayHotel[dest][x]["NUM"] == hot_num)
			{
				if(document.getElementById("pDestForfaits").value != dest)
					document.getElementById("pDestForfaits").value = dest;
			}
		}//loop on arrayHotelSelect
	}
}
function createSelectHotel(dest,star,selected)
{
	/*if((arrayDestination) && (typeof(arrayDestination) == "object") && (arrayDestination.length > 0))
	{*/
		//Variable initialisation
		if(isNaN(star)) var star = 2; //default star value
		if(dest)
		{
			var sDestList = new String(dest); //Split the dest string and make an array
			var tDestArray = sDestList.split(',');
			var country = getCountryByDest(tDestArray);//return country name or false
		}
		else{
			tDestArray = arrayAlldest;
		}
		try { var sb_allhot = _gtext("sb_allhot"); } catch(e) { var sb_allhot = "Tous les hotels"; };		
		if(!document.getElementById("pHotel")) return false;
		var selectBox = document.getElementById("pHotel"); //Selectbox
		selectBox.options.length = 0; // Clear this selectbox
		var countOption = 0;
		
		//Loop on the destination
		for(var y=0; y<tDestArray.length; y++)
		{
			//Variable initialisation for each destination
			var currentDest = tDestArray[y];
			//Add start string message
			if(countOption == 0){
				selectBox.options[countOption] = new Option(sb_allhot+(tDestArray.length == 1?" de "+arrayDestination[currentDest]["NAME"]:(country?" de "+country:"")),"");
				if(selected==selectBox.options[countOption].value){selectBox.options[countOption].selected = true;}
				countOption++;
			}
			if((arrayHotel[currentDest]) && (typeof(arrayHotel[currentDest]) == "object") && (arrayHotel[currentDest].length > 0))
			{
				var tDestHotPoss = new Array();
				tDestHotPoss = sortAssoc(arrayHotel[currentDest],"NAME","NUM");
				var selectDest = arrayDestination[currentDest]["NAME"]+", "+arrayDestination[currentDest]["PAYS"];
				//Count the hotel poss before the selectbox creation
				var tHotList = new Array();
				for(var i=0; i<tDestHotPoss.length; i++)
				{
					//Check the hotel star vs the selected star value
					if((tDestHotPoss[i]) && (typeof(tDestHotPoss[i]) == "object") && (tDestHotPoss[i]['HALFSTARS']))
					{
						if(parseInt(tDestHotPoss[i]['HALFSTARS']) >= parseInt(star)){
							tHotList.push(i);
						}
					}
				}
				// Add the valid hotel for the select dest and the select star
				for(var x=0; x<tHotList.length; x++)
				{
					//First execution print the destination name and a space before
					if(x==0)
					{
						if(tDestArray.length > 1){
							//Add space if the Destination is set
							if(countOption > 0){
								selectBox.options[countOption] = new Option("","#");
								countOption++;
							}
							//Add the destination name in Uppercase
							selectBox.options[countOption] = new Option(selectDest,"#");
							selectBox.options[countOption].className = "country";
							countOption++;
						}
					}
					//Concat the valid hotel at the selectbox
					selectBox.options[countOption] = new Option("- "+ucwords(tDestHotPoss[tHotList[x]]['NAME']),tDestHotPoss[tHotList[x]]['NUM']);
					selectBox.options[countOption].id = currentDest;//POP
					if(selected==selectBox.options[countOption].value){selectBox.options[countOption].selected = true;}
					countOption++;
				}
			}
		}
	//}//Check on the Destination array exist
}
function checkSelectHotel(valeur,index)
{
	/*
	if(document.getElementById("pDestForfaits").value == arrayOpt[index].id){ return false;}
	VERIFIER SI LA DEST EST DIFFERENTE
	*/
	var numOpt = index+1; //Numero de l'option suivante celle selectionné
	var selectBox = document.getElementById("pHotel");
	var arrayOpt = selectBox.options; //Array de tout les options du select
	var notGoodValue = "#";
	if(!valeur){return false;}
	if(valeur == notGoodValue) //Check si la valeur de l'agence selectionné n'est pas vide
	{
		if(arrayOpt.length > 1)//Si oui on trouve la prochaine valeur valide
		{
			for (var loop = numOpt; loop < arrayOpt.length; loop++) 
			{
				if(selectBox.options[loop].value != notGoodValue)
				{
					selectBox.value = selectBox.options[loop].value;
					return true;
				}
			}
		}
		else
		{
			return false;
		}
	}
	return true;
}
function sortAssoc(array,bywhat,indextype)
{
	var keymisc = new Array();
	var sorted = new Array();
	if(!indextype){var indextype = "text";}
	for (var key in array)
		keymisc.push((array[key][bywhat]).toLowerCase()+"_"+key);//Concat the bywhat with the array key
	keymisc.sort();//Sort the bywhat value array
	for (var x=0;x<keymisc.length;x++)//After this recreate the new sorted array
	{
		var tempArray = keymisc[x].split("_");
		var piece2 = tempArray[1];
		var index = (indextype=="text"?piece2:x);
		sorted[index] = new Array();
		sorted[index] = array[piece2];
	}
	return sorted;
}
function ucwords(string)
{
	return string.replace(/\w+/g, function(a){return a.charAt(0).toUpperCase() + a.substr(1).toLowerCase();});
}

//-->