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;
}

function MostrarConsulta(datos){
	divResultado = document.getElementById('mycalendardiv');
	ajax=objetoAjax();
	ajax.prototype.setAsync = true;
	ajax.open("GET", datos);
	
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			divResultado.innerHTML = ajax.responseText
		}
	}
	ajax.send(null)
}



function mostrar_fechas(id_oferta,origen_name,init_me, doubleajax,id_hotel)
{
  //reset price...
  divpricedata = document.getElementById('totalpricediv');
  document.form1.totalprice.value="";
  divpricedata.innerHTML="";
  
  //reset hotel selection.....
    
  divhotelResultado = document.getElementById('hotelsdiv');
  divhotelResultado.innerHTML='';
  
  //reset price total
  divpricedata = document.getElementById('totalpricediv');
  document.form1.totalprice.value="";
  divpricedata.innerHTML="No disponible.";
  
  
  divFResultado = document.getElementById('fechadetails');  
  divFResultado.style.display='inline';
  divHResultado = document.getElementById('fechashrremove');
  divHResultado.style.display='none';
  
  
	divResultado = document.getElementById('mycalendardiv');
	divResultado.innerHTML = '<center><br><br><img src=\'images/ajax-loaderb.gif\' border=0><br><br>Cargando...</center>';
	ajax=objetoAjax();	
	ajax.open("GET", "includes/ajax.php?action=flight_dates&id_oferta="+id_oferta+"&origen_name="+origen_name+"&init_me="+init_me);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			divResultado.innerHTML = ajax.responseText;
			if(doubleajax==1)
      {
          elige_vuelo(id_oferta,init_me,id_hotel);
      }
		}
	}
	ajax.send(null)

}

function mostrar_googlemap()
{
 
	divResultado = document.getElementById('promo');
	divFResultado = document.getElementById('map');
	divResultado.innerHTML = '<center><br><br><img src=\'images/loadingroller.gif\' border=0><br><br>Cargando...</center>';
	ajax=objetoAjax();	
	ajax.open("GET", "includes/ajax.php?action=googlemap&showzones=1");
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
		  //alert(ajax.responseText);
		  divResultado.style.display='inline';
			
      
      //divResultado.innerHTML = ajax.responseText;
			
			//eval(showmap); 
			//divResultado.innerHTML += "hola";
			//showmap;
			
      var selectorpanel="<div id=\"selector\">";                       
          selectorpanel += "<div id=\"selectoritems\" >";
          selectorpanel += "<table border=\"0\">";  
          selectorpanel += "<tr valign=\"middle\">";
          selectorpanel += "<td align=left><a href=\"javascript:centerme('42.68','-99.52');\" onfocus=\"blur();\">USA</a></td>";
          selectorpanel += "<td align=left width=\"90\"><a href=\"javascript:centerme('-12.12','-55.41');\" onfocus=\"blur();\">América del Sur</a></td>";
          selectorpanel += "<td align=left><a href=\"javascript:centerme('48.57','9.14');\" onfocus=\"blur();\">Europe</a></td>";
          selectorpanel += "<td align=left><a href=\"javascript:centerme('18.31','15.66');\" onfocus=\"blur();\">Africa</a></td>";
          selectorpanel += "<td align=left><a href=\"javascript:centerme('54.77','93.51');\" onfocus=\"blur();\">Asia</a></td>";
          selectorpanel += "<td align=left><a href=\"javascript:centerme('-24.84','135.17');\" onfocus=\"blur();\">Oceanía</a></td>";
          
          selectorpanel += "<td width=\"25\"><img src=\"images/ico_surf_map.png\" bordeR=\"0\"/></td><td width=\"25\"><input id=\"surfbox\" onclick=\"boxclick(this,'surf')\" type=\"checkbox\"/></td>";
          selectorpanel += "<td width=\"25\"><img src=\"images/ico_kite_map.png\" bordeR=\"0\"/></td><td width=\"25\"><input id=\"kitesurfbox\" onclick=\"boxclick(this,'kitesurf')\" type=\"checkbox\"/></td>";
          selectorpanel += "<td width=\"25\"><img src=\"images/ico_nieve_map.png\" bordeR=\"0\"/></td><td width=\"25\"><input id=\"skisnowbox\" onclick=\"boxclick(this,'skisnow')\" type=\"checkbox\"/></td>";    
          selectorpanel += "<td width=\"25\"><img src=\"images/ico_buceo_map.png\" bordeR=\"0\"/></td><td width=\"25\"><input id=\"divingbox\" onclick=\"boxclick(this,'diving')\" type=\"checkbox\"/></td>";
          selectorpanel += "<td width=\"60\" align=\"right\"><a href=\"javascript:resetmap();\" onfocus=\"blur();\" class=\"\"><img src=\"images/ref_16.gif\" bordeR=\"0\"/></a></td></tr>";
          selectorpanel += "</table></div></div>";
			divResultado.innerHTML="<div id=\"map\" style=\"width: 730px; height: 388px\">\n</div>"+selectorpanel;
			
      eval(ajax.responseText);
      showmap();
      
		}
	}
	ajax.send(null)

}


function mostrar_minigooglemap(location2show)
{
  //alert(location2show);
	divResultado = document.getElementById('columnaizquierda');
	divFResultado = document.getElementById('map');
	var prevcontent=divResultado.innerHTML;

	divResultado.innerHTML = '<center><br><br><img src=\'images/ajax-loaderb.gif\' border=0><br><br>Cargando...</center><br>'+prevcontent;
	ajax=objetoAjax();	
	ajax.open("GET", "includes/ajax.php?action=googlemap&showzones=0&showme="+location2show);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
		  //alert(ajax.responseText);
		  divResultado.style.display='inline';
			
      
			divResultado.innerHTML="<div id=\"map\" style=\"width: 172px; height: 172px\">\n</div>"+prevcontent;
			
      eval(ajax.responseText);
      showmap();
      
		}
	}
	ajax.send(null)

}


function elige_vuelo(id_oferta,already_init,id_hotel){

  divpricedata = document.getElementById('totalpricediv');
  document.form1.totalprice.value="";
  divpricedata.innerHTML="No disponible.";
  
  divFResultado = document.getElementById('hoteldetails');  
  divFResultado.style.display='inline';
  divHResultado = document.getElementById('hotelhrremove');
  divHResultado.style.display='none';
  
  divResultado = document.getElementById('hotelsdiv');
   
   if(already_init==0)
   {
      var indice = document.form1.fechacombo.selectedIndex;   
      var valor = document.form1.fechacombo.options[indice].id; 
   }
   else
   {  
      var valor = already_init;   
   }
   	var airports=document.form1.aeropuerto;
	if(airports.length>1){
    	for(j=0;j<airports.length;j++){
			if(airports[j].checked)
				var valor_air = airports[j].value;
			}
	}else{
		var valor_air = airports.value;
	}
	divResultado.innerHTML = '<center><br><br><img src=\'images/ajax-loaderb.gif\' border=0><br><br>Cargando...</center>';
	ajax=objetoAjax();	
	ajax.open("GET", "includes/ajax.php?action=hotels&id_oferta="+id_oferta+"&fecha="+valor+"&hotelselected="+id_hotel+"&aeropuerto="+valor_air);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			divResultado.innerHTML = ajax.responseText;
			CalculatePrice();
		}
	}
	ajax.send(null)

}

function SetTripData(data){  
  divtripdata = document.getElementById('myresultcaldiv');
  divtripdata.style.display="block";
  mydata='<table valign=top width="180" border="0" cellspacing="1" cellpadding="0"><tr><td class="today">Vuelo</td></tr><tr><td><table align=left><tr><td>'+data+'</td></tr></table></td></tr></table>';
  divtripdata.innerHTML= mydata;
  //set form data....
}


function init_page(id_paquetes,origen,fecha_paquete,id_hotel){
  
  mostrar_fechas(id_paquetes,origen,fecha_paquete,'1',id_hotel);
  
  num_hab('1');
  num_pass('2');
  //CalculatePrice();
}

function num_hab(num){
      var pasajeros="<br>";
      var i;
      
      for(i=0;i<num;i++){
      	pasajeros+="<select name='tipohab_"+i+"' class='text_black_9'><option value='Doble'>Doble</option><option value='Triple'>Triple</option><option value='Cuadruple'>Cu&aacute;druple</option></select><br>";
      	}
      document.getElementById("lista_habitaciones").innerHTML=pasajeros;
}

function num_pass(num){
    var pasajeros="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=text_red_9 ><i>Nombre y apellidos</i></span><br>";
    var i;
    var total=0;
    var totales="";
    
    //total=num*document.pepeday.precio_persona.value;
    for(i=0;i<num;i++){
    	pasajeros+="<select name='tipopasajero_"+i+"' class='text_black_9' ><option value='ADT'>Adulto</option><option value='CHD'>Niño</option><option value='INF'>Bebe</option></select><input type='text' class='text_black_9' name='pasajero_"+i+"' size='25'><br>";
    	}
    document.getElementById("lista_pasajeros").innerHTML=pasajeros;
    //totales="<table align='right' border='0' cellpadding='0' cellspacing='0'><tr><td class='text_black_18b'>Total:</td><td class='text_red_12'>"+document.pepeday.precio_persona.value+" x "+num+" =</td><td class='text_red_18b'>"+total+" €</td></tr></table><input type='hidden' name='total' value='"+total+"' >";
    
    //document.getElementById("precio_total").innerHTML=totales;
}



function CalculatePrice(){
  
  divpricedata = document.getElementById('totalpricediv');
        
    var exists = eval(document.form1["hotelselect"]);
    if(exists)
    {
          var radioLength = document.form1.hotelselect.length;      
    }
    else
    {
          divpricedata.innerHTML= "No disponible.";
          return;
    }

    if(radioLength == undefined) {
        hotelprice = document.form1.hotelselect.id;                
    }
    else
    {
         var hotelchosen="";
         for (i=0;i<document.form1.hotelselect.length;i++){
             if (document.form1.hotelselect[i].checked)
             {
                hotelprice = document.form1.hotelselect[i].id;
                hotelchosen=1;
                break;
             }
         }    
         if(hotelchosen!=1) return;
         
    } 
  
    //alert(hotelprice);
    
    //the activities to be added
        var activitiesprice = 0;
        var activitiesnames = "";
        for(i=0; i<document.form1.elements.length; i++)
        {
            if(document.form1.elements[i].type=="checkbox")
            {              
              tempprice=document.form1.elements[i].value;
              tempname=document.form1.elements[i].name;
              if (document.form1.elements[i].checked) 
              {
                  activitiesprice = (activitiesprice*1) + (tempprice*1);
                  activitiesnames += "["+tempname+"]";
              }
            }
        }
       
       document.form1.myactivities.value=activitiesnames;
       
       //alert(activitiesprice);
//     radioLength = document.form1.surfclass.length;
//     if(radioLength == undefined) {
//         surfclassprice = document.form1.surfclass.id;                
//     }
//     else
//     {
//         for (i=0;i<document.form1.surfclass.length;i++){
//            if (document.form1.surfclass[i].checked)
//               break;
//         }
//         surfclassprice= document.form1.surfclass[i].id;
//     } 
//   
    

    //for (i=0;i<document.form1.rooms.length;i++){
    //   if (document.form1.rooms[i].checked)
    //      break;
    //}
   
    places= document.form1.rooms.value; 
    places=1;
    //for (i=0;i<document.form1.passengers.length;i++){
    //   if (document.form1.passengers[i].checked)
    //      break;
    //}
    people= document.form1.passengers.value; 
  
      
    //hotelprice= (hotelprice*1)*((places*1)+(people*1));
   /* if(people<=2) people=1;
    if(people>2 && people <=4) people=2;
    if(people>4 && people <7) people=3;
    if(people>6 && people <9) people=4;*/
    
    hotelprice= (hotelprice*1)*(people*1);
    
    //surfprice=(surfclassprice * 1) * ( people*1);
    
    //document.form1.totalprice.value=(hotelprice * 1) + (surfprice * 1) ;
    document.form1.totalprice.value=(hotelprice * 1) + (activitiesprice*people) ;
    
   // alert("1");
    
    //document.totalprice.innerHTML = "";
    
    divpricedata.innerHTML=document.form1.totalprice.value + "€";
    //document.totalprice.innerHTML= document.form1.totalprice.value + "€";
}

function formsubmit(form_name)
{
  
  var frm = document.forms[form_name];

  if(frm.pasajero_0.value=="") 
  {
    alert("Rellena el nombre.");
    return;
  }
   
  if(frm.totalprice.value=="") return;
  frm.submit();
}

function testCreditCard (myform) {
  myCardNo = myform.numero_tarjeta.value;
  myCardType = myform.tipo_tarjeta.value;
  if (checkCreditCard (myCardNo,myCardType)) {
    return true;
  } 
  else {
    alert (ccErrors[ccErrorNo]);
    return false;
  };
}

function FSfncCheckCCexpire(FormField) {
	// Verifica que la fecha de caducidad de la tarjeta tiene un formato válido
	var CCexpire=FormField.value;
	if (CCexpire=="") {alert("Introduzca la fecha de caducidad de su tarjeta de crédito."); FormField.focus(); return false}
	var ArrayCCexpr=CCexpire.split("/");
	if ((ArrayCCexpr.length!=2) || (ArrayCCexpr[0]=="") || (isNaN(ArrayCCexpr[0])) || (ArrayCCexpr[1]=="") || (isNaN(ArrayCCexpr[1])) || (ArrayCCexpr[0]<1) || (ArrayCCexpr[0]>12)) {alert("El formato de la fecha de caducidad no es correcto."); FormField.focus(); return false}
	return true;
	}

function FSfncCheckCCnum(FormField) {
	// Verifica que el número de la tarjeta de crédito/débito tiene un formato válido
	var ccRE=/\W/gi;
	var CCnumber=FormField.value.replace(ccRE, "");
	if (isNaN(CCnumber)) {alert("El número de la tarjeta de crédito no tiene formato numérico."); FormField.focus(); return false}
	if ((CCnumber.length!=16) && (CCnumber.length!=18)) {alert("El número de dígitos en la tarjeta de crédito es incorrecto."); FormField.focus(); return false}
	var cardMath=0;
	for (i=CCnumber.length; i>0; i--) {
		if (i % 2 == 1) {
			var doubled = "" + (parseInt(CCnumber.substring(i - 1, i)) * 2);
			if (doubled.length==2) {doubled = parseInt(doubled.substring(0,1)) + parseInt(doubled.substring(1,2))}
			cardMath += parseInt(doubled);
			}
		else {cardMath += parseInt(CCnumber.substring(i - 1, i))}
		}
		
		
	if (cardMath % 10 != 0) {alert("El número de la tarjeta de crédito no es válido."); FormField.focus(); return false}
	return true;
}


function nif(dni) {
  		numero = dni.substr(0,dni.length-1);
  		let = dni.substr(dni.length-1,1);
  		numero = numero % 23;
  		letra='TRWAGMYFPDXBNJZSQVHLCKET';
  		letra=letra.substring(numero,numero+1);
  		if (letra!=let){ 
    		//alert('Dni erroneo');
			return false;
			}
		return true;
	}

	function FSfncCheckForm(form_name) {
	
	 var FormRef = document.forms[form_name];
	 
  //QUITAR
	/*FormRef.formsent.value="10";
		FormRef.submit();
	 return true;*/
	 //QUITAR
	 
	 
	
		if(FormRef.nombre_declaracion.value==""){
			alert("- Introduzca el nombre del titular de la tarjeta de crédito");
			return false;
			}
	
		if(!nif(FormRef.dni_declaracion.value)){
			alert("- Introduzca su DNI/Pasaporte");
			return false;
			}
			
		if (!testCreditCard(FormRef)) {return false}	
		//if (!FSfncCheckCCnum(FormRef.numero_tarjeta)) {return false}
		//if (!FSfncCheckCCexpire(FormRef.fecha_tarjeta)) {return false}
		
		if(FormRef.email.value==""){
			alert("- Introduzca un email de contacto");
			return false;
		}else{
			mail=FormRef.email.value;
			mail_1 = mail.split("@");
			mail_2 = mail.split(".");
			if((mail_1.length!=2)||(mail_2.length<2)){
				alert(" - Introduce una direccion de email valida\n");
				return false;
				}
			}
		if(FormRef.telefono.value==""){
			alert("- Introduzca un telefono de contacto");
			return false;
		}else{
			var tlf=FormRef.telefono.value;
					
			if(isNaN(tlf)){
			 	alert(" - El teléfono ha de ser un número\n");
				return false;
				}
			var pnum = tlf.substr(0,1);
			if((pnum!=6)&&(pnum!=9)){
				alert(" - El teléfono ha de empezar por 6 o 9\n");
				return false;
				}
			if(tlf.length!=9){
				alert(" - El teléfono ha de ser de 9 digitos\n");
				return false;
				}
			}
		if(FormRef.codigo_postal.value==""){
			alert("- Introduzca su código postal para saber su ciudad de salida del viaje");
			return false;
			}
		if(FormRef.acepto.checked==false){
			alert("- Debe aceptar las condiciones de uso y privacidad para finalizar la reserva.");
			return false;
			}
		
		FormRef.formsent.value="10";
		FormRef.submit();
		return true; // disable actual submit for example purposes - borrar esta línea en las páginas en vivo.
		}
