<!--
// All functions required for checkbox processing

var messagesSelected = 0;


// Loops though an array of checkbox named 'toDelete' to determine if they were checked
// and if they were, sets a flag and prompts the user to verify that they want to delete
// the items

//The second argument defaults to 'toDelete', but is optional to specify the checkbox name to parse
//The third argument defaults to 'item', but is optional to specify the type of item being listed (ie. jobs, contacts, etc.)
//The forth argument defaults to 'delete', but is optional to specify the action of the form for message box purposes
function checkToDelete() {
	var myForm = arguments[0];
	var nameOfCheckboxes = '';
	var submitAction = '';
	var submitItemName = '';
	if (!(arguments[1])) { nameOfCheckboxes = 'toDelete';	} else { nameOfCheckboxes = arguments[1]; }
	if (!(arguments[2])) { submitItemName = 'items';	} else { submitItemName = arguments[2]; }
	if (!(arguments[3])) { submitAction = 'delete';	} else { submitAction = arguments[3]; }
	var somethingChecked = 0;
	var numberOfBoxes;
	
	numberOfBoxes = myForm.elements.length;

	//check all boxes (< not <= because I want to ignore the delete button form element !
	for (var i=0; i<numberOfBoxes; i++) {
		if (myForm.elements[i].name.indexOf(nameOfCheckboxes) > -1) {
			if (myForm.elements[i].checked) {
				somethingChecked = 1;
			}
		}		  	
	}
	if (somethingChecked != 1) {
		alert('You must select ' + submitItemName + ' to ' + submitAction + '. Please try again.');
		return false;
	} else {
		if (confirm('You are about to ' + submitAction + ' the selected ' + submitItemName + '. Are you sure?')) {
			return true;
		} else {
		 	return false;
		}
	}
}

function decrementSelected() {messagesSelected = messagesSelected - 1}

function incrementSelected() {messagesSelected = messagesSelected + 1}

function setValues() {messagesSelected = 0}

function checkReplies(myForm, startAt, levelsDeep) {
	//if zeroes passed in then check all boxes on page
	var checkAll = 0;
	if ((startAt==0) && (levelsDeep == 0)) {
		checkAll = 1;
	}
	
	//Loop through all form elements (except last two elements (*buttons*) on form (hence the "- 2" below)
	for (i = 1, j = myForm.elements.length - 3; i < j; i = i + 2) {

		//If this is the checked box, then check it's replies
		if ((myForm.elements[i + 1].value == startAt) || (checkAll == 1)) {

			// start at first checkbox if checking all (not hte one after that which has been clicked to launch this function)
			startingIndex = (checkAll == 1) ? i - 2 : i;

			//Loop through next elements until level is equal to current level or past end of checkboxes
			for (k = startingIndex + 2, l = j; k < l ;k = k + 2) {		
				if (((myForm.elements[k+1].checked == 0) && (myForm.elements[k].value > levelsDeep)) || (checkAll == 1)) {
					myForm.elements[k+1].checked = 1;
					incrementSelected();
				} else {
					k = l + 1;
				}
			}
		}
	}
}

function renameCheckboxes( myForm , strCheckboxToRename , strNewCheckboxName ){
	var numberOfBoxes;
	numberOfBoxes = myForm.elements.length;

	//rename all boxes that have strCheckboxToRename in the name to strNewCheckboxName
	for (var i=0; i<numberOfBoxes; i++) {
		if (myForm.elements[i].name.indexOf(strCheckboxToRename) > -1) {
			myForm.elements[i].name=strNewCheckboxName;
		}		  	
	}		
}


function isOkToUncheck( objForm , objCheckbox , previousIndex , currentIndex ){
	if ((eval('objForm.toDelete_' + previousIndex + '.checked') == true) && (eval('objForm.levelsDeep_' + previousIndex + '.value') < eval('objForm.levelsDeep_' + currentIndex + '.value')))
	{
		// this message is deeper than the previous message (a reply to it) and the previous message is checked for deletion, so disallow unchecking of this box
		objCheckbox.checked=true;
		return false;
	}
	else
	{	
		return true;
	}
}

function oldCheckToUncheck( objForm , objCheckbox , intPreviousIndex , intCurrentIndex) {
	if (isOkToUncheck(objForm , objCheckbox , intPreviousIndex , intCurrentIndex))
		{
			if (eval('objForm.levelsDeep_' + intPreviousIndex + '.value') == eval('objForm.levelsDeep_' + intCurrentIndex + '.value')) {
				if (eval('objForm.toDelete_' + intPreviousIndex + '.checked')==true){
					eval('objForm.toDelete_' + intPreviousIndex).click();
					eval('objForm.toDelete_' + intPreviousIndex).checked=true;
					decrementSelected();
				}
			}
			return true;
		}
		else
		{
			alert('You cannot delete the parent thread without deleting this message\n\nTo avoid deleting this message, please uncheck the parent message first and then try again');		
			return false;
		}
	}

	function checkToUncheck( objForm , objCheckbox) {
		var isOkToUncheck = true;
		var messages = '';
		var currentLevel = 0;
		var previousLevel = 0;
		previousIsCheckedAndCurrentIsUnchecked = false;
		atLeastOneLevelDown = false;
		for (var i = 2; i < (objForm.elements.length - 3); i++ )
		{
			if (objForm.elements[i].name.indexOf('toDelete') > -1)
			{
				currentLevel = objForm.elements[i - 1].value;
				// Is this message is at a level deeper than it's parent message (a reply to it) 
				// and the parent is checked but the child is not, check the child?
				previousIsCheckedAndCurrentIsUnchecked = (!(objForm.elements[i].checked) && (objForm.elements[i -2].checked))? true: false;
				// Is this message at least one level down in the structure?
				atLeastOneLevelDown = (currentLevel > 0)? true: false;
				if (previousIsCheckedAndCurrentIsUnchecked && atLeastOneLevelDown)
				{
					objForm.elements[i].checked = true;
					alert('You cannot delete the parent thread without deleting this message\n\nTo avoid deleting this message, please uncheck the parent message first and then try again');		
				}
			}
			if (objForm.elements[i - 2].name.indexOf('toDelete') > -1)
			{
				previousLevel = objForm.elements[i - 3].value;
			}
		}
		return true;
	}


function checkAll( objForm , fieldName ) {
	var intCheckedValue = 1; // defaults to checked
	if (arguments.length == 4)
		intCheckedValue = (arguments[3].toLowerCase() == 'clear') ? 0 : 1;
	if (typeof objForm == 'undefined') {
		return false;
	} else {
		var numberOfBoxes;
		var specifiedValue = ((typeof arguments[2] == 'undefined') || (arguments[2] == null)) ? '' : String(arguments[2]) ;
		numberOfBoxes = objForm.elements.length;
		//check all boxes (< not <= because I want to ignore the delete button form element !
		for (var i=0; i<numberOfBoxes; i++) {
			if (objForm.elements[i].name.indexOf(fieldName) > -1) {
				if (specifiedValue!='') {
					if (objForm.elements[i].value.indexOf(specifiedValue) > -1) 
						objForm.elements[i].checked=intCheckedValue;
				} else {
					objForm.elements[i].checked=intCheckedValue;
				}
			}		  	
		}
	}
}

// this function is used to determine if an option is selected from a group of radio/checkbox elements
// inputs:	
//		objForm	
//			- this is the form to vaidate on			
//		fieldName
//			- this is the name of group of elements to be checked
function optionSelected( objForm , fieldName ) {
	var somethingChecked = 0;
	var numberOfBoxes;
	numberOfBoxes = objForm.elements.length;
	//check all boxes (< not <= because I want to ignore the delete button form element !
	for (var i=0; i<numberOfBoxes; i++) {
		if (objForm.elements[i].name.indexOf(fieldName) > -1) {
			if (objForm.elements[i].checked) {
				somethingChecked = 1;
			}
		}		  	
	}
	return (somethingChecked == 1);
}

function countChecked( objForm , fieldName ) {
	var intCount=0;
	if (typeof objForm == 'undefined') {
		return false;
	} else {
		var numberOfBoxes;
		var specifiedValue = (typeof arguments[2] == 'undefined') ? '' : String(arguments[2]) ;
		numberOfBoxes = objForm.elements.length;
		//check all boxes (< not <= because I want to ignore the delete button form element !
		for (var i=0; i<numberOfBoxes; i++) {
			if ((objForm.elements[i].name.indexOf(fieldName) > -1) && (objForm.elements[i].value.indexOf(specifiedValue) > -1)) {
				if (objForm.elements[i].checked) {intCount++;}
			}		  	
		}
	}
	return intCount;
}

//-->
