﻿

function addEvent() {
    // dropDown de Departamento
    $('.ordenacao select').change(function() {
        $(this).next().text($(this).children("option:selected").text());
    });
    $('.lblSelect').each(function() {
        $(this).text($(this).prev().children("option:selected").text());
    });
}

function alertDialog(vc_mensagem, vc_title, vc_funcoes, it_width, vc_dialog) {

    $(document).ready(function() {

        if (!vc_dialog) {
            vc_dialog = 'dialog';
        }
        if (vc_dialog == '') {
            vc_dialog = 'dialog';
        }
        $('#' + vc_dialog).dialog("destroy");


        var obj = getDivDialog(vc_dialog);
        if (vc_mensagem != '') {
            vc_mensagem = vc_mensagem.replace(/\n/gi, '<br />');
            obj.innerHTML = vc_mensagem;
        }

        // valores padrao caso nao receba esses valores
        if (typeof (vc_title) == "undefined" || vc_title == null) {
            vc_title = "ATENÇÃO!";
        }

        if (typeof (it_width) == "undefined" || it_width == null) {
            it_width = 450;
        }

        if (typeof (vc_funcoes) == "undefined" || vc_funcoes == null) {
            vc_funcoes = "void(0);";
        }


        // Dialog Propriedades
        $('#' + vc_dialog).dialog({
            autoOpen: false,
            modal: true,
            overlay: { backgroundColor: '#000', opacity: 0.95 },
            title: '<span class=\'ui-icon ui-icon-alert\'>&nbsp;</span>&nbsp;' + vc_title,
            width: it_width,
            position: "center",
            hide: "slide",
            buttons: { "fechar": function() { $(this).dialog("close"); } },
            close: function() { eval(vc_funcoes); }

        });

        //hover states on the static widgets
        $('#' + vc_dialog + '_link, ul#icons li').hover(
		function() { $(this).addClass('ui-state-hover'); },
		function() { $(this).removeClass('ui-state-hover'); }
	);

        $('#' + vc_dialog).dialog("open");
    });
}


function fecharDialog() {
    $("#dialog").dialog("close");
}

function alertDialogSemButton(vc_mensagem, vc_title, vc_funcoes, it_width, vc_classe) {

    $(document).ready(function() {
        $('#dialog').dialog("destroy");

        var obj = getDivDialog('dialog');
        if (vc_mensagem != '') {
            //vc_mensagem = vc_mensagem.replace(/\n/gi, '<br />');
            obj.innerHTML = vc_mensagem;
        }

        // valores padrao caso nao receba esses valores
        if (typeof (vc_title) == "undefined" || vc_title == null) {
            vc_title = "ATENÇÃO!";
        }

        if (typeof (it_width) == "undefined" || it_width == null) {
            it_width = 450;
        }

        if (typeof (vc_funcoes) == "undefined" || vc_funcoes == null) {
            vc_funcoes = "void(0);";
        }



        // Dialog Propriedades
        $('#dialog').dialog({
            autoOpen: false,
            modal: true,
            overlay: { backgroundColor: '#000', opacity: 0.95 },
            title: '<span class=\'ui-icon ui-icon-alert\'>&nbsp;</span>&nbsp;' + vc_title,
            width: it_width,
            position: "center",
            hide: "slide",
            close: function() { eval(vc_funcoes); },
            dialogClass: vc_classe

        });

        //hover states on the static widgets
        $('#dialog_link, ul#icons li').hover(
		function() { $(this).addClass('ui-state-hover'); },
		function() { $(this).removeClass('ui-state-hover'); }
	);

        $("#dialog").dialog("open");
    });
}

function getDivDialog(idDialog) {

    var tmpDiv = (document.getElementById(idDialog)) ? document.getElementById(idDialog) : document.createElement('div');
    if (document.getElementById(idDialog)) {
        return document.getElementById(idDialog)
    }
    else {
        var tmpDiv = document.createElement('div');

        with (tmpDiv) {
            className = 'dialog';
            setAttribute('id', idDialog);
            setAttribute('title', 'Atenção!');
            style.display = 'none';
        }

        $('body').append(tmpDiv);
        return tmpDiv;
    }
}

function carregaDialogPaginaSemButton(vc_url, vc_title, it_width, it_height, vc_classe) {
    alertDialogSemButton("<iframe src=\"" + vc_url + "\" width=\"100%\" height=\"" + it_height + "\" style=\"border:none;\" border=\"0\" frameborder=\"no\" allowtransparency=\"true\"></iframe>", vc_title, '', it_width, vc_classe);
}

/*# aceita só números digitados num campo */
function soNumero(evtKeyPress) {
	var nTecla;
	nTecla = (evtKeyPress.which) ? evtKeyPress.which : evtKeyPress.keyCode;
	if ((nTecla > 47 && nTecla < 58) || nTecla == 8 || nTecla == 9 || nTecla == 37 || nTecla == 38 || nTecla == 39 || nTecla == 40 || nTecla == 46)
		return true;
	else
		return false;
}

/*# MASCARA */
function mascarar(vc_campo, vc_mascara) {
	var i = vc_campo.value.length;
	var x = vc_mascara.substring(0, 1);
	var y = vc_mascara.substring(i);

	if (y.substring(0, 1) != x) {
		vc_campo.value += y.substring(0, 1);
	}
}

//Corrige o bug com o defaultbutton com o firefox
function clickButton(e, buttonid) {
    
    var bt = document.getElementById(buttonid);
    if (typeof (bt) == 'object') {
        if (navigator.appName.indexOf("Netscape") > -1) {
            if (e.keyCode == 13) {
                if (bt && typeof (bt.click) == 'undefined') {
                    bt.click = addClickFunction1(bt);
                }
            }
        }
        if (navigator.appName.indexOf("Microsoft Internet Explorer") > -1) {
            if (event.keyCode == 13) {
                bt.click();
                return false;
            }
        }
    }
}

//Corrige o bug com o defaultbutton com o firefox
function addClickFunction1(bt) {
  
    var result = true;
    if (bt.onclick) result = bt.onclick();
    if (typeof (result) == 'undefined' || result) {
        eval(bt.href);
    }
}

/*confirm personalizado*/
function confirmDialog(vc_mensagem, vc_title, vc_funcao_ok, vc_funcao_cancelar, it_width) {

    $(document).ready(function() {

        var obj = getDivDialog('dialog');
        vc_mensagem = vc_mensagem.replace(/\n/gi, '<br />');
        obj.innerHTML = vc_mensagem;

        if (typeof (vc_title) == "undefined" || vc_title == null) {
            vc_title = "ATENÇÃO!";
        }

        if (typeof (it_width) == "undefined" || it_width == null) {
            it_width = 450;
        }

        if (typeof (vc_funcao_ok) == "undefined" || vc_funcao_ok == null) {
            vc_funcao_ok = "$(this).dialog(\"close\");";
        }

        if (typeof (vc_funcao_cancelar) == "undefined" || vc_funcao_cancelar == null) {
            vc_funcao_cancelar = "$(this).dialog(\"close\");";
        }

        //$('#dialog').dialog("destroy");

        // Dialog Propriedades
        $('#dialog').dialog({
            autoOpen: false,
            modal: true,
            overlay: { backgroundColor: '#000', opacity: 0.95 },
            title: '<span class=\'ui-icon ui-icon-alert\'>&nbsp;</span>&nbsp;' + vc_title,
            width: it_width,
            position: "center",
            hide: null,
            buttons: {
                "Não": function() { eval(vc_funcao_cancelar) }
			    , "Sim": function() { eval(vc_funcao_ok + ';$(this).dialog(\'close\');') }
            }
            //close: function(){eval(vc_funcoes);}

        });

        //hover states on the static widgets
        $('#dialog_link, ul#icons li').hover(
		    function() { $(this).addClass('ui-state-hover'); },
		    function() { $(this).removeClass('ui-state-hover'); }
	    );

        $("#dialog").dialog("open");
    });
} 




