var intPais = 0; 
var intDestino = 0; 
var intHotel = 0; 

//funciones que implementan la relacion entre los combos
function Control_onload()
{
	//cargar todos los combos
	RecargarCombo(document.getElementById(strNombreControl + "_ddlPais"), arrPais, "0", "0");
	intPais = 0;
	RecargarCombo(document.getElementById(strNombreControl + "_ddlDestino"), arrDestino, "0", "0");
	intDestino = 0;
	RecargarCombo(document.getElementById(strNombreControl + "_ddlHotel"), arrHotel, "0", "0");	
	intHotel = 0;
}

function Pais_onchange(_objCombo_Pais)
{	

	//si el pais seleccionado es 0 y diferente a variable global, guardar valor y recargar combos
	var strPais_Seleccionado = _objCombo_Pais.options[_objCombo_Pais.selectedIndex].value.split("#")[1];
	if ((strPais_Seleccionado== "0")&&(intPais!=0))
	{
		intPais=0;
		Control_onload();
	}
	//si no
	else
	{
		//si el pais seleccionado no es el mismo 
		//que ya habia en la variable global
		if (strPais_Seleccionado!=intPais.toString())
		{
			//guardamos el pais seleccionado en la variable global
			intPais = parseInt(strPais_Seleccionado); 
			//recargar el combo de destino
			RecargarCombo(document.getElementById(strNombreControl + "_ddlDestino"), arrDestino, "2", strPais_Seleccionado );
			//recargar el combo de hotel
			RecargarCombo(document.getElementById(strNombreControl + "_ddlHotel"), arrHotel, "2", strPais_Seleccionado );
		}
	}
	
}
function Destino_onchange(_objCombo_Destino)
{
	
	//si el destino seleccionado es 0 y diferente a variable global, guardar valor y recargar combos
	var strDestino_Seleccionado = _objCombo_Destino.options[_objCombo_Destino.selectedIndex].value.split("#")[1];
	if ((strDestino_Seleccionado == "0")&&(intDestino!=0))
	{
		intDestino=0;
		Control_onload();
	}
	//si no
	else
	{
		
		//si el destino seleccionado no es el mismo 
		//que ya habia en la variable global 
		if (strDestino_Seleccionado!=intDestino.toString())
		{
			
			//guardamos el destino seleccionado en su variable global
			intDestino = parseInt(strDestino_Seleccionado ); 
			//recargar el combo de hotel
			RecargarCombo(document.getElementById(strNombreControl + "_ddlHotel"), arrHotel, "3", strDestino_Seleccionado);
			//actualizar el combo de pais
			CambiarComboSuperior(_objCombo_Destino, arrPais, "intPais" , intPais, "ddlPais" , 2);
			
		}
	}				
}
function Hotel_onchange(_objCombo_Hotel)
{
	//guardar valor de hotel
	var strHotel_Seleccionado = _objCombo_Hotel.options[_objCombo_Hotel.selectedIndex].value.split("#")[1];
	//si el hotel seleccionado es 0 y diferente a variable global, recargar combos
	if ((strHotel_Seleccionado   == "0")&&(intHotel!=0))
	{
		intHotel=0;
		Control_onload();
	}
	//si no 
	else
	{	
		//guardamos el hotel seleccionado en su variable global
		intHotel = parseInt(strHotel_Seleccionado);  
		//actualizar el combo de pais
		CambiarComboSuperior(_objCombo_Hotel, arrPais, "intPais" , intPais, "ddlPais" , 2);
		//actualizar el combo de destino
		CambiarComboSuperior(_objCombo_Hotel, arrDestino, "intDestino" , intDestino, "ddlDestino", 3);
		
				
			
	 }
}

function CambiarComboSuperior(_objcombo_actual, _array_superior, _strnombre_variable_global, _intvalor_variable_global, 
								_strnombre_combo_superior, _intposicion_busqueda_seleccion)
{
	//si el combo superior no es el mismo que habia
	var strSuperior_Seleccionado = _objcombo_actual.options[_objcombo_actual.selectedIndex].value.split("#")[0];
	strSuperior_Seleccionado  = strSuperior_Seleccionado .split("|")[_intposicion_busqueda_seleccion];
		
	if (_intvalor_variable_global!=parseInt(strSuperior_Seleccionado  ))
	{
			//guardar variable de combo superior en variable global correspondiente
			eval(_strnombre_variable_global + "=parseInt(strSuperior_Seleccionado)");
			//seleccionar el combo superior
			for(var x=0;x<_array_superior.length-1;x++)
			{	
				objElementoCombo = document.getElementById(strNombreControl + "_" +_strnombre_combo_superior ).options[x]
				//si existe el elemento
				if (objElementoCombo)
				{
					if (objElementoCombo.value.split("#")[1] == strSuperior_Seleccionado  )
					{
						objElementoCombo.selected = true;
					}
		
				}
			}		
	}
}
								
function RecargarCombo(_combo, _array, _strPosicionSeleccion, _strValorSeleccion)
{
	var x;
	var strElemento_Seleccion;
	var strTexto;
	var strValor;
	var arrayAux = new Array();
	var i = 0;
	//limpiar combo llamando a la funcion LimpiarCombo
	LimpiarCombo(_combo)
	//hacer un array a partir del array original
	for(var x=1;x<_array.length;x++)
	{	
		//si existe el elemento
		if (_array[x])
		{
			strTexto = _array[x].split("|")[0];
			//el valor de cada elemento del combo será
			//el que se pone por c# concatenado con su codigo
			strValor = _array[x] +"#" + x.toString();
			arrayAux[i] = strValor
			i = i + 1;
		}
	}
	arrayAux = arrayAux.sort(compare);
	strValor = _array[0];
	arrauAux = arrayAux.unshift(strValor + "#0");  
	
	//ordenar el nuevo array
	//para cada elemento del array
	for(var x=0;x<arrayAux.length;x++)
	{	
		//si existe el elemento
		if (arrayAux[x])
		{
			//si cumple filtro o si el filtro es 0 o si el elemento es 0
			strElemento_Seleccion = arrayAux[x].split("|")[parseInt(_strPosicionSeleccion)];
			strElemento_Seleccion = strElemento_Seleccion.split("#")[0]; 
			
			if(strElemento_Seleccion==_strValorSeleccion || strElemento_Seleccion=="0" //caso de 1er elemento
													|| _strValorSeleccion=="0" ) //caso en el que se quieren cargar todos
			   {
			   	//añadir a los elementos del combo con la funcion AnyadirOpcion
			   	strTexto = arrayAux[x].split("|")[0];
			   	strValor = arrayAux[x];
			   	AnyadirOpcion(strValor, strTexto, _combo);
				}
		}
	}  
	//reiniciar valor de la variable del combo y seleccionar el valor 0 en el combo
	_combo.selectedIndex = 0;
	
}

//MNG 20061810 A-1137 Firefox
/*
function AnyadirOpcion(_value, _text, _control)
{
	var NewOption = document.createElement("OPTION");
	NewOption.value = _value;
	NewOption.text = _text;
	_control.add(NewOption);
	NewOption = null;
}*/
function AnyadirOpcion(_value, _text, _control)
{
	var where = (document.all) ? -1 : null;
	var NewOption = document.createElement("OPTION");
	NewOption.value = _value;
	NewOption.text = _text;
	_control.add(NewOption, where);
	NewOption = null;
}

function LimpiarCombo(_Combo)
{
	for ( var i = _Combo.length; i > 0; i-- )
	{
		_Combo.remove(0);
	}
}
//FIN funciones que implementan la relacion entre los combos

//*********************************************************************************************
//*********************************************************************************************

//recuperar estados previos: desde c# se informan las intVariable_previo
function Estado_Previo()
{
	Estado_Combos();
	//Estado_Calendarios(); 
}

function Estado_Combos()
{
	
	InformarValoresPrevios();
	
	if (intPais_previo != 0)
	{ 
		SeleccionarValoresCombos(arrPais, "ddlPais", intPais_previo);
		Pais_onchange(document.getElementById(strNombreControl + "_ddlPais"));
	}
	if (intDestino_previo != 0)
	{ 
		SeleccionarValoresCombos(arrDestino, "ddlDestino", intDestino_previo);
		Destino_onchange(document.getElementById(strNombreControl + "_ddlDestino"));
	}
	
	if (intHotel_previo != 0)
	{
		SeleccionarValoresCombos(arrHotel, "ddlHotel", intHotel_previo);
		Hotel_onchange(document.getElementById(strNombreControl + "_ddlHotel"));
	}
			
}

function SeleccionarValoresCombos( _array, _strnombre_combo, _id_seleccionado)	
{	
	for(var x=0;x<_array.length-1;x++)
	{	
		objElementoCombo = document.getElementById(strNombreControl + "_" +_strnombre_combo).options[x];
		//si existe el elemento
		if (objElementoCombo)
		{
			if (objElementoCombo.value.split("#")[1] == _id_seleccionado  )
			{
				objElementoCombo.selected = true;
			}
		}
	}		
}

//*********************************************************************************************
//*********************************************************************************************

//validaciones

function Validar(_mensaje_destino)
{
	strMensaje = "";
	var arrMensajes = _mensaje_destino.split('|');
	
	var bolHayError = false;
	
	objCombo_Destino = document.getElementById(strNombreControl + "_ddlDestino")
	var strDestino_Seleccionado = objCombo_Destino.options[objCombo_Destino.selectedIndex].value.split("#")[1];
	//si no se ha seleccionado destino...
	if (strDestino_Seleccionado == "0")
	{
		strMensaje = strMensaje + "- " + arrMensajes[0] + "\n";
		bolHayError = true;
	}
	//MNG 20060215 Limitamos la busqueda temporalmente
	objCombo_Hotel = document.getElementById(strNombreControl + "_ddlHotel")
	var strHotel_Seleccionado = objCombo_Hotel.selectedIndex;
	//si no se ha seleccionado HOTEL...
	//JAB 20060929 elimino la validacion que obliga a informar el hotel
	//if (strHotel_Seleccionado <= 0)
	//{
	//	strMensaje = strMensaje + "- " + arrMensajes[1] + "\n";
	//	bolHayError = true;
	//}
	//FIN JAB 20060929
	
	//objCombo_TipoOferta = document.getElementById(strNombreControl + "_ddlTipoOferta")
	//var strTipoOferta_Seleccionado = objCombo_TipoOferta.selectedIndex;
	//si no se ha seleccionado destino...
	//if (strTipoOferta_Seleccionado <= 0)
	//{
	//	strMensaje = strMensaje + "- " + arrMensajes[2] + "\n";
	//	bolHayError = true;
	//}
	
	if (bolHayError)
	{
		window.alert(strMensaje);
		Page_BlockSubmit = true;
		return false;		
	}
	else
	{
		return true;
	}

}

function Redireccionar(_mensaje_destino, _strHost, _strCultura, clickmapOmniture)
{
	var bValidacionEsCorrecta = Validar(_mensaje_destino);
	if (bValidacionEsCorrecta)
	{
		var strURLRedireccion; 
		
		s_objectID= clickmapOmniture;
		
		strURLRedireccion = "http://" + _strHost + "/BarceloHotels/";
		strURLRedireccion += _strCultura + "/Offers/"; 
		strURLRedireccion += "ListaPromocionesOfertas?hotel="+ intHotel.toString()  +"&destino="+ intDestino.toString() +"&tipooferta="+ document.getElementById(strNombreControl + "_ddlTipoOferta").value; 

		Page_BlockSubmit = true;
		window.location.href = strURLRedireccion;
		return false;
	}
	return false;		
}


/*
function RedireccionarASeleccHotel(_mensaje_destino, _mensaje_fechas, _bFechasObligatorias, _mensaje_fechas_iguales, _mensaje_fecha_en_blanco, _strHost, _strCultura)
{
	var bValidacionEsCorrecta = Validar(_mensaje_destino, _mensaje_fechas, _bFechasObligatorias, _mensaje_fechas_iguales, _mensaje_fecha_en_blanco);
	if (bValidacionEsCorrecta)
	{
		var strURLRedireccion; 
		
			var arrFormato = strgFormato.split(strSeparador);
			var strFechaEntrada = PasarFechaParametro( document.getElementById(strNombreControl + ":dtpFechaEntrada:Fecha").value, strSeparador, arrFormato); 
			var strFechaSalida = PasarFechaParametro( document.getElementById(strNombreControl + ":dtpFechaSalida:Fecha").value, strSeparador, arrFormato); 
			
			strURLRedireccion = "http://" + _strHost + "/BarceloHotels/";
			strURLRedireccion += _strCultura + "/Reservations/"; 
			strURLRedireccion += "SeleccionHotel?hotel="+ intHotel.toString()  +"&destino="+ intDestino.toString() 
			strURLRedireccion += "&checkin=" + strFechaEntrada	+"&checkout="+ strFechaSalida;
	
			Page_BlockSubmit = true;
			window.location.href = strURLRedireccion;
					
	}		
}
*/

//20050206 JAB funciones para comparar independientemente de los acentos
function compare(a, b) {
if (EliminaTilde(a) < EliminaTilde(b))
{
return -1;
}
if (EliminaTilde(a) > EliminaTilde(b))
{
return 1;
}

return 0;
}


function EliminaTilde(str){
 ret=str.replace(String.fromCharCode(225),"a");
 ret=ret.replace(String.fromCharCode(233),"e");
 ret=ret.replace(String.fromCharCode(237),"i");
 ret=ret.replace(String.fromCharCode(243),"o");
 ret=ret.replace(String.fromCharCode(250),"u");
 ret=ret.replace(String.fromCharCode(224),"a");
 ret=ret.replace(String.fromCharCode(232),"e");
 ret=ret.replace(String.fromCharCode(236),"i");
 ret=ret.replace(String.fromCharCode(242),"o");
 ret=ret.replace(String.fromCharCode(249),"u");
 return ret;
}
