﻿
	/*** ENVIAR BUSQUEDA ***/

function EnviarBusqueda()
{
//		document.getElementById("divAltaBusqueda").innerHTML = '<img src="img/procesando.gif" width="16" height="16">' ;
		document.getElementById("btnEnviar").disabled = true ;
	
		xajax_EnviarBusqueda( xajax.getFormValues('frmBusqueda') , GetAgenciasSeleccionadas() );
}

function evaluaEnviarBusqueda( resultado )
{
	if( resultado == "" )
	{
		//alert( "La Búsqueda ha sido enviada exitosamente" ) ;
		//Sexy.info('<h1>La búsqueda ha sido enviada exitosamente</h1>' ) ;

		Sexy.info('<h1>La búsqueda ha sido enviada exitosamente</h1>',
				   {
				   onComplete: function()
				   {
						window.location = "index.php" ;
				   }
				 }		
			) ;
		
		
		//window.location = "index.php" ;
	}
	else
	{
		Sexy.error('<h1>Se han encontrado errores al procesar el formulario</h1><p>'+resultado+'</p>');
		//document.getElementById("divAltaBusqueda").innerHTML = resultado ;
		document.getElementById("btnEnviar").disabled = false ;
	}

	
}

/*** ELIMINAR BUSQUEDA ***/

function EliminarBusqueda( idBusqueda )
{
	
	Sexy.confirm('<h1>Esta seguro que desea eliminar la Busqueda ? <h1>', 
			{ 
				textBoxBtnOk: 'Si', 
				textBoxBtnCancel: 'No' ,
				onComplete: function(returnvalue) 
				{
					if (returnvalue) 
					{
						document.getElementById("imgElimina"+idBusqueda).src = "img/procesando.gif" ;
						xajax_EliminarBusqueda( idBusqueda );
					}
				}		
			});			

}

function evaluaEliminarBusqueda( resultado )
{
	if( resultado == "" )
	{
		Sexy.info('<h1>Exito</h1><p>Se ha eliminado exitosamente la Busqueda</p>',
				   {
					   onComplete: function()
					   {
							window.location.reload(false);
					   }
				   }		
			) ;		
	}
	else
	{
		Sexy.error('<h1>Errores</h1><p>'+resultado+'</p>',
				   {
					   onComplete: function()
					   {
							window.location.reload(false);
					   }
				   }		
			) ;
	}


}	


	/*** SELECTS ***/

/*** SELECTS ***/

//SELECCIONA PAIS TASACION
function SeleccionarPaisBusqueda( idPais )
{
	if( idPais == "" )
	{
		LimpiarCombo( 'lstAgenciasSeleccionadas' ) ;
		LimpiarCombo( 'lstAgenciasDisponibles' ) ;
	}
	else
	{
		document.getElementById('btnSeleccionarAgencia').disabled = true ;
		document.getElementById('btnDesSeleccionarAgencia').disabled = true ;
		document.getElementById('lstAgenciasDisponibles').disabled = true ;
		document.getElementById('lstAgenciasSeleccionadas').disabled = true ;
		
		xajax_SeleccionarPaisBusqueda( idPais ) ;
	}
}

function evaluaSeleccionarPaisBusqueda( resultado )
{	
	document.getElementById('btnSeleccionarAgencia').disabled = false ;
	document.getElementById('btnDesSeleccionarAgencia').disabled = false ;
	document.getElementById('lstAgenciasSeleccionadas').disabled = false ;
	document.getElementById('divAgenciasDisponibles').innerHTML = resultado ;
	LimpiarCombo( 'lstAgenciasSeleccionadas' ) ;
}	



	function ordenarSelect(elSelect)
	{
	    items=document.getElementById(elSelect).options;        // los originales
	    opciones=new Array();                                    // sera un doble array: con el texto, y con el valor
	    //ordenaremos 'opciones', despues sobrescribiremos todas las opciones originales con 'opciones' que ya estaran ordenadas
	    //rellenamos 'opciones'
	    for(a=0;a<items.length;a++)
	        opciones[a]=new Array( items[a].text, items[a].value);
	    opciones.sort();    // ordenamos 'opciones'. Se ordenara alfabeticamente opciones[0], y los cambios seran efectivos tambien para opciones[1]
	    for(a=0;a<items.length;a++) {        //sobrescribimos las opciones originales a las ordenadas
	        items[a].text=opciones[a][0];
	        items[a].value=opciones[a][1];
	    }
	}

	function volcarSelects(emisor, receptor)
	{

	    // Accedemos a los 2 selects
	    emisor = document.getElementById(emisor);
	    receptor = document.getElementById(receptor);
		var arrayRemover = new Array() ;

		for (i = 0; i < emisor.options.length; i++)
		{
			if( emisor.options[i].selected )
			{

				// Obtenemos algunos datos necesarios
				posicion = receptor.options.length;
			    selecionado = i;
		        volcado = emisor.options[i];
		        // Volcamos la opcion al select receptor y lo eliminamos del emisor
		        receptor.options[posicion] = new Option(volcado.text, volcado.value);

				arrayRemover.push( i ) ;
			}
		}

		arrayRemover.reverse() ;
		for (x in arrayRemover)
		{
			emisor.options[arrayRemover[x]] = null;
		}

	    ordenarSelect(document.getElementById("lstAgenciasDisponibles").id);
	    ordenarSelect(document.getElementById("lstAgenciasSeleccionadas").id);
	}


	/**
	 *
	 * @access public
	 * @return string con los id de los destinos seleccionados, separados por ","
	 **/
	function GetAgenciasSeleccionadas()
	{
		var agencias = "" ;

        for (i = 0; i < document.getElementById("lstAgenciasSeleccionadas").options.length; i++)
        {

			agencias = agencias + document.getElementById("lstAgenciasSeleccionadas").options[i].value + ";" ;
        }

		return( agencias ) ;
	}

	/**
	 *
	 * @access public
	 * @return void
	 **/
	function EliminarAgencia( )
	{
		var destinoSeleccionado = document.getElementById("lstAgenciasSeleccionadas").selectedIndex ;
		if( destinoSeleccionado	 >= 0 )
		{
			var idDestino = document.getElementById("lstAgenciasSeleccionadas").options[document.getElementById("lstAgenciasSeleccionadas").selectedIndex].value ;
			var nombre = document.getElementById("lstAgenciasSeleccionadas").options[document.getElementById("lstAgenciasSeleccionadas").selectedIndex].text ;
			volcarSelects('lstAgenciasSeleccionadas', 'lstAgenciasDisponibles');
			
	    ordenarSelect(document.getElementById("lstAgenciasDisponibles").id);
	    ordenarSelect(document.getElementById("lstAgenciasSeleccionadas").id);			
		}
	}

	// FUNCIONES GENERICAS
	function LimpiarCombo( comboID )
	{
	    if (document.getElementById(comboID) != null ) 
	    {
			if( document.getElementById(comboID).options.length>0 )
			{
				document.getElementById(comboID).options.length = 0
				document.getElementById(comboID).selectedIndex = 0;

			}
	    }
	}