/* Lots of functiosn for creating specific elements.
	Most are self-explanatory, but all reference the createNewElement
	function for creating an element in the document and passing it an 
	ID and Class if so desired
*/
function createNewElement( elemType, elemID, elemClass ) {
	var myElem = document.createElement( elemType );
	
	if( elemID ) {
		myElem.setAttribute( 'id', elemID );
	}
	
	if( elemClass ) {
		myElem.setAttribute( ( document.all ? 'className' : 'class' ), elemClass );
	}
	
	return myElem;
}

function createDiv( divID, divClass ) {
	var myDiv = createNewElement( 'div', divID, divClass );
	return myDiv;
}

function createParagraph( paraText, paraID, paraClass ) {
	var myPara = createNewElement( 'p', paraID, paraClass );
	
	if( paraText ) {
		myPara.appendChild( document.createTextNode( paraText ) );
	}
	
	return myPara;
}

function createHeader( headerNum, headerText, headerID, headerClass ) {
	var myHeader = createNewElement( headerNum, headerID, headerClass );
	
	if( headerText ) {
		myHeader.appendChild( document.createTextNode( headerText ) );
	}
	
	return myHeader;
}

function createImage( imagePath, imageID, imageClass ) {
	var myImage = createNewElement( 'img', imageID, imageClass );
	myImage.setAttribute( 'src', imagePath );
	
	return myImage;
}

function createTable( headerArray, tableID, tableClass ) {
	var myTable = createNewElement( 'table', tableID, tableClass );
	myTable.style.borderCollapse = 'collapse';
	
	// IE doesn't understand if there's no tbody, so add it
	if( document.all ) {
		var myTbody = createNewElement( 'tbody' );
		myTable.appendChild( myTbody );
	}
	
	insertRows( myTable, headerArray, 'th' );
	return myTable;	
}

/* Create an input depending on the input type */
function createInput( inputType, inputSize, inputValue, inputName, inputId, inputClass ) {
	var myInput = createNewElement( 'input', inputId, inputClass );
	myInput.setAttribute( 'type', inputType );
	myInput.setAttribute( 'name', inputName );
	myInput.setAttribute( 'size', inputSize );
	
	if( inputValue ) {
		myInput.setAttribute( 'value', inputValue );
	}
	return myInput;
}

function createLabel( labelText, labelTarget, labelId, labelClass ) {
	var myLabel = createNewElement( 'label', labelId, labelClass );
	myLabel.setAttribute( 'for', labelTarget );
	myLabel.appendChild( document.createTextNode( labelText ) );
	
	return myLabel;
}

function createList( listType, listId, listClass ) {
	var myList = createNewElement( listType, listId, listClass );
	
	return myList;
}

function insertListItem( whichList, listElem, listId, listClass ) {
	var myListItem = createNewElement( 'li', listId, listClass );
	
	if( typeof listElem == 'string' ) {
		myListItem.appendChild( document.createTextNode( listElem ) );
	}
	else {
		myListItem.appendChild( listElem );
	}
	
	whichList.appendChild( myListItem );
}

/* Insert rows into a table by passing in an array of values to append to the table */
function insertRows( whichElem, elemArray, cellType, cellId, cellClass ) {
	if( !cellType ) {
		cellType = 'td';
	}
	
	if( document.all ) {
		whichElem = whichElem.firstChild;
	}
	
	if( elemArray.length > 0 ) {
		var myRow = createNewElement( 'tr' );
	
		for( var i = 0; i < elemArray.length; i++ ) {
			var myCol = elemArray[ i ] ? createNewElement( cellType, cellId, cellClass ) : createNewElement( cellType );
			if( typeof elemArray[ i ] == 'string' ) {
				myCol.appendChild( document.createTextNode( elemArray[ i ] ) );
			}
			else {
				myCol.appendChild( elemArray[ i ] );
			}
			myRow.appendChild( myCol );				
		}
		
		whichElem.appendChild( myRow );
	}
}

/* Simple function to remove an element from a container */
function removeFromElement( whichElem, whichObj ) {
	whichElem.removeChild( whichObj );
}

function addNewOption( whichSelect, optionValue, optionText ) {
	whichSelect.options[ whichSelect.options.length ] = new Option( optionValue, optionText );
}

function setElementClass( whichElem, whichClass ) {
	whichElem.setAttribute( ( document.all ? 'className' : 'class' ), whichClass );
}

/* Creates a 'fake' link that has the mouse cursor turn to the pointer/hand when over an element */
function setFakeLink( whichElem ) {
	whichElem.onmouseover = function() {
		this.style.cursor = ( document.all ? 'hand' : 'pointer' );
	}
					
	whichElem.onmouseout = function() {
		this.style.cursor = 'default';
	}
}