// Rosenzweig

// Pass in an element (usually the current element or "this")
// Pass in a tagName (the enclosing tag, perhaps a "TR")
// Returns the first enclosing element of that tag type
function getParentByTagName( element, tagName ) {
	var parentWithTag = null;
	while( element.parentNode != null ) {
		element = element.parentNode;
		if ( element.tagName.toLowerCase() == tagName.toLowerCase() ) {
			parentWithTag = element;
			break;
		}
	}
	return parentWithTag;
}

function getParentByClassName( element, tagName ) {
	var parentWithClass = null;
	while( element.parentNode != null ) {
		element = element.parentNode;
		if (element.className != null && element.className.match('\\b'+tagName+'\\b')) {
			parentWithClass = element;
			break;
		}
	}
	return parentWithClass;
}

// Pass in an enclosing tag (maybe a TR or a TD)
// Pass in a boolean (False to uncheck, True to select)
// Makes all checkboxes enclosed by the parent element either checked or unchecked
function setAllCheckBoxes( parentElement, setCheckedBool ) {
	if ( parentElement != null ) {
		var elements = parentElement.getElementsByTagName('input');
		for(var i = 0; elements.length > i; i++) { 
			if(elements[i].type == 'checkbox') elements[i].checked = setCheckedBool; 
		}
	}
}

function doCheckAllBoxes(currentElement, check) {
	var parentRow = getParentByClassName(currentElement, 'simpleSearchFieldRow');
	if(parentRow==null) {
		parentRow = getParentByTagName( currentElement, "tr" );
	}
	setAllCheckBoxes( parentRow, check );
}

function uncheckAllCheckBoxes( currentElement ) {
	doCheckAllBoxes( currentElement, false );
}

function checkAllCheckBoxes( currentElement ) {
	doCheckAllBoxes( currentElement, true );
}

function hasAtLeast5CheckboxesUnderneath( parentElement ) {
	var hasAtLeast5 = false;
	var checkBoxCounter = 0;
	if ( parentElement != null ) {
		var elements = parentElement.getElementsByTagName('input');
		for(var i = 0; elements.length > i; i++) { 
			if(elements[i].type == 'checkbox') {
				checkBoxCounter++;
				if(checkBoxCounter >=5){
					hasAtLeast5=true;
					break;
				}
			} 
		}
	}	
	return hasAtLeast5;
}

// Pass in an element (usually the current element or "this")
// Pass in a tagName (the enclosing tag, perhaps a "TR")
// Returns true if there are 2 or more Checkboxes and false otherwise
function parentHasMultipleCheckboxes( currentElement, tagName ) {
	return hasAtLeast5CheckboxesUnderneath( 
		getParentByTagName( currentElement, tagName ) );
}

function uncheckOtherIfIAmChecked(currentElement, otherid) {
	
	if(currentElement.checked) {
		var other = document.getElementById(otheid);
		
		if(other != null) {
			other.checked = false;
		}
	}


}

