<!--//
//document.write("V4");
/*/////////////////////////////////////////////////////////////////////////
// - search_box-v4.js // version 4.0
///////////////////////////////////////////////////////////////////////////
// - DESCRIPTION
//-------------------------------------------------------------------------
// Dynamic Search Input: Destinations, Hotels, Stars
///////////////////////////////////////////////////////////////////////////
// - DEPENDENCIES SITE : AVC, VOS, VC
// - DEPENDENCIES FILE : search_box_data-v4.php
///////////////////////////////////////////////////////////////////////////
// - HISTORY MODIFICATION
//-------------------------------------------------------------------------
// Benoit	16DEC06  Add comment
// Benoit	02FEV07  Add sb_lang for support different language
// Benoit   13AVR08  Add dest group
///////////////////////////////////////////////////////////////////////////
// - CONTACT INFO
//-------------------------------------------------------------------------
// Benoit Renaud (benoit at voyagesconstellation.com)
/////////////////////////////////////////////////////////////////////////*/

var arrayCountrydest = new Array();
var selectDestType = new Array(); // array by id width limited, all (string)
var lastOrigCode = new Array(); // 3 char (string)

function createSelectDest(targetSelectId, currentDestArray, selected, withAllDest, withGroupCountry, forHotelUse, multiple, origCode)
{
	//Variable initialisation
	var destArray = new Array();
	var arrayCountryAlreadyAdd = new Array();
	var tempCountrydest = new Array();
	var currentSelectDestType = null;
	var destArrayNotSet = true;
	var arrayAlldest = new Array();
	
	// The limitedDestArray is use for specific origne with limited destination list
	if((typeof(origCode) == "string") && (origCode.length == 3))
	{
		if(lastOrigCode[targetSelectId] == origCode) return true;
		lastOrigCode[targetSelectId] = origCode;
		if((arrayDestPoss[origCode]) && (typeof(arrayDestPoss[origCode]) == "object") && (arrayDestPoss[origCode].length > 0))
		{
			var tempDestLimited = getLimitedDest(currentDestArray, arrayDestPoss[origCode]);
			try 
			{ 
				destArray = sortAssoc(tempDestLimited,"PAYS"); 
				currentSelectDestType = "limited-"+origCode; 
				destArrayNotSet = false;
			} 
			catch(e)
			{  
				destArrayNotSet = true;
			};
		}
	}
	//if no limited dest, put all destination
	if(destArrayNotSet) 
	{
		destArray = sortAssoc(currentDestArray,"PAYS");
		currentSelectDestType = "all";
	}
	//If the select box type is the same, not proccess to the creation
	if(currentSelectDestType == selectDestType[targetSelectId]) return true;
	selectDestType[targetSelectId] = currentSelectDestType;
	
	//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+"_dest")) return false;
	var selectBox = document.getElementById(targetSelectId+"_dest"); //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
	arrayAlldest = new Array();
	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[targetSelectId] = tempCountrydest;}
	//Reset the hotel select box
	if (!destArrayNotSet && document.getElementById(targetSelectId+"_stars")) { createSelectHotel(targetSelectId+"_hot", currentDestArray, selectBox.options[0].value, document.getElementById(targetSelectId+"_stars").value); }
	return true;
}
function getLimitedDest(currentDestArray, dest_code_array)
{
	var arrayLimitedDest = new Array();
	for (var dest in dest_code_array)
	{
		var currentDest = dest_code_array[dest];
		if(typeof(currentDestArray[currentDest]) == "object")
		{
			if(currentDestArray[currentDest]["CODE"] && (currentDestArray[currentDest]["CODE"].length == 3))
			{
				arrayLimitedDest[currentDest] = new Array();
				arrayLimitedDest[currentDest]["NAME"] = currentDestArray[currentDest]["NAME"];
				arrayLimitedDest[currentDest]["PAYS"] = currentDestArray[currentDest]["PAYS"];
				arrayLimitedDest[currentDest]["CODE"] = currentDestArray[currentDest]["CODE"];
			}
		}
	}
	return arrayLimitedDest;
}
function getCountryByDest(targetSelectId, dest_array)
{
	//Loop on the country
	for (var country in arrayCountrydest[targetSelectId])
	{
		if(compare_array(dest_array, arrayCountrydest[targetSelectId][country]))
			return country;
	}
	return false;
}
function setDestByHotel(targetSelectId,hot_num,dest)
{
	var selectBox = document.getElementById(targetSelectId+"_dest"); //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(targetSelectId+"_dest").value != dest)
					document.getElementById(targetSelectId+"_dest").value = dest;
			}
		}//loop on arrayHotelSelect
	}
}
function createSelectHotel(targetSelectId, currentDestArray, dest, star, selected)
{
	/*if((currentDestArray) && (typeof(currentDestArray) == "object") && (currentDestArray.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(targetSelectId, tDestArray);//return country name or false
		}
		else{
			tDestArray = document.getElementById(targetSelectId+"_dest").options[0].value;
		}
		try { var sb_allhot = _gtext("sb_allhot"); } catch(e) { var sb_allhot = "Tous les hotels"; };		
		if(!document.getElementById(targetSelectId+"_hot")) return false;
		var selectBox = document.getElementById(targetSelectId+"_hot"); //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 "+currentDestArray[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 = currentDestArray[currentDest]["NAME"]+", "+currentDestArray[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(targetSelectId,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(targetSelectId+"_hot");
	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();});
}

//-->