/**
 * Copyright 2010 Allvesta, LLC
 *
 * This file and all of the source code contained within are the intellectual 
 * property of Allvesta, LLC. Any reproduction, description (either
 * written or verbal), or derivative of this code is strictly prohibited without
 * the written consent of Allvesta, LLC.
 *
 * -------------------------------------------------------------------------------
 *
 * Description: Javascript dynamic content methods.
 *      Author: Daniel Jennings (dsj@allvesta.com)
 *    Revision: A
 *  References: None
 *       Notes: None
 */


/**
 * Limits the size of input text dynamically.
 * @param limitCountElement DOM Element object (or element ID) of the field to limit.
 * @param limitNum Number of characters to limit to.
 * @param limitCountElement DOM Element object (or element ID) of the field to keep count in. If null then not used.
 */
function limitText(limitFieldElement, limitNum, limitCountElement) {
	//get the limit field element
	limitFieldElement = getElement(limitFieldElement);

	//check the length of the text against the limit
	if (limitFieldElement.value.length > limitNum) {
		limitFieldElement.value = limitFieldElement.value.substring(0, limitNum);
	} 
	else if (limitCountElement != null) {
		//update the counter
		limitCountElement = getElement(limitCountElement);
		limitCountElement.innerHTML = limitNum - limitFieldElement.value.length;
	}
} //end method limitText


/**
 * Initialize the character count of limited text.
 * @param limitCountElement DOM Element object (or element ID) of the field to limit.
 * @param limitNum Number of characters to limit to.
 * @param limitCountElement DOM Element object (or element ID) of the field to keep count in. If null then not used.
 */
function initializeCharacterCount(limitFieldElement, limitNum, limitCountElement) {
	//get the elements
	limitFieldElement = getElement(limitFieldElement);
	limitCountElement = getElement(limitCountElement);
	
	//update text
	limitCountElement.innerHTML = limitNum - limitFieldElement.value.length;
} //end method initializeCharacterCount


/**
 * Get the position and size of an element.
 * @param element DOM Element object (or element ID) to get left position for.
 * @return Array with left, top, width, and height of the given element.
 */
function getElementPositionSize(element) {
	//get the element
	element = getElement(element);

	//initialize output
	var position = [];
	position[0] = 0;
	position[1] = 0;
	position[2] = 0;
	position[3] = 0;
	
	//get left and top
    if (element.offsetParent) {
		position[0] += element.offsetLeft;
		position[1] += element.offsetTop;
		position[3] = element.offsetWidth;
		position[4] = element.offsetHeight;
	}
    //else if (element.x) {
	//	position[0] += element.x;
	//	position[1] += element.y;
	//}

    return position;
} //end method getElementPositionSize;


/**
 * Sort a select box
 * @param element Element to sort
 */
function sortSelectBox(element, isNoneFirst) {
	//get the select element
	var select = getElement(element);
	
	//get current texts and values
	var none_text = null;
	var none_value = null;
	var index = 0;
	var texts = []; 
	var values = []; 
	var old_texts = []; 
	for(var ii=0; ii<select.length; ii++) {
		if (isNoneFirst && select.options[ii].value.equalToIgnoreCase('none')) {
			none_text = select.options[ii].text;
			none_value = select.options[ii].value;
		}
		else {
			texts[index] = select.options[ii].text;
			old_texts[index] = texts[ii]; 
			values[index] = select.options[ii].value;
			index++;
		}
	} 
	
	//sort texts
	texts.sort(); 

	//if selected then set none value
	index = 0;
	if (isNoneFirst && none_text != null && none_value != null) {
		select.options[0].text = none_text;
		select.options[0].value = none_value;
		index++;
	}
	
	//update list
	for(var ii=0; ii<texts.length; ii++) { 
		select.options[index].text = texts[ii];
		
		for(var jj=0; jj<texts.length; jj++) { 
			if (texts[ii] == old_texts[jj]) { 
				select.options[index].value = values[jj];
				index++;
				break;
			} 
		} //end for
	} //end for 
} //end method sortSelectBoxList


/**
 * Add an option to a select box
 * @param selectElement The select box element
 * @param optionName The name of the option
 * @param optionValue The value of the option
 */
function addSelectBoxOption(selectElement, optionName, optionValue) {
    //build option
    var option = document.createElement('option');
    option.text = optionName;
    option.value = optionValue;
    
    //add option to select
	selectElement = getElement(selectElement);
    try {
    	selectElement.add(option, null); // standards compliant; doesn't work in IE
    }
    catch(ex) {
    	selectElement.add(option); // IE only
    }
} //end method addSelectBoxOption


/**
 * Remove an option from a select box
 * @param selectElement Select box element
 * @param optionValue The option value to remove
 */
function removeSelectBoxOption(selectElement, optionValue) {
	selectElement = getElement(selectElement);
	
	//loop over each element until match is found
	var num_options = selectElement.options.length;
	for (var ii=0; ii<num_options; ii++) {
		if (selectElement.options[ii].value == optionValue) {
			selectElement.remove(ii);
			break;
		}
	}
} //end method removeSelectBoxOption
