/* ==========================================================
Tools Site Javascript

AJAX concession proce build/load / functionality

version:   0.1
author:    Sam Hocking
website:   http://www.parkertools.co.uk
============================================================ */


/* Retrieves concession pricing from ConcessionPriceBuild.php using AJAX
============================================================ */
// global request and XML document objects
var concXmlReq;
var menuItem; 
var concURL = "/ConcessionPriceBuild.php";
var menuGroup;
var noConcHTML = "<div style=\"text-align: center;\">Sorry, no concession prices found for this product.</div>";
var loadingConcHTML = "<p style=\"line-height:50px;\"><img style=\"vertical-align: middle; padding-right:30px;\" src=\"/media/images/general/loader_small.gif\" alt=\"\"/>Loading your concession prices, please wait...</p>\n";

// replaces DOM child node with new content
function replaceContents(oldElement,newElementName,newContent){
	var
	newElement=document.createElement(newElementName);
	newElement.innerHTML=newContent;
	oldElement.parentNode.replaceChild(newElement,oldElement);
}

// Show Classes
function showConcessionProducts(productNo, productDesc, numSizes, clientHeight){
	//document.getElementById("prodSizeContent").innerHTML = "NOTHING";
	concURL += "/" + productNo + "/" + productDesc + "/" + numSizes;
	document.getElementsByName("prices")[0].style.display='none';
	document.getElementById('concContainer').style.display='none';
    document.getElementById('addToBasket').style.display='none';
	if (document.getElementById) {
        if (document.getElementById("prodSizeContent")) {
        	
        	document.getElementById("lightBox").style.display = 'block';
        	//document.getElementById("lightBoxContent").style.top = clientHeight/2.
        	document.getElementById("lightBoxContent").innerHTML = loadingConcHTML;
        	loadConcessionProducts();
        }
    }
}

// Load Concessions
function loadConcessionProducts() {
    var meth = "GET";
    var str = null;
    
    try {
        // get the data and wait for response
        getConcXML(concURL, meth, str);
    } catch(e) {
        var msg = (typeof e == "string") ? e : ((e.message) ? e.message : "Unknown Error");
        alert("Unable to get XML data:\n" + msg);
        return;
    }
}

// Fetch the XML Request
function getConcXML(file, meth, str) {
    var doc = null;
    
    if (typeof window.ActiveXObject != 'undefined' ) {
        concXmlReq = new ActiveXObject("Microsoft.XMLHTTP");
        concXmlReq.onreadystatechange = displayConcState;
    } else {
        concXmlReq = new XMLHttpRequest();
        concXmlReq.onload = displayConcState;
    }
    document.body.style.cursor = "wait";
    concXmlReq.open( meth, file, true ); 
    concXmlReq.send(str);
}

// Handle onreadystatechange event of req object
function displayConcState() {
    // only if req shows "loaded"
    if (concXmlReq.readyState == 4) {
        // only if "OK"
        if (concXmlReq.status == 200) {
            if (document.getElementById("prodSizeContent")) {
                if (concXmlReq.responseText.length == 0) {
                	document.getElementById("prodSizeContent").innerHTML = noConcHTML;
                } else {
                	replaceContents(document.getElementById("prodSizeContent"), "div", concXmlReq.responseText)
                }
            }
        } else {
            alert("Sorry, there was a problem loading concession prices:\n" + concXmlReq.statusText);
        }
        document.getElementById("lightBoxContent").innerHTML = '';
    	document.getElementById("lightBox").style.display='none';
    	
        document.body.style.cursor = "default";
        document.getElementById('addToBasket').style.display='block';
    }
}