if (typeof String.prototype.trim == "undefined") {
    String.prototype.trim = function() {
        var s = this.replace(/^\s*/, "");
        return s.replace(/\s*$/, "");
    }
}

function Cookie() {
    this.get = function(name) {
        var cookies = document.cookie.split(";");
        for (var i = 0; i < cookies.length; i++) {
            var a = cookies[i].split("=");
            if (a.length == 2) {
                a[0] = a[0].trim();
                a[1] = a[1].trim();
                if (a[0] == name) {
                    return unescape(a[1]);
                }
            }
        }
        return "";
    }
    this.set = function(name, value) {
        var date = new Date();
        var days = 3;
		date.setTime(date.getTime()+(days*24*60*60*1000));
        document.cookie = name + "=" + escape(value) + "; expires=" + date.toGMTString() + "; path=/";
    }
    this.del = function(name) {
        document.cookie = name + "=; expires=Thu, 01-Jan-70 00:00:01 GMT; path=/";
    }
}
cookie = new Cookie();

function change_account_type(select)
{
    var action = document.getElementById('registration_form').action;

    if (select.selectedIndex == 0) {
        location.href = action + '&type=user';
    } else {
        location.href = action + '&type=firm';
    }
}

function RegistrationFormInit(is_person)
{
    var is_person_select = document.getElementById('id_is_person');

    if (is_person == 'True') {
        is_person_select.selectedIndex = 0;
    } else {
        is_person_select.selectedIndex = 1;
    }
}

function show_delivery(city)
{
    var city_id = city.options[city.selectedIndex].value;

    var filials = get_delivery(city_id);
    var filials_count = filials.length;

    var delivery_select   = document.getElementById('id_delivery');
    var delivery_info     = document.getElementById('delivery_info');
    var new_delivery_info = '';

    // clear delivery select
    delivery_select.options.length = 0;
    // add "-----" option
    delivery_select.options[0] = new Option("---------", "");

    for (var i=0;i<filials_count;i++) {
        delivery_select.options[i+1] = new Option(filials[i]._company_cache.company + ' (' + filials[i].address + ')', filials[i].id);
        var params = filials[i].params

        var filial_style = 'display: none;';
        if (i == 0) {
            delivery_select.options[i+1].selected = true;
            filial_style = '';
        }

        new_delivery_info += '<div class="delivery" id="' + filials[i].id + '" style="' + filial_style + '"><p>Адрес: ' + filials[i].address + '</p><p>Телефон: ' + filials[i].phone + '</p>';

        new_delivery_info += (filials[i].shipping_price>0) ? '<p>Стоимость доставки: <b>' + filials[i].shipping_price + ' руб.</b></p>' : '';
        new_delivery_info += (filials[i].shipping_time!= 0) ? '<p>Срок доставки: <b>' + filials[i].shipping_time + ' суток</b></p>' : '';

        if (params) {
            for (var n=0;n<params.length;n++) {
                if (params[n].type == 1) {
                    new_delivery_info += '<p>' + params[n].name + ': ' + params[n].value + '</p>';
                } else {
                    new_delivery_info += '<p><a href="' + params[n].value + '" target="_blank">' + params[n].name + '</a></p>';
                }
            }
        }

        new_delivery_info += filials[i].url ? '<p><a href="' + filials[i].url + '" target="_blank">Сайт</a></p>' : '';
        new_delivery_info += filials[i].image ? '<p><a href="/media/' + filials[i].image + '" target="_blank">Схема проезда</a></p>' : '';
        new_delivery_info += '</div>'
    }

    delivery_info.innerHTML = new_delivery_info;
}

function show_delivery_info(filial)
{
    var filial_id = filial.options[filial.selectedIndex].value;

    var delivery_info = document.getElementById('delivery_info');
    delivery_info_objects = delivery_info.getElementsByTagName('div');

    for (i=0;i<delivery_info_objects.length;i++) {
        if (delivery_info_objects[i].id == filial_id) {
            delivery_info_objects[i].style.display="";
        } else {
            delivery_info_objects[i].style.display="none";
        }
    }
}

function get_delivery(city_id)
{
    var oXmlHttp = zXmlHttp.createRequest();
    oXmlHttp.open("GET", "/delivery/?city_id=" + city_id, false);
    oXmlHttp.send(null);

    if (oXmlHttp.status == 200) {
        return oXmlHttp.responseText.parseJSON();
    }

    return false;
}

function block_display(ids, display)
{
    count = ids.length;
    for (i=0; i<count; i++) {
        document.getElementById(ids[i]).style.display=display;
    }
}

function windowOpen(url)
{
    //window.open(url, '', 'resizable=no,location=yes,menubar=no,scrollbars=yes,status=no,toolbar=yes,fullscreen=no,dependent=no,width=460,height=530,status');
    window.open(url, '', 'resizable=yes,location=yes,menubar=yes,scrollbars=yes,status=yes,toolbar=yes,fullscreen=no,dependent=yes,width=460,height=530,status');
}

function cartInit()
{
    cart   = [];

    var cart_json = cookie.get('cart');
    var cart_price    = 0;
    var cart_quantity = 0;

    if (cart_json!= '') {
        cart            = cart_json.parseJSON();
        var cart_length = cart.length;

        for (var i=0;i<cart_length;i++) {
            cart_quantity = cart_quantity + parseInt(cart[i].q);
            cart_price    = parseFloat(cart_price + (cart[i].p * cart[i].q));
        }
    }

    document.getElementById('quantity').innerHTML = cart_quantity;
    document.getElementById('price').innerHTML    = cart_price;
}

function add2cart()
{
    var id       = document.getElementById('cart_id').value;
    var quantity = document.getElementById('cart_quantity').value;
    var price    = document.getElementById('cart_price').value;
    var already_in = false;

    var add_cart = {'i' : id, 'q' : quantity, 'p' : price};

    for (var i=0;i<cart.length;i++) {
        if (cart[i]['i'] == id) {
            cart[i]['q'] = parseInt(cart[i]['q']) + parseInt(quantity);
            already_in = true;
        }
    }

    if (already_in == false) {
        cart.push(add_cart);
    }

    cookie.set('cart', cart.toJSONString());
    cartInit();

    document.getElementById('quantity_window').style.display = 'none';
}

function emptyCart()
{
    cookie.del('cart');
    cartInit();
    location.href='';
}

function emptyOrderCart()
{
    cookie.del('cart');
    cartInit();
}

function moveQuantityWindow(){
    var w = document.getElementById('quantity_window');
    w.style.top = document.body.scrollTop;
}

function quantityWindow(id, quantity, price)
{
    document.getElementById('quantity_window').style.display = '';
    document.getElementById('cart_quantity').value = quantity;
    document.getElementById('cart_id').value       = id;
    document.getElementById('cart_price').value    = price;

    moveQuantityWindow();
}

function quantityWindowClose()
{
    document.getElementById('quantity_window').style.display = 'none';
}

function car_check_table()
{
    var table = document.getElementById('id_car_check_table');

    if (table.style.display == 'none') {
        table.style.display = '';
    } else {
        table.style.display = 'none';
    }
}

