/**
 * classified category cascadedSelect RPC
 *
 * this javascript takes the input from a source selection list
 * and fills the destination selection list with the data that 
 * is obtained from the remote procedure call url that is passed
 * into the function.
 *
 * @author randy young
 * @filesource
 * @version $Revision: $
 * @package jscript
 * @copyright Copyright 2008, Pastime Enterprises, LLC
 * @license http://www.PastimeEnterprises.com/license/PEengine
 */

/**
 * this function is called from the selection list event. It passes
 * in the source selection list object, the destination selection list
 * object and the url of the remote procedure to call. This method will
 * kick off the call to the server.
 */
function cascadedSelect(srcSel, dstSel, RPC_URL)
{
	var srcSelectedItem;
	var fixedRPC_URL;
	var data;
	
	// get the selected value
	srcSelectedItem = srcSel.options[srcSel.selectedIndex].value;
	
	if(srcSelectedItem != '0')
	{
		// set up the actual url to call
		fixedRPC_URL = RPC_URL + srcSelectedItem;
			
		// get rid of the contents of the destination selection box
		dstSel.options.length = 0;

		// make the Remote Procedure Call to the server
		serverRPC(fixedRPC_URL, null, "POST", dstSel);
	}
	
};

/**
 * this function is called from the selection list event. It passes
 * in the source selection list object, the destination selection list
 * object and the url of the remote procedure to call. This method will
 * kick off the call to the server.
 * onchange="cascadedSelect(this, document.forms[0].type, '/rpcalls/classifieds/getCategoryChildren/', document.forms[0].type, '/rpcalls/classifieds/getBuilderChoices/')"
 */
function cascadedSelect2(srcSel, dstSel, RPC_URL, dstSel2, RPC_URL2)
{
	var srcSelectedItem;
	var fixedRPC_URL;
	var fixedRPC_URL2;
	var data;
	
	
	// get the selected value
	srcSelectedItem = srcSel.options[srcSel.selectedIndex].value;
	
	if(srcSelectedItem != '0')
	{
		fixedRPC_URL = RPC_URL + srcSelectedItem + '/';

		// get rid of the contents of the destination selection box
		dstSel.options.length = 0;

		// make the Remote Procedure Call to the server
		serverRPC(fixedRPC_URL, null, "POST", dstSel);
		
		// do the second list now
		fixedRPC_URL2 = RPC_URL2 + srcSelectedItem + '/';

		// get rid of the contents of the destination selection box
		dstSel2.options.length = 0;

		// make the Remote Procedure Call to the server
		serverRPC(fixedRPC_URL2, null, "POST", dstSel2);
	}
	
};

/**
 * this function is called from the selection list event. It passes
 * in the source selection list object, the destination selection list
 * object and the url of the remote procedure to call. This method will
 * kick off the call to the server.
 */
function cascadedSelectPD(srcSel, dstSel, RPC_URL)
{
	var srcSelectedItem;
	var fixedRPC_URL;
	var data;
	
	// get the selected value
	srcSelectedItem = srcSel.options[srcSel.selectedIndex].text;
	
	if((srcSelectedItem == 'New Parts') || (srcSelectedItem == 'Used Parts'))
	{
		// set up the actual url to call
		fixedRPC_URL = RPC_URL + srcSelectedItem;
			
		// get rid of the contents of the destination selection box
		dstSel.options.length = 0;

		// make the Remote Procedure Call to the server
		serverRPC(fixedRPC_URL, null, "POST", dstSel);
		
		// make the label and selection list elements viewable
		document.getElementById('hiddenLabel').style.display = 'inline';
		document.getElementById('parts_department').style.display = 'inline';
		document.getElementById('parts_department_err').style.display = 'inline';
	}
	else
	{
		// make the label and selection list elements hidden
		document.getElementById('hiddenLabel').style.display = 'none';
		document.getElementById('parts_department').style.display = 'none';
		document.getElementById('parts_department_err').style.display = 'none';
	}
};

/**
 * this function is called from the selection list event. It passes
 * in the source selection list object, the destination selection list
 * object and the url of the remote procedure to call. This method will
 * kick off the call to the server.
 */
function cascadedSelectPDcheck(srcSel, dstSel, RPC_URL)
{
	var srcSelectedItem;
	var fixedRPC_URL;
	var data;
	
	// get the selected value
	srcSelectedItem = srcSel.options[srcSel.selectedIndex].text;
	
	if((srcSelectedItem == 'New Parts') || (srcSelectedItem == 'Used Parts'))
	{
		// set up the actual url to call
		fixedRPC_URL = RPC_URL + srcSelectedItem;
			
		// get rid of the contents of the destination selection box
		dstSel.options.length = 0;

		// make the Remote Procedure Call to the server
		serverRPC(fixedRPC_URL, null, "POST", dstSel);
		
		// make the label and selection list elements viewable
		document.getElementById('hiddenLabel').style.display = 'inline';
		document.getElementById('parts_department').style.display = 'inline';
	}
	else
	{
		// make the label and selection list elements hidden
		document.getElementById('hiddenLabel').style.display = 'none';
		document.getElementById('parts_department').style.display = 'none';
	}
};

/**
 * This function will call the given url and wait for a response.
 * When the response arrives, it will call the setDestinationSelectionListContent
 * function to process the returned XML data and set the destination list content.
 */
function serverRPC(url, params, method, destinationSelectionList)
{

  var requester;
  try
  {
    requester = new XMLHttpRequest();
  }
  catch (error)
  {
    try
    {
      requester = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (error)
    {
      requester = null;
    }
  }

  if (requester != null)
  {
    requester.onreadystatechange = function()
    {
      if (requester.readyState == 4)
      {
        if (requester.status == 200 || requester.status == 304)
        {
        	var doc = requester.responseText; //XML.documentElement;
        	//alert(doc);
        	var docArr = doc.split('\n');
        	for(var z=0; z< docArr.length; z++)
        	{
        		var optParts = docArr[z].split('|');
				if(optParts.length == 2)
				{
        			// new Option('text property', 'value property', 'defaultSelected property', 'selected property')
					destinationSelectionList.options[z] = new Option(optParts[0], optParts[1], false, false);
				}
        	}

//          	setDestinationSelectionListContent(doc, destinationSelectionList);
        }
        else
        {
          failure(requester);
        }
      }

      return true;
    };

    requester.open(method, url);
    requester.send(params);
  }
  else
  {
    return false;
  }

  return true;
}

/**
 * This method will process the XML data that is returned from the server
 * as a result of the RPC. It will insert that data into the destination 
 * selection list.
 */
function setDestinationSelectionListContent(doc, destinationSelectionList)
{
	var root = doc.documentElement;
	var nds;
	var j = 0;

	alert(doc);
	if(root.hasChildNodes())
	{
		nds = root.childNodes;
		for(var i = 0; i < nds.length; i+=2)
		{
			if(nds[i].hasChildNodes())
			{
				// put the values into an array
				// new Option('text property', 'value property', 'defaultSelected property', 'selected property')
				destinationSelectionList.options[j] = new Option(nds[i+1].firstChild.nodeValue, nds[i].firstChild.nodeValue, false, false);
				j++;
			}
		}
	}
	
	return true;
}

/**
 * this method will let the user know that something went wrong. Probably
 * need to rework this.
 */
function failure(requester)
{
  alert("The XMLHttpRequest failed with status code: " + requester.status);

  return true;
}

