var sub_category_value = new Array();
var sub_category_image = new Array();
var sub_category_id = new Array();
var sub_category_total = '';
var no_pages ='';
// stores the reference to the XMLHttpRequest object
var xmlHttp = createXmlHttpRequestObject();

// retrieves the XMLHttpRequest object
function createXmlHttpRequestObject()
{
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // if running Internet Explorer
  if(window.ActiveXObject)
  {
    // assume IE6 or older
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP",
                                    "Microsoft.XMLHTTP");
		// try every id until one works
		for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) 
		{
		  try 
		  { 
			// try to create XMLHttpRequest object
			xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
		  } 
		  catch (e) {} // ignore potential error
		}
  }
  // if running Mozilla or other browsers
  else
  {
    try
    {
      xmlHttp = new XMLHttpRequest();
    }
    catch (e)
    {
      xmlHttp = false;
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else
    return xmlHttp;
}

function category_name(name)
{
  language_id='1';
  //alert(" list category - "+name+" , language - "+language_id);
  
 // document.getElementById('product_table').innerHTML = '';
    
    // only continue if xmlHttp isn't void
 if (xmlHttp)
 {
  // try to connect to the server
  try
  {

  //  var url = "language="+cat_lang+"&cat_number="+cat_num;
  //   var url = "language="+cat_lang;
   // alert(url); 
    // execute the quickstart.php page from the server
    xmlHttp.open("GET", "./xml/catalog_product_list_xml.php?category="+name+"&language="+language_id , true);
    document.getElementById('leftnavitems').innerHTML = '<img src="./images/waiting.gif">';
    // define the method to handle server responses
    xmlHttp.onreadystatechange =  handlecategories_list;
    // make the server request
    xmlHttp.send(null);
  }
  // display the error in case of failure
  catch (e)
  {
    alert("Can't connect to server:\n" + e.toString());
  }
 }
 
    
}
// function called when the state of the HTTP request changes
function handlecategories_list()
{
  //  alert("request");
  // when readyState is 4, we are ready to read the server response
  if (xmlHttp.readyState == 4)
  {
    // continue only if HTTP status is "OK"
    if (xmlHttp.status == 200)
    {
      try
      {
        // do something with the response from the server
        handlecategoriesResponse();
      }
      catch(e)
      {
        // display error message
        alert("Error reading the response: " + e.toString());
      }
    }
    else
    {
      // display status message
      alert("There was a problem retrieving the data:\n" +
            xmlHttp.statusText);
    }
  }
}

// handles the response received from the server
function handlecategoriesResponse()
{

  //alert("handle");

  // read the message from the server
  var xmlResponse = xmlHttp.responseXML;
  // catching potential errors with IE and Opera
  if (!xmlResponse || !xmlResponse.documentElement)
    throw("Invalid XML structure:\n" + xmlHttp.responseText);
  // catching potential errors with Firefox
  var rootNodeName = xmlResponse.documentElement.nodeName;
  if (rootNodeName == "parsererror")
    throw("Invalid XML structure:\n" + xmlHttp.responseText);
  // obtain the XML's document element
  xmlRoot = xmlResponse.documentElement;
  // obtain arrays with book titles and ISBNs
  category = xmlRoot.getElementsByTagName("Main-category")[0].firstChild.data;
//  alert("Category Name - "+category);
  language = xmlRoot.getElementsByTagName("language")[0].firstChild.data;
//  alert("Language Id - "+language);
  sub_category_total = xmlRoot.getElementsByTagName("sub-category-total")[0].firstChild.data;
 // alert("sub_category_total id - "+sub_category_total);

  var html = "";
  var myDiv='';
  if(sub_category_total == '0')
  {
	  html = html +"No Categories Present <br />";
      myDiv = document.getElementById("leftnavitems");
	  myDiv.innerHTML = html;
  }
  else
  {
	 for (var i=0; i<sub_category_total; i++)
	  {
		sub_category = xmlRoot.getElementsByTagName("sub-category-value");
		sub_category_value[i] = sub_category.item(i).firstChild.data;
		

		sub_categoryimg = xmlRoot.getElementsByTagName("sub-category-image");
		sub_category_image[i] = sub_categoryimg.item(i).firstChild.data;
	    //alert(sub_category_value[i]);

		sub_categoryid = xmlRoot.getElementsByTagName("sub-category-id");
		sub_category_id[i] = sub_categoryid.item(i).firstChild.data;
	    //alert(sub_category_id[i]);
	
	  }

	var html ='';
	var myDiv='';
	for(var j=0;j< sub_category_total;j++)
	{
		//html = html + "<p class=\"leftnavitems\"><a href=\"product.php?category="+sub_category_value[j]+"\" style=\"color: #7F7F7F; \" >"+sub_category_value[j]+"</a></p>";			
		html = html + "<p class=\"leftnavitems\"><a href=\"product.php?category="+sub_category_id[j]+"\"  >"+sub_category_value[j]+"</a></p>";			
		myDiv = document.getElementById("leftnavitems");
	}
	myDiv.innerHTML = html;
	  
  }
  
}

/*
function cat_list()
{
	//alert("category list");
	var html ='';
	for(var j=0;j< sub_category_total;j++)
	  {
		html = html +"<a href=\"Javascript:prod_list('"+sub_category_value[j]+"');\" name=\"'+sub_category_value[j]+'\"  >"+sub_category_value[j]+"</a><br />";			
		myDiv = document.getElementById("cat_list");
	  }
		myDiv.innerHTML = html;
}
*/