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 userIsLoggedIn() {
    var cookie = readCookie('UKPLCSite');
    return /LoginID=[0-9]/.test(cookie);
}

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 c.substring(nameEQ.length,c.length);
    }
    return null;
}

var goToB2B = function() {
    // Ensure the cookie is set to B2B, and make sure we're not 
    // looking at a page restricted to publicly-buyable products

    createCookie('marketplace_public_b2b', 'b2b', 7);

    $('switchPublicB2bTab').set('html', 'Switch to Public');

    if (/\/categories\//i.test(window.location)) {
        window.location = window.location.href.replace(/\/categories\//i, '/categories-b2b/');
    }
    else if (/index.htm/i.test(window.location)) {
        window.location = window.location.href.replace(/index.htm/i, 'index-b2b.htm');
    }
    else if (/all-categories.htm/i.test(window.location)) {
        window.location = window.location.href.replace(/all-categories.htm/i, 'all-categories-b2b.htm');
    }
    else if (/plc.net\/?#?$/i.test(window.location)) {
        window.location = window.location.href.replace(/plc.net\/?#?$/i, 'plc.net/index-b2b.htm');
    }
    return false;
}

var goToPublic = function() {
    // Ensure the cookie is set to Public, and make sure we're 
    // looking at a page restricted to publicly-buyable products

    createCookie('marketplace_public_b2b', 'public', 7);

    $('switchPublicB2bTab').set('html', 'Switch to B2B');

    if (/\/categories-b2b\//i.test(window.location)) {
        window.location = window.location.href.replace(/\/categories-b2b\//i, '/categories/');
    }
    else if (/index-b2b.htm/i.test(window.location)) {
        window.location = window.location.href.replace(/index-b2b.htm/i, 'index.htm');
    }
    else if (/all-categories-b2b.htm/i.test(window.location)) {
        window.location = window.location.href.replace(/all-categories-b2b.htm/i, 'all-categories.htm');
    }
    return false;
}

var publiclyBuyableOnly = true;

var notification
var notificationDivClass = '.notimoo'

window.onload=function(){

    // Check the tabs reflect whether or not the user is logged in
    // (currently this is independent of whether or not they are viewing the public products)
    if (userIsLoggedIn()) {
        $('loginTab').setStyle('display', 'none');
        $('controlCentreTab').setStyle('display', '');
        $('logoutTab').setStyle('display', '');
    }
    else {
        $('loginTab').setStyle('display', '');
        $('controlCentreTab').setStyle('display', 'none');
        $('logoutTab').setStyle('display', 'none');
    }

    if ($('publiclyBuyableOnly') != null) {
        publiclyBuyableOnly = $('publiclyBuyableOnly').get('html') == 'True'
    }

    // The tab toggles between just publicly-buyable and all publicly-visible products
    $('switchPublicB2bTab').addEvent('click', function() {
        if (publiclyBuyableOnly) {
            goToB2B();
        }
        else {
            goToPublic();
        }
    });
    
    if (publiclyBuyableOnly) {
 
        notification = new Notimoo({
            locationVType: 'top',
            locationHType: 'right'
        });

        if (readCookie('marketplace_public_b2b') == null) {
            notification.show({
                title: '<h1><span class="title">@UKPLC</span><span>Please select where would you like to go.</span></h1>',
                message: "<span class='close'><a href='#' class='button' onclick='goToPublic(); notification.close($$(notificationDivClass)[0]);'><img src='http://images.uk-plc.net/templates/ATUKMarketplace/images/close.gif'/></a></span><div style='clear:both'></div><div class='marketplace'><table><tr><td><a href='#' onclick='goToPublic(); notification.close($$(notificationDivClass)[0]);'><img border='0'  src='http://images.uk-plc.net/templates/ATUKMarketplace/images/notiimages/marketplace.jpg' alt='This will close the popup until you clear the cookies of your internet browser'/></a></td><td><a href='#' class='button' onclick='goToPublic(); notification.close($$(notificationDivClass)[0]);'>Public Site</a></td><td><a href='#' class='button' onclick='goToB2B(); notification.close($$(notificationDivClass)[0]);'>B2B Site</a></td><td><a href='#' onclick='goToPublic(); notification.close($$(notificationDivClass)[0]);'><img border='0'  align='right' width='130px' src='http://images.uk-plc.net/templates/ATUKMarketplace/images/notiimages/transparent.gif' /></a></td></tr></table><div class='text'>@UK Public or B2B eMarketplace to buy products and services.</div></div><div class='other'><a href='http://purchasing.uk-plc.net/buyerdotnet2/default.aspx'><img border='0' src='http://images.uk-plc.net/templates/ATUKMarketplace/images/notiimages/purchasing.jpg' /></a><div class='text'>Log in to @UK Purchasing system to manage your organisation's portal or buy products and services from approved suppliers.</div></div><div class='other'><a href='http://www.spendinsight.com/'><img border='0'  src='http://images.uk-plc.net/templates/ATUKMarketplace/images/notiimages/spendinsight.jpg' /></a><div class='text'>Use our fast spend analysis service to find out where you could save money.</div></div><div class='other'><a href='http://www.green-insight.com/'><img border='0'  src='http://images.uk-plc.net/templates/ATUKMarketplace/images/notiimages/greeninsight.jpg' /></a><div class='text'>Find out your carbon footprint, and its composition by supplier and by product.</div></div><div class='other'><a href='http://companyregistration.uk-plc.net/'><img border='0'  src='http://images.uk-plc.net/templates/ATUKMarketplace/images/notiimages/companyformation.jpg' /></a><div class='text'>Form a Company with @UK Company Formation or Log in to manage your Company Formation and Appointments.</div></div><div class='other'><a href='http://controlcentre.uk-plc.net/homepage.aspx'><img border='0'  src='http://images.uk-plc.net/templates/ATUKMarketplace/images/notiimages/controlcentre.jpg' /></a><div class='text'>Log in to @UK Control Centre to manage your eCommerce site or check your Customer Order and send them an Invoice</div></div><div class='multiple'><table width='100%' border='0' cellspacing='0' cellpadding='5'><tr><td align='center'><div><a href='http://www.uk-plc.net/domains.htm'><img border='0'  src='http://images.uk-plc.net/templates/ATUKMarketplace/images/notiimages/domains.jpg' /></a><br /><a href='http://www.uk-plc.net/domains.htm' class='button'>Select</a></div></td><td align='center'><div><a href='http://www.uk-plc.net/webmail.htm'><img border='0'  src='http://images.uk-plc.net/templates/ATUKMarketplace/images/notiimages/webmail.jpg' /></a><br /><a href='http://www.uk-plc.net/webmail.htm' class='button'>Select</a></div></td><td align='center'><a href='http://www.uk-plc.net/web-hosting.htm'><img border='0'  src='http://images.uk-plc.net/templates/ATUKMarketplace/images/notiimages/hosting.jpg' /></a><br /><a href='http://www.uk-plc.net/web-hosting.htm' class='button'>Select</a></td></tr></table></div>",
                width: 600,
                height: 800,
                sticky: true
            });
        }
    }
    if (readCookie('marketplace_public_b2b') == 'b2b') {
        goToB2B();
    }
    if (readCookie('marketplace_public_b2b') == 'public') {
        goToPublic();
    }
}
