﻿function UserSecurity() {
    var async = null;
    var isEditing = false;
}

UserSecurity.prototype.Authenticate = function(user, pass, previousPage) {

    var callback =
	    {
	        success: function(o) {
	            var doc = o.responseText;

	            eval("var json = " + doc);

	            switch (json.Result.Status) {

	                case "OK":

	                    /*if (json.Result.Data.Sexo == 'M') {
	                    Alert("Seja bem-vindo " + json.Result.Data.Nome);
	                    }
	                    else {
	                    Alert("Seja bem-vinda " + json.Result.Data.Nome);
	                    }*/

	                    if (previousPage != undefined && previousPage != "") {

	                        if (previousPage == "Checkout.aspx") {

	                            var fnPagto = function() {
	                                window.location.href = previousPage;
	                            }

	                            var fnCancelar = function() {
	                                window.location.href = 'Default.aspx';
	                            }

	                            var msgDialog = "Você será redirecionado para o ambiente seguro de pagamento do UOL PagSeguro. <br/> ";
	                            msgDialog += "Ao entrar na tela de Pagamento verique o cadeado de segurança no barra do seu navegador. <br/>";
	                            msgDialog += "<img src='images/certificado-seguranca.jpg' border='0' align='absmiddle' />";

	                            var dlg = new CustomDialog();

	                            dlg.Show("Bem-vindo(a) " + json.Result.Data.Nome, msgDialog, fnPagto, "Continuar", fnCancelar, "Cancelar");
	                        }
	                        else {
	                            window.location.href = previousPage;
	                        }
	                    }
	                    else {
	                        window.location.href = 'Default.aspx';
	                    }

	                    break;
	                case "LoginRequired":
	                    Alert(json.Result.Message, "Erro");
	                    break;
	                case "InvalidData":
	                    Alert(json.Result.Message, "Erro");
	                    break;
	                case "Error":
	                    Alert(json.Result.Message, "Erro");
	                    break;
	            }

	            //hideLoadingMessage();
	        },

	        failure: function(o) {
	            Alert(o.responseText, "Erro no Request");

	            //hideLoadingMessage();
	        },

	        argument: this
	    }

    var url = wrapperUrl + "act_user_authenticate";

    var params = "email=" + encodeURIComponent(user);
    params += "&senha=" + encodeURIComponent(pass);

    YAHOO.util.Connect.asyncRequest('POST', url, callback, params);

    //showLoadingMessage();

}

UserSecurity.prototype.SaveProfile = function() {

    var idProfile = _gel('cmbListaPerfil').options[_gel('cmbListaPerfil').selectedIndex].value;

    var fnSave = function() {

        var callback =
	    {
	        success: function(o) {
	            var doc = o.responseText;

	            eval("var json = " + doc);

	            switch (json.Result.Status) {

	                case "OK":
	                    load('CadastroPerfiLAcesso.aspx?idProfile=' + json.Result.Data, 'Perfil de Acesso');

	                    if (idProfile == 0) {
	                        Alert(MSG01);
	                    }
	                    else {
	                        Alert(MSG02);
	                    }

	                    _gel('cmbListaPerfil').selectedIndex = 0;
	                    
	                    break;

	                case "InvalidData":
	                    Alert(json.Result.Message, "Erro");
	                    break;

	                case "Logoff":
	                    Alert(json.Result.Message, "Erro");
	                    break;

	                case "Error":
	                    Alert(json.Result.Message, "Erro");
	                    break;

	            }

	            hideLoadingMessage();
	        },

	        failure: function(o) {
	            Alert(o.responseText, "Erro no Request");

	            hideLoadingMessage();
	        },

	        argument: this
	    }

        var url = wrapperUrl + "act_userSecurity_profile_save";

        var params = buildPostData('renderArea_perfilAcesso_form');
        params += "&idProfile=" + idProfile;

        YAHOO.util.Connect.asyncRequest('POST', url, callback, params);

        showLoadingMessage();

    }

    var dlg = new Dialog();

    if (idProfile > 0)
        dlg.Show(MSG08, fnSave);
    else
        dlg.Show(MSG07, fnSave);
}

UserSecurity.prototype.SavePrivileges = function() {

    var idProfile = _gel('cmbListaPerfil').options[_gel('cmbListaPerfil').selectedIndex].value;

    var fnSave = function() {

        var callback =
	    {
	        success: function(o) {

	            userSecurity.isEditing = false;

	            var doc = o.responseText;

	            eval("var json = " + doc);

	            switch (json.Result.Status) {

	                case "OK":
	                    load('CadastroPerfiLAcesso.aspx?idProfile=' + idProfile, 'Perfil de Acesso');

	                    Alert(MSG02);
	                    break;

	                case "InvalidData":
	                    Alert(json.Result.Message, "Erro");
	                    break;

	                case "Logoff":
	                    Alert(json.Result.Message, "Erro");
	                    break;

	                case "Error":
	                    Alert(json.Result.Message, "Erro");
	                    break;

	            }

	            hideLoadingMessage();
	        },

	        failure: function(o) {
	            Alert(o.responseText, "Erro no Request");

	            hideLoadingMessage();
	        },

	        argument: this
	    }

        var url = wrapperUrl + "act_userSecurity_privilege_save";

        userSecurity.Set_Privilege();

        var params = buildPostData('renderArea_perfilAcesso_form');
        params += "&idProfile=" + idProfile;

        YAHOO.util.Connect.asyncRequest('POST', url, callback, params);

        showLoadingMessage();

    }

    var dlg = new Dialog();

    dlg.Show(MSG08, fnSave);
}

UserSecurity.prototype.CancelProfileEdit = function() {

    var fnCancel = function() {

        load('CadastroPerfilAcesso.aspx', 'Perfil de Acesso');
 
    }

    var dlg = new Dialog();

    dlg.Show(MSG05, fnCancel);
}

UserSecurity.prototype.DeleteProfile = function() {

    var idProfile = _gel('cmbListaPerfil').options[_gel('cmbListaPerfil').selectedIndex].value;
    
    var fnDelete = function() {

        var callback =
	    {
	        success: function(o) {
	            var doc = o.responseText;

	            eval("var json = " + doc);

	            switch (json.Result.Status) {
	                case "OK":
	                    load('CadastroPerfiLAcesso.aspx', 'Perfil de Acesso');
	                    
	                    Alert(MSG10);

	                    showElement('divSelecaoPerfil');
	                    hideElement('divNovoPerfil');
	                    hideElement('divPermissaoPerfil');
	                    
	                    break;
	                case "InvalidData":
	                    Alert(json.Result.Message, "Erro");
	                    break;
	                case "Logoff":
	                    Alert(json.Result.Message, "Erro");
	                    break;
	                case "Error":
	                    Alert(json.Result.Message, "Erro");
	                    break;
	            }

	            hideLoadingMessage();
	        },

	        failure: function(o) {
	            Alert(o.responseText, "Erro no Request");

	            hideLoadingMessage();
	        },

	        argument: this
	    }

	    var url = wrapperUrl + "act_userSecurity_profile_delete";
     
        var params = "idProfile=" + idProfile;
        params += "&cmbListaPerfil=" + _gel('cmbListaPerfil').options[_gel('cmbListaPerfil').selectedIndex].text;

        YAHOO.util.Connect.asyncRequest('POST', url, callback, params);

        showLoadingMessage();

    }

    var dlg = new Dialog();

    dlg.Show(MSG09, fnDelete);
}

UserSecurity.prototype.Select_Move = function(p_orign, p_destiny) {

    userSecurity.isEditing = true;

    var selProcess = _gel(p_orign);
    var selPrivileges = _gel(p_destiny);
    var selOption;
    var selIndex = new Array(selProcess.length);

    for (var i = 0; i < selProcess.length; i++) {
        if (selProcess[i].selected) {
            selOption = document.createElement('option');

            selOption.value = selProcess.options[i].value;
            selOption.text = selProcess.options[i].text;

            try {
                selPrivileges.add(selOption, null); // standards compliant
            }
            catch (ex) {
                selPrivileges.add(selOption); // IE only
            }

            selIndex[i] = 1;
        }
    }

    for (var i = selProcess.length; i >= 0; i--) {
        if (selIndex[i] == 1) {
            selProcess.remove(i);
        }
    }

    userSecurity.Select_Order(selProcess);
    userSecurity.Select_Order(selPrivileges);

}

UserSecurity.prototype.Select_Move_All = function(p_orign, p_destiny) {

    userSecurity.isEditing = true;

    var selProcess = _gel(p_orign);
    var selPrivileges = _gel(p_destiny);
    var selOption;
    var selIndex = new Array(selProcess.length);

    for (var i = 0; i < selProcess.length; i++) {
        selOption = document.createElement('option');

        selOption.value = selProcess.options[i].value;
        selOption.text = selProcess.options[i].text;

        try {
            selPrivileges.add(selOption, null); // standards compliant
        }
        catch (ex) {
            selPrivileges.add(selOption); // IE only
        }

        selIndex[i] = 1;
    }

    for (var i = selProcess.length; i >= 0; i--) {
        if (selIndex[i] == 1) {
            selProcess.remove(i);
        }
    }

    userSecurity.Select_Order(selProcess);
    userSecurity.Select_Order(selPrivileges);

}

UserSecurity.prototype.Select_Order = function(p_select) {

    var strArrValue = new Array(p_select.length);
    var strValue = new Array(2);

    for (var i = 0; i < p_select.length; i++) {
        strArrValue[i] = p_select.options[i].text + "_" + p_select.options[i].value;
    }

    strArrValue.sort();

    for (var i = 0; i < p_select.length; i++) {
        strValue = strArrValue[i].split('_');

        p_select.options[i].value = strValue[1];
        p_select.options[i].text = strValue[0];
    }
}

UserSecurity.prototype.Set_Privilege = function(p_privilege) {

    var hdfPrivileges = _gel("hdfListaPrivilegio");
    var selPrivileges = _gel("cmbListaPrivilegio");

    for (var i = 0; i < selPrivileges.length; i++) {
        if (i == 0) {
            hdfPrivileges.value = selPrivileges.options[i].value;
        } else {
            hdfPrivileges.value += "," + selPrivileges.options[i].value;
        }
    }

}  

UserSecurity.prototype.cmbListaPerfil_OnChange = function() {

    load('CadastroPerfiLAcesso.aspx?idProfile=' + _gel('cmbListaPerfil').options[_gel('cmbListaPerfil').selectedIndex].value, 'Perfil de Acesso');

}

UserSecurity.prototype.IncludeProfile = function() {

    _gel('cmbListaPerfil').selectedIndex = 0;
    
    hideElement('divSelecaoPerfil');
    showElement('divNovoPerfil');
    hideElement('divPermissaoPerfil');

}

UserSecurity.prototype.EditProfile = function() {


    var fnConfirm = function() {

        if (_gel('cmbListaPerfil').selectedIndex > 0) {

            hideElement('divSelecaoPerfil');
            showElement('divNovoPerfil');
            hideElement('divPermissaoPerfil');

            _gel('txtDescPerfilAcesso').value = _gel('cmbListaPerfil').options[_gel('cmbListaPerfil').selectedIndex].text;

            userSecurity.isEditing = false;

        }

    }

    if (userSecurity.isEditing) {
        var dlg = new Dialog();

        dlg.Show("Você ainda não salvou as alterações feitas. Deseja prosseguir e descartá-las?", fnConfirm);
    }
    else {
        fnConfirm();
    }

}



