/******************************************************************************************/
/* -= Advanced Search -[+]- Recoded by Alex Gerdev, Guy Atia. (c)2006 =-                  */
/*----------------------------------------------------------------------------------------*/
/* Description:                                                                           */
/*----------------------------------------------------------------------------------------*/
/* This is a java script in Ajax technology that used for the auto change the combo boxes */
/* That defined in the ez35\design\general\templates\advanced_search.tpl                  */
/*                                                                                        */
/*----------------------------------------------------------------------------------------*/
/* Usage:									          */
/*----------------------------------------------------------------------------------------*/
/* onChange="return AuthorsOnChange()" - For the authors combo box                        */
/* onChange="return CategoryOnChange()" - For the Category combo box                      */
/******************************************************************************************/

// declare a global  XMLHTTP Request object
var XmlHttpObj;

var NotAvailableStr;

// Function that set the Not Available string
function SetNotAvailableStr(s)
{
	NotAvailableStr = s;
}

// create an instance of XMLHTTPRequest Object, varies with browser type, try for IE first then Mozilla
function CreateXmlHttpObj()
{
	// try creating for IE (note: we don't know the user's browser type here, just attempting IE first.)
	try
	{
		XmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttpObj = null;
		}
	}
	// if unable to create using IE specific code then try creating for Mozilla (FireFox) 
	if(!XmlHttpObj && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttpObj = new XMLHttpRequest();
	}
}

// This function is called from onChange event of the authors combo box list. It is updating the content of the category
// and sub category combo box lists.
function AuthorsOnChange(url_prefix) 
{
	// Get the authors combobox by ID
        var AuthorsList = document.getElementById("author");
    
        // get selected continent from dropdown list
        var selectedAuthor = AuthorsList.options[AuthorsList.selectedIndex].value;

        // url of page that will send xml data back to client browser
        var requestUrl = url_prefix+"/content/view/xml_node_fetch/2/(filter)/" + encodeURIComponent(selectedAuthor);

	CreateXmlHttpObj();
	
	// verify XmlHttpObj variable was successfully initialized
	if(XmlHttpObj)
	{
        	// assign the CategoryChangeHandler function ( defined below in this file)
	        // to be called when the state of the XmlHttpObj changes
        	// receiving data back from the server is one such change
		XmlHttpObj.onreadystatechange = CategoryChangeHandler;
		
		// define the iteraction with the server -- true for as asynchronous.
		XmlHttpObj.open("GET", requestUrl,  true);
		
		// send request to server, null arg  when using "GET"
		XmlHttpObj.send(null);		
	}
}


// this function called when state of  XmlHttpObj changes
// we are interested in the state that indicates data has been
// received from the server
function CategoryChangeHandler()
{
	// state ==4 indicates receiving response data from server is completed
	if(XmlHttpObj.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpObj.status == 200)
		{			
			PopulateCategoryList(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}

// populate the contents of the Category combo box list
function PopulateCategoryList(CategoryNode)
{
	// Get the category list by ID
	var CategoryList = document.getElementById("category");
    
	// clear the Category list 
	for (var count = CategoryList.options.length-1; count >-1; count--)
	{
		CategoryList.options[count] = null;
	}

	var idValue;
	var textValue; 
	var optionItem;
	var CategoryNodes = CategoryNode.getElementsByTagName('item');

	// populate the combobox list with data from the xml doc
	for (var count = 0; count < CategoryNodes.length; count++)
	{
   		textValue = GetInnerText(CategoryNodes[count]);
		idValue = CategoryNodes[count].getAttribute("value");
		optionItem = new Option( textValue, idValue,  false, false);
		CategoryList.options[CategoryList.length] = optionItem;
	}

	// Get the sub category list by ID
  	var SubCategoryList = document.getElementById("sub_category");

	// clear the SubCategory list 
	for (var count = SubCategoryList.options.length-1; count >-1; count--)
	{
		SubCategoryList.options[count] = null;
	}

	// Set only the Not avalibale value to the sub category combo box
	optionItem = new Option( NotAvailableStr, "0",  false, false);
	SubCategoryList.options[SubCategoryList.length] = optionItem;
}


// called from onChange or onClick event of the continent dropdown list
function CategoryOnChange(url_prefix)
{
	var CategoryList = document.getElementById("category");
    
        // get selected continent from dropdown list
	var selectedCategory = CategoryList.options[CategoryList.selectedIndex].value;
    
        // url of page that will send xml data back to client browser
        var requestUrl = url_prefix+"/content/view/xml_node_fetch/2/(filter)/" + encodeURIComponent(selectedCategory);

	CreateXmlHttpObj();
	
	// verify XmlHttpObj variable was successfully initialized
	if(XmlHttpObj)
	{
        // assign the CategoryChangeHandler function ( defined below in this file)
        // to be called when the state of the XmlHttpObj changes
        // receiving data back from the server is one such change
		XmlHttpObj.onreadystatechange = SubCategoryChangeHandler;
		
		// define the iteraction with the server -- true for as asynchronous.
		XmlHttpObj.open("GET", requestUrl,  true);
		
		// send request to server, null arg  when using "GET"
		XmlHttpObj.send(null);		

	}
}

// this function called when state of  XmlHttpObj changes
// we are interested in the state that indicates data has been
// received from the server
function SubCategoryChangeHandler()
{
	// state ==4 indicates receiving response data from server is completed
	if(XmlHttpObj.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpObj.status == 200)
		{			
			PopulateSubCategoryList(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}

// populate the contents of the Sub Category combo box list
function PopulateSubCategoryList(SubCategoryNode)
{
    var SubCategoryList = document.getElementById("sub_category");
	// clear the SubCategory list 

	for (var count = SubCategoryList.options.length-1; count >-1; count--)
	{
		SubCategoryList.options[count] = null;
	}

	var idValue;
	var textValue; 
	var optionItem;
	var SubCategoryNodes = SubCategoryNode.getElementsByTagName('item');

	// populate the dropdown list with data from the xml doc
	for (var count = 0; count < SubCategoryNodes.length; count++)
	{
   		textValue = GetInnerText(SubCategoryNodes[count]);
		idValue = SubCategoryNodes[count].getAttribute("value");
		optionItem = new Option( textValue, idValue,  false, false);
		SubCategoryList.options[SubCategoryList.length] = optionItem;
	}
}

// returns the node text value 
function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}



