// JavaScript Document

/* lista - sistema */
var openList = false;
var escolha ;

function topo_form_sistema_openselect(acao)
{
	var Lista = window.document.getElementById("topo_form_sistema_selectboxlista");

	var status = $("#topo_form_sistema_selectboxlista").css("display");

	if (status != 'none') {
        Lista.style.display = "none";
        openList = false;
    } else {
        Lista.style.display = "";
		openList = true;
	}
}

function topo_form_sistema_setvalor(valor, legenda)
{
	var Texto = window.document.getElementById("topo_form_sistema_selectboxtexto");
	var Input = window.document.getElementById("HiddenBox");
	
	
	Input.value = valor;	
	Texto.innerHTML = legenda;
	escolha = valor;
	topo_form_sistema_openselect();
	document.getElementById('txtUsuario').value = '';
	document.getElementById('txtUsuario').focus();
}

/* layer - esqueci minha senha */

var antLayer = false;

function FechaLayer(layer)
{
	window.document.getElementById(layer).style.display="none";
	antLayer = false;
}

function AbreLayer(layer)
{
	if(antLayer != false)
	{
		window.document.getElementById(antLayer).style.display="none";
	}
	window.document.getElementById(layer).style.display="block";
	antLayer = layer;
}

/* lista - acesso rapido */

function topo_form_acesso_rapido_openselect(acao)
{
	var Lista = window.document.getElementById("topo_form_acesso_rapido_selectboxlista");
	
	if(acao == 'none'){
		Lista.style.display = "none";
		openList = false;
	}else{
		Lista.style.display = "";
		openList = true;
	}
}

function topo_form_acesso_rapido_setvalor(valor, legenda)
{
	var Texto = window.document.getElementById("topo_form_acesso_rapido_selectboxtexto");
	var Input = window.document.getElementById("HiddenBox");
	
	Input.value = valor;	
	Texto.innerHTML = legenda;
	topo_form_acesso_rapido_openselect();
	escolha = valor;
	topo_form_acesso_rapido_openselect('none');
}

window.load = function(){
alert('oi');
}

//Specify affected tags. Add or remove from list:
var tgs = new Array( 'span' );

//Specify spectrum of different font sizes:
var szs = new Array( '5px', '6px', '7px', '8px', '9px', '10px', '11px', '12px', '13px', '14px', '16px', '18px', '20px', '22px', '22px' );
var startSz = 6;
//var szs;

function ts( trgt,inc )
{
	/*
	if(szs == "")
	{
		szs = 10;
	}
	else
	{
		szs = parseInt(szs+inc);
	}*/
	
	
	if (!document.getElementById) return
	
	var d = document,cEl = null,sz = startSz,i,j,cTags;
	
	sz += inc;
	
	if ( sz < 0 ) sz = 0;
	if ( sz > 14 ) sz = 14;
	
	startSz = sz;
	
	if ( !( cEl = d.getElementById( trgt ) ) ) cEl = d.getElementsByTagName( trgt )[ 0 ];
	//alert(document.body.style.size);
	cEl.style.fontSize = szs[ sz ];
	//cTags.style.fontSize = szs[ sz ];
	
	for ( i = 0; i < tgs.length; i++ )
	{
		cTags = cEl.getElementsByTagName( tgs [ i ]);
		for ( j = 0; j < cTags.length; j++ )
			//alert(cTags[ j ].style.fontSizeAdjust);
			cTags[ j ].style.fontSize = szs[ sz ];
	}
}

function edjan() {

var e = document.getElementsByTagName('html')[ 0 ];
alert(e.style.fontSize);
e.style.fontSize += 10;

}

function mostraOpcoes( _o_, _s_, _p_ )
{
    var obj = document.getElementById(_o_);
    if( _s_.type == 'radio') {
        if( _p_){
            obj.style.display = (_s_.checked)? 'block': 'none';
        }else{
            obj.style.display = (_s_.checked)? 'none':'block';
        }
    }else{
        obj.style.display = (_s_.checked)? 'block': 'none';
    }
}




//******************************************//
//*** Validação de Documentos CPF e CNPJ ***//
//******************************************//
function ValidarDoc2( src, args ) {

    //alert(args.Value+" - "+args.elements);

    //alert(LimpaCpfCnpj(args.Value) + ' ' + args.Value.length);
    var qtda = LimpaCpfCnpj(args.Value);
    qtda = qtda.length;

	if( qtda <= 11){
		//args.IsValid = TwVerificarCpf( src, args );
		args.IsValid = TwVerificarCpf( args.Value );
	}else {
		//args.IsValid = validaCNPJ( src, args );	
		args.IsValid = validaCNPJ( args.Value );	
	} 

}

function ValidarDoc3(valor) {
    //alert('oi');
    //valor = document.getElementById(valor.id);
    //alert(valor.value+" - "+valor.elements);
    //alert(LimpaCpfCnpj(args.Value) + ' ' + args.Value.length);
    var qtda = LimpaCpfCnpj(valor.value);
    qtda = qtda.length;
    
	if( qtda <= 11){
	//alert(TwVerificarCpf(valor.value));
		//args.IsValid = TwVerificarCpf( src, args );
		//valor.IsValid = TwVerificarCpf( valor.value );
		if(!TwVerificarCpf( valor.value )) {
		    alert('CPF incorreto, por favor verifique.');
		    valor.value = "";
		    document.getElementById(valor.id).focus();
		    document.getElementById(valor.id).focus;
		} 
	}

}

function LimpaCpfCnpj(num){
    valor = num.replace('.','');
    valor = valor.replace('.','');
    valor = valor.replace('.','');
    valor = valor.replace(',','');
    valor = valor.replace('-','');
    valor = valor.replace('/','');
    valor = valor.replace('\\','');
    return valor
}

function TwVerificarCpf( sCPF ){
	sCPF = LimpaCpfCnpj(sCPF);
//function TwVerificarCpf( src, args ){
	//sCPF = LimpaCpfCnpj(args.Value);
	//alert ( sCPF);
   if (sCPF == "") return false;
    var i;
    var intDigito;
    var varQuociente;
    var iCPF = new Array();
    var iDig = new Array();
    sCPF=formatear_cpf(sCPF);
    if (sCPF == "") return false;
    for (i = 1;i<10;i++) {
        iCPF[i] = sCPF.substr(i-1, 1);
        iCPF[i] = parseInt(iCPF[i]);
	}

	for(nI=1; nI <= iCPF.length; nI++){
		if(nI == 1){
			var nAnterior = iCPF[nI];
		}else{
			var nAnterior = iCPF[nI-1];
		}

		if(isNaN(iCPF[nI])==true){
			break;
		}else{
			if(nAnterior != iCPF[nI]){
				var bIguais = false;
				break;
			}else{
				var bIguais = true;
			}
		}
	}
	if(bIguais) return false;
	for (i = 1;i<10;i++) {
        iCPF[i] = sCPF.substr(i-1, 1);
        iCPF[i] = parseInt(iCPF[i]);
        }
    iDig[1] = sCPF.substr(9, 1);
    iDig[1] = parseInt(iDig[1]);
    iDig[2] = sCPF.substr(10, 1);
    iDig[2] = parseInt(iDig[2]);
    intDigito = (iCPF[9] * 2) + (iCPF[8] * 3) + (iCPF[7] * 4);
    intDigito = intDigito + (iCPF[6] * 5) + (iCPF[5] * 6) + (iCPF[4] * 7);  
    intDigito = intDigito + (iCPF[3] * 8) + (iCPF[2] * 9) + (iCPF[1] * 10);
    intDigito = (intDigito * 10) % 11;
    if (intDigito == 10) intDigito = 0;
    if (intDigito !== iDig[1])return false;
    intDigito = (iDig[1] * 2) + (iCPF[9] * 3) + (iCPF[8] * 4) + (iCPF[7] * 5);
    intDigito = intDigito + (iCPF[6] * 6) + (iCPF[5] * 7) + (iCPF[4] * 8);
    intDigito = intDigito + (iCPF[3] * 9) + (iCPF[2] * 10) + (iCPF[1] * 11);
    intDigito = (intDigito * 10) % 11;
    if (intDigito == 10) intDigito = 0;
    if (intDigito !== iDig[2]) return false;
	return true;
	
}

function formatear_cpf(sCGC){
    var POS;
    if (sCGC == "") return;
    do {POS = sCGC.indexOf(".",0);
        if (POS == -1)break;
        sCGC = sCGC.substr(0, POS) + sCGC.substr(POS + 1);}
    while (POS !== -1);
    do {POS = sCGC.indexOf("/",0);
        if (POS == -1)break;
        sCGC = sCGC.substr(0, POS) + sCGC.substr(POS + 1);}
    while (POS !== -1);
    do {POS = sCGC.indexOf("-",0);
        if (POS == -1)break;
        sCGC = sCGC.substr(0, POS) + sCGC.substr(POS + 1);}
    while (POS !== -1);
    return sCGC;
}



function validaCNPJ(cnpj)
      {
      var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais;
      cnpj = LimpaCpfCnpj(cnpj);
      
      digitos_iguais = 1;
      if (cnpj.length < 14 && cnpj.length < 15)
            return false;
      for (i = 0; i < cnpj.length - 1; i++)
            if (cnpj.charAt(i) != cnpj.charAt(i + 1))
                  {
                  digitos_iguais = 0;
                  break;
                  }
      if (!digitos_iguais)
            {
            tamanho = cnpj.length - 2
            numeros = cnpj.substring(0,tamanho);
            digitos = cnpj.substring(tamanho);
            soma = 0;
            pos = tamanho - 7;
            for (i = tamanho; i >= 1; i--)
                  {
                  soma += numeros.charAt(tamanho - i) * pos--;
                  if (pos < 2)
                        pos = 9;
                  }
            resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
            if (resultado != digitos.charAt(0))
                  return false;
            tamanho = tamanho + 1;
            numeros = cnpj.substring(0,tamanho);
            soma = 0;
            pos = tamanho - 7;
            for (i = tamanho; i >= 1; i--)
                  {
                  soma += numeros.charAt(tamanho - i) * pos--;
                  if (pos < 2)
                        pos = 9;
                  }
            resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
            if (resultado != digitos.charAt(1))
                  return false;
            return true;
            }
      else
            return false;
      } 
//******************************************//
//*** Validação de Documentos CPF e CNPJ ***//
//******************************************//

function removimento(elemento, direcao, campo){
    var sel_pai = document.getElementById(arguments[2]);
	var selecionados = new Array();
	if (!sel_pai) {
		return;
	}
    len = sel_pai.options.length;
	for (i = 0; i < len; i++) {
		if (sel_pai.options[i].selected) {
			document.getElementById('ctl00_conteudo_list_destino').value = document.getElementById('ctl00_conteudo_list_destino').value.replace(sel_pai.options[i].value+",","");
		}
	}
	movimento2(elemento, direcao, campo);
}

function movimento(elemento, direcao, campo){
    var sel_pai = document.getElementById(arguments[2]);
    //var sel_pai = document.getElementById('ctl00_conteudo_list_destino');
    
	var selecionados = new Array();
	if (!sel_pai) {
	    alert('oi');
		return;
	}
    len = sel_pai.options.length;
	for (i = 0; i < len; i++) {
		if (sel_pai.options[i].selected) {
			document.getElementById('ctl00_conteudo_list_destino').value += sel_pai.options[i].value+", ";
		}
	}
	movimento2(elemento, direcao, campo);
}




function movimento2(elemento, direcao) {

	var total = document.getElementById(arguments[2]).length;
	if(total>=2 && elemento=='menu' && direcao!='cima' && direcao!='baixo'){
		alert('Já foram selecionado os dois itens.');
		return;
	}
	
	var sel = document.getElementById(elemento);
	var len, i;
	if (!sel) {
		return;
	}
	if (direcao == 'passar' && arguments[2] == undefined) {
		return;
	} else if (direcao == 'passar') {
		var sel_pai = document.getElementById(arguments[2]);
		var selecionados = new Array();
		if (!sel_pai) {
			return;
		}
		len = sel_pai.options.length;
		for (i = 0; i < len; i++) {
			if (sel_pai.options[i].selected) {
				sel.options[sel.options.length] = new Option(sel_pai.options[i].text, sel_pai.options[i].value);
				selecionados.push(i);
			}
		}
		len = selecionados.length;
		for (i = len-1; i >= 0; i--) {
			sel_pai.options[selecionados[i]] = null;
		}
	} else if (direcao == 'cima' || direcao == 'baixo') {
		var selecionado = sel.selectedIndex;
		var comparacao = direcao == 'cima' ? selecionado - 1 : selecionado;
		var opts_values = new Array();
		var opts_texts = new Array();
		var tam = sel.options.length;
		var i;
		if (selecionado == -1) {
			return;
		}
		if (direcao == 'cima' && selecionado == 0) {
			return;
		}
		if (direcao == 'baixo' && selecionado == tam - 1) {
			return;
		}
		selecionado = direcao == 'cima' ? selecionado - 1 : selecionado + 1;
		for (i = 0; i < sel.options.length; i++) {
			if (i == comparacao) {
				opts_values.push(sel.options[i+1].value);
				opts_texts.push(sel.options[i+1].text);
				sel.options[i + 1] = null;
			}
			opts_values.push(sel.options[i].value);
			opts_texts.push(sel.options[i].text);
		}
		for (i = 0; i < tam; i++) {
			sel.options[i] = new Option(opts_texts[i], opts_values[i]);
		}
		sel.selectedIndex = selecionado;
	}
}

function suporte_removimento(elemento, direcao, campo){
    var sel_pai = document.getElementById(arguments[2]);
	var selecionados = new Array();
	if (!sel_pai) {
		return;
	}
    len = sel_pai.options.length;
	for (i = 0; i < len; i++) {
		if (sel_pai.options[i].selected) {
			document.getElementById('ctl00_cSuporte_list_destino').value = document.getElementById('ctl00_cSuporte_list_destino').value.replace(sel_pai.options[i].value+",","");
		}
	}
	suporte_movimento2(elemento, direcao, campo);
}

function suporte_movimento(elemento, direcao, campo){
    var sel_pai = document.getElementById(arguments[2]);
    //var sel_pai = document.getElementById('ctl00_conteudo_list_destino');
    
	var selecionados = new Array();
	if (!sel_pai) {
	    alert('oi');
		return;
	}
    len = sel_pai.options.length;
	for (i = 0; i < len; i++) {
		if (sel_pai.options[i].selected) {
			document.getElementById('ctl00_cSuporte_list_destino').value += sel_pai.options[i].value+", ";
		}
	}
	suporte_movimento2(elemento, direcao, campo);
}

function suporte_movimento2(elemento, direcao) {

	var total = document.getElementById(arguments[2]).length;
	if(total>=2 && elemento=='menu' && direcao!='cima' && direcao!='baixo'){
		alert('Já foram selecionado os dois itens.');
		return;
	}
	
	var sel = document.getElementById(elemento);
	var len, i;
	if (!sel) {
		return;
	}
	if (direcao == 'passar' && arguments[2] == undefined) {
		return;
	} else if (direcao == 'passar') {
		var sel_pai = document.getElementById(arguments[2]);
		var selecionados = new Array();
		if (!sel_pai) {
			return;
		}
		len = sel_pai.options.length;
		for (i = 0; i < len; i++) {
			if (sel_pai.options[i].selected) {
				sel.options[sel.options.length] = new Option(sel_pai.options[i].text, sel_pai.options[i].value);
				selecionados.push(i);
			}
		}
		len = selecionados.length;
		for (i = len-1; i >= 0; i--) {
			sel_pai.options[selecionados[i]] = null;
		}
	} else if (direcao == 'cima' || direcao == 'baixo') {
		var selecionado = sel.selectedIndex;
		var comparacao = direcao == 'cima' ? selecionado - 1 : selecionado;
		var opts_values = new Array();
		var opts_texts = new Array();
		var tam = sel.options.length;
		var i;
		if (selecionado == -1) {
			return;
		}
		if (direcao == 'cima' && selecionado == 0) {
			return;
		}
		if (direcao == 'baixo' && selecionado == tam - 1) {
			return;
		}
		selecionado = direcao == 'cima' ? selecionado - 1 : selecionado + 1;
		for (i = 0; i < sel.options.length; i++) {
			if (i == comparacao) {
				opts_values.push(sel.options[i+1].value);
				opts_texts.push(sel.options[i+1].text);
				sel.options[i + 1] = null;
			}
			opts_values.push(sel.options[i].value);
			opts_texts.push(sel.options[i].text);
		}
		for (i = 0; i < tam; i++) {
			sel.options[i] = new Option(opts_texts[i], opts_values[i]);
		}
		sel.selectedIndex = selecionado;
	}
}
