//VERIFICANDO O NAVEGADOR
if (navigator.appName.indexOf('Microsoft') != -1){   
    clientNavigator = "IE";   
}else{   
    clientNavigator = "Other";   
} 
//FLASH
function flash(versao, largura, altura, nome, modo, cor)
{
	/*
	versao = 8 .................. <- Versão do player
	largura = 200px ............. <- Largura do SWF
	altura = 200px .............. <- Altura do SWF
	id = nome ................... <- ID do arquivo
	nome = arquivo.swf .......... <- Caminho do SWF, essa opção não pode ser vazia
	modo = transparent/opaque ... <- Flash transparente ou opaco / pode ser vazia
	cor = #ffffff ............... <- Cor de fundo
	*/
	document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version="'+versao+',0,0,0" width="'+largura+'" height="'+altura+'">\n');
	document.write('<param name="allowScriptAccess" value="sameDomain" />\n');
	document.write('<param name="movie" value="../../includes/'+nome+'" />\n');
	document.write('<param name="wmode" value="'+modo+'" />\n');
	document.write('<param name="bgcolor" value="'+cor+'" />\n');
	document.write('<embed src="../../includes/'+nome+'" wmode="'+modo+'" bgcolor="'+cor+'" width="'+largura+'" height="'+altura+'" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />\n');
	document.write('</object>\n');
}

//POPUP
function popup(theURL, width, height, features) { 
	if(window.screen){
		per_ancho=(width/screen.width)*100;
		per_alto=(height/width)*100;
		win_ancho=(screen.width*per_ancho)/100;
		win_alto=(win_ancho*per_alto)/100;
		x=(screen.width-win_ancho)/2;
		y=(screen.height-win_alto)/2;
	}else{
		x=0;
		y=0;
		win_ancho=w;
		win_alto=y;
	}
	winfeatures=("top="+y+",left="+x+",width="+win_ancho+",height="+win_alto+","+features);
	window.open(theURL, 'popup', winfeatures);
}

//CONDICIONAR UM CAMPO A ACEITAR APENAS NÚMEROS
//onKeyPress="return(IsNumeric(this,event))"
function IsNumeric(fld, e) {
	var strCheck = '0123456789';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	key = String.fromCharCode(whichCode);
	if (strCheck.indexOf(key) == -1) {
		return false;
	}
}

//CONDICIONAR UM CAMPO A ACEITAR APENAS LETRAS
//onKeyPress="return(IsText(this,event))"
function IsText(fld, e){
	var strCheck = 'ABCDEFGHIJKLMNOPQRSTUVWXYZÃÁÉÍÓÜÊÔabcdefghijklmnopqrstuvwxyzáéíóãêôüaeiou- /.';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	key = String.fromCharCode(whichCode);
	if (strCheck.indexOf(key) == -1) return false;
}

//LAYER
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_showHideLayers() {
	//v6.0
	var i, p, v, obj, args = MM_showHideLayers.arguments;
	for (i=0; i<(args.length-2); i += 3) {
		if ((obj=MM_findObj(args[i])) != null) {
			v = args[i+2];
			var DivRef = document.getElementById(obj.id);
			var IfrRef = document.getElementById('menuSubBase');
			IfrRef.style.width = DivRef.offsetWidth;
			IfrRef.style.height = (DivRef.offsetHeight);
			IfrRef.style.top = DivRef.style.top;
			IfrRef.style.left = DivRef.style.left;
			IfrRef.style.zIndex = DivRef.style.zIndex-1;
			IfrRef.style.display = "block";
			//
			if (obj.style) {
				obj = obj.style;
				v = (v == 'show') ? 'visible' : (v == 'hide') ? 'hidden' : v;
			}
			obj.visibility = v;
			IfrRef.style.visibility = v;
		}
	}
}


//SHOW/HIDE LOGIN
function ShowLogin() {
    
	if (document.getElementById('revendaLogin').style.display == "none") {
		document.getElementById('revendaLogin').style.display = "block";
	} else {
		document.getElementById('revendaLogin').style.display = "none";
	}
}

//VALIDAR E-MAIL
function emailValidar(textarea) {
	var filter  = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/;
	var x = textarea.value;
	
	if (x!=""){
		if (filter.test(x)){
			return true;
		}else{
			alert('E-mail em formato incorreto!');
			textarea.focus();
			textarea.select();
		}
	}
}

//MENU
var anterior,subAnterior;

function menuShow(div){
	document.getElementById(div).style.display = ("none" == document.getElementById(div).style.display)  ? "block" : "none";
	
	if((anterior) && (anterior !=div)){
		document.getElementById(anterior).style.display = 'none';
	}
	 anterior = div;
}

//MENU SUB
function menuSubShow(div){
	document.getElementById(div).style.display = ("none" == document.getElementById(div).style.display)  ? "block" : "none";
	
	if((subAnterior) && (subAnterior !=div)){
		document.getElementById(subAnterior).style.display = 'none';
	}
	 subAnterior = div;
}

//MASCARA CPF
function maskCPF(CPF) {   
    var evt = window.event;   
    kcode=evt.keyCode;   
    if (kcode == 8) return;   
    if (CPF.value.length == 3) { CPF.value = CPF.value + '.'; }   
    if (CPF.value.length == 7) { CPF.value = CPF.value + '.'; }   
    if (CPF.value.length == 11) { CPF.value = CPF.value + '-'; }   
}

//MASCARA CARTAO
function maskCartao(CARTAO) {   
    var evt = window.event;   
    kcode=evt.keyCode;   
    if (kcode == 8) return;   
    if (CARTAO.value.length == 4) { CARTAO.value = CARTAO.value + '.'; }   
    if (CARTAO.value.length == 9) { CARTAO.value = CARTAO.value + '.'; }   
    if (CARTAO.value.length == 14) { CARTAO.value = CARTAO.value + '.'; }   
}

function validaCPF(cpf){   
 	erro = new String;
		cpfVerifica = cpf.value;
	if (cpf.value.length == 14){    
		cpfVerifica = cpfVerifica.replace('.', '');   
		cpfVerifica = cpfVerifica.replace('.', '');   
		cpfVerifica = cpfVerifica.replace('-', '');  
		var a = [];   
		var b = new Number;   
		var c = 11;   

		for (i=0; i<11; i++){   
			a[i] = cpfVerifica.charAt(i);   
			if (i < 9) b += (a[i] * --c);   
		}   

		if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }   
		b = 0;   
		c = 11;   

		for (y=0; y<10; y++) b += (a[y] * c--);   

		if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }   

		if ((cpfVerifica.charAt(9) != a[9]) || (cpfVerifica.charAt(10) != a[10])) {   
			erro = "Número de CPF inválido.";   
		}      
	}   
	else   
	{   
		if(cpf.value.length == 0)   
			return false   
		else   
			erro = "Número de CPF inválido.";   
	}   
	if (erro.length > 0) {   
			alert(erro);   
			cpf.focus();   
			return false;   
	}     
	return true;       
}   

//MASCARA TELEFONE
function maskTEL(input, evnt) {

        if (input.value.length == 0) {
            if (clientNavigator == "IE") {
                input.value = "(" + input.value;
            } else {
                if (evnt.keyCode == 0) {
                    input.value = "(" + input.value;
                }
            }
        }
    
	    if (input.value.length == 3){   
            if(clientNavigator == "IE"){   
                input.value = input.value+") ";   
            }else{   
                if(evnt.keyCode == 0){   
                   input.value = input.value+") ";   
                }   
            }   
        }
    	
	    if (input.value.length == 9){   
            if(clientNavigator == "IE"){   
                input.value +="-";   
            }else{   
                if(evnt.keyCode == 0){   
                    input.value +="-";  
                }   
            }   
        }
}

//VALIDAR TELEFONE
function validaTEL(input){
	if (input.value.length < 14 && input.value.length != 0){
	    alert("Número de telefone incompleto!");
	    input.focus();
	    input.value = "";
		return false;
	}
}


//MASCARA CEP
function maskCEP(input, evnt){   
//Ajusta máscara de CEP e só permite digitação de números   
    if (input.value.length == 5){   
        if(clientNavigator == "IE"){   
            input.value += "-";   
        }else{   
            if(evnt.keyCode == 0){   
                input.value += "-";   
            }   
        }   
    }
}

//VALIDA ORGÃO EXPEDIDOR
function validaORGAO(input){
	if (input.value.length < 6 && input.value.length != 0){
		alert ('Orgão Expedidor incompleto!');
		input.focus();
		return false;
	}
}
//MASCARA ORGÃO EXPEDIDOR
function maskORGAO(input, evnt){   
    if (input.value.length == 3){
        if(clientNavigator == "IE"){   
            input.value = input.value+"/";   
        }else{   
            if(evnt.keyCode == 0){   
                input.value = input.value+"/";  
            }   
        }  
		return false;
    }
}


//MASCARA DATA
function maskData(input, evnt){   
alert('sss');
//Ajusta máscara de Data e só permite digitação de números   
    if (input.value.length == 2 || input.value.length == 5){   
        if(clientNavigator == "IE"){   
            input.value += "/";   
        }else{   
            if(evnt.keyCode == 0){   
                input.value += "/";   
            }   
        }   
    } 
}

//VALIDAÇÃO DATA 
function VerificaData(input, digData){
	var bissexto = 0;
	var data = digData; 
	var tam = data.length;
	if (tam == 10) 
	{
		var dia = data.substr(0,2)
		var mes = data.substr(3,2)
		var ano = data.substr(6,4)
		if ((ano > 1900)||(ano < 2100))
		{
			switch (mes) 
			{
				case '01':
				case '03':
				case '05':
				case '07':
				case '08':
				case '10':
				case '12':
					if  (dia <= 31) 
					{
						return true;
					}
					break
				
				case '04':        
				case '06':
				case '09':
				case '11':
					if  (dia <= 30) 
					{
						return true;
					}
					break
				case '02':
					/* Validando ano Bissexto / fevereiro / dia */ 
					if ((ano % 4 == 0) || (ano % 100 == 0) || (ano % 400 == 0)) 
					{ 
						bissexto = 1; 
					} 
					if ((bissexto == 1) && (dia <= 29)) 
					{ 
						return true;                 
					} 
					if ((bissexto != 1) && (dia <= 28)) 
					{ 
						return true; 
					}            
					break                        
			}
		}
	}
	if (input.value){
		alert("A Data "+data+" é inválida!");
		input.focus();
		input.value="";
		return false;
	}
return false;
}

//MASCARA RG
function maskRG(RG) {   
    var evt = window.event;   
    kcode=evt.keyCode;
    if (kcode == 9) return;   
    if (RG.value.length == 2) { RG.value = RG.value + '.'; }   
    if (RG.value.length == 6) { RG.value = RG.value + '.'; }
	if (RG.value.length == 10) { RG.value = RG.value + '-'; }
}

//MASCARA R$
function formataMoeda(objTextBox, SeparadorMilesimo, SeparadorDecimal, e){
    var sep = 0;
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';
    var whichCode = (window.Event) ? e.which : e.keyCode;    
    // 13=enter, 8=backspace as demais retornam 0(zero)
    // whichCode==0 faz com que seja possivel usar todas as teclas como delete, setas, etc    
    if ((whichCode == 13) || (whichCode == 0) || (whichCode == 8))
    	return true;
    key = String.fromCharCode(whichCode); // Valor para o código da Chave
 
 
    if (strCheck.indexOf(key) == -1) 
    	return false; // Chave inválida
    len = objTextBox.value.length;
    for(i = 0; i < len; i++)
        if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) 
        	break;
    aux = '';
    for(; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) 
        	aux += objTextBox.value.charAt(i);
    aux += key;
    len = aux.length;
    if (len == 0) 
    	objTextBox.value = '';
    if (len == 1) 
    	objTextBox.value = '0'+ SeparadorDecimal + '0' + aux;
    if (len == 2) 
    	objTextBox.value = '0'+ SeparadorDecimal + aux;
    if (len > 2) {
        aux2 = '';
        for (j = 0, i = len - 3; i >= 0; i--) {
            if (j == 3) {
                aux2 += SeparadorMilesimo;
                j = 0;
            }
            aux2 += aux.charAt(i);
            j++;
        }
        objTextBox.value = '';
        len2 = aux2.length;
        for (i = len2 - 1; i >= 0; i--)
        	objTextBox.value += aux2.charAt(i);
        objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);
    }
    return false;
}
function IsText(fld, e){
	var strCheck = 'ABCDEFGHIJKLMNOPQRSTUVWXYZÃÁÉÍÓÜÊÔabcdefghijklmnopqrstuvwxyzáéíóãêôüaeiou- /.';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	key = String.fromCharCode(whichCode);
	if (strCheck.indexOf(key) == -1) return false;
}
