// JavaScript Document

//-------------------------------------------------------------------------------------------------------------------------------
// w3cookies
// Forma de uso: 
// Criar: w3cookies.create('nome_do_cookie', 'valor', dias_para_expirar);
// Ler: w3cookies.read('nome_do_cookie');
// Deletar: w3cookies.erase('nome_do_cookie');
var w3cookies = {
	date: new Date(),
	create: function(strName, strValue, intDays) {
		if ( intDays ) {
			this.date.setTime(this.date.getTime()+(intDays*24*60*60*1000));
			var expires = "; expires=" + this.date.toGMTString();
		} else {
			var expires = "";
		}
		document.cookie = strName + "=" + strValue + expires + "; path=/";
	},

	read: function(strName) {
		var strNameIgual = strName + "=";
		var arrCookies = document.cookie.split(";");
		for ( var i = 0, strCookie; strCookie = arrCookies[i]; i++ ) {
			while ( strCookie.charAt(0) == " ") {
				strCookie = strCookie.substring(1,strCookie.length);
			}
			if ( strCookie.indexOf(strNameIgual) == 0 ) {
				return strCookie.substring(strNameIgual.length,strCookie.length);
			}
		}
		return null;
	},
	erase: function(strName) {
		this.create(strName,"",-1);
	}
}

//menuLateral(subMenu)
function menuLateral(idMenu, url){

	var idSeta = 'idSeta'+idMenu;	
	idMenu = 'idMenu'+idMenu;
	var menu = w3cookies.read('menu');

	if(document.getElementById(idMenu).style.display == 'none'){
		document.getElementById(idMenu).style.display = 'block';
		document.getElementById(idSeta).src = '/ras/imagens/menuLateral/IcoMenos.gif';

		if(menu != null){
			if(menu.indexOf('|'+idMenu+'|') == -1) menu += '|'+idMenu+'|';
		}else{
			menu = '|'+idMenu+'|';
		}
	}else{
		document.getElementById(idMenu).style.display = 'none';
		document.getElementById(idSeta).src = '/ras/imagens/menuLateral/IcoMais.gif';

		menu = menu.replace('|'+idMenu+'|', '');
	}

	w3cookies.create('menu', menu, 1);

	if(url.length > 0) window.location.href = url;
}

//function txtBoxFormat(objeto, sMask, evtKeyPress) {
function txtBoxFormat(objeto, sMask, evtKeyPress) {
	var nTecla;

	if(document.all){
		nTecla = evtKeyPress.keyCode;
	}else if(document.layers){
		nTecla = evtKeyPress.which;
	}

	sValue = objeto.value;

	// Limpa todos os caracteres de formatacao que ja estiverem no campo.
	sValue = sValue.toString().replace("-", "");
	sValue = sValue.toString().replace("-", "");
	sValue = sValue.toString().replace(".", "");
	sValue = sValue.toString().replace(".", "");
	sValue = sValue.toString().replace("/", "");
	sValue = sValue.toString().replace("/", "");
	sValue = sValue.toString().replace(":", "");
	sValue = sValue.toString().replace(":", "");
	sValue = sValue.toString().replace("(", "");
	sValue = sValue.toString().replace("(", "");
	sValue = sValue.toString().replace(")", "");
	sValue = sValue.toString().replace(")", "");
	sValue = sValue.toString().replace(" ", "");
	sValue = sValue.toString().replace(" ", "");

	fldLen = sValue.length;
	mskLen = sMask.length;
	i = 0;
	nCount = 0;
	sCod = "";
	mskLen = fldLen;

	while (i <= mskLen) {
		bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/") || (sMask.charAt(i) == ":"))
		bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))

		if (bolMask){
			sCod += sMask.charAt(i);
			mskLen++; 
		}else{
			sCod += sValue.charAt(nCount);
			nCount++;
		}

		i++;
	}

	objeto.value = sCod;

	if (nTecla != 8) {// backspace
		if (sMask.charAt(i-1) == "9") { // apenas números...
			return ((nTecla > 47) && (nTecla < 58)); } // números de 0 a 9
		else { // qualquer caracter...
			return true;
		}
	}else{
		return true;
	}
}

//-------------------------------------------------------------------------------------------------------------------------------
//function validaEmail(campo)
//Função que valida o e-mail informado
function validaEmail(campo) {
    var A 					= campo.indexOf('@');
    var AA 					= campo.indexOf('@', A + 1) != -1;
    var ponto 				= campo.indexOf(".") < 1;
    var espaco 				= campo.indexOf(" ") != -1;
    var pontoAntesDoA 		= campo.indexOf(".@") > 0;
    var pontoDepoisDoA 		= campo.indexOf("@.") > 0;
    var pontoDepoisDoBr 	= campo.indexOf(".com.br.") > 0;
    var barra 				= campo.indexOf("/") > 0;
    var abreColchete 		= campo.indexOf("[") > 0;
    var fechaColchete 		= campo.indexOf("]") > 0;
    var abreParentese 		= campo.indexOf("(") > 0;
    var fechaParentese 		= campo.indexOf(")") > 0;
    var pontoConsecutivo 	= campo.indexOf("..") > 0;
    var aspas				= campo.indexOf('"') > 0;

    if(A < 2 || AA || ponto || espaco || pontoAntesDoA || pontoDepoisDoA || pontoDepoisDoBr || barra || abreColchete || fechaColchete || abreParentese || fechaParentese || pontoConsecutivo || aspas) {
        return false;
    }

    return true;
}

//function validaCpf(campo)
function validaCpf(campo){
	campo = campo.replace(/\./g, '');
	campo = campo.replace(/\-/g, '');

	if(isNaN(campo)) return false;

	var i;
	var c = campo.substr(0, 9);
	var dv = campo.substr(9, 2);
	var d1 = 0;

	for (i = 0; i < 9; i++){
		d1 += c.charAt(i)*(10-i);
	}

	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 < 9; i++){
		d1 += c.charAt(i)*(11-i);
	}

	d1 = 11 - (d1 % 11);

	if (d1 > 9) d1 = 0;

	if (dv.charAt(1) != d1) return false;

	return true;
}

// valida CNPJ

	var NUM_DIGITOS_CNPJ = 14;
	var NUM_DGT_CNPJ_BASE = 8;

String.prototype.lpad = function(pSize, pCharPad)
{
	var str = this;
	var dif = pSize - str.length;
	var ch = String(pCharPad).charAt(0);
	for (; dif>0; dif--) str = ch + str;
	return (str);
}

String.prototype.trim = function()
{
	return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

//unformatNumber(pNum)
function unformatNumber(pNum)
{
	return String(pNum).replace(/\D/g, "").replace(/^0+/, "");
}

//formatCpfCnpj(pCpfCnpj, pUseSepar, pIsCnpj)
function formatCpfCnpj(pCpfCnpj, pUseSepar, pIsCnpj)
{
	if (pIsCnpj==null) pIsCnpj = false;
	if (pUseSepar==null) pUseSepar = true;
	var maxDigitos = pIsCnpj? NUM_DIGITOS_CNPJ: NUM_DIGITOS_CPF;
	var numero = unformatNumber(pCpfCnpj);

	numero = numero.lpad(maxDigitos, '0');
	if (!pUseSepar) return numero;

	if (pIsCnpj)
	{
		reCnpj = /(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})$/;
		numero = numero.replace(reCnpj, "$1.$2.$3/$4-$5");
	}
	else
	{
		reCpf  = /(\d{3})(\d{3})(\d{3})(\d{2})$/;
		numero = numero.replace(reCpf, "$1.$2.$3-$4");
	}
	return numero;
}

//function dvCpfCnpj(pEfetivo, pIsCnpj)
function dvCpfCnpj(pEfetivo, pIsCnpj)
{
	if (pIsCnpj==null) pIsCnpj = false;
	var i, j, k, soma, dv;
	var cicloPeso = pIsCnpj? NUM_DGT_CNPJ_BASE: NUM_DIGITOS_CPF;
	var maxDigitos = pIsCnpj? NUM_DIGITOS_CNPJ: NUM_DIGITOS_CPF;
	var calculado = formatCpfCnpj(pEfetivo, false, pIsCnpj);
	calculado = calculado.substring(2, maxDigitos);
	var result = "";

	for (j = 1; j <= 2; j++)
	{
		k = 2;
		soma = 0;
		for (i = calculado.length-1; i >= 0; i--)
		{
			soma += (calculado.charAt(i) - '0') * k;
			k = (k-1) % cicloPeso + 2;
		}
		dv = 11 - soma % 11;
		if (dv > 9) dv = 0;
		calculado += dv;
		result += dv
	}

	return result;
}

//function isCnpj(pCnpj)
function isCnpj(pCnpj)
{
	var numero = formatCpfCnpj(pCnpj, false, true);
	var base = numero.substring(0, NUM_DGT_CNPJ_BASE);
	var ordem = numero.substring(NUM_DGT_CNPJ_BASE, 12);
	var digitos = dvCpfCnpj(base + ordem, true);
	var algUnico;

	// Valida dígitos verificadores
	if (numero != base + ordem + digitos) return false;

	/* Não serão considerados válidos os CNPJ com os seguintes números BÁSICOS:
	 * 11.111.111, 22.222.222, 33.333.333, 44.444.444, 55.555.555,
	 * 66.666.666, 77.777.777, 88.888.888, 99.999.999.
	 */
	algUnico = numero.charAt(0) != '0';
	for (i=1; algUnico && i<NUM_DGT_CNPJ_BASE; i++)
	{
		algUnico = (numero.charAt(i-1) == numero.charAt(i));
	}
	if (algUnico) return false;

	if (ordem == "0000") return false;
	return (base == "00000000"
		|| parseInt(ordem, 10) <= 300 || base.substring(0, 3) != "000");
}

function ordenaLista(spn, url){
	document.getElementById('filtroAbc').value = spn.innerHTML;
	document.getElementById('form').action = url;

	document.getElementById('pagAtual').value = 0;
	document.getElementById('numPag').value = 0;
	document.form.submit();
}
//paginacao(img)
function paginacao(img, url){
	if(img.src.indexOf('Dsb.jpg') == -1){
		document.getElementById('form').action = url;

		if(img.id == 'arwFirst'){
			document.getElementById('pagAtual').value = 0;
		}else if(img.id == 'arwPrev'){
			document.getElementById('pagAtual').value--;
		}else if(img.id == 'arwNext'){
			document.getElementById('pagAtual').value++;
		}else if(img.id == 'arwLast'){
			document.getElementById('pagAtual').value = document.getElementById('numPag').value - 1;
		}

		document.form.submit();
	}
}

//btnNumRegSubmit(url)
function btnNumRegSubmit(url){
	document.form.action = url;
	document.getElementById('pagAtual').value = 0;
	document.getElementById('numPag').value = 0;
	document.form.submit();
}
