// Almacenar las fechas máximas de las ciudades.
// ---------------------------------------------------------

var fechaMaxGeneral = new Date(3007,11,24);
var fechaMaxPdelE = new Date(3007,11,24);
var fechaMaxLaPaloma = new Date(3007,11,24);
var fechaMaxPiria = new Date(3007,11,24);
var fechaMaxColonia = new Date(3007,11,24);
var fechaMaxRural = new Date(3007,11,24);
var fechaMaxMvdeo = new Date(3007,11,24);
var fechaMaxTermas = new Date(3007,11,24);
var fechaMaxOtros = new Date(3007,11,24);

var arrayMaxDiasMeses = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var yaHizoClick = false;

function dropMesColonia_onblur()
{
	if (document.formulario.dropMesDesdeColonia.value != "Not_Sel")
		document.formulario.checkBoxColonia.checked = true;	
}

function dropMesLaPaloma_onblur()
{
	if (document.formulario.dropMesDesdeLaPaloma.value != "Not_Sel")
		document.formulario.checkBoxLaPaloma.checked = true;	
}

function dropMesMvdeo_onblur() 
{		
	if (document.formulario.dropMesDesdeMvdeo.value != "Not_Sel")
		document.formulario.checkBoxMvdeo.checked = true;
}

function dropMesPdelE_onblur() 
{
	if (document.formulario.dropMesDesdePdelE.value != "Not_Sel")
		document.formulario.checkBoxPdelE.checked = true;	
}

function dropMesRural_onblur() 
{
	if (document.formulario.dropMesDesdeRural.value != "Not_Sel")
		document.formulario.checkBoxRural.checked = true;	
}

function dropMesTermas_onblur() 
{
	if (document.formulario.dropMesDesdeTermas.value != "Not_Sel")
		document.formulario.checkBoxTermas.checked = true;	
}

function dropMesPiria_onblur() 
{
	if (document.formulario.dropMesDesdePiria.value != "Not_Sel")
		document.formulario.checkBoxPiria.checked = true;	
}

function dropMesBsAs_onblur() 
{
	if (document.formulario.dropMesDesdeBsAs.value != "Not_Sel")
		document.formulario.checkBoxBsAs.checked = true;	
}

function pertenece(caracter,cadena)
{
	var indexCadena, charActCadena;
	var loEncontre = false;
	
	for (indexCadena = 0; indexCadena < cadena.length; indexCadena ++)
	{
		charActCadena = cadena.charAt(indexCadena);
		if (charActCadena == caracter)
		{
			loEncontre = true;
			break;
		}
	}
	return loEncontre;
}

function cadenaNumericaValida(cadena)
{
	var indexCadena;
	var cadenaValida = "0123456789";
	var charActCadena;
	var huboMatch = true;
	
	if (cadena.length == 0)
		huboMatch = false;
	
	for (indexCadena = 0; indexCadena < cadena.length; indexCadena ++)
	{
		charActCadena = cadena.charAt(indexCadena);
		if (!pertenece(charActCadena,cadenaValida))
		{
			huboMatch = false;
			break;
		}
	}
	return huboMatch;
}

function verificarCampoValido(textField,minParam,maxParam,tipoParam)
{
	var retornoBoolean;
	
	retornoBoolean = true;
	if (textField.value == "")
	{
		alert(campoNoDebeSerVacioStr);
		textField.select();
		retornoBoolean = false;
	}
	else if (!cadenaNumericaValida(textField.value))
	{
		alert(campoDebeSerUnNumeroStr);
		textField.select();
		retornoBoolean = false;
	}
	else if (textField.value < minParam || textField.value > maxParam)
	{
		alert(tipoParam);
		textField.select();
		retornoBoolean = false;
	}
	return retornoBoolean;
}

function analizarCampoDeFecha(textField,tipoDeCampo,fechaMax)
{
	var retornoBoolean;
	
	retornoBoolean = true;
	switch (tipoDeCampo)
	{
		case 0: // día.		
			retornoBoolean = verificarCampoValido(textField,1,31,diaNoValidoStr);
		break;
		
		case 1: // mes.
			retornoBoolean = true; //Dejar por cualquier cambio de tipo_campo.
		break; // año.
		
		case 2:
			retornoBoolean = verificarCampoValido(textField,2010,3010,anioNoValidoStr);
		break;
	}	
	return retornoBoolean;
}

function analizarLugaresSeleccionados()
{		
	// Constantes.
	// -----------
	var MAX_CIUDADES = 8;
	var PIR = 0;
	var PE = 1;
	var LP = 2;
	var M = 3;
	var C = 4;
	var T = 5;
	var E = 6;
	var O = 7;
	
	var i=0;
	var lugaresStr = "";
	var fechaMin = new Date(9999,11,31);
	var fechaSeleccionada;
	var mesMin;
	var arrayLugaresSeleccionados = new Array(MAX_CIUDADES);
	
	for (i; i < MAX_CIUDADES; i++)
		arrayLugaresSeleccionados[i] = false;
	
	if (document.formulario.checkBoxPdelE.checked)
	{													
		arrayLugaresSeleccionados[PE] = true;
		if (!analizarFechas(document.formulario.textDiaDesdePdelE,document.formulario.dropMesDesdePdelE,document.formulario.textAnioDesdePdelE,
							document.formulario.textDiaHastaPdelE,document.formulario.dropMesHastaPdelE,document.formulario.textAnioHastaPdelE,fechaMaxPdelE))
			return false;
		
		fechaSeleccionada = new Date(document.formulario.textAnioDesdePdelE.value,document.formulario.dropMesDesdePdelE.value - 1,document.formulario.textDiaDesdePdelE.value);
		if (fechaMin > fechaSeleccionada)			
			fechaMin = new Date(fechaSeleccionada.getFullYear(),fechaSeleccionada.getMonth(),fechaSeleccionada.getDate());
	}	
	if (document.formulario.checkBoxLaPaloma.checked)
	{
		arrayLugaresSeleccionados[LP] = true;
		if (!analizarFechas(document.formulario.textDiaDesdeLaPaloma,document.formulario.dropMesDesdeLaPaloma,document.formulario.textAnioDesdeLaPaloma,
							document.formulario.textDiaHastaLaPaloma,document.formulario.dropMesHastaLaPaloma,document.formulario.textAnioHastaLaPaloma,fechaMaxLaPaloma))
			return false;
		
		fechaSeleccionada = new Date(document.formulario.textAnioDesdeLaPaloma.value,document.formulario.dropMesDesdeLaPaloma.value - 1,document.formulario.textDiaDesdeLaPaloma.value);
		if (fechaMin > fechaSeleccionada)			
			fechaMin = new Date(fechaSeleccionada.getFullYear(),fechaSeleccionada.getMonth(),fechaSeleccionada.getDate());
	}
	if (document.formulario.checkBoxPiria.checked)
	{
		arrayLugaresSeleccionados[PIR] = true;
		if (!analizarFechas(document.formulario.textDiaDesdePiria,document.formulario.dropMesDesdePiria,document.formulario.textAnioDesdePiria,
							document.formulario.textDiaHastaPiria,document.formulario.dropMesHastaPiria,document.formulario.textAnioHastaPiria,fechaMaxPiria))
			return false;
		
		fechaSeleccionada = new Date(document.formulario.textAnioDesdePiria.value,document.formulario.dropMesDesdePiria.value - 1,document.formulario.textDiaDesdePiria.value);
		if (fechaMin > fechaSeleccionada)			
			fechaMin = new Date(fechaSeleccionada.getFullYear(),fechaSeleccionada.getMonth(),fechaSeleccionada.getDate());
	}
	if (document.formulario.checkBoxColonia.checked)
	{
		arrayLugaresSeleccionados[C] = true;
		if (!analizarFechas(document.formulario.textDiaDesdeColonia,document.formulario.dropMesDesdeColonia,document.formulario.textAnioDesdeColonia,
							document.formulario.textDiaHastaColonia,document.formulario.dropMesHastaColonia,document.formulario.textAnioHastaColonia,fechaMaxColonia))
			return false;
		
		fechaSeleccionada = new Date(document.formulario.textAnioDesdeColonia.value,document.formulario.dropMesDesdeColonia.value - 1,document.formulario.textDiaDesdeColonia.value);
		if (fechaMin > fechaSeleccionada)			
			fechaMin = new Date(fechaSeleccionada.getFullYear(),fechaSeleccionada.getMonth(),fechaSeleccionada.getDate());
	}
	if (document.formulario.checkBoxRural.checked)
	{		
		arrayLugaresSeleccionados[E] = true;
		if (!analizarFechas(document.formulario.textDiaDesdeRural,document.formulario.dropMesDesdeRural,document.formulario.textAnioDesdeRural,
							document.formulario.textDiaHastaRural,document.formulario.dropMesHastaRural,document.formulario.textAnioHastaRural,fechaMaxRural))
			return false;
		
		fechaSeleccionada = new Date(document.formulario.textAnioDesdeRural.value,document.formulario.dropMesDesdeRural.value - 1,document.formulario.textDiaDesdeRural.value);
		if (fechaMin > fechaSeleccionada)			
			fechaMin = new Date(fechaSeleccionada.getFullYear(),fechaSeleccionada.getMonth(),fechaSeleccionada.getDate());
	}
	if (document.formulario.checkBoxMvdeo.checked)
	{	
		arrayLugaresSeleccionados[M] = true;
		if (!analizarFechas(document.formulario.textDiaDesdeMvdeo,document.formulario.dropMesDesdeMvdeo,document.formulario.textAnioDesdeMvdeo,
							document.formulario.textDiaHastaMvdeo,document.formulario.dropMesHastaMvdeo,document.formulario.textAnioHastaMvdeo,fechaMaxMvdeo))
			return false;
		
		fechaSeleccionada = new Date(document.formulario.textAnioDesdeMvdeo.value,document.formulario.dropMesDesdeMvdeo.value - 1,document.formulario.textDiaDesdeMvdeo.value);
		if (fechaMin > fechaSeleccionada)			
			fechaMin = new Date(fechaSeleccionada.getFullYear(),fechaSeleccionada.getMonth(),fechaSeleccionada.getDate());
	}
	if (document.formulario.checkBoxTermas.checked)
	{	
		arrayLugaresSeleccionados[T] = true;
		if (!analizarFechas(document.formulario.textDiaDesdeTermas,document.formulario.dropMesDesdeTermas,document.formulario.textAnioDesdeTermas,
							document.formulario.textDiaHastaTermas,document.formulario.dropMesHastaTermas,document.formulario.textAnioHastaTermas,fechaMaxTermas))
			return false;
		
		fechaSeleccionada = new Date(document.formulario.textAnioDesdeTermas.value,document.formulario.dropMesDesdeTermas.value - 1,document.formulario.textDiaDesdeTermas.value);
		if (fechaMin > fechaSeleccionada)			
			fechaMin = new Date(fechaSeleccionada.getFullYear(),fechaSeleccionada.getMonth(),fechaSeleccionada.getDate());
			
	}
	if (document.formulario.checkBoxBsAs != undefined && document.formulario.checkBoxBsAs.checked)
	{	
		arrayLugaresSeleccionados[O] = true;
		if (!analizarFechas(document.formulario.textDiaDesdeBsAs,document.formulario.dropMesDesdeBsAs,document.formulario.textAnioDesdeBsAs,
							document.formulario.textDiaHastaBsAs,document.formulario.dropMesHastaBsAs,document.formulario.textAnioHastaBsAs,fechaMaxOtros))
			return false;
		
		fechaSeleccionada = new Date(document.formulario.textAnioDesdeBsAs.value,document.formulario.dropMesDesdeBsAs.value - 1,document.formulario.textDiaDesdeBsAs.value);
		if (fechaMin > fechaSeleccionada)			
			fechaMin = new Date(fechaSeleccionada.getFullYear(),fechaSeleccionada.getMonth(),fechaSeleccionada.getDate());
			
	}
	for (i=0; i < MAX_CIUDADES; i++)
	{						
		lugaresStr += arrayLugaresSeleccionados[i];
		if (i < MAX_CIUDADES - 1)
			lugaresStr += ",";		
		arrayLugaresSeleccionados[i] = false;
	}		
	document.all('textLugaresSeleccionados').value = lugaresStr;	
	mesMin = new String(fechaMin.getMonth() + 1);
	document.all('textMenorFecha').value =  mesMin + "/" + fechaMin.getDate() + "/" + fechaMin.getFullYear();
	return true;
}

function setAnioBisiesto(textAnio)
{
	if ((textAnio.value % 400 == 0) || 
	    (textAnio.value % 4 == 0 && textAnio.value % 100 != 0))
		arrayMaxDiasMeses[1] = 29;
	else
		arrayMaxDiasMeses[1] = 28;
	return 0;
}

function analizarFechas(textDiaDesde,dropMesDesde,textAnioDesde,textDiaHasta,dropMesHasta,textAnioHasta,fechaMaxCiudadDate)
{			
	var fechaDesdeDate, fechaHastaDate;

	if (!analizarCampoDeFecha(textDiaDesde,0))
		return false;
	
	if (dropMesDesde.value == "Not_Sel")
	{
		alert(mesNoSeleccionadoStr);
		dropMesDesde.focus();
		return false;
	}			
	if (!analizarCampoDeFecha(textAnioDesde,2))
		return false;
	
	if (!analizarCampoDeFecha(textDiaHasta,0))
		return false;
	
	if (dropMesHasta.value == "Not_Sel")
	{
		alert(mesNoSeleccionadoStr);
		dropMesHasta.focus();
		return false;
	}	
	if (!analizarCampoDeFecha(textAnioHasta,2))
		return false;
	
	setAnioBisiesto(textAnioDesde);
	if (textDiaDesde.value > arrayMaxDiasMeses[dropMesDesde.value-1])
	{
		alert(fechaNoValidaStr);
		textDiaDesde.select();
		return false;
	}
	
	setAnioBisiesto(textAnioHasta);
	if (textDiaHasta.value > arrayMaxDiasMeses[dropMesHasta.value-1])
	{
		alert(fechaNoValidaStr);
		textDiaHasta.select();
		return false;
	}
	
	fechaDesdeDate = new Date(textAnioDesde.value,dropMesDesde.value-1,textDiaDesde.value);
	fechaHastaDate = new Date(textAnioHasta.value,dropMesHasta.value-1,textDiaHasta.value);
	
	if (fechaDesdeDate >= fechaHastaDate)
	{
		alert(fechaNoValidaStr);
		textDiaDesde.select();
		return false;
	}
	
	if (fechaDesdeDate > fechaMaxCiudadDate)
	{
		alert(fechaExcedeStr);
		textDiaDesde.select();
		return false;
	}
	
	if (fechaHastaDate > fechaMaxCiudadDate)
	{
		alert(fechaExcedeStr);
		textDiaHasta.select();
		return false;
	}
	return true;
}

function analizarAcompanantes()
{
	if (document.formulario.dropCantMenores.value >= 
		document.formulario.dropCantAcomp.value)
	{
		alert(cantMenExcedeStr);
		document.formulario.dropCantMenores.focus();
		return false;
	}
	return true;
}

function analizarTraslados()
{
	if (document.formulario.checkBoxAutoTotal.checked)
	{
		if (document.formulario.dropSelAutoTotal.value == "Seleccione Auto")
		{
			document.formulario.dropSelAutoTotal.focus();
			alert("No se ha seleccionado un Auto.");
			return false;							
		}
	}
	else // Mayor precedencia para Auto Total.
	{
		if (document.formulario.textDiasAuto.value != "")
		{
			if (document.formulario.dropSelAutoDias.value == "Seleccione Auto")
			{
				document.formulario.dropSelAutoDias.focus();
				alert("No se ha seleccionado un Auto.");
				return false;							
			}	
		}
	}
	return true;
}

function analizarDatosPersonales()
{
	if (document.formulario.textNombre.value == "")
	{
		document.formulario.textNombre.focus();
		alert(olvidoElNombreStr);
		return false;
	}
	if (document.formulario.textEMail.value == "")
	{
		document.formulario.textEMail.focus();
		alert(olvidoEMailStr);
		return false;
	}
	return true;
}

function buttonEnviar_onclick()
{		
	var resultado = false;	
	
	if (!yaHizoClick)
	{
		yaHizoClick = true;
		resultado = analizarLugaresSeleccionados() && analizarAcompanantes() && analizarTraslados() && analizarDatosPersonales();		
		if (!resultado)
			yaHizoClick = false;
	}
	return resultado;
}
