﻿//Global Ajax Server Page Name
var AjaxServerPageName = window.location;
var requestUrl = AjaxServerPageName;

//Global XMLHTTP Request object
var XmlHttp;

//Global Variables
var gNodeId = '';
var gCategoryGUId = '';

//Creating and setting the instance of appropriate XMLHTTP Request object to a "XmlHttp" variable  
function CreateXmlHttp()
{
    //Creating object of XMLHTTP in IE
    try
    {
	    XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
	    try
	    {
		    XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	    } 
	    catch(oc)
	    {
		    XmlHttp = null;
	    }
    }
    //Creating object of XMLHTTP in Mozilla and Safari 
	if(!XmlHttp && typeof window.XMLHttpRequest != "undefined") 
	{
		XmlHttp = new XMLHttpRequest();
	}
}

function HideSubCategories(oMenuItem,pCategoryGUId)
{
    oMenuItem.parentNode.className = "iestatic";

//    oMenuItem.parentNode.style.backgroundColor = "#005bab";
//    var oUL = document.getElementById('ul_' + pCategoryGUId);
//    if (oUL != null && oUL != undefined)
//    {
//        //oUL.parentNode.style.backgroundColor = "#005bab";
//        oUL.style.display = "none";
//        return false;
//    }
}

function ShowSubCategories(oMenuItem,pCategoryGUId)
{
    AssignHoverCSSClass(oMenuItem.parentNode);
    gCategoryGUId = pCategoryGUId;
    //oMenuItem.parentNode.style.backgroundColor = "#fedf15";
    
    // If the ul for sub-categories is already populated, no need to precess this request
    var oUL = document.getElementById('ul_' + gCategoryGUId);
    if (oUL != null && oUL != undefined)
    {
        oUL.style.display = "";
        return false;
    }
    gNodeId = oMenuItem.id;
	CreateXmlHttp();
	
	if(XmlHttp)
	{
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = HandleResponse;
		
		//Initializes the request object with POST (METHOD of posting), 
		//Request URL and sets the request as asynchronous.
		XmlHttp.open("POST", requestUrl,  true);
		XmlHttp.setRequestHeader("Content-Type", "text/xml")
		
		var xml = '<reqXml><category_data category_guid="' + pCategoryGUId + '" /></reqXml>'
		XmlHttp.send(xml);		
	}	    
}

//Called when response comes back from server
function HandleResponse()
{
	// To make sure receiving response data from server is completed
	if(XmlHttp.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttp.status == 200)
		{	
		    var liNode = document.getElementById(gNodeId).parentNode;
		    liNode.innerHTML = liNode.innerHTML + XmlHttp.responseText;
        }
	}
}