
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

var xmlreqs=new Array();
var theDiv='';
var toggleState=0;

function CXMLReq(type,xmlhttp,theDiv) {
    this.type = type;
    this.xmlhttp = xmlhttp;
    this.theDiv = theDiv;
}

function xmlreqGET(url,theDiv) {
    document.getElementById(theDiv).innerHTML='<img src="/_img/_ajax/1.gif" />';
    var xmlhttp = false;
    var theDiv = theDiv;
    if (window.XMLHttpRequest) {
        xmlhttp=new XMLHttpRequest();
        xmlhttp.onreadystatechange = xmlhttpChange;
        xmlhttp.open("GET",url,true);
        xmlhttp.send(null);
    } else if (window.ActiveXObject) {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        if (xmlhttp) {
            xmlhttp.onreadystatechange = xmlhttpChange;
            xmlhttp.open("GET",url,true);
            xmlhttp.send();
        }
    }
    var xmlreq = new CXMLReq('', xmlhttp, theDiv);
    xmlreqs.push(xmlreq);
}

function xmlhttpChange() {
    if (typeof(window['xmlreqs']) == "undefined") return;
    for (var i=0; i < xmlreqs.length; i++) {
        if (xmlreqs[i].xmlhttp.readyState == 4) {
            if (xmlreqs[i].xmlhttp.status == 200 || xmlreqs[i].xmlhttp.status == 304) {
                handle_response(xmlreqs[i].theDiv, xmlreqs[i].xmlhttp);
                xmlreqs.splice(i,1);
                i--;
            } else {
                xmlreqs.splice(i,1);
                i--;
            }
        }
    }
}

function handle_response(theDiv,xmlhttp) {
    var thisResponse = xmlhttp.responseText;
    if (theDiv != '') {
        document.getElementById(theDiv).innerHTML = thisResponse;
    }
}

function clearElement(element) {
    document.getElementById(element).value = '';
}

function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if (browser == "Microsoft Internet Explorer") {
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        ro = new XMLHttpRequest();
    }
    return ro;
}

/* Calling Functions */

function viewByCategory(cat,subcat) {
    xmlreqGET('/_ajax/prod-nav.php?viewby=category&cat='+escape(cat)+'&subcat='+subcat,'productCatsTop'); 
}

function viewByBrand(cat,subcat) {
    xmlreqGET('/_ajax/prod-nav.php?viewby=brand&cat='+escape(cat)+'&subcat='+subcat,'productCatsTop'); 
}
 
function viewByType(cat,subcat) {
    xmlreqGET('/_ajax/prod-nav.php?viewby=type&cat='+escape(cat)+'&subcat='+subcat,'productCatsTop'); 
}

function viewByAdvanced(cat,subcat) {
    xmlreqGET('/_ajax/prod-nav.php?viewby=advanced&cat='+escape(cat)+'&subcat='+subcat,'productCatsTop'); 
}
 
function toggleProdCats() {
    if (toggleState == 0) {
        Effect.toggle('prodCats','appear');
        toggleState=1;
    }
}

function toggleProdBrands() {
    if (toggleState == 0) {
        Effect.toggle('prodCats','appear');
        toggleState=1;
    }
}
 
function unToggleProdCats() {
    toggleState=0;
    Effect.toggle('prodCats','appear');
}
 
function addToBasket(sku,q) {
    if (q == '') {
        q = 1;
    }
    if (q == 0) {
        q = 1;
    }
    scroll(0,0);
    document.getElementById('basketPanelText').innerHTML='<img src="/_img/_ajax/1.gif" style="padding-top:7px;" />';
    setTimeout("addToBasketGo('"+sku+"','"+q+"')",1000);
}

function goToBasket() {
    rand = Math.floor(Math.random() * 10000);
    window.open("/greenlife-direct/my-basket/?qs="+rand,"_self");
}

function addToBasketGo(sku,q) {
    rand = Math.floor(Math.random() * 10000);
    xmlreqGET('/_ajax/add-to-basket.php?sku='+sku+'&q='+q+'&qs='+rand,'basketPanelText');
    /* var msg = window.confirm("Product has been added to your basket.\n\nTo view your basket and checkout please click OK, or click Cancel to continue shopping.");
    if (msg) {*/
    setTimeout('goToBasket()', 2000);
    /*}*/
}

function newsPic(nid,num) {
    scroll(0,0);
    xmlreqGET('/_ajax/news-pic.php?nid='+escape(nid)+'&num='+num,'townStoreStaticRight'); 
}

function changeDelivery() {
    var del = document.getElementById('del').value;
    window.open("/?do=GO_delivery&del="+del,"_self");
}
 
function changeCountry() {
    var country = document.getElementById('postage_country').value;
    window.open("/?do=GO_country&country="+country,"_self");
}
