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;
}

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();
	}
	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();
	}
	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();
	}
	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;
}

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();
}

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;
		}
	}		
}

//FIN recuperar estados previos: desde c# se informan las intVariable_previo

//*********************************************************************************************

//Validaciones
function Validar(_mensaje_pais, _mensaje_participantes)
{
	strMensaje = "";

	objCombo_Pais = document.getElementById(strNombreControl + "_ddlPais")
	var strPais_Seleccionado = objCombo_Pais.options[objCombo_Pais.selectedIndex].value.split("#")[1];

	//si no se ha seleccionado pais...
	if ( strPais_Seleccionado == "0" )
		strMensaje = strMensaje + "- " + _mensaje_pais + "\n";

	objTextBox_Numero = document.getElementById(strNombreControl + "_txtNumero")
	var strParticipates = objTextBox_Numero.value;

	//si no se ha seleccionado el numero de participantes...
	if ( strParticipates == "" )
		strMensaje = strMensaje + "- " + _mensaje_participantes;

	if ( strMensaje != "" )
	{
		window.alert(strMensaje);
		Page_BlockSubmit = true;

		return false;	
	}
	else
	{
		return true;
	}
}

function Redireccionar(_mensaje_pais, _mensaje_participantes, _strHost, _strCultura)
{
	
	var bValidacionEsCorrecta = Validar(_mensaje_pais, _mensaje_participantes);

	if ( bValidacionEsCorrecta )
	{
		var strURLRedireccion;

		strURLRedireccion = "http://" + _strHost + "/reunionesyeventos/";
		strURLRedireccion += _strCultura + "/"; 
		strURLRedireccion += "ListaHoteles?hotel=" + intHotel.toString() + "&destino=" + intDestino.toString()+ "&pais=" + intPais.toString()
		+"&participantes=" + document.getElementById(strNombreControl + "_txtNumero").value;
			
		Page_BlockSubmit = true;
		window.location.href = strURLRedireccion;

		return false;
	}		
	
	Page_BlockSubmit = true;

	return false;
}

//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;
}