// JavaScript Document


document.observe("dom:loaded", function() {
    if ($("carregando")) {
        $("carregando").style.display = "none";
    }

    Abre_Menu();
});


function Abre_Menu() {
    var menu = Le_Cookie('menu');

    if (menu) {
        if ($(menu)) {
            $(menu).style.display = "block";
        }
    }    
}

function Grava_Cookie(nome, valor) {
	var d = new Date();
	d.setTime(d.getTime()+(30*24*60*60*1000));
	var cookie_expire = "; expires="+d.toGMTString();
	document.cookie = nome + "=" + valor + cookie_expire;
}

function Le_Cookie(nome){
	if ( document.cookie.match(eval("/" + nome + "=/")) ) {
		return document.cookie.replace(eval("/^.*?" + nome + "=([^\\s;]*).*$/"), "$1");
	}else{
		return false;
	}
}

function findPosition( oElement ) {
	if( typeof( oElement.offsetParent ) != 'undefined' ) {
		for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent ) {
			posX += oElement.offsetLeft;
			posY += oElement.offsetTop;
		}
		return [ posX, posY ];
	} else {
		return [ oElement.x, oElement.y ];
	}
}

function colorsAll(cor, cookieName) {
    if (cor == false)
        cor = '006699';
        
	var corn = "#"+cor;
	document.body.style.backgroundColor = corn;

	if ($('usuario'))
	    $('usuario').style.backgroundColor = corn;

	if ($('bt_config'))
	    $('bt_config').style.backgroundColor = corn;

	if ($('usuario_nomargin'))
	    $('usuario_nomargin').style.backgroundColor = corn;

	if ($('bt_config_nomargin'))
	    $('bt_config_nomargin').style.backgroundColor = corn;

		
	Grava_Cookie(cookieName, cor);
	var R = cor.substring(0,2);
	var G = cor.substring(2,4);
	var B = cor.substring(4,6);
	R = parseInt(R,16);
	G = parseInt(G,16);
	B = parseInt(B,16);
	RGB = R + G + B;

    /*
	if ($('rodapeconteudo')) {
	    if (RGB > 280) {
	        $('rodapeconteudo').style.background = "transparent url(../img/rodape_br.png) no-repeat top center";
	    }
	    else {
	        $('rodapeconteudo').style.background = "transparent url(../img/rodape.png) no-repeat top center";
	    }
	}
	*/

	if ($('unidade')) {
	    if (RGB < 280) {
            if ($('unidade'))
                $('unidade').style.color = "#FFFFFF";
            if ($('sair'))
                $('sair').style.color = "#FFFFFF";
            if ($('lksair'))
                $('lksair').className = "lk";
        }else{
            if ($('unidade'))
                $('unidade').style.color = "#000000";
            if ($('sair'))
                $('sair').style.color = "#000000";
            if ($('lksair'))
                $('lksair').className = "link";
        }
    }
    
    
}

function abre_cor(){
	if($("cores").style.display != "block"){
	    $("cores").style.display = "block";
	    $("cores").style.height = document.body.offsetHeight;
	}
}

function fecha_cor(){
	$("cores").style.display = "none";
}

function abre_isso(id){
	var atual = $(id).getStyle('display');
	if(atual == 'block'){
		$(id).style.display = "none";
	}else{
		$(id).style.display = "block";
	}
}



//botoes e laterais
function abre_brilho(alvo) {
    var nome = alvo.id + "_br";
    if ($(nome))
        $(nome).className = "";
}

function fecha_brilho(alvo) {
    var nome = alvo.id + "_br";
    if ($(nome))
        $(nome).className = "bts_brilho";
}


function Validar_CNPJ(source, clientside_arguments)
{
  CNPJ = clientside_arguments.Value
  
  CNPJ = CNPJ.replace('.','');
  CNPJ = CNPJ.replace('.','');
  CNPJ = CNPJ.replace('/','');
  CNPJ = CNPJ.replace('-','');
  
  if(Vale = (CNPJ.length == 14))
     for(z=0;z<2;z++){
        Soma = 0;
        for(i=(5+z),j=0;j<(12+z);i--,j++){
           Soma += (CNPJ.substr(j, 1) * i);
           if(i == 2) i = 10;
        }
        Fim = (((Soma % 11) < 2) ? 0 : (((Soma % 11) == 10) ? 1 : 11-(Soma % 11)));
        if(!(Vale = (CNPJ.substr((12+z),1) == Fim)))
           break;
     }
     
  clientside_arguments.IsValid = Vale;   
  
  return Vale;
}

function EditText(campo, removeCaracterEspecial) {
    campo.value = removeAccents(campo.value.toUpperCase(), removeCaracterEspecial);
}

function removeAccents(strAccents, removeCaracterEspecial) {
    strAccents = strAccents.split('');
    strAccentsOut = new Array();
    strAccentsLen = strAccents.length;

    var accents = '\u00c0\u00c1\u00c2\u00c3\u00c4\u00c5\u00e0\u00e1\u00e2\u00e3\u00e4\u00e5\u00d2\u00d3\u00d4\u00d5\u00d5\u00d6\u00d8\u00f2\u00f3\u00f4\u00f5\u00f6\u00f8\u00c8\u00c9\u00ca\u00cb\u00e8\u00e9\u00ea\u00eb\u00f0\u00c7\u00e7\u00d0\u00cc\u00cd\u00ce\u00cf\u00ec\u00ed\u00ee\u00ef\u00d9\u00da\u00db\u00dc\u00f9\u00fa\u00fb\u00fc\u00d1\u00f1\u0160\u0161\u0178\u00ff\u00fd\u017d\u017e';
    var accentsOut = ['A', 'A', 'A', 'A', 'A', 'A', 'a', 'a', 'a', 'a', 'a', 'a', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'o', 'o', 'o', 'o', 'o', 'o', 'E', 'E', 'E', 'E', 'e', 'e', 'e', 'e', 'e', 'C', 'c', 'D', 'I', 'I', 'I', 'I', 'i', 'i', 'i', 'i', 'U', 'U', 'U', 'U', 'u', 'u', 'u', 'u', 'N', 'n', 'S', 's', 'Y', 'y', 'y', 'Z', 'z'];

    for (var y = 0; y < strAccentsLen; y++) {
        if (accents.indexOf(strAccents[y]) != -1) {
            strAccentsOut[y] = accentsOut[accents.indexOf(strAccents[y])];
        }
        else
            strAccentsOut[y] = strAccents[y];
    }

    strAccentsOut = strAccentsOut.join('');
    if (removeCaracterEspecial) {
        strAccentsOut = strAccentsOut.replace(/[^\w\s]/g, '');
    }

    return strAccentsOut;
}



