﻿function UserSecurity() {
    var async = null;
}

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 != "" || json.Result.Data != undefined) && (json.Result.Data.IdUsuario == undefined || json.Result.Data.IdUsuario == "")) {
	                        window.location.href = '/Ofertas/Cliente/Cadastro.aspx?idCliente=' + json.Result.Data + '&step=1&previous=' + encodeURIComponent(previousPage);
	                    }
	                    else {
	                        if (previousPage == undefined)
	                            window.location.href = '/Default.aspx';
	                        else
	                            window.location.href = previousPage;
	                    }
                        
	                    /*if (previousPage == undefined)
	                        window.location.href = 'Default.aspx';
	                    else
	                        window.location.href = previousPage;
                        */
                        
	                    /*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", "error");
	                    break;
	                case "InvalidData":
	                    Alert(json.Result.Message, "Erro", "error");
	                    break;
	                case "Error":
	                    Alert(json.Result.Message, "Erro", "error");
	                    break;
	            }

	            $('#loading-ajax').hide();
	            //hideLoadingMessage();
	        },

	        failure: function(o) {
	            Alert(o.responseText, "Erro no Request");

	            $('#loading-ajax').hide();
	        },

	        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.Logout = function() {

    $.ajax({
        url: wrapperUrl + "act_user_logout",
        cache: false,
        error:
                function(xhr, ajaxOptions, thrownError) {
                    Alert(xhr.status); alert(thrownError);
                },
        success:
                function() {
                    window.location.href = '/Index.aspx';
                }
    });

}
