var intPais = 0; 
var intDestino = 0; 
var intHotel = 0; 

//funciones que implementan la relacion entre los combos
function Control_onload()
{	
	//20060228 JAB resetear el formulario
	document.forms[0].reset();
	//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;
	
	objSelectorFechaEntrada = document.getElementById(strNombreControl + "_dtpFechaEntrada_DateSelector");
	objSelectorFechaSalida = document.getElementById(strNombreControl + "_dtpFechaSalida_DateSelector");

	if (objSelectorFechaEntrada && objSelectorFechaSalida)
	{
		objSelectorFechaEntrada.value = "";
		objSelectorFechaSalida.value = "";
	}

}

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 );
		}
	}
	
	intDestino = 0;
	intHotel = 0;
}

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);
		}
	}			
	intHotel = 0;	
}

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 20061016 Cambios 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;
			}
		}
	}		
}

function Estado_Calendarios()
{
	var objFecha = document.getElementById(strNombreControl + "_dtpFechaEntrada_DateSelector");
	
	//Noches_onChange(strNombreControl);
	//Noches_onChange(strNombreControl);
	ControlaLogicaFechas(objFecha);

}
//FIN recuperar estados previos: desde c# se informan las intVariable_previo

//*********************************************************************************************
//*********************************************************************************************

//validaciones

function Validar(_mensaje_destino, _mensaje_fechas, _bFechasObligatorias, _mensaje_fechas_iguales, _mensaje_fecha_en_blanco)
{
	strMensaje = "";
	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 + "- " + _mensaje_destino + "\n";
	}
	//si no se ha seleccionado alguna fecha...

	//	objSelectorFechaEntrada = document.getElementById(strNombreControl + ":dtpFechaEntrada:Fecha")
	//  objSelectorFechaSalida = document.getElementById(strNombreControl + ":dtpFechaSalida:Fecha")
	
	objSelectorFechaEntrada = document.getElementById(strNombreControl + "_dtpFechaEntrada_DateSelector");
	objSelectorFechaSalida = document.getElementById(strNombreControl + "_dtpFechaSalida_DateSelector");

	if ( (objSelectorFechaEntrada.value == "" || objSelectorFechaSalida.value == "") && _bFechasObligatorias )
	{
		strMensaje = strMensaje + "- " + _mensaje_fechas;
	}	

	//alert(( (objSelectorFechaEntrada.value == objSelectorFechaSalida.value) && (objSelectorFechaSalida.value != "" && objSelectorFechaSalida.value != "undefined" ) ));
	//si las fechas son iguales...
	if ( (objSelectorFechaEntrada.value == objSelectorFechaSalida.value) && (objSelectorFechaSalida.value != "" && objSelectorFechaSalida.value != null ) )
	{
		strMensaje = strMensaje + "- " + _mensaje_fechas_iguales + "\n";
	}	
	//si se ha dejado una fecha en blanco...
	if (((objSelectorFechaEntrada.value == "" && objSelectorFechaSalida.value != "") || 
		(objSelectorFechaEntrada.value != "" && objSelectorFechaSalida.value == "")) &&
		(!_bFechasObligatorias))
	{
		strMensaje = strMensaje + "- " + _mensaje_fecha_en_blanco;
	}	
	
	if ( strMensaje != "" )
	{
		window.alert(strMensaje);
		Page_BlockSubmit = true;
		return false;	
	}
	else
	{
		return true;
	}
}

function Redireccionar(_mensaje_destino, _mensaje_fechas, _bFechasObligatorias, _mensaje_fechas_iguales, _mensaje_fecha_en_blanco, _strHost, _strCultura, clickmapOmniture)
{
	
	var bValidacionEsCorrecta = Validar(_mensaje_destino, _mensaje_fechas, _bFechasObligatorias, _mensaje_fechas_iguales, _mensaje_fecha_en_blanco);

	if ( bValidacionEsCorrecta )
	{
		var strURLRedireccion; 
		
		s_objectID= clickmapOmniture;
				
		//si se han selecionado fechas, llamar a SeleccionHotel con parametros
		if ( (document.getElementById(strNombreControl + "_dtpFechaEntrada_DateSelector").value != '' && document.getElementById(strNombreControl + "_dtpFechaEntrada_DateSelector").value != null  ) 
			&& (document.getElementById(strNombreControl + "_dtpFechaSalida_DateSelector").value != '' && document.getElementById(strNombreControl + "_dtpFechaSalida_DateSelector").value != null ))
		{
			var strFechaEntrada =document.getElementById(strNombreControl + "_dtpFechaEntrada_DateSelector").value; 
			var strFechaSalida =document.getElementById(strNombreControl + "_dtpFechaSalida_DateSelector").value;
			
			strURLRedireccion = "http://" + _strHost + "/BarceloHotels/";
			strURLRedireccion += _strCultura + "/Reservations/"; 
			strURLRedireccion += "SeleccionHotel?hotel="+ intHotel.toString()  +"&destino="+ intDestino.toString() 
			+"&checkin=" + strFechaEntrada 
			+"&checkout="+ strFechaSalida
			+"&origen=" + document.getElementById(strNombreControl + "_hdProcedencia").value;
			
			Page_BlockSubmit = true;
			window.location.href = strURLRedireccion;
			return false;
		}
		//si no se han seleccionado fechas, llamar a HoteleEnDestino con parametros
		else
		{
			strURLRedireccion = "http://" + _strHost + "/BarceloHotels/";
			strURLRedireccion += _strCultura + "/Reservations/"; 
			strURLRedireccion += "HotelesEnDestino?hotel=" + intHotel.toString() + "&destino=" + intDestino.toString(); 
			Page_BlockSubmit = true;
			window.location.href = strURLRedireccion;
			return false;
		}
	}		
	
	Page_BlockSubmit = true;
	return false;
}

function RedireccionarASeleccHotel(_mensaje_destino, _mensaje_fechas, _bFechasObligatorias, _mensaje_fechas_iguales, _mensaje_fecha_en_blanco, _strHost, _strCultura, _strCodigo_Tematica)
{
	var bValidacionEsCorrecta = Validar(_mensaje_destino, _mensaje_fechas, _bFechasObligatorias, _mensaje_fechas_iguales, _mensaje_fecha_en_blanco);
	
	if ( bValidacionEsCorrecta )
	{
		var strURLRedireccion; 
	
		/* JDR 20061018 - A1137 Mozilla
		var strFechaEntrada = PasarFechaParametro( document.getElementById(strNombreControl + ":dtpFechaEntrada:Fecha").value, strSeparador, arrFormato); 
		var strFechaSalida = PasarFechaParametro( document.getElementById(strNombreControl + ":dtpFechaSalida:Fecha").value, strSeparador, arrFormato); 
		*/
		
		var strFechaEntrada = document.getElementById(strNombreControl + "_dtpFechaEntrada_DateSelector").value; 
		var strFechaSalida = document.getElementById(strNombreControl + "_dtpFechaSalida_DateSelector").value; 
		
		
		strURLRedireccion = "http://" + _strHost + "/BarceloHotels/";
		strURLRedireccion += _strCultura + "/Reservations/"; 
		strURLRedireccion += "SeleccionHotel?hotel=" + intHotel.toString() + "&destino=" + intDestino.toString() 
		strURLRedireccion += "&checkin=" + strFechaEntrada + "&checkout=" + strFechaSalida;
		
		if ( _strCodigo_Tematica != "" )
		{
			strURLRedireccion += "&tematica=" + _strCodigo_Tematica;
		}
		
		strURLRedireccion += "&origen=" + document.getElementById(strNombreControl + "_hdProcedencia").value;
		
	
		Page_BlockSubmit = true;
		window.location.href = strURLRedireccion;		
		return false;
	}		
	
	Page_BlockSubmit = true;
	return false;
}

/*

function PasarFechaParametro(_strFecha, _strSeparador, _arrFormato)
{		
		var strFechaRespuesta = ObtenerDatoFecha(_arrFormato, _strFecha, "Y", _strSeparador); 
		strFechaRespuesta += ObtenerDatoFecha(_arrFormato, _strFecha, "M", _strSeparador); 
		strFechaRespuesta += ObtenerDatoFecha(_arrFormato, _strFecha, "D", _strSeparador); 
		return strFechaRespuesta;
}

function ObtenerDatoFecha(_arrFormato, _strFecha, _strCodigoDato, _strSeparador)
{
	for ( var i = 0; i < _arrFormato.length; i++ )
	{
		if ( _arrFormato[i].substr(0, 1).toUpperCase() == _strCodigoDato.toUpperCase() )
		{	
			return _strFecha.split(_strSeparador)[i].substr(0, _arrFormato[i].length);
		}
	}
}*/
//FIN validaciones

//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;
}