var xmlDoc;
var form1 = "";
var validationArray = new Array();
var dtCh= "/";
var minYear=1900;
var maxYear=2100;
////////////////////////////////////////////////////////////////////////////////
// Read XML File
function importXML() {
	validationArray = ","
	document.getElementById("ctl00_Cph1_btnSubmit").disabled = true;
	if (document.implementation.createDocument) {
		xmlDoc = document.implementation.createDocument("", "", null);
		xmlDoc.onload = buildForm;
		}
	else if (window.ActiveXObject) {
		xmlDoc = new ActiveXObject("MSXML2.DOMDocument");
		xmlDoc.onreadystatechange = function () {if (xmlDoc.readyState == 4) buildForm()};
		}
	else {
		alert('Your browser can\'t handle this script');
		return;
		}
	xmlDoc.async=false;
	xmlDoc.load(strFile);
}
importXML();
////////////////////////////////////////////////////////////////////////////////
// Build Form
function buildForm() {
	var form1 = "";
	var x = xmlDoc.getElementsByTagName("FormBuilder");
	var tbl = "<table cellpadding=2 cellspacing=0 border=0 width='98%'>";
	if (x.length > 0) {
		for (i=0;i < x.length; i++) {
			for (j=0;j < x[i].childNodes.length; j++) {
				if (x[i].childNodes[j].nodeName != '#text') {
			
//form1 += "<tr><td colspan=2 class='dvHeading'>" + switchChars(x[i].childNodes[j].getAttribute('text')) + "<hr /></td></tr>";
					for (r=0; r < x[i].childNodes[j].childNodes.length; r++) {
						if (x[i].childNodes[j].childNodes[r].nodeType != 1) continue;
						switch(x[i].childNodes[j].childNodes[r].getAttribute('type')) {
								case "input":
									form1 += "<tr><td class='dvPer'>" + switchChars(x[i].childNodes[j].childNodes[r].getAttribute('label')) + ": </td>";
									form1 += "<td>" + buildInput(x[i].childNodes[j].childNodes[r].getAttribute('id'),x[i].childNodes[j].childNodes[r].getAttribute('size'),switchChars(x[i].childNodes[j].childNodes[r].getAttribute('value')),x[i].childNodes[j].childNodes[r].getAttribute('validate'),x[i].childNodes[j].childNodes[r].getAttribute('min'),x[i].childNodes[j].childNodes[r].getAttribute('max'),switchChars(x[i].childNodes[j].childNodes[r].getAttribute('label'))) + "</td></tr>";
									break;
								case "pwd":
									form1 += "<tr><td class='dvPer'>" + switchChars(x[i].childNodes[j].childNodes[r].getAttribute('label')) + ": </td>";
									form1 += "<td>" + buildPwd(x[i].childNodes[j].childNodes[r].getAttribute('id'),x[i].childNodes[j].childNodes[r].getAttribute('size'),switchChars(x[i].childNodes[j].childNodes[r].getAttribute('value')),x[i].childNodes[j].childNodes[r].getAttribute('validate'),x[i].childNodes[j].childNodes[r].getAttribute('min'),x[i].childNodes[j].childNodes[r].getAttribute('max'),switchChars(x[i].childNodes[j].childNodes[r].getAttribute('label'))) + "</td></tr>";
									break;
								case "dropdown":
									form1 += "<tr><td class='dvPer' valign='top'>" + switchChars(x[i].childNodes[j].childNodes[r].getAttribute('label')) + ": </td>";
									form1 += "<td valign='top'>" + buildDropDown(x[i].childNodes[j].childNodes[r].getAttribute('node'),x[i].childNodes[j].childNodes[r].getAttribute('id'),switchChars(x[i].childNodes[j].childNodes[r].getAttribute('value')),x[i].childNodes[j].childNodes[r].getAttribute('size'),x[i].childNodes[j].childNodes[r].getAttribute('multiple'),switchChars(x[i].childNodes[j].childNodes[r].getAttribute('label')),x[i].childNodes[j].childNodes[r].getAttribute('validate')) + "</td></tr>";
									break;
								case "textarea":
									form1 += "<tr><td class='dvPer' valign='top'>" + switchChars(x[i].childNodes[j].childNodes[r].getAttribute('label')) + ": </td>";
									form1 += "<td valign='top'>" + buildTextArea(x[i].childNodes[j].childNodes[r].getAttribute('id'),x[i].childNodes[j].childNodes[r].getAttribute('width'),x[i].childNodes[j].childNodes[r].getAttribute('height'),switchChars(x[i].childNodes[j].childNodes[r].getAttribute('label')),switchChars(x[i].childNodes[j].childNodes[r].getAttribute('value'))) + "</td></tr>";
									break;
								case "usphone":
									form1 += "<tr><td class='dvPer'>" + switchChars(x[i].childNodes[j].childNodes[r].getAttribute('label')) + ": </td>";
									form1 += "<td>" + buildUSPhone(switchChars(x[i].childNodes[j].childNodes[r].getAttribute('label')),switchChars(x[i].childNodes[j].childNodes[r].getAttribute('value')),x[i].childNodes[j].childNodes[r].getAttribute('validate')) + "</td></tr>";
									break;
								case "date":
									form1 += "<tr><td class='dvPer'>" + switchChars(x[i].childNodes[j].childNodes[r].getAttribute('label')) + ": </td>";
									form1 += "<td>" + buildDate(x[i].childNodes[j].childNodes[r].getAttribute('id'),switchChars(x[i].childNodes[j].childNodes[r].getAttribute('label')),switchChars(x[i].childNodes[j].childNodes[r].getAttribute('value')),x[i].childNodes[j].childNodes[r].getAttribute('validate')) + "</td></tr>";
									break;
								case "radio":
									form1 += "<tr><td class='dvPer' colspan=2 style='text-align:left'>" + switchChars(x[i].childNodes[j].childNodes[r].getAttribute('label')) + "</td></tr>";
									form1 += "<tr><td class='dvPer'>&nbsp;</td>";
									form1 += "<td valign='top'  style='text-align:left; padding-left:20px;'>" + buildRadios(x[i].childNodes[j].childNodes[r].getAttribute('node'),x[i].childNodes[j].childNodes[r].getAttribute('cols'),switchChars(x[i].childNodes[j].childNodes[r].getAttribute('value')),x[i].childNodes[j].childNodes[r].getAttribute('node')) + "</td></tr>";
									break;
								case "checkbox":
									form1 += "<tr><td class='dvPer'>&nbsp;</td>";
									form1 += "<td valign='top' style='text-align:left; padding-left:20px;'>" + buildCheckBox(x[i].childNodes[j].childNodes[r].getAttribute('node'),x[i].childNodes[j].childNodes[r].getAttribute('cols'),x[i].childNodes[j].childNodes[r].getAttribute('rows')) + "</td></tr>";
									break;
							}
						}
					}
				}
			}
		}
	document.getElementById("dvForm").innerHTML += tbl + form1 + "</table><br />";
}
////////////////////////////////////////////////////////////////////////////////
// Build Form Element Functions
function buildCheckBox(nde,cols,rows) {
	var p1 = "<table border=0 cellpadding=0 cellspacing=0 width='100%'><tr><td style='width:50%;'>";
	var g = 0, h = 0, splt = 0;
	var ck = false;
	var xf = xmlDoc.getElementsByTagName(nde);
	if (xf.length > 0) {
		for (g=0;g < xf.length; g++) {
			for (h=0;h < xf[g].childNodes.length; h++) {
				if (xf[g].childNodes[h].nodeType != 1) continue;
				if (cols > 0)
					splt = (xf[g].childNodes.length / 2);
				if (cols > 0 && h > 0 && h == Math.round(splt))
					p1 += "</td><td class='dvPer' valign='top' style='width:50%; text-align:left; padding-left:20px;'>";
				p1 += "<input type='checkbox' value='" + xf[g].childNodes[h].getAttribute("value") + "' id='" + xf[g].childNodes[h].getAttribute("id") + "' name='" + xf[g].childNodes[h].getAttribute("id") + "'";
				if (h == xf[g].childNodes.length - 1 && ck == false)
					p1 += " checked"
				else {
					if (xf[g].childNodes[h].getAttribute("checked") == "true") {
						p1 += " checked";
						ck = true;
						}
					}
					p1 += "> " + xf[g].childNodes[h].getAttribute("label") + "<br>";
				}
			}
		}
	return p1 + "</td></tr></table>";
}

function buildRadios(nde,cols,val,group) {
	var p1 = "<table border=0 cellpadding=0 cellspacing=0 width='100%'><tr><td style='width:50%;'>";
	var g = 0, h = 0, splt = 0;
	var ck = false;
	var xf = xmlDoc.getElementsByTagName(nde);
	if (xf.length > 0) {
		for (g=0;g < xf.length; g++) {
			for (h=0;h < xf[g].childNodes.length; h++) {
				if (xf[g].childNodes[h].nodeType != 1) continue;
				if (cols > 0)
					splt = (xf[g].childNodes.length / 2);
					if (cols > 0 && h > 0 && h == Math.round(splt))
						p1 += "</td><td class='dvPer' valign='top'  style='width:50%;text-align:left; padding-left:20px;'>";
					p1 += "<input type='radio' value='" + xf[g].childNodes[h].getAttribute("value") + "' name='" + group + "'";
					if (h == xf[g].childNodes.length - 1 && ck == false)
						p1 += " checked"
					else {
						if (xf[g].childNodes[h].getAttribute("value") == val) {
							p1 += " checked";
							ck = true;
							}
						}
					p1 += "> " + xf[g].childNodes[h].getAttribute("text") + "<br>";
				}
			}
		}
	return p1 + "</td></tr></table>";
}

function buildInput(id,width,value,validate,min,max,label) {
	if (validate.length > 0) {
		validationArray += id + ","
		return "<input type='text' name='"+ id +"' id='"+ id +"' size='" + width + "' value='" + value + "' onblur=\"" + validate + "('" + id + "'," + min + "," + max + ",'" + label + "');\">&nbsp;<span id='" + label + "' class='dvPer error'>*</span>";
		}
	else
		return "<input type='text' name='"+ id +"' id='"+ id +"' size='" + width + "' value='" + value + "'>";
}

function buildPwd(id,width,value,validate,min,max,label) {
	if (validate.length > 0) {
		validationArray += id + ","
		return "<input type='password' name='"+ id +"' id='"+ id +"' size='" + width + "' value='" + value + "' onblur=\"" + validate + "('" + id + "'," + min + "," + max + ",'" + label + "');\">&nbsp;<span id='" + label + "' class='dvPer error'>*</span>";
		}
	else
		return "<input type='password' name='"+ id +"' id='"+ id +"' size='" + width + "' value='" + value + "'>";
}

function buildTextArea(id,width,height,label,value) {
	return "<textarea name='"+ id +"' id='"+ id +"' rows='" + height + "' cols='" + width + "'>" + value + "</textarea>";
}

function buildUSPhone(label,value,validate) {
	var strPhone = "";
	if (value == "")
		value = "---";
	value = value.split("-");
	if (validate > 0) {
		validationArray += "txtAC,txtPrefix,txtSuffix,"
		strPhone = "<input name='txtAC' id='txtAC' type='input' size='2' onblur=\"validateInt('txtAC',3,3,'"+ label +"')\" value='" + value[0] + "'>&nbsp;";
		strPhone += "<input name='txtPrefix' id='txtPrefix' type='input' size='2' onblur=\"validateInt('txtPrefix',3,3,'"+ label +"')\" value='" + value[1] + "'>&nbsp;";
		strPhone += "<input name='txtSuffix' id='txtSuffix' type='input' size='3' onblur=\"validateInt('txtSuffix',4,4,'"+ label +"')\" value='" + value[2] + "'>&nbsp;<span id='" + label + "' class='dvPer error'>*</span>";
		}
	else {
		strPhone = "<input name='txtAC' id='txtAC' type='input' size=2 value='" + value[0] + "'>&nbsp;";
		strPhone += "<input name='txtPrefix' id='txtPrefix' type='input' size='2' value='" + value[1] + "'>&nbsp;";
		strPhone += "<input name='txtSuffix' id='txtSuffix' type='input' size='3' value='" + value[2] + "'>";
		}
	return strPhone;
}

function buildDropDown(nde,id,val,min,multi,label,validate) {
	var p2 = "", g = 0, h = 0;
	p2 = "<select id='"+ id +"' name='" + id + "' size='" + min + "'";
	if (multi > 0) {
		p2 += " multiple";
		}
	if (validate.length > 0) {
		validationArray += id + ","
		p2 += " onChange=\"" + validate + "('" + id + "','" + label + "');\"";
		}
	p2 += ">";
	var xf = xmlDoc.getElementsByTagName(nde);
	if (xf.length > 0) {
		for (g=0;g < xf.length; g++) {
			for (h=0;h < xf[g].childNodes.length; h++) {
				if (xf[g].childNodes[h].nodeType != 1) continue;
				p2 += "<option value='" + xf[g].childNodes[h].getAttribute("value") + "'";
				if (h > 0) {
					if (val.indexOf(xf[g].childNodes[h].getAttribute("value")) != -1)
						p2 += " selected";
					}
				p2 += ">";
				if (xf[g].childNodes[h].getAttribute("text") == "") {
					p2 += xf[g].childNodes[h].getAttribute("value")
					}
				else {
					p2 += xf[g].childNodes[h].getAttribute("text")
					}
				p2 +="</option>";
				}
			}
		}
	p2 += "</select>";
	if (validate.length > 0)
		p2 += "&nbsp;<span id='" + label + "' class='dvPer error'>*</span>"
	return p2
}

function buildDate(id,label,value,validate) {
	var dte = "";
	dte = "<input id='" + id + "' name='" + id + "' size=7";
	if (validate > 0) {
		validationArray += id + ","
		dte += " onblur=\"isDate('" + id + "','" + label + "');\""
		}
	dte += " value='" + value + "'>&nbsp;<span id='" + label + "' class='dvPer error'>*</span>";
	return dte;
}

//////////////////////////////////////////////////////////////////////////
// Function to replace characters removed from XML Doc
function switchChars(val) {
	val = val.replace("[chr(38)]", "&");
	val = val.replace("[chr(34)]", "\"");
	val = val.replace("[chr(60)]", "<");
	val = val.replace("[chr(62)]", ">");
	val = val.replace("[chr(47)]", "/");
	return val;
}
function addtoArray(id) {
	if (validationArray.indexOf("," + id + ",") == -1)
		validationArray += id + ",";
}
function removeFromArray(id) {
	if (validationArray.indexOf("," + id + ",") != -1)
			validationArray = validationArray.replace(id + ",", "");
}
function checkValidationArray() {
	if (validationArray.length == 1)
		document.getElementById("ctl00_Cph1_btnSubmit").disabled = false;
	else
		document.getElementById("ctl00_Cph1_btnSubmit").disabled = true;
}
