// BinaryChoice - JScripts Library // 
// Rogério Chiavegatti :: 09/2005

// Hide Status Bar
function hidestatus(){
var statusmsg="CD Equipamentos"
window.status=statusmsg
return true
}

if (document.layers)
document.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT)

document.onmouseover=hidestatus
document.onmouseout=hidestatus
//  Janelas Pop-UP ( com auto-fechamento)
// Uso: javascript: WindowsShow('elemento.ext','150','200','yes')
<!--
function WindowsShow(elemento,wsize,hsize,scro)
{
if (navigator.appName == "Microsoft Internet Explorer")
{
wsize = parseInt(wsize) + 20;
hsize = parseInt(hsize) + 20;
}
BinaryWindow = window.open(elemento,"WinView","resizable=no,toolbar=no,status=no,menubar=no,scrollbars="+scro+",screenX=10,screenY=10,top=10,left=10,dependent=yes,width="+wsize+",height="+hsize+"")
if(false == BinaryWindow.closed) 
 {
   BinaryWindow.close ();
   BinaryWindow = window.open(elemento,"WinView","resizable=no,toolbar=no,status=no,menubar=no,scrollbars="+scro+",screenX=10,screenY=10,top=10,left=10,dependent=yes,width="+wsize+",height="+hsize+"")
 }
}
// -->

// Formatação de campos para moeda 

function FormataValor(campo,tammax,teclapres) 
 {
 	//uso:
	//<input type="Text" name="fat_vr_bruto" maxlength="17" onKeyDown="FormataValor(this,17,event)">

	var tecla = teclapres.keyCode;
	vr = campo.value;
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( ",", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	tam = vr.length;

	if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }

	if (tecla == 8 ){	tam = tam - 1 ; }
		
	if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){
		if ( tam <= 2 ){ 
	 		campo.value = vr ; }
	 	if ( (tam > 2) && (tam <= 5) ){
	 		campo.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 6) && (tam <= 8) ){
	 		campo.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
	 	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 ) ; }
	 	if ( (tam >= 12) && (tam <= 14) ){
	 		campo.value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 15) && (tam <= 17) ){
	 		campo.value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ;}
	}			
}

// Formata campo de CEP Ex: 13420-669

function formataCEP(formato, keypress, objeto)
{
//onKeyPress="formataCEP('CEP', window.event.keyCode, this);"
campo = eval(objeto);
if(window.event) {
		// Para o IE, e.keyCode ou window.event.keyCode
		keypress= keypress.keyCode; 
	}
	else if(keypress.which) {
		// para o KMOS usar o which
		keypress = keypress.which; 
	}
if (formato=='CEP')
	{
	caracteres = '01234567890';
	separacoes = 1;
	separacao1 = '-';
	conjuntos = 2;
	conjunto1 = 5;
	conjunto2 = 3;
	if ((caracteres.search(String.fromCharCode (keypress))!=-1) && campo.value.length < 
	(conjunto1 + conjunto2 + 1))
		{
		if (campo.value.length == conjunto1) 
		   campo.value = campo.value + separacao1;
		}
	else 
		keypress.returnValue = false;
	}
}


function trim(valorTrim){
	var len = valorTrim.length;
	for(var x=0; x<len; x++) if(valorTrim.charCodeAt(x)!=32 && valorTrim.charCodeAt(x)!=10 && valorTrim.charCodeAt(x)!=13) break;
	valorTrim = valorTrim.substring(x,len);
	len = valorTrim.length;
	for(x=len-1; x>1; x--) if(valorTrim.charCodeAt(x)!=32 && valorTrim.charCodeAt(x)!=10 && valorTrim.charCodeAt(x)!=13) break;
	valorTrim = valorTrim.substring(0,x+1);
	return valorTrim;
}

function getFocus(formulario, campo){
	eval("document." + formulario + "." + campo + ".focus()")
}

// Formata campo vazio, verifica o preenchimento do Form

function camposVazios(formulario){
	//se retornar false quer dizer que o formulário não possui nenhum campo em branco
	var i, temp;
	var args = camposVazios.arguments;
	var nomeCampo = false;
	
	for(i=1; i<args.length; i++){
		if(eval("trim(document." + formulario + "." + args[i] + ".value)")=="" ){
			nomeCampo = args[i];
			break;
		}
	}
	return nomeCampo;
}

//coloque os caracteres que deseja bloquear no argumento da função
//ex: blockChar('0123456789') -- Bloqueia os numeros
//se quiser bloquear pelo codigo ASC coloque outros parametros na frente do primeiro;
//ex: blockChar('', 13, 85, 94)

function blockChar(args){
	
	for(var i=0; i<args.length;  i++){
		if (window.event.keyCode == args.charCodeAt(i)){
			event.returnValue = false;
			break;
		}
	}//end for

	if(blockChar.arguments.length > 1){
		for(var i=1; i<blockChar.arguments.length; i++){
			if (!isNaN(blockChar.arguments[i]) && window.event.keyCode == blockChar.arguments[i]){
				event.returnValue = false;
				break;
			}
		}//end for
	}// end if
}//eend function


// Formata campos: somente para números


function soNumeros(){
	if (window.event.keyCode < 48 || window.event.keyCode > 57)
	event.returnValue = false;
}

function popup(caminho,nome,largura,altura,rolagem) 
{
//	var esquerda = 0;
//	var cima = 0;
//	var largura = screen.width - 10;
//	var altura = screen.height - 70;
	var esquerda = (screen.width - largura) / 2;
	var cima = (screen.height - altura) / 2 -50;
	window.open(caminho,nome,'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=' + rolagem + ',resizable=yes,copyhistory=no,top=' + cima + ',left=' + esquerda + ',width=' + largura + ',height=' + altura);
//popup('lrm_default.cfm','logisticaMaster','750','550','yes');
}


// Esconder Camadas

function showHideLayers() { //v2.0
  var i, visStr, args, theObj;
  args = showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) { //with arg triples (objNS,objIE,visStr)
    visStr   = args[i+2];
    if (navigator.appName == 'Netscape' && document.layers != null) {
      theObj = eval(args[i]);
      if (theObj) theObj.visibility = visStr;
    } else if (document.all != null) { //IE
      if (visStr == 'show') visStr = 'visible'; //convert vals
      if (visStr == 'hide') visStr = 'hidden';
      theObj = eval(args[i+1]);
      if (theObj) theObj.style.visibility = visStr;
  } }
}


function repeatString(str, count){
	var novaStr="";
	for(var i=0; i<count; i++) novaStr = novaStr + str;
	return novaStr
}


// Formatar Campos: Validar CNPJ


function validarCNPJ(strCNPJ){
	if (trim(strCNPJ) == "")
		return true;
	
	var
		strDV = strCNPJ.substr(12, 2),
		intSoma,
		intDigito = 0,
		strControle = "",
		strMultiplicador = "543298765432";
		strCNPJ = strCNPJ.substr(0, 12);

	for(var j = 1; j <= 2; j++){
		intSoma = 0;
		for(var i = 0; i <= 11; i++){
			intSoma += (parseInt(strCNPJ.substr(i, 1), 10) * parseInt(strMultiplicador.substr(i, 1), 10))
		}
		if(j == 2){intSoma += (2 * intDigito)}
		intDigito = (intSoma * 10) % 11;
		if(intDigito == 10){intDigito = 0}
		strControle += intDigito.toString();
		strMultiplicador = "654329876543";
	}
	return(strControle == strDV);
}


// Formatar Campos:: Validar Emails

function validarMail(email) 
{
	 if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email))
 	   {
			return true;
       }
	 else
	   { 
	      return false;
	   }
}

// Comprar Datas


function comparaData(data1, data2){
	//------------------------------
	// Uso: comparaData("10/01/2002", "10/03/2002");
	// data1 e data2 no formato "dd/mm/aaaa"
	// se retornar 1 "data1" é maior
	// se retornar 2 "data2" é maior
	// se retornar 0 "data1" é igual a "data2"
	//------------------------------
	var dataDiff = transformaData(data2) - transformaData(data1);
	var maiorData;

	if(dataDiff > 0){dataSaida = 2;}
	else if(dataDiff < 0){dataSaida = 1;}
	else {dataSaida == 0;}

	return dataSaida;
}


// Trasnformar o Formato das Datas


function transformaData(data){// data no formato dd/mm/aaaa
	var data = trim(data);
	argDia = parseInt(data.substring(0,2), 10);
	argMes = parseInt(data.substring(3,5), 10) - 1;
	argAno = parseInt(data.substring(6,10),10);
	data = new Date(argAno, argMes, argDia);
	return data;
}

function isDate(data){
	var data = trim(data);
	if(data != ""){
		dia = parseInt(data.substring(0,2), 10);
		mes = parseInt(data.substring(3,5), 10);
		ano = parseInt(data.substring(6,10),10);
		//Verifica se o tamanho da string é 10 
		if(data.length < 10 || (mes < 1 || mes > 12) || (dia > 31 || dia < 1)) 
			return false;
		var quantDias;
		if(mes==4 || mes==6 || mes==9 || mes==11)//mes com 30 dias
			quantDias = 30;
		else if(mes==1 || mes==3 || mes==5 || mes==7 || mes==8 || mes==10 || mes==12)//mes com 31	
			quantDias = 31;
		else if(mes==2){ //mes com 28
			if((ano % 4 == 0) && (ano % 100 != 0 || ano % 400 ==0)) quantDias = 29; //verifica se é bissexto
			else quantDias = 28;
		}
		if(dia > quantDias || dia < 1) return false;
	}
	return true;
}


// Formata campos de moeda (segunda function)


function FormataValor(campo,tammax,teclapres) 
 {
 	//uso:
	//<input type="Text" name="fat_vr_bruto" maxlength="17" onKeyDown="FormataValor(this,17,event)">

	var tecla = teclapres.keyCode;
	vr = campo.value;
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( ",", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	tam = vr.length;

	if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }

	if (tecla == 8 ){	tam = tam - 1 ; }
		
	if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){
		if ( tam <= 2 ){ 
	 		campo.value = vr ; }
	 	if ( (tam > 2) && (tam <= 5) ){
	 		campo.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 6) && (tam <= 8) ){
	 		campo.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
	 	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 ) ; }
	 	if ( (tam >= 12) && (tam <= 14) ){
	 		campo.value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 15) && (tam <= 17) ){
	 		campo.value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ;}
	}			
}


// Formata Data : Segunda Function


function FormataData(nomecampo,teclapres) {
//	onKeydown="FormataData('nome_campo',event);"
	var tecla = teclapres.keyCode;
	vr = document.form1[nomecampo].value;
	vr = vr.replace( ".", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	tam = vr.length + 1;

	if ( tecla != 9 && tecla != 8 ){
		if ( tam > 2 && tam < 5 )
			document.form1[nomecampo].value = vr.substr( 0, tam - 2  ) + '/' + vr.substr( tam - 2, tam );
		if ( tam >= 5 && tam <= 10 )
			document.form1[nomecampo].value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 ); }
}

function FormataData2(nomecampo,teclapres) {
//	onKeydown="FormataData('nome_campo',event);"
	var tecla = teclapres.keyCode;
	vr = document.form2[nomecampo].value;
	vr = vr.replace( ".", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	tam = vr.length + 1;

	if ( tecla != 9 && tecla != 8 ){
		if ( tam > 2 && tam < 5 )
			document.form2[nomecampo].value = vr.substr( 0, tam - 2  ) + '/' + vr.substr( tam - 2, tam );
		if ( tam >= 5 && tam <= 10 )
			document.form2[nomecampo].value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 ); }
}

// Formata a Adição aos Favoritos


function addBookmark(url, titulo)
{
	//Parâmetros: 
	//url: Endereço a ser adicionado
	//titulo: Título que aparecerá nos Favoritos
	if (document.all)
		window.external.AddFavorite(url, titulo)
}

// Validação do CEP

function validarCep(cep)
{
	txCep = trim(cep);
	if(txCep.length == 0)
		return false;

	txCep = txCep.replace("-","");
	txCep = txCep.replace(".","");
	if(txCep.length != 8 || isNaN(txCep))
		return false;
	else
		return true;
}

// Exibição de Camadas e Divs - Controle de exposição

//  usado para exibir e omitir as subcategorias

//------------------------------------------------------------------------------------------------------------------------
// Menu em camadas
//------------------------------------------------------------------------------------------------------------------------

function SwitchMenu(obj){
	if(document.getElementById){
	var el = document.getElementById(obj);
	var ar = document.getElementById("masterdiv").getElementsByTagName("span"); 
		if(el.style.display != "block"){ 
			for (var i=0; i<ar.length; i++){
				if (ar[i].className=="submenu") 
				ar[i].style.display = "none";
			}
			el.style.display = "block";
		}else{
			el.style.display = "none";
		}
	}
}
//------------------------------ Switchmenu2
function SwitchMenu2(obj){
	if(document.getElementById){
	var el = document.getElementById(obj);
	var ar = document.getElementById("masterdiv2").getElementsByTagName("span"); //DynamicDrive.com change
		if(el.style.display != "block"){ //DynamicDrive.com change
			for (var i=0; i<ar.length; i++){
				if (ar[i].className=="submenu2") //DynamicDrive.com change
				ar[i].style.display = "none";
			}
			el.style.display = "block";
		}else{
			el.style.display = "none";
		}
	}
}
//---------------------------------------------------------------
