// JavaScript Document
if(!friz.autoText) friz.autoText = {};

friz.autoText = function(){
	var ddobjects = new Array();
	var currObj = -1;
	var dropdown = document.createElement("span");
	
	dropdown = document.createElement("span");
	dropdown.className = "friz_autotext_dropdown";
	dropdown.silent=true;
	dropdown.responseType = "xml";
	
	dropdown.update = function(value){
		if(value != ""){
			var url = ddobjects[currObj].url.replace("@value",value.replace(" ","%20"));
			friz.xmlhttp.call(url,'',receiveData,this);
		} else {
			this.style.visibility = "";
			this.removeAll();
		}
	}
	
	dropdown.receiveData=function(response){
		this.removeAll();
		//this.style.height="auto";
		var xmlopt = response.getElementsByTagName("option");
		if(xmlopt.length > 0){
			for(var i = 0; i < xmlopt.length; i++){
					if(xmlopt[i].firstChild.data){
					var a = document.createElement("a");
					a.setAttribute("href","#");
					var value = xmlopt[i].firstChild.data;
					value = value.replace(/^\s*/,"");
					value = value.replace(/\s*$/,"");
					a.appendChild(document.createTextNode(value));
					friz.addEvent(a,"click",updateText);
					this.appendChild(a);
				}
			}
			
			this.style.marginTop = ddobjects[currObj].offsetHeight+"px";
			//this.style.width = ddobjects[currObj].offsetWidth + "px";
			this.style.minWidth = ddobjects[currObj].offsetWidth + "px";
			this.style.visibility = "visible";
	
		} else {
			this.style.visibility = "";
		}
	}
	
	dropdown.removeAll=function(){
		while(this.childNodes[0]){
			this.removeChild(this.childNodes[0]);
		}
	}
	
	
	function add(params){
		if(typeof params.obj == "object"){
			var txtbox = params.textbox;
		} else {
			txtbox = document.getElementById(params.textbox);
		}
		
		txtbox.url = params.url;
		txtbox.setAttribute("autocomplete","off");
		friz.addEvent(txtbox,"keyup",updateAutoText);
		friz.addEvent(txtbox,"blur",blurAutoText);
		txtbox.autotextid = ddobjects.length;
		ddobjects.push(txtbox);
	}
	
	function updateText(e){
		if(!e) e = window.event;
		var a = e.srcElement ? e.srcElement : e.target;
		a=friz.findParentByTag(a,"a");
		var value = a.firstChild.data;
		friz.autoText.setValue(value);
		e.preventDefault ? e.preventDefault() : e.returnValue=false;
	}
	
	function receiveData(response,context){
			dropdown.receiveData(response);
	}
	
	function blurAutoText(e){
		if(!e) e=window.event;
		var txtbox = e.srcElement ? e.srcElement : e.target;
		txtbox = friz.findParentByTag(txtbox,"input");
		dropdown.timer = setTimeout("friz.autoText.clear('"+txtbox.id+"')",200);
	}
	
	function updateAutoText(e){
		if(!e) e=window.event;
		var key = e.which ? e.which : e.keyCode;
		
		if((key!=null) && (key!=0) && (key!=9) && (key!=13) && (key!=27)){
			var keychar = String.fromCharCode(key);
			keychar = keychar.toLowerCase();
			var txtbox = e.srcElement ? e.srcElement : e.target;
			txtbox = friz.findParentByTag(txtbox,"input");
			friz.autoText.currentObject(txtbox.autotextid);
			friz.autoText.dropdown.update(txtbox.value);
		}
	}
	
	return {
		add:add,
		clear:function(){
			clearTimeout(dropdown.timer);
			dropdown.style.visibility="";
			dropdown.removeAll();
		},
		
		currentObject:function(index){
			currObj = index;
			if(ddobjects[currObj].previousSibling != dropdown){
				ddobjects[currObj].parentNode.insertBefore(dropdown,ddobjects[currObj]);
			}
		},
		
		setValue:function(value){
			ddobjects[currObj].value = value;
			ddobjects[currObj].focus();
			dropdown.style.visibility = "";
			dropdown.removeAll();
		},
		
		dropdown:dropdown
	};
}();

if(!friz.autoText.updater) friz.autoText.updater = {};

friz.autoText.updater = function(){
	
	function add(params){
		var parent = document.getElementById(params["parentid"]);
		if(!parent.childid){
			parent.childid = new Array();
		}
		parent.childid.push(params["childid"])
		var child = document.getElementById(params["childid"]);
		child.updaterurl = params["url"];
		child.update = function(value){
			if(value !='' && this.value==''){
				var url = this.updaterurl.replace("@value",value.replace(" ","%20"));
				var context = { responseType:"xml",silent:true,id:this.id };
				friz.xmlhttp.call(url,'',receiveData,context);
			}
		}
		
		child.receiveData = function(response){
			var xmlopt = response.getElementsByTagName("option");
			if(xmlopt.length==1 && this.value==''){
				this.value = xmlopt[0].firstChild.data;
			}
		}
		
		friz.addEvent(parent,"blur",updateChild);
	}
		
	function receiveData(response,context){
		document.getElementById(context.id).receiveData(response);
	}
	
	function updateChild(e){
		if(!e) e = window.event;
		var src = e.srcElement ? e.srcElement : e.target;
		while(!src.childid){
			src = src.parentNode;
		}
		
		if(src.timer){
			clearTimeout(src.timer);
		}

		src.timer = setTimeout("friz.autoText.updater.recallUpdate('" + src.id + "')",200);
	}
	
	return {
		add:add,
		
		recallUpdate:function(id){
			var parent = document.getElementById(id);
			clearTimeout(parent.timer);
			for(var i = 0; i < parent.childid.length; i++){
				document.getElementById(parent.childid[i]).update(parent.value);
			}
		}
	};
}();