/** 

-- permSet --
permList = the list of permissions
theForm = current Form

This functions updates the text and value of the option whose properties are being changed


*/

	function permSet(permList,form){

		var rightsListArray = form.rightsList.value.split(",");
		var checkTotal = 0;

		//get checkBox values
		var valueString = "+";
		var textString = " (";

		//loop through rights list
		for(i = 0; i < rightsListArray.length; i++) {

			var currentInput = rightsListArray[i].split(":");
			var currentValue = (currentInput[1]+":"+currentInput[2]);
			var currentText = (currentInput[1]);
			var currentInput = (currentInput[0] + "Rights");
			
			//Read
			if(form[currentInput].checked){
				
				if((checkTotal != 0)&&(checkTotal < rightsListArray.length)){
					textString = textString + ",";
				}

				valueString = valueString + currentValue;
				textString = textString + currentText;				

				checkTotal=checkTotal+1;

			}

			if(i<(rightsListArray.length-1)){
				valueString = valueString + "+";
			}
	
		}

		textString = textString + ")";
		
		if(textString == " ()"){
			textString = "";
		}

		//loop through list and change list text as well as values
		if ((permList.selectedIndex != -1) && (permList.options[permList.selectedIndex].value != "")) { 

			var previousTitle = permList.options[permList.selectedIndex].text.split(" (");
			var previousValue = permList.options[permList.selectedIndex].value.split("+");

		 	permList.options[permList.selectedIndex].text = previousTitle[0] + textString;
		 	permList.options[permList.selectedIndex].value = previousValue[0] + valueString;

			permList.options[permList.selectedIndex].selected = 1;

		}


	}


/** 

-- permCheck

permList = the list of permissions
form = form 

This function disables the checkboxes if we select more than 1 user in the permissions list

*/

	function permCheck(permList,form){

		var rightsListArray = form.rightsList.value.split(",");
		var permCounter = 0;

		// loop and check number of fields selected
	  	for(i = 0; i < permList.options.length; i++) {
		    	if (permList.options[i].selected && permList.options[i].value != "") {
				permCounter = permCounter + 1;
			}
		}

		// loop through and check required fields
		for(i = 0; i < rightsListArray.length; i++) {

			var currentInput = rightsListArray[i].split(":");
			var currentInput = (currentInput[0] + "Rights");

			if (permCounter > 1){

				// uncheck
				form[currentInput].checked = 0;	

				//disable
				form[currentInput].disabled = 1;

			} else {

				// enable
				form[currentInput].disabled = 0;

				//alert(permList.options[permList.selectedIndex].value);

				// check and show permissions
				if (permList.options[permList.selectedIndex].selected && permList.options[permList.selectedIndex].value != "") { 

					var permArray = permList.options[permList.selectedIndex].value.split("+");				

					//alert(permArray[i+1]);						

					if(permArray[i+1]){
						form[currentInput].checked = 1;			
					} else {
						form[currentInput].checked = 0;
					}			
				}		
			}
		}
	}

/** 

-- clearPerms

permList = the list of permissions
form = form 

clear perms when not selected

*/

	function clearPerms(form){


		var rightsListArray = form.rightsList.value.split(",");


		// loop through and uncheck fields
		for(i = 0; i < rightsListArray.length; i++) {

			var currentInput = rightsListArray[i].split(":");
			var currentInput = (currentInput[0] + "Rights");

			// uncheck
			form[currentInput].checked = 0;	
	
		}

	}
