
//Global XMLHTTP Request object
var XmlHttpCategory;
var pageId;

//Creating and setting the instance of appropriate XMLHTTP Request object to a “XmlHttp” variable  
function CreateXmlHttpCategory()
{
	//Creating object of XMLHTTP in IE
	try
	{
		XmlHttpCategory = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttpCategory = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			  XmlHttpCategory = null;
		}
	}
	//Creating object of XMLHTTP in Mozilla and Safari 
	if(!XmlHttpCategory && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttpCategory = new XMLHttpRequest();
	}
}

function SplitString(varString, varChar)
{
      
        
}

//Gets called when Category combo box selection changes
function CategoryListOnChange(varId) 
{  
    pageId = varId;
    var catId;
    if (varId != null)
    {
        var vars = varId.split("_");
        for (var i=0;i<vars.length;i++) 
        {
           if (vars[i] == 'Category2')
           {
                catId = parentId + "Category2_ddlCategory";
                break;
           }
           else
                catId = parentId + "Category1_ddlCategory";
        }
    }
    else
        catId = parentId + "Category1_ddlCategory";  
    
    var categoryList = document.getElementById(catId);   
	//Getting the selected Category from Category combo box.
	var selectedCategory = categoryList.options[categoryList.selectedIndex].value;
	// URL to get states for a given Category
	
	var requestUrl = AjaxServerPageName + "?lookupType=Category&SelectedCategory=" + encodeURIComponent(selectedCategory);
	//alert(requestUrl);
	CreateXmlHttpCategory();
		
    // If browser supports XMLHTTPRequest object
    if(XmlHttpCategory)
    {
	    //Setting the event handler for the response
	    XmlHttpCategory.onreadystatechange = HandleResponseCategory;
	    //Initializes the request object with GET (METHOD of posting), 
	    //Request URL and sets the request as asynchronous.
	    XmlHttpCategory.open("GET", requestUrl,  true);
		
	    //Sends the request to server
	    XmlHttpCategory.send(null);		
    }
}


//Gets called when B2C Category combo box selection changes
function B2C_CategoryListOnChange(varId) 
{  
    pageId = varId;
    var catId;
    if (varId != null)
    {
        var vars = varId.split("_");
        for (var i=0;i<vars.length;i++) 
        {
           if (vars[i] == 'Category1')
           {
                catId = parentId + "Category1_ddlCategory";
                break;
           }
           else
                catId = parentId + "Category2_ddlCategory";
        }
    }
    else
    {
        catId = parentId + "Category2_ddlCategory";  
        pageId = "Category2";  
    }
    
    var categoryList = document.getElementById(catId);   
	//Getting the selected Category from Category combo box.
	var selectedCategory = categoryList.options[categoryList.selectedIndex].value;
	// URL to get states for a given Category
	
	var requestUrl = AjaxServerPageName + "?lookupType=B2CCategory&SelectedCategory=" + encodeURIComponent(selectedCategory);
	//alert(requestUrl);
	CreateXmlHttpCategory();
		
    // If browser supports XMLHTTPRequest object
    if(XmlHttpCategory)
    {
	    //Setting the event handler for the response
	    XmlHttpCategory.onreadystatechange = HandleResponseCategory;
	    //Initializes the request object with GET (METHOD of posting), 
	    //Request URL and sets the request as asynchronous.
	    XmlHttpCategory.open("GET", requestUrl,  true);
		
	    //Sends the request to server
	    XmlHttpCategory.send(null);		
    }
}


//Called when response comes back from server for Category
function HandleResponseCategory()
{
    // To make sure receiving response data from server is completed
	if(XmlHttpCategory.readyState == 4)
	{
	    // To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpCategory.status == 200)
		{	
		    ClearAndSetsubCategoryListItems(XmlHttpCategory.responseXML);
		}
		else
		{
			alert("There was a problem retrieving data from the server." );
		}
	}
}

//Clears the contents of state combo box and adds the states of currently selected Category
function ClearAndSetsubCategoryListItems(responseXML)
{
	//Clears the sub category combo box contents.
	var subCatId;
	if (pageId != null)
	{
        var vars = pageId.split("_");
        for (var i=0;i<vars.length;i++) 
        {
           if (vars[i] == 'Category2')
           {
                subCatId = parentId + "Category2_ddlSubCategory";
                break;
           }
           else
                subCatId = parentId + "Category1_ddlSubCategory";
        }
    }
    else
        subCatId = parentId + "Category1_ddlSubCategory";
    var subCategoryList = document.getElementById(subCatId);
    for (var count = subCategoryList.options.length-1; count >-1; count--)
	{
		subCategoryList.options[count] = null;
	}
	//End Clear Boxes
	var statePkID = responseXML.getElementsByTagName('catId');
	var stateNodes = responseXML.getElementsByTagName('Name');
	var textValue; 
	var PkID;
	var optionItem;
	//Add new states list to the state combo box.
	for (var count = 0; count < stateNodes.length; count++)
	{
	    PkID=GetInnerTextCategory(statePkID[count]);
	    textValue = GetInnerTextCategory(stateNodes[count]);
   		if(textValue==BlankSelection)
   		{
		optionItem = new Option(textValue, PkID,  true, true);
		}
		else
		{
		optionItem = new Option(textValue, PkID,  false, false);
		}
		subCategoryList.options[subCategoryList.length] = optionItem;
	}
}

//Returns the node text value 
function GetInnerTextCategory (node)
{
	 return (node.textContent || node.innerText || node.text || node.value || node.innerHTML || node.firstChild.nodeValue) ;
}




















