function set_eq_heights() {
    set_title_height();
    set_annotation_height();
    set_flag_place();
}
function set_annotation_height() {
    var ch = 0,
    crs = 0,
    divs = new Array(),
    $el, tp = 0;
    $('.eqheight').each(function(){
        $el = $(this);
        tp = $el.position().top;
        if (crs != tp) {
            for (i = 0; i < divs.length ; i++)
                $($($(divs[i].children()[1])).children()[2]).height(ch);
            divs.length = 0; // empty the array
            crs = tp;
            ch = $($($($el.children()[1])).children()[2]).height();
            divs.push($el);
        } else {
            divs.push($el);
            h = $($($($el.children()[1])).children()[2]).height();
            ch = ch < h ? h : ch;
        }
        // do the last row
        for (i = 0; i < divs.length ; i++)
            $($($(divs[i].children()[1])).children()[2]).height(ch);
    });
}
function set_title_height() {
    var ch = 0,
    crs = 0,
    divs = new Array(),
    $el, tp = 0;
    $('.eqheight').each(function(){
        $el = $(this);
        tp = $el.position().top;
        if (crs != tp) {
            for (i = 0; i < divs.length ; i++)
                $($($($(divs[i].children()[1])).children()[1]).children()[1]).height(ch);
            divs.length = 0; // empty the array
            crs = tp;
            ch = $($($($($el.children()[1])).children()[1]).children()[1]).height();
            divs.push($el);
        } else {
            divs.push($el);
            h = $($($($($el.children()[1])).children()[1]).children()[1]).height();
            ch = ch < h ? h : ch;
        }
        // do the last row
        for (i = 0; i < divs.length ; i++)
            $($($($(divs[i].children()[1])).children()[1]).children()[1]).height(ch);
    });
}
function set_flag_place(){
    $(".deal-flag").each(function(){
       try{
            $el = $(this);
            var p = $el.parent().parent().parent();
            $el.css("top", -1 * (p.height() - 42));
       }catch(e){};
    });
    $(".new-flag").each(function(){
        try{
            $el = $(this);
            var p = $el.parent().parent().parent();
            $el.css("top", -1 * (p.height() - 42));
        }catch(e){};
    });
}
function add_to_cart(p) {
    Dajaxice.shopping.add_to_cart(post_responce, {"item": p, "delivery": $("#sh_delivery").val()});
}
function remove_from_cart(p) {
    Dajaxice.shopping.remove_from_cart(post_responce, {"item": p, "delivery": $("#sh_delivery").val()});
}
function post_responce(data) {
    if (data!=Dajaxice.EXCEPTION) Dajax.process(data);
}
function make_order() {
    if ($("#sh_delivery").val() == 0) {
        alert(ERR_DELIVERY); return;
    }
    Dajaxice.shopping.get_order_form(post_responce, {"delivery": $("#sh_delivery").val()});
}
function save_order() {
    $('#shopping-cart .f-row').removeClass('error')
    var e = false;
    if ($("#id_phone").val() == "") {
        $("#id_phone").parent().addClass("error");
        e = ERR_PHONE;
    }
    if ($("#id_delivery").val() == 0) {
        $("#id_delivery").parent().addClass("error");
        e = ERR_DELIVERY;
    }
    if ($("#id_address").val() == "") {
        $("#id_address").parent().addClass("error");
        e = ERR_ADDRESS;
    }
    if($("#id_people_count").val() < 1){
      $("#id_people_count").parent().addClass("error");
      e = ERR_COUNT;
    }
    if (e != false) { alert(e); return}
    var agree = $("#id_agree_sms").attr("checked") ? 1 : 0;
    var order_later = $("#id_order_later").attr("checked") ? 1 : 0;
    var credit_cart = $("#id_credit_cart").attr("checked") ? 1 : 0;
    Dajaxice.shopping.save_order(post_responce, {"name": $("#id_name").val(),
                                                 "phone": $("#id_phone").val(),
                                                 "email": $("#id_email").val(),
                                                 "delivery": $("#id_delivery").val(),
                                                 "address": $("#id_address").val(),
                                                 "door_code": $("#id_door_code").val(),
                                                 "people_count": $("#id_people_count").val(),
                                                 "birth_day": $("#id_birth_day").val(),
                                                 "birth_month": $("#id_birth_month").val(),
                                                 "order_later": order_later,
                                                 "order_later_date": $("#id_order_later_date").val(),
                                                 "order_later_time_hour": $("#id_order_later_time_hour").val(),
                                                 "order_later_time_minute": $("#id_order_later_time_minute").val(),
                                                 "discount_cart": $("#id_discount_cart").val(),
                                                 "agree_sms": agree,
                                                 "credit_cart": credit_cart
    });
}

function num2money(n_value) {
    if (isNaN(Number(n_value))) return 'ERROR';
    var b_negative = Boolean(n_value < 0);
    n_value = Math.abs(n_value);
    var s_result = String(Math.round(n_value * 1e2) % 1e2 + '00').substring(0, 2);
    var b_first = true;
    var s_subresult;
    while (n_value >= 1) {
        s_subresult = (n_value >= 1e3 ? '00' : '') + Math.floor(n_value % 1e3);
        s_result = s_subresult.slice(-3) + (b_first ? ',' : '') + s_result;
        b_first = false;
        n_value = n_value / 1e3;
    }
    if (b_first) s_result = '0,' + s_result;
    return b_negative ? '(' + s_result + ')' : '' + s_result;
}

