function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function validaForm1() {
	 var digits="0123456789";
	 var temp;
     var d = document.frmCadastro;
           //validar nome
           if (d.txtRazaoSocial.value == ""){
                     alert("O campo razão social deve ser preenchido!");
                     d.txtRazaoSocial.focus();
                     return false;
           }
		   if (d.txtEndereco.value == ""){
                     alert("O campo endereço deve ser preenchido!");
                     d.txtEndereco.focus();
                     return false;
           }
		   if (d.txtBairro.value == ""){
                     alert("O campo bairro deve ser preenchido!");
                     d.txtBairro.focus();
                     return false;
           }
		   if (d.txtCidade.value == ""){
                     alert("O campo cidade deve ser preenchido!");
                     d.txtCidade.focus();
                     return false;
           }
		   if (d.txtContato.value == ""){
                     alert("O campo contato deve ser preenchido!");
                     d.txtContato.focus();
                     return false;
           }
		   if (d.txtEstado.value == ""){
                     alert("O campo estado deve ser preenchido!");
                     d.txtEstado.focus();
                     return false;
           }
		   if (d.txtCEP.value == ""){
                     alert("O campo CEP deve ser preenchido!");
                     d.txtCEP.focus();
                     return false;
           }
		   if (d.txtTelefone.value == ""){
                     alert("O campo telefone deve ser preenchido!");
                     d.txtTelefone.focus();
                     return false;
           }
		   if (d.txtSelecao.value == ""){
                     alert("O campo Tipo de Cadastro deve ser preenchido!");
                     d.txtSelecao.focus();
                     return false;
           }
		   if (d.txtEmail.value == ""){
                     alert("O campo E-mail deve ser preenchido!");
                     d.txtEmail.focus();
                     return false;
           }
         return true;
 }
function FormataCNPJ(Campo, teclapres, Tipo){
		var tecla = teclapres.keyCode;
		
		var vr = new String(Campo.value);
		vr = vr.replace(".", "");
		vr = vr.replace(".", "");
		vr = vr.replace("-", "");

		tam = vr.length + 1;
		
		if (Tipo == 0) {
			if (tecla != 9 && tecla != 8){
				if (tam > 2 && tam < 4)
					Campo.value = vr.substr(0, 2) + '.' + vr.substr(2, tam);
				if (tam >= 6 && tam <7)
					Campo.value = vr.substr(0, 2) + '.' + vr.substr(2,3) + '.';
				if (tam >= 9 && tam <10)
					Campo.value = vr.substr(0, 2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/';
				if (tam >= 14 && tam <15)
					Campo.value = vr.substr(0, 2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(9,4) + '-';
				}
		}
		else
		{
			if (tecla != 9 && tecla != 8){
				if (tam > 3 && tam < 5)
					Campo.value = vr.substr(0, 3) + '.' + vr.substr(3, tam);
				if (tam >= 7 && tam <8)
					Campo.value = vr.substr(0, 3) + '.' + vr.substr(3,3) + '.' + vr.substr(6, tam);
				if (tam >= 10 && tam <11)
					Campo.value = vr.substr(0, 3) + '.' + vr.substr(3,3) + '.' + vr.substr(6,3) + '-' + vr.substr(9, tam);
				}
		}
	}

function FormataCEP(Campo, teclapres){
		var tecla = teclapres.keyCode;
		
		var vr = new String(Campo.value);
		vr = vr.replace(".", "");
		vr = vr.replace(".", "");
		vr = vr.replace("-", "");

		tam = vr.length + 1;
		
		if (tecla != 8 && tecla != 9){
			if (tam > 5 && tam < 7)
				Campo.value = vr.substr(0, 5) + '-' + vr.substr(6, tam);
			}
	}

function FormataINSCRICAO_MUNICIPAL(Campo, teclapres){
		var tecla = teclapres.keyCode;
		
		var vr = new String(Campo.value);
		vr = vr.replace(".", "");
		vr = vr.replace(".", "");
		vr = vr.replace("-", "");

		tam = vr.length + 1;
		
		if (tecla != 9 && tecla != 8){
			if (tam > 1 && tam < 3)
				Campo.value = vr.substr(0, 1) + '.' + vr.substr(2, tam);
			if (tam >= 7 && tam <8)
				Campo.value = vr.substr(0, 1) + '.' + vr.substr(1,5) + '-';
			}
	}

function FormataINSCRICAO(Campo, teclapres){
		var tecla = teclapres.keyCode;
		
		var vr = new String(Campo.value);
		vr = vr.replace(".", "");
		vr = vr.replace(".", "");
		vr = vr.replace("-", "");

		tam = vr.length + 1;
		
		if (tecla != 9 && tecla != 8){
			if (tam > 5 && tam < 7)
				Campo.value = vr.substr(0, 5) + '.' + vr.substr(6, tam);
			if (tam >= 8 && tam <9)
				Campo.value = vr.substr(0, 5) + '.' + vr.substr(5,2) + '.';
			if (tam >= 10 && tam <11)
				Campo.value = vr.substr(0, 5) + '.' + vr.substr(5,2) + '.' + vr.substr(7,2) + '.';
			if (tam >= 15 && tam <16)
				Campo.value = vr.substr(0, 5) + '.' + vr.substr(5,2) + '.' + vr.substr(7,2) + '.' + vr.substr(10,4) + '.';
			if (tam >= 18 && tam <19)
				Campo.value = vr.substr(0, 5) + '.' + vr.substr(5,2) + '.' + vr.substr(7,2) + '.' + vr.substr(10,4) + '.' + vr.substr(15,2) + '.';
			if (tam >= 22 && tam <23)
				Campo.value = vr.substr(0, 5) + '.' + vr.substr(5,2) + '.' + vr.substr(7,2) + '.' + vr.substr(10,4) + '.' + vr.substr(15,2) + '.' + vr.substr(18,3) + '.';
			}
	}

	function chkNumero() {

	 if (event.keyCode == '13') 
	 {
	 }
	 else if (event.keyCode < '48' || event.keyCode > '58')
	 {
	  alert('Por favor, digite apenas números!'); 
	  event.keyCode = '127';
	  }
	}

	function ValidaDigito(num){
		var n1, n2, n3, n4, n5, n6, dig;
  
		n1 = parseInt(num.substr(0,1)) * 2;
		n2 = parseInt(num.substr(1,1)) * 2;
		n3 = parseInt(num.substr(2,1));
		n4 = parseInt(num.substr(3,1)) * 2;
		n5 = parseInt(num.substr(4,1));
		n6 = parseInt(num.substr(5,1)) * 2;
  
		if(n1 > 9){n1 -= 9};
		if(n2 > 9){n2 -= 9};
		if(n4 > 9){n4 -= 9};
		if(n6 > 9){n6 -= 9};
  
		dig = 10 - ((n1 + n2 + n3 + n4 + n5 + n6) % 10)
  
		if(dig == 10) {
			dig = 0
		}
		
		//alert(dig + '   ' + parseInt(num.substr(6,1)));
		
		if(parseInt(num.substr(6,1)) == dig) {
			return(true)
		}
	
		if(parseInt(num.substr(6,1)) != dig) {
			return(false)
		}
	}	

/*
function CalcularDV(sCampo, iPeso){
	
	var iTamCampo;
	var iPosicao, iDigito;
	var iSoma1 = 0;
	var iSoma2=0;
	var iDV1, iDV2;
		
	iTamCampo = sCampo.length;

	for (iPosicao=1; iPosicao<=iTamCampo; iPosicao++){
		iDigito = sCampo.substr(iPosicao-1, 1);
		iSoma1 = parseInt(iSoma1,10) + parseInt((iDigito * Calcular_Peso(iTamCampo - iPosicao, iPeso)),10);
		iSoma2 = parseInt(iSoma2,10) + parseInt((iDigito * Calcular_Peso(iTamCampo - iPosicao + 1, iPeso)),10);
		}

	iDV1 = 11 - (iSoma1 % 11);
	if (iDV1 > 9)
		iDV1 = 0;

	iSoma2 = iSoma2 + (iDV1 * 2);
	iDV2 = 11 - (iSoma2 % 11);
	if (iDV2 > 9)
		iDV2 = 0;

	Ret = (parseInt(iDV1 * 10,10) + parseInt(iDV2));

	Ret = "0" + Ret;
	Ret = Ret.substr(Ret.length - 2,Ret.length);
		
	return(Ret);
}
*/

////// RECEITA

//////////////////////////////////////////////////////////////////
function CalcularDV(sCampo, iPeso){
	
	var iTamCampo;
	var iPosicao, iDigito;
	var iSoma1 = 0;
	var iSoma2=0;
	var iDV1, iDV2;
		
	iTamCampo = sCampo.length;

	for (iPosicao=1; iPosicao<=iTamCampo; iPosicao++){
		iDigito = sCampo.substr(iPosicao-1, 1);
		iSoma1 = parseInt(iSoma1,10) + parseInt((iDigito * Calcular_Peso(iTamCampo - iPosicao, iPeso)),10);
		iSoma2 = parseInt(iSoma2,10) + parseInt((iDigito * Calcular_Peso(iTamCampo - iPosicao + 1, iPeso)),10);
		}

	iDV1 = 11 - (iSoma1 % 11);
	if (iDV1 > 9)
		iDV1 = 0;

	iSoma2 = iSoma2 + (iDV1 * 2);
	iDV2 = 11 - (iSoma2 % 11);
	if (iDV2 > 9)
		iDV2 = 0;

	Ret = (parseInt(iDV1 * 10,10) + parseInt(iDV2));

	Ret = "0" + Ret;
	Ret = Ret.substr(Ret.length - 2,Ret.length);
		
	return(Ret);
}

//////////////////////////////////////////////////////////////////		
function Calcular_Peso(iPosicao, iPeso){

	//Pesos
	//CPF 11
	//CNPJ 9
	return (iPosicao % (iPeso - 1)) + 2;
	}
	
/////////////////////////////////////////////////////////////////
function LimpaCampo(sValor,iBase){
	var tam = sValor.length
	var saida = new String
	for (i=0;i<tam;i++)
		if (!isNaN(parseInt(sValor.substr(i,1),iBase)))
			saida = saida + String(sValor.substr(i,1));
	return (saida);		
	}
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
function TestaNI(cNI,iTipo){
	var NI 
	NI = LimpaCampo(cNI.value,10);
	switch (iTipo) {
		case 1:
			if (NI.length != 14){
				//alert('O número do CNPJ informado está incorreto');
				cNI.value = "";
				cNI.focus();
				return(false);
				}

			if (NI.substr(12,2) != CalcularDV(NI.substr(0,12), 9)){
				//alert('O número do CNPJ informado está incorreto');
				cNI.value = "";
				cNI.focus();
				return(false);
				}
			break;

		case 2:
			if (NI.length != 11){
				//alert('O número do CPF informado está incorreto');
				cNI.value = "";
				cNI.focus();
				return(false);
				}

			if (NI.substr(9,2) != CalcularDV(NI.substr(0,9), 11)){
				//alert('O número do CPF informado está incorreto');
				cNI.value = "";
				cNI.focus();
				return(false);
				}
			break;

		default:
			return(false);
		}
	
	return (true);	
	}  
/////////////////////////////////////////////////////////////////
//////////Exibe e Oculta  caixa de login/////////////////////////
/////////////////////////////////////////////////////////////////
function visibleLogin()	{
	if ((obj=MM_findObj('visibleLogin'))!=null) {
		if (obj.style.display == "inline")
			obj.style.display = "none";
		else
			obj.style.display = "inline";
	}

}
/////////////////////////////////////////////////////////////////
//////////Exibe e Oculta  caixa de login/////////////////////////
/////////////////////////////////////////////////////////////////
function validarLogin(){
	if ((obj=MM_findObj('user'))!=null) {
		if (obj.value == "") {
			alert("Usuário deve ser informado!");
			return false;
		}
	}
	if ((obj=MM_findObj('senha'))!=null) {
		if (obj.value == "") {
			alert("Senha deve ser informada!");
			return  false;
		}
	}
	return true;
}

function loginIncorreto(msgErro){

	document.all.user.readOnly = false;
	document.all.senha.readOnly = false;
	document.all.user.className = "Campo";
	document.all.senha.className = "Campo";
	document.all.Botao.disabled = false;
}

/***
* Descrição.: formata um campo do formulário de
* acordo com a máscara informada...
* Parâmetros: - objForm (o Objeto Form)
* - strField (string contendo o nome
* do textbox)
* - sMask (mascara que define o
* formato que o dado será apresentado,
* usando o algarismo "9" para
* definir números e o símbolo "!" para
* qualquer caracter...
* - evtKeyPress (evento)
* Uso.......: <input type="textbox"
* name="xxx".....
* onkeypress="return txtBoxFormat(document.rcfDownload, 'str_cep', '99999-999', event);">
* Observação: As máscaras podem ser representadas como os exemplos abaixo:
* CEP -> 99.999-999
* CPF -> 999.999.999-99
* CNPJ -> 99.999.999/9999-99
* Data -> 99/99/9999
* Tel Resid -> (99) 999-9999
* Tel Cel -> (99) 9999-9999
* Processo -> 99.999999999/999-99
* C/C -> 999999-!
* E por aí vai...
***/

function txtBoxFormat(objForm, strField, sMask, evtKeyPress) {
      var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;

      if(document.all) { // Internet Explorer
        nTecla = evtKeyPress.keyCode; }
      else if(document.layers) { // Nestcape
        nTecla = evtKeyPress.which;
      }

      sValue = objForm[strField].value;

      // Limpa todos os caracteres de formatação que
      // já estiverem no campo.
      sValue = sValue.toString().replace( "-", "" );
      sValue = sValue.toString().replace( "-", "" );
      sValue = sValue.toString().replace( ".", "" );
      sValue = sValue.toString().replace( ".", "" );
      sValue = sValue.toString().replace( "/", "" );
      sValue = sValue.toString().replace( "/", "" );
      sValue = sValue.toString().replace( "(", "" );
      sValue = sValue.toString().replace( "(", "" );
      sValue = sValue.toString().replace( ")", "" );
      sValue = sValue.toString().replace( ")", "" );
      sValue = sValue.toString().replace( " ", "" );
      sValue = sValue.toString().replace( " ", "" );
      fldLen = sValue.length;
      mskLen = sMask.length;

      i = 0;
      nCount = 0;
      sCod = "";
      mskLen = fldLen;

      while (i <= mskLen) {
        bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/"))
        bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))

        if (bolMask) {
          sCod += sMask.charAt(i);
          mskLen++; }
        else {
          sCod += sValue.charAt(nCount);
          nCount++;
        }

        i++;
      }

      objForm[strField].value = sCod;

      if (nTecla != 8) { // backspace
        if (sMask.charAt(i-1) == "9") { // apenas números...
          return ((nTecla > 47) && (nTecla < 58)); } // números de 0 a 9
        else { // qualquer caracter...
          return true;
        } }
      else {
        return true;
      }
    }
//Fim da Função Máscaras Gerais