﻿function Cliente() {
    var async = null;
}

Cliente.prototype.Add = function() {

    var callback =
	{
	    success: function(o) {
	        var doc = o.responseText;

	        eval("var json = " + doc);

	        switch (json.Result.Status) {

	            case "OK":

	                if (_gel('previous').value == "/Ofertas/CheckoutIntegrador.aspx") {
	                    cliente.Logar("compra");
	                }
	                else {
	                    Alert("Seu e-mail foi cadastrado com sucesso!", "Sucesso!", "success");
	                    cliente.Logar();
	                }
	                
	                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;
	        }
	    },

	    failure: function(o) {
	        Alert("Ocorreu um erro. Verifique as informações digitadas.");
	    }
	}

    var url = wrapperUrl + "act_cliente_add";

    var params = buildPostData('form-cadastro-cliente');

    if (_gel('rdbtSexoM').checked)
        params += "&sexo=" + _gel('rdbtSexoM').value;
    else
        params += "&sexo=" + _gel('rdbtSexoF').value;

    var async = YAHOO.util.Connect.asyncRequest('POST', url, callback, params);

}

Cliente.prototype.AddEmail = function() {

    var callback =
	{
	    success: function(o) {
	        var doc = o.responseText;

	        eval("var json = " + doc);

	        switch (json.Result.Status) {

	            case "OK":

	                Alert("Seu e-mail foi cadastrado com sucesso! <br/>Em breve você receberá as melhores ofertas de Campos do Jordão. <br/>São descontos de até 90%", "Sucesso!", "success");

	                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;
	        }
	    },

	    failure: function(o) {
	        Alert("Ocorreu um erro. Verifique as informações digitadas.");
	    }
	}

    var url = wrapperUrl + "act_cliente_teaser_add";

    var params = "email=" + encodeURIComponent(_gel('txtEmailCadastrar').value); //buildPostData('cadastro-cliente');
    var async = YAHOO.util.Connect.asyncRequest('POST', url, callback, params);

}

Cliente.prototype.Logar = function(previous) {

    var callback =
	{
	    success: function(o) {
	        var doc = o.responseText;

	        eval("var json = " + doc);

	        switch (json.Result.Status) {

	            case "OK":

	                if (previous == "compra") {
	                    window.location.href = '/Ofertas/CheckoutIntegrador.aspx';
	                }
	                else {
	                    window.location.href = '/Ofertas/Cliente/MinhaConta.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;
	        }
	    },

	    failure: function(o) {
	        Alert("Ocorreu um erro. Verifique as informações digitadas.");
	    }
	}

    var url = wrapperUrl + "act_user_authenticate";

    var step = _gel('step').value;
    if (step == undefined) step = "0";
    
    var email = "";
    var senha = "";

    email = encodeURIComponent(_gel('txtEmail').value);
    senha = encodeURIComponent(_gel('txtSenha').value);

    if (previous == "compra" && step == "0") {
        email = encodeURIComponent($('input[id = "txtEmailCadastro"]').val());
        senha = encodeURIComponent($('input[id = "txtSenhaCadastro"]').val());
    }
    else {
        email = encodeURIComponent(_gel('txtEmail').value);
        senha = encodeURIComponent(_gel('txtSenha').value);
    }

    var params = "email=" + email;
    params += "&senha=" + senha;

    var async = YAHOO.util.Connect.asyncRequest('POST', url, callback, params);

}

Cliente.prototype.AbrirCupom = function(idPedido) {

    $.ajax({
        url: 'CupomDetalhes.aspx?idPedido=' + idPedido,
        cache: false,
        error: function(xhr, ajaxOptions, thrownError) {
            Alert(thrownError, "Erro", "error");
        },
        success: function(data) {
            $('<div id="dialog-cupom"></div>').html(data).dialog({
                maximize: false,
                modal: true,
                width: 480,
                height:600,
                //title: titulo,
                overlay: { background: '#000000', opacity: 0.4 }
            });
        }
    });

}

Cliente.prototype.SalvarNomesOferta = function(idPedido) {

    var tbody = _gel("tblCupons").getElementsByTagName("tbody")[0];
    var rows = tbody.getElementsByTagName("tr");

    var data = "";

    for (var i = 0; i < rows.length; i++) {

        var cells = rows[i].getElementsByTagName("td");
        var o_nome = cells[1].getElementsByTagName("input")[0].value;

        data += (i + 1) + "/" + o_nome;

        if (i + 1 < rows.length)
            data += ";";
    }

    $.ajax({
        url: wrapperUrl + 'act_pedido_nomes_salvar&idPedido=' + idPedido + '&data=' + data,
        cache: false,
        dataType: "json",
        error: function(xhr, ajaxOptions, thrownError) {
            Alert(thrownError, "Erro", "error");
        },
        success: function(data) {
            $('#dialog-cupom').hide();
            $('#dialog-cupom').dialog('close');
            cliente.AbrirCupom(idPedido);
        }
    });

}

Cliente.prototype.ImprimirCupom = function(idPedido, idCupom) {

    window.open("/Ofertas/Cliente/CupomImpressao.aspx?idPedido=" + idPedido + "&idCupom=" + idCupom, "Cupom", "width=480,height=400,resizable=0,scrollbars=1");

}
