// JavaScript Document
function locacionx(){
	d1= document.forma.locacion.value;
	document.forma.otralocacion.value;
	if (d1=='otra'){
		divi= document.getElementById('otraL');
		divi.style.display= 'block';
		document.forma.otralocacion.value= "";
	} else {
		divi= document.getElementById('otraL');
		divi.style.display= 'none';
	}
}
function locacionxRe(){
	d1= document.form3.locacion.value;
	document.form3.otralocacion.value;
	if (d1=='otra'){
		divi= document.getElementById('otraL');
		divi.style.display= 'block';
		document.form3.otralocacion.value= "";
	} else {
		divi= document.getElementById('otraL');
		divi.style.display= 'none';
	}
}
function locacionxr(){
	d1= document.forma.locacion2.value;
	if (d1=='otra'){
		divi= document.getElementById('otraLR');
		divi.style.display= 'block';
		document.forma.otralocacion2.value= "";
	} else {
		divi= document.getElementById('otraLR');
		divi.style.display= 'none';
	}
}
function locacionxrR(){
	d1= document.form3.locacion2.value;
	if (d1=='otra'){
		divi= document.getElementById('otraLR');
		divi.style.display= 'block';
		document.forma.otralocacion2.value= "";
	} else {
		divi= document.getElementById('otraLR');
		divi.style.display= 'none';
	}
}
function airlinexr(){
	d1= document.forma.airline.value;
	if (d1=='otra'){
		divi= document.getElementById('otraAIR');
		divi.style.display= 'block';
		document.forma.otraairline.value= "";
	} else {
		divi= document.getElementById('otraAIR');
		divi.style.display= 'none';
	}
}
function cardxr(){
	d1= document.forma.payment.value;
	if (d1=='1' || d1=='2'){
		divi= document.getElementById('payWithCard');
		divi.style.display= 'block';
		document.forma.card_name.value= "";
		document.forma.card_number.value= "";
		document.forma.card_code.value= "";
	} else {
		diva= document.getElementById('payWithCard');
		diva.style.display= 'none';
		
	}
}
function revisainsurance(cuantos){
	sigue= cuantos + 1;
	ultimo= eval("document.forma.extra" + sigue);
	if (ultimo.checked== true){
		for (i=1; i<=cuantos; i++){
			este= eval("document.forma.extra" + i);
			este.disabled= true;
			este.checked= false;
		}
	} else {
		for (i=1; i<=cuantos; i++){
			este= eval("document.forma.extra" + i);
			este.disabled= false;
		}
		
	}
		
}
function revisainsuranceB(cuantos){
	estos= 0;
	sigue= cuantos + 1;
	ultimo= eval("document.forma.extra" + sigue);
	for (i=1; i<=cuantos; i++){
		este= eval("document.forma.extra" + i);
		if (este.checked== true){
			estos++;
		}
	}
	if (cuantos == estos){
		ultimo.checked= true;	
		for (i=1; i<=cuantos; i++){
			este= eval("document.forma.extra" + i);
			este.disabled= true;
			este.checked= false;
		}
	}		
}
function revisainsuranceR(cuantos){
	sigue= cuantos + 1;
	ultimo= eval("document.form2.extra" + sigue);
	if (ultimo.checked== true){
		for (i=1; i<=cuantos; i++){
			este= eval("document.form2.extra" + i);
			este.disabled= true;
			este.checked= false;
		}
	} else {
		for (i=1; i<=cuantos; i++){
			este= eval("document.form2.extra" + i);
			este.disabled= false;
		}
		
	}
		
}
function revisainsuranceBR(cuantos){
	estos= 0;
	sigue= cuantos + 1;
	ultimo= eval("document.form2.extra" + sigue);
	for (i=1; i<=cuantos; i++){
		este= eval("document.form2.extra" + i);
		if (este.checked== true){
			estos++;
		}
	}
	if (cuantos == estos){
		ultimo.checked= true;	
		for (i=1; i<=cuantos; i++){
			este= eval("document.form2.extra" + i);
			este.disabled= true;
			este.checked= false;
		}
	}		
}
function revisaforma1(){
	d1= document.forma.locacion.value;
	d2= document.forma.locacion2.value;
	d3= document.forma.fecha1.value;
	d4= document.forma.fecha2.value;
	d5= document.forma.elcarro.value;
	d6= document.forma.otralocacion.value;
	d7= document.forma.otralocacion2.value;
	error= "";
	if (d1==0){
		error += "\n - Pickup location";
	}
	if (d1=="otra" && d6==""){
		error += "\n - Alternative pickup location";
	}
	if (d2=="otra" && d7==""){
		error += "\n - Alternative return location";
	}
	if (d3==""){
		error+= "\n - Pickup date";
	}
	if (d4==""){
		error+= "\n - Return date";
	}
	if (d5==""){
		error+= "\n - Choose a car";
	}
	if (error!=""){
		mensaje= "You are forgeting to fill some required fields\n" + error;
		alert (mensaje);
		return false;
	}
}
function revisaforma2(){
	d0= document.forma.locacion.value;
	d1= document.forma.nombre.value;
	d2= document.forma.nombre2.value;
	d3= document.forma.email.value;
	d4= document.forma.phone.value;
	d5= document.forma.country.value;
	d6= document.forma.city.value;
	d7= document.forma.airline.value;
	d8= document.forma.otraairline.value;
	d9= document.forma.flight.value;
	d10= document.forma.arrival.value;
	d11= document.forma.hotel.value;
	d12= document.forma.room.value;
	d13= document.forma.payment.value;
	d14= document.forma.card_name.value;
	d15= document.forma.card_number.value;
	d16= document.forma.card_code.value;
	error= "";
	// obligatorios
	if (d1==0){ error += "\n - First Name"; }
	if (d2==""){ error+= "\n - Last Name"; }
	if (d3==""){ error+= "\n - E-mail"; }
	if (d4==""){ error+= "\n - Phone number"; }
	if (d5==0){ error+= "\n - Country"; }
	if (d6==""){ error+= "\n - City"; }
	// obligatorios
	if (d0=='20'){
		if (d7==0){ error+= "\n - Airline"; }
		
		if (d7=='otra' && d8==""){ error+= "\n - Other Airline"; }
		
		if (d9==""){ error+= "\n - Flight"; }
		if (d10==""){ error+= "\n - Arrival time"; }
	} else {
		if (d11==""){ error+= "\n - Hotel"; }
		if (d12==""){ error+= "\n - Room"; }
	}
	if (d13==0){ error+= "\n - Payment method"; }
	filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(d3)) {
		error+= '\n\n - Please provide a valid email address';
	}
	if (error!=""){
		mensaje= "You are forgeting to fill some required fields\n" + error;
		alert (mensaje);
		return false;
	}
	
}
function revisaforma3(){
	var pre = document.forma.pregun.value;
	if (pre ==""){
		alert("Please write a question");
		return false;
	} else{
		return true;
	}
}
function revisaforma4(){
	var pre = document.forma.ticketid.value;
	if (pre ==""){
		alert("Please write a ticket ID");
		return false;
	} else{
		return true;
	}
}
function muestra1(){
	diva= document.getElementById('oculto1');
	divb= document.getElementById('ocultador1');
	if (diva.style.display=='none'){
		diva.style.display= 'block';
		divb.style.display= 'none';
	} else 	if (diva.style.display=='block'){
		diva.style.display= 'none';
		divb.style.display= 'block';
	}
}
/////////-----------------------------------------------------------------
function lahora(){
	d1= document.forma.hora1.options.selectedIndex;
	dX= document.forma.hora2.options.selectedIndex;
	d2= d1+1;
	if (dX>d2){
		divi= document.getElementById('leyenda');
		divi.style.display= 'block';
	} else {
		divi= document.getElementById('leyenda');
		divi.style.display= 'none';
	}
}
function buscafecha(){
	d1= document.forma.hora1.options.selectedIndex;
	dX= document.forma.hora1.options.length;
	d2= d1+1;
	if (d2>=dX){
		d2= 0 + (d2-dX);
	}
	document.forma.hora2.options.selectedIndex= d2;
}
function buscafecha1(){
	d1= document.forma.hora1.options.selectedIndex;
	dX= document.forma.hora1.options.length;
	d2= d1+1;
	if (d2>=dX){
		d2= 0 + (d2-dX);
	}
	document.forma.hora2.options.selectedIndex= d2;
	lahora();
}
/////////-----------------------------------------------------------------
function cambiar(a,b){
	diva= document.getElementById(a);
	divb= document.getElementById(b);
	//alert (diva + " vs " + divb);
	if (diva.style.display=='none'){
		diva.style.display= 'block';
		divb.style.display= 'none';
	} else 	if (diva.style.display=='block'){
		diva.style.display= 'none';
		divb.style.display= 'block';
	}
}
function enviaforma1(){
	document.forms["form1"].action= 'reservacion2b.php';
	document.forms["form1"].submit();

}
function enviaforma2(este){
	document.forms["form1"].action= 'reservacion2.php';
	document.forms["form1"].elcarro.value= este;
	document.forms["form1"].submit();

}
//el AJAX sencillito-----------------------------------------------------------------------------
function llamarAJAX(url, id_contenedor){
	var pagina_requerida = false
	if (window.XMLHttpRequest) {// Si es Mozilla, Safari etc
		pagina_requerida = new XMLHttpRequest()
	} else if (window.ActiveXObject){ // pero si es IE
		try {
			pagina_requerida = new ActiveXObject("Msxml2.XMLHTTP")
		} 
		catch (e){ // en caso que sea una versión antigua
			try{
				pagina_requerida = new ActiveXObject("Microsoft.XMLHTTP")
		}
		catch (e){ }
		}
	} else {
		return false;
	}
	pagina_requerida.onreadystatechange=function(){ // función de respuesta
	cargarpagina(pagina_requerida, id_contenedor)
	}
	pagina_requerida.open('GET', url, true) // asignamos los métodos open y send
	pagina_requerida.send(null)
}
	// todo es correcto y ha llegado el momento de poner la información requerida en su sitio en la pagina xhtml
function cargarpagina(pagina_requerida, id_contenedor){
	if (pagina_requerida.readyState == 4 && (pagina_requerida.status==200 || window.location.href.indexOf("http")==-1))
	document.getElementById(id_contenedor).innerHTML=pagina_requerida.responseText
}
//--------------------------------------------------------------------------------------------------------------------