function criaMascara(_RefObjeto, _Modelo){

var valorAtual = _RefObjeto.value;
var valorNumerico = '';
var nIndexModelo = 0;
var nIndexString = 0;
var valorFinal = '';
var adicionarValor = true;
 

// limpa a string valor atual para verificar
// se todos os caracteres são números
for (i=0;i<_Modelo.length;i++){
  if (_Modelo.substr(i,1) != '#'){
    valorAtual = valorAtual.replace(_Modelo.substr(i,1),'');
}}
 
// verifica se todos os caracteres são números
for (i=0;i<valorAtual.length;i++){
  if (!isNaN(parseFloat(valorAtual.substr(i,1)))){
    valorNumerico = valorNumerico + valorAtual.substr(i,1);
}}
 
// aplica a máscara ao campo informado usando
// o modelo de máscara informado no script
for (i=0;i<_Modelo.length;i++){
 
  if (_Modelo.substr(i,1) == '#'){
    if (valorNumerico.substr(nIndexModelo,1) != ''){
      valorFinal = valorFinal + valorNumerico.substr(nIndexModelo,1);
      nIndexModelo++;nIndexString++;
    }
      else {
        adicionarValor = false;
  }}
 
    else {
      if (adicionarValor && valorNumerico.substr(nIndexModelo,1) != ''){
      valorFinal = valorFinal + _Modelo.substr(nIndexString,1)
      nIndexString++;
    }}
}
 
_RefObjeto.value = valorFinal
 
}

function validaFormulario(form){
	//alert(form.length);
	var msg = "";
	for (i=0;i<form.length;i++){
		//var obg = form[i].obrigatorio;
		var obg = form[i].getAttribute('obrigatorio');
		if (obg==1){

			//msg += "\n dddd";
			if (form[i].value == ""){
			var nome = form[i].id
				//msg += "\n as";
				alert("Por favor, preencha o campo: "+ nome)
				//alert("Por favor, preencha todos os campos");
				form[i].focus();
				return false;
				break;
			}
		}
	}
	//alert(msg); return false;
	return true;
}

function cep() {
	CEP = document.getElementById("Cep").value;
		if(CEP != "") {
		cidade = document.getElementById("cidade");
		var con = new XHConn();
		var resposta = function (ajaxCEP) {
			var Endereco = ajaxCEP.responseText.replace(/\+/g, " ").split('name="endereco" value="');
			document.getElementById("Endereco").value = Endereco[1].substr(0, Endereco[1].indexOf('"'));
			
			Endereco = Endereco[1].split('name="bairro" value="');
			document.getElementById("Bairro").value = Endereco[1].substr(0, Endereco[1].indexOf('"'));
			
			Endereco = Endereco[1].split('name="cidade" value="');
			document.getElementById("Cidade").value = Endereco[1].substr(0, Endereco[1].indexOf('"'));

		}
		if(!con.connect("ajax_cep.php", "POST", 'cepDest='+CEP+'&invoca=1;',resposta)) {
			alert("Não foi possível conexao, atualize a página e tente novamente.");
		}
	}
}

function abreSub(id) {
	if (document.getElementById("abre_sub_"+id).style.display=="none") {
		document.getElementById('abre_sub_'+id).style.display="block";
	} else {
		document.getElementById('abre_sub_'+id).style.display="none";
	}
} 

function XHConn() {
	
  var xmlhttp, bComplete = false;
  
  var browser = navigator.appName;
  browser = browser.substring(0,1);  

  if(browser == 'M') // IEs :/
  	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")
  else // Firefox, Netscape, Opera, Chrome
  	xmlhttp = new XMLHttpRequest();
  
  /*try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { try { xmlhttp = new XMLHttpRequest(); }
  catch (e) { xmlhttp = false; }}}*/
  
  if (!xmlhttp) return null;
  this.connect = function(sURL, sMethod, sVars, fnDone)
  {
    if (!xmlhttp) return false;
    bComplete = false;
    sMethod = sMethod.toUpperCase();

    try {
      if (sMethod == "GET")
      {
        xmlhttp.open(sMethod, sURL+"?"+sVars, true);
        sVars = "";
      }
      else
      {
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type",
          "application/x-www-form-urlencoded");
      }
      xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && !bComplete)
        {
          bComplete = true;
          fnDone(xmlhttp);
        }};
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}

