﻿//Exemplo de utilizacao
//action = "~/PropostaReduzida/Hierarquia.aspx"
//dadosForm = [{ name: "Ticket", value: "123"}]
function Redireciona(action, dadosForm, target) {

    var form = document.createElement("form");
    form.setAttribute("method", "post");
    form.setAttribute("action", action);
    if (target != null || target != undefined)
        form.setAttribute("target", target);

    for (var i = 0; i < dadosForm.length; i++) 
    {
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", dadosForm[i].name);
        hiddenField.setAttribute("id", dadosForm[i].name);
        hiddenField.setAttribute("value", dadosForm[i].value);
        form.appendChild(hiddenField);
    }

    document.body.appendChild(form);
    form.submit();
}

function DataAtual() {
    return Data2DDMMYYYY(new Date());
}

function Data2DDMMYYYY(cData) {
    var strData;
    var strDia;
    var strMes;
    var strAno;
    var separaHora;
    var separaDMY;

    if (cData.toString().length > 20) { //formato 'Tue Jul 7 15:53:01 UTC-0300 2009'
        strData = new Date(cData);

        strDia = strData.getDate().toString();
        strMes = (strData.getMonth() + 1).toString();
        strAno = strData.getFullYear().toString();
    } else {
        strData = cData.toString();
        separaHora = strData.split(' ');
        separaDMY = separaHora[0].split('/');

        strDia = separaDMY[0];
        strMes = separaDMY[1];
        strAno = separaDMY[2];
    }

    if (strDia.length == 1) strDia = '0' + strDia;
    if (strMes.length == 1) strMes = '0' + strMes;
    if (strAno.length == 2) {
        if (strAno < 40) {
            strAno = '20' + strAno;
        } else {
            strAno = '19' + strAno;
        }
    }
    strData = strDia + '/' + strMes + '/' + strAno;

    return strData;
}


function collapseMenu(s) {
	var td = s;
	var d = td.getElementsByTagName("div").item(0);

	td.className = "menuNormalSup"

	if (d != null)
		d.className = "menuNormalSup";
}

function expandMenu(s) {
	var td = s;
	var d = td.getElementsByTagName("div").item(0);

	td.className = "menuNormalSup";

	if (d != null)
		d.className = "menuHover";
}

function getQuerystring(key, default_)
{
	if (default_ == null) default_ = "";
	key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
	var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
	var qs = regex.exec(window.location.href);
	if (qs == null)
		return default_;
	else
		return qs[1];
}

function TecladoVirtual_BloqueioAoDigitar()
{
	alert('Use o teclado virtual ao lado para digitar sua senha');
	return false;
}

function TecladoVirtual_Limpar(campo, hidden)
{
	//$("input[id$='" + hidden + "']").attr({ value: '' });
	//$('.teclado_campoDigitacao').attr({ value: '' });

	$("input[id$='" + hidden + "']").attr({ value: '' });
	$("#" + campo + "").attr({ value: '' });
}

function TecladoVirtual_CliqueBotao(campo, hidden, hash, areaTeclado)
{
	var txt = $get(campo);
	var area = $get(areaTeclado);
	if ($(txt).attr('value').length < 6)
	{
		var values = $(txt).attr('value') + '0';
		$(txt).attr({ value: values });
		
		if($(txt).attr('value').length > 0)
		{
			var valueHidden = $("input[id$='" + hidden + "']").attr('value');
			valueHidden += '[NOVO_HASH]' + hash;
			
			$("input[id$='" + hidden + "']").attr({ value: valueHidden }); 
		}
		else
			$("input[id$='" + hidden + "']").attr({ value: hash }); 

		$(area).css({ visibility: 'hidden' });
		setTimeout('$("#' + area.id  + '").css({ visibility: \'visible\' });', 500);
	}
}

function TecladoVirtual_AplicarEventos(fieldID, hiddenField)
{
	$(".teclado_imgTeclado").attr({ onclick: '' });
	$(".teclado_imgTeclado").unbind();
	$('.teclado_imgTeclado').each(function()
	{
		var hash = $(this).attr('numberHash');
		$(this).click(function()
		{
			TecladoVirtual_CliqueBotao(fieldID, hiddenField, hash, $('.tecladoVirtualArea').attr('id'));
			return false;
		});
	});

	$(".botaoLimparTecladoVirtual").attr({ onclick: '' });
	$('.botaoLimparTecladoVirtual').unbind();
	$('.botaoLimparTecladoVirtual').click(function()
	{
		TecladoVirtual_Limpar(fieldID, hiddenField);
		return false;
	});
}

function mostraPopUpOver(nome) {
	var estado = document.getElementById(nome).style.display;

	document.getElementById(nome).style.display = "block";
}

function mostraPopUpOut(nome) {
	var estado = document.getElementById(nome).style.display;

	document.getElementById(nome).style.display = "none";
}

function ApenasNumeros(e) {

    navegador = /msie/i.test(navigator.userAgent);

    if (navegador)
        var tecla = event.keyCode;
    else var tecla = e.which;

    if (tecla > 47 && tecla < 58) // numeros de 0 a 9
        return true;
    else {
        if (tecla != 8) // backspace
            return false;
        else
            return true;
    }
}


//////////////////////////////////////////////////////
/**********Valida CPF*************/
function ValidarCPF(cpf)
{
	var i;
	var filtro = /^\d{3}.\d{3}.\d{3}-\d{2}$/i;
	if (!filtro.test(cpf))
	{
		return false;
	}

	cpf = remove(cpf, ".");
	cpf = remove(cpf, "-");

	if (cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" ||
	  cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" ||
	  cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" ||
	  cpf == "88888888888" || cpf == "99999999999")
	{
		return false;
	}

	soma = 0;
	for (i = 0; i < 9; i++)
		soma += parseInt(cpf.charAt(i)) * (10 - i);
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11)
		resto = 0;
	if (resto != parseInt(cpf.charAt(9)))
	{
		return false;
	}
	soma = 0;
	for (i = 0; i < 10; i++)
		soma += parseInt(cpf.charAt(i)) * (11 - i);
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11)
		resto = 0;
	if (resto != parseInt(cpf.charAt(10)))
	{
		return false;
	}
	return true;
}

function ValidarCPFPortal(cpf) {
    var i;
    cpf = remove(cpf, ".");
    cpf = remove(cpf, "-");

    if (cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" ||
	  cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" ||
	  cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" ||
	  cpf == "88888888888" || cpf == "99999999999") {
        return false;
    }
    soma = 0;
    for (i = 0; i < 9; i++)
        soma += parseInt(cpf.charAt(i)) * (10 - i);
    resto = 11 - (soma % 11);
    if (resto == 10 || resto == 11)
        resto = 0;
    if (resto != parseInt(cpf.charAt(9))) {
        return false;
    }
    soma = 0;
    for (i = 0; i < 10; i++)
        soma += parseInt(cpf.charAt(i)) * (11 - i);
    resto = 11 - (soma % 11);
    if (resto == 10 || resto == 11)
        resto = 0;
    if (resto != parseInt(cpf.charAt(10))) {
        return false;
    }
    return true;
}

function remove(str, sub)
{
	i = str.indexOf(sub);
	r = "";
	if (i == -1) return str;
	r += str.substring(0, i) + remove(str.substring(i + sub.length), sub);
	return r;
}

//#####################
//# VALIDAÇÃO DA DATA #
//#####################
function VerificaData(cData)
{
    var data = cData;
    var tam = data.length;
    if (tam != 10)
    {
        return false;
    }
    var dia = data.substr(0, 2)
    var mes = data.substr(3, 2)
    var ano = data.substr(6, 4)

    if (ano < 1900)
    {
        return false;
    }
    if (ano > 2999)
    {
        return false;
    }

    switch (mes)
    {
        case '01':
            if (dia <= 31)
                return (true);
            break;
        case '02':
            if (dia <= 28)
            {
                return true;
            }
            if ((dia == 29) && (ano % 4 == 0))
            {
                return true;
            }
            break;
        case '03':
            if (dia <= 31)
                return (true);
            break;
        case '04':
            if (dia <= 30)
                return (true);
            break;
        case '05':
            if (dia <= 31)
                return (true);
            break;
        case '06':
            if (dia <= 30)
                return (true);
            break;
        case '07':
            if (dia <= 31)
                return (true);
            break;
        case '08':
            if (dia <= 31)
                return (true);
            break;
        case '09':
            if (dia <= 30)
                return (true);
            break;
        case '10':
            if (dia <= 31)
                return (true);
            break;
        case '11':
            if (dia <= 30)
                return (true);
            break;
        case '12':
            if (dia <= 31)
                return (true);
            break;
    }
    {
        return false;
    }
    return true;
}
