function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

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 MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function mascara_cnpj(evento,campo_teste){
var campo = eval("document.frmcliente."+campo_teste).value;
 if (evento.keyCode == 8){
         subtracao = campo.length - 1; 
        eval("document.frmcliente."+campo_teste).value = campo.substr(0,subtracao + 1);

 }else{
        if (campo.length == 2)
        {
            eval("document.frmcliente."+campo_teste).value = eval("document.frmcliente."+campo_teste).value + ".";
        }
        if (campo.length == 6)
        {
            eval("document.frmcliente."+campo_teste).value = eval("document.frmcliente."+campo_teste).value + ".";
        }
        if (campo.length == 10)
        {
            eval("document.frmcliente."+campo_teste).value = eval("document.frmcliente."+campo_teste).value + "/";
        }
        if (campo.length == 15)
        {
            eval("document.frmcliente."+campo_teste).value = eval("document.frmcliente."+campo_teste).value + "-";
        }

 }
}
function limpa_string(S){
    // Deixa so' os digitos no numero
    var Digitos = "0123456789";
    var temp = "";
    var digito = "";

    for (var i=0; i<S.length; i++)  {
        digito = S.charAt(i);
        if (Digitos.indexOf(digito)>=0) {
            temp=temp+digito    }
    } //for

    return temp
}
function validaCNPJ(s)
{
    var i;
    s = limpa_string(s);
    var c = s.substr(0,12);
    var dv = s.substr(12,2);
    var d1 = 0;
    for (i = 0; i < 12; i++)
    {
        d1 += c.charAt(11-i)*(2+(i % 8));
    }
        if (d1 == 0) return false;
        d1 = 11 - (d1 % 11);
    if (d1 > 9) d1 = 0;
    if (dv.charAt(0) != d1)
    {
        return false;
    }

    d1 *= 2;
    for (i = 0; i < 12; i++)
    {
        d1 += c.charAt(11-i)*(2+((i+1) % 8));
    }
    d1 = 11 - (d1 % 11);
    if (d1 > 9) d1 = 0;
    if (dv.charAt(1) != d1)
    {
        return false;
    }
    return true;
}
//função de validaçào de CPF
	function CPFValido(CPF) 
	{
		var digitsInCIC = 11;
        var digito = new Array(digitsInCIC);
        var dv = new Array(2);
        var aux = 0;
        var i = 0;
		
        
		switch(CPF)
			{
			 	case "11111111111":
					  return false;
				case "22222222222":
					  return false;
				case "33333333333":
					  return false;
				case "44444444444":
					  return false;
				case "55555555555":
					  return false;
				case "66666666666":
					  return false;
				case "77777777777":
					  return false;
				case "88888888888":
					  return false;
				case "99999999999":
					  return false;
				case "00000000000":
					  return false;
				default:
		
						// Se tiver menos de digitsInCIC posicoes, completa com zeros à frente
        				if (CPF.length < digitsInCIC-1) 
        				{
						   CPF = "0000000000" + CPF;
		    			   CPF = CPF.substring(CPF.length - digitsInCIC, CPF.length);
		 				 }
		 }
	    
        // Separa os dígitos (12 dígitos no máximo)
        for (i=0; i<=(CPF.length-1); i++) 
        {
            digito[i]=CPF.substring(i,i+1);
			
        }
        alert
        // Calcula o primeiro dígito verificador
        for (i=0; i<=8; i++) 
        {
		    aux += (digito[i] * (10-i));
        }
        
        if (((aux%digitsInCIC)==0) | ((aux%digitsInCIC)==1)) 
        {
            dv[0] = 0;
        } 
        else 
        {
		    dv[0] = digitsInCIC - (aux%digitsInCIC);
        }
        
        // Se o primeiro dígito não valer, pára por aqui
        if (dv[0] != digito[9]) 
        {
           return false;
        }
        
        // Calcula o segundo dígito verificador
        aux = 0;
		for (i=0; i<=8; i++) 
		{
           aux += (digito[i] * (digitsInCIC-i));
        }
        aux += dv[0] * 2;
        
        if (((aux%digitsInCIC)==0) | ((aux%digitsInCIC)==1)) 
        {
           dv[1] = 0;
        } 
        else 
        {
	       dv[1] = digitsInCIC - (aux%digitsInCIC);
        }
        
        // Se o segundo dígito não valer, pára por aqui
        if (dv[1] != digito[10]) 
        {
           return false;
        }
        
        // Se chegou até aqui, não há por que não dizer
        // que o CPF não é válido
        return true;
        
	}  // END FUNCTION CPFValido ()

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++;
          }}
      }

      //alert(valorFinal)
      _RefObjeto.value = valorFinal
  }

function abrir(pagina,largura,altura){
	config="toolbar=no,location=no,width="+largura+",height="+altura+",status=no,menubar=no,scrollbars=yes,resible=no,top=10,left=10";
	pop=window.open(pagina,"newwindow",config);
  }

function abrePopUp(pagina, largura, altura){
	
	var top;
	var left;
	var y, x;
	if (screen.width == 800 && screen.height == 600){
		y = 100;	
		x = 5;
	}else
		if (screen.width == 1024 && screen.height == 768){
			y = 220;
			x = 110;
	 	}
		
	y = 20;
	x = 20;		
	
	if (document.all){
		top  = "top  ="+y;
		left = "left ="+x;
	}else{
		top  = "screenY ="+y;
		left = "screenX ="+x;
	}
	
	config="toolbar=no,location=no,width="+largura+",height="+altura+",status=no,menubar=no,scrollbars=no,resible=no,"+top+","+left;
	pop=window.open(pagina,"newwindow",config);
}
function SendUrl()
{
	var args = SendUrl.arguments;

	args[1] = (args[1] != null && args[1] != '') ? args[1] : "";
	args[2] = (args[2] != null && args[2] != '') ? "#"+args[2] : "";

	url = "montatela.php?t="+args[0]+"&"+args[1]+args[2];
	
	//if (args[0] == 'main') { abrePopUp('popup.htm', 388, 415); }

	window.open(url, '_self');
}

function Apagar(tela)
{
	if (confirm("Confirma exclusão?"))
	{
		location.href = tela;
	}
}

function APagaImg(php, param)
  {
  var link = "monta_popup.php?t="+php+"&"+param;
  parent.frames['auxFrame'].document.location = link;
  }
