if(typeof(surveyMonster)=="undefined")	
	surveyMonster={};


if(typeof(surveyMonster.widgets)=="undefined")
	surveyMonster.widgets={};


surveyMonster.widgets.autoComplete = function(url,inputId,containerId,forceSelection)
{

	var mySearch = new YAHOO.widget.DS_XHR("includes/api.asp",["network","networkName","province","country","networkName"]);
	mySearch.responseType = YAHOO.widget.DS_XHR.TYPE_XML;
	mySearch.queryMatchContains = true;
	mySearch.scriptQueryParam="q";
	mySearch.scriptQueryAppend = "function=" + url;
	
	mySearch.parseResponse = function (sQuery,oResponse,oParent){
	}
	
	var oAutoComp = new YAHOO.widget.AutoComplete(inputId,containerId,mySearch);
		oAutoComp.useIFrame=true;
		oAutoComp.forceSelection=forceSelection||false;
	
	var position = function(autoComplete){
	
		var input = this.getInputEl();
		var container = this.getContainerEl();
		var region = YAHOO.util.Dom.getRegion(input);
		
		container.style.top=region.bottom +'px';
		container.style.left=region.left +'px';
		container.style.width='500px';
		
	};
	
	oAutoComp.resultTypeList = false; 
	
	oAutoComp.containerExpandEvent.subscribe(position);
	
	oAutoComp.formatResult = function(oResultData, sQuery, sResultMatch) {
	
		var html = "<div style='padding:2px 0px;border-bottom:1px dotted #EEEEEE'><div style='font-weight:bold'>" + oResultData.networkName + "</div>";
		
		if(oResultData.country && oResultData.province){
			html += "<div style='padding-left:15px;color:#999999'>"+ oResultData.province + ", " + oResultData.country + "</div>";
		}else if(oResultData.country){
			html += "<div style='padding-left:15px;color:#999999'>" + oResultData.country + "</div>";
		}
		
		return html + "</div>";		
    };

	oAutoComp.disable = function(){
		YAHOO.util.Event.purgeElement(this._elTextbox);	
	}
	
	oAutoComp.enable = function(){	
		YAHOO.util.Event.addListener(this._elTextbox,"keypress",this._onTextboxKeyPress,this);
		YAHOO.util.Event.addListener(this._elTextbox,"keyup",this._onTextboxKeyUp,this);
		YAHOO.util.Event.addListener(this._elTextbox,"keydown",this._onTextboxKeyDown,this);
		YAHOO.util.Event.addListener(this._elTextbox,"focus",this._onTextboxFocus,this);
		YAHOO.util.Event.addListener(this._elTextbox,"blur",this._onTextboxBlur,this);
	}
	
	return oAutoComp;
};
