/*put this script tag in your code, then just use buildHtml.function() or buildParentHtml.function()
<script language="javascript" src="/scripts/formBuilder/buildHTMLClass.js"></script>
<script language="javascript">
	<!---1 parameter is current window, 2 is parent window--->
	var buildHtml = new buildHTMLClass(1);
	var buildParentHtml = new buildHTMLClass(2);
</script> */

/*if its IE, we need to refresh the innerHTML or events on DOM elements won't work...
we can do it here, but if we return it we need to do it after its appended to its parent
element*/


function buildHTMLClass(winType){
		
	/*set instance variables*/
	this.winType = winType;
	/*1 the document is the current window, 2 the document is the parent window (the window doing the js is a pop-up) */																				   
	if(this.winType==1){
		this.DHTMLWindow = document;
	}
	else if (this.winType==2){
		this.DHTMLWindow = window.opener.document;
	}	
	
	/*create basic text field element*/
	 this.createTextHTMLElement = function(textFieldID,textFieldValue,textFieldSize,maxFieldLen, parentElement){
		
		var input = this.DHTMLWindow.createElement('INPUT');
		
		if ( typeof(textFieldSize) == "undefined" || textFieldSize==null) {
			var textFieldSize = 30; 
		}		
		if ( typeof(textFieldValue) == "undefined" || textFieldValue==null ) {
			var textFieldValue = ''; 
		}
		if ( typeof(textFieldSize) == "undefined" || textFieldSize==null ) {
			var textFieldSize = ''; 
		}

		if ( typeof(maxFieldLen) == "undefined" || maxFieldLen==null ) {
			var maxFieldLen = ''; 
		}

		if ( typeof(parentElement) == "undefined" || parentElement==null ) {
			var parentElement = ''; 
		}
		
		input.type = "text";
		input.name = textFieldID;
		input.id = textFieldID;
		input.size = textFieldSize;
		input.value = textFieldValue;
		input.maxlength = maxFieldLen;

		return this.appendReturn(input,parentElement,0);
	}
	
	/*create a caption for HTML fields*/
	this.createHTMLFieldCaption = function(textFieldCaption,parentElement){
				
		if ( typeof(textFieldCaption) == "undefined" || textFieldCaption==null ) {
			var textFieldCaption = ''; 
		}

		if ( typeof(parentElement) == "undefined" || parentElement==null ) {
			var parentElement = ''; 
		}
		
		var fieldCaption = this.DHTMLWindow.createTextNode(textFieldCaption);
		
		return this.appendReturn(fieldCaption,parentElement,0);
		
	}
	
	/*remove and element by ID.... */
	this.removeElement = function(elementID){
		var delRow = this.DHTMLWindow.getElementById(elementID);
			delRow.parentNode.removeChild(delRow);
	}
	
	/*note, the onClickEvent parameter can't be the name of a function, but must be the actual function object*/
	this.createURLLink = function(linkUrl,displayText,onClickEvent){
		var newLink = this.DHTMLWindow.createElement('a');
		
		/*set defaults for parameters*/
		if ( typeof(linkUrl) == "undefined" ) {
			var linkUrl = ''; 
		}
		
		if ( typeof(displayText) == "undefined" ) {
			var displayText = ''; 
		}
		
		if ( typeof(onClickEvent) == "undefined" ) {
			var onClickEvent = ''; 
		}
		
		if (this.DHTMLWindow.all) {
			// IE
			/*we only want the href attribute if one is defined, otherwise it might interfere with
			our onclick attribute*/
			if(linkUrl.length > 0){
				newLink.href = linkUrl;
			}
			
			if(linkUrl.onClickEvent > 0){
				newLink.attachEvent("onclick", onClickEvent);
			}
			newLink.appendChild(this.DHTMLWindow.createTextNode(displayText));
		}
		else if (this.DHTMLWindow.getElementById) { 
			// FF
			if(linkUrl.length > 0){
				newLink.setAttribute('href', linkUrl);
			}
			
			if(linkUrl.onClickEvent > 0){
				newLink.setAttribute('onclick', onClickEvent);
			}
			newLink.appendChild(this.DHTMLWindow.createTextNode(displayText));
		}
		
		return newLink;
	}
	
	/*create a div*/
	this.createDivHTMLElement = function(divID,divClass,divStyle,mouseup, parentElement,onClickValue){
		var htmlDiv = this.DHTMLWindow.createElement('div');
				
		if ( typeof(divID) == "undefined" || divID == null ) {
			var divID = ''; 
		}
		
		if ( typeof(divClass) == "undefined" || divClass == null ) {
			var divClass = ''; 
		}
				
		if ( typeof(divStyle) == "undefined" || divStyle == null ) {
			var divStyle = ''; 
		}
		
		if ( typeof(mouseup) == "undefined" || mouseup == null ) {
			var mouseup = ''; 
		}
		
		if ( typeof(parentElement) == "undefined" || parentElement == null ) {
			var parentElement = ''; 
		}
		
		if ( typeof(onClickValue) == "undefined" || onClickValue == null ) {
			var onClickValue = ''; 
		}
		
		if (this.DHTMLWindow.all) {
			htmlDiv.onclick = onClickValue;
		}
		else if (this.DHTMLWindow.getElementById) { 
			htmlDiv.setAttribute('onclick', onClickValue);
		}
		
		htmlDiv.className = divClass;
		htmlDiv.id = divID;
		if(divStyle.length > 0){
			htmlDiv.className = divStyle;
		}
		htmlDiv.onmouseup = mouseup;

		return this.appendReturn(htmlDiv,parentElement,1);
	}
	
	/*create a basic HTML check box element*/
	this.createCheckBoxHTMLElement = function(checkBoxID,checkValue){
		var input = this.DHTMLWindow.createElement('INPUT');
		
		input.type = "checkbox";
		input.name = checkBoxID;
		input.id = checkBoxID;
		input.value = checkValue;

		return this.appendReturn(input,"",0);
	}
	
	/*create a basic HTML check box element*/
	this.createButtonHTMLElement = function(buttonID,buttonValue,onClickValue,parentElement,elementClass){
		var input = this.DHTMLWindow.createElement('INPUT');
		
		if ( typeof(buttonID) == "undefined" || buttonID == null ) {
			var buttonID = ''; 
		}
		
		if ( typeof(buttonValue) == "undefined" || buttonValue == null ) {
			var buttonValue = ''; 
		}
		
		if ( typeof(onClickValue) == "undefined" || onClickValue == null ) {
			var onClickValue = ''; 
		}
		
		if ( typeof(parentElement) == "undefined" || parentElement==null ) {
			var parentElement = ''; 
		}
		
		if ( typeof(elementClass) == "undefined" || elementClass==null ) {
			var elementClass = ''; 
		}
		
		if (this.DHTMLWindow.all) {
			input.onclick = onClickValue;
		}
		else if (this.DHTMLWindow.getElementById) { 
			input.setAttribute('onclick', onClickValue);
		}

		input.type = "button";
		input.name = buttonID;
		input.id = buttonID;
		input.value = buttonValue;
		input.className = elementClass;
		
		return this.appendReturn(input,parentElement,1);
		
}
	
	/*create a basic HTML hidden element*/
	this.createHiddenHTMLElement = function(hiddenFieldID,hiddenFieldValue,parentElement){
		var input = this.DHTMLWindow.createElement('INPUT');
		
		if ( typeof(hiddenFieldID) == "undefined" || hiddenFieldID == null ) {
			var hiddenFieldID = ''; 
		}
		
		if ( typeof(hiddenFieldValue) == "undefined" || hiddenFieldValue == null ) {
			var hiddenFieldValue = ''; 
		}
		
		if ( typeof(parentElement) == "undefined" || parentElement==null ) {
			var parentElement = ''; 
		}
		
		input.type = "hidden";
		input.name = hiddenFieldID;
		input.id = hiddenFieldID;
		input.value = hiddenFieldValue;
				
		return this.appendReturn(input,parentElement,0);
		
	}
	
	/*create a table*/
	this.createTable = function(tableID,cellspacing, border, cellpadding,width,align){
		var newTable = this.DHTMLWindow.createElement('TABLE');
		
		if ( typeof(tableID) == "undefined" || tableID == null ) {
			var tableID = ''; 
		}
		if ( typeof(cellspacing) == "undefined" || cellspacing == null ) {
			var cellspacing = '0'; 
		}
		if ( typeof(border) == "undefined" || border == null ) {
			var border = '0'; 
		}
		if ( typeof(cellpadding) == "undefined" || cellpadding == null ) {
			var cellpadding = '0'; 
		}	
		
		
		if (this.DHTMLWindow.all) {
			// IE
			newTable.id = tableID;
			newTable.cellspacing = cellspacing;
			newTable.border = border;
			newTable.cellpadding = cellpadding;
			if ( typeof(width) != "undefined" & border != null ) {
				newTable.width = width;
			}
			if ( typeof(align) != "undefined" & border != null ) {
				newTable.align = align;
			}
		}
		else if (this.DHTMLWindow.getElementById) { 
			// FF
			newTable.setAttribute('id', tableID);
			newTable.setAttribute('cellspacing', cellspacing);
			newTable.setAttribute('border', border);
			newTable.setAttribute('cellpadding', cellpadding);
			if ( typeof(width) != "undefined" & border != null ) {
				newTable.setAttribute('width', width);
			}
			if ( typeof(align) != "undefined" & border != null ) {
				newTable.setAttribute('align', align);
			}
		}
		
		return newTable;
	}
	
	/*create a row*/
	this.createRow = function(rowID,height,mouseout,mouseover){
		var newRow = this.DHTMLWindow.createElement('TR');
						
		if ( typeof(rowID) == "undefined" || rowID == null ) {
			var rowID = ''; 
		}
		
		if ( typeof(height) == "undefined" || height == null ) {
			var height = ''; 
		}
		
		if ( typeof(mouseout) == "undefined" || mouseout == null ) {
			var mouseout = ''; 
		}
		
		if ( typeof(mouseover) == "undefined" || mouseover == null ) {
			var mouseover = ''; 
		}
		
		if (this.DHTMLWindow.all) {
			// IE
			newRow.id = rowID;
			newRow.height = height;
			newRow.onmouseout = mouseout;
			newRow.onmouseover = mouseover;
		}
		else if (this.DHTMLWindow.getElementById) { 
			// FF
			newRow.setAttribute('id', rowID);
			newRow.setAttribute('height', height);
			newRow.setAttribute('onmouseout', mouseout);
			newRow.setAttribute('onmouseover', mouseover);
		}
		
		return newRow;
	}
	
	/*create datacell*/
	this.dataCell = function(cellID,height,width, nowrap){
		var newDataCell = this.DHTMLWindow.createElement('TD');
		
		if ( typeof(cellID) == "undefined" || cellID == null ) {
			var cellID = ''; 
		}
		
		if ( typeof(height) == "undefined" || height == null ) {
			var height = ''; 
		}
		
		if ( typeof(width) == "undefined" || width == null ) {
			var width = ''; 
		}
		
		if (this.DHTMLWindow.all) {
			// IE
			newDataCell.id = cellID;
			newDataCell.height = height;
			newDataCell.width = width;
			if(typeof(nowrap) != "undefined" && nowrap != null && typeof(nowrap) == "boolean" && nowrap == true){
				newDataCell.noWrap = true;
			}
		}
		else if (this.DHTMLWindow.getElementById) { 
			// FF
			newDataCell.setAttribute('id', cellID);
			newDataCell.setAttribute('height', height);
			newDataCell.setAttribute('width', width);
			 if(typeof(nowrap) != "undefined" && nowrap != null && typeof(nowrap) == "boolean" && nowrap == true){	 
				newDataCell.setAttribute('nowrap', "true");
			}
		}
		
		return newDataCell;
	}
	
	this.createImage = function(imagePath, imgHeight, imgWidth, parentElement, imgId){
		
		if ( typeof(imagePath) == "undefined" || imagePath == null ) {
			var imagePath = ''; 
		}
		
		if ( typeof(imgHeight) == "undefined" || imgHeight == null ) {
			var imgHeight = ''; 
		}
		
		if ( typeof(imgWidth) == "undefined" || imgWidth == null ) {
			var imgWidth = ''; 
		}
		
		if ( typeof(parentElement) == "undefined" || parentElement == null ) {
			var parentElement = ''; 
		}
		
		if ( typeof(imgId) == "undefined" || imgId == null ) {
			var imgId = ''; 
		}
		
	
		var newImage = this.DHTMLWindow.createElement('IMG');
	
		newImage.src = imagePath;
		newImage.id = imgId;
		
		if(imgHeight.length > 0){
			newImage.height = imgHeight;
		}
		if(imgWidth.length > 0){
			newImage.width = imgWidth;		
		}
			
		return this.appendReturn(newImage,parentElement,0);

}
	
	/*creates a new script. html created with innerHTML won't work unless we add the script to the head of the HTML document*/
	this.createScript = function(scriptType,scriptPath,scriptOnload,scriptID){
		
		if ( typeof(scriptType) == "undefined" || scriptType == null ) {
			var scriptType = ''; 
		}
		
		if ( typeof(scriptPath) == "undefined" || scriptPath == null ) {
			var scriptPath = ''; 
		}
		
		if ( typeof(scriptOnload) == "undefined" || scriptOnload == null ) {
			var scriptOnload = ''; 
		}
		
		if ( typeof(scriptID) == "undefined" || scriptID == null ) {
			var scriptID = ''; 
		}
		
		var newScript = this.DHTMLWindow.createElement('script');
		
		/*notes about the scriptOnload..... it would seem IE is unreliable in providing the readyState...
		this person talks about it a bit...  http://unixpapa.com/js/dyna.html... the recommended solution is
		to call your function from the last line in the the script you're calling...*/
		
		/*UPDATE - to try to fix\work around the buy mentioned above, we look for loaded and complete readystates,
		and if we get either one, we make the onreadystatechange equal nothing, so that it doesn't run a second time*/
		
		if(scriptOnload.length > 0){		
			// IE
			if(document.all){
				newScript.onreadystatechange = function() {
					if (newScript.readyState == 'complete') {
						newScript.onreadystatechange = "";
						eval(scriptOnload + "();");
					}
					else if (newScript.readyState == 'loaded') {
						newScript.onreadystatechange = "";
						eval(scriptOnload + "();");
					}
				}
			}
			else{
				// most browsers
				newScript.onload = function() {
					eval(scriptOnload + "();");
				}
			}
		}
		newScript.type = scriptType;
		newScript.src = scriptPath;
		newScript.id = scriptID;
		
		return newScript;

	}
	
	this.createLink = function(linkHref,linkRel,linkType,linkID){
			
		if ( typeof(linkRel) == "undefined" || linkRel == null ) {
			var linkRel = 'stylesheet'; 
		}
		
		if ( typeof(linkType) == "undefined" || linkType == null ) {
			var linkType = 'text/css'; 
		}
		
		if ( typeof(linkHref) == "undefined" || linkHref == null ) {
			var linkHref = ''; 
		}
		
		if ( typeof(linkID) == "undefined" || linkID == null ) {
			var linkID = ''; 
		}
		
		var newLink = this.DHTMLWindow.createElement('link');
		
		newLink.rel = linkRel;
		newLink.type = linkType;
		newLink.href = linkHref;
		newLink.id = linkID;
		
		return newLink;

	}
	
	this.createFileField = function(fileFieldID,fileFieldSize){
		
		var newFileField = this.DHTMLWindow.createElement('input');
		
		newFileField.type = "file";
		newFileField.name = fileFieldID;
		newFileField.id = fileFieldID;
		newFileField.size = fileFieldSize;
		
		return newFileField;
	}
	
	this.appendReturn = function(childElement,parentElement,IEinner){
		
		/*if our parentElement is the object itself*/
		if (typeof(parentElement.length) == "undefined") {
			
			parentElement.appendChild(childElement);
			
			return childElement;
		}
		/*or if its a string of the existing elements ID*/
		else if ( parentElement.length > 0 ) {
			
			this.DHTMLWindow.getElementById(parentElement).appendChild(childElement);
			
			/*IE DOM bug for events...*/
			if(document.all){
				if(IEinner==1){
					this.DHTMLWindow.getElementById(parentElement).innerHTML = this.DHTMLWindow.getElementById(parentElement).innerHTML;
				}
			}
			
			return childElement;
		}
		else{
			return childElement;
		}
	}
	
}