var productsCookie = 'building_construction';
var container = 'e_borsa_left';
var productsContainer = 'construction_exchange_cart';
var constructionExchangeCart = 'construction_exchange_cart_content';
var constructionExchangeProducts = 'construction_exchange_cart_products';

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return unescape(c.substring(nameEQ.length,c.length));
    }
    return null;
}

function setCookie(name,value,expiredays)
{
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=name+ "=" +escape(value)+
    ((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}

function getId(id){
    return document.getElementById( id );
}

function hide(id){
    if( idObj = getId(id) )
        idObj.style.display = 'none';
}

function show(id){
    if( idObj = getId(id) )
        idObj.style.display = 'block';
}

function addToCart(product, amountID){
    var products = new Array();
    var amountObj = getId(amountID);
    var amount = amountObj.value;

    amountObj.value = '';
    
    if( cookie = readCookie( productsCookie ) )
        products = cookie.split(';;;');

    products[products.length] = product + '###' + amount;
    setCookie(productsCookie, products.join(';;;') );

    showCart();
}

function showCart(){
    var html = '';
    var cookie = readCookie( productsCookie );
    
    if( !cookie ){
        getId(productsContainer).innerHTML = html;
        hide(container);
        return;
    }
        
    products = cookie.split(';;;');

    for( c = 0; c < products.length; c++){
        elems = products[c].split('###');
        html += '<table class="e_borsa_table"><tr><td valign="top"><p>' + (c+1) + '.&nbsp;</p></td><td><p> ' + elems[0] + (elems[1] ? ' - <strong> ' + elems[1] + '</strong></p>' : '') + '</p></td></tr></table>';
    }

    getId(productsContainer).innerHTML = html;
    show(container);
}

function removeProduct(index){
    var cookie = readCookie( productsCookie );
    var products2 = new Array();
    
    if( !cookie ) return;

    products = cookie.split(';;;');

    for( c = 0; c < products.length; c++)
        if( c != index ) 
            products2[products2.length] = products[c];
        
    if( products2.length >= 1 )
        setCookie(productsCookie, products2.join(';;;') );
    else 
        eraseCookie(productsCookie);

    showCartDetails();
    showCart();
}

function showCartDetails(){
    var html = '';
    var cookie = readCookie( productsCookie );
    
    if( !cookie ){
        getId(constructionExchangeProducts).innerHTML = html;
        hide(constructionExchangeCart);
        return;
    }

    products = cookie.split(';;;');

    for( c = 0; c < products.length; c++){
        elems = products[c].split('###');
        html += '<li><input type="hidden" name="products[]" value="' + (c + 1) + '. ' + htmlentities(elems[0]) + ' - ' + htmlentities(elems[1]) + '" />' + (c + 1) + '. ' + htmlentities(elems[0]) + '<strong> ' + htmlentities(elems[1]) + '</strong> <a href="javascript:void(0);" onClick="removeProduct(' + c + ');" title="Махни продукт от списъка"><img alt="Махни продукт от списъка" src="/images/www/eborsa/remove_added_product.jpg"></a></p>';
    }

    getId(constructionExchangeProducts).innerHTML = html;
    show(constructionExchangeCart);
}

function clearCart(){
    eraseCookie(productsCookie);
    showCart();
    if( getId(constructionExchangeProducts) )
       showCartDetails();
}

function TryParseInt(str, defaultValue) {
    return /^\d+$/.test(str) ? parseInt(str) : defaultValue;
}

function htmlentities (string) {
    return string.split("&").join("&amp;").split( "<").join("&lt;").split(">").join("&gt;").split('"').join("&quot;");
}

