$(document).ready(function() {
    /*$('form').keypress( function(event) {
            if (event.ctrlKey && event.keyCode==13) {
                this.submit();
            }
        }
    );*/
    $("#msg").ajaxError(function(event, request, settings) {
        showPopup("Error");
    });
});
function removeFile(el, id) {
    $el = $(el);
    $file = $('#'+id);
    if ($file.val()=='') {
        $file.val($el.attr('filename'));
    } else {
        $el.attr('filename', $file.val());
        $file.val('');
    }
}
function submit_form(id) {
    $ajaxForm = $('#'+id);
    $ajaxFormAction = $ajaxForm.attr('action') ? $ajaxForm.attr('action'): document.location;
    $.ajax({
        url: $ajaxFormAction,
        type: 'POST',
        dataType: 'json',
        data: $ajaxForm.serialize(),
        success: function(data) {
            if (data.status == 'OK') {
                alert('ok');
            } else {
                var str = "Error in fields: \n";
                error = []
                for(i in data.content) {
                    error.push(data.content[i].caption);
                }
                alert(str+error.join("\n"));
            }
        }
    });
}

function addToBasket(id, count, type) {
    if (count>0) {
        var url = 'shop/basket/add/product/'+id+'/count/'+count+'/';
        if (type !== undefined) {
            url += 'type/'+type+'/';
        } 
        $.getJSON(url, function(json) {
            if (json.status == 'OK') {
                eval(json.js);
            }
            showPopup(json.content);
        });
    } else {
        alert("Укажите колличество");
    }
    return false;
}
function removeFromBasket(id, size) {
    $.getJSON('/shop/basket/remove/product/'+id+'/size/'+size+'/', function(json) {
        if (json.status == 'OK') {
            eval(json.js);
        }
        showPopup(json.content);
    });
}
function showPopup(text, time) {
    if (time != 0 && !time) {
        time = 1500;
    }
    /*$('#message-text').html(text);
    $("#msg").show();
    if (time) setTimeout('$("#msg").hide()', time);*/
    alert(text);
}
function UpdateBasket(num, sum, kind) {
    //$('#basket-num').text(num);
    //$('#basket-sum').text(sum);
    //$('#basket-kind').text(kind);
    $('#basket-num').text(kind);
}
