/**
 * pass in id for textfield, and countSpan
 * attempt to find both fields and update the countSpan value with maxlength - textfield.length
 */
function textCounter( textfieldId, countSpanId, inMaxlimit ) {
	// 09/22/2017 NGC-TM Fortify ID 1346975 possible Cross-Site-Scripting attack due to the unvalidated input of maxlimit
	// convert maxLimit to a number before utilizing further to avoid the risk.
	var maxlimit = parseInt(inMaxlimit);
	var textfield = document.getElementById(textfieldId);
	if(textfield != null) {
		if ( textfield.value.length > maxlimit ) {
			textfield.value = textfield.value.substring( 0, maxlimit );
			textfield.blur();
			textfield.focus();
	  		return false;
	 	} else {
	 		var countSpan = document.getElementById(countSpanId);
	 		if(countSpan != null){
	 			countSpan.innerHTML = maxlimit - textfield.value.length;
	  		}
	 	}
	}
}

/**
 * pass in id for textfield (this.id), and countSpan name (valud of the id= field).  
 * will try to parse the textfield dynamic name to the table node and then append on the count apsn name.
 * This is used when the text field is in a dynamic built table and that occurs more than once on the page 
 * and unique names cannot be defined the to count and text fields. 
 * attempt to find both fields and update the countSpan value with maxlength - textfield.length
 */
function textCounterTable( textfieldId, countSpanName, inMaxlimit ) {
	// 09/22/2017 NGC-TM Fortify ID 1346975 possible Cross-Site-Scripting attack due to the unvalidated input of maxlimit
	// convert maxLimit to a number before utilizing further to avoid the risk.
	var maxlimit = parseInt(inMaxlimit);

	var textfield = document.getElementById(textfieldId);
	if(textfield != null) {
		if ( textfield.value.length > maxlimit ) {
			textfield.value = textfield.value.substring( 0, maxlimit );
			textfield.blur();
			textfield.focus();
	  		return false;
	 	} else {
	 		var textSub=textfieldId.substring(0,(textfieldId.lastIndexOf(':')+1));
	 		if ('input_' == textSub.substring(0,6)) {
	 			textSub=textSub.substring(6);
	 		}
	 		var countSpan = document.getElementById(textSub+countSpanName);
	 		if(countSpan != null){
	 			countSpan.innerHTML = maxlimit - textfield.value.length;
	  		}
	 	}
	}
}

/**
 * pass in id for label and attempt to add cssClass to label.className
 */
function addCssClass( labelId, cssClass ) {
	var label = document.getElementById(labelId);
	if(label != null) {
		if(label.className != null) {
			label.className += " " + cssClass;
		} else {
			label.className = " " + cssClass;
		}
	}
}

/**
 * pass in id for field and label
 * if field.className contains cssClass attempt to add cssClass to label
 */
function containsAddCssClass( fieldId, labelId, cssClass ) {
	var field = document.getElementById(fieldId);
	if(field != null && field.className != null && field.className.indexOf(cssClass) != -1) {
		addCssClass( labelId, cssClass );
	}
}

/**
 * Toggle the showing of a container based on the passed in values
 * 
 * shwIndicator - The select element that holds the value to compare
 * showValue - the value to toggle the container visibility for
 * showContainer - the contianer to show/hide
 */
function toggleContainer(showIndicator, showValue, showContainer)
{
	// get indicator
	if ($(showIndicator).val() === showValue)
	{
		// show container
		$(showContainer).removeClass('hide').addClass('show');
	}
	else
	{
		// hide container
		$(showContainer).removeClass('show').addClass('hide');
	}
	
}
