function buscaPreview()
{
	this.init = function(id) {
		this.id = id;
		this.disabled = false;
		this.keyword = false;
	}
	this.createXmlRequest = function() {
		var xmlhttp;
		/*@cc_on
		@if (@_jscript_version >= 5)
			try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				xmlhttp = false;
			}
			}
		@else
		xmlhttp = false;
		@end @*/
		if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
			try {
			xmlhttp = new XMLHttpRequest();
			xmlhttp.overrideMimeType("text/xml"); 
			} catch (e) {
			xmlhttp = false;
			}
		}
		return xmlhttp;
	}
	this.query = function(k, onStart, onResult, onEmpty) {
		if(this.disabled) return;
		k = k.trim();
		if((this.keyword == true && k.length == 0) || (this.keyword == false && k.length < 3)) {
			onEmpty();
			return;
		}
		var url = '/Visual/SearchOnKey.aspx?q=' + myEncode(k) + '&k=' + (this.keyword == true ? '1' : '0');
		var xmlHttp = this.createXmlRequest();
		xmlHttp.open('GET', url, true);
		xmlHttp.onreadystatechange = function() {	
			if(xmlHttp.readyState == 4) {
				//xmlHttp.close;
				var xml = xmlHttp.responseXML;
				xml.setProperty("SelectionLanguage", "XPath");
				var res = '';
				var items = xml.selectNodes('//NewDataSet/Table');
				if(items.length > 0) {
					onStart();
					for(var i = 0; i < items.length; i++) {
						var item = items[i];
						var tipo = gnv('tipo', item);
						if(tipo == 'p') 
							res = gnv('produto', item);
						else if(tipo == 'r')
							res = gnv('referencia', item);
						else if(tipo == 'k')
							res = gnv('valor', item);
						onResult(res, (tipo == 'k' ? res : gnv('codigo', item)));
						//c.innerHTML = '<a href="javascript:alert("' + res + '">' + res + '</a>';
					}
				}
				else onEmpty();
			}
		}
		xmlHttp.send(null);
	}
}
function gnv(f, item)
{
	var o = item.getElementsByTagName(f).item(0);
	if(o != null && o.firstChild != null) return o.firstChild.nodeValue;
	return '';
}
function myEncode(s)
{
	if(typeof(encodeURI) == 'function') {
		return encodeURI(s);
	}
	return escape(s);
}
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ""); };
	
