var boolFlagCartBusy = false;
var tmpVal = 0;

/**
 * Update basket summary.
 */
function updateBasketInfo( data ) {
    $('#basket_info_items').html( data.basket_info.items );
    
    $('#basket_info_summ').html( data.basket_info.summ );
    $('#basket_summ').html( data.basket_info.summ );
    $('#basket_summ_nds').html( data.basket_info.summ_nds );
    
    $('#basket_info_total').html( data.basket_info.total );
    $('#basket_total').html( data.basket_info.total );
    $('#basket_total_nds').html( data.basket_info.total_nds );
}

function updateProductInfo( data ) {
    $('#prod_summ_'+data.prod_info.id).html(data.prod_info.summ);
    $('#prod_nds_'+data.prod_info.id).html(data.prod_info.nds);
}

/**
 * Change number of products in the cart.
 */
function updateCart( article, num ) {
    if ( num.indexOf('.') == num.length-1 ) {
        num = num + '0';
    }
    
    if ( num == 0 ) {
        deleteFromCart( article );
    }
    else {
        $.post(
            site_url + 'basket/update_cart',
            { id: article, new_num: num }, 
            function (data) {
                boolFlagCartBusy = false;
                
                if ( data.error == '' ) {
                    updateBasketInfo( data );
                    updateProductInfo( data );
                    
                    jQuery.noticeAdd({
                        caption: 'Обновление:',
                        text: data.msg,
                        stay: false
                    });
                }
                else {
                    jQuery.noticeAdd({
                        caption: 'Обновление:',
                        text: data.error,
                        stay: false
                    });
                }
            },
            'json'
        );
    }
}

/**
 * Delete one product from cart.
 */
function deleteFromCart( article ) {
    $.post(
        site_url + 'basket/delete_from_cart',
        { id: article },
        function (data) {
            if ( data.error == '' ) {
                updateBasketInfo( data );
                
                $("#tr_"+article).remove();
                
                if ( data.msg != '' ) {
                    $('#basket_content').html(data.msg);
                }
                
                jQuery.noticeAdd({
                    caption: 'Удаление:',
                    text: '<strong>Товар:</strong> "'+data.prod_info.name,
                    stay: false
                });
            }
            else {
                jQuery.noticeAdd({
                    caption: 'Удаление:',
                    text: data.error,
                    stay: false
                });
            }
            
            boolFlagCartBusy = false;
        },
        'json'
    );
}

/**
 * Starts clear basket process.
 */
function clear_cart( v,m,f ) {
    if ( v == true ) {
        if (!boolFlagCartBusy) {
            boolFlagCartBusy = true;
            
            clearCart();
        }
    }
}

/**
 * Call server to clear basket.
 */
function clearCart() {
    $.post(
        site_url + 'basket/clear_cart',
        { cnd: 'go' },
        function (data) {
            updateBasketInfo( data );
            
            $('#basket_content').html(data.msg);
            
            jQuery.noticeAdd({
                caption: 'Корзина:',
                text: data.msg,
                stay: false
            });
            
            boolFlagCartBusy = false;
        },
        'json'
    );
}

/**
 * Call server to add product to the cart.
 */
function addToCart( postData ) {
    $.post(
        site_url + 'basket/add_to_cart',
        postData,
        function (data) {
            if ( data.error == '' ) {
                updateBasketInfo( data );
                
                jQuery.noticeAdd({
                    caption: 'Добавлено:',
                    text: '<strong>Товар:</strong> '+data.prod_info.name+'<br><strong>Кол-во:</strong> '+data.prod_info.num+' шт.',
                    stay: false
                });
                
                $('#basket_info_order').click( function () { 
                    document.location.href = site_url + 'order';
                });
            }
            else {
                jQuery.noticeAdd({
                    caption: 'Добавление:',
                    text: data.error,
                    stay: false
                });
            }
            
            boolFlagCartBusy = false;
        },
        'json'
    );
}

/**
 * Parses a string and returns a floating point number
 */
function formatNumber( val ) {
    var res="";
    for (i=0; i<val.length; i++) {
        var s=val.substr(i, 1);
        if (s=='0'||s=='1'||s=='2'||s=='3'||s=='4'||s=='5'||s=='6'||s=='7'||s=='8'||s=='9') res=res+s;
        if (i!=0 && s=='.' && (res.indexOf('.')<1)) res=res+'.'; 
    }
    
    return res;
}

/**
 * Check param to be valid floating pointer number.
 */
function validateNumber( num, errorMsg ) {
    pattern = /^([\d]+)(\.[\d]+)?$/;
    
    if ( pattern.test(num) ) {
        return true;
    }
    else {
        if (errorMsg != '') { 
            
            jQuery.noticeAdd({
                caption: 'Ошибка:',
                text: errorMsg,
                stay: false
            });
        }
        
        return false;
    }
}
