/*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');
		
		/*argCount is the maximum number of arguments for this function*/
		var args = this.setDefaults(arguments,5);
		
		input.type = "text";
		input.name = args[0];
		input.id = args[0];
		input.size = args[2];
		input.value = args[1];
		input.maxlength = args[3];

		return this.appendReturn(input,args[4],0);
	}
	
	/*create a caption for HTML fields*/
	this.createHTMLFieldCaption = function(textFieldCaption,parentElement){
				
		/*argCount is the maximum number of arguments for this function*/
		var args = this.setDefaults(arguments,2);
		
		var fieldCaption = this.DHTMLWindow.createTextNode(args[0]);
		
		return this.appendReturn(fieldCaption,args[1],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');
		
		/*argCount is the maximum number of arguments for this function*/
		var args = this.setDefaults(arguments,3);
		
		if (this.DHTMLWindow.all) {
			// IE
			/*we only want the href attribute if one is defined, otherwise it might interfere with
			our onclick attribute*/
			if(args[0].length > 0){
				newLink.href = args[0];
			}
			
			if(linkUrl.args[2] > 0){
				newLink.attachEvent("onclick", args[2]);
			}
		}
		else if (this.DHTMLWindow.getElementById) { 
			// FF
			if(args[0].length > 0){
				newLink.setAttribute('href', args[0]);
			}
			
			if(args[2].onClickEvent > 0){
				newLink.setAttribute('onclick', args[2]);
			}
		}
		
		newLink.appendChild(this.DHTMLWindow.createTextNode(args[1]));
		
		return newLink;
	}
	
	/*create a div*/
	this.createDivHTMLElement = function(divID,divClass,divStyle,mouseup, parentElement,onClickValue,divContent){
		var htmlDiv = this.DHTMLWindow.createElement('div');
		
		/*argCount is the maximum number of arguments for this function*/
		var args = this.setDefaults(arguments,7);
		
		if (this.DHTMLWindow.all) {
			htmlDiv.onclick = args[5];
		}
		else if (this.DHTMLWindow.getElementById) { 
			htmlDiv.setAttribute('onclick', args[5]);
		}
		
		htmlDiv.className = args[1];
		htmlDiv.id = args[0];
		if(divStyle.length > 0){
			htmlDiv.className = args[2];
		}
		htmlDiv.onmouseup = args[3];
		htmlDiv.innerHTML = args[6];

		return this.appendReturn(htmlDiv,parentElement,1);
	}
	
	/*create a span*/
	this.createSpanHTMLElement = function(spanID,spanClass,spanStyle,mouseup, parentElement,onClickValue,spanContent){
		var htmlSpan = this.DHTMLWindow.createElement('span');
		
		/*argCount is the maximum number of arguments for this function*/
		var args = this.setDefaults(arguments,7);
		
		if (this.DHTMLWindow.all) {
			htmlSpan.onclick = args[5];
		}
		else if (this.DHTMLWindow.getElementById) { 
			htmlSpan.setAttribute('onclick', args[5]);
		}
		
		htmlSpan.className = args[1];
		htmlSpan.id = args[0];
		if(divStyle.length > 0){
			htmlSpan.className = args[2];
		}
		htmlSpan.onmouseup = args[3];
		htmlSpan.innerHTML = args[6];

		return this.appendReturn(htmlSpan,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');
		
		/*argCount is the maximum number of arguments for this function*/
		var args = this.setDefaults(arguments,5);
		
		if (this.DHTMLWindow.all) {
			input.onclick = args[2];
		}
		else if (this.DHTMLWindow.getElementById) { 
			input.setAttribute('onclick', args[2]);
		}

		input.type = "button";
		input.name = args[0];
		input.id = args[0];
		input.value = args[1];
		input.className = args[4];
		
		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){
		
		/*argCount is the maximum number of arguments for this function*/
		var args = this.setDefaults(arguments,4);
		
		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 bug 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(args[2] + "();");
					}
					else if (newScript.readyState == 'loaded') {
						newScript.onreadystatechange = "";
						eval(args[2] + "();");
					}
				}
			}
			else{
				// most browsers
				newScript.onload = function() {
					eval(args[2] + "();");
				}
			}
		}
		newScript.type = args[0];
		newScript.src = args[1];
		newScript.id = args[3];
		
		return newScript;

	}
	
	this.createLink = function(linkHref,linkRel,linkType,linkID,parentElement){
			
		/*argCount is the maximum number of arguments for this function*/
		var args = this.setDefaults(arguments,5);
		
		var newLink = this.DHTMLWindow.createElement('link');
		
		newLink.href = args[0];
		newLink.rel = args[1];
		newLink.type = args[2];
		newLink.id = args[3];
		
		return this.appendReturn(newLink,args[4],0);

	}
	
	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){
		
		try{
			/*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;
			}
		}
		catch(e){
			alert(e);
		}
	}
	
	this.setDefaults = function(functionArgs,argCnt){
		for(i=0; i<argCnt; i++){
			if ( typeof(functionArgs[i]) == "undefined" || functionArgs[i] == null ) {
				functionArgs[i] = ""; 
			}
		}
		var returnArray = functionArgs;
		return returnArray;
	}
	
}