﻿$(document).ready(function() {
    $('a[id$=lnkAdicionarAoCarrinho]').click(function(event) {
        event.preventDefault();

        var a = this;

        $.ajax({
            url: $(this).attr('href'),
            success: function(data, textStatus) {
                $(a).addClass('inative');
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                $(a).parent().find('.logtip1').show().find('span').html(getException(XMLHttpRequest.responseText));
            }
        });
    });

    $('.logtip1 .tipClose').click(function(event) {
        event.preventDefault();

        $(this).parent().parent().hide();
    })
});

function getException(responseText) {
    var message = responseText.replace(/\n|\r/g, '\uffff').replace(/^.*<h2>\s*<i>\s*(.*?)\s*<\/i>\s*<\/h2>.*$/, '$1');


    return decode(message);
}

function decode(string) {
    var match;

    while (/&#\d+;/.test(string)) {
        match = string.match(/&#(\d+);/);
        string = string.replace(new RegExp(match[0], 'g'), String.fromCharCode(match[1]));
    }

    return string;
}