	var xmlHttpAjax, objDestinoAjax;
	var htmlDestinoAjax = 'htmlConteudo';
	var objDestinoIndiceInicioAjax = 0;
	var objMultDestinoAjax = null;
	var objMultDestinoIndiceInicioAjax = null;
	
	function carregarUrl(urlDestino,metodo){ 
		metodo = (metodo.length>0)?metodo:"GET";
		xmlHttpAjax = getXmlHttpObject();
		if(xmlHttpAjax!=null){
			url = urlDestino + "&sid=" + Math.random();
			htmlDestinoAjax = ajaxAutoTestImpl('htmlDestinoAjax');
			document.getElementById(htmlDestinoAjax).innerHTML = 'Aguarde, carregando...';
			xmlHttpAjax.onreadystatechange = stateChangedHTML;
			xmlHttpAjax.open(metodo,url,true);
			xmlHttpAjax.send(null);
		}else{
			alert('Seu navegador não tem suporte a AJAX');
		}
	}
	
	function carregaSelect(_obj){
		_form = _obj.form;
		_metodo = (_form.method.length>0)?_form.method:"GET";
		_url = ajaxAutoTestImpl(_obj);
		if(_url!=null){
			_obj.disabled = true;
			xmlHttpAjax = getXmlHttpObject();
			if(xmlHttpAjax!=null){
				_tmpOption = objDestinoAjax.options[0];
				objDestinoAjax.options[0] = new Option('Aguarde, carregando...','');
				url = _url + "&sid=" + Math.random();
				xmlHttpAjax.onreadystatechange = stateChangedSELECT;
				xmlHttpAjax.open(_metodo,url,true);
				xmlHttpAjax.send(null);
				_obj.disabled = false;
				if(objDestinoIndiceInicioAjax>0)
					objDestinoAjax.options[0] = _tmpOption;
				else if(objMultDestinoIndiceInicioAjax[0]>0)
					objDestinoAjax.options[0] = _tmpOption;
			}else{
				objDestinoAjax.options[0] = new Option('Seu navegador não tem suporte a AJAX','');
			}
		}
	}

	function stateChangedHTML(){ 
		if (xmlHttpAjax.readyState==4 || xmlHttpAjax.readyState=="complete") { 
			responseText = xmlHttpAjax.responseText;
			responseText = responseText.replace(/\+/g," ");
			responseText = unescape(responseText);

			document.getElementById(htmlDestinoAjax).innerHTML = responseText;
		} 
	} 

	function stateChangedSELECT(){ 
		if (xmlHttpAjax.readyState==4 || xmlHttpAjax.readyState=="complete") { 
			responseText = xmlHttpAjax.responseText;
			responseText = responseText.replace(/\+/g," ");
			responseText = unescape(responseText);
			m_result = responseText.split('\n');
			if(objMultDestinoAjax!=null && isArray(objMultDestinoAjax) && objMultDestinoIndiceInicioAjax!=null && isArray(objMultDestinoIndiceInicioAjax)){
				if(objMultDestinoAjax.length == objMultDestinoIndiceInicioAjax.length){
					for(y=0;y<objMultDestinoAjax.length;y++){
						objDestinoAjax 				= objMultDestinoAjax[y];
						objDestinoIndiceInicioAjax 	= objMultDestinoIndiceInicioAjax[y];
						addResultSelect(m_result);
					}
					objDestinoAjax 				= objMultDestinoAjax[0];
					objDestinoIndiceInicioAjax 	= objMultDestinoIndiceInicioAjax[0];
				}else{
					alert('Você deve definir os múltiplos índices de início');
				}
			}else{
				addResultSelect(m_result);
			}
			
		} 
	} 

	function addResultSelect(m_result){
		objDestinoAjax.length = objDestinoIndiceInicioAjax;
		x=objDestinoIndiceInicioAjax;
		for(i=0;i<m_result.length;i++){
			if(m_result[i].length>0){
				m_campo = m_result[i].split(',');
				objDestinoAjax.options[x] = new Option(m_campo[1],m_campo[0]);
			}
			x++;
		}
	}

	function getXmlHttpObject(){ 
		var objXMLHttp=null;
		if (window.XMLHttpRequest){
			objXMLHttp=new XMLHttpRequest();
		}else if (window.ActiveXObject){
			objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		return objXMLHttp;
	}

	function isObject(a){
	     return (typeof a == 'object' && !!a) || isFunction(a);
	}
	function isArray(a){
		return isObject(a) && a.constructor == Array;
	}
	function isFunction(a) 
	{
    	return typeof a == 'function';
	}

	function ajaxAutoTestImpl(_obj){
		if(isObject(_obj)){
			switch(_obj.name){
				case 'id_cidade':
					objDestinoAjax = _form.id_bairro;
					return _url = 'ajax/carregaSelectBairroByCidade.php?id_cidade='+_obj.value;
					break;
			}
		}else if (_obj!=undefined && _obj!=null && _obj!='' && _obj.length>0){
			switch(_obj){
				case 'htmlDestinoAjax':
					return 'htmlConteudo';
				case 'msgNenhumRegistroHTML':
					return 'nenhum registro';
				case 'msgNenhumRegistroSELECT':
					return '-----------';
			}
		}
		return null;
	}
