/**
 * @description [remember the advanced search form data]
 */

/* **********************************************************************************************************************
   advsEncodeCriteriaAndLimits
			In order to maintain the state of the form when people return to it via the back button, we encode the criteria and
			limits into two hidden fields.  A search for "AND Smith In Author" and "Age Group is Neonatal" would write these:
			Smith   0       1|       0       0
			0       0       1|       0       0
			Note that the numbers represent selected indexes.  If the selection allows multiple items to be chosen, the list will 
			be separated by commas.  Each line is separated by a vertical bar.  In the example above, the second criterion and the
			second limit are both blank.
			********************************************************************************************************************** */
function advsEncodeCriteriaAndLimits()
	{
		document.frmEncoding.criteria.value = advsEncodeGroup("advsLookForLines", "c", "o", "f", "v");
		document.frmEncoding.limits.value = advsEncodeGroup("advsLimitLines", "l", "lo", "l", "lv");
		
		document.frmEncoding.dateconstraints.value = advsEncodeDateOptions();
		document.frmEncoding.displayoptions.value = advsEncodeDisplayOptions();
		
		return true;
	}
	
	function advsEncodeDateOptions(){
		
		var input = { 
					  type:getRadioButtonValue('frmAdvancedSearch','pub'),
					  puba:document.frmAdvancedSearch.puba.value,
					  puby1:document.frmAdvancedSearch.puby1.value,
					  from:document.getElementById('advsPubF1').value, 
					  to:document.getElementById('advsPubF2').value };
	
		return JSON.stringify(input);

	}
	
	function advsEncodeDisplayOptions(){
		
		var input = { rpp:getRadioButtonValue('frmAdvancedSearch','rpp'),
					  sb:getRadioButtonValue('frmAdvancedSearch','sb') };
	
		return JSON.stringify(input);

	}
	
	function advsDecodeDateOptions(){

		if( document.frmEncoding.dateconstraints.value == '' )
			return true;
		
		var input = JSON.parse(document.frmEncoding.dateconstraints.value);
		
		document.frmAdvancedSearch.puba.value = input.puba;
		document.frmAdvancedSearch.puby1.value = input.puby1;
		document.getElementById('advsPubF1').value = input.from;
		document.getElementById('advsPubF2').value = input.to;

		selectRadioButton('frmAdvancedSearch','pub',input.type);

		return true;

	}
	
	function advsDecodeDisplayOptions(){
		
		if( document.frmEncoding.displayoptions.value == '' )
			return true;
			
		var input = JSON.parse(document.frmEncoding.displayoptions.value);

		selectRadioButton('frmAdvancedSearch','rpp',input.rpp);
		selectRadioButton('frmAdvancedSearch','sb',input.sb);

		return true;

	}
	
	function selectRadioButton(formName,groupName,value){
	
		var radioGrp = document['forms'][formName][groupName];
		for(i=0; i < radioGrp.length; i++){
		    if (radioGrp[i].value == value)
		        radioGrp[i].checked = true;
		}
	
	}
	
	function getRadioButtonValue(formName,groupName){
		var radioGrp = document['forms'][formName][groupName];
		for(i=0; i < radioGrp.length; i++){
		    if (radioGrp[i].checked == true)
		        //var radioValue = radioGrp[i].value;
		        return radioGrp[i].value;
		}
		
		return null;
	}
	
/* **********************************************************************************************************************
   advsEncodeGroup
   Returns a string that represents the state of a group of criteria or limits.
			See the commentary on advsEncodeCriteriaAndLimits for details about the format of the returned string.
			********************************************************************************************************************** */	
	function advsEncodeGroup(id, namePrefix, opSuffix, fieldSuffix, valueSuffix)
	 {
		var count = 0;
		var thisCriterion = "";
		var thisOperator = "";
		var thisField = "";
 	var thisLine = "";
		var thisGroup = "";
		var lstSelectedIndexes = "";
		for(i = 0; i < document.getElementById(id).childNodes.length; i++)
			{
			thisLine="";
			thisCriterion = document.getElementsByName(namePrefix+(i+1)+"."+valueSuffix)[0];
			thisOperator = document.getElementsByName(namePrefix+(i+1)+"."+opSuffix)[0];
			thisField = document.getElementsByName(namePrefix+(i+1)+"."+fieldSuffix)[0];
			if(thisCriterion && thisOperator && thisField)
				{
				// Okay, maybe I shouldn't have tried to group these into a single method...
				if(namePrefix == "c")
					thisLine = thisCriterion.value + "\t" + thisOperator.selectedIndex + "\t" + thisField.selectedIndex;
				else if(namePrefix == "l")
				 {
						// Could be more than one selection here.
						lstSelectedIndexes = "";
						for(k=thisCriterion.options.length - 1; k >= 0; k--)
						 {
								if(thisCriterion.options[k].selected==true)
								 lstSelectedIndexes += k + ",";
							}
					lstSelectedIndexes = lstSelectedIndexes.substring(0, lstSelectedIndexes.length-1); // Get rid of the last comma
				 thisLine = lstSelectedIndexes + "\t" + thisOperator.selectedIndex + "\t" + thisField.selectedIndex;
					}
				thisGroup += thisLine + "|";
				count++;
				}
			}	
			// Remove final delimiter
			thisGroup = thisGroup.substring(0, thisGroup.length-1);
			return thisGroup;
	}
	
	/* **********************************************************************************************************************
   advsDecodeCriteriaAndLimits
   Reads the encoded values stored in the hidden text boxes for criteria and limits, and applies them to the search form.
			********************************************************************************************************************** */
function advsDecodeCriteriaAndLimits()
	{
	 var prefix = "";
		var limitFieldElement = new Object();
	 var arrCriterionFields = new Array(1);
		var arrCriterionLines = document.frmEncoding.criteria.value.split("|");
	 var arrLimitFields = new Array(1);
		var arrLimitLines = document.frmEncoding.limits.value.split("|");		
		var lstSelectedIndexes = "";
		var arrSelectedIndexes = new Array(1);
		
		/* CRITERIA */		
		if(arrCriterionLines[0].length)
		{
		for(index = 0; index < arrCriterionLines.length; index++)
		 {
			arrCriterionFields = arrCriterionLines[index].split("\t");
			prefix = "c" + (index+1) + ".";
			if(document.getElementsByName(prefix + "v").length == 0) CriterionList.addRow();
   document.getElementsByName(prefix + "v")[0].value = arrCriterionFields[0];						
   document.getElementsByName(prefix + "o")[0].selectedIndex = arrCriterionFields[1];
   document.getElementsByName(prefix + "f")[0].selectedIndex = arrCriterionFields[2];			
			}
		}
		
		/* LIMITS */ 
		if(arrLimitLines[0].length)
		{
		for(index = 0; index < arrLimitLines.length; index++)
		 {
			arrLimitFields = arrLimitLines[index].split("\t");
			prefix = "l" + (index+1) + ".";
			limitFieldElement = document.getElementsByName(prefix + "l")[0];			
			if(limitFieldElement == null) 
			 {				
				LimitList.addRow();
				refreshFieldLists();
				limitFieldElement = document.getElementsByName(prefix + "l")[0];	
				}
   limitFieldElement.selectedIndex = arrLimitFields[2];	
			refreshLimitValues(prefix + "lv", limitFieldElement.value)	;						
   document.getElementsByName(prefix + "lo")[0].selectedIndex = arrLimitFields[1];
			lstSelectedIndexes = arrLimitFields[0];
			arrSelectedIndexes = lstSelectedIndexes.split(",");
			for(i = 0; i < arrSelectedIndexes.length; i++)
			 {
				if(arrSelectedIndexes[i]) document.getElementsByName(prefix + "lv")[0].options[arrSelectedIndexes[i]].selected = true;
			 }
			resizeLimit("l"+(index+1)+".lv");		
			}
		}
		
		return true;
	}	
