/**
 * @author angel
 */

function showFormReservation(productID,adults,kids,arrival,departure){
	parametros = 'modulo=reservaciones&accion=step2&isAjax=1';
	if(productID!=null) parametros+="&productID="+productID;
	if(adults!=null) parametros+="&adults="+adults;
	if(kids!=null) parametros+="&kids="+kids;
	if(arrival!=null) parametros+="&arrival="+arrival;
	if(departure!=null) parametros+="&departure="+departure;	
	showWindowAjax('wReservacion',{width:380,height:500,title:'Make Reservation',maximizable:false,resizable:true,minimizable:false,zIndex:10},'index.php',parametros,null,null,'post',true);
}


function getInfoHotel(id){

		
		$('infoHotel').innerHTML="Loading info...";
		
		var opt = {
			   parameters : "modulo=reservaciones&accion=getinfohotel&isAjax=1&id="+id,
			   method: 'post'
		}		
		
	
		var myAjax = new Ajax.Updater( 'infoHotel', 'index.php', opt);	
}


function reservationStep2(){
	form = $('formReservacion');
	
	checked = false;
	if(form.hotel.value==0) checked=false;
	else checked=true;
	
	if(!checked){
		 obj = form.getElementsByTagName("input");

		for (i=0; i<obj.length; i++) {
			if(obj.item(i).type == "checkbox"){
				if(obj.item(i).checked==true){
					checked = true;
					break;
				}
			}
		}
	}
	
	if(!checked)
	 alert("You need to select at least one service to make the reservation");
	else {
		Effect.toggle("step1","blind");
		Effect.toggle("step2","blind");
	}
}

 function makeReservation(){
 	 
	
	form = $('formReservacion');
	
	if (trim(form.fullname.value) == "") {
		alert("Please type your full name");
		return;
	}
	if (trim(form.email.value) == "") {
		alert("Please type your email");
		return ;
	}	

	  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(form.email.value)){
	   
	  } else {
	    alert("Your email address is invalid.");
	    return;
	  }
  
	if (trim(form.country.value) == "none") {
		alert("Please select your country");
		return;
	}

	if (trim(form.phone.value) == "none") {
		alert("Please type your phone number");
		return;
	}

	if (trim(form.arrival.value) == "") {
		alert("Please select your arrival date");
		return;
	}
	
	if (trim(form.departure.value) == "") {
		alert("Please select your departure date");
		return;
	}
	
	if (trim(form.departure.value) < trim(form.arrival.value)) {
		alert("Please select a valid departure date");
		return;
	}
		
	$('btreservationback').disable();
	$('btreservationmake').disable();
	
	parametros = Form.serialize($('formReservacion'));
	var opt = {
		   parameters : parametros,
		   onFailure:function(){
				alert("Falló el envio de datos");
			},
		 	onSuccess:function(t){
			 	if(t.responseXML.getElementsByTagName("respuesta")[0].firstChild.data == 1) {
					
					alert(t.responseXML.getElementsByTagName("mensaje")[0].firstChild.data);
					if($('wReservacion')){
						Windows.close('wReservacion');
					}

			}else {
			 		alert(t.responseXML.getElementsByTagName("mensaje")[0].firstChild.data);
					$('btreservationback').enable();
					$('btreservationmake').enable();
			 	}
		 	}
		}
		
	var request = new Ajax.Request('index.php',opt);
}


function bookNow(form){
	
	if(form.arrival.value!="" && form.departure.value!=""){
	showFormReservation(form.productID.value,form.adults.value,form.kids.value,form.arrival.value,form.departure.value)
	}else{
	 alert("Please select an arrival and departure date");
	}
}