   var strMsgErroPrincipal = null;

   function abrePopup(url){
     janela=window.open(url,'janela','width=600,height=500,top=100,left=100,resizable=no,scrollbars=yes')
   }
   
  function CheckAll() { 
    for (var i=0;i<document.forms[0].elements.length;i++) {
      var x = document.forms[0].elements[i];
      if (x.type == 'checkbox') { 
        x.checked = document.forms[0].checkbox.checked;
      } 
    }
  } 
  
  function ClearCheckAll(objcheck) {
    if (objcheck.checked) {
	} else {
	  document.forms[0].checkbox.checked = false;
	}
  }

  function DeleteAllCheck(cancela) { 
    var lista = '0';
    for (var i=0;i<document.forms[0].elements.length;i++) {
      var x = document.forms[0].elements[i];
      if ((x.type == 'checkbox') && (x.name != 'checkbox')) { 
        if (x.checked) {
		    lista = lista + ',' + x.value; 
		}
      } 
    }
	if (lista == '0') { 
	  alert('Nenhum serviço selecionado.'); 
	} else { 
	  if (confirm('Clique em OK para excluir os serviços selecionados.')) { 
		window.location=cancela+"?lista="+lista;
		//return lista; 
	  }
	}
  } 

   function ValidaForm(frm) {

	var intQtdInput = frm.elements.length;
	var alertMsg = "";
	
	for (i=0; i < intQtdInput; i++) {

		var objCampo = frm.elements[i];

		if (objCampo.type == "text" || objCampo.type == "textarea" || objCampo.type == "password") {

			if (objCampo.value == "") {

				if (objCampo.attributes["required"] != null) {

					if (objCampo.attributes["required"].value == "true") {
						var strRequiredMSG = objCampo.attributes["requiredmsg"].value;
						alertMsg += ("\t - " + strRequiredMSG + "\n");
					}
				}
			} else {

				if (objCampo.attributes["mascara"] != null && objCampo.attributes["errormsg"] != null) {

					if (objCampo.attributes["mascara"].value.search('^FNC#') != -1) {
						var strMascara = objCampo.attributes["mascara"].value.replace('FNC#', '');
						strMascara = eval(strMascara);
					}
					else { 
						var strMascara = objCampo.attributes["mascara"].value
					    if (strMascara == "CPF();") {
						   var strMascara = CPF(objCampo.value); 
						} else {
						   var strMascara = objCampo.attributes["mascara"].value; 
						}
					}

					var strErrorMSG = objCampo.attributes["errormsg"].value;
					var erroBoolean = false;
					//alert("mascara:" + strMascara + "\n MsgErro:" + strErrorMSG);
					switch (strMascara) 
					{
						case true :
							break;
							
						case false:
							alertMsg += ("\t - " + strErrorMSG + "\n");
							break;
							
						default :
							if (objCampo.value.search(strMascara) == -1)
								alertMsg += ("\t - " + strErrorMSG + "\n");
					}
				}
			}
		}
		else {
			if (objCampo.type == "radio"){
				var checked = false;
				
				for (var j=0; j < frm.elements[i].qtRadios; j++){
					if (frm.elements[i+j].checked) { checked = true; }
				}
				
				if (frm.elements[i].required && !checked) { 
				   alertMsg += ("\t - " + frm.elements[i].requiredmsg + "\n");
				}

			} else {
				if (objCampo.type == "select-one"){

					if (objCampo.options[objCampo.selectedIndex].value == ""){

						if (objCampo.attributes["required"] != null) {

							if (objCampo.attributes["required"].value == "true")
								alertMsg += ("\t - " + objCampo.attributes["requiredmsg"].value + "\n");	
						}
					}
				}
			}
		}
	}//for

	if(alertMsg.length>0) {

		if (strMsgErroPrincipal != null) {
			alert(strMsgErroPrincipal + "\n\n" + alertMsg);
			return false;
		}
		else {
			alert("Por favor:\n\n" + alertMsg);
			return false;
		}
	}
	else
		return true;
   }

   function SomenteNumeros() { return "^\\d+$"; }

   function SomenteLetras() { return "^[\\D]+$"; }

   function Data() { return "^((0?[1-9]|[12]\\d)\\/(0?[1-9]|1[0-2])|30\\/(0?[13-9]|1[0-2])|31\\/(0?[13578]|1[02]))\\/(19|20)\\d{2}$"; }

//   function Email() { return "^[A-Za-z0-9]+([._+]{1}[a-zA-Z0-9]+)*[@]{1}[A-Za-z0-9]+([._]{1}[a-zA-Z0-9]+)*"; }
   function Email() { return "^[a-zA-Z0-9]{1}([\._a-zA-Z0-9-]+)(\.[_a-zA-Z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+){1,3}$"; }
                             
   function Telefone() { return "^\\d{4}[-]\\d{4}$"; }

   function Cep() { return "^\\d{5}[-]\\d{3}$"; }

   function Senha() { return "^([A-Z,a-z,0-9,_,-]){4,}"; }

   function CPF(cpf) {
      erro = new String;

      if (cpf.length < 11) erro += "Sao necessarios 11 digitos para verificacao do CPF! \n\n"; 

      var nonNumbers = /\D/;

      if (nonNumbers.test(cpf)) erro += "A verificacao de CPF suporta apenas numeros! \n\n"; 

      if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999"){
           erro += "Numero de CPF invalido!"
      }

      var a = [];
      var b = new Number;
      var c = 11;
  
      for (i=0; i<11; i++){
          a[i] = cpf.charAt(i);
          if (i < 9) b += (a[i] * --c);
      }

      if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }

      b = 0;
      c = 11;
 
      for (y=0; y<10; y++) b += (a[y] * c--); 

      if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }

      if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){
          erro +="Digito verificador com problema!";
      }

      if (erro.length > 0){
          return false;
      }
      return true;
   }
   
   function frmSubmit()
   {
   }

   function Dados(valor) {
      //verifica se o browser tem suporte a ajax
	  try {
         ajax = new ActiveXObject("Microsoft.XMLHTTP");
      } 
      catch(e) {
         try {
            ajax = new ActiveXObject("Msxml2.XMLHTTP");
         }
	     catch(ex) {
            try {
               ajax = new XMLHttpRequest();
            }
	        catch(exc) {
               alert("Esse browser não tem recursos para uso do Ajax");
               ajax = null;
            }
         }
      }
	  //se tiver suporte ajax
	  if(ajax) {
	     //deixa apenas o elemento 1 no option, os outros são excluídos
		 document.forms["frm"].objCidade.options.length = 1;
		 	     
		 idOpcao  = document.getElementById("opcoes");
		 
	     ajax.open("POST", "cidades.php", true);
		 ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		 
		 ajax.onreadystatechange = function() {
            //enquanto estiver processando...emite a msg de carregando
			if(ajax.readyState == 1) {
			   idOpcao.innerHTML = "Aguarde. Carregando...!";   
	        }
			//após ser processado - chama função processXML que vai varrer os dados
            if(ajax.readyState == 4 ) {
			   if(ajax.responseXML) {
			      processXML(ajax.responseXML);
			   }
			   else {
			       //caso não seja um arquivo XML emite a mensagem abaixo
				   idOpcao.innerHTML = "Selecione o Estado";
			   }
            }
         }
		 //passa o código do estado escolhido
	     var params = "estado=\""+valor+"\"";

         ajax.send(params);
      }
   }

   function Dados2(valor) {
      //verifica se o browser tem suporte a ajax
	  try {
         ajax = new ActiveXObject("Microsoft.XMLHTTP");
      } 
      catch(e) {
         try {
            ajax = new ActiveXObject("Msxml2.XMLHTTP");
         }
	     catch(ex) {
            try {
               ajax = new XMLHttpRequest();
            }
	        catch(exc) {
               alert("Esse browser não tem recursos para uso do Ajax");
               ajax = null;
            }
         }
      }
	  //se tiver suporte ajax
	  if(ajax) {
	     //deixa apenas o elemento 1 no option, os outros são excluídos
		 document.forms["frm"].objCidade_escola.options.length = 1;
		 	     
		 idOpcao2  = document.getElementById("opcoes2");
		 
	     ajax.open("POST", "cidades.php", true);
		 ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		 
		 ajax.onreadystatechange = function() {
            //enquanto estiver processando...emite a msg de carregando
			if(ajax.readyState == 1) {
			   idOpcao2.innerHTML = "Aguarde. Carregando...!";   
	        }
			//após ser processado - chama função processXML que vai varrer os dados
            if(ajax.readyState == 4 ) {
			   if(ajax.responseXML) {
			      processXML2(ajax.responseXML);
			   }
			   else {
			       //caso não seja um arquivo XML emite a mensagem abaixo
				   idOpcao2.innerHTML = "Selecione o Estado";
			   }
            }
         }
		 //passa o código do estado escolhido
	     var params = "estado=\""+valor+"\"";

         ajax.send(params);
      }
   }
      
   function processXML(obj){
      //pega a tag cidade
      var dataArray   = obj.getElementsByTagName("cidade");
      
	  //total de elementos contidos na tag cidade
	  if(dataArray.length > 0) {
	     //percorre o arquivo XML paara extrair os dados
         for(var i = 0 ; i < dataArray.length ; i++) {
            var item = dataArray[i];
			//contéudo dos campos no arquivo XML
			var codigo    =  item.getElementsByTagName("codigo")[0].firstChild.nodeValue;
			var descricao =  item.getElementsByTagName("descricao")[0].firstChild.nodeValue;
			
	        idOpcao.innerHTML = "Selecione uma Cidade";
			
			//cria um novo option dinamicamente  
			var novo = document.createElement("option");
			    //atribui um ID a esse elemento
			    novo.setAttribute("id", "opcoes");
				//atribui um valor
			    novo.value = codigo;
				//atribui um texto
			    novo.text  = descricao;
				//finalmente adiciona o novo elemento
				document.forms["frm"].objCidade.options.add(novo);
		 }
	  }
	  else {
	    //caso o XML volte vazio, printa a mensagem abaixo
		idOpcao.innerHTML = "Selecione o Estado";
	  }	  
   }

   function processXML2(obj){
      //pega a tag cidade
      var dataArray   = obj.getElementsByTagName("cidade");
      
	  //total de elementos contidos na tag cidade
	  if(dataArray.length > 0) {
	     //percorre o arquivo XML paara extrair os dados
         for(var i = 0 ; i < dataArray.length ; i++) {
            var item = dataArray[i];
			//contéudo dos campos no arquivo XML
			var codigo    =  item.getElementsByTagName("codigo")[0].firstChild.nodeValue;
			var descricao =  item.getElementsByTagName("descricao")[0].firstChild.nodeValue;
			
	        idOpcao2.innerHTML = "Selecione uma Cidade";
			
			//cria um novo option dinamicamente  
			var novo = document.createElement("option");
			    //atribui um ID a esse elemento
			    novo.setAttribute("id", "opcoes");
				//atribui um valor
			    novo.value = codigo;
				//atribui um texto
			    novo.text  = descricao;
				//finalmente adiciona o novo elemento
				document.forms["frm"].objCidade_escola.options.add(novo);
		 }
	  }
	  else {
	    //caso o XML volte vazio, printa a mensagem abaixo
		idOpcao2.innerHTML = "Selecione o Estado";
	  }	  
   }