﻿function EmptySearch(Link) {
	var box = document.getElementById("ctl00_searchField");
	var X = document.getElementById("X");
	
	if(box == null) {
		box = document.getElementById("ctl00_ctl00_searchField");
	}
	document.getElementById("suggestions-1").setAttribute("style","display:none");
	box.value = "";
	X.setAttribute("style","display:none");
}
function ShowX(Backspace) {
	var box = document.getElementById("ctl00_searchField");
	var X = document.getElementById("X");
	
	if(box == null) {
		box = document.getElementById("ctl00_ctl00_searchField");
	}
	
	var string = box.value;
	
	if(string.length <= 1 && Backspace) {
		X.setAttribute("style","display:none");
	}
	else {
		X.setAttribute("style","display:block");
	}
}
function CheckLength() {
	var box = document.getElementById("ctl00_searchField");
	var X = document.getElementById("X");
	
	if(box == null) {
		box = document.getElementById("ctl00_ctl00_searchField");
	}
	
	var string = box.value;
	
	if(string.length == 0) {
		X.setAttribute("style","display:none");
	}
	else {
		X.setAttribute("style","display:block");
	}
}
function createXmlHttpObject() {
	var XMLHTTP;
	
	try {
		XMLHTTP = new XMLHttpRequest();
		return XMLHTTP;
	}
	catch(e) {
		try {
			XMLHTTP = new ActiveXObject("Msxml2.XMLHTTP");
			return XMLHTTP;
		}
		catch(e) {
			try {
				var_XMLHTTP = ActiveXObject("Microsoft.XMLHTTP");
				return XMLHTTP;
			}
			catch(e) {
				alert("Din webbläsare stödjer ej AJAX.");
				return null;
			}
		}
	}
}
var Keywords = {
		initialize: function() {
			var box = document.getElementById("ctl00_searchField");
			
			if(box == null) {
				box = document.getElementById("ctl00_ctl00_searchField");
			}
			
			this._Element = box;
			this.SuggestionList = new SuggestionList.List(document.getElementById("searchField"),this, "suggestions-1");
		},
		_Element: null,
		_Timer: null,
		SuggestionList: null,
		AppendElement: function(___Element) {
			this._Element = ___Element;
		},
		ObjectPath: function() {
			return "Keywords";
		},
		Value: function(__Value) {
			if(__Value == null) {
				return this._Element.value;
			}
			else {
				this._Element.value = __Value;
			}
		},
		OnKeyDown : function(event) {
			if(event.keyCode == 8 || event.keyCode == 13) {
				ShowX(true);
			}
			else{
				ShowX(false);
			}
		},
		OnBlur: function() {
			this._Timer = setTimeout("Keywords.SuggestionList.Hide();Keywords._Timer = null;", 500);
		},
		OnFocus: function() {
			if(this._Timer != null) {
				clearTimeout(this._Timer);
				
				this._Timer = null;
			}
			
			if(this._Element.value.length > 1) {
				this.SuggestionList.Load("/Fixer.aspx?Keywords=" + this._Element.value);
				
				if(this.SuggestionList.Items.length > 0) {
					this.SuggestionList.Show();
				}
			}
		},
		OnKeyUp: function(__Event) {
			CheckLength();
			if(__Event.keyCode == 38) {
				this.SuggestionList.Previous();
			}
			else if(__Event.keyCode == 40) {
				this.SuggestionList.Next();
			}
			else {
				if(this._Element.value.length > 1) {
					this.SuggestionList.Load("/Fixer.aspx?Keywords=" + this._Element.value);
					
					if(this.SuggestionList.Items.length > 0) {
						this.SuggestionList.Show();
					}
				} else {
					this.SuggestionList.Hide();
				}
			}
		}
};
var SuggestionList = {
	List: function(__Parent, __Source, __Class) {
		// Methods.
		this.AppendElement = function(___Element) {
			this._Element.appendChild(___Element);
		};
		
		this.ObjectPath = function() {
			return this._Source.ObjectPath() + ".SuggestionList";
		};
		
		this.Hide = function() {
			this._Element.style.display = "none";
		};
		
		this.Show = function() {
			this._Element.style.display = "block";
		};
		
		this.Select = function(__Item) {
			if(this._SelectedItem > -1) {
				this.Items[this._SelectedItem].Deselect();
			}
			
			this._SelectedItem = __Item;
			this.Items[this._SelectedItem].Select();
			
			this._Source.Value(this.Items[this._SelectedItem].Value());
		};
		
		this.Next = function() {
			if(this._SelectedItem < this.Items.length - 1) {
				this.Select(this._SelectedItem + 1);
			}
		};
		
		this.Previous = function() {
			if(this._SelectedItem > 0) {
				this.Select(this._SelectedItem - 1);
			}
		};
		
		this.Load = function(__Url) {
			this._HttpRequest = createXmlHttpObject();
			eval("var ___Function = function() { " + this.ObjectPath() + ".OnLoad(); }");
			this._HttpRequest.onreadystatechange = ___Function;
			this._HttpRequest.open("GET", __Url, true);
			this._HttpRequest.setRequestHeader("Content-Type", "charset=UTF-8");
			this._HttpRequest.send(null);
		};
		
		this.OnLoad = function() {
			if(this._HttpRequest.readyState == 4) {
				this._Element.innerHTML = "";
				this.Items = new Array();
				
				var ___Items = this._HttpRequest.responseText.split(";");
				
				if(___Items.length > 1) {
					for(___I = 0; ___I < ___Items.length - 1; ___I++) {
						this.Items[___I] = new SuggestionList.Item(this, ___I, ___Items[___I]);
					}
					
					this.Show();
				} else {
					this.Hide();
				}
			}
		};
		
		// Members.
		this._Source = __Source;
		this._Element = document.createElement("div");
		this._Element.className = __Class;
		this._Element.setAttribute("Id",__Class);
		this._HttpRequest = null;
		this._SelectedItem = -1;
		this.Items = new Array();
		
		__Parent.appendChild(this._Element);
	},
	Item: function(__Parent, __Id, __Value) {
		// Methods.
		this.ObjectPath = function() {
			return this._Parent.ObjectPath() + ".Items[" + __Id + "]";
		};
		
		this.Value = function() {
			return this._Value;
		}
		
		this.Select = function() {
			this._Element.className = "Selected";
		};
		
		this.Deselect = function() {
			this._Element.className = "";
		};
		
		this.OnClick = function() {
			this._Parent.Select(this._Id);
		};
		
		// Members.
		this._Parent = __Parent;
		this._Id = __Id;
		this._Element = document.createElement("p");
		this._Element.innerHTML = "<a href=\"javascript:void(0);\" onclick=\"javascript:" + this.ObjectPath() + ".OnClick();\">" + __Value + "</a>";
		this._Value = __Value;
		
		__Parent.AppendElement(this._Element);
	}
};

