﻿var CompanySearchForm = {
	_Element: null,
	AppendElement: function(___Element) {
		this._Element.appendChild(___Element);
	},
	initialize: function(_Keywords, _County, _City, _Address, _Number) {
		this._Element = document.getElementById("CompanySearchForm");
		
		// Keywords.
		this.Keywords._Element = this._Element.elements[0];
		this.Keywords._Element.value = _Keywords;
		this.Keywords.SuggestionList = new SuggestionList.List(this, this.Keywords, "suggestions-1");
		
		// County.
		this.County.Element[0] = this._Element.elements[1];
		this.County.Element[0].value = _County;
		this.County.Element[1] = this._Element.elements[2];
		this.County.SuggestionList = new SuggestionList.List(this, this.County, "suggestions-2");
		this.County.Load();
		
		// City.
		this.City.Element[0] = this._Element.elements[3];
		this.City.Element[0].value = _City;
		this.City.Element[1] = this._Element.elements[4];
		this.City.SuggestionList = new SuggestionList.List(this, this.City, "suggestions-3");
		this.City.Load();
		
		// Address.
		this.Address.element = this._Element.elements[5];
		this.Address.element.value = _Address;
		
		// Number.
		this.Number.element = this._Element.elements[6];
		this.Number.element.value = _Number;
	},
	Keywords: {
		_Element: null,
		_Timer: null,
		SuggestionList: null,
		ObjectPath: function() {
			return "CompanySearchForm.Keywords";
		},
		Value: function(__Value) {
			if(__Value == null) {
				return this._Element.value;
			} else {
				this._Element.value = __Value;
			}
		},
		OnBlur: function() {
			this._Timer = setTimeout("CompanySearchForm.Keywords.SuggestionList.Hide();CompanySearchForm.Keywords._Timer = null;", 1000);
		},
		OnFocus: function() {
			if(this._Timer != null) {
				clearTimeout(this._Timer);
				
				this._Timer = null;
			}
			
			if(this._Element.value.length > 1) {
				this.SuggestionList.Load("/!Controls/CompanySearchForm.ascx.Keywords.aspx?keywords=" + this._Element.value);
				
				if(this.SuggestionList.Items.length > 0) {
					this.SuggestionList.Show();
				}
			}
		},
		OnKeyUp: function(__Event) {
			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("/!Controls/CompanySearchForm.ascx.Keywords.aspx?keywords=" + this._Element.value);
					
					if(this.SuggestionList.Items.length > 0) {
						this.SuggestionList.Show();
					}
				} else {
					this.SuggestionList.Hide();
				}
			}
		}
	},
	County: {
		Element: new Array(null, null),
		_Timer: null,
		SuggestionList: null,
		HttpRequest: null,
		ObjectPath: function() {
			return "CompanySearchForm.County";
		},
		Value: function(__Value) {
			if(__Value == null) {
				return this.Element[0].value;
			} else {
				this.Element[0].value = __Value;
			}
		},
		Toggle: function() {
			if(this.Element[1].style.display == "block") {
				this.Element[1].style.display = "none";
			} else {
				this.Element[1].style.display = "block";
			}
		},
		Load: function() {
			this.HttpRequest = createXmlHttpObject();
			eval("this.HttpRequest.onreadystatechange = function() { " + this.ObjectPath() + ".OnLoad(); }");
			this.HttpRequest.open("GET", "/!Controls/CompanySearchForm.ascx.County.aspx", true);
			this.HttpRequest.setRequestHeader("Content-Type", "charset=UTF-8");
			this.HttpRequest.send(null);
		},
		OnLoad: function() {
			if(this.HttpRequest.readyState == 4) {
				this.Element[1].options.length = 0;
				
				var ___Options = this.HttpRequest.responseText.split("\n");
				var ___Option = null;
				
				___Option = document.createElement("option");
				___Option.value = "";
				___Option.text = "Ingen";
				
				try {
					this.Element[1].add(___Option, null)
				} catch(e) {
					this.Element[1].add(___Option);
				}
				
				if(___Options.length > 1) {
					for(___I = 0; ___I < ___Options.length - 1; ___I++) {
						___Option = document.createElement("option");
						___Option.value = ___Options[___I].toLowerCase();
						___Option.text = ___Options[___I];
						
						try {
							this.Element[1].add(___Option, null)
						} catch(e) {
							this.Element[1].add(___Option);
						}
					}
				}
				
				if(this.Element[0].value.length > 0) {
					Select.SelectedValue(this.Element[1], this.Element[0].value.toLowerCase());
				}
			}
		},
		OnBlur: function() {
			this._Timer = setTimeout("CompanySearchForm.County.SuggestionList.Hide();CompanySearchForm.County._Timer = null;", 1000);
		},
		OnFocus: function() {
			if(this._Timer != null) {
				clearTimeout(this._Timer);
				
				this._Timer = null;
			}
			
			if(this.Element[0].value.length > 0) {
				this.SuggestionList.Load("/!Controls/CompanySearchForm.ascx.County.aspx?county=" + this.Element[0].value);
				
				if(this.SuggestionList.Items.length > 0) {
					this.SuggestionList.Show();
				}
			}
		},
		OnKeyUp: function(__Event) {
			if(__Event.keyCode == 38) {
				this.SuggestionList.Previous();
			} else if(__Event.keyCode == 40) {
				this.SuggestionList.Next();
			} else {
				if(this.Element[0].value.length > 0) {
					this.SuggestionList.Load("/!Controls/CompanySearchForm.ascx.County.aspx?county=" + this.Element[0].value);
					
					if(this.SuggestionList.Items.length > 0) {
						this.SuggestionList.Show();
					}
					
					this.Element[1].selectedIndex = 0;
					
					Select.SelectedValue(this.Element[1], this.Element[0].value.toLowerCase());
				} else {
					this.SuggestionList.Hide();
				}
			}
			
			CompanySearchForm.City.Load();
		},
		OnChange: function() {
			this.Element[0].value = Select.SelectedValue(this.Element[1], null);
			
			CompanySearchForm.City.Load();
		}
	},
	City: {
		Element: new Array(null, null),
		_Timer: null,
		SuggestionList: null,
		ObjectPath: function() {
			return "CompanySearchForm.City";
		},
		Value: function(__Value) {
			if(__Value == null) {
				return this.Element[0].value;
			} else {
				this.Element[0].value = __Value;
			}
		},
		Toggle: function() {
			if(this.Element[1].style.display == "block") {
				this.Element[1].style.display = "none";
			} else {
				this.Element[1].style.display = "block";
			}
		},
		Load: function() {
			this.HttpRequest = createXmlHttpObject();
			eval("this.HttpRequest.onreadystatechange = function() { " + this.ObjectPath() + ".OnLoad(); }");
			this.HttpRequest.open("GET", "/!Controls/CompanySearchForm.ascx.City.aspx?county=" + CompanySearchForm.County.Value(), true);
			this.HttpRequest.setRequestHeader("Content-Type", "charset=UTF-8");
			this.HttpRequest.send(null);
		},
		OnLoad: function() {
			if(this.HttpRequest.readyState == 4) {
				this.Element[1].options.length = 0;
				
				var ___Options = this.HttpRequest.responseText.split("\n");
				var ___Option = null;
				
				___Option = document.createElement("option");
				___Option.value = "";
				___Option.text = "Ingen";
				
				try {
					this.Element[1].add(___Option, null)
				} catch(e) {
					this.Element[1].add(___Option);
				}
				
				if(___Options.length > 1) {
					for(___I = 0; ___I < ___Options.length - 1; ___I++) {
						___Option = document.createElement("option");
						___Option.value = ___Options[___I].toLowerCase();
						___Option.text = ___Options[___I];
						
						try {
							this.Element[1].add(___Option, null)
						} catch(e) {
							this.Element[1].add(___Option);
						}
					}
				}
				
				if(this.Element[0].value.length > 0) {
					Select.SelectedValue(this.Element[1], this.Element[0].value.toLowerCase());
				}
			}
		},
		OnBlur: function() {
			this._Timer = setTimeout("CompanySearchForm.City.SuggestionList.Hide();CompanySearchForm.City._Timer = null;", 1000);
		},
		OnFocus: function() {
			if(this._Timer != null) {
				clearTimeout(this._Timer);
				
				this._Timer = null;
			}
			
			if(this.Element[0].value.length > 0) {
				this.SuggestionList.Load("/!Controls/CompanySearchForm.ascx.City.aspx?county=" + CompanySearchForm.County.Value() + "&city=" + this.Element[0].value);
				
				if(this.SuggestionList.Items.length > 0) {
					this.SuggestionList.Show();
				}
			}
		},
		OnKeyUp: function(__Event) {
			if(__Event.keyCode == 38) {
				this.SuggestionList.Previous();
			} else if(__Event.keyCode == 40) {
				this.SuggestionList.Next();
			} else {
				if(this.Element[0].value.length > 0) {
					this.SuggestionList.Load("/!Controls/CompanySearchForm.ascx.City.aspx?county=" + CompanySearchForm.County.Value() + "&city=" + this.Element[0].value);
					
					if(this.SuggestionList.Items.length > 0) {
						this.SuggestionList.Show();
					}
					
					this.Element[1].selectedIndex = 0;
					
					Select.SelectedValue(this.Element[1], this.Element[0].value.toLowerCase());
				} else {
					this.SuggestionList.Hide();
				}
			}
		},
		OnChange: function() {
			this.Element[0].value = Select.SelectedValue(this.Element[1], null);
		}
	},
	Address: {
		element: null
	},
	Number: {
		element: null
	}
};
