//alert('V.008');
function AddhandleTree()
{

	IdentifiedAllUL();// Assign a id to All UL
	treeListener(); // Add Listner to parent UL/image
        AutoFilltext(); // Copy the typed text to othere Search textBox
	CheckBoxCheck();// Display All Checkbox Tree Div Visible
	Checkedsetting();
	ResultOption(); // Add Event Listner to Textbox and ComboBox
	RadioOptionListener();
	//ReCreateHistory();

        // Close links in bills and legislation
        var linkImg = document.getElementById("anonymous_element_10")
                              .up().down('img');
        toggle(linkImg);

}

function AddhandleTreeMyProfile()
{
	IdentifiedAllUL();// Assign a id to All UL
	treeListenerMyProfile();// Add Listner to parent UL/image
	CheckBoxCheck();// Display All Checkbox Tree Div Visible
	Checkedsetting();
}

function CheckAllRootOptions()
{
  var myInputArray = document.getElementsByTagName("input");
  for (var i=0; i<myInputArray.length; i++) {
    if (myInputArray[i].type == 'checkbox') {
      if (myInputArray[i].value.indexOf("/") == -1) {
        if (!myInputArray[i].checked)
        {
          checkBoxUnCheck(myInputArray[i]);
          myInputArray[i].checked = true;
          ListUl = myInputArray[i].next('ul');
          if(ListUl)
          {
            checkBoxDisable(myInputArray[i]);
          }
        }
      }
    }
  }
}


/**************RadioOptionListener Start*********************/
// This Function Add a Click listner to All Search Option Text and selectBox.
// by Ronty (20080804)
/**************RadioOptionListener Start *********************/
function RadioOptionListener()
{
	//searchOptions
	$$('div#advSearchLimits input[type="radio"]').each(function(FormElement)
		{
			//jslog(3,FormElement.id);
			FormElement.observe('click', ShowHideTreeDive);//
			FormElement.observe('click', addHistoryEvent);

		});
}
/**************RadioOptionListener Start *********************/

/**************ShowHideTreeDive Start*********************/
// This Function Get the Click Radio Button Id and pass that to
// OptionSeleted function .
// by Ronty (20080804)
/**************ShowHideTreeDive Start*********************/

function ShowHideTreeDive(e)
{
	 var elt = e.element();
	 OptionSeleted(elt.id)
}
/**************ShowHideTreeDive Start*********************/

/**************OptionSeleted Start*********************/
// This Function Check Which combobox (in) list need to display
// and  Call Show or hide Tree div function.
// by Ronty (20080804)
/**************OptionSeleted Start*********************/
function OptionSeleted(RadioId)
{
                ShowTreeDive();
		// This list Generate Search options in dropdown list
//                var allList = new Array();
//                allList[0] = ["ALL"];
//                if (RadioId=='parliament')
//                {
//                        //HideTreeDive();
//                        ShowTreeDive();
//                        populateList(1, allList);       // jason function
//                }
//                else if(RadioId=='all')
//                {
//                        //HideTreeDive();
//                        ShowTreeDive();
//                        populateList(1, allList);       // jason function
//                }
//                else if(RadioId=='settings')
//                {
//                        //HideTreeDive();
//                        ShowTreeDive();
//                        populateList(userList.length, userList);         // jason function
//                }
//                else if(RadioId=='new')
//                {
//                        ShowTreeDive();
//                        usingChecks();   // jason function
//                }
}
/**************OptionSeleted Start*********************/


/**************treeListener Start*********************/
// This Function Add a Click listner to All Search Option Text and selectBox.
// by Ronty (20080724)
/**************ResultOption Start *********************/
function ResultOption()
{
	//searchOptions
	$$('div#advSearchOptions input').each(function(FormElement)
		{
			//jslog(3,FormElement.id);
			FormElement.observe('focus', addHistoryEvent);
		});

	$$('div#advSearchOptions select').each(function(FormElement)
		{

			//jslog(3,FormElement.id);
			FormElement.observe('change', addHistoryEvent);
		});
}
/**************ResultOption Start *********************/

/**************treeListener Start*********************/
// This Function Add a Click listner to All CheckBox and +/- image.
// It Checked if you Click any parent CheckBox or +/- image then it
// unCheck All child checkbox then hide or show list.
// "handleTreeClick" Function Same as "handleTreeClickMyProfile" but This Save History.
// by Ronty (20080724)
/**************treeListener Start *********************/
function treeListener()
{
	$('categoryTreeList1a' ).observe('click', handleTreeClick);
	$('categoryTreeList1b' ).observe('click', handleTreeClick);
	$('categoryTreeList2a' ).observe('click', handleTreeClick);
	$('categoryTreeList2b' ).observe('click', handleTreeClick);
	$('categoryTreeList3a' ).observe('click', handleTreeClick);
	$('categoryTreeList3b' ).observe('click', handleTreeClick);
	$('categoryTreeList4a' ).observe('click', handleTreeClick);
	$('categoryTreeList4b' ).observe('click', handleTreeClick);
}
/**************treeListener Start *********************/

/**************treeListenerMyProfile Start*********************/
// This Function Add a Click listner to All CheckBox and +/- image.
// It Checked if you Click any parent CheckBox or +/- image then it
// unCheck All child checkbox then hide or show list.
// "handleTreeClickMyProfile" Function Same as "handleTreeClick" but This dosn't Save History.
// by Ronty (20080724)
/**************treeListenerMyProfile Start *********************/

function treeListenerMyProfile()
{
	$('categoryTreeList1a' ).observe('click', handleTreeClickMyProfile);
	$('categoryTreeList1b' ).observe('click', handleTreeClickMyProfile);
	$('categoryTreeList2a' ).observe('click', handleTreeClickMyProfile);
	$('categoryTreeList2b' ).observe('click', handleTreeClickMyProfile);
	$('categoryTreeList3a' ).observe('click', handleTreeClickMyProfile);
	$('categoryTreeList3b' ).observe('click', handleTreeClickMyProfile);
	$('categoryTreeList4a' ).observe('click', handleTreeClickMyProfile);
	$('categoryTreeList4b' ).observe('click', handleTreeClickMyProfile);
}
/**************treeListenerMyProfile Start *********************/


/**************CheckBoxCheck Start*********************/
// This Function Display All Checkbox Tree Div Visible.
// by Ronty (20080724)
/**************CheckBoxCheck Start *********************/
function CheckBoxCheck()
{
		$$('div.mainBoxDiv input:checked').each(function(Searchdiv)
		{
			 while (Searchdiv = Searchdiv.up('ul'))
			 {
				if (!Searchdiv.visible())
	 			{
					ShowList(Searchdiv);// Show Ul div

				}
			}
		});
}
/**************CheckBoxCheck Start *********************/


/**************ShowList Start*********************/
// This Function Make a Ul list Visible and Also Change the +/- Image .
// by Ronty (20080724)
/**************ShowList Start *********************/
function ShowList(elementUL)
{
	
	var ImageID = $(elementUL.previous('img'));
	if(ImageID!='')
	{
		elementUL.show();
		ImageID.src = '/images/group_open.gif';
		ImageID.alt = 'Close checkbox group';
		ImageID.title = 'Close checkbox group';
	}

}
/**************ShowList Start *********************/


/**************HideList Start*********************/
// This Function Make a Ul list Hide and Also Change the +/- Image .
// by Ronty (20080724)
/**************HideList Start *********************/

function HideList(elementUL)
{
	var ImageID = $(elementUL.previous('img'));

	if(ImageID!='')
	{
		elementUL.hide();
		ImageID.src = '/images/group_closed.gif';
		ImageID.alt = 'Open checkbox group';
		ImageID.title = 'Open checkbox group'
	}

}
/**************HideList Start *********************/


/**************checkBoxListShowHide Start*********************/
// This Function Check is the CheckBox Checked or not.
// If Checked then the CheckBoxit Hide next child UL list.
// If UnChecked then the CheckBox it Show next child UL list.
// by Ronty (20080724)
/**************checkBoxListShowHide Start *********************/
function checkBoxListShowHide(CheckBox)
{
	
	 if (CheckBox.checked)
	  {
	
			checkBoxUnCheck(CheckBox);// to Uncheck All CheckBox;
			ListUl = CheckBox.next('ul');
				if(ListUl)
				{
					
					//checkBox(CheckBox);
					checkBoxDisable(CheckBox);
					//HideList(ListUl);// to Hide Ul list
				}
				
	  }
	  else
	  {
	  		ListUl = CheckBox.next('ul');	
			 	if(ListUl)
				{
					checkBoxEnalbe(CheckBox);
					//checkBox(CheckBox);
					//checkBox(ListUl);
					//ShowList(ListUl);// to Show Ul list
				}
	  }
          CheckBox.focus();
}
/**************checkBoxListShowHide Start *********************/
function Checkedsetting()
{
	$$('div.mainBoxDiv input:checked').each(function(CheckBox) 
		{
			if(CheckBox.type=="checkbox")
			{	
				checkBoxDisable(CheckBox);
			}
		});
	
	
	}

/**************handleTreeClickMyProfile Start*********************/
// This Function Check the Click event on CheckBox or +/- image.
// If you click on '-' then it Hide next child UL list.
// If you click on '+' then it Show next child UL list.
// And
// If Checked the CheckBox then it Hide next child UL list.
// If UnChecked the CheckBox then it Show next child UL list.
// by Ronty (20080724)
// "handleTreeClickMyProfile" Function Same as "handleTreeClick" without History function.
/**************handleTreeClickMyProfile Start *********************/
function handleTreeClickMyProfile(e) {
 
  var elt = e.element();
  var eltStr = elt.inspect();
  if (elt.tagName == 'IMG') {
  	//elt.identify()
	toggle(elt);// on image click,show / hide child ul list
	addHistoryEvent();
  	e.stop();
        changeListProfile();
  }
  if (elt.tagName == 'INPUT') {
		checkBoxListShowHide(elt); // on Checkbox ,show / hide child ul list
		
  }
} // handleTreeClick
/**************handleTreeClickMyProfile Start *********************/

/**************ReCreateHistoryStart*********************/
// On load this Function Check All Checked box and Add them in history 
// by Ronty (20080731)
/**************ReCreateHistoryStart *********************/
function ReCreateHistory()
{
	$$('div.mainBoxDiv input:checked').each(function(CheckBox) 
		{
			
			if(CheckBox.type=="checkbox")
			{
				 addHistoryEvent(); // On Every Checkbox or Image Add History Event
	
				//checkBoxDisable(CheckBox);
			}
		});

}
/**************ReCreateHistoryStart *********************/

/**************handleTreeClick Start*********************/
// This Function Check the Click event on CheckBox or +/- image.
// If you click on '-' then it Hide next child UL list.
// If you click on '+' then it Show next child UL list.
// And
// If Checked the CheckBox then it Hide next child UL list.
// If UnChecked the CheckBox then it Show next child UL list.
// by Ronty (20080724)
// "handleTreeClick" Function Same as "handleTreeClickMyProfile" With History function.
/**************handleTreeClick Start *********************/
function handleTreeClick(e) {
 
  var elt = e.element();
  var eltStr = elt.inspect();
  if (elt.tagName == 'IMG') {
  	toggle(elt);// on image click,show / hide child ul list
	e.stop();
        changeList();
  }
  if (elt.tagName == 'INPUT') {
		checkBoxListShowHide(elt);// on Checkbox ,show / hide child ul list
  }
   
   	if(elt.type=="checkbox")
			{	
   			
			addHistoryEvent(); // On Every Checkbox or Image Add History Event
			
			//changeRadio(this);
			}
   
} // handleTreeClick
/**************handleTreeClick Start *********************/


/**************toggle Start*********************/
// This Function Check the Click event on  +/- image.
// If you click on '-' then it Hide next child UL list.
// If you click on '+' then it Show next child UL list.
// by Ronty (20080724)
/**************toggle Start *********************/

function toggle(elt) { 
	

	var group = $(elt.up().down('ul'));
	var toggler = elt;
	var groupIsVisible = group.toggle().visible();
	toggler.src = '/images/group_' + (groupIsVisible ? 'open' : 'closed') +	'.gif';
	toggler.alt = (groupIsVisible ? 'Close checkbox group' : 'Open checkbox group');
	toggler.title = (groupIsVisible ? 'Close checkbox group' : 'Open checkbox group');
	
	//var Checkbox=elt.up().down('input[type="checkbox"]');
	//checkBoxUnCheck(Checkbox);// for Uncheck chield CheckBox
	//jslog(3,Checkbox.inspect());
	
}
/**************toggle End *********************/


/**************checkBoxUnCheck Start*********************/
// This Function UnCheck all child UL Checkbox List.
// If Checked the parent CheckBox then it unCheck All child UL Checkbox list.
// by Ronty (20080724)
/**************checkBoxUnCheck Start *********************/

function checkBoxUnCheck(elt) {
	var checkbox = elt;
	var parent = $(elt.up());
	parent.descendants().each(function(descendant) {
	if (descendant != checkbox) {
			if (descendant.tagName == 'INPUT' && descendant.type == "checkbox") {
				//if ($F(checkbox)) {
					descendant.setValue(false);
				//} 
		}
		}
	});
}
/**************checkBoxUnCheck Start *********************/

/**************checkBox Start*********************/
// This Function enalbe/disable  all child UL Checkbox List.
// If Checked the parent CheckBox then it disable All child UL Checkbox list.
// If Unchecked the parent CheckBox then it Enable All child UL Checkbox list.
// by Ronty (20080724)
/**************checkBox Start *********************/
function checkBox(elt) {
	
	var checkbox = elt;
	var parent = $(elt.up());
	parent.descendants().each(function(descendant) {
	if (descendant != checkbox) {
			if (descendant.tagName == 'INPUT' && descendant.type == "checkbox") {
				if ($F(checkbox)) {
					Field.disable(descendant);
					descendant.setValue(false);
				} else {
					Field.enable(descendant);
				}
			}
		}
	});
}
/**************checkBox Start *********************/


/**************checkBoxDisable Start*********************/
// This Function enalbe/disable  all child UL Checkbox List.
// If Checked the parent CheckBox then it disable All child UL Checkbox list.
// If Unchecked the parent CheckBox then it Enable All child UL Checkbox list.
// by Ronty (20080724)
/**************checkBoxDisable Start *********************/
function checkBoxDisable(elt) {
	
	var checkbox = elt;
	var parent = $(elt.up());
	parent.descendants().each(function(descendant) {
	if (descendant != checkbox) {
			if (descendant.tagName == 'INPUT' && descendant.type == "checkbox") {
				//if ($F(checkbox)) {
					
					Field.disable(descendant);
					descendant.setValue(false);
				//}
				
				//else {
				//	Field.enable(descendant);
				//}
			}
		}
	});
}
/**************checkBoxDisable Start *********************/


/**************checkBoxEnalbe Start*********************/
// This Function enalbe/disable  all child UL Checkbox List.
// If Checked the parent CheckBox then it disable All child UL Checkbox list.
// If Unchecked the parent CheckBox then it Enable All child UL Checkbox list.
// by Ronty (20080724)
/**************checkBoxEnalbe Start *********************/
function checkBoxEnalbe(elt) {
	var checkbox = elt;
	var parent = $(elt.up());
	parent.descendants().each(function(descendant) {
	if (descendant != checkbox) {
			if (descendant.tagName == 'INPUT' && descendant.type == "checkbox") {
				//if ($F(!(checkbox))) {
					
					//Field.disable(descendant);
					//descendant.setValue(false);
				//} 
				//else {
						//jslog(1,'cc');
					Field.enable(descendant);
				//}
			}
		}
	});
}


/**************checkBoxEnalbe Start*********************/
// This Function Check ID For All UL inside mainboxDiv .
// If No ID define then it Create a Id for that UL.
// by Ronty (20080724)
/**************IdentifiedAllUL Start *********************/
function IdentifiedAllUL()
{
	$$('div.mainBoxDiv ul').each(function(UL)
		{
			
			UL.identify();
			//jslog(4,''+UL.id);
		});
}
/**************IdentifiedAllUL End *********************/

/**************DisplayAllDiv Start*********************/
// This Function Display All Ul and Lis list.
// by Ronty (20080724)
/**************DisplayAllDiv Start *********************/
function  DisplayAllDiv()
{
	$$('div.mainBoxDiv Ul').each(function(Searchdiv) 
		{
		if(!Searchdiv.visible())
			{
				ShowList(Searchdiv); // Show Ul div
			}				 
		});
}
/**************DisplayAllDiv End *********************/

/**************UnCheckAll Start*********************/
// This Function Unchecked All Checkbox inside mainboxDiv .
// by Ronty (20080724)
/**************UnCheckAll Start *********************/
function  UnCheckAll()
{
	$$('div.mainBoxDiv input[type="checkbox"]').each(function(Searchdiv) 
		{
			Searchdiv.checked = false;
		});
}
/**************EnableAllCheckBox End *********************/

/**************UnCheckAll Start*********************/
// This Function Enalbe All Checkbox inside mainboxDiv .
// by Ronty (20080804)
/**************EnableAllCheckBox Start *********************/
function  EnableAllCheckBox()
{
	 $$('div.mainBoxDiv input[type="checkbox"]').each(function(Searchdiv) 
		{
				Field.enable(Searchdiv);
			//checkBoxEnalbe(Searchdiv.id)
		});
}
/**************EnableAllCheckBox End *********************/



/**************HideTreeDive Start*********************/
// This function hide full Tree div
// by Ronty (20080804)
/**************HideTreeDive Start *********************/
function HideTreeDive()
{
$$('div.subBoxDivided').each(function(RadioElement) 
	{
		RadioElement.hide();
	});
	
	$('DeSelectAll').hide();// Hide Deselected All//
	$('browseimage').hide();// Hide Deselected All//
	
}

/**************HideTreeDive End *********************/

/**************ShowTreeDive Start*********************/
// This function Show full Tree div
// by Ronty (20080804)
/**************ShowTreeDive Start *********************/

function ShowTreeDive()
{
$$('div.subBoxDivided').each(function(RadioElement) 
	{
		RadioElement.show();
	});
	$('DeSelectAll').show();// Hide Deselected All//
	$('browseimage').show();// Hide Deselected All//
}
/**************ShowTreeDive End *********************/




function ResetAllCheckBox()
{
	UnCheckAll();
	EnableAllCheckBox();
	addHistoryEvent();// Change History

        // clear field lists.
        document.getElementById('queryForm').fieldname1.options.length = 0;
        document.getElementById('queryForm').fieldname2.options.length = 0;
        document.getElementById('queryForm').fieldname3.options.length = 0;

        var allList = new Array();
        allList[0] = ["ALL"];
        populateList(1, allList); 
}


/*************************************************************** History  Function List Start***************************************************************************************************/
var rawIndex = 0;
//***************************************************/
function historyChange(newLocation, historyData) {
	var historyMsg = (typeof historyData == "object" && historyData != null
		? historyStorage.toJSON(historyData)
		: historyData
	);
	//var msg = "<b>A history change has occured:</b> | newLocation=" + newLocation + " | historyData=" + historyMsg + " |";
	//log(msg);
	 IdentifiedAllUL();// Assign a id to All UL
	 DisplayAllDiv(); //Display All Ul and Lis list.
	 UnCheckAll();  //Uncheck All Checkbox.
	
	if (historyData == null) {

	}
	else if (typeof historyData == "object" && historyData != null) 
	{

		// RadioButton and Create New Dropdown List
		
		var results = $H(historyData["RadioOption"]);
		results.each(function(pair) {
			$$('div#advSearchLimits input[type="radio"]#'+pair[0]).each(function(RadioElement) 
				{
					RadioElement.checked = true; // Check the Radio Button
					
					OptionSeleted(pair[0])
	
				
				});
		});	
		
		

		// Check Box
		var results = $H(historyData["CheckBox"]);
		//jslog(4,'his:'+dump(results ));
		results.each(function(pair) {
			$$('div.mainBoxDiv input#'+pair[0]).each(function(CheckBox) 
				{
					
					CheckBox.checked = true;
					setDropDowns(CheckBox);
					checkBoxDisable(CheckBox);
					ListUl = CheckBox.next('ul');	
					
					//else if(RadioId=='parliamentSelected')
						//{
						//	 ShowTreeDive();
							
						//}
					
				
				});
		});	
		// ul
		var results = $H(historyData["Element"]);
		results.each(function(pair) {
			$$('div.mainBoxDiv ul#'+pair[0]).each(function(Searchdiv)
				{
					if(pair[1]==false)
					{
						//jslog(3,'==: '+pair[1]+" "+Searchdiv);
						HideList(Searchdiv);	// to Hide Ul list		 
						
					}
			});
		});
		
		
		
		
		
		// for Search Option
		// Text Box
		var results = $H(historyData["OptionInput"]);
		results.each(function(pair) {
			$$('div#advSearchOptions input').each(function(Searchdiv)
				{
					if(pair[0]==Searchdiv.id)
					{
						Searchdiv.value=pair[1];
					}
			});
		});
		// Select Box
		var results = $H(historyData["OptionSelect"]);
		results.each(function(pair) {
			$$('div#advSearchOptions Select').each(function(Searchdiv)
				{
					
					 
					if(pair[0]==Searchdiv.id)
					{
						//jslog(2,Searchdiv.id+''+pair[1]);
						//usingChecks();	 // jason function 
						Searchdiv.value=pair[1];
						
					}
			});
		});
		
		//HideTreeDive();// Onload Hide Tree view

		
		//-----------End for Search Option
		
	}

};


/* Our event handler to add history change events */
addHistoryEvent = function() {
	
	var a_formValue = {};
	a_formValue['Element'] = {}; // array to store div status(Hide=True and ID)
	a_formValue['CheckBox'] = {}; // array to store CheckBox Status (Checked=True and ID)
	a_formValue['OptionInput'] = {}; // array to store TextBox Value (ID & Value)
	a_formValue['OptionSelect'] = {}; // array to store seleceted Value (ID & Value)
	a_formValue['RadioOption'] = {}; // array to store Radio Selected  (Checked=True and ID)
	

	$$('div.mainBoxDiv ul').each(function(formElement)  // START div#content input, div#content select'
		{ // for each diveId start
			if (!formElement.visible())
			{
				ID=formElement.identify();
				a_formValue['Element'][ID]= formElement.visible();
			}                                                  
		}// for each diveId start
	);// END div#content input, div#content select'
	
	
		$$('div.mainBoxDiv input:checked').each(function(Searchdiv) 
		{	
			if(Searchdiv.type=="checkbox")
			{
			//jslog(2,"sdf "+Searchdiv.type);	
			a_formValue['CheckBox'][Searchdiv.identify()]= Searchdiv.checked;
			}
		});
		
		// Search Option Textbox
	$$('div#advSearchOptions input').each(function(FormElement) 
		{
			if(FormElement.value!='')
			{
				a_formValue['OptionInput'][FormElement.identify()]= FormElement.value;		
			}
		});
	// Search Option Select
	$$('div#advSearchOptions select').each(function(FormElement) 
		{
			
			if(FormElement.value!='')
			{
				a_formValue['OptionSelect'][FormElement.identify()]= FormElement.value;		
			}
		});
	// Search RadioButton
	$$('div#advSearchLimits input[type="radio"]').each(function(FormElement) 
		{
			if(FormElement.checked==true)
			{
				a_formValue['RadioOption'][FormElement.identify()]= true;		
			}
		});
	
	

	var stepIndex = $('hdnStep').getValue();
	stepIndex++;
	var key = 'dps';
	//var key = 'dps'+stepIndex;
	$('hdnStep').value = stepIndex;
	//var txtsearch = $('Other').getValue();
	dhtmlHistory.add(key,a_formValue);
	if (typeof a_formValue == "object") {/*stringify this so we can log it better*/
		a_formValue = historyStorage.toJSON(a_formValue);
	}
	//var msg = "<b>A history event has been added:</b> [newLocation=" +key + " | historyData=" +a_formValue + "]";
	rawIndex++;
	
}
/*************************************************************** History  Function List End***************************************************************************************************/


/*================log start=========================**/
// This functin use for check history
/*basic DHTML logging for our test page*/
/*function log(msg) {
	var logNode = document.getElementById("logWin");
	var content = "<p>" + msg + "</p>" + logNode.innerHTML;
	logNode.innerHTML = content;
}
function clearLog(msg) {
	var logNode = document.getElementById("logWin");
	logNode.innerHTML = "";
}*/

/*=================log end===========================*/


