/*----------------------------------------------------------------------------
Formatação para qualquer mascara
Exemplo:
<input name="data" type="text"  onkeypress="return formatgen(this, '##/##/####')"/>
<input name="cpf" type="text"  onkeypress="return formatgen(this, '###.###.###-##')"/>
-----------------------------------------------------------------------------*/
function formatgen(src, mask, e){
  var i = src.value.length;
  var saida = mask.substring(0,1);
  var texto = mask.substring(i)

//  var brow = navigator.appName;
  
        if (document.all){var keypressed=event.keyCode;} // caso seja IE
        else{var keypressed = e.which;}    // do contrário deve ser Mozilla

//	alert(keypressed);

  if ((keypressed >= 48 && keypressed <= 57) == false && keypressed != 13 && keypressed != 8 && keypressed != 0) {
		if (document.all) {
		  window.event.keyCode = 8;
		} else  {
		  return false;
		}
  }

  if (texto.substring(0,1) != saida)  {
    src.value += texto.substring(0,1);
  }
}

// funcoes usadas em projetos Furquim & Delgado

function mudapto() {
	var keypressed = window.event.keyCode;
	var ElementText  = window.event.srcElement.value ;
	if (keypressed == 44) {
		window.event.keyCode = 46;
	}
}

// Função genérica para testar se um campo radio foi preenchido
// uso: na função mysubmit() colocar da forma abaixo:	
// if (!chkradio('nome do campo')) { return false; }

function chkradio(obj) {

    tamanho = eval('document.forms[0].'+ obj + '.length');	
	marcado = -1;
	
 	for (i=0;i<tamanho; i++) {
		checado = eval('document.forms[0].'+ obj + '[' + i + ']' + '.checked');		
		if (checado) {
			marcado = i
			resposta = eval('document.forms[0].'+ obj + '[' + i + ']' + '.value');
		}
	}		

	 if (marcado == -1) {alert('Campo ' + obj + ' é obrigatório. ');
		return false;
	}
	return true;	 	
}



//------------------------------------------------------
// Arredonda um valor
//------------------------------------------------------

// uso vlrinsc.format();

String.PAD_LEFT  = 0;
String.PAD_RIGHT = 1;
String.PAD_BOTH  = 2;

String.prototype.pad = function(size, pad, side) {
  var str = this, append = "", size = (size - str.length);
  var pad = ((pad != null) ? pad : " ");
  if ((typeof size != "number") || ((typeof pad != "string") || (pad == ""))) {
    throw new Error("Wrong parameters for String.pad() method.");
  }
  if (side == String.PAD_BOTH) {
    str = str.pad((Math.floor(size / 2) + str.length), pad, String.PAD_LEFT);
    return str.pad((Math.ceil(size / 2) + str.length), pad, String.PAD_RIGHT);
  }
  while ((size -= pad.length) > 0) {
    append += pad;
  }
  append += pad.substr(0, (size + pad.length));
  return ((side == String.PAD_LEFT) ? append.concat(str) : str.concat(append));
}

Number.prototype.format = function(d_len, d_pt, t_pt) {
  var d_len = d_len || 2;
  var d_pt = d_pt || ",";
  var t_pt = t_pt || ".";
  if ((typeof d_len != "number")
    || (typeof d_pt != "string")
    || (typeof t_pt != "string")) {
    throw new Error("wrong parameters for method 'String.pad()'.");
  }
  var integer = "", decimal = "";
  var n = new String(this).split(/\./), i_len = n[0].length, i = 0;
  if (d_len > 0) {
    n[1] = (typeof n[1] != "undefined") ? n[1].substr(0, d_len) : "";
    decimal = d_pt.concat(n[1].pad(d_len, "0", String.PAD_RIGHT));
  }
  while (i_len > 0) {
    if ((++i % 3 == 1) && (i_len != n[0].length)) {
      integer = t_pt.concat(integer);
    }
    integer = n[0].substr(--i_len, 1).concat(integer);
  }
  return (integer + decimal);
}

Number.prototype.formatvlr = function(d_len, d_pt, t_pt) {
  var d_len = d_len || 2;
  var d_pt = d_pt || ".";
  var t_pt = t_pt || "";
  if ((typeof d_len != "number")
    || (typeof d_pt != "string")
    || (typeof t_pt != "string")) {
    throw new Error("wrong parameters for method 'String.pad()'.");
  }
  var integer = "", decimal = "";
  var n = new String(this).split(/\./), i_len = n[0].length, i = 0;
  if (d_len > 0) {
    n[1] = (typeof n[1] != "undefined") ? n[1].substr(0, d_len) : "";
    decimal = d_pt.concat(n[1].pad(d_len, "0", String.PAD_RIGHT));
  }
  while (i_len > 0) {
    if ((++i % 3 == 1) && (i_len != n[0].length)) {
      integer = t_pt.concat(integer);
    }
    integer = n[0].substr(--i_len, 1).concat(integer);
  }
  return (integer + decimal);
}

function f_Arredondar(dblValor, intCasas)
{return parseInt(dblValor * Math.pow(10,intCasas) + 0.5) / Math.pow(10,intCasas)}

function mostra(id, mtop)
{
	document.getElementById(id).style.display = 'block';
	document.getElementById(id).style.top = mtop;
}

function mostraparent(id, mtop)
{
	parent.document.getElementById(id).style.display = 'block';
	parent.document.getElementById(id).style.top = mtop;
}

function show(id)
{
          document.getElementById(id).style.display = 'block';
//          document.forms[0].enviar.disabled = false;
}
function hide(id)
{
          document.getElementById(id).style.display = 'none';
//          document.forms[0].enviar.disabled = false;
}

function apenasdigitos() {
	var keypressed = window.event.keyCode;
	var ElementText  = window.event.srcElement.value ;
  
	if ((keypressed >= 48 && keypressed <= 57) == false && keypressed != 13) {
		window.event.keyCode = 0;
	}
}

// JavaScript Document

function FormataData(objeto,teclapress) { 

//alert(objeto);	

var tecla = teclapress.keyCode; 



//Testa se o campo está selecionado, se estiver, limpa o conteúdo para nova digitação. 
var selecionado = document.selection.createRange();  
var selecao = selecionado.text;
if((selecao != "") && (tecla != 8) && (tecla != 9) && (tecla != 13) && (tecla != 35) && (tecla != 36) && (tecla != 46) && (tecla != 16) && (tecla != 17) && (tecla != 18) && (tecla != 20) && (tecla != 27) && (tecla != 37) && (tecla != 38) && (tecla != 39) && (tecla != 40)) {objeto.value = "";} 

  if(((window.event.keyCode == 13) || (window.event.keyCode == 9))&&objeto.value != ""){
if(!(ValidaData(objeto))){
window.event.cancelBubble = true;
window.event.returnValue = false;
alert("Data Inválida");
objeto.value = "";
objeto.focus();
}
}
if (( tecla == 8 || tecla == 88 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 )&& objeto.value.length < (10)) {
vr = objeto.value; 
vr = vr.replace( "/", "" ); 
vr = vr.replace( "/", "" ); 
tam = vr.length;
if (tam < 8){
if (tecla != 8){
tam = vr.length + 1 ;
}
}
else{
window.event.cancelBubble = true;
window.event.returnValue = false;
}
if ((tecla == 8) && (tam > 1)){
tam = tam - 1 ;
objeto.value = vr.substr(0,tam);
window.event.cancelBubble = true;
window.event.returnValue = false;
}
if ( tam <= 4 && tecla != 8){
objeto.value = vr;
}
if ( (tam >= 4) && (tam <= 6) ){ 
objeto.value = vr.substr(0, tam - 4) + '/' + vr.substr( tam - 4, 4 );
}
if ( (tam >= 6) && (tam <= 8) ){
objeto.value = vr.substr(0, tam - 6 ) + '/' + vr.substr( tam - 6, 2 ) + '/' + vr.substr( tam - 4, 4 );
} 
if ((tam == (8)) && tecla != 8){
if(tecla >=96 && tecla <=105){
tecla = tecla - 48;
}
objeto.value = objeto.value + (String.fromCharCode(tecla));
window.event.cancelBubble = true;
window.event.returnValue = false;
if (!(ValidaData(objeto))){
alert("Data Inválida");
objeto.value = "";
objeto.focus();
}
}
}
else if((window.event.keyCode != 8) && (window.event.keyCode != 9) && (window.event.keyCode != 13) && (window.event.keyCode != 35) && (window.event.keyCode != 36) && (window.event.keyCode != 46)){
event.returnValue = false;
} 
}

function f_cep(obj) {
	var keypressed = window.event.keyCode;
	var ElementText  = window.event.srcElement.value ;
  
	if ((keypressed >= 48 && keypressed <= 57) == false && keypressed != 13) {
		window.event.keyCode = 0;
	}

	if (obj.value.length == 2 || obj.value.length == 5 ) {obj.value = obj.value + "/"}
}

function f_data(obj) {
	var keypressed = window.event.keyCode;
	var ElementText  = window.event.srcElement.value ;
  
	if ((keypressed >= 48 && keypressed <= 57) == false && keypressed != 13) {
		window.event.keyCode = 0;
	}

	if (obj.value.length == 2 || obj.value.length == 5 ) {obj.value = obj.value + "/"}
}

function f_hora(obj) {
	var keypressed = window.event.keyCode;
	var ElementText  = window.event.srcElement.value ;
  
	if ((keypressed >= 48 && keypressed <= 57) == false && keypressed != 13) {
		window.event.keyCode = 0;
	}

	if (obj.value.length == 2) {obj.value = obj.value + ":"}
}

function f_telefone(obj) {
	var keypressed = window.event.keyCode;
	var ElementText  = window.event.srcElement.value ;
  
	if ((keypressed >= 48 && keypressed <= 57) == false && keypressed != 13) {
		window.event.keyCode = 0;
	}

	if (obj.value.length == 4) {obj.value = obj.value + "-"}
}

function f_telddd(obj) {
    if (obj.value.length == 8 || obj.value.length == 0 || obj.value.length == 9){f_checar_tab()} else {v_tab=false;}
    var c = document.layers ? evt.which
            : document.all ? event.keyCode
            : evt.keyCode;
	if ((c < 48 || c > 58)) {return false;}
    else {
    
		if (obj.value.length == 0) {obj.value = obj.value + "("}
		if (obj.value.length == 3) {obj.value = obj.value + ") "}
		//if (obj.value.length == 4) {obj.value = obj.value + "-"}
		if (obj.value.length == 9) {obj.value = obj.value + "-"}
	}
}

function f_checar_tab() { v_tab=true; } 

function formatar(decimal,quem) {
  var keypressed = window.event.keyCode;
  var ElementText  = window.event.srcElement.value ;
  
  if (keypressed == 45){
    if (ElementText.length!=0) {
      window.event.keyCode = 0;
    }
  }
  else {
    if (decimal) {
      var Reg = /\./g;
      if (Reg.test(ElementText)) {
        if( (keypressed >= 48 && keypressed <= 57) == false) {
          window.event.keyCode = 0;
        }
      }
      else {
        if ((keypressed >= 48 && keypressed <= 57) == false) {
          window.event.keyCode = 0;
        }
      }
    }
 
    else {
      if(quem == 'cnpj')
   {
    if ((keypressed >= 48 && keypressed <= 57) == false) {
   window.event.keyCode = 0;
    }
   }
   else if (quem == 'cpf' || quem == 'ie')
   {
    if ((keypressed >= 47 && keypressed <= 57) == false) {
   window.event.keyCode = 0;
    }
   }
    }
  }
    if(quem == 'cnpj')
 {
  if (ElementText.length == 2 || ElementText.length == 6) { document.forms[0].cnpj.value += '.' }
  if (ElementText.length == 10) { document.forms[0].cnpj.value += '/' }
  if (ElementText.length == 15) { document.forms[0].cnpj.value += '-' }
//  if (ElementText.length == 18) { document.forms[0].txtRazao.focus() }
  
 }
    if(quem == 'cpf')
 {
  if (ElementText.length == 3 || ElementText.length == 7) { document.forms[0].cpf.value += '.' }
  if (ElementText.length == 11) { document.forms[0].cpf.value += '-' }
//  if (ElementText.length == 14) { document.forms[0].txtNome.focus()}
 }
    if(quem == 'ie')
 {
  if (ElementText.length == 3 || ElementText.length == 7 || ElementText.length == 11) { document.forms[0].ie.value += '.' }
//  if (ElementText.length == 15) { document.forms[0].txtEndereco.focus() }  
 }
    if(quem == 'cep')
 {
  if (ElementText.length == 5) { document.forms[0].cep.value += '-' }
//  if (ElementText.length == 9) { document.forms[0].txtCidade.focus() }
 }
 
}
function isInteiro(obj)
{
	var reDigits = /^\d+$/;
	var pStr = obj.value;
	if (!reDigits.test(pStr) && pStr != null && pStr != "") {
		alert(obj.name + " NÃO é um número inteiro.");
		obj.value = 0;
		obj.focus();
	}
}

function isDecimal(obj)
{
	var reDecimalPt = /^[+-]?((\d+|\d{1,3}(\.\d{3})+)(\,\d*)?|\,\d+)$/;
	var reDecimalEn = /^[+-]?((\d+|\d{1,3}(\,\d{3})+)(\.\d*)?|\.\d+)$/;
	var reDecimal = reDecimalPt;
	var pLang = "En";
	var pStr = obj.value;
	charDec = ( pLang != "En" ? "," : "." );
	eval("reDecimal = reDecimal" + pLang);
	if (!reDecimal.test(pStr) && pStr != null && pStr != "") {
		alert(pStr + " NÃO é um número válido. \n\nUtilize . para casas decimais");
		//obj.value = 0;
		obj.focus();
	}
} // doDecimal

function isMoeda(obj)
{
	var reMoeda = /^\d{1,3}(\.\d{3})*\,\d{2}$/;
	var pStr = obj.value;
	if (!reMoeda.test(pStr) && pStr != null && pStr != "") {
		alert(pStr + " NÃO é um valor financeiro válido.");
		obj.value = 0;
		obj.focus();
	}
}

function isData(obj)
{
	var reDate1 = /^\d{1,2}\/\d{1,2}\/\d{1,4}$/;
	var reDate2 = /^[0-3]?\d\/[01]?\d\/(\d{2}|\d{4})$/;
	var reDate3 = /^(0?[1-9]|[12]\d|3[01])\/(0?[1-9]|1[0-2])\/(19|20)?\d{2}$/;
	var reDate4 = /^((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}$/;
	var reDate5 = /^((0[1-9]|[12]\d)\/(0[1-9]|1[0-2])|30\/(0[13-9]|1[0-2])|31\/(0[13578]|1[02]))\/(\d{2}|\d{4})$/;
	var reDate = reDate4;
	var pStr = obj.value;
	pFmt = 4 // assim pede sempre a data no formato dd/mm/aaaa ou dd/mm/aa
	eval("reDate = reDate" + pFmt);
	if (!reDate.test(pStr) && pStr != null && pStr != "") {
		alert(pStr + " NÃO é uma data válida.");
		obj.focus();
	}
} // doDate

function isHora(obj)
{
	var reTime1 = /^\d{2}:\d{2}$/;
	var reTime2 = /^([0-1]\d|2[0-3]):[0-5]\d$/;
	var reTime3 = /^(0[1-9]|1[0-2]):[0-5]\d$/;
	var reTime4 = /^\d+:[0-5]\d:[0-5]\d$/;
	var reTime5 = /^\d+:[0-5]\d:[0-5]\.\d{3}\d$/;
	var pStr = obj.value;
	pFmt = 2 // assim pede sempre o horario no formato HH:MM 24hs (00:00 a 23:59)
	eval("reTime = reTime" + pFmt);
	if (!reTime.test(pStr) && pStr != null && pStr != "") {
		alert(pStr + " NÃO é um horário válido.");
		obj.focus();
	}
} // doTime

function isEmail(obj)
{
	
	var reEmail1 = /^[\w!#$%&'*+\/=?^`{|}~-]+(\.[\w!#$%&'*+\/=?^`{|}~-]+)*@(([\w-]+\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
	var reEmail2 = /^[\w-]+(\.[\w-]+)*@(([\w-]{2,63}\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
	var reEmail3 = /^[\w-]+(\.[\w-]+)*@(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
	var reEmail = reEmail3;
	var pStr = obj.value;
	pFmt = 1 // assim pede sempre o horario no formato HH:MM 24hs (00:00 a 23:59)
	eval("reEmail = reEmail" + pFmt);
	if (!reEmail.test(pStr) && pStr != null && pStr != "") {
		alert(pStr + " NÃO é um endereço de e-mail válido.");
		obj.focus();
	}
} // doEmail

// desliga todos os controles do form
function desligatudo() {
	var d,i,el;
  	d=document;
	for (i=0;i<d.forms[0].length;i++) {
		el=d.forms[0][i];
		el.disabled = true;
	}
}

function desliga(mturnoff) {
	if(mturnoff) {
		alert('Como este Formulário já foi enviado, não será possível fazer alterações no mesmo.\n\n Para alterações, solicite através do e-mail elise@satoreventos.com.br');
		//alert('Para efetuar alterações preencha o formulário XX');
		for (i=0;i<document.forms[0].length;i++) {
    		document.forms[0][i].disabled = true;
		}
	}	
}		

// desliga tudo menos radiobutton e botao enviar
function desabilita(onoff) {
		ignore = 'numestande_area_kw_min_cv_gratis_cv_extras_mb_valor';
		for (i=0;i<document.forms[0].length;i++) {
    	if (document.forms[0][i].type != 'radio' && document.forms[0][i].type != 'submit' && document.forms[0][i].type != 'hidden') {
//			if(onoff) {document.forms[0][i].value = '';}
			nome = document.forms[0][i].name;
			if (ignore.indexOf(nome) == -1) {document.forms[0][i].disabled = onoff;}
			}
		}
}		

function myformat_number(p,d) 
{
/*
  	var r = String(p);
	if (r.indexOf('.') == -1) {
		r = r + '.00'; }
	else {
		round
	}
*/
	p = Math.ceil(parseFloat(p * 100));
	p = parseFloat(p/100);
  	return p;
}

function format_number(p,d) 
{
  var r;
  if(p<0){p=-p;r=format_number2(p,d);r="-"+r;}
  else   {r=format_number2(p,d);}
  return r;
}

function format_number2(pnumber,decimals) 
{
  var strNumber = new String(pnumber);
  var arrParts = strNumber.split('.');
  var intWholePart = parseInt(arrParts[0],10);
  var strResult = '';
  if (isNaN(intWholePart))
    intWholePart = '0';
  if(arrParts.length > 1)
  {
    var decDecimalPart = new String(arrParts[1]);
    var i = 0;
    var intZeroCount = 0;
     while ( i < String(arrParts[1]).length )
     {
       if( parseInt(String(arrParts[1]).charAt(i),10) == 0 )
       {
         intZeroCount += 1;
         i += 1;
       }
       else
         break;
    }
    decDecimalPart = parseInt(decDecimalPart,10)/Math.pow(10,parseInt(decDecimalPart.length-decimals-1)); 
    Math.round(decDecimalPart); 
    decDecimalPart = parseInt(decDecimalPart)/10; 
    decDecimalPart = Math.round(decDecimalPart); 

    //If the number was rounded up from 9 to 10, and it was for 1 'decimal' 
    //then we need to add 1 to the 'intWholePart' and set the decDecimalPart to 0. 

    if(decDecimalPart==Math.pow(10, parseInt(decimals)))
    { 
      intWholePart+=1; 
      decDecimalPart="0"; 
    } 
    var stringOfZeros = new String('');
    i=0;
    if( decDecimalPart > 0 )
    {
      while( i < intZeroCount)
      {
        stringOfZeros += '0';
        i += 1;
      }
    }
    decDecimalPart = String(intWholePart) + "." + stringOfZeros + String(decDecimalPart); 
    var dot = decDecimalPart.indexOf('.');
    if(dot == -1)
    {
      decDecimalPart += '.'; 
      dot = decDecimalPart.indexOf('.'); 
    } 
    var l=parseInt(dot)+parseInt(decimals); 
    while(decDecimalPart.length <= l) 
    {
      decDecimalPart += '0'; 
    }
    strResult = decDecimalPart;
  }
  else
  {
    var dot; 
    var decDecimalPart = new String(intWholePart); 

    decDecimalPart += '.'; 
    dot = decDecimalPart.indexOf('.'); 
    var Al=parseInt(dot)+parseInt(decimals); 
    while(decDecimalPart.length <= l) 
    {
      decDecimalPart += '0'; 
    }
    strResult = decDecimalPart;
  }
  return strResult;
}

function handleEnter (field, event) {
		var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
		if (keyCode == 13) {
			var i;
			for (i = 0; i < field.form.elements.length; i++)
				if (field == field.form.elements[i])
					break;
			i = (i + 1) % field.form.elements.length;
			field.form.elements[i].focus();
			return false;
		} 
		else
		return true;
}      

function open_window(url, x, y) {
xx = String((800 - parseFloat(x))/2);
//yy = String((600 - parseFloat(y))/2);
//mywin = window.open(url,"_blank",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width='+x+',height='+y+',left='+xx+',top='+yy+',screenX=100,screenY=100');
yy = String((600 - parseFloat(y))/2-65);
mywin = window.open(url,"_blank",'toolbar=1,location=1,directories=0,status=1,menubar=1,scrollbars=1,resizable=1,width='+x+',height='+y+',left='+xx+',top='+yy+',screenX=100,screenY=100');
}

function open_window_noscroll(url, x, y) {
xx = String((800 - parseFloat(x))/2);
yy = String((600 - parseFloat(y))/2);
mywin = window.open(url,"_blank",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width='+x+',height='+y+',left='+xx+',top='+yy+',screenX=100,screenY=100');
//yy = String((600 - parseFloat(y))/2-65);
//mywin = window.open(url,"win",'toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=0,width='+x+',height='+y+',left='+xx+',top='+yy+',screenX=100,screenY=100');
}

function open_winvectra_noscroll(url, x, y) {
xx = String((800 - parseFloat(x))/2);
yy = String((600 - parseFloat(y))/2);
mywin = window.open(url,"_blank",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width='+x+',height='+y+',left='+xx+',top='+yy+',screenX=100,screenY=100');
//yy = String((600 - parseFloat(y))/2-65);
//mywin = window.open(url,"win",'toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=0,width='+x+',height='+y+',left='+xx+',top='+yy+',screenX=100,screenY=100');
}

function gotourl(myurl) { //v3.0
  document.MM_returnValue = false;
  location='"+myurl+"';
//  eval("document.location='"+myurl+"'");
  document.location=myurl;
}

function myselect(n) { //v4.01
eval('document.forms[0].'+n+'.focus()');
}

function destaca(obj, tipo){
var clfont, clback;
// tipo 1: qdo a caixa de texto recebe foco
// tipo 2: qdo a caixa de texto perde foco
// tipo 3: qdo botao recebe foco
// tipo 4: qdo botao perde foco
if (tipo==1) {clfont = "#003399"; clback = "#FFFFAA";}
if (tipo==2) {clfont = "#000000"; clback = "#dFdFdF";}
if (tipo==3) {clfont = "white"; clback = "#FFCCFF";}
if (tipo==4) {clfont = "#000000"; clback = "#FFFFFF";}
  obj.style.background = clback;
  obj.style.color = clfont;
  return true;
}


function undestaca(obj){
obj.className = 'formunselected';
return true;
}

function change(e, colorfont, colorback){
var el=window.event? event.srcElement: e.target
if (el.tagName=="INPUT"&&(el.type=="submit"||el.type=="reset"||el.type=="button")) {
el.style.backgroundColor=colorback
el.style.color=colorfont
}
if (el.tagName=="INPUT"&&(el.type=="text")) {
el.style.backgroundColor=yellow
el.style.color=green
}
}

var buttontarget=""

function jumpto2(url){
if (buttontarget=="")
window.location=url
else if (buttontarget=="_new")
window.open(url)
else
parent[buttontarget].location=url
}

function checacnpj() 
{
   var CNPJ=document.forms[0].cnpj.value;   
   var result=false;
     
   CNPJ =  CNPJ.replace(/[./-]/g,'');
   
   if (CNPJ=='') return(true);
   if( (RealTestaCNPJ(CNPJ,12) == 1) && (RealTestaCNPJ(CNPJ,13) == 1) ) result=true; 
   if (result==false) {
      alert('CNPJ inválido !');
      document.forms[0].cnpj.select();
      document.forms[0].cnpj.focus(); }
    return(result); 
}

function RealTestaCNPJ(CNPJ,g) 
{ 
 var VerCNPJ=0; 
 var ind=2; 
 var tam; 

 for(f=g;f>0;f--) 
  { 
  VerCNPJ+=parseInt(CNPJ.charAt(f-1))*ind; 
  if(ind>8) { 
   ind=2;} 
  else { 
   ind++; } 
  } 

  VerCNPJ%=11; 
  if(VerCNPJ==0 || VerCNPJ==1) {
     VerCNPJ=0; } 
  else { 
   VerCNPJ=11-VerCNPJ; } 

  if(VerCNPJ!=parseInt(CNPJ.charAt(g))) { 
     return(0); } 
  else { 
     return(1); } 
}

function checacpf() {

	var NUMERO = document.forms[0].cpf.value;
	
	NUMERO = NUMERO.replace(/[./-]/g,'');
		
	if (NUMERO == '00000000000' ||
		NUMERO == '11111111111' ||
		NUMERO == '22222222222' ||
		NUMERO == '33333333333' ||
		NUMERO == '44444444444' ||
		NUMERO == '55555555555' ||
		NUMERO == '66666666666' ||
		NUMERO == '77777777777' ||
		NUMERO == '88888888888' ||
		NUMERO == '99999999999')
	{
		alert('CPF inválido !');
		document.forms[0].cpf.focus();
		return false;
	}
		
        var POSICAO, I, SOMA, DV, DV_INFORMADO;
        var DIGITO = new Array(10);
		
		/* Retira do número informado os dois últimos dígitos */
        DV_INFORMADO = NUMERO.substr( 9, 2);

        /* Desmembra o número do CPF na matriz DIGITO */
        for (I=0; I<=8; I++) {
          DIGITO[I] = NUMERO.substr( I, 1);
        }

        /* Calcula o valor do 10o. digito de verificação */
        POSICAO = 10;
        SOMA = 0;
        for (I=0; I<=8; I++) {
          SOMA = SOMA + DIGITO[I] * POSICAO;
          POSICAO = POSICAO - 1;
        }
        DIGITO[9] = SOMA % 11;
        if (DIGITO[9] < 2) {
          DIGITO[9] = 0; }
        else {
          DIGITO[9] = 11 - DIGITO[9];
        }

        /* Calcula o valor do 11o. digito de verificação */
        POSICAO = 11;
        SOMA = 0;
        for (I=0; I<=9; I++) {
          SOMA = SOMA + DIGITO[I] * POSICAO;
          POSICAO = POSICAO - 1;
        }
        DIGITO[10] = SOMA % 11;
        if (DIGITO[10] < 2) {
          DIGITO[10] = 0; }
        else {
          DIGITO[10] = 11 - DIGITO[10];
        }

        /* Vericica se o DV calculado é igual ao informado */
        DV = DIGITO[9] * 10 + DIGITO[10];
        if (DV != DV_INFORMADO) {
           alert('CPF inválido !');
           document.forms[0].cpf.focus();
		   return false;
        } 
		
		return true;
}


// Função de teste de CPF para o nome do campo genérico
// Serve para quando tem varios cpfs na mesma pagina
// De preferência colocar o teste no mysubmit() e não no OnBlur, 
// pq se for no OnBlur e a pessoa teclar enter o formulario passa sem testar o CPF
// usar assim:
// if(!chkcpf_gen(document.getElementById('cpf1'))){return false;}

function chkcpf_gen(obj) {

	var NUMERO = obj.value;
	var nomecampo = obj.name;
		
	if (NUMERO == '00000000000' ||
		NUMERO == '11111111111' ||
		NUMERO == '22222222222' ||
		NUMERO == '33333333333' ||
		NUMERO == '44444444444' ||
		NUMERO == '55555555555' ||
		NUMERO == '66666666666' ||
		NUMERO == '77777777777' ||
		NUMERO == '88888888888' ||
		NUMERO == '99999999999')
	{
		alert('CPF inválido !');
		document.getElementById(nomecampo).select();			
		document.getElementById(nomecampo).focus();		
		return false;
	}
		
        var POSICAO, I, SOMA, DV, DV_INFORMADO;
        var DIGITO = new Array(10);
		
		/* Retira do número informado os dois últimos dígitos */
        DV_INFORMADO = NUMERO.substr( 9, 2);

        /* Desmembra o número do CPF na matriz DIGITO */
        for (I=0; I<=8; I++) {
          DIGITO[I] = NUMERO.substr( I, 1);
        }

        /* Calcula o valor do 10o. digito de verificação */
        POSICAO = 10;
        SOMA = 0;
        for (I=0; I<=8; I++) {
          SOMA = SOMA + DIGITO[I] * POSICAO;
          POSICAO = POSICAO - 1;
        }
        DIGITO[9] = SOMA % 11;
        if (DIGITO[9] < 2) {
          DIGITO[9] = 0; }
        else {
          DIGITO[9] = 11 - DIGITO[9];
        }

        /* Calcula o valor do 11o. digito de verificação */
        POSICAO = 11;
        SOMA = 0;
        for (I=0; I<=9; I++) {
          SOMA = SOMA + DIGITO[I] * POSICAO;
          POSICAO = POSICAO - 1;
        }
        DIGITO[10] = SOMA % 11;
        if (DIGITO[10] < 2) {
          DIGITO[10] = 0; }
        else {
          DIGITO[10] = 11 - DIGITO[10];
        }

        /* Verifica se o DV calculado é igual ao informado */
        DV = DIGITO[9] * 10 + DIGITO[10];
        if (DV != DV_INFORMADO) {
        	alert('CPF inválido !');
			document.getElementById(nomecampo).select();				   
			document.getElementById(nomecampo).focus();
		   return false;	
        } 
		
		return true;
}

