// These functions show live stock levels on your website and replace the Add to Cart button with an Out of Stock message, where relevant
// Place a copy of this file in your 'Site1' folder and add a reference to it in your main Actinic template for product pages
// Then add 'checkElements()' to your '<BODY "onload=...">' tag to the same product page templates to run the scripts when a page is loaded

function checkElements(){
	var products = "";
	// get all the elements in the web page with the tag <div>
	var mycheck = document.getElementsByTagName("div");
	// loop through all the <div> elements
		 for (i=0;i<mycheck.length; i++){
		 //check if the current <div> element has an 'id'
			if (mycheck[i].id){
			//if it has, take the first 4 characters
			var strId = mycheck[i].id.substr(0,3);
			//if the first 4 characters = "qty"
			if (strId == "qty"){
					var prodarray = mycheck[i].id.split("qty_");
					//throw away the first part of the id and you're left with the product reference
					var prodref = prodarray[1];
					//build up a comma-delimited string of all the product references on the page that we want to process
					products += prodref + ",";
				}	
			}
		}
	//call the function to retrieve the stock data
	xmlhttpPost(products);
	}	
	
//This function takes the products string as an argument and makes a background request to a web page to get the stock data
function xmlhttpPost(ref) {
    var xmlHttpReq = false;
    var self = this;
    // This line works for Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // This line is required for IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
	//set the webpage to be called **Replace MYURL in this line with your own domain**
	var qstr = 'http://www.seasaltcornwall.co.uk/cgi-bin/checkstock.pl?prodref=' + escape(ref) + '&time=' + new Date().getTime();
	//set up the request
    self.xmlHttpReq.open('GET', qstr, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	//check for a state change following the request
    self.xmlHttpReq.onreadystatechange = function() {
		//if the response has completed successfully
        if (self.xmlHttpReq.readyState == 4) {
			//call the function to update the webpage
			updateElem(self.xmlHttpReq.responseText);
			}	
        }
	//send the request
    self.xmlHttpReq.send(null);
}

// This function takes the response from the web request above as an argument and uses it to update the current webpage
	function updateElem(response){
	//create an array of the stock information returned from the web request
	var stock = response.split(",");
	var i=0;
	//step through the array, 3 at a time
	for (i=0;i<stock.length;i=i+3){
		var myId = "qty_" +  stock[i];
		//get the element with id="cart_[product reference]"
		var myElem = document.getElementById(myId);
		//just check it exists before carrying on
		if(myElem){	
			//if the product is out of stock....
			if(stock[i+1] == 1){
				myElem.childNodes[0].style.display="none";
				myElem.childNodes[1].style.display="block";
				}
			//else if it is in stock....
			else {
				myElem.childNodes[1].style.display="none";
				myElem.childNodes[0].style.display="block";
			}
			}
		}
	}
//That's it!! Please get in touch if you need any more help.
//Copyright 2009 Richard Morrow
