// JavaScript Document



function objetoAjax(){
	var xmlhttp=false;
	try{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}catch(e){
		try{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}catch(E){
			xmlhttp = false;
  		}
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}
//formulario inicial---------------------------------
function select_loc()
{
	var lugar = document.datosIniciales.recoger.value.toString();
	document.datosIniciales.dejar.value = lugar;
}

function cambiaHora(){
	var horaIni = document.datosIniciales.horaInicial.value.toString();
	document.datosIniciales.horaFinal.value = horaIni;
}

function validaFechas(){
	
}

function muestraError(msj){
	divError = document.getElementById("error");
	divError.style.display = "block";
	divError.innerHTML = "<p>"+msj+"</p>";
}

function valida(){
	var recoger = document.datosIniciales.recoger.selectedIndex;
	var dejar = document.datosIniciales.dejar.selectedIndex;
	var fechaIni = document.datosIniciales.fechaInicial.value.toString();
	var fechaFin = document.datosIniciales.fechaFinal.value.toString();
	var horaIni = document.datosIniciales.horaInicial.value.toString();
	var leng = document.datosIniciales.leng.value.toString();
	
	
	
	if (recoger == 0){
		if(leng=="es")
		muestraError("Escoge una ubicacion para el campo recoger");
		else
		muestraError("Choose a location for the field Pick Up");
		return false;
	}
	
	if (dejar == 0){
		if(leng=="es")
		muestraError("Escoge una ubicacion para el campo recoger");
		else
		muestraError("Choose a location for the field Drop Off");
		return false;
	}
	
	if ( fechaIni == "" ){
		if(leng=="es")
		muestraError("Debes de introducir una fecha para la renta");
		else
		muestraError("Enter a Date for rent");
		return false;
	}
	
	if ( fechaFin == "" ){
		if(leng=="es")
		muestraError("Debes de introducir una fecha para la devoluci&oacute;n");
		else
		muestraError("Enter a Date for the return");
		return false;
	}
	
	//------------------ VALIDACION DE FECHAS
	var hoy = new Date();
	var month = hoy.getMonth() + 1;
	var day = hoy.getDate();
	var year = hoy.getFullYear();
	var hora = hoy.getHours();
	var hoyUs = fechaIni.charAt(3)+fechaIni.charAt(4);
	var mesUs = fechaIni.charAt(0)+fechaIni.charAt(1);
		
	if ( Date.parse(fechaIni) < Date.parse(month + "/" + day + "/" + year) ){
		if(leng=="es")
		muestraError("Fecha caduca. Debes de introducir una fecha actual");
		else
		muestraError("Date expires. You must enter a current date");
		return false;
	}
	
	if ( Date.parse(fechaIni) > Date.parse(fechaFin) ){
		if(leng=="es")
		muestraError("Fecha caduca. La fecha de devolucion debe ser despues de la renta");
		else
		muestraError("Date expires. The return date must be after the income");
		return false;
	}
	
	if ( hoyUs == day && month == mesUs){
		if ( horaIni <= hora+1 ){
		if(leng=="es")
		muestraError("Fecha caduca. Verifique la hora de renta. (Para renta del dia 2 horas de margen)");
		else
		muestraError("Date expires. Check the time of rental. (To \"rent day\" 2 hours of margin)");
		return false;
		}
	}
	
	totalDias = ( ( Date.parse(fechaFin) - Date.parse(fechaIni) ) /1000/3600/24 ) ;

	if ( totalDias == 0 ){
		if(leng=="es")
		muestraError("Fecha caduca. Verifique las fechas");
		else
		muestraError("Date expires. Check Dates");
		return false;
	}


	//alert(Date.parse(fechaIni));
	
	
}

function soloNumeros(){
	
	var key=window.event.keyCode;

	if (key < 48 || key > 57){
	window.event.keyCode=0;
	}
	
}

function validaDatosPersonales(){
	
	// CAPTCHA--------------------------
	
	
	var nombre = document.datosFinales.nombre;
	var email = document.datosFinales.correo;
	var telefono = document.datosFinales.telefono;
	var ciudad = document.datosFinales.ciudad;
	var recoger = document.datosFinales.recoger;
	var comentario = document.datosFinales.comentario;
	var leng = document.datosFinales.leng.value.toString();
	var tracking = document.datosFinales.tracking;
	
	if ( nombre.value == 0){
		if(leng=="es")
		muestraError("Error: Introduce tu nombre en el campo");
		else
		muestraError("Error: Please enter your name in the field");
		nombre.focus();
		return false;
	}
	
	if ( ciudad.value == 0){
		if(leng=="es")
		muestraError("Error: Introduce la Ciudad en el campo");
		else
		muestraError("Error: Enter the City in the field");
		ciudad.focus();
		return false;
	}
	
	if ( email.value == 0){
		if(leng=="es")
		muestraError("Error: Introduce su correo electronico en el campo");
		else
		muestraError("Error: Please enter your email address in the field");
		email.focus();
		return false;
	}
	
	if ( telefono.value == 0){
		if(leng=="es")
		muestraError("Error: Introduce tu numero de telefono en el campo");
		else
		muestraError("Error: Enter your phone number in the field");
		telefono.focus();
		return false;
	}
	

	
	
	
	
	//Si escogio Cancun Airport el usuario entonces:
	
	var vuelo = document.datosFinales.vuelo;
	var aereolinea = document.datosFinales.aereolinea;
	var horaLlegada = document.datosFinales.horaLlegada;
	var origen = document.datosFinales.origen;
		
	
	if ( recoger.value == "Cancun Airport"){
		
		if ( vuelo.value == 0){
			if(leng=="es")
			muestraError("Error: Introduce el numero de tu vuelo en el campo");
			else
			muestraError("Error: Enter the number of your flight from the field");
			vuelo.focus();
			return false;
		}
		if ( aereolinea.value == 0){
			if(leng=="es")
			muestraError("Error: Introduce la Aereolinea de tu vuelo en el campo");
			else
			muestraError("Error: Enter the name of your flight Aereolinea in the field");
			aereolinea.focus();
			return false;
		}
		if ( horaLlegada.value == 0){
			if(leng=="es")
			muestraError("Error: Introduce la hora de llegada de tu vuelo en el campo");
			else
			muestraError("Error: Enter the arrival time of your flight from the field");
			horaLlegada.focus();
			return false;
		}
		if ( origen.value == 0){
			if(leng=="es")
			muestraError("Error: Introduce el lugar de origen en el campo");
			else
			muestraError("Error: Enter the place of origin in the field");
			origen.focus();
			return false;
		}
		
	}
	
	
	if ( tracking.value == 0){
		muestraError("Please enter how did you found us");
		tracking.focus();
		return false;
	}
	
	var letras = document.datosFinales.letras;
	var tempCap = document.datosFinales.tempCap;
	
	if ( letras.value == tempCap.value ){
		return true;
	}else{
		muestraError("Error: ");
		tempCap.focus();
		divError = document.getElementById("error");
		divError.style.display = "none";
		divIntenta = document.getElementById("errorCap");
		divIntenta.style.display = "Block";
		return false;
	}
	
	
	
}

function reserva(carro){
	
	divRes = document.getElementById("divPanel");
	fechaInicial = document.datosPaso2.fechaInicial.value;
	fechaFinal = document.datosPaso2.fechaFinal.value;
	horaInicial = document.datosPaso2.horaIni.value;
	minutosHI = document.datosPaso2.minutosHI.value;
	horaFinal = document.datosPaso2.horaFin.value;
	totalDias = ( ( Date.parse(fechaFinal) - Date.parse(fechaInicial) ) /1000/3600/24 ) ;
	des = document.tipoAutos.desRow.value;
	
	ajax=objetoAjax();
	
	ajax.open("POST", "panel.php");
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			divRes.style.display = "Block";
			divRes.innerHTML = ajax.responseText;
		}else{
			divRes.style.display = "Block";
			divRes.innerHTML = "<img src='imagenes/load.gif' />";
		}
	}
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	ajax.send("carro="+carro+"&totalDias="+totalDias+"&horaIni="+horaInicial+"&horaFin="+horaFinal+"&minutosHI="+minutosHI+"&descripccionRow="+des+"&fechaInicial="+fechaInicial);
	
}

function cambiaTipoAuto(){
	
	divRes = document.getElementById("autos");
	divPan = document.getElementById("divPanel");
	fechaInicial = document.datosPaso2.fechaInicial.value;
	divPan.style.display = "none";

	tipoAuto = document.tipoAutos.escogeTipo.value;
	des = document.tipoAutos.desRow.value;

	ajax = objetoAjax();
	
	ajax.open("POST", "tipoAutos.php");
	ajax.onreadystatechange=function() {
	if (ajax.readyState==4) {
			divRes.style.display = "Block";
			divRes.innerHTML = ajax.responseText;
		}else{
			divRes.style.display = "Block";
			divRes.innerHTML = "<img src='imagenes/load2.gif' />";
		}
	}
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	ajax.send("tipoAuto="+tipoAuto+"&descripccionRow="+des+"&fechaInicial="+fechaInicial);
	
}

function mostrarVuelo(objeto){
	
	check = document.datosFinales.datosVuelo.checked;
	divMostrar = document.getElementById("datosVuelo");
	
	if (  check == true ){
		divMostrar.style.display = "Block";
	}
	else{
		divMostrar.style.display = "none";
	}
	
}

function validaDatos(){
		nombre = document.datosFinales.nombre;
		correo = document.datosFinales.correo;
		ciudad = document.datosFinales.ciudad;
		telefono = document.datosFinales.telefono;
		celular = document.datosFinales.celular;
		
		
		
		
		if ( nombre.value == ""){
				muestraError("Debes de introducir un nombre v&aacute;lido");
				nombre.focus();
				nombre.style.background = "#F8FED3";
				return false;
		}
		else{nombre.style.background = "#FFF";}
		
		if ( correo.value == ""){
				muestraError("Debes de introducir un nombre v&aacute;lido");
				correo.focus();
				correo.style.background = "#F8FED3";
				return false;
		}
		else{correo.style.background = "#FFF";}
		
		if ( telefono.value == ""){
				muestraError("Debes de introducir un tel&eacute;fono v&aacute;lido");
				telefono.focus();
				telefono.style.background = "#F8FED3";
				return false;
		}
		else{telefono.style.background = "#FFF";}
		
		if ( ciudad.value == ""){
				muestraError("Debes de introducir una ciudad v&aacute;lida");
				ciudad.focus();
				ciudad.style.background = "#F8FED3";
				return false;
		}
		else{ciudad.style.background = "#FFF";}
		
		
		
		if ( celular.value == ""){
				muestraError("Debes de introducir un celular v&aacute;lido");
				celular.focus();
				celular.style.background = "#F8FED3";
				return false;
		}
		else{celular.style.background = "#FFF";}
}

function submitForm(){
				
		document.forms["datosPaso2"].submit();
}

function cargaVideo(lugar){
	divResul = document.getElementById("divDestVideos");
	
	ajax = objetoAjax();
	
	ajax.open("POST", "destinosVideos/"+lugar+".php");
	ajax.onreadystatechange=function() {
	if (ajax.readyState==4) {
			divResul.style.display = "Block";
			divResul.innerHTML = ajax.responseText;
		}else{
			divResul.style.display = "Block";
			divResul.innerHTML = "<br /><img src='../imagenes/load3.gif' />";
		}
	}
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	ajax.send(null);
}

function validaContacto(){
		
	var nombre = document.contacto.nombre;
	var correo = document.contacto.correo;
	var comentario = document.contacto.comentario;
	var permiso = true;
	divForm = document.getElementById("formulario");
	
	if(comentario.value == 0){
		muestraError("Proporciona el comentario en el campo requerido");
		comentario.focus();
		permiso = false;
	}
	
	if(correo.value == 0){
		muestraError("Introduce tu correo en el campo requerido");
		correo.focus();
		permiso = false;
	}
	
	if( nombre.value == 0){
		muestraError("Introduce tu Nombre completo en el campo requerido");
		nombre.focus();
		permiso = false;
	}
	
	
	var letras = document.contacto.letras;
	var tempCap = document.contacto.tempCap;
	
	if ( letras.value == tempCap.value ){
		permiso = true;
	}else{
		
		divIntenta = document.getElementById("errorCap");
		divIntenta.style.display = "Block";
		return false;
	}
	
	if(permiso == true){
		divError = document.getElementById("error");
		divError.style.display = "none";
		
		ajax = objetoAjax();
		
		ajax.open("POST", "correoContacto.php");
		ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
				divForm.style.display = "Block";
				divForm.innerHTML = ajax.responseText;
			}else{
				divForm.style.display = "Block";
				divForm.innerHTML = "enviando...<br /><img src='imagenes/load3.gif' />";
			}
		}
		ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		ajax.send("nombre="+nombre.value+"&correo="+correo.value+"&comentario="+comentario.value);
		
	}else{
		return false;
	}

}