
var l_viajeSinCapacidad = 'Este viaje no tiene capacidad disponible';
var l_noReturnConfirm = '&iquest;Esta seguro que desea seleccionar un viaje SIN REGRESO?';
var l_openConfirm = '&iquest;Esta seguro que desea seleccionar un viaje ABIERTO a 1 a\u00f1o?';
var l_fechaMayor = "La fecha ingresada debe ser mayor a hoy";
var l_fechaNoValida = "La fecha ingresada no es una fecha v&aacute;lida";
var l_selecViaje = "Debe seleccionar el viaje a realizar";
var l_distintaRow = "La bodega y los pasajes deben ser del mismo viaje";
var haySeleccionadoPax = 0;
var haySeleccionadoBod = 0;

$(document).ready(function(){
	//se comenta para que se pueda mostrar el datepicker.
	//Esto no se utiliza porque ya no existe el boton de boton de compra avanzada
	//armaDestinos();  
	
	//DATEPICKER
	$("#campoFecha").datepicker({
		   showOn: 'button',
		   buttonImage: imgPath+'calend.png',
		   buttonImageOnly: true,		   
		   numberOfMonths: 2,
		   onSelect: function(textoFecha, objDatepicker){
		   poneFecha(textoFecha, true);
		   realizaBusqueda();
		   },
		   maxDate:'+11m', 
		   minDate: new Date(),
		   defaultDate: $("[name='parDia']").val()+"/"+$("[name='parMes']").val(),
		   stepMonths: 2
		});
	
	$("[src$='calend.png']").attr("title","Calendario");
	$("img[src$='calend.png']").css("cursor","pointer");
});

function poneFecha(parFecha, esIda){
	dia = parFecha.substring(0, parFecha.indexOf('/'));
	mesAnio = parFecha.substring(parFecha.indexOf('/')+1);	 
	$("[name='parDia']").attr("value",dia);
	$("[name='parMes']").attr("value",mesAnio);	
}

function armaDestinos(){
	var cdadOrigenSeleccionada = $("[name='parOrigen']").val(); //obtengo la ciudad de origen seleccionada
	var comboDestino = document.formVisible.parDestino;
	
	var ciudadDestinos = new Array();
	ciudadDestinos = ciudadesDestinos[cdadOrigenSeleccionada];  //obtengo el array correspondiente, segun la ciudad origen elegida
	
	$("[name = '"+comboDestino.name+"']>option").remove(); //remuevo todas las opciones del comboDestino correcto
	for(i = 0; i < ciudadDestinos.length; i++){
		vYT = ciudadDestinos[i]; //esta variable tiene el valor y el texto de las posibles opciones destino 
		opcion = new Option(vYT.substring(vYT.indexOf(",")+1), vYT.substring(0 ,vYT.indexOf(","))); //creo las opciones
		comboDestino.options[i] = opcion; //coloco las opciones en su comboDestino
	}
	$("[name='parDestino']>option[value='"+ciudadDestinos[0]+"']").attr("selected","true"); //selecciono el 1º destino
	$("[name='parDestino']>option[value='"+ciudadDestinoSeleccionada+"']").attr("selected","true"); //si es posible elijo el destino q ya estaba seleccionado
}

function viajeSinCapacidad(){
	alert(l_viajeSinCapacidad);
}

function cambiaColorFondo(theTr,color){
	theTr.style.backgroundColor = color;
}

function realizaBusqueda(esOrigen){
	if (validaFecha()){
		if(esOrigen)
			armaDestinos();
		document.formVisible.submit();
	}
}

function cambiarAPromocion(datosPromo){

	var datos = datosPromo.split(",");
	
	/*for (var i=0;i<datos.length;++i){
		alert(datos[i]);
	}*/
	
	if (datos.length == 6){

		document.formSend.actualizaCabecera.value = "true";
		document.formSend.codPaquete.value = datos[0];
		document.formSend.codPromocion.value = datos[1];
		document.formSend.codViaje.value = datos[2];				
		document.formSend.codServicio.value = datos[3];
		document.formSend.codViajeVuelta.value = datos[4];
		document.formSend.codServicioVuelta.value = datos[5];	
		document.formSend.submit();		
	}	
}

function elijeViaje(){
	
	var noHayViaje = (document.formSend.datosPax.value == null);
	
	var paxBod = (haySeleccionadoPax != 0);
	
	if(paxBod && (haySeleccionadoBod != 0))
		paxBod = (haySeleccionadoBod == haySeleccionadoPax);
	
	if (noHayViaje ) alert(l_selecViaje);
	else if (!paxBod) alert(l_distintaRow);
	else document.formSend.submit();
}

function elijeServicio(radio){
	
	with(document.formSend){

		if(datosPax.value == radio.value || datosBod.value == radio.value){
			radio.checked = false;
			if(datosPax.value == radio.value){
				datosPax.value = null;
				haySeleccionadoPax = 0;
			}else{
				datosBod.value = null;
				haySeleccionadoBod = 0;
			}
		}else{
			cad = radio.value.split('_');
			
			if (radio.name == "viaje"){
					
				datosPax.value = radio.value;
				codViaje.value = cad[0];
				codServicio.value = cad[1];
				codPromocionPax.value = cad[2];
				nroColumnaPax.value = cad[3];
				codPaquete.value = cad[6];
				servicioRegreso.value = cad[7];
				if(!(document.formSend.parRecurso == undefined || parRecurso == null))
					parRecurso.value = cad[4];

				haySeleccionadoPax = radio.parentNode.parentNode.rowIndex-1;
				
				for (var i = 0; i < document.formSend.elements.length; i++){
					if(elements[i].type == 'hidden' && elements[i].name.indexOf('PAX_P') > -1){
						nombre = elements[i].name.split('_');
						elements[i].name = nombre[0]+'_'+nombre[1]+'_'+cad[4]+'_'+nombre[3]; //nombre es 'PAX_P_TU_MA'
					}
				}				
			} else if(radio.name == "bodega"){
				
				datosBod.value = radio.value;
				codPromocionBod.value = cad[2];
				nroColumnaBod.value = cad[3];
				
				for (var i = 0; i < document.formSend.elements.length; i++){
					if(elements[i].type == 'hidden' && elements[i].name.indexOf('_'+cad[4]) > -1){
						if(elements[i].name.indexOf('_'+cad[5]) > -1){
							elements[i].value = '1'; //nombre es 'PAX_B_BO_BM'		
							haySeleccionadoBod = radio.parentNode.parentNode.rowIndex-1;
						}else elements[i].value = '0';
					}						
				}		
			} 
		}
	}
}

function elijeServicioIV(codViaje,codServicio,codViajeVuelta,codServicioVuelta){
	document.formSend.codViaje.value = codViaje;
	document.formSend.codServicio.value = codServicio;
	document.formSend.codViajeVuelta.value = codViajeVuelta;
	document.formSend.codServicioVuelta.value = codServicioVuelta;	
	document.formSend.submit();
}

function validaFecha(){

	var theForm = document.formVisible;

	var diasMeseses = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	
	var mesanio = theForm.parMes.value.split("/");
	
	var dia = parseInt(theForm.parDia.value,10);
	var mes = parseInt(mesanio[0],10)-1;
	var anio = parseInt(mesanio[1],10);
	
	//Me fijo si es anio biciesto
	if (((anio%400) == 0) || (((anio%100) !=0) && ((anio%4) == 0)))
		diasMeseses[1]=29;
	else
		diasMeseses[1]=28;		
	
	if (dia > diasMeseses[mes]){
		alert(l_fechaNoValida);
		return false;
	}
		
	var dt = new Date(anio,mes,dia,0,0,0,0);
	
	var hoy = new Date();
	var dateRef = new Date(hoy.getYear(),hoy.getMonth(),hoy.getDate(),0,0,0,0);
	
	var difTime = dt.getTime() - dateRef.getTime();
	
	if (difTime < 0){
		alert(l_fechaMayor);
		return false;
	}
	
	return true;	
}


function muestraViajeDivs(tipoMuestra){

	var divTitViajes = document.getElementById('divTitViajes');
	var divDetViajes = document.getElementById('divDetViajes');
	var divViajes = document.getElementById('divViajes');
	var divFiltros = document.getElementById('divFiltros');

	if (tipoMuestra){
		divTitViajes.style.position = "relative";
		divFiltros.style.position = "relative";
		divDetViajes.style.position = "relative";	
		divViajes.style.position = "relative";				
		alert(l_selecViaje);		
		divTitViajes.style.visibility = "visible";	
		divFiltros.style.visibility = "visible";
		divDetViajes.style.visibility = "visible";
		divViajes.style.visibility = "visible";				
	}
	else{
	
		divTitViajes.style.position = "absolute";
		divFiltros.style.position = "absolute";
		divDetViajes.style.position = "absolute";	
		divViajes.style.position = "absolute";				
		divTitViajes.style.visibility = "hidden";	
		divFiltros.style.visibility = "hidden";
		divDetViajes.style.visibility = "hidden";
		divViajes.style.visibility = "hidden";		
	}
}

function setearRegreso(codViaje){

	document.formSend.codViaje.value = codViaje;
	document.formSend.codServicio.value = 0;
	
	haySeleccionadoPax = 1;
	elijeViaje();	
}

function viajeAbierto(){
	if (confirm(l_openConfirm)){
		setearRegreso("-1");
	}
	//muestraViajeDivs(false);
}

function viajeCerrado(){
	muestraViajeDivs(true);
}

function viajeSinRegreso(){

	if (confirm(l_noReturnConfirm)){
		setearRegreso("0");
	}
	//muestraViajeDivs(false);
}
