﻿var linkRedirecionamentoSC = 'http://clientessc.icecards.com.br/login.aspx';
var linkRedirecionamentoMS = 'http://clientesms.icecards.com.br/login.aspx';
//var linkWebService = 'http://clientesms.icecards.com.br/WS/ServicosWeb.asmx/';
var linkWebService = 'http://www.icecards.com.br/WS/ServicosWeb.asmx/';

//var linkWebService = 'http://www.icecards.com.br/servico.aspx/';

//var linkRedirecionamentoSC = 'http://icehomolog02/clientesSC/Login.aspx';
//var linkRedirecionamentoMS = 'http://icehomolog02/clientesMS/Login.aspx';
//var linkWebService = 'http://icehomolog02/clientesMS/WS/ServicosWeb.asmx/';

//var linkRedirecionamentoSC = 'http://icecnhteste02/clientesSC/Login.aspx';
//var linkRedirecionamentoMS = 'http://icecnhteste02/clientesMS/Login.aspx';
//var linkWebService = 'http://icecnhteste02/clientesMS/WS/ServicosWeb.asmx/';

//var linkRedirecionamentoSC = 'http://localhost:6182/site%20ice%20(neotix)/login.aspx';
//var linkRedirecionamentoMS = 'http://localhost:6182/site%20ice%20(neotix)/login.aspx';
//var linkWebService = 'http://localhost:6182/site%20ice%20(neotix)/WS/ServicosWeb.asmx/'

var url = window.location.toString();

var alertUsuario, alertSenha, alertSenhaIncorreta, alertLembrouSenha, alertUsuarioNaoEncontrado;


if (url.indexOf('en-us') > -1) {
    alertUsuario = 'Fill Username';
    alertSenha = 'Fill Password';
    alertSenhaIncorreta = 'Username not found. Request access to the person responsible by the CFC.';
    alertLembrouSenha = 'A new password was send to your e-mail. Check your e-mail in a few minutes.';
    alertUsuarioNaoEncontrado = 'Username not found. Request access to the person responsible by the CFC.';
    alertErro = 'An error ocurried during the transaction. Try again in a few minutes.';
}
else {
    alertUsuario = 'Informe o usuário';
    alertSenha = 'Informe a senha';
    alertSenhaIncorreta = 'Usuário não encontrado. Solicite acesso ao responsável por seu CFC.';
    alertLembrouSenha = 'Uma nova senha foi gerada e enviada para o(s) email(s) cadastrado(s) para o seu usuário';
    alertUsuarioNaoEncontrado = 'Usuário não encontrado. Solicite acesso ao responsável por seu CFC.';
    alertErro = 'Ocorreu um problema na requisição. Tente novamente em alguns minutos.';
}

$(document).ready(function () {
    $('#imgLogin').click(function () {

        var login = $('#txtLogin').val();
        var senha = $('#txtSenha').val();

        login = login.replace(/[']/g, '\\\'').replace(/["]/g, '\\\"');

        senha = senha.replace(/[']/g, '\\\'').replace(/["]/g, '\\\"');

        if (login == '') {
            alert(alertUsuario);
        }
        else if (senha == '') {
            alert(alertSenha);
        }
        else {
            $('#imgLogin').hide();
            $('#imgLoading').show();

            var parameters = "{'login_':'" + login + "','senha_':'" + senha + "'}";
            $.ajaxSetup({
                // Disable caching of AJAX responses
                cache: false
            });

            $.ajax({
                type: "POST",
                url: linkWebService + 'AutenticarUsuario',
                data: parameters,
                cache: false,
                async: true,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    resultado = data.d;

                    if (resultado == 70 || resultado == 76) {
                        document.forms["formPost"].action = linkRedirecionamentoSC;
                        document.forms["formPost"].submit();
                    }
                    else if (resultado == 73 || resultado == 77) 
                    {
                        document.forms["formPost"].action = linkRedirecionamentoMS;
                        document.forms["formPost"].submit();
                    }
                    else {
                        alert(alertSenhaIncorreta);
                        $('#imgLogin').show();
                        $('#imgLoading').hide();
                    }

                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(XMLHttpRequest.statusText);
                    alert(XMLHttpRequest.responseText);
                }
            });
        }
    });
});

function EsqueciSenha() {
    var login = $('#txtLogin').val();

    if (login == '') {
        alert(alertUsuario);
    }
    else {
        $('#hlEsqueciSenha').hide();
        $('#imgLoadingEsqueciSenha').show();

        var parameters = "{'login_':'" + login + "'}";
        $.ajax({
            type: "POST",
            url: linkWebService + 'EsqueciMinhaSenha',
            data: parameters,
            async: true,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                resultado = data.d;

                if (resultado == '') {
                    alert(alertLembrouSenha);

                    $('#hlEsqueciSenha').show();
                    $('#imgLoadingEsqueciSenha').hide();
                }
                else {
                    if (resultado.indexOf('Exception') >= 0) {
                        alert(alertErro);
                    }
                    else {
                        alert(resultado);
                    }

                    $('#hlEsqueciSenha').show();
                    $('#imgLoadingEsqueciSenha').hide();
                }

            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest.statusText);
                alert(XMLHttpRequest.responseText);
                resultado = false;
            }
        });
    }
}
