/* AJAX */
var requisicoes = new Number(0);
var XMLVersion = '';
xmlConnection = function () {
	var strNav = new String('');
	var objAjax = null;
	var strMethod = new String('');
	strMethod = 'POST';
	var strParams = new String('');
	var strURL = new String('');
	var strChildName = new String('');
	var arrParamName = new Array();
	var arrParamValue = new Array();
	var numTotalParams = new Number(0);
	var xmlReturn = null;
	var textReturn = new String('');
	var thisObj = this;
	this.onComplete = function() {
	};
	this.onLoad = function() {
	};
	this.onStateChange = function() {
	};
	this.onError = function() {
		trace('Erro ao ler o xml!');
	};
	this.add = function() {
		arrParamName[numTotalParams] = arguments[0];
		arrParamValue[numTotalParams] = arguments[1];
		numTotalParams++;
	};
	this.emptyParameters = function() {
		numTotalParams = new Number(0);
		arrParamName = new Array();
		arrParamValue = new Array();
	};
	this.setURL = function() {
		strURL = arguments[0];
	};
	this.setMethod = function() {
		if ((arguments[0].toUpperCase() == 'POST') || (arguments[0].toUpperCase() == 'GET')) {
			strMethod = arguments[0].toUpperCase();
		}
	};
	this.setChildName = function() {
		strChildName = arguments[0];
	};
	this.getStatus = function() {
		return objAjax.statusText;
	};
	this.getXML = function() {
		return xmlReturn;
	};
	this.getText = function() {
		return textReturn;
	};
	this.getCountItens = function() {
		return xmlReturn.getElementsByTagName(strChildName).length;
	};
	this.getAttByName = function() {
		try {
			return xmlReturn.getElementsByTagName(strChildName)[arguments[1]].getAttribute(arguments[0]);
		} catch (e) {
			return null;
		}
	};
	this.getDataByName = function() {
		try {
			return xmlReturn.getElementsByTagName(strChildName)[arguments[0]].firstChild.data;
		} catch (e) {
			return null;
		}
	};
	this.execute = function() {
		if (requisicoes++ == 0) {
			//
		}
		strParams = '';
		for (var i = 0; i<arrParamName.length; i++) {
			if (i>0) {
				strParams += '&';
			}
			strParams += arrParamName[i]+'='+encodeURIComponent(arrParamValue[i]);
		}
		if (strMethod == 'GET') {
			objAjax.open(strMethod, strURL+'?'+strParams, true);
		} else {
			objAjax.open(strMethod, strURL, true);
		}
		with (objAjax) {
			setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			setRequestHeader("CharSet", "UTF-8");
			setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
			setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
			setRequestHeader("Pragma", "no-cache");
			onreadystatechange = function () {
				thisObj.onStateChange(objAjax.readyState);
				if (objAjax.readyState == 4) {
					thisObj.onLoad(objAjax.status);
					if (objAjax.status == 200) {
						xmlReturn = objAjax.responseXML;
						textReturn = objAjax.responseText;
						thisObj.onComplete();
					} else {
						thisObj.onError(objAjax.status, objAjax.statusText);
					}
					if (--requisicoes == 0) {
						//
					}
				}
			};
			if (strMethod == 'GET') {
				send(null);
			} else {
				send(strParams);
			}
		}
	};
	this.create = function() {
		if (window.ActiveXObject) {
			strNav = 'ie';
			if (XMLVersion == '') {
				var AXO = ['Msxml2.XMLHTTP.8.0','Msxml2.XMLHTTP.7.0','Msxml2.XMLHTTP.6.0','Msxml2.XMLHTTP.5.0','Msxml2.XMLHTTP.4.0','Msxml2.XMLHTTP.3.0','Msxml2.XMLHTTP.2.0','Msxml2.XMLHTTP','Microsoft.XMLHTTP'];
				for (var i = 0; i < AXO.length; i++) {
					try {
						objAjax = new ActiveXObject(AXO[i]);
						XMLVersion = AXO[i];
						break;
					} catch (e) {
						continue;
					}
					if (i >= (AXO.length-1)) {
						objAjax = null;
						return;
					}
				};
			} else {
				try {
					objAjax = new ActiveXObject(XMLVersion);
				} catch (e) {
					objAjax = null;
					return;
				}
			}
		} else if (window.XMLHttpRequest) {
			XMLVersion = 'XMLHttpRequest';
			objAjax = new XMLHttpRequest();
			strNav = 'ff';
		} else {
			objAjax = null;
			return;
		}
		return true;
	};
};
/* Função para carregar itens em objetos do tipo select */
selectLoader = function () {
	var xml = new xmlConnection();
	var thisObj = this;
	this.onLoad = function() {
	};
	this.load = function(strObj, strList, strValue, cache) {
		cache = cache || false;
		if (xml.create()) {
			xml.setURL(strList);
			xml.setMethod('GET');
			if (cache != false) {
				xml.add('nocache', nocache());
			}
			xml.onComplete = function() {
				this.setChildName('item');
				$(strObj).innerHTML = '';
				if (this.getCountItens()>1) {
					var linha = document.createElement("OPTION");
					$(strObj).appendChild(linha);
					linha.value = '';
					linha.text = 'Selecione...';
					linha.style.color = '#999999';
					var index = 0;
					for (var j = 0; j<this.getCountItens(); j++) {
						linha = document.createElement("OPTION");
						$(strObj).appendChild(linha);
						linha.value = this.getAttByName('id', j);
						linha.text = this.getAttByName('nome', j);
						if (strValue) {
							if (linha.value == strValue) {
								linha.selected = true;
								index = (j+1);
							}
						}
					}
					$(strObj).selectedIndex = index;
					$(strObj).disabled = false;
				} else if (this.getCountItens() == 1) {
					linha = document.createElement("OPTION");
					$(strObj).appendChild(linha);
					linha.value = this.getAttByName('id', 0);
					linha.text = this.getAttByName('nome', 0);
					linha.selected = true;
					$(strObj).disabled = true;
				} else {
					linha = document.createElement("OPTION");
					$(strObj).appendChild(linha);
					linha.value = '';
					linha.text = 'Sem dados';
					linha.selected = true;
					$(strObj).disabled = true;
				}
				thisObj.onLoad(($(strObj).disabled) ? false : true);
			};
			xml.execute();
		}
	};
};