
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;

function viajeSinCapacidad(){
	alert(l_viajeSinCapacidad);
}

function cambiaColorFondo(theTr,color){
	theTr.style.backgroundColor = color;
}


function realizaBusqueda(){
	if (validaFecha()){
		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);
}


/*-----------------------------------------------------------------------*/

// This function gets called when the end-user clicks on some date.
function selected(cal, date) {

var theForm = document.formVisible;

  var selY = cal.date.getFullYear();
  var selM = cal.date.getMonth()+1; 
  var selD = cal.date.getDate();

	selD = (parseInt(selD,10) < 10 ? "0"+selD : selD );
	selM = (parseInt(selM,10) < 10 ? "0"+selM : selM );


	theForm.parDia.value = selD;
	theForm.parMes.value = selM+"/"+selY;

	cal.callCloseHandler();
	realizaBusqueda();
}

function obtAnioSelect(selectValue){
	return selectValue.substring(selectValue.indexOf("/")+1,selectValue.length);
}

function obtMesSelect(selectValue){
	return selectValue.substring(0,selectValue.indexOf("/"));
}

/*-----------------------------------------------------------------------*/

// And this gets called when the end-user clicks on the _selected_ date,
// or clicks on the "Close" button.  It just hides the calendar without
// destroying it.
function closeHandler(cal) {

  cal.hide();                        // hide the calendar
}

var calendar = null;
var diaInicio;
var mesInicio;
var anioInicio;
var diaLimite;
var mesLimite;
var anioLimite;


// This function shows the calendar under the element having the given id.
// It takes care of catching "mousedown" signals on document and hiding the
// calendar if the click was outside.
function showCalendar(id, format) {

	if (!validaFecha())
		return;

	var theForm = document.formVisible;
	
	var anioInicio = obtAnioSelect(theForm.parMes.options[0].value);
	var anioLimite = obtAnioSelect(theForm.parMes.options[theForm.parMes.length-1].value);
	var mesInicio = obtMesSelect(theForm.parMes.options[0].value);
	var mesLimite = obtMesSelect(theForm.parMes.options[theForm.parMes.length-1].value);	
	var diaInicio = 1;
	var diaLimite = 31;	
		
	
  if (calendar != null) {
    // we already have some calendar created
    calendar.hide();                 // so we hide it first.
  } else {
    // first-time call, create the calendar.
    var cal = new Calendar(false, null, selected, closeHandler);

    cal.weekNumbers = false;

    calendar = cal;                  // remember it in the global var

	cal.setRange(anioInicio, anioLimite);        // min/max year allowed.
	cal.setDisabledHandler(dateStatusHandler);
    cal.create();
  }


	var diaActual = theForm.parDia.value+"/"+theForm.parMes.value;
	var formatoFecha = "dd/mm/y";



  calendar.setDateFormat(formatoFecha);    // set the specified date format
  calendar.parseDate(diaActual);      // try to parse the text in field


  calendar.sel = theForm.fechaCalendario;                 // inform it what input field we use
  calendar.showAtElement(theForm.fechaCalendario);        // show the calendar below it

  return false;
}

/*-----------------------------------------------------------------------*/

var MINUTE = 60 * 1000;
var HOUR = 60 * MINUTE;
var DAY = 24 * HOUR;
var WEEK = 7 * DAY;

// If this handler returns true then the "date" given as
// parameter will be disabled.  In this example we enable
// only days within a range of 10 days from the current
// date.
// You can use the functions date.getFullYear() -- returns the year
// as 4 digit number, date.getMonth() -- returns the month as 0..11,
// and date.getDate() -- returns the date of the month as 1..31, to
// make heavy calculations here.  However, beware that this function
// should be very fast, as it is called for each day in a month when
// the calendar is (re)constructed.
function isDisabled(date) {
	return false;
  /*var today = new Date();
  return (Math.abs(date.getTime() - today.getTime()) / DAY) > 10;*/
}

/*-----------------------------------------------------------------------*/

/*
function flatSelected(cal, date) {
  var el = document.getElementById("preview");
  el.innerHTML = date;
}*/

/*-----------------------------------------------------------------------*/

/*function showFlatCalendar() {
  var parent = document.getElementById("display");

  // construct a calendar giving only the "selected" handler.
  var cal = new Calendar(false, null, flatSelected);
  

  // hide week numbers
  cal.weekNumbers = false;

  // We want some dates to be disabled; see function isDisabled above
  cal.setDisabledHandler(isDisabled);
  cal.setDateFormat("DD, M d");

  // this call must be the last as it might use data initialized above; if
  // we specify a parent, as opposite to the "showCalendar" function above,
  // then we create a flat calendar -- not popup.  Hidden, though, but...
  cal.setDisabledHandler(dateStatusHandler);
  cal.create(parent);

  // ... we can show it here.
  cal.show();
}
*/
/*-----------------------------------------------------------------------*/

function dateStatusHandler(date) {

	/*var y = date.getFullYear();
	var m = date.getMonth() + 1;
	var d = date.getDate();*/


	/*if (  (y < anioInicio || y > anioLimite) 
			||
		  (y == anioInicio && m < mesInicio) || (y == anioLimite && m > mesLimite)
		  	||
		  (y == anioInicio && m == mesInicio && d < diaInicio) 
			||
		  (y == anioLimite && m == mesLimite && d > diaLimite))

		return true;
	else
		return false;*/
	return false;
}
