// JavaScript Document
var YPS = {
	configured:false,
	site:null,
	searchtype:null,
	provider:'bw',
	codeFrom:null,
	codeTo:null,
	formsent:false,
	valid:false,
	no_validate:false,
	use_login:true,
	page:null,
	webtitle:null,
	ot_id:null,
	
	trace: function(msg){ alert("Please correct the following:\n"+msg); },
	
	configure: function(o)
	{
		this.use_login = (o.use_login==1||o.use_login=='1')?true:false;
		this.webtitle = o.webtitle;
		this.webroot = o.webroot;
		this.page = o.page;
		this.ot_id = o.ot_id;
		this.site = o.site;
		this.ids = o.ids;
		this.searchtype = o.searchtype;
		this.provider = o.provider;
		this.codeFrom = o.codeFrom;
		this.codeTo = o.codeTo;
		if (this.codeFrom+this.codeTo != '' && this.codeFrom.length == 3) this.valid = true;
		this.configured = true;
	},
	
	tracking: function(data_in)
	{
		var DATA = data_in||"site="+this.site+"&searchtype="+this.searchtype;
		new Ajax.Request("/search/searchtracking.php?nocache="+new Date(), {
			method: 'post',
			postBody: DATA,
			onFailure: function() {},
			onComplete: function(transport){}
		});
	},
	
	badSearch: function(state, which)
	{
		this.valid = false;
		this.formsent = false;
		
		new Effect.Appear('searchbegin', {speed:0.05});
		$('step2header').style.display = "none";
		$('searchit_frame').style.display = "none";
		$('notifyer').style.display = "none";
		$('searching').style.display = "none";
		if (which == "to") var msg = "- Please enter a Valid Destination city or Airport Code\n";
		else var msg = "- Please enter a Valid Arrival city or Airport Code\n";
		trace(msg);
		
		$(which+"_flight").value = "";
		if ($(which+"_flight").className != "invalid") $(which+"_flight").className = "invalid";
	},
	
	build_post: function()
	{
		var DATA = "";
		var obj = document.forms["searchForm"].elements;
		var len = obj.length;
		for (var i=0; i<len; i++)
		{
			var element = obj[i];
			if (DATA != "") DATA += "&";
			if (element.type == "text" || element.type == "textarea" || element.type == "hidden")
			{
				DATA += element.name + "=" + escape(element.value);
			}
			else if (element.type == "select-one")
			{
				DATA += element.name + "=" + $(element.id).options[$(element.id).selectedIndex].value;
			}
		}
		DATA += "&provider="+this.provider;
		return DATA;
	},
	
	display: function(results)
	{
		switch(this.provider)
		{
			case "bw": this.display_bw(results); break
			case "ew": this.display_ew(results); break
			case "ot": this.display_ot(results); break
			case "sm": this.display_simple(results); break
			default: this.display_simple(results); break
		}
	},
	
	display_simple: function(results)
	{
		var self = this;
		if($('introtext'))$('introtext').style.display = "inline";
		$('resultlayer').style.display = "none";
		if ($('advertiser-layer')) $('advertiser-layer').style.display = "none";
		new Effect.Fade('introtext', {speed:0.05, afterFinishInternal:function()
		{
			if($('introtext'))$('introtext').style.display = "none";
			$("resultlayer").show();
			if ($('advertiser-layer')) $('advertiser-layer').style.display = "inline";
			self.user_login();
		}
		});
	},
	
	display_bw: function(results)
	{
		if (!this.formsent) return;
		
		if (results)
		{
			this.user_login();
			this.setTravelZoo();
			$('searching').style.display = "none";
			$('searchit_frame').style.display = "inline";
			if ($('frameheader')) $('frameheader').style.display = "inline";
			if ($('advertiser-layer')) $('advertiser-layer').style.display = "inline";
			this.formsent = false;
		}
		else
		{
			var goUrl = "/search/send_out.php?page="+this.page+"&sendGet=1&";
			var DATA = this.build_post();
			DATA += '&bw_cid='+this.ids.bw.cid+'&bw_bwiab='+this.ids.bw.bwiab;
			//alert(DATA);
			if ($('searchit_frame')) $('searchit_frame').src = goUrl+DATA;
			if ($('searchit_frame')) $('searchit_frame').style.display = "none";
			if ($('advertiser-layer')) $('advertiser-layer').style.display = "none";
			$("searching").show();
			if ($('step2header')) $('step2header').style.display = "inline";
			if ($('introtext'))
			{
				$('introtext').style.display = "none";
				if ($('notifyer')) $('notifyer').style.display = "inline";
				new Effect.Appear('notifyer', {speed:0.15, afterFinishInternal:function()
				{
					new Effect.Pulsate('notifyer', {pulses: 8, afterFinishInternal:function()
					{
						//$('notifyer').style.display = "none";
					}});
				}});
			}
			else
			{
				new Effect.Appear('notifyer', {speed:0.15, afterFinishInternal:function()
				{
					new Effect.Pulsate('notifyer', {pulses: 8});
				}});
				new Effect.Appear('searchit_frame', {speed:0.05, afterFinishInternal:function()
				{
					//$('searchit_frame').style.display = "inline";
				}
				});
			}
		}
	},
	
	display_ot: function()
	{
		if (this.searchtype == "flights")
		{
			if ((this.codeFrom == "" && $("from_flight").value=="") || (this.codeTo == "" && $("to_flight").value==""))
			{
				var msg = "";
				if (this.codeFrom == "" && $("from_flight").value=="") msg += "- Please enter a Valid Departure city or Airport Code\n";
				if (this.codeTo == "" && $("to_flight").value=="") msg += "- Please enter a Valid Arrival city or Airport Code\n";
				
				
				this.trace(msg);
				return (false);
			}
			
			if (this.codeFrom == "" && $("from_flight").value!="") this.codeFrom = $("from_flight").value;
			if (this.codeTo == "" && $("to_flight").value!="") this.codeTo = $("to_flight").value;
		}
		
		if($('introtext'))$('introtext').style.display = "inline";
		$('resultlayer').style.display = "none";
		if ($('advertiser-layer')) $('advertiser-layer').style.display = "none";
		if ($('other-sites')) $('other-sites').style.display = "none";
		if ($('resultlayer-txt')) $('resultlayer-txt').hide();
		$('resultlayer').innerHTML = "";
		
		var self = this;
		
		var displayOptions = { 
			cache: false, 
			anchorElement: 'resultlayer', 
			cssURL: self.webroot+'/search/onetimemeetsus.css',
			dimensions: { width: 360, height: 250 }
		};
		
		if (this.searchtype == "flights"){
			var trackingTags = { tags: 'Site='+self.webtitle+';Type=flight;From='+self.codeFrom+';to='+self.codeTo};
			var flightBookingOptions = {
				flight: {
					fromId: null, 
					toId: null,
					fromName: self.codeFrom,
					toName: self.codeTo,
					departureDate: $("ddate").value, //'MM/DD/YYYY' format
					returnDate: $("rdate").value, //'MM/DD/YYYY'
					oneWay: (($("flightType_oneway").checked)?true:false), // true or false
					passengers: $("tickets").value // int
				}
			};

			var otbookingbox = new OT.BookingBox(self.ids.ot, flightBookingOptions, displayOptions, trackingTags);
			
		} else if (this.searchtype == "hotels"){
			var trackingTags = { tags: 'Site='+this.webtitle+';Type=hotel;City='+$("city_hotel").value.replace(/,/g, " ")};
			var rooms = $("rooms").options[$("rooms").selectedIndex].value;
			var adults = $("adults").options[$("adults").selectedIndex].value;
			var hotelBookingOptions = {
			location: {
				 name: $("city_hotel").value,
				 checkInDate: $("checkin").value,
				 checkOutDate: $("checkout").value,
				 guests: adults, // int
				 rooms: rooms // int
			 } 
			};
			var otbookingbox = new OT.BookingBox(self.ids.ot, hotelBookingOptions, displayOptions, trackingTags);
		}

		this.user_login();
		this.tracking();
		if ($('introtext'))
		{
			new Effect.Fade('introtext', {speed:0.05, afterFinishInternal:function()
			{
				if($('introtext'))$('introtext').style.display = "none";
				if ($('resultlayer-txt')) $('resultlayer-txt').show();
				$("resultlayer").show();
				if ($('advertiser-layer')) $('advertiser-layer').style.display = "inline";
				if ($('other-sites')) $('other-sites').show();
			}
			});
		}
		else
		{
			$('searchform_layer').style.display = "none";
			$('step2box').style.display = "block";
			if ($('resultlayer-txt')) $('resultlayer-txt').show();
			$("resultlayer").show();
			if ($('resultlayer-txt')) $('resultlayer-txt').style.display = "block";
		}
	},
	
	display_ew: function(results)
	{
		this.display_simple(results);
	},
	
	validate: function(form)
	{
		var msg = "";
		var obj = form.elements;
		for (var i=0; i<obj.length; i++)
		{
			var name = obj[i].name;
			var value = obj[i].value;
			if (this.searchtype == "flights"){
				if (name == "to" && value == "") msg += "- Please enter an Arrival city or Airport Code\n";
				else if (name == "from" && value == "") msg += "- Please enter a Destination city or Airport Code\n";
			} else if (this.searchtype == "hotels"){
				if (name == "city" && value == "") msg += "- Please enter an Airport Name / Code or City\n";
			} else if (this.searchtype == "hostels"){
				if (name == "city" && value == "") msg += "- Please enter an Airport Name / Code or City\n";
			} else if (this.searchtype == "cars"){
				if (name == "to" && value == "") msg += "- Please enter an Arrival city or Airport Code\n";
				else if (name == "from" && value == "") msg += "- Please enter a Destination city or Airport Code\n";
			} else if (this.searchtype == "cruises"){
			} else if (this.searchtype == "vacations"){
				if (name == "to" && value == "") msg += "- Please enter an Arrival city or Airport Code\n";
				else if (name == "from" && value == "") msg += "- Please enter a Destination city or Airport Code\n";
			}
		}
		
		if (msg != "")// || !this.valid)
		{ 
			if (msg == "")
			{ 
				msg = "- One of your locations is invalid"; 
			} 
			this.trace(msg); 
			return false;
		}
		else 
		{ 
			this.formsent = true;
			this.display(false);
			return true ;
		}
	},
	
	check_hotel_value: function(id, act, val)
	{
		if (val == "") return(false);
		var self = this;
		new Ajax.Request("/search/validate_hotel.php", {
		method: 'post',
		postBody: '&action='+act+'&value='+val+'&id='+id,
			onFailure: function() {  },
			onComplete: function(transport)
			{
				var response = transport.responseText;
				var json = response.evalJSON();
				if (json.state == "valid")
				{ 
					self.valid = true; 
					if ($(id).className != "pinputt") $(id).className = "pinputt";
					$(id).value = json.full; 
				}
				else
				{
					self.valid = false; 
					var msg = "- Please enter a Valid Airport Name / Code or City\n";
					self.trace(msg);
					$(id).value = "";
					if ($(id).className != "invalid") $(id).className = "invalid";
				}
				return self.valid;
			}
		});
	},
	
	check_value: function(id, act, val)
	{
		if (this.no_validate) return true;
		if (val == "") return(false);
		if (id == "to_cars" && val == "(same as pick-up)") return(true);
		
		var self = this;
		var url = '/search/validate.php?new=1';
		if (this.provider == 'bw') url += "&bw=1";
		
		new Ajax.Request(url, {
		method: 'post',
		postBody: '&action='+act+'&value='+val+'&id='+id,
			onFailure: function() {  },
			onComplete: function(transport)
			{
				var response = transport.responseText;
				
				var json = response.evalJSON();
				if (json.state == "valid")
				{ 
					self.valid = true; 
					if ($(id).className != "pinputt") $(id).className = "pinputt";
					if (self.searchtype == "cars" || self.searchtype == "flights" || self.searchtype == "vacations")
					{
						if (act == "location")
						{
							if (!dclicked)
							{
								if (self.searchtype == "flights"){
									$(id).value = json.code;
								} else {
									$(id).value = json.code;
								}
							}
							else dclicked = false;
						}
						if (json.code != "")
						{
							if (id == "to_flight") self.codeTo = json.code;
							else if (id == "from_flight") self.codeFrom = json.code;
							
						}
					} else {
						if (act == "location" || act == "city" && (id != "city_hostels")) $(id).value = json.full; 
					}
				}
				else
				{
					self.valid = false; 
					var msg = "";
					if (self.searchtype == "flights"){
						if (id == "to_flight") msg += "- Please enter a Valid Arrival city or Airport Code\n";
						else if (id == "from_flight") msg += "- Please enter a Valid Destination city or Airport Code\n";
					} else if (self.searchtype == "hotels"){
						if (id == "city_hotel") msg += "- Please enter a Valid Airport Name / Code or City\n";
					} else if (self.searchtype == "hostels"){
						if (id == "city_hostels") msg += "- Please enter a Valid Airport Name / Code or City\n";
					} else if (self.searchtype == "cars"){
						if (id == "to_cars") msg += "- Please enter a Valid Arrival city or Airport Code\n";
						else if (id == "from_cars") msg += "- Please enter a Valid Destination city or Airport Code\n";
					} else if (self.searchtype == "cruises"){
					} else if (self.searchtype == "vacations"){
						if (id == "to_vacation") msg += "- Please enter a Valid Arrival city or Airport Code\n";
						else if (id == "from_vacation") msg += "- Please enter a Valid Destination city or Airport Code\n";
					}
					
					if (msg != "") self.trace(msg);
					$(id).value = "";
					if ($(id).className != "invalid") $(id).className = "invalid";
				}
				return this.valid;
			}
		});
	},
	
	user_login: function()
	{
		if (!this.use_login) return;
		return false;
		
		if (email == "") __window_login(0);
		if (user_city == "")
		{
			new Ajax.Request("/actions.php", {method: 'post',postBody: '&email='+email+'&city='+this.codeFrom});
			user_city = this.codeFrom;
		}
	},
	
	setTravelZoo: function()
	{
		var to = this.codeTo;
		var from = this.codeFrom;
		if (!$('heading_object')) return (true);
		if (to != "" && to.length == 3 && from != "" && from.length == 3)
		{
			if (!$('TZF'))
			{
				var trav = '<iframe src="about:blank" id="TZF" name="TZF" width="510" height="85" scrolling="no" marginheight="0" marginwidth="0" frameborder="0"></iframe>';
				$('heading_object').innerHTML = trav;
			}
			//var url = "http://oascentral.travelzoo.com/RealMedia/ads/adstream_sx.ads/Yellowpages.travel/results@Top?O="+from+"&D="+to;
			var url = "/includes/doFlightDeals.php?from="+from+"&to="+to;
			$('TZF').src = url;
		}
	}
};


function checkValue(a,b,c){ YPS.check_value(a,b,c); }
function checkValueHotel(a,b,c){ YPS.check_hotel_value(a,b,c); }