/****************************************************************************
  Filename: mocc_lib_elems.js
  Current version: 1.4
  Created: 2008-09-11
  Last Modified: 2009-04-15
  
  Description:
     This file contains the necessary functions to handle and 
     modify elements in general functionality for specific elements 
     should be placed in specific libraries.
  
  Dependencies: none
  
  Contributors (chronological order, starting with originating author):
    1. Matt Olinger (MO)
    2. Cliff Chambers (CC)
    3. Matt Meyer (MM)

  Revision History:
    v1.0 (MO, 2007-09-11) -
      created with functions: getElem and setElemStyle
  	v1.1 (CC, 2007-09-29) - 
  	  updated with function: getElemCountByClassName 
    v1.2 (CC, 2007-10-06) -
      updated with function: insertElem, removeElem
    v1.3 (MO, 2007-10-28) -
      updated with function: getElemCountByID
    v1.4 (MM, 2009-04-15) -
      Cleaned up comments for readability
 ****************************************************************************/

/****************************************************************************
  FUNCTION getElem  (original source unknown)
  Parameters: ob = string specifying ID of the element to be returned
  Returns: the element specified by ID

  Dependencies: none

********************************************************************************/
function getElem(ob) {
  if (document.getElementById) {    // this is the way the standards work
    return document.getElementById(ob);
  }
  else if (document.all) {        // this is the way old msie versions work
    elem = document.all[ob];
  }
  else if (document.layers) {      // this is the way nn4 works
    elem = document.layers[ob];
  }
return elem;
}


/*********************************************************************************/
/*  FUNCTION setElemStyle  (alpha version)                    							*/
/*	Parameters: ob=string specifying ID of the element to be modified					*/
/*				str=a string representing the style that is to be applied							*/
/*					e.g. "display: block; border: 1px solid black;"									*/
/*																											*/
/*	Returns: all js commands used to modify the element in full												*/
/*																											*/
/*	Dependencies: mocc_lib_string.js (.trim)																*/
/*																											*/
/*********************************************************************************/
function setElemStyle(ob,str) {
	var singleStr, fullStr='';
	var elem=getElem(ob);
	var attributes=str.split(";");
	for (var i=0;i<attributes.length;i++) {
		var val=attributes[i].replace(/;/g,'');	//global replace of semicolon to null (mostly in case last attribute is only a semicolon
		val=val.trim();
		if (val!='') {
			val=val.replace(/:\s*/g,'="');			//globally remove the colon and any following spaces
			singleStr='elem.style.' + val + '";';	//transfer css language into js (width: 100px -> elem.style.width="100px";)
			eval(singleStr);							//perform the js equivalent fo the css
			fullStr+=singleStr;						//add the newly performed command to the output
		}
	}
return fullStr;		//send the entire list of js commands that was used (only good for debugging purposes)
}

/*********************************************************************************/
/*	FUNCTION isArray																					*/
/*	Parameters: ob=string specifying variable/constant to be checked					*/
/*																											*/
/*	Returns: true if ob is an array, false if ob is not an array												*/
/*																											*/
/*	Dependencies: none																					*/
/*																											*/
/*********************************************************************************/
function isArray(ob) {
   return (ob.constructor.toString().indexOf("Array") != -1)
}


/*********************************************************************************/
/*	FUNCTION getElemCountByID																					*/
/*	Parameters: ob=string specifying class name to be checked					*/
/*																											*/
/*	Returns: count 											*/
/*																											*/
/*	Dependencies: none																					*/
/*																											*/
/*********************************************************************************/


function getElemCountByID(ob) {
	var pgt = new Array();
	var cn=0;
	pgt = document.getElementsByTagName("*");
	for (i=0; i<pgt.length; i++) {
		if (pgt[i].id==ob) {
			cn++;
		}
	}
	return cn;
}


/*********************************************************************************/
/*	FUNCTION getElemCountByClassName																					*/
/*	Parameters: ob=string specifying class name to be checked					*/
/*																											*/
/*	Returns: count 											*/
/*																											*/
/*	Dependencies: none																					*/
/*																											*/
/*********************************************************************************/


function getElemCountByClassName(ob) {
	var pgt = new Array();
	var cn=0;
	pgt = document.getElementsByTagName("*");
	for (i=0; i<pgt.length; i++) {
		if (pgt[i].className==ob) {
			cn++;
		}
	}
	return cn;
}


/*********************************************************************************/
/*	FUNCTION insertElem																					*/
/*	Parameters: parent = parent element					*/
/*  			child = element to be added inside of parent
/*				txt = text to be assigned to the child element
/*				childattrib = array of attributes that are assigned to the child element
/*																											*/
/*	Returns: nothing 											*/
/*																											*/
/*	Dependencies: none																					*/
/*																											*/
/*********************************************************************************/


function insertElem(parent,child,txt,childattrib) {
	var elem = document.createElement(child);
	var elemtxt = document.createTextNode(txt);
	//if (parent=="document") var parentelem = document.getChildNodes().item(0).getChildNodes().item(1); //get body element
	if (parent=="document") var parentelem = document.body; //get body element
	else var parentelem = getElem(parent);

	for (i=0; i < childattrib.length; i++) {
		var tribs = childattrib[i].split(",")
		if (tribs[0]=='onclick') {
			elem.onclick=function() { removeElem(tribs[1]);};
		} else if (tribs[0]=="class") {
			elem.className=tribs[1];
		} else {
			elem.setAttribute(tribs[0].replace("|",","),tribs[1].replace("|",","));
		}

	}
	parentelem.appendChild(elem);
	elem.appendChild(elemtxt);
}



/*********************************************************************************/
/*	FUNCTION removeElem																					*/
/*	Parameters: parent = parent element					*/
/*  			child = element to be removed inside of parent
/*																											*/
/*	Returns:  											*/
/*																											*/
/*	Dependencies: none																					*/
/*																											*/
/*********************************************************************************/


function removeElem(child) {
	childelem=getElem(child);
	childelem.parentNode.removeChild(childelem);
}