
var Reservation = {
	spinnerInterval: undefined,
	showError: function(msg){
		if($("error_messages").visible()==false){
			new Effect.Appear("error_messages", {duration:0.5});
		}
		$("error_messages").update(msg);
		window.setTimeout(function(){
			new Effect.Fade("error_messages");
		}, 5000);
	},
	saveCustomerData: function(element){
		if($F("documento").strip()==""){
			new Reservation.showError("Por favor indique su n&uacute;mero de identificaci&oacute;n");
			$("documento").activate();
			return;
		};
		if($F("nombre").strip()==""){
			new Reservation.showError("Por favor indique su nombre");
			$("nombre").activate();
			return;
		};
		if($F("telefono_fijo").strip()==""){
			new Reservation.showError("Por favor indique su tel&eacute;fono fijo");
			$("telefono_fijo").activate();
			return;
		};
		if($F("telefono_movil").strip()==""){
			new Reservation.showError("Por favor indique su tel&eacute;fono m&oacute;vil");
			$("telefono_movil").activate();
			return;
		};
		if($F("direccion").strip()==""){
			new Reservation.showError("Por favor indique su direcci&oacute;n");
			$("direccion").activate();
			return;
		};
		if($F("nom_ciudad").strip()==""){
			new Reservation.showError("Por favor indique su ciudad");
			$("nom_ciudad").activate();
			return;
		};
		if($F("email").strip()==""){
			new Reservation.showError("Por favor indique su e-mail");
			$("email").activate();
			return;
		} else {
			var filter = /^[A-Za-z][A-Za-z0-9_]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/
			if(filter.test($F("email"))==false){
				new Reservation.showError("Por favor indique un e-mail valido");
				$("email").activate();
				return;
			} else {
				if($F("email")!=$F("confirm_email")){
					new Reservation.showError("El e-mail de confirmaci&oacute;n no es igual al e-mail ingresado");
					$("email").activate();
					return;
				}
			}
		};
		new Ajax.Request(Utils.getKumbiaURL()+"index/saveReservation", {
			onLoading: function(){
				$("tipo_documento").disable();
				$("documento").disable();
				$("nombre").disable();
				$("telefono_fijo").disable();
				$("telefono_movil").disable();
				$("email").disable();
				$("direccion").disable();
				$("nom_ciudad").disable();
				$("observaciones").disable();
				element.disable();
			},
			parameters: {
				tipo_documento: $F("tipo_documento"),
				documento: $F("documento"),
				nombre: $F("nombre"),
				telefono_fijo: $F("telefono_fijo"),
				telefono_movil: $F("telefono_movil"),
				email: $F("email"),
				direccion: $F("direccion"),
				ciudad: $F("nom_ciudad"),
				observacion: $F("observaciones")
			},
			onLoading: function(transport){
				new Reservation.showError("Procesando reserva...");
			},
			onSuccess: function(transport){
				$("hotel_info").update(transport.responseText);
			}
		});
	},
	saveData: function(element){
		if($F("nombre").strip()==""){
			new Reservation.showError("Por favor indique su nombre");
			$("nombre").activate();
			return;
		};
		if($F("telefono").strip()==""){
			new Reservation.showError("Por favor indique su tel&eacute;fono");
			$("telefono").activate();
			return;
		};
		if($F("email").strip()==""){
			new Reservation.showError("Por favor indique su e-mail");
			$("email").activate();
			return;
		};
		new Ajax.Request(Utils.getKumbiaURL()+"index/storeInfo", {
			onLoading: function(){
				$("nombre").disable();
				$("telefono").disable();
				$("email").disable();
				element.disable();
			},
			parameters: {
				nombre: $F("nombre"),
				telefono: $F("telefono"),
				email: $F("email")
			},
			onSuccess: function(transport){
				$("hotel_info").update(transport.responseText);
			}
		});
	},
	searchAvalibility: function(element){
		$(element).disable();
		if($F("hotel")=="@"){
			alert("No ha seleccionado el hotel");
			$(element).enable();
			return;
		};
		if($F("adultos")==""){
			$("adultos").value = 0;
		}
		if($F("ninos")==""){
			$("ninos").value = 0;
		}
		if((parseInt($F("adultos"))+parseInt($F("ninos")))==0){
			$("adultos").value = 1;
		}
		if(Prototype.Browser.IE){
			hotel_name = $("hotel").options[$("hotel").selectedIndex].innerText;
		} else {
			hotel_name = $("hotel").options[$("hotel").selectedIndex].text;
		}
		new Ajax.Request(Utils.getKumbiaURL()+"index/searchAvalibility", {
			parameters: {
				hotel: $F("hotel"),
				fecha_inicial: $F("id_fecini"),
				fecha_final: $F("id_fecfin"),
				adultos: $F("adultos"),
				ninos: $F("ninos")
			},
			onLoading: function(){
				var color = "", fg_color = "";;
				$("td_info").style.background = "#ffffff";
				$("ciudad").disable();
				$("ninos").disable();
				$("adultos").disable();
				$("hotel").disable();
				disableDateInput('fecini');
				disableDateInput('fecfin');
				if($F("hotel")=='RE'){
					color = "#C0AE95";
					fg_color = "#ffffff";
				}
				if($F("hotel")=='SA'){
					color = "#E1E5CF";
					fg_color = "#000000";
				}
				if($F("hotel")=='QU'){
					color = "#F0B297";
					fg_color = "#000000";
				}
				if($F("hotel")=='?'){
					color = "#C3DAE7";
					fg_color = "#000000";
					$("hotel_info").style.background = "#ffffff";
				}
				if(hotel_name!='CUALQUIERA'){
					var div = "<div align='center'><div style='width:280px;margin-left:10px;margin-top:20px;padding:10px;color:"+fg_color+";background:"+color+"'>"+
					" <table cellpadding='5'><tr><td><img src='/hfos/img/"+$F("hotel")+"_logo.png'></td><td>Estamos buscando disponibilidad"+
					" en el hotel "+hotel_name+", por favor espere unos segundos <span id='spinner'>...</span></td></tr></table></div></div>";
				} else {
					var div = "<div align='center'><div style='width:280px;margin-left:10px;margin-top:20px;padding:10px;color:"+fg_color+";background:"+color+"'>"+
					" <table cellpadding='5'><tr><td><img src='/hfos/img/bh-logo.png'></td><td>Estamos buscando disponibilidad"+
					" en los hoteles bh, por favor espere unos segundos <span id='spinner'>...</span></td></tr></table></div></div>";
				}
				new Reservation.startSpinner();
				$("ofertas").hide();
				$("hotel_info").update(div);
			},
			onSuccess: function(transport){
				$("image_up").hide();
				$("ciudad").enable();
				$("hotel").enable();
				$("adultos").enable();
				$("ninos").enable();
				enableDateInput('fecini');
				enableDateInput('fecfin');
				$("hotel_info").setStyle({backgroundColor:"#C0CCD3"});
				$("hotel_info").update(transport.responseText);
				element.enable();
				window.clearInterval(Reservation.spinnerInterval)
			}
		});
	},

	showHotelInfo: function(id, background){
		$$(".hotel_div").each(function(element){
			if(element!=$("hotel_div"+id)){
				if(element.visible()==true){
					new Effect.Fade(element, {
						duration: 0.3,
						afterFinish: function(){
							new Effect.Appear($("hotel_div"+id), {
								duration: 0.3,
								afterFinish: function(){
									$("td_info").style.background = background;
								}
							});
						}
					});
				}
			}
		})
	},

	startSpinner: function(){
		Reservation.spinnerInterval = window.setInterval(function(){
			var value = $("spinner").innerHTML;
			if(value=='.'){
				value = '..';
				$("spinner").update(value);
				return;
			}
			if(value=='..'){
				value = '...';
				$("spinner").update(value);
				return;
			}
			if(value=='...'){
				value = '.';
				$("spinner").update(value);
				return;
			}
		}, 1000);
	},

	getHotelInfo: function(element){
		var value = $F(element);
		if(value!='@'){
			new Ajax.Request(Utils.getKumbiaURL()+"index/getHotelInfo/"+value, {
				onSuccess: function(transport){
					$("image_up").hide();
					$("hotel_info").update(transport.responseText);
				}
			});
		}
	},
	captureCustomerData: function(id){
		new Ajax.Request(Utils.getKumbiaURL()+"/index/captureCustomerData/"+id, {
			onSuccess: function(transport){
				$("hotel_info").update(transport.responseText);
			}
		})
	}

};
