var theForm = null;
var lErrorMismaCiudades = "Las ciudades de origen y destino no pueden ser las mismas.";
var lErrorCiudades = "No hay viajes posibles para ir desde ##ORIGEN## hacia ##DESTINO##.\n\nLas ciudades deben ser de distintos paises.";
var lErrorCantPasajeros = "Por favor indique la cantidad de pasajeros que van a viajar.";
var lErrorCantBodegas = "La cantidad de bodegas no puede superar la cantidad de pasajeros mayores.";
var lErrorTipoBuque = "No hay viajes en ese tipo de buques hacia ##DESTINO##.";
var lErrorTipoFecha = "La fecha de la vuelta debe ser posterior a la de la ida.";
var lErrorIdaIgualAVuelta = "La ida y la vuelta deben ser de ciudades distintas.";
var calendarioDia = null;
var calendarioMes = null;

function RealizarCompra(parForm){
	theForm = parForm;
	
	var ciudadDestino = theForm.parCiudadDestino.value;
	var ciudadVueltaDestino = theForm.parCiudadVueltaDestino.value;
	
	//Chequeo que las ciudades no sean la misma
	if ((theForm.parCiudadOrigen.value == ciudadDestino) || ((theForm.parCiudadVueltaOrigen.disabled == false) && (theForm.parCiudadVueltaOrigen.value == ciudadVueltaDestino))){
		alert(lErrorMismaCiudades);
		return;
	}
	
	if (theForm.parCiudadVueltaOrigen.disabled == false){
		if ((theForm.parCiudadOrigen.value == theForm.parCiudadVueltaOrigen.value)  && (ciudadDestino == ciudadVueltaDestino)){
			alert(lErrorIdaIgualAVuelta);
			return;
		}
	}
	var ida = theForm.parMes.value.split("/");
	var vta = theForm.parMesV.value.split("/");
	
	if 	((theForm.parMesV.disabled == false) &&
		((ida[1] > vta[1]) || 
		((ida[1] == vta[1]) && (ida[0] > vta[0])) || 
		((ida[1] == vta[1]) && (ida[0] == vta[0]) && (theForm.parDia.value > theForm.parDiaV.value)))){
		alert(lErrorTipoFecha);
		return;
	}
	
	
	var paisOrigen = theForm.parCiudadOrigen.value.substring(0,1);
	var paisDestino = theForm.parCiudadDestino.value.substring(0,1);
	
	//Chequeo ahora que los paises sean distintos, sino, se asume que no hay pasajes
	if (paisOrigen == paisDestino){
		lErrorCiudades = lErrorCiudades.replace("##ORIGEN##",theForm.parCiudadOrigen.options[theForm.parCiudadOrigen.selectedIndex].text);
		lErrorCiudades = lErrorCiudades.replace("##DESTINO##",theForm.parCiudadDestino.options[theForm.parCiudadDestino.selectedIndex].text);
		alert(lErrorCiudades);
		return;
	}
	
	theForm.parOrigen.value = theForm.parCiudadOrigen.value.substring(1);
	theForm.parDestino.value = theForm.parCiudadDestino.value.substring(1);

	//Verifico que los paises sean distintos para la vuelta
	if (theForm.parCiudadVueltaOrigen.disabled == false){
		var paisOrigenVuelta = theForm.parCiudadVueltaOrigen.value.substring(0,1);
		var paisDestinoVuelta = theForm.parCiudadVueltaDestino.value.substring(0,1);
		
		//Chequeo ahora que los paises sean distintos, sino, se asume que no hay pasajes
		if (paisOrigenVuelta == paisDestinoVuelta){
			lErrorCiudades = lErrorCiudades.replace("##ORIGEN##",theForm.parCiudadVueltaOrigen.options[theForm.parCiudadVueltaOrigen.selectedIndex].text);
			lErrorCiudades = lErrorCiudades.replace("##DESTINO##",theForm.parCiudadVueltaDestino.options[theForm.parCiudadVueltaDestino.selectedIndex].text);
			alert(lErrorCiudades);
			return;
		}
		
		theForm.parVueltaOrigen.value = theForm.parCiudadVueltaOrigen.value.substring(1);
		theForm.parVueltaDestino.value = theForm.parCiudadVueltaDestino.value.substring(1);
	}
	
	
	var pasajeros = cuentaPasajerosPedidos();
	var bodegas = 0;//cuentaBodegasPedidas();

	if (pasajeros == 0){
		alert(lErrorCantPasajeros);
		return;
	}
	
	
	if (bodegas > parseInt(theForm.PAX_P_TU_MA.value,10)){
		alert(lErrorCantBodegas);
		return;
	}

/*
	var restricc = theForm.parBuque.value.split("#")[1];

	if (bodegas > 0 && restricc != "*" && restricc.indexOf(ciudadDestino) == -1){
		lErrorTipoBuque = lErrorTipoBuque.replace("##DESTINO##",theForm.parCiudadDestino.options[theForm.parCiudadDestino.selectedIndex].text);
		alert(lErrorTipoBuque);
		return;
	}
*/

	theForm.submit();



}

function bloqVuelta(parForm){
	theForm = parForm;
	theForm.parCiudadVueltaOrigen.disabled = true;
	theForm.parCiudadVueltaDestino.disabled = true;
	theForm.parDiaV.disabled = true;
	theForm.parMesV.disabled = true;
	document.getElementById('calFechaVuelta').style.visibility = 'hidden';
}
//la segunda parte se volvio medio inutil, es igual a bloqVuelta (mferro)
function showVuelta(bool, parForm){
	theForm = parForm;
/*	theForm.parCiudadVueltaOrigen.value = theForm.parCiudadDestino.value;
	theForm.parCiudadVueltaDestino.value = theForm.parCiudadOrigen.value;*/
	
	if (bool == 't'){
		theForm.parCiudadVueltaOrigen.disabled = false;
		theForm.parCiudadVueltaDestino.disabled = false;
		theForm.parDiaV.disabled = false;
		theForm.parMesV.disabled = false;
		document.getElementById('calFechaVuelta').style.visibility = 'visible';
	} else {
		theForm.parCiudadVueltaOrigen.disabled = true;
		theForm.parCiudadVueltaDestino.disabled = true;
		theForm.parDiaV.disabled = true;
		theForm.parMesV.disabled = true;
		document.getElementById('calFechaVuelta').style.visibility = 'hidden';
	}
}

function fechaIdaMayorAVta(parForm){
	theForm = parForm;
	
	var ida = theForm.parMes.value.split("/");
	var vta = theForm.parMesV.value.split("/");
	
	if 	((theForm.parMesV.disabled == false) &&
		((ida[1] > vta[1]) || 
		((ida[1] == vta[1]) && (ida[0] > vta[0])) || 
		((ida[1] == vta[1]) && (ida[0] == vta[0]) && (theForm.parDia.value > theForm.parDiaV.value)))){
		theForm.parDiaV.value = theForm.parDia.value;
		theForm.parMesV.value = theForm.parMes.value;
	}
	
}

function cambCiud(parForm,ida){
	theForm = parForm;
	if (theForm.parCiudadVueltaOrigen != null && ida == 1){
		theForm.parCiudadVueltaOrigen.value = theForm.parCiudadDestino.value;
		theForm.parCiudadVueltaDestino.value = theForm.parCiudadOrigen.value;
	}
}

function cuentaPasajerosPedidos(){
	return parseInt(theForm.PAX_P_TU_MA.value,10)+
		parseInt(theForm.PAX_P_TU_ME.value,10)+
		parseInt(theForm.PAX_P_TU_S1.value,10);
}

function cuentaBodegasPedidas(){
	return parseInt(theForm.PAX_B_BO_BM.value,10)+
		parseInt(theForm.PAX_B_BO_B4.value,10);
}

function setTipoVuelta(parInput){
	theForm = parInput.form;
	
	var valor = parInput.value;
	
	if (valor == "C"){
		theForm.parDiaVuelta.disabled = false;
		theForm.parMesVuelta.disabled = false;
		document.imgCalVta.style.visibility = "visible";
	}
	else{
		theForm.parDiaVuelta.disabled = true;
		theForm.parMesVuelta.disabled = true;
		document.imgCalVta.style.visibility = "hidden";
	}

}


var calendarioParaIda = true;

function mostrarCalendario(input) {
	showCalendar(input, 'dd/mm/yy');
}



// 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 );

	calendarioDia.value = selD;
	calendarioMes.value = selM+"/"+selY;

	
	cal.callCloseHandler();
	fechaIdaMayorAVta(theForm);
}

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.
// La llamada de la funcion incluye dia y mes solo para no tener q cambiar 
// la llamada, si existiese, en otros lugares (pero claramente no es necesario 
// enviarle parametros ya q lo primero q hace la funcion es setearlos)
function showCalendar(dia, mes) {
		
	var dia = document.getElementsByName(dia)[0];
	var mes = document.getElementsByName(mes)[0];
	calendarioDia = dia;
	calendarioMes = mes;
	var theForm = document.formVisible;
	
	var anioInicio = obtAnioSelect(mes.options[0].value);
	var anioLimite = obtAnioSelect(mes.options[mes.length-1].value);
	var mesInicio = obtMesSelect(mes.options[0].value);
	var mesLimite = obtMesSelect(mes.options[mes.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 = null;
	
	diaActual = dia.value+"/"+mes.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 = dia;                 // inform it what input field we use
	calendar.showAtElement(theForm.parCiudadDestino);        // 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;
}

function dateStatusHandler(date) {
	return false;
}
