var intPais = 0; 
var intDestino = 0; 
var intHotel = 0; 
var intIndiceDias, intIndiceMes, intDigDias, intDigMes, strSeparador;

//--------Funciones que implementan la relacion entre los combos
//Cargar todos los combos
function Control_onload()
{	
	RecargarCombo(document.getElementById("ddlPais"), arrPais, "0", "0");
	intPais = 0;
	RecargarCombo(document.getElementById("ddlDestino"), arrDestino, "0", "0");
	intDestino = 0;
	RecargarCombo(document.getElementById("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("ddlDestino"), arrDestino, "2", strPais_Seleccionado );
			//recargar el combo de hotel
			RecargarCombo(document.getElementById("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("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(_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();
	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]; 
			
			//strElemento_Seleccion=="0" (caso de 1er elemento)
			//_strValorSeleccion=="0" ) caso en el que se quieren cargar todos
			if(strElemento_Seleccion==_strValorSeleccion || strElemento_Seleccion=="0" 
													|| _strValorSeleccion=="0" ) 
			{
			   	//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 NewOption = document.createElement("OPTION");
	NewOption.value = _value;
	NewOption.text = _text;
	_control.add(NewOption);
	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("ddlPais"));
	}
	if (intDestino_previo != 0)
	{ 
		SeleccionarValoresCombos(arrDestino, "ddlDestino", intDestino_previo);
		Destino_onchange(document.getElementById("ddlDestino"));
	}
	
	if (intHotel_previo != 0)
	{
		SeleccionarValoresCombos(arrHotel, "ddlHotel", intHotel_previo);
		Hotel_onchange(document.getElementById("ddlHotel"));
	}
}

function SeleccionarValoresCombos( _array, _strnombre_combo, _id_seleccionado)	
{	
	for(var x=0;x<_array.length-1;x++)
	{	
		objElementoCombo = document.getElementById(_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 ValidarContacto(_mensaje_Min1Campo, _mensaje_LocalizYHotelOblig, _mensaje_InfoCamposUsuarioReg, _EsUsuarioRegistrado)
{ 
	strMensaje = "";
	objCombo_Destino = document.getElementById("ddlDestino");
	objCombo_Pais = document.getElementById("ddlPais");
	objCombo_Hotel = document.getElementById("ddlHotel");
	objLocalizador = document.getElementById("txtLocalizador");
	
	var strDestino_Seleccionado = objCombo_Destino.options[objCombo_Destino.selectedIndex].value.split("#")[1];
	var strPais_Seleccionado = objCombo_Pais.options[objCombo_Pais.selectedIndex].value.split("#")[1];
	var strHotel_Seleccionado = objCombo_Hotel.options[objCombo_Hotel.selectedIndex].value.split("#")[1];
	var strLocalizador = objLocalizador.value;
	var bolMinUnaFecha = MinimoUnaFecha();
	var bolParesFechas = true;
	var bolHayError = false;
			
	//Validaciones para Usuario Registrado			
	if (_EsUsuarioRegistrado == "True")
	{	
		if  (strLocalizador == "")
		{
			if (strHotel_Seleccionado != "0") 
			{
				if (bolMinUnaFecha)
				{
					strMensaje = "";				
				}
				else
				{
					strMensaje = _mensaje_InfoCamposUsuarioReg;		
					bolHayError = true;		
				}
			}
			else
			{
				strMensaje = _mensaje_InfoCamposUsuarioReg;		
				bolHayError = true;								
			}			
		}
		else 
		{	
			bolParesFechas = ValidacionParesFechas();
		}		
		
	}	
	else
	{ 	
		//Validaciones para Usuario NO Registrado
		if ( (strHotel_Seleccionado == "0") || (strLocalizador == "") )
		{  
			//Mensaje de que los 2 campos deben estar informados.
			strMensaje = _mensaje_LocalizYHotelOblig;		
			bolHayError = true;				
		}	
	}
		
	if ( (bolHayError)&&(bolParesFechas) )
	{
		window.alert(strMensaje);
		Page_BlockSubmit = true;	
	}	
	else if ( (!bolHayError)&&(bolParesFechas) )
	{
		Page_BlockSubmit = false;	
	}
		
}

function DesactivarCombosEMail(_bolDesactivacion)
{ 
	DesSeleccionarCombosCabecera();
	document.getElementById("ddlDestino").disabled = _bolDesactivacion;
	document.getElementById("ddlPais").disabled = _bolDesactivacion;
	document.getElementById("ddlHotel").disabled = _bolDesactivacion;
	document.getElementById("ddlDepartamento").disabled = !(_bolDesactivacion);
}

function DesSeleccionarCombosCabecera()
{ 
	document.getElementById("ddlDestino").selectedIndex = 0;
	document.getElementById("ddlPais").selectedIndex = 0;
	document.getElementById("ddlHotel").selectedIndex = 0;
	document.getElementById("ddlDepartamento").selectedIndex = 0;	
}

function LimpiarTodo()
{ 
	document.getElementById("Formulario").reset();
}

function ValidarCamposEnviar()
{
	if( (document.getElementById("rbEmailHot").checked == false) && (document.getElementById("rbEmailDpto").checked == false) )
	{		
		alert(document.getElementById("hFaltaDestinoEmail").value);
		Page_BlockSubmit = true;
	}
	else if ( (document.getElementById("rbComunicacion").checked == false) && (document.getElementById("rbSugerencia").checked == false) && (document.getElementById("rbQueja").checked == false) && (document.getElementById("rbFelicitacion").checked == false) )
	{
		alert(document.getElementById("hFaltaComunicacion").value);
		Page_BlockSubmit = true;	
	}
	else
	{	
		PonerRequiredsDatosMail(true);
		Page_ClientValidate();
		PonerRequiredsDatosMail(false);
		document.getElementById("hCodHotel").value = document.getElementById("ddlHotel").value;			
		
	}

}

function PonerRequiredsDatosMail(_estado)
{
				
		document.getElementById("rfvNombre").enabled = _estado;
		document.getElementById("rfvApellidos").enabled = _estado;
		document.getElementById("rfvEmail").enabled = _estado;
		document.getElementById("revEMail").enabled = _estado;
		
		if(document.getElementById("chkTelefono").checked == true)
		{
			document.getElementById("rfvTelefono").enabled = _estado;
		}
		
		document.getElementById("rfvDireccion").enabled = _estado;
		document.getElementById("rfvCiudad").enabled = _estado;
		document.getElementById("rfvCPostal").enabled = _estado;
		
		if ( (document.getElementById("ddlPaisDelHotel").value == "CA") || (document.getElementById("ddlPaisDelHotel").value == "US") )
		{
			document.getElementById("rfvEstado").enabled = _estado;
		}
		else
		{
			document.getElementById("rfvProvincia").enabled = _estado;
		}				
		
		document.getElementById("rfvPais").enabled = _estado;
		document.getElementById("rfvTexto").enabled = _estado;
		if (document.getElementById("rbEmailHot").checked == true)
		{
			document.getElementById("cvHotel").enabled = _estado;
		}
		else
		{
			document.getElementById("cvDepartamento").enabled = _estado;
		}	

}

//Controla el cambio del CheckBox Email
function CambioModoContactoEmail()
{		
	if( (document.getElementById("chkEmail").checked == true) && (document.getElementById("chkTelefono").checked == false) )
	{		
		document.getElementById("chkEmail").checked = true;
		document.getElementById("chkTelefono").checked = false;
		document.getElementById("AsteriscoTelf").style.display = "none";
			
	}
	else if( (document.getElementById("chkEmail").checked == false) && (document.getElementById("chkTelefono").checked == false) )
	{
		document.getElementById("chkEmail").checked = false;
		document.getElementById("chkTelefono").checked = true;
		document.getElementById("AsteriscoTelf").style.display = "inline";
	}
	else if( (document.getElementById("chkEmail").checked == true) && (document.getElementById("chkTelefono").checked == true) )	
	{
		document.getElementById("chkEmail").checked = true;
		document.getElementById("chkTelefono").checked = false;
		document.getElementById("AsteriscoTelf").style.display = "none";	
	}
	else if( (document.getElementById("chkEmail").checked == false) && (document.getElementById("chkTelefono").checked == true) )	
	{
		document.getElementById("chkEmail").checked = false;
		document.getElementById("chkTelefono").checked = true;
		document.getElementById("AsteriscoTelf").style.display = "inline";
	}
	
}

//Controla el cambio del CheckBox Telefono
function CambioModoContactoTelefono()
{
	if( (document.getElementById("chkTelefono").checked == true) && (document.getElementById("chkEmail").checked == false) )
	{		
		document.getElementById("chkTelefono").checked = true;
		document.getElementById("AsteriscoTelf").style.display = "inline";
		document.getElementById("chkEmail").checked = false;
	}
	else if ( (document.getElementById("chkTelefono").checked == false) && (document.getElementById("chkEmail").checked == false) )
	{
		document.getElementById("chkTelefono").checked = false;
		document.getElementById("AsteriscoTelf").style.display = "none";
		document.getElementById("chkEmail").checked = true;
	}
	else if ( (document.getElementById("chkTelefono").checked == true) && (document.getElementById("chkEmail").checked == true) )
	{
		document.getElementById("chkTelefono").checked = true;
		document.getElementById("AsteriscoTelf").style.display = "inline";
		document.getElementById("chkEmail").checked = false;	
	}
	else if ( (document.getElementById("chkTelefono").checked == false) && (document.getElementById("chkEmail").checked == true) )
	{
		document.getElementById("chkTelefono").checked = false;
		document.getElementById("AsteriscoTelf").style.display = "none";
		document.getElementById("chkEmail").checked = true;	
	}
}

function PaisOnChange()
{
	var Pais = document.getElementById("ddlPaisDelHotel").value;
	
	ControlaDropEstados(Pais);
	
	if ((Pais == "CA")||(Pais == "US"))
	{
		var stateList = document.getElementById("hEstados").value;
		var combo = document.getElementById("ddlEstado");
		ClearCombo(combo);
		var splits = stateList.split("#");
		var i = 0;
		var comparer = splits[i];
		// Se deja la posibilidad de no rellenar
		optNueva=document.createElement("OPTION");
		optNueva.value="";
		optNueva.text="";
		combo.add(optNueva);
		// Se rellena con los estados del país
		while (comparer != "")
		{
			var state = comparer.split("|");
			if (state[0] == Pais)
			{
				optNueva=document.createElement("OPTION");
				optNueva.value=state[1];
				optNueva.text=state[2];
				combo.add(optNueva);
			}
			i += 1;
			comparer = splits[i];
		}
	}
}

function ControlaDropEstados(_Pais)
{
	if ((_Pais == "CA")||(_Pais == "US"))
	{
		document.getElementById("ddlEstado").style.display = "inline";	
		document.getElementById("lblEstado").style.display = "inline";
		document.getElementById("txtProvincia").style.display = "none";
		document.getElementById("lblProvincia").style.display = "none";			
		
		
	}
	else
	{
		document.getElementById("ddlEstado").style.display = "none";
		document.getElementById("lblEstado").style.display = "none";	
		document.getElementById("txtProvincia").style.display = "inline";
		document.getElementById("lblProvincia").style.display = "inline";		
		
	}
}

function ClearCombo(_combo)
{
	for(x=_combo.length;x>0;x--)
	{
		_combo.remove(0);
	}
}