/* =====================================================================
AUTHOR: Vince Lee - Forte Trinity
10/09/07 - fiddled to make more general functions - some now deprecated
10/09/07 - put in IE shittiness workarounds
===================================================================== */
/* =====================================================================
Form Creation Functions
===================================================================== */

function addForm(parentElem, formname, method, action, submitFunction) {
	var elem = document.getElementById(parentElem);
	var newelem = document.createElement('form');
	if((formname != "")&&(formname != null)) {newelem.setAttribute('id', formname);}
	if((formname != "")&&(formname != null)) {newelem.setAttribute('name', formname);}
	//if((submitFunction != "")&&(submitFunction != null)) {newelem.setAttribute('onsubmit', submitFunction);}
	if((method != "")&&(method != null)) {		newelem.setAttribute('method', method);	}
	if((action != "")&&(action != null)) {		newelem.setAttribute('action', action);	}
	elem.appendChild(newelem);	
}

function addFormElem(parentElem, newid, type, name, value, src, formElemType, innerHTML, alt, classname) {
	
	var elem = document.getElementById(parentElem);
	var newelem = document.createElement(formElemType);
	if((newid != "")&&(newid != null)) { newelem.setAttribute('id', newid); }
	if((type != "")&&(type != null)) { newelem.setAttribute('type', type); }
	if((name != "")&&(name != null)) { newelem.setAttribute('name', name); }
	if((value != "")&&(value != null)) { newelem.setAttribute('value', value); }
	if((classname != "")&&(classname != null)) { newelem.setAttribute('class', classname); }
	if((src != "")&&(src != null)) { newelem.setAttribute('src', src); }
	if((innerHTML != "")&&(innerHTML != null)) { newelem.innerHTML = innerHTML; }
	if((alt != "")&&(alt != null)) { newelem.setAttribute('alt', alt); }
	elem.appendChild(newelem);	
}

function addCheckBox(parentElem, checkboxname, labelText) {
	addLabel(parentElem, checkboxname, labelText);
	addFormElem(parentElem, checkboxname, "checkbox", checkboxname, "", "", "input", "");
}

function addSelect(parentElem, selectName, optionLabelsArray) {
	addFormElem(parentElem, selectName, "", selectName, "", "", "select", "", "");
	
	//alert("total options: " + optionLabelsArray.length);
	
	for(i = 0; i < optionLabelsArray.length; i++) {
		addFormElem(selectName, "", "", "", optionLabelsArray[i][0], "", "option", optionLabelsArray[i][1], "")
	}
}

function hideElement(elemID) {
	//alert("hiding: " + elemID);
	if(elem = document.getElementById(elemID)) {
		elem.style.visible = "hidden";
	}
}

function showElement(elemID) {
	//alert(elemID);
	if(elem = document.getElementById(elemID)) {
		elem.style.display = "visible";
	}
}

function destroyElement(elemToDestroy) {
	if(elem = document.getElementById(elemToDestroy)) {
		elem.parentNode.removeChild(elem);
	}
}
/* =====================================================================
Radio Buttons
===================================================================== */
function addRadioButtons(radiogroupname, fieldnameArray, labelArray) {
	
	addBasicElem(radiogroupname + "_li", "ul", radiogroupname + "_ul");
	
	for(i = 0; i < fieldnameArray.length; i++) {
		addBasicElem(radiogroupname + "_ul", "li", fieldnameArray[i] + "_li");
		
		addLabel(fieldnameArray[i] + "_li", fieldnameArray[i], labelArray[i]);
	
		var elem = document.getElementById(fieldnameArray[i] + "_li");
	
		/*var newelem = document.createElement('input');
		newelem.setAttribute('type', 'radio');
		newelem.setAttribute('name', radiogroupname + "_radio");
		newelem.setAttribute('value', labelArray[i]);
		elem.appendChild(newelem);*/
		
		try{
			rdo = document.createElement('<input type="radio" name="' + radiogroupname + '"_radio" value="' + labelArray[i] + '"/>');
		}catch(err){
			rdo = document.createElement('input');
			rdo.setAttribute('type','radio');
			rdo.setAttribute('name',radiogroupname + '"_radio"');
			rdo.setAttribute('value',labelArray[i]);
		}
		
		elem.appendChild(rdo);
		
	}
}
function addButton(parentElem, value, nextFunction, newid) {
	var elem = document.getElementById(parentElem);
	var newelem = document.createElement('input');
	newelem.setAttribute('type', 'button');
	newelem.setAttribute('value', value);
	newelem.setAttribute('id', newid);
	//newelem.setAttribute('onclick', nextFunction);
	eventAdder(newelem,"onclick",nextFunction);
	elem.appendChild(newelem);	
}

function getCheckedValue(radioObj) { 
	if(!radioObj) return ""; 
	
	var radioLength = radioObj.length; 
	if(radioLength == undefined) 
		if(radioObj.checked) 
		return radioObj.value; 
	else return ""; 
	
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) { 
			return radioObj[i].value; 
		} 
	} 
	return ""; 
}

/* =====================================================================
IE Workaround functions
===================================================================== */
function eventAdder(objAttrib,handler,addFunction) { 
	if ((!document.all)&&(document.getElementById)) { 
		objAttrib.setAttribute(handler,addFunction); 
	} 
	
	//workaround for IE 5.x 
	if ((document.all)&&(document.getElementById)) { 
		objAttrib[handler] = new Function(addFunction); 
	} 
}
/* =====================================================================
Deprecated functions
===================================================================== */
function addBasicElem(parentElem, elemType, newid, innerHTML) {
	addFormElem(parentElem, newid, "", "", "", "", elemType, innerHTML);
}
function addInputElem(parentElem, newid, type, name, value, src) {
	addFormElem(parentElem, newid, type, name, value, src, "input");
	//parentElem, newid, type, name, value, src, formElemType, innerHTML
}
function addLabel(parentElem, fieldname, labelText) {
	addBasicElem(parentElem, "label", "", labelText);	
}

function displayLoader(stepNo) {
	var elem;
	if(elem = document.getElementById("loading" + stepNo)) {
		elem.style.display = "block";	
	}else {
		addFormElem("step" + stepNo, "loading" + stepNo, "", "", "", "images/loadingAnim.gif", "img", "", "loading, please wait");	
	}
}
function hideLoader(stepNo) {
	var elem = document.getElementById("loading" + stepNo);
	elem.style.display = "none";	
}