/**************************************************************************
*	  @Workfile: AsyncHandler.js
*  Description : Implements client side operations required by search page - asynchronous request management, location box behavior.
*		@Author: Tanmay Soni
*      @version: 1.0
*		  @Date: 08/10/2007.
**************************************************************************/

//Create XMLHTTP object for MS and Non-MS platforms.
var xHRObject = false;
if (window.XMLHttpRequest)
	{xHRObject = new XMLHttpRequest();}
else if (window.ActiveXObject)
	{xHRObject = new ActiveXObject("Microsoft.XMLHTTP");}
/**
 * @access  public
 * @return  void
 * @summary prepares GET URL for search panel asynchronous request and sends to the server. Used in load locations by state, load  sub categories by category selection.
 **/
function sendSearchPanelAsyncRequest(method, parameter)
{
	xHRObject.open("GET", "/ss/bubhub/inc/inc_side_new.php?mode=async&method="+method+"&parameter=" + parameter, true);
	xHRObject.onreadystatechange = fetchSearchPanelAsyncResponseSelect;
	xHRObject.send(null);
}
/**
 * @access  public
 * @return  void
 * @summary prepares GET URL for search result guided navigation panel asynchronous requests and sends to the server. Used in update search results by job type selection changes, location (below state) selection changes.
 **/
function sendResultUpdateAsyncRequest(method, parameter,value,checkStatus)
{
	xHRObject.open("GET", "/ss/bubhub/search.php?mode=async&method="+method+"&parameter=" + parameter+"&value="+value+"&checked="+checkStatus, true);
	xHRObject.onreadystatechange = fetchSearchPanelAsyncResponse;
	xHRObject.send(null);
}
/**
 * @access  public
 * @return  void
 * @summary prepares GET URL for search result guided navigation panel, days selection, asynchronous requests and sends to the server.
 **/
function sendResultUpdateAsyncRequestDays(method, value)
{
	xHRObject.open("GET", "/ss/bubhub/search.php?mode=async&method="+method+"&value="+value, true);
	xHRObject.onreadystatechange = fetchSearchPanelAsyncResponse;
	xHRObject.send(null);
}
/**
 * @access  public
 * @return  void
 * @summary prepares GET URL for search result guided navigation panel - state checkboxes check/uncheck event - asynchronous requests and sends to the server.
 **/
function sendResultUpdateAsyncRequestState(method,parameter,value,divLocationsID,checkStatus)
{

    handleLocationsExpandCollapse(divLocationsID,checkStatus);

    if (document.getElementById(divLocationsID) != null)
    {
        var divCheckboxes = document.getElementById(divLocationsID).childNodes;
        var chkLocations = "";

        for(var i = 0; i < divCheckboxes.length; i++)
            {
             if(divCheckboxes[i].type == "checkbox")
                {
                     chkLocations += divCheckboxes[i].value + ",";
                }
            }
	    chkLocations = chkLocations.slice(0, -1);
	    xHRObject.open("GET", "/ss/bubhub/search.php?mode=async&method="+method+"&state="+value+"&stateChecked="+checkStatus+"&locationsList="+chkLocations, true);
	    xHRObject.onreadystatechange = fetchSearchPanelAsyncResponse;
	    xHRObject.send(null);
	}
}

/**
 * @access  public
 * @return  void
 * @summary listens to the HTTP response for asynchronous requests sent; upon reception of response, emits the inner HTML into the control targeted for the response.
 **/function fetchSearchPanelAsyncResponseSelect()
{
if (xHRObject.readyState == 4 && xHRObject.status == 200)
		{
			var serverText = xHRObject.responseText;

			if(serverText.indexOf("%##%") > 0)
			{
				element = serverText.split("%##%");
				//alert(element[2]);
				document.getElementById(element[0]).innerHTML = element[2];

			}
		}
}
/**
 * @access  public
 * @return  void
 * @summary listens to the HTTP response for asynchronous requests sent; upon reception of response, emits the inner HTML into the control targeted for the response.
 **/function fetchSearchPanelAsyncResponse()
{
if (xHRObject.readyState == 4 && xHRObject.status == 200)
		{
			var serverText = xHRObject.responseText;
			if(serverText.indexOf("%##%") > 0)
			{
				element = serverText.split("%##%");
				document.getElementById(element[0]).innerHTML = element[1];
			}
		}
}
/**
 * @access  public
 * @return  void
 * @summary Invoked on state checkbox check/uncheck at guided navigation panel. Handles expand/collapse and check/uncheck of location checkboxes below a state checkbox in reference.
 **/
function handleLocationsExpandCollapse(divLocationsID,chkStateChecked)
{
    if (document.getElementById(divLocationsID) != null)
    {
        var divCheckboxes = document.getElementById(divLocationsID).childNodes;

        for(var i = 0; i < divCheckboxes.length; i++)
            {
             if(divCheckboxes[i].type == "checkbox")
             {
                 if (chkStateChecked)
                     divCheckboxes[i].checked = 'true';
                 else
                     divCheckboxes[i].checked = 'false';
             }
            }
        if (chkStateChecked)
        {
            document.getElementById(divLocationsID).style.visibility = 'visible';
            document.getElementById(divLocationsID).style.display='';
        }
        else
        {
            document.getElementById(divLocationsID).style.visibility = 'hidden';
            document.getElementById(divLocationsID).style.display='none';
        }
    }
}

function submitSearchForm()
{
	 document.frmSeacrh.action='/ss/bubhub/search.php?mode=search';
	 document.frmSeacrh.submit();
}