/**
* Function : dump()
* Arguments: The data - array,hash(associative array),object
*    The level - OPTIONAL
* Returns  : The textual representation of the array.
* This function was inspired by the print_r function of PHP.
* This will accept some data as the argument and return a
* text that will be a more readable version of the
* array/hash/object that is given.

function dumpme(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}
*/

function selectSimilar(formName,selectName)
	{
		//add to 'toSelect'
		var intOptionCount = document[formName][selectName].options.length
		var i;
		var j=-1;
		var arrOptionList = new Array(intOptionCount);
		var arrSelectList = new Array(intOptionCount);
		
		for(i = 0; i < intOptionCount; i++) {
			arrOptionList[i] = document[formName][selectName].options[i].value;
			if(document[formName][selectName].options[i].selected == true)
			{
				j = j + 1;
				arrSelectList[j] = document[formName][selectName].options[i].value;
			}
		}
		
		var arrSelectedList = new Array(j + 1);
		
		for (i = 0; i <= j; i++) {
			arrSelectedList[i] = arrSelectList[i];
		}
		
		//alert('options'+dumpme(arrOptionList));
		//alert('selected'+dumpme(arrSelectedList));
		
		for(i = 0; i < arrOptionList.length; i++) {
			for (j = 0; j < arrSelectedList.length; j++) {
				if (arrSelectedList[j].indexOf( arrOptionList[i] + '.') == 0) {
					//alert(arrSelectedList[j]);
					// This checks parent not child now
					document[formName][selectName].options[i].selected = true;
				}
			}
		}
	}

function unselectFields(formName,selectName)
	{
		len_f = document[formName][selectName].options.length;
		for(idx=0; idx<len_f; idx++)
			document[formName][selectName].options[idx].selected = false;
	}

function deleteSpaces(formName,selectName)
	{
		var i;
		with(document[formName][selectName])
		{
			if(document[formName][selectName].options.length > 0)
			{
				for(i=0; i<document[formName][selectName].options.length; i++)
				if(document[formName][selectName].options[i].value == "Spaces")
					document[formName][selectName].options[i] = null;
			}		
		}
		var x;
		with(document[formName][selectName])
		{
			if(document[formName][selectName].options.length > 0)
			{
				for(x=0; x<document[formName][selectName].options.length; x++)
					if(document[formName][selectName].options[x].value == "Spaces")
						document[formName][selectName].options[x] = null;
			}		
		}
	}

function selectAll(formName,selectName)
	{
		var i;
		var size = document[formName][selectName].options.length;
		for(i = 0 ; i < size; i++)
			document[formName][selectName].options[i].selected = true;
	}

function addSelectedTaxonomiesToHidden(formName,selectName,hiddenName)
	{
		var size = document[formName][selectName].options.length;
		for(i = 0 ; i < size; i++) {
			if(document[formName][selectName].options[i].selected == true) {
				var aHeirarchy = document[formName][selectName].options[i].value.split('.');
				var newID = aHeirarchy[aHeirarchy.length - 1];
				var myRegExp = new RegExp('\,' + newID + '\,','g');
				if(document[formName][hiddenName].value.match(myRegExp) != true) {
					document[formName][hiddenName].value = document[formName][hiddenName].value + newID + ',';
				}
			}
		}
	}

function removeSelectedTaxonomiesFromHidden(formName,selectName,hiddenName)
	{
		var size = document[formName][selectName].options.length;
		//loop through the selected fields in the select box
		for(i = 0 ; i < size; i++) {
			if(document[formName][selectName].options[i].selected == true) {
				var aHeirarchy = document[formName][selectName].options[i].value.split('.');
				var newID = aHeirarchy[aHeirarchy.length - 1];
				var myRegExp = new RegExp('\,' + newID + '\,','g');
				document[formName][hiddenName].value = document[formName][hiddenName].value.replace(myRegExp,',');
			}
		}
	}

function moveOptions(formName,fromSelect,toSelect)
	{
		//add to 'toSelect'
		var len_f = document[formName][fromSelect].options.length;
		var text,value, i
		for(i = 0; i < len_f; i++) {
			if(document[formName][fromSelect].options[i].selected == true)
			{
				text = document[formName][fromSelect].options[i].text;
				value = document[formName][fromSelect].options[i].value;
				optNew = new Option(text, value);
				len_sf = document[formName][toSelect].options.length;
				document[formName][toSelect].options[len_sf]=optNew;
			}
		}

		// Remove from 'fromSelect'
		for(i = 0; i < len_f; i++) {
			if(document[formName][fromSelect].options[i].selected == true)
			{
				document[formName][fromSelect].options[i] = null;
				i--;
				len_f = document[formName][fromSelect].options.length;
			}
		}

		len_f = document[formName][toSelect].options.length;
		if	(len_f > 0) {
			document[formName][toSelect].options[0].selected = true;
		}
		
		sortSelect(formName,toSelect);

	}

function addSelected(formName,masterSelect,slaveSelect,hiddenName)
	{
		
		//alert('here');
		
		// If there are no selected fields, do nothing.
		idx = document[formName][masterSelect].selectedIndex;
		if(idx < 0 ) return;
		
		//delete spaces from 'toSelect'
		deleteSpaces(formName,slaveSelect);
		
		//This function selects similarly valued fields.  It is particularly useful for heirarchical select boxes.
		selectSimilar(formName,masterSelect)
		
		//prepend the hidden field with a comma
		if (document[formName][hiddenName].value.length == 0) {
			document[formName][hiddenName].value =  ','
		}
		else {
			document[formName][hiddenName].value =  ',' + document[formName][hiddenName].value + ',';
		}
		
		//add the selected Taxonomies to the hidden field
		addSelectedTaxonomiesToHidden(formName,masterSelect,hiddenName);
		
		//remove the initial comma from the hidden field
		newsize = document[formName][hiddenName].value.length;
		newvalue = document[formName][hiddenName].value;
		if (document[formName][hiddenName].value.length < 2) {
			document[formName][hiddenName].value = ''; 
		}
		else {
			document[formName][hiddenName].value = newvalue.substring(1,newsize - 1); 
		}
		
		//Move the master options to the slave select box.
		moveOptions(formName,masterSelect,slaveSelect);
	}

function removeSelected(formName,masterSelect,slaveSelect,hiddenName)
	{
		// If there are no selected fields, do nothing.
		idx = document[formName][slaveSelect].selectedIndex;
		if(idx < 0 ) return;
		
		//delete spaces from 'toSelect'
		deleteSpaces(formName,masterSelect);
		
		//This function selects similarly valued fields.  It is particularly useful for heirarchical select boxes.
		//selectSimilar(formName,slaveSelect)
		
		//prepend the hidden field with a comma
		if (document[formName][hiddenName].value.length == 0) {
			document[formName][hiddenName].value =  ','
		}
		else {
			document[formName][hiddenName].value =  ',' + document[formName][hiddenName].value + ',';
		}
		
		//add the selected Taxonomies to the hidden field
		removeSelectedTaxonomiesFromHidden(formName,slaveSelect,hiddenName);
		
		//remove the initial comma from the hidden field
		newsize = document[formName][hiddenName].value.length;
		newvalue = document[formName][hiddenName].value;
		if (document[formName][hiddenName].value.length < 2) {
			document[formName][hiddenName].value = ''; 
		}
		else {
			document[formName][hiddenName].value = newvalue.substring(1,newsize - 1); 
		}
		
		//Move the master options to the slave select box.
		moveOptions(formName,slaveSelect,masterSelect);
	}
	
function addAll(formName,masterSelect,slaveSelect,hiddenName)
	{
		selectAll(formName,masterSelect);
		addSelected(formName,masterSelect,slaveSelect,hiddenName);
	}

function removeAll(formName,masterSelect,slaveSelect,hiddenName)
	{
		selectAll(formName,slaveSelect);
		removeSelected(formName,masterSelect,slaveSelect,hiddenName);
	}

function sortSelect(formName,selectName)
{
	objSelect = document[formName][selectName];
	var intSelectLen = objSelect.options.length;
	var arrSortArray = new Array(intSelectLen);
	

	//Put the value of each option into an array
	for (i = 0; i < intSelectLen; i++) {
		arrSortArray[i] = objSelect.options[i].value;
	}

	//Sort the array by option
	arrSortArray.sort();
	
	for (i = 0; i < intSelectLen; i++)
		{
		var strCurrentValue = arrSortArray[i];
		for (j = 0; j < intSelectLen; j++)
			{
			if (strCurrentValue == objSelect.options[j].value)
				{
					val =  objSelect.options[i].value;
					txt =  objSelect.options[i].text;
					objSelect.options[i].value = objSelect.options[j].value;
					objSelect.options[i].text = objSelect.options[j].text;
					objSelect.options[j].value = val;
					objSelect.options[j].text = txt;
				}
			}
		}
}