function hasOwnProperty(obj, prop) 
{
    if (Object.prototype.hasOwnProperty) 
    {
        return obj.hasOwnProperty(prop);
    }
    return typeof obj[prop] != 'undefined' && obj.constructor.prototype[prop] !== obj[prop];
}

function checkCount( value )
{
    var val = parseInt(value);

    if ( isNaN(val) )
    {
        val = 1;    
    }
    
    if( val <= 1 )
    {
        val = 1;
    }
    
    return val;
}

function updatePrice(item_id, quantity)
{
    var item_summ   = 0;
    var all_price   = 0;
    var all_count   = 0;
    var cookie_str  = '';
    
    basket[item_id]['count'] = quantity;
    
    //update item image
    if ( quantity == 1)
    {
        obj = document.getElementById('iid_'+item_id);
        if(obj)
        {
        	obj.src = '/i/marker_9.gif';
        }
    }
    else
    {
        obj = document.getElementById('iid_'+item_id);
        if(obj)
        {
        	obj.src = '/i/marker_10_1.gif';
        }
    }
    
    //update item_summ
    /*
    item_summ = basket[item_id]['count'] * basket[item_id]['price'];
    obj = document.getElementById('b_i_price_'+item_id);
    if(obj)
    {
    	obj.innerHTML = item_summ;
    }
    */
    
    //update item_count
    obj = document.getElementById('b_i_quantity_'+item_id);
    if(obj)
    {
    	obj.innerHTML = quantity+'штук'+( quantity == 1 ? 'a' : ( quantity <=4 ? 'и' : '' ) );
    }

    for ( key in basket )
    {
        if( hasOwnProperty(basket, key) )
        {
            all_price   += parseInt(basket[key]['count']) * parseInt(basket[key]['price']);
            cookie_str  += '-' + key + '_' + basket[key]['count'];
            all_count   += parseInt(basket[key]['count']);
        }
    }
    all_price = Math.ceil(all_price * 100) / 100;
    
    //update summ_price
    obj = document.getElementById('b_s_price');
    if(obj)
    {
    	obj.innerHTML = all_price;
    }
    
    //refresh basket
    setBasket(all_count, all_price, cookie_str);
}

function setBasket( count, price, cookie_str )
{
    obj = document.getElementById('basket_count');
    if( obj )
    {
    	obj.innerHTML = count + ' товар' + ( count >= 5 ? 'ов' : ( count > 1  ? 'a' : '' )  );
    }
    
    obj = document.getElementById('basket_price');
    if( obj )
    {
    	obj.innerHTML = price;
    }
    
    //обновляем куки
    document.cookie = "noteCount=" + count + "; path=/";
    document.cookie  = "notePrice=" + price + "; path=/";
    
    if ( cookie_str != '' )
    {
        p_cookie='book='+escape(cookie_str)+'-NSB; path=/';
        document.cookie=p_cookie;
    }
}

function addToBasket(iid, price) 
{
    if( !isInBk(iid) ) 
    {
         var count=parseInt(cookieVal("noteCount"))+1;
         var fullprice = parseFloat( cookieVal("notePrice") )+parseFloat(price);
         
         fullprice = Math.ceil(fullprice*100)/100;
         
         addToBk(iid+'_1');
         
         /*
         obj=document.getElementById('ib_'+iid);
         if(obj)
		 {
		 	obj.innerHTML = 'товар отложен';
		 }
		 */
         
		 setBasket(count, fullprice, '');
		 
		 $('atb_link_'+iid).style.display = 'none';
		 $('atb_text_'+iid).style.display = 'block';
    } 
    
	return false;
}

function cookieVal(cookieName) 
{
    thisCookie = document.cookie.split("; ")
    for (i = 0; i < thisCookie.length; i++) 
    {
    	if (cookieName == thisCookie[i].split("=")[0]) 
        {
        	return thisCookie[i].split("=")[1];
        }
    }
    return 0;
}


function isInBk(posNum)
{
    var p_cookie=document.cookie;
    var cookie;
    cookie=unescape(p_cookie.substr(p_cookie.indexOf('book=')+5,p_cookie.indexOf('-NSB')-p_cookie.indexOf('book=')-4));
    return cookie.indexOf('-'+posNum+'_')==-1?false:true;
}

function addToBk(code)
{
    var p_cookie=document.cookie;
    var cookie;
    cookie=unescape(p_cookie.substr(p_cookie.indexOf('book=')+5,p_cookie.indexOf('-NSB')-p_cookie.indexOf('book=')-5));
    p_cookie='book='+escape(cookie+'-'+code)+'-NSB; path=/';
    document.cookie=p_cookie;
}

function show_doc(url,w,h)
{
	var l=15;
	var t=15;
	return window.open(url,'','left='+l+',top='+t+',width='+w+',height='+h+',toolbar=no,statusbar=no,scrollbars=yes');
}

function refreshCompareAfterCCD( arr_str )
{
    var arr;
    
    arr = arr_str.split('_');
    
    for(i=0; i < arr.length; ++i)
    {   
        try
        {
            $('ict_text_'+arr[i]).style.display = 'none';
            $('ict_link_'+arr[i]).style.display = 'block';
        }
        
        catch(err)
        {
            
        }
    }
}