function getLayerById(id)
{
    for (i = 0; i < document.layers.length; ++i)
        if (document.layers[i].id && id == document.layers[i].id) {
            return document.layers[i];
        }
        return null;
}

if (!document.getElementById && document.all) 
    document.getElementById = new Function("id", "return document.all[id];");
else if (document.layers)
    document.getElementById = getLayerById;


function openWindow(url)
{
    regexp = /.*\/$/;
    if (regexp.test(url)) return true;
    size="";
    if (arguments.length>1)
        size=arguments[1]+", ";
    var okno=window.open(url, "", size+" resizable=yes, scrollbars=yes, toolbar=no, status=no, dependent=yes");
    okno.focus();
    return false;
}



function getCookie(name) // use: getCookie('name');
{
    begin = document.cookie.indexOf(name + '=');
    if (begin == -1) return null;
    begin = document.cookie.indexOf('=', begin) + 1;
    end = document.cookie.indexOf(';', begin);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(begin, end));
}

function setCookie(name, value, time) // use: setCookie('name', value, [time]);
{
/*
    date_time='';
    if (time == null || time == '') {
        today = new Date();
        expiry = new Date(today.getTime() + time); // sample 28 days -> 28 * 24 * 60 * 60 * 1000
        date_time = '; expires=' + expiry.toGMTString();
    }
*/
    if (value != null && value != "")
    document.cookie = name + '=' + escape(value) + "; path=/";
    // + date_time;
}


function addBasket(item, amount)
{
    if (item == null || item == '')
        return false;
    if (amount == null || amount == '')
        amount = 1;    
    cookies = getCookie('basket');
    if (cookies == null || cookies == ' ') cookies = '';
    if ((begin_item = cookies.indexOf(item)) != -1) {
        begin_amount = cookies.indexOf('(', begin_item) + 1;
        end_amount = cookies.indexOf(')', begin_item);
        amount = Number(amount)+Number(cookies.substring(begin_amount, end_amount));
        cookies = cookies.substring(0, begin_amount)+amount+cookies.substring(end_amount);
    } else 
        cookies = cookies+(cookies.length>1?'-':'')+item+'('+amount+')';
    setCookie('basket', cookies);
    alert('Товар добавлен в корзину');
    return false;
}

function getAmount(item)
{
    if (document.forms['amount'].amount.value != '' && document.forms['amount'].amount.value != 0)
        addBasket(item, document.forms['amount'].amount.value);
}


