/* ==========================================================
Tools Site Javascript

This file deals with the AJAX left menu hierachy load / functionality

version:   0.1
author:    Sam Hocking
website:   http://www.parkertools.co.uk
============================================================ */


/* Builds Menu Classes hierachy using AJAX
============================================================ */
// global request and XML document objects
var xmlReq;
var menuItem; 
var menuURL;
var menuGroup;
var noProductsHTML = "No Products Available";
var loadingHTML = "<div class=\"loading\">Loading Items...</div>";

// Show Classes
function showClass(idnum, group){
    if (document.getElementById) {
        if (document.getElementById("spanClasses"+idnum)) {
            menuItem = document.getElementById("spanClasses"+idnum);
            menuURL = "/ClassSubMenu.php?group=" + group;
            
            if (document.getElementById("spanClasses"+idnum).style.display == "none") {
                document.getElementById("spanClasses"+idnum).style.display = "";
                
                if (document.getElementById("spanPlus"+idnum)) {
                	document.getElementById("spanPlus"+idnum).style.display = "none";
                }
                if (document.getElementById("spanMinus"+idnum)) {
                	document.getElementById("spanMinus"+idnum).style.display = "";
                }
                // fetch the remote page
                if (menuGroup != group) {
                    document.getElementById("spanClasses"+idnum).innerHTML = loadingHTML;
                    menuGroup = group;

                    loadClassMenu();
                }
            } else {
                document.getElementById("spanClasses"+idnum).style.display = "none";
                if (document.getElementById("spanPlus"+idnum)) { 
                	document.getElementById("spanPlus"+idnum).style.display = "";
                }
                if (document.getElementById("spanMinus"+idnum)) {
                	document.getElementById("spanMinus"+idnum).style.display = "none";
                }
            }
        }
    }
}

// Load Menu
function loadClassMenu() {
    var meth = "GET";
    var str = null;
    
    try {
        // post the data and wait for response
        getXML(menuURL, meth, str);
    } catch(e) {
        var msg = (typeof e == "string") ? e : ((e.message) ? e.message : "Unknown Error");
        //alert("Unable to get XML data:\n" + msg);
        // load the class names the traditional way if an error occurs
        window.location("/HierGroup/" + group);
        return;
    }
}

// Fetch the XML Request
function getXML(file, meth, str) {
    var doc = null;
    if (typeof window.ActiveXObject != 'undefined' ) {
        xmlReq = new ActiveXObject("Microsoft.XMLHTTP");
        xmlReq.onreadystatechange = displayState;
    } else {
        xmlReq = new XMLHttpRequest();
        xmlReq.onload = displayState;
    }
    document.body.style.cursor = "wait";
    xmlReq.open( meth, file, true ); 
    xmlReq.send(str);
}

// Handle onreadystatechange event of req object
function displayState() {
    // only if req shows "loaded"
    if (xmlReq.readyState == 4) {
        // only if "OK"
        if (xmlReq.status == 200) {
            if (menuItem) {
                if (xmlReq.responseText.length == 0) {
                    menuItem.innerHTML = noProductsHTML;
                } else {
                    menuItem.innerHTML = xmlReq.responseText;
                }
            }
        } else {
            //alert("There was a problem retrieving the XML data:\n" + xmlReq.statusText);
            window.location(menuURL);
        }
        document.body.style.cursor = "default";
    }
}