/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure)
{
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain)
{
    if (getCookie(name))
    {
        document.cookie = name + "=" + 
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

//Checks cart.
function checkCart(){
    var c = new String(getCookie('catalog_cart'));
    var arr = c.split(',');
    
    for(var i = 0; i < arr.length; i++){
        if(arr[i] != 'null'){
            //changeText("buytext"+arr[i], "In Cart");
        }
    }
}

function addItemToCatalog(id, siteurl){
    var c = new String(getCookie('catalog_cart'));
    var arr = c.split(',');
    var additem = true;
    
    for(var i = 0; i < arr.length; i++){
        if(arr[i] == 'null' || arr[i] == ''){
            arr.splice(i, 1);
        }
        if(arr[i] == id){
            additem = false;
        }
    }
    if(additem == true){
        arr.push(id);
    }
    //setCookie('catalog_cart', arr.toString(), null, '/');
    //setCookie('catalog_quantity['+id+']', 1, null, '/');
    //alert(arr.toString());
    
    //changeText("buytext"+id, "In Cart");
    
    //var siteurl = 'http://www.bridgelabs.ca';
    window.open(siteurl, 'AddingItem', 'toolbar=yes,location=yes,resizable=yes,scrollbars=yes,left=0,top=0,width=600,height=400');
    //screenX=0,screenY=0,
    //'/folder/showImage.exe/pdf?id=<%=IDNumber%>','Image','toolbar=no,resizable=yes,scrollbars=yes,left=0,top=0,screenX=0,screenY=0,width='+w+',height='+h+'
}

function removeFromCart(id){
    var c = new String(getCookie('catalog_cart'));
    var arr = c.split(',');
    var additem = true;
    
    for(var i = 0; i < arr.length; i++){
        if(arr[i] == id){
            arr.splice(i, 1);
        }
    }
    setCookie('catalog_cart', arr.toString(), null, '/');
    
    //window.opener.parent.location.reload();
    
    //alert(arr.toString());
    
    //changeText("buytext"+id, "In Cart");
}

//function storeItemInfo(id, name, number, description, price, image, thumbnail, imagex, imagey){
//    var iteminfo = new Array(id, name, number, description, price, image, thumbnail, imagex, imagey);
//    var iteminfostr = iteminfo.toString();
//    
//    setCookie('catalog_iteminfo'+id, iteminfostr, null, '/');
//}

function storeItemInfo(id, text){
    var iteminfo = new Array(id, text);
    var iteminfostr = iteminfo.toString();
    
    setCookie('catalog_iteminfo'+id, iteminfostr, null, '/');
}

function storeItemPrice(id, price){
    var iteminfo = new Array(id, price);
    var iteminfostr = iteminfo.toString();
    
    setCookie('catalog_itemprice'+id, iteminfostr, null, '/');
}

function storeColumnInfo(text){
    setCookie('catalog_columninfo', text, null, '/');
}


function changeText(id, text){
    //var spanid = "buytext"+id;
    //document.all.testing.innerHTML = text; //for ie4
    var element = document.getElementById(id);
    if(element != null){
        element.innerHTML = text //ie5, ns6
    }
}

function viewCart(){
    var w = window.open('../bridgeimages/shoppingcart.html', 'Cart', 'scrollbars=yes');
}

//document.onLoad = checkCart;
//addLoadEvent(checkCart);
