// creates a hidden form field
function createHidden(name, val)
{
	var el = document.createElement("input");
	el.setAttribute("name", name);
	el.setAttribute("id", name);	
	el.setAttribute("type", "hidden");
	if (val != null) el.value = val;
	return el;
}

// creates a select box
function createSelect(name, width)
{
	var el = document.createElement("select");
	el.style.width = width;
	el.setAttribute("name", name);
	el.setAttribute("id", name);	
	return el;
}

// appends an option to a select box
function appendOption(sel, txt, val, selected)
{
	var el = new Option(txt, val, selected);
	
    if (document.all)
    {    
		sel.options.add(el); // use non DOM-compliant method
		if (selected) el.selected = true;	// workaround for IE6 - see http://support.microsoft.com/default.aspx?scid=kb;en-us;298978&Product=iep
	}
    else
    {
	    sel.add(el, null); // DOM-compliant method
    }
}

// creates a text input field
function createInput(name, width, val)
{
	var el = document.createElement("input");
	el.style.width = width;
	el.setAttribute("name", name);
	el.setAttribute("id", name);	
	el.setAttribute("type", "text");
	if (val != null) el.value = val;
	return el;
}

// creates a text area
function createTextarea(name, width, height, val)
{
	var el = document.createElement("textarea");
	el.style.width = width;
	el.style.height = height;
	el.setAttribute("name", name);
	el.setAttribute("id", name);	
	if (val != null) 
	{
		var txt = document.createTextNode(val);
		el.appendChild(txt);
	}
	return el;
}

// creates a div
function createDiv(cls, id)
{
	var el = document.createElement("div");
	el.className = cls;
	if (id != null) el.setAttribute("id", id);
	return el;
}

// creates an anchor
function createAnchor(cls, href, onclick, txt, title, rel)
{
	var el = document.createElement("a");
	el.href = href;
	el.onclick = function () { return eval(onclick) };
	el.setAttribute("title", title);	
	el.setAttribute("rel", rel);
	var lbl = document.createTextNode(txt);
	el.appendChild(lbl);
	
	if (cls != null)
	{
		// wrap a div around the anchor with the given class
		var div = createDiv(cls);
		div.appendChild(el);
		return div;
	}
	else
	{	
		return el;
	}
}

function createAnchorHelpPopup(cls, href, onclick, txt, title, rel)
{
	var el = document.createElement("a");
	
	el.setAttribute("title", title);	
	el.onclick = function () { return GB_showCenter('Help', this.href) };	
	el.href = href;
	
	var lbl = document.createTextNode(txt);
	el.appendChild(lbl);
	
	if (cls != null)
	{
		// wrap a div around the anchor with the given class
		var div = createDiv(cls);
		div.appendChild(el);
		return div;
	}
	else
	{	
		return el;
	}
}

// creates an image link
function createImageAnchor(href, onclick, src, alt)
{
	var el = document.createElement("a");
	el.href = href;
	el.onclick = function () { return eval(onclick) };
	var img = document.createElement("img");
	img.src = src;
	img.setAttribute("alt", alt);
	el.appendChild(img);
	
	return el;
}

// creates a span to tell the user that a field is mandatory
function createMandatorySpan()
{
	var el = document.createElement("span");
	var lbl = document.createTextNode("*");
	el.appendChild(lbl);
	el.className = "denote";
	return el
}

// inserts a populated text entry into the DOM
function AddPopulated(parent, placeholder_to_insert_before, label, name, val, liclss, custom_tag, instruct)
{
	var li = document.createElement("li");
	li.setAttribute("custom", custom_tag);
	if (liclss != null) li.className = liclss;
	var div1 = createDiv("what");
	var lbl = document.createTextNode(label);
	div1.appendChild(lbl);
	li.appendChild(div1);
	var div2 = createDiv("populated");
	var lbl2 = document.createTextNode(val);
	div2.appendChild(lbl2);
	li.appendChild(div2);	
	if (instruct)
	{
		// note that instruct should be a DOM node (this allows for text / links etc)
		var div3 = createDiv("instruct");
		div3.appendChild(instruct);
		li.appendChild(div3);	
	}	
	parent.insertBefore(li, placeholder_to_insert_before);
}

// inserts a hidden form field into the DOM
function AddHiddenField(parent, name, val, custom_tag)
{
	var h = createHidden(name, val);
	h.setAttribute("custom", custom_tag);	
	parent.appendChild(h);
}

// inserts a text form field into the DOM
function AddField(parent, placeholder_to_insert_before, label, name, val, mandatory, liclss, custom_tag, instruct)
{
	var li = document.createElement("li");
	li.setAttribute("custom", custom_tag);
	if (liclss != null) li.className = liclss;
	var div1 = createDiv("what");
	var lbl = document.createTextNode(label);
	div1.appendChild(lbl);
	if (mandatory) div1.appendChild(createMandatorySpan());
	li.appendChild(div1);
	var div2 = createDiv("input");
	var ip = createInput(name, "195px", val);
	div2.appendChild(ip);
	li.appendChild(div2);	
	if (instruct)
	{
		// note that instruct should be a DOM node (this allows for text / links etc)
		var div3 = createDiv("instruct");
		div3.appendChild(instruct);
		li.appendChild(div3);	
	}	
	parent.insertBefore(li, placeholder_to_insert_before);
	
}

// inserts a textarea form field into the DOM
function AddTxtField(parent, placeholder_to_insert_before, label, name, val, mandatory, liclss, custom_tag, instruct)
{
	var li = document.createElement("li");
	li.setAttribute("custom", custom_tag);
	if (liclss != null) li.className = liclss;
	var div1 = createDiv("what");
	var lbl = document.createTextNode(label);
	div1.appendChild(lbl);
	if (mandatory) div1.appendChild(createMandatorySpan());
	li.appendChild(div1);
	var div2 = createDiv("input");
	var ip = createTextarea(name, "194px", "64px", val);
	div2.appendChild(ip);
	li.appendChild(div2);
	if (instruct)
	{
		// note that instruct should be a DOM node (this allows for text / links etc)
		var div3 = createDiv("instruct");
		div3.appendChild(instruct);
		li.appendChild(div3);	
	}	
	parent.insertBefore(li, placeholder_to_insert_before);
}

// inserts a select form field into the DOM
function AddSelect(parent, placeholder_to_insert_before, label, select, mandatory, custom_tag, instruct)
{
	var li = document.createElement("li");
	li.setAttribute("custom", custom_tag);
	var div1 = createDiv("what");
	var lbl = document.createTextNode(label);
	div1.appendChild(lbl);
	if (mandatory) div1.appendChild(createMandatorySpan());
	li.appendChild(div1);
	var div2 = createDiv("input");
	div2.appendChild(select);
	li.appendChild(div2);
	if (instruct)
	{
		// note that instruct should be a DOM node (this allows for text / links etc)
		var div3 = createDiv("instruct");
		div3.appendChild(instruct);
		li.appendChild(div3);	
	}	
	parent.insertBefore(li, placeholder_to_insert_before);
}

// inserts a checkbox form field into the DOM
function AddCheckbox(parent, placeholder_to_insert_before, label, name, onclick, custom_tag, instruct)
{
	var li = document.createElement("li");
	li.setAttribute("custom", custom_tag);
	var div1 = createDiv("what");
	li.appendChild(div1);
	var div2 = createDiv("input");
	var chk = document.createElement("input");
	chk.setAttribute("type", "checkbox");
	chk.setAttribute("id", name);
	chk.setAttribute("name", name);
	//chk.name = name;
	chk.setAttribute("value", 1);
	chk.onclick = function () { eval(onclick) };
	div2.appendChild(chk);
	var sp = document.createTextNode(" ");
	div2.appendChild(sp);
	var lbl = document.createElement("label");
	if (document.all)
		lbl.htmlFor = name;	//IE doesn't set for attribute properly using setAttribute
	else	
		lbl.setAttribute("for", name); // DOM compliant browsers
	var txt = document.createTextNode(label);
	lbl.appendChild(txt);
	div2.appendChild(lbl);	
	li.appendChild(div2);
	if (instruct)
	{
		// note that instruct should be a DOM node (this allows for text / links etc)
		var div3 = createDiv("instruct");
		div3.appendChild(instruct);
		li.appendChild(div3);	
	}		
	parent.insertBefore(li, placeholder_to_insert_before);
}

// inserts a title into the DOM
function AddTitle(parent, placeholder_to_insert_before, label, htag, custom_tag)
{
	var li = document.createElement("li");
	li.setAttribute("custom", custom_tag);
	li.className = "header";
	var h = document.createElement(htag);
	var lbl = document.createTextNode(label);
	h.appendChild(lbl);
	li.appendChild(h);
	parent.insertBefore(li, placeholder_to_insert_before);
}

// Inserts a tell user instruction into the DOM
function AddTellUser(parent, placeholder_to_insert_before, label, custom_tag)
{
	var li = document.createElement("li");
	li.setAttribute("custom", custom_tag);
	var div1 = createDiv("tell-user");
	var lbl = document.createTextNode(label);
	div1.appendChild(lbl);
	li.appendChild(div1);
	parent.insertBefore(li, placeholder_to_insert_before);
}

// Inserts links that allow a form to be added or deleted into the DOM
function AddAddDeleteFormLinks(parent, placeholder_to_insert_before, nFormsAdded, custom_tag)
{
	var li = document.createElement("li");
	li.setAttribute("custom", custom_tag);
	var div1 = createDiv("add-another");
	var add = createAnchor(null, "#", "AddAnotherForm(null)", "+ Add Another");
	var del = createAnchor(null, "#", "DeleteForm(" + nFormsAdded + ")", "- Delete Entry");
	
	div1.appendChild(add);
	div1.appendChild(del);
	li.appendChild(div1);
	parent.insertBefore(li, placeholder_to_insert_before);
}

