function html_entity_decode(str)
      {
      try
      {
      var tarea=document.createElement('textarea');
      tarea.innerHTML = str; return tarea.value;

      tarea.parentNode.removeChild(tarea);
      }
      catch(e)
      {
      //for IE add <div id="htmlconverter" style="display:none;"></div> to the page
      document.getElementById("htmlconverter").innerHTML = '<textarea id="innerConverter">' + str + '</textarea>';
      var content = document.getElementById("innerConverter").value;
      document.getElementById("htmlconverter").innerHTML = "";
      return content;
      }
 }
var SimpleSearch = {
	init_text: "Type What you are looking for. eg: Fridge, TV, Car etc..",
	val: "",
	errors: "",
	cache: {},
	last_search: null,
	init: function(){
		this.form = $("horizontalForm");
		this.search = $("sss");
		this.distance = $("distance");
		this.errors_box = $("errors-box");
		if(!this.form || !this.search){
			return false;
		}
		this.form.onsubmit = this.submit.bind(this);
		this.val = this.search.getValue();
		this.search.observe("focus", function(){
			if(this.val.length == 0 || this.val == this.init_text){
				this.search.setValue("");
			}
		}.bind(this));
		this.search.observe("blur", function(){
			this.val = this.search.getValue();
			if(this.val.length == 0){
				this.search.setValue(this.init_text);
			}
		}.bind(this));
		this.postcode.init();
		this.last_search = this.search.getValue();
		new PeriodicalExecuter(function(){
			var val = this.search.getValue();
			if(val.length < 15 && val.length > 0 && val != this.init_text && val != this.last_search){
				this.last_search = val;
				if(!this.cache[val]){
					var params = {
							"side"	: "index",
							"cl"  	: "index",
							"act" 	: "get-suggestions",
							"q"		: val
						};
					var distance = this.distance.getValue();
					if(distance != "nationwide" && parseInt(distance) > 0){
						if(this.postcode.val.length > 0 && this.postcode.val != this.postcode.init_text){
							params["postcode"] = this.postcode.val;
							params["distance"] = distance;
						}
					}
					new Ajax.Request("index.php",{
						method: 'post',
						parameters: params,
						onComplete: function(response){
							var js = "";
							eval("js="+response.responseText);
							if(js){
								this.cache[val] = js;
								this.renderSuggestions(js);
							}
						}.bind(this)
					});
				}else{
					this.renderSuggestions(this.cache[val]);
				}
			}
		}.bind(this), 0.5);

		this.search.observe("keydown", this.onKeyPress.bindAsEventListener(this));
		//this.search.observe("mousemove", this.onKeyPress.bindAsEventListener(this));
	},
	mouseSelectEntry: function(el){
		el.className = "bold";
		this.selectEntry();
	},
	selectEntry: function(){
		this.active = false;
		 if(this.table){
		 	var childs = this.table.childNodes;
			var val = "";
			if(childs.length > 0){
				for(var i =0; i < childs.length; i++){
					if(childs[i].className == "bold"){
						val = childs[i].firstChild.innerHTML;
					}
				}
			}
			if(val.length > 0){
		 		this.search.value = html_entity_decode(val);
		 		this.last_search = val;
		 		if(this.table && typeof this.table.remove == "function"){
					this.table.remove();
					this.table = null;
					this.active = null;
				}
			}
		 }
	},
	onKeyPress: function(event){
		if(true || this.active){
			switch(event.keyCode) {
		       case Event.KEY_TAB:
		       case Event.KEY_RETURN:
			     this.selectEntry();
				 Event.stop(event);
		         return false;
		       case Event.KEY_ESC:
		         if(this.table){
			        this.table.remove();
		         	this.table = null;
		         	this.active = null;
		         }
				 this.active = null;
		         Event.stop(event);
		         return;
		       case Event.KEY_UP:
		         this.markPrevious();
		         Event.stop(event);
		         return;
		       case Event.KEY_DOWN:
		         this.markNext();
		         Event.stop(event);
		         return;
		      }
		}
	},
	markNext: function(){
		if(!this.table){
			var val = this.search.getValue();
			if(!this.cache[val]){
				return false;
			}
			this.renderSuggestions(this.cache[val]);
		}
			var childs = this.table.childNodes;
			if(childs.length > 0){
				var first = true;
				var val = "";
				for(var i =0; i < childs.length; i++){
					if(childs[i].className == "bold"){
						if(childs[i+1]){
							childs[i].className = "";
							val = childs[i+1].firstChild.innerHTML;
							childs[i+1].className = "bold";
							i++;
						}
						first = false;
					}
				}
				if(first && childs[0]){
					childs[0].className = "bold";
				}
				if(val.length > 0){
					this.last_search = val;
			 		this.search.value = html_entity_decode(val);
				}
			}
	},
	markPrevious: function(){
		if(this.table){
			var childs = this.table.childNodes;
			if(childs.length > 0){
				var val = "";
				for(var i =0; i < childs.length; i++){
					if(childs[i].className == "bold"){
						if((i-1) >=0 && childs[i-1]){
							childs[i].className = "";
							val = childs[i-1].firstChild.innerHTML;
							childs[i-1].className = "bold";
						}
					}
				}
				if(val.length > 0){
					this.last_search = val;
			 		this.search.value = html_entity_decode(val);
				}
			}
		}
	},
	renderSuggestions: function(js){
		if(this.table && typeof this.table.remove == "function"){
			this.table.remove();
			this.table = null;
			this.active = null;
		}
		this.table = new Element("table", {"id": "completeTable", "cellspacing": "0", "cellpadding": "0", "border": "0"});
		var d = js["suggestions"];
		var cat = js["category"];
		if(!this.old_action){
			this.old_action = this.form.action;
		}
		if(cat && cat["category"] && cat["parent"]){
			this.form.action = js_url+js["location"]+"/"+cat["parent"]+"/"+cat["category"]+"/";
			//this.form.method = "post";
		}else{
			this.form.action = this.old_action;
			//this.form.method = "get";
		}
		if(d && d.size() > 0){
			for(var i=0; i < 10; i++){
				if(d[i] && d[i]){
					var tr = new Element("tr", {"style": "cursor: pointer;"});
					tr.appendChild(new Element("td").update(d[i]));
					tr.observe("click", this.mouseSelectEntry.bind(this, tr));
					tr.observe("mouseover", function(__el){
						__el.className = "bold";
					}.bind(this, tr));
					tr.observe("mouseout", function(__el){
						__el.className = "";
					}.bind(this, tr));
					if(i==0){
						tr.appendChild(new Element("td").update("Suggestions"));
					}else if(i == 9 || !d[i+1]){
						tr.appendChild(new Element("td").appendChild(new Element("a", {"style": "cursor: pointer;"}).update("Close").observe("click", function(){
							if(this.table && typeof this.table.remove == "function"){
								this.table.remove();
								this.table = null;
								this.active = null;
							}
						}.bind(this))));
					}else{
						tr.appendChild(new Element("td").update(""));
					}
					this.table.appendChild(tr);
				}
			}
			$("suggestions").appendChild(this.table);
			this.active = true;
		}else{
			//var td = new Element("td").update("No matches found");
			//this.table.appendChild(new Element("tr").appendChild(td));
		    //$("suggestions").appendChild(this.table);
			//this.active = null;
		}
	},
	submit: function(){
		var svalue = this.search.getValue();
		if(svalue.length == 0 || svalue == this.init_text){
			this.addError("Search keywords should be entered.");
		}

		var distance = this.distance.getValue();
		if(distance != "nationwide" && parseInt(distance) > 0){
			if(this.postcode.val.length == 0 || this.postcode.val == this.postcode.init_text){
				this.addError("Post code should be entered.");
				if(this._checkErrors()){
					this.form.submit();
				}
			}else{
				var requestData = new Ajax.Request("index.php", {
					method: 'post',
					parameters: {
						"side"		: "index",
						"cl"  		: "index",
						"act" 		: "check-postcode",
						"postcode"	: this.postcode.val
					},
					onComplete: function(response){
						var js = "";
						eval("js="+response.responseText);
						if(!js || js != "exist"){
							this.addError("Incorrect Postcode.");
						}
						if(this._checkErrors()){
							this.form.submit();
						}
					}.bind(this)
				});
			}
		}else if(distance != "nationwide" && this.postcode.val.length > 0 && this.postcode.val != this.postcode.init_text){
			this.addError("Distance should be selected.");
			if(this._checkErrors()){
				this.form.submit();
			}
		}else{
			if(this._checkErrors()){
				this.postcode.field.setValue("");
				this.form.submit();
			}
		}
		return false;
	},
	_checkErrors: function(){
		if(this.errors.length == 0){
			return true;
		}else{
			this.errors_box.update(this.errors);
			this.errors = "";
			return false;
		}
	},
	addError: function(error){
		this.errors += " " + error;
	},
	postcode: {
		init_text: "Postcode",
		val: "",
		init: function(){
			this.field = $("postcodes");
			if(!this.field){
				return false;
			}
			this.val = this.field.getValue();
			//this.init_text = this.field.getValue();
			this.field.observe("focus", function(){
				if(this.val.length == 0 || this.val == this.init_text){
					this.field.setValue("");
				}
			}.bind(this));
			this.field.observe("blur", function(){
				this.val = this.field.getValue();
				if(this.val.length == 0){
					this.field.setValue(this.init_text);
				}
			}.bind(this));
		}
	}
};
Main.onReady(SimpleSearch.init, SimpleSearch);