/*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){
		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 = ''; 
		}
		
		htmlDiv.className = divClass;
		htmlDiv.id = divID;
		if(divStyle.length > 0){
			htmlDiv.className = divStyle;
		}
		htmlDiv.onmouseup = mouseup;

		return this.appendReturn(htmlDiv,parentElement,0);
	}
	
	/*create a basic HTML check box element*/
	this.createCheckBoxHTMLElement = function(checkBoxID,checkValue){
		var input = this.DHTMLWindow.createElement('INPUT');
		
		if (this.DHTMLWindow.all) {
			// IE
			input.type = "checkbox";
			input.name = checkBoxID;
			input.id = checkBoxID;
			input.value = checkValue;
		}
		else if (this.DHTMLWindow.getElementById) { 
			// FF
			input.setAttribute('type', 'checkbox');
			input.setAttribute('name', checkBoxID);
			input.setAttribute('id', checkBoxID);
			input.setAttribute('value', checkValue);
		}
		
		return input;
	}
	
	/*create a basic HTML check box element*/
	this.createButtonHTMLElement = function(buttonID,buttonValue,onClickValue,parentElement){
		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 (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;
		
		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, imgAlign, imgBorder, imgHspace){
	
		var newImage = this.DHTMLWindow.createElement('IMG');
	
		if (document.all) {
			// IE
			newImage.src = imagePath;
			newImage.height = imgHeight;
			newImage.width = imgWidth;
			newImage.align = imgAlign;
			newImage.border = imgBorder;
			newImage.hspace = imgHspace;
		}
		else if (document.getElementById) { 
			// FF
			newImage.setAttribute('src', imagePath);
			newImage.setAttribute('height', imgHeight);
			newImage.setAttribute('width', imgWidth);
			newImage.setAttribute('align', imgAlign);
			newImage.setAttribute('border', imgBorder);
			newImage.setAttribute('hspace', imgHspace);
		}
		
		return newImage;
	}
	
	/*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){
		
		if ( typeof(scriptType) == "undefined" || scriptType == null ) {
			var scriptType = ''; 
		}
		
		if ( typeof(scriptPath) == "undefined" || scriptPath == null ) {
			var scriptPath = ''; 
		}
		
		if ( typeof(scriptOnload) == "undefined" || scriptOnload == null ) {
			var scriptOnload = ''; 
		}
		
		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...*/
		
		
		// most browsers
/*		newScript.onload = eval(scriptOnload);
		// IE 
		newScript.onreadystatechange = function() {alert(newScript.readyState);
			if (newScript.readyState == 'complete') {
				eval(scriptOnload + "()");
			}
			else if (newScript.readyState == 'loaded') {
				eval(scriptOnload + "()");
			}
		}*/
		
		newScript.type = scriptType;
		newScript.src = scriptPath;
		
		return newScript;

	}
	
	this.createLink = function(linkHref,linkRel,linkType){
			
		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 = ''; 
		}
		
		var newLink = this.DHTMLWindow.createElement('link');
		
		newLink.rel = linkRel;
		newLink.type = linkType;
		newLink.href = linkHref;
		
		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;
		}
	}
	
}