function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

addLoadEvent(getCart);


// XMLHTTP Object
// **************
var xmlhttp = false;

try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
    try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");   
    } catch (E) {
        xmlhttp = false;   
    }
}

if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    xmlhttp = new XMLHttpRequest();   
}



// Load the products for the category if passed
// if not just pass empty catID
// ********************************************
function getProducts(catID) {
    
    if (!document.getElementById) return false;
    
    // if catID is undefined set as empty
    if (catID == undefined) {
        catID = '';   
    }
    
    var prodContainer = document.getElementById("content");

    displayLoading(prodContainer);
    
    xmlhttp.open("GET","/merchandise/ajax/getProducts.php?c="+catID,true);
    
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200) {
                prodContainer.innerHTML = xmlhttp.responseText;
            }
        }
    }    
    
    xmlhttp.send(null);     

} 

// Add to cart form for product
// ****************************
function prepareAddtoCartForm() {
    var prodForm = document.getElementById("prodForm");
    prodForm.onsubmit = function() {
        addProduct();
        return false;
    }
}

// Get the contents of the cart
// ****************************
function getCart() {
 
	if (!document.getElementById) return false;
	if (!document.getElementById("order-container")) return false;
	
    var container = document.getElementById("order-container");

    displayLoading(container);

    // load cart contents into basket-wrap
    xmlhttp.open("GET","/merchandise/ajax/getCart.php",true);
    
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200) {
                container.innerHTML = xmlhttp.responseText;
            }
        }
    }
    
    xmlhttp.send(null);
    
}

// Add item to cart
// ****************
function addProduct() {
    
    if (!document.getElementById) return false;
    
    var prodID = document.getElementById("prodID").value;
    var size = document.getElementById("size").value;
    var qty = document.getElementById("qty").value;       
    
    var basket = document.getElementById("basket-status");
    basket.style.display = 'block';
    
    xmlhttp.open("GET","/merchandise/ajax/addCart.php?p="+prodID+"&s="+size+"&q="+qty,true);
    
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200) {
                var response = xmlhttp.responseText.split("|");   
                basket.innerHTML = response[1];
                
                if (response[0] == 'pass') {                    
                    basket.style.border = "solid 2px #96BF6C";
                    fadeup('basket-status',135,216,110);    // green
                    document.forms[0].reset();
                } else {                    
                    basket.style.border = "solid 2px #cc0000";                    
                    fadeup('basket-status',255,153,153);    // red                
                }
                                
            }
        }
    }
    
    xmlhttp.send(null); 
   
}

// Update the contents of the cart
// *******************************
function updateCart() {

	if (!document.getElementById) return false;
	if (!document.getElementById("order-container")) return false;
	
    var container = document.getElementById("order-container");
    var theform   = document.getElementById("basketfrm");

    var url = "/merchandise/ajax/updateCart.php?";

    displayLoading(container);
    
    var npts = theform.getElementsByTagName("input");
    
    // loop over inputs fields
    for (var i=0; i < npts.length; i++) {
        url += npts[i].getAttribute("name");
        url += "=";
        url += npts[i].value;
        url += "&";   
    }        

	// call updateCart.php
    xmlhttp.open("GET",url,true);
    
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200) {
                //container.innerHTML = xmlhttp.responseText;
                setTimeout("getCart()", 700);
                //getCart();
            }
        }
    }
    
    xmlhttp.send(null);

}

// Remove an item from the cart
// ****************************
function removeCart(item) {
 
	if (!document.getElementById) return false;
	if (!document.getElementById("order-container")) return false;
	
    var container = document.getElementById("order-container");

    var url = "/merchandise/ajax/removeCart.php?item="+item;
    
    displayLoading(container);
   
    // call removeCart.php
    xmlhttp.open("GET",url,true);
    
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200) {
                setTimeout("getCart()", 700);
                //getCart();
            }
        }
    }
    
    xmlhttp.send(null);
        
}


// Display loading graphic
// ***********************
function displayLoading(obj) {
    while (obj.hasChildNodes()) {
        obj.removeChild(obj.lastChild);
    }
    
    var loadDiv = document.createElement("div");
    loadDiv.setAttribute("id","loading");
    obj.appendChild(loadDiv);

    var image = document.createElement("img");
    image.setAttribute("src","/merchandise/img/loading.gif");
    image.setAttribute("alt","Loading...");
    loadDiv.appendChild(image);
    
    var para = document.createElement("p");
    var text = document.createTextNode("updating");
    para.appendChild(text);
    loadDiv.appendChild(para);
   
}


// Fade a box up - visual alert
// ****************************
function fadeup(elementId,red,green,blue) {

    if (red == 255 && green == 255 && blue == 255) {
        document.getElementById(elementId).style.backgroundColor = "#fff";
        return true;
    }

    document.getElementById(elementId).style.backgroundColor = "rgb("+red+","+green+","+blue+")";

    red = red + Math.ceil((255 - red)/10);
    green = green + Math.ceil((255-green)/10);
    blue = blue + Math.ceil((255-blue)/10);

    setTimeout("fadeup('"+elementId+"',"+red+","+green+","+blue+")", 20);

}
