// JavaScript Document
//////////////////////////////////////////////////////////////////////////////////////
//ADM

//Vinculação de secoes no conteudo
function loadCombos(section, idElm, idSection) {
	if ($("#"+idElm+":not(:has(input))")) {
		$("#"+idElm).html("").load("/adm/ajaxhelper/sections.asp", {tipo: section, secaoPai: idSection});
	}
}

//Função mask utilizada para criar máscararas para campos

function mask(o,opt){
    v_obj=o
    v_opt=opt
    setTimeout("execMask()",1)
}

function execMask(){
    v_obj.value=doMask(v_opt, v_obj.value)
}


//----------------------------------------------------------------------------------------------//

function doMask(option, value) {
	value = value.replace(/\D/g,"");
	
	switch (option) {
		case "phone": // (11) 1111-1111
			value=value.replace(/^(\d{2})(\d)/g,"($1) $2");
    		value=value.replace(/(\d{4})(\d)/,"$1-$2");
		break;
		case "vox": // 1111-11
			value=value.replace(/^(\d{4})(\d)/g,"$1-$2");
		break;
		case "zipcode": // CEP: 11111-11
			value=value.replace(/^(\d{5})(\d)/g,"$1-$2");
		break;
		case "date": // 11/11/1111
			value=value.replace(/^(\d{2})(\d)/g,"$1/$2");
			value=value.replace(/(\d{2})(\d)/,"$1/$2");
		break;
		case "decimal": // ...111.111.111,11
			value = value.replace(/(\d)(\d{2})$/g, "$1,$2");
			value = value.replace(/(\d)(\d{3},\d{2})/g, "$1.$2");
			value = value.replace(/(\d)(\d{3}\.\d{3},\d{2})/g, "$1.$2");
		break;
		case "kilo": // ...1111,111
			value=value.replace(/(\d)(\d{3})$/g,"$1,$2");
		break;
		case "cpf": // 111.111.111-11
			value=value.replace(/^(\d{3})(\d)/g, "$1.$2");
			value=value.replace(/(\d{3})(\d)/, "$1.$2");
			value=value.replace(/(\d{3})(\d)/, "$1-$2");
		break;
		case "id": // RG: 11.111.111-1 || 11.111.111-x
			value=value.replace(/^(\d{2})(\d)/g, "$1.$2");
			value=value.replace(/(\d{3})(\d)/, "$1.$2");
			value=value.replace(/(\d{3})(\d|x)/, "$1-$2");
		break;
		case "cnpj": // CNPJ: 00.000.000/0000-00
			value=value.replace(/(\d)(\d{2})$/g, "$1-$2");
			value=value.replace(/(\d)(\d{4}-\d{2})/g, "$1/$2");
			value=value.replace(/(\d)(\d{3}\/\d{4}-\d{2})/g, "$1.$2");
			value=value.replace(/(\d)(\d{3}\.\d{3}\/\d{4}-\d{2})/g, "$1.$2");
		break;
	}
	return value;
}
function sanitize(obj, patt) {
	var objVal = obj.value;
	var objId = obj.id;
	var regex = new RegExp(patt, "g");
	
   objVal=objVal.replace(regex,"");  
   document.getElementById(objId).value= objVal; 
}
// Função utilizada para bloquear a digitação de caracteres que não sejam números
function noDigit( o ){
	var objVal = o.value;
	var objId = o.id;
	
    objVal=objVal.replace(/\D/g,"");  
    document.getElementById( objId ).value= objVal;  
}

//Script para validação do CNPJ
function validaCNPJ(CNPJID) {
	CNPJ = CNPJID.value;
	erro = new String;
	if (CNPJ.length < 18) erro += "É necessario preencher corretamente o numero do CNPJ! \n\n";
	if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")){
	if (erro.length == 0) erro += "É necessario preencher corretamente o numero do CNPJ! \n\n";
	}
	//substituir os caracteres que nao sao numeros
	if(document.layers && parseInt(navigator.appVersion) == 4){
		x = CNPJ.substring(0,2);
		x += CNPJ.substring(3,6);
		x += CNPJ.substring(7,10);
		x += CNPJ.substring(11,15);
		x += CNPJ.substring(16,18);
		CNPJ = x;
	} else {
		CNPJ = CNPJ.replace(".","");
		CNPJ = CNPJ.replace(".","");
		CNPJ = CNPJ.replace("-","");
		CNPJ = CNPJ.replace("/","");
	}
	var nonNumbers = /\D/;
	if (nonNumbers.test(CNPJ)) erro += "A verificacao de CNPJ suporta apenas numeros! \n\n";
		var a = [];
		var b = new Number;
		var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
		for (i=0; i<12; i++){
		a[i] = CNPJ.charAt(i);
		b += a[i] * c[i+1];
	}
		if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11-x }
		b = 0;
		for (y=0; y<13; y++) {
		b += (a[y] * c[y]);
	}
	if ((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11-x; }
	if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])){
		erro +="Digito verificador com problema!";
	}
	if (erro.length > 0){
		alert(erro);
		return false;
	} else {
		//alert("CNPJ valido!");
	}
		return true;
	}
//FIM Script para validação do CNPJ


//////////////////////////////////////////////////////////////////////////////////////
//Função para exibir data
hoje = new Date()
dia = hoje.getDate()
dias = hoje.getDay()
mes = hoje.getMonth()
ano = hoje.getYear()

if (ano < 2000)    // Y2K Fix, Isaac Powell
	ano = ano + 1900;
	
if (dia < 10)
	dia = "0" + dia

function CriaArray (n) {
	this.length = n
}

NomeDia = new CriaArray(7)
NomeDia[0] = "Domingo"
NomeDia[1] = "Segunda-feira"
NomeDia[2] = "Ter&ccedil;a-feira"
NomeDia[3] = "Quarta-feira"
NomeDia[4] = "Quinta-feira"
NomeDia[5] = "Sexta-feira"
NomeDia[6] = "S&aacute;bado"
NomeMes = new CriaArray(12)
NomeMes[0] = "janeiro"
NomeMes[1] = "fevereiro"
NomeMes[2] = "mar&ccedil;o"
NomeMes[3] = "abril"
NomeMes[4] = "maio"
NomeMes[5] = "junho"
NomeMes[6] = "julho"
NomeMes[7] = "agosto"
NomeMes[8] = "setembro"
NomeMes[9] = "outubro"
NomeMes[10] = "novembro"
NomeMes[11] = "dezembro"

function escreveData(){
	data = dia + " de " + NomeMes[mes] + " de " + ano;
	return data;
}

function escreveDia (){
	return NomeDia[dias];
}
//Função para exibir data
//////////////////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////////////////////
//Funções Dreamweaver
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

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_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_displayStatusMsg(msgStr) { //v1.0
  status=msgStr;
  document.MM_returnValue = true;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
//Funções Dreamweaver
//////////////////////////////////////////////////////////////////////////////////////

//ADM
//////////////////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////////////////////
//Intranet

//////////////////////////////////////////////////////////////////////////////////////
///Saudação: Bom dia, tarde e noite
var hoje = new Date();
var hours = hoje.getHours();
var saudacao = '';
if (hours >=0 && hours <12){
	saudacao = "Bom dia"
}
if(hours >=12 && hours <18){
	saudacao = "Boa tarde"
}
//if(hours >18 && hours <=23){
if(hours >=18){
	saudacao = "Boa noite";
}
function escreveSaudacao(){
	document.write(saudacao)
}
///Saudação: Bom dia, tarde e noite
//////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////
///Iframe ajustável
function iframeAutoHeight(altura){
    //by Micox - elmicox.blogspot.com - elmicox.com - webly.com.br  
    if(navigator.appName.indexOf("Internet Explorer")>-1){ //ie sucks
        var func_temp = function(){
            var val_temp = altura.contentWindow.document.body.scrollHeight
            altura.style.height = val_temp + "px";
        }
        setTimeout(function() { func_temp() },100) //ie sucks
    }else{
        var val = altura.contentWindow.document.body.parentNode.offsetHeight
        altura.style.height= val + "px";
    }    
}
///Iframe ajustável
//////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////
///Google Analytics
/*import("http://www.google-analytics.com/urchin.js");
_uacct = "UA-3143166-1";
urchinTracker();*/
///Google Analytics
//////////////////////////////////////////////////////////////////////////////////////

function mostraForm(id) {
	if (id == "juridica") {
		$("#fisica").fadeOut("fast");
	} else {
		$("#juridica").fadeOut("fast");
	}
	$("#"+id).fadeIn();
}


function init() { //Function executed after the page loads
	var dia = escreveDia();
	var data = escreveData();
	if (document.getElementById("date")) {
		document.getElementById("date").innerHTML = dia + ", " + data;
	}
	$("a[rel*=external]").attr("target", "_blank");
	if ($("#sub-menu ul").is(".menu-accordion")) {
		$("#sub-menu ul.menu-accordion").accordion({header: "a.accord-handle", navigation: true});
	}
	$("#menu li").hover(function() { $(this).addClass("over") }, function() { $(this).removeClass("over"); });
}

$(document).ready(init);


//ADM
//////////////////////////////////////////////////////////////////////////////////////

