// TIGER HTML FUNCTIONS //

TIGER.html = {};

/**
* @method getForm
* @description This method accepts a form and return an array of the
* form's input-able elements. It will not return hidden or disabled fields.
* @author K. Beau Beauchamp
* @public
* @static
* @param {string || object} form id or name attribute, or form object
* @param {bool} TRUE return all elements, FALSE return only editable elements
* @param {string} nodes to ONLY include, example: 'select,input.checkbox'
* @param {string} nodes to exclude, example: 'button,img,input.radio'
* @return {array} array of the HTML form elements.
*/
TIGER.html.getForm = function (formId, bShowAll, sInclude, sExclude)
{
/* Description:
 * The M2.html.getForm method is designed return form elements that would
 * normally be sent in a standard form post. Optionally, a boolean flag can 
 * be set to filter out any elements that are disabled, hidden, or otherwise 
 * not editable.
 */

	var oForm, oElement, oHidden, oName, oVisible, oDisabled, oNode, oType, bShow, aElements = [], i, len;
    
	if (typeof(formId) === 'string')
	{
		// Determine if the argument is a form id or a form name.
		// oForm = document.getElementById(formId);
		oForm = Dom.get(formId);

		if (!oForm)
		{ 
			return formID;
		}
	}
	else if (typeof(formId) === 'object')
	{
		// Treat argument as an HTML form object.
		oForm = formId;

		if (oForm.nodeName.toString().toLowerCase() != 'form' )
		{ 
			return formID; 
		}
	}
	else
	{
		return;
	}
 
	sInclude = (sInclude) ? sInclude : '';
	sExclude = (sExclude) ? sExclude : '';
	bShowAll = (bShowAll) ? bShowAll : false;
 
	/* Iterate over the form elements collection to construct the 
	 * attribute-value pairs. */
	for (i=0, len=oForm.elements.length; i<len; ++i){
 
		oElement	= oForm.elements[i];
		oName		= oElement.name;
		oType		= (oElement.type) ? '.'+oElement.type.toString().toLowerCase() : '';
		oNode		= oElement.nodeName.toString().toLowerCase() + oType;
		oDisabled	= oElement.disabled;
		oHidden		= (oElement.type=='hidden') ? true : false;
		oVisible	= (oElement.style.display != 'none' || oElement.style.visibility != 'hidden') ? true : false;
 
		/* Only return elements that have a name attribute. This gets rid of
		 * a lot of junk right off the bat. */
		if (oName)
		{
			/* Do not return fields that are hidden, invisible, disabled or 
			 * do not have a name attribute. */
			// alert(bShowAll);

			if (!bShowAll)
			{ 
				bShow = (!oDisabled && oName && !oHidden && oVisible); 
			}
			else
			{
				bShow = true;
			}
 
			if (sInclude !== '')
			{
				bInclude = (sInclude.indexOf(oNode,0) !== -1);
			}
			else
			{
				bInclude = true;
			}
 
			if (sExclude !== '')
			{
				bExclude = (sExclude.indexOf(oNode,0) !== -1);
			}
			else
			{
				bExclude = false;
			}
 
			if (bShow && bInclude && !bExclude)
			{
				aElements.push(oElement);                             
			}
                  
		}
 
	}
     
	return aElements;
 
};


/**
 * @method applyAttributes
 * @description Applys attributes to newly created DOM element
 * @author K. Beau Beauchamp
 * @public
 * @static
 * @param {elm} a DOM element
 * @param {object} Name:Value pairs of element attritbutes to apply
 * @return nothing
 */
TIGER.html.applyAttributes = function(elm, oAttributes) {
	for (var sAttributeName in oAttributes) {
		var sAttributeValue = oAttributes[sAttributeName];
		if(sAttributeName == 'class') {
			elm.className = sAttributeValue;
		}
		else{
			elm.setAttribute(sAttributeName, sAttributeValue);
		}
	}
};

/**
 * @method elementString
 * @description Builds an element string for creating IE elements with 'name' a
 * attribute
 * @author K. Beau Beauchamp
 * @public
 * @static
 * @param {string} element node name to be created, ie. input || select
 * @param {object} Name:Value pairs of element attritbutes to apply
 * @return nothing
 */
TIGER.html.elementString = function(sNode, oAttributes) {
	var eString = '<'+sNode;
	for (var sAttributeName in oAttributes) {
		var sAttributeValue = oAttributes[sAttributeName];
		eString += ' '+sAttributeName+'="'+sAttributeValue+'"';
	}
	return eString += ' />';
};

/**
 * @method createElement
 * @description Creates new elements and inserts them into the DOM.
 * @author K. Beau Beauchamp
 * @public
 * @static
 * @param {string} Id and name of the new element
 * @param {object} Name:Value pairs of the new element's attritbutes
 * @param {str || elm} (optional) target element of where to place newly created element
 * @param {str} (optional) where to place the newly created element (see below)
 * @return {elm} returns the newly created element
 */
TIGER.html.createElement = function(sNode,oAttributes,eTarget,sPlacement)
{
	var elm;
	sPlacement = (sPlacement !== null && sPlacement !== undefined) ? sPlacement : 'child';
	
	// IE Browsers:
	// We need to make a special consideration for IE and name attrubutes; IE
	// doesn't apply the name attribute in certain circumstances so we need to
	// build the IE elements a bit differently

	if (TIGER.util.isObject(oAttributes)) {
		if (TIGER.ua.ie > 0) {
			elm = document.createElement(TIGER.html.elementString(sNode,oAttributes));
		}
		else {
			elm = document.createElement(sNode);
			TIGER.html.applyAttributes(elm, oAttributes);
		}
	}
	else
	{
		// create a blank element without attributes
		elm = document.createElement(sNode);
	}

	// Groom eTarget

	if (eTarget != null && eTarget !== undefined){
		eTarget = (Dom.inDocument(eTarget) || typeof(eTarget).toString().toLowerCase() === 'object') ? Dom.get(eTarget) : null;
	}

	// where to put the new element
	if (eTarget != null) {
		if (typeof(eTarget) == 'object') {
			
			switch(sPlacement.toString().toLowerCase()){
				
				case 'before':
					Dom.insertBefore(elm,eTarget);
					break;

				case 'after':
					Dom.insertAfter(elm,eTarget);
					break;

				case 'child':
				default:
					eTarget.appendChild(elm);
					break;
			
			}
		}
	}

	return elm;	
	
};

