function camposForm(nomeForm)
{


/**************************************************
* Autor: José Cláudio Medeiros de Lima
* Data: 17/12/2007
* Objetivos: Pega todos os campos de um formulário, seus valores 
      e junta tudo em um formato de querystring, 
      por exemplo: var1=valor1var2=valor2
* Parâmetros: nomeForm - O nome do formulário qualquer.
**************************************************/


var buff = [];
var contador = 0; //viabiliza a contagem de laços do for,  
      //pois sem ele dá "undefined" nos checkboxes
      //ou radiobuttons que não estão marcados
nf = document.getElementById(nomeForm);	  
for (i=0;i<nf.length;i++) 
{
	
  campo = nf.elements.item(i);
  if(campo.type=="checkbox" || campo.type=="radio")
  {
   if(campo.checked)
   {
    buff[contador] = campo.name + "=" + campo.value;
    contador ++; //incrementa 1 ao contador
   }
  }
  else
  {
   buff[contador] = campo.name + "=" + campo.value;
   contador++;
  }
}
return buff.join("&");


}


var maiornivel = 10;
var cont = 0;
function criaXMLHttp() {
if (typeof XMLHttpRequest != "undefined")
return new XMLHttpRequest();
else if (window.ActiveXObject){
var versoes = ["MSXML2.XMLHttp.5.0","MSXML2.XMLHttp.4.0", "MSXML2.XMLHttp.3.0","MSXML2.XMLHttp", "Microsoft.XMLHttp"];
}
for (var i = 0; i < versoes.length; i++){
try{
return new ActiveXObject(versoes[i]);
}catch (e) {}
}
throw new Error("Seu browser nao suporta AJAX");
}


function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}



function criaXMLHttp() {
if (typeof XMLHttpRequest != "undefined")
return new XMLHttpRequest();
else if (window.ActiveXObject){
var versoes = ["MSXML2.XMLHttp.5.0","MSXML2.XMLHttp.4.0", "MSXML2.XMLHttp.3.0","MSXML2.XMLHttp", "Microsoft.XMLHttp"];
}
for (var i = 0; i < versoes.length; i++){
try{
return new ActiveXObject(versoes[i]);
}catch (e) {}
}
throw new Error("Seu browser nao suporta AJAX");
}
function abreAjax(id, metodo, url) {
	
	

	var divInfo = document.getElementById(id);
	var url = url;
	var tempo = new Date();
	url = url+'&tempo='+tempo.getTime();
	divInfo.innerHTML = '<p><img src="images/loading.gif" \> Carregando ...</p>';
	
	var XMLHttp = criaXMLHttp();
	var parametros = null;
	
	if((metodo == "GET") || (metodo == "get")) {
		XMLHttp.open("GET", url, true);
		parametros = null;
	} else { 
		parametros = "mytextarea1=" + encodeURI( document.getElementById("mytextarea1").value ) + "&mytextarea2=" + encodeURI( document.getElementById("mytextarea2").value );
		XMLHttp.open("POST", url, true);
		XMLHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1");
		XMLHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		XMLHttp.setRequestHeader("Content-length", parametros.length);
		XMLHttp.send(parametros);
	} 
	
	XMLHttp.onreadystatechange = function () {
	if (XMLHttp.readyState == 4)
		if (XMLHttp.status == 200){
			divInfo.innerHTML = (unescape(XMLHttp.responseText));
		} else {
			divInfo.innerHTML = "Um erro ocorreu" + XMLHttp.statusText;
		}
	};
XMLHttp.send(parametros);

}


function abreAjaxBox(id, metodo, url, parametros) {
	
	

	var divInfo = document.getElementById(id);
	var url = url;
	url = url
	
	divInfo.innerHTML = '<p><img src="images/loading.gif" \> Carregando ...</p>';
	
	var XMLHttp = criaXMLHttp();
	
	if((metodo == "GET") || (metodo == "get")) {
		XMLHttp.open("GET", url, true);
	} else { 
		XMLHttp.open("POST", url, true);
		XMLHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1");
		XMLHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		XMLHttp.setRequestHeader("Content-length", parametros.length);
	} 
	
	if (url == 'envia_pedido.asp') {
		document.getElementById('comentario3').style.display = 'none';
	}
	
	XMLHttp.onreadystatechange = function () {
	if (XMLHttp.readyState == 4)
		if (XMLHttp.status == 200){
				
				divInfo.innerHTML = '<div class="confirmacao"><p>'+(unescape(XMLHttp.responseText))+'</p><p><input name="btok" type="image"  src="images/bt_ok.gif" onclick="document.getElementById(\''+id+'\').innerHTML=\'\';" /></p></div>';
				if (url == 'envia_pedido.asp') {
					divInfo.innerHTML = '<div class="confirmacao"><p>'+(unescape(XMLHttp.responseText))+'</p><p><input name="btok" type="image"  src="images/bt_ok.gif" onclick="document.getElementById(\''+id+'\').innerHTML=\'\'; document.getElementById(\''+comentario3+'\').style.display = \''+block+'\';" /></p></div>';
				}
			} else {
				divInfo.innerHTML = '<div class="alerta"><p>Ocorreu um erro: ' + XMLHttp.statusText+ '</p><p><input name="btok" type="image"  src="images/bt_ok.gif" onclick="document.getElementById(\''+id+'\').innerHTML=\'\';" /></p></div>';
				if (url == 'envia_pedido.asp') {
					divInfo.innerHTML = '<div class="alerta"><p>Ocorreu um erro: ' + XMLHttp.statusText+ '</p><p><input name="btok" type="image"  src="images/bt_ok.gif" onclick="document.getElementById(\''+id+'\').innerHTML=\'\'; document.getElementById(\''+comentario3+'\').style.display = \''+block+'\';" /></p></div>';
				}
			}
	};
XMLHttp.send(parametros);

}




function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}


function cepAjax(id, metodo, url, meuform2, retorno) {
	
	
	meuform = eval('document.'+ meuform2);
	var divInfo = document.getElementById(id);
	var divInfo2 = document.getElementById(retorno);
	divInfo.style.visibility = "visible";
	divInfo.style.position = "relative";
	divInfo.innerHTML = "Processando..."; 
	

	document.getElementById('endereco').value = 'Buscando Dados ...';
	document.getElementById('bairro').value = 'Buscando Dados ...';
	document.getElementById('complemento').value = 'Buscando Dados ...';
	
	url = url; //Localização do documento de pesquisa.
    
	//divInfo.innerHTML = '<p><img src="images/loading.gif" \> Carregando ...</p>';
	
	var XMLHttp = criaXMLHttp();
	
	
	if((metodo == "GET") || (metodo == "get")) {
		XMLHttp.open("GET", url, true);
	} else { 
		XMLHttp.open("POST", url, true);
 		XMLHttp.setRequestHeader('encoding','ISO-8859-1'); //Define o conjunto de caracteres usado no documento.
    	XMLHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded' ); //Prepara o servidor para receber dados POST.
		XMLHttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
		XMLHttp.setRequestHeader('Content-length', strData.length ); //Comunica ou servidor o tamanho dos dados a serem enviados.
		XMLHttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
		XMLHttp.setRequestHeader("Pragma", "no-cache");
	} 
	
	XMLHttp.onreadystatechange = function () {
	if (XMLHttp.readyState == 4)
		if (XMLHttp.status == 200){
		
			var result = unescape(escape(XMLHttp.responseText)); //Armazena a resposta XML.
			var pesquisa = result.split(','); //Captura todas as respostas nas Tags <retorno>
			divInfo.innerHTML = '';
			divInfo.style.visibility = "hidden";
			divInfo.style.position = "absolute"
			var endereco = '';
			var complemento = '';
			var bairro = '';
			var cidade = '';
			var V_bairro = '';
			var V_cidade = '';
			var V_estado = '';
			var estado = '';
				

			document.getElementById('endereco').value = '';
			document.getElementById('complemento').value = '';
			document.getElementById('cidade').selectedIndex = 0;
			document.getElementById('estado').selectedIndex = 0;
			document.getElementById('bairro').value = '';
			if (pesquisa.length > 0){
				if (pesquisa[0] == ''){		
					document.getElementById('cep').focus();
					divInfo.style.visibility = "visible";
					divInfo.style.position = "relative";
					divInfo.innerHTML = "CEP não encontrado.";
					
					document.getElementById('cidade').innerHTML = "";
					document.getElementById('cidade').appendChild(create_opcao("|---------"));
					
				}else{
						
				
						endereco = trim(pesquisa[0],' '); //Endereco
						complemento = trim(pesquisa[1],' '); //Complemento
						V_bairro = trim(pesquisa[2],' '); //Bairro				
						bairro = trim(pesquisa[3],' '); //Bairro
						V_cidade = trim(pesquisa[4],' ');//Codigo Cidade
						cidade = trim(pesquisa[5],' '); //Cidade
						V_estado = trim(pesquisa[6],' ');//Codigo Estado
						estado = trim(pesquisa[7],' '); //Estado
					
						cidadeAjax('cidade', 'GET', 'buscaCIDADES.asp?busca='+V_estado, meuform2, 'retornoEstado',V_cidade);
						
						minhasopcoes = document.getElementById('estado').options;
						for (var i = 0; i<minhasopcoes.length;i++){
							if (document.getElementById('estado').options[i].value == V_estado){
								document.getElementById('estado').selectedIndex = i;
								
							}
						}	
						
				
						
						
						document.getElementById('num').focus(); //Passa o focu para o campo de pesquisa.
						document.getElementById('endereco').value = endereco
						document.getElementById('bairro').value  = bairro
						document.getElementById('complemento').value = complemento				
				}		
			}
			
		} else {
			divInfo.innerHTML = "Um erro ocorreu" + XMLHttp.statusText;
		}
	};

	XMLHttp.send(null);
}




function create_opcao(stirngdados) { //Cria um novo elemento OPTION.
	var minhacidae = stirngdados.split('|')
    var new_opcao = document.createElement("option"); //Cria um OPTION.
    var texto = document.createTextNode(minhacidae[1]); //Cria um texto.
    new_opcao.setAttribute("value",minhacidae[0]); //Adiciona o atributo de valor a nova opção.
    new_opcao.appendChild(texto); //Adiciona o texto a OPTION.
    return new_opcao; // Retorna a nova OPTION.
}

function cidadeAjax(id, metodo, url, meuform, retorno,marcar) {
	
	meuform = eval('document.'+ meuform);
	var divInfo = document.getElementById(id);
	url = url; //Localização do documento de pesquisa.
    
	var retorno = document.getElementById(retorno);
	retorno.style.visibility = "visible";
	retorno.style.position = "relative";
	retorno.innerHTML = "Buscando Dados..."; 
	
	var XMLHttp = criaXMLHttp();
	
	
	if((metodo == "GET") || (metodo == "get")) {
		XMLHttp.open("GET", url, true);
	} else { 
		XMLHttp.open("POST", url, true);
 		XMLHttp.setRequestHeader('encoding','ISO-8859-1'); //Define o conjunto de caracteres usado no documento.
    	XMLHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded' ); //Prepara o servidor para receber dados POST.
		XMLHttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
		XMLHttp.setRequestHeader('Content-length', strData.length ); //Comunica ou servidor o tamanho dos dados a serem enviados.
		XMLHttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
		XMLHttp.setRequestHeader("Pragma", "no-cache");
	} 
	
	XMLHttp.onreadystatechange = function () {
	if (XMLHttp.readyState == 4)
		if (XMLHttp.status == 200){
		
			var result = unescape(escape(XMLHttp.responseText)); //Armazena a resposta XML.
			var pesquisa = result.split(','); //Captura todas as respostas nas Tags <retorno>
			
			
			divInfo.innerHTML = ""; //Apaga o conteúdo da listbox.

			for (var i = 0; i < pesquisa.length; i++) { //Populariza a listbox
				new_opcao = create_opcao(pesquisa[i]);
				divInfo.appendChild(new_opcao);
			}
				
				
			if (marcar != ''){
						minhasopcoes = divInfo.options;
						for (var i = 0; i<minhasopcoes.length;i++){
							if (divInfo.options[i].value == marcar){
								divInfo.selectedIndex = i;
							}
						}	
			}	
				
			document.getElementById('bairro').focus();
			
			retorno.style.visibility = "hidden";
			retorno.style.position = "absolute"
			retorno.innerHTML = ""; 	
				
		
		} else {
				retorno.style.visibility = "visible";
				retorno.style.position = "relative";
				retorno.innerHTML = "Um erro ocorreu" + XMLHttp.statusText; 
		}
	};

	XMLHttp.send(null);
}



/***************** CRIADOR DE POPUPS *********************************/
function novoPop(nome,tamanho,titulo,url, comrefresh, metodo, nomeForm) {

	if ( metodo === undefined )
		metodo= 'GET';
	
   var retorno = null;
   var stringpop = '';
   var stringpopINI = '';
   var stringpopFIM = '';


	maiornivel = maiornivel*50;
	stringpopINI +='<div id="'+nome+'" class="popup" style="z-index: '+maiornivel+';">';
		stringpop +='<img src="images/preto.gif" class="popupimage" />';
		stringpop +='<div class="box2 '+tamanho+'">';
		stringpop +=      '<div class="topo">';
		stringpop +=            '<div class="left"></div>';
		stringpop +=            '<h2>'+titulo+'</h2>';
		stringpop +=            '<div class="acctionbar">';
		stringpop +=				'<img src="images/bt_close.gif" onclick="fechaDIV2(\''+nome+'\');" />';
		stringpop +=			'</div>';
		stringpop +=        '<div class="right"></div>';
		stringpop +=    '</div>';
		stringpop +=      '<div id="content_'+nome+'" class="content">';
		stringpop +=      '</div>';            
		stringpop +=    '<div class="rodape">';
		stringpop +=        '<div class="left"></div>';
		stringpop +=        '<div class="right"></div>';
		stringpop +=    '</div>';
		stringpop +='</div>';
	stringpopFIM +='</div>';
 	
	if(document.getElementById(nome)==null){
 		stringpop = (stringpopINI + stringpop + stringpopFIM);
		destino = document.body;
		destino.innerHTML =  stringpop  + destino.innerHTML;

	}else{
		destino = document.getElementById(nome);
		destino.innerHTML =  stringpop;
	}
	
	document.getElementById(nome).style.visibility = "visible"; 

		
	if ( nomeForm === undefined )
			retorno = abreAjax('content_'+nome, metodo, url, null);
	else
			retorno = abreAjax('content_'+nome, metodo, url, null);
	
	
	
}




function abreDIV2(param) {
   document.getElementById(param).style.visibility = "visible";
   document.getElementById(param).style.display = "block"; 
}
function fechaDIV2(param) {
   document.getElementById(param).style.visibility = "hidden";
   document.getElementById(param).style.display = "none";
   document.getElementById(param).innerHTML = '';
}