function cadastrar()
{
   if(document.cadastro.representante.value == "")
   {
     alert("Informe o nome do representante.");
	 document.cadastro.representante.focus();
   }
   else
   if(document.cadastro.empresa.value == "")
   {
     alert("Informe o nome da empresa.");
	 document.cadastro.empresa.focus();
   }
   else
   if(!ValidaCNPJ(document.cadastro.cnpj.value))
   {
     alert("Informe um CNPJ válido.");
	 document.cadastro.cnpj.focus();
   }
   else
   if(document.cadastro.logradouro.value == "")
   {
     alert("Informe o logradouro.");
	 document.cadastro.logradouro.focus();
   }
   else
   if(document.cadastro.bairro.value == "")
   {
     alert("Informe o bairro.");
	 document.cadastro.bairro.focus();
   }
   else
   if(document.cadastro.cidade.value == "")
   {
     alert("Informe a cidade.");
	 document.cadastro.cidade.focus();
   }
   else
   if(document.cadastro.cep.value == "")
   {
     alert("Informe o CEP");
	 document.cadastro.cep.focus();
   }
   else
   if(document.cadastro.ddd_telefone.value == "")
   {
     alert("Informe o telefone com o DDD");
	 document.cadastro.ddd_telefone.focus();
   }
   else
   if(document.cadastro.telefone.value == "")
   {
     alert("Informe o telefone com o DDD");
	 document.cadastro.telefone.focus();
   }
   else
   if(document.cadastro.email.value == "")
   {
     alert("Informe um e-mail válido. Será através deste e-mail que iremos validar seu cadastro.");
	 document.cadastro.email.focus();
   }
   else
     document.cadastro.submit();
}
//-------------------------------------------------------
// Valida o CNPJ informado
function ValidaCNPJ(numCNPJ) {
	var campo="";
	// retira carateres não numericos do identificador( CNPJ )
	for (i=0; i<numCNPJ.length;i++){
	    if ((numCNPJ.charAt(i)!=".") && (numCNPJ.charAt(i)!="-") && (numCNPJ.charAt(i)!="/")){campo = campo + numCNPJ.charAt(i)}}

	if (campo.length>0)
	{
		if ((campo.length > 14)||(campo == "00000000000000") || (campo == "11111111111111") || (campo == "22222222222222") || (campo == "33333333333333") || (campo == "44444444444444") || (campo == "55555555555555") || (campo == "66666666666666") || (campo == "77777777777777") || (campo == "88888888888888") || (campo == "99999999999999")){return false;}
		dig2 = campo.substring (13,14)
		dig1 = campo.substring (12,13)
		mult = 5
		soma = 0
		// calcula primeiro digito
		for (i=0;i<=11;i++)
		{
			vnum = parseInt(campo.substring (i,i+1))
			vnum = vnum * mult
			soma = soma + vnum
			mult = mult - 1
			if (mult < 2)
			{
				mult = 9
			}
		}
		rest = parseInt(soma / 11)
		rest = soma - (rest * 11)
		rest = 11 - rest
		if (rest == 10 || rest == 11)
		{
			rest = 0
		}
		// valida primeiro digito
		if (rest == dig1)
		{
			mult = 6
			soma = 0
			// calcula segundo digito
			for (i=0;i<=12;i++)
			{
				vnum = parseInt(campo.substring (i,i+1))
				vnum = vnum * mult
				soma = soma + vnum
				mult = mult - 1
				if (mult < 2)
				{
					mult = 9
				}
			}
			rest = parseInt(soma / 11)
			rest = soma - (rest * 11)
			rest = 11 - rest
			if (rest == 10 || rest == 11)
			{
				rest = 0
			}
			// valida segundo digito
			if (rest != dig2)
			{	return false;	}
		}
		else
		{	return false;	}
		return true;
	}
}

//-----------------------------------------------------------------
function pos_layer(layer,largura_site,tam_x,tam_y,x,pos_y)
{
	/*
	Parametros
	string layer => Nome do Layer
	int largura_site => largura total do site
	int tam_x => largura do layer
	int tam_y => altura do layer
	int x => posição em X
	int pos_y => posição em Y
	*/
	
	//calcula a posição em X
	diferenca = (screen.width - largura_site)/2; 
	pos_x = diferenca + x;
	
	document.write("<style type='text/css'>"+layer+" {position:absolute;left:"+ pos_x +"px;top:"+pos_y+"px;width:"+tam_x+"px;height:"+tam_y+"px;z-index:1;overflow: auto;}</style>");
}

//função de confirmação
 function verifica(dest)
 {
  x = false;
  x = confirm("Deseja realmente apagar?");
  if(x)
   window.location = dest;
  }
//----------------------------------------------------
//Movimento do menu

var ns4 = (document.layers) ? true : false;
var ie4 = (document.all) ? true : false;
var ns6 = (document.getElementById&&!document.all) ? true:false;
var total = 2;
var lastY = 0;
for (i=0;i<total;i++){eval("var pos"+i+";");}
for (i=0;i<total;i++){eval("var posV"+i+";");}
//Inicia o Menu
function init(){
		for (i=0;i<total;i++){
				if (ns4) {eval("obj"+i+"=document.div"+i)}
				if (ie4) {eval("obj"+i+"=div"+i+".style")}
				if (ns6) {eval("ns6"+i+"=document.getElementById('div"+i+"')");
				              eval("obj"+i+"=ns6"+i+".style")}
				eval("pos"+i+"=parseInt(obj"+i+".top);");
				eval("posV"+i+"=pos"+i+"-120")
												  }
		}
moving=setTimeout('null',1);
parado=true;

//mostra o menu
function mostra (obj) {
lugar=parseInt(eval(obj+".left "));
if (parado==true){
	parado=false;
	if (lugar == -134) {
	  cond="lugar<=-10";
	  dif=2; //velocidade
	  indice_inicial = eval(obj+".zIndex");
	  fator = 500 - (5*indice_inicial);if (fator < 81) {fator=81};
	  eval(obj+".zIndex =indice_inicial + fator");
	}
    if (lugar == -4) {
	  cond="lugar>=-124";
	  dif=-10;
	  eval(obj+".zIndex =" + indice_inicial);
	}
	}
	if (eval(cond)) {

	clearTimeout(moving);
	eval(obj+".left =lugar+dif");
	obj = "'"+obj+"'";
	moving = eval('setTimeout("mostra('+obj+')", 1);');}
	else {
		clearTimeout(moving);
		moving=setTimeout('null',1);
		parado=true;
		}

}

//----------------------------------------------------------------------

function pop_up(url,x,y)
 {
   /* 
    y -> Altura
    x -> Largura
	url -> página à abrir
   */
   pos_y = (screen.availHeight/2)-(y/2);
   pos_x = (screen.availWidth/2)-(x/2);
   window.open(url,'login','width='+x+',directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no,height='+y+',left='+pos_x+',top='+pos_y+''); 
}
//-----------------------------------------------------------------------
function enviar_arquivos(ident,arq,tipo)
 {
   window.open('upload_arquivos.asp?ident=' + ident + '&arq=' + arq + '&tipo=' + tipo,'upload','width=350,height=200'); 
 }
//-------------------------------------------------------  

function formata_cpf(campo)
{
	var tecla = event.keyCode;
	vr = "";
	if(campo.value.length < 14)
	{
		  for(i=0;i<campo.value.length;i++)
		  	  if (campo.value.charAt(i)!="." && campo.value.charAt(i)!="." && campo.value.charAt(i)!="-")
			     vr = vr + campo.value.charAt(i);
		  tam = vr.length ;
		  campo.maxLength = 14;
		  if (tam < 11 && tecla != 8)
		  {
			tam = vr.length + 1;
		  }
		  
		  if (tecla == 8)
		  { 
			tam = tam - 1 ;
		  }
		 
		  if(tam <= 3)
		  {
			campo.value = vr ;
		  } 
		  else if((tam > 3) && (tam <= 6) )
			   { 
				 campo.value = vr.substr( 0, tam - 2 ) + '.' + vr.substr( tam - 2, tam ) ;
			   } 
				  else if((tam > 6) && (tam <= 9) )
					   { 
						 campo.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + '.' + vr.substr( tam - 2, tam ) ; 
					   } 
						  else if((tam > 9) && (tam <= 11) )
							   {
								 campo.value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + '-' + vr.substr( tam - 2, tam ) ;
							   }
	}
}	

//--------------------------------------------------------

// Valida o CPF informado
function ValidaCPF(numCPF) {
	var campo="";
	// retira carateres não numericos do identificador( CPF )
	for (i=0; i<numCPF.length;i++){
		if ((numCPF.charAt(i)!=".") && (numCPF.charAt(i)!="-") && (numCPF.charAt(i)!="/")){campo = campo + numCPF.charAt(i)}}

	if (campo.length>0)
	{
		// exclui números inválidos
		if ((campo=="00000000000") || (campo == "11111111111") || (campo == "22222222222") || (campo == "33333333333") || (campo == "44444444444") || (campo == "55555555555") || (campo == "66666666666") || (campo == "77777777777") || (campo == "88888888888") || (campo == "99999999999") || (campo == "12345678901")){return false;}
		tam = campo.length;
		// calcula primeiro digito
		val1 = tam-1;
		soma = 0
		for (i=0;i<=tam-3;i++)
		{
			val2 = campo.substring(i,i+1);
			soma = soma + ( val2 * val1);
			val1 = val1 - 1;
		}
		dig1 = 11 - (soma % 11);
		if (dig1 == 10) dig1=0 ;
		if (dig1 == 11) dig1=0 ;
		//calcula segundo digito
		val1 = 11;
		soma = 0;
		for (i=0;i<=tam-2;i++)
		{
			soma = soma + (campo.substring(i,i+1) * val1);
			val1 = val1 - 1;
		}
		dig2 = 11 - (soma % 11);
		if (dig2 == 10) dig2=0;
		if (dig2 == 11) dig2=0;
		// valida digitos verificadores
		if ((dig1 + "" + dig2) == campo.substring(tam,tam-2))
		{
			return true;
		}
		else {	return false;	}
	}
}

//-----------------------------------------------------------
function enviar_img(url,ident,arq)
 {
   window.open(url +'?ident=' + ident +'&arq='+arq,'upload','width=350,height=200'); 
 }
//-----------------------------------------------------------
function confirma(num_form)
{
	conf = confirm('Deseja realmente Remover?');
	if(conf)
	  document.forms[num_form].submit();
	else
	  document.forms[num_form].act.checked = false;
}
//----------------------------------------------------------------------

function pop_up(url,x,y,rol)
 {
   /* 
    y -> Altura
    x -> Largura
	url -> página à abrir
	rol -> Rolagem (yes/no)
   */
   pos_y = (screen.availHeight/2)-(y/2);
   pos_x = (screen.availWidth/2)-(x/2);
   window.open(url,'login','width='+x+',directories=no,location=no,menubar=no,resizable=no,scrollbars='+rol+',status=no,toolbar=no,height='+y+',left='+pos_x+',top='+pos_y+''); 
}
//--------------------------------------------------------
function formataMascara(format, field)
{
	var result = "";
	var maskIdx = format.length - 1;
	var error = false;
	var valor = field.value;
	var posFinal = false;
	if( field.setSelectionRange ) 
	{
    	if(field.selectionStart == valor.length)
    		posFinal = true;
    }
	valor = valor.replace(/[^0123456789Xx]/g,'')
	for (var valIdx = valor.length - 1; valIdx >= 0 && maskIdx >= 0; --maskIdx)
	{
		var chr = valor.charAt(valIdx);
		var chrMask = format.charAt(maskIdx);
		switch (chrMask)
		{
		case '#':
			if(!(/\d/.test(chr)))
				error = true;
			result = chr + result;
			--valIdx;
			break;
		case '@':
			result = chr + result;
			--valIdx;
			break;
		default:
			result = chrMask + result;
		}
	}

	field.value = result;
	field.style.color = error ? 'red' : '';
	if(posFinal)
	{
		field.selectionStart = result.length;
		field.selectionEnd = result.length;
	}
	return result;
}

function getTeclaPressionada(evt)
{
	if(typeof(evt)=='undefined')
		evt = window.event;
	return(evt.keyCode ? evt.keyCode : (evt.which ? evt.which : evt.charCode));
}

// teclas 63230 a 63240 = safari
function isTeclaEspecial(key)
{
	return key<32||(key>=35&&key<=36)||(key>=37&&key<=40)||key==46||(key>=63230&&key<=63240);
}

function isTeclaRelevante(key)
{
	return (key == 8)||(key == 46)||(key>=48&&key<=57)||(key>=96&&key<=105);
}

function isCaracterRelevante(key)
{
	return (key == 120)||(key>=48&&key<=57);
}

function StopEvent(evt)
{
	if(document.all)evt.returnValue=false;
	else if(evt.preventDefault)evt.preventDefault();
}

function filtraTeclas(evt)
{
	var key = getTeclaPressionada(evt);
	if(isTeclaEspecial(key) || isTeclaRelevante(key))
		return true;
	StopEvent(evt);
	return false;
}

function filtraCaracteres(evt)
{
	var key = getTeclaPressionada(evt);
	if(isTeclaEspecial(key) || isCaracterRelevante(key))
		return true;
	StopEvent(evt);
	return false;
}

function saltaCampo(campo,proxCampo,tamanhoMaximo){
	var vr = campo.value;
	var tam = vr.length;
	if (tam >= tamanhoMaximo)
	{
		proxCampo.focus();
	}
}