var form_loader = new YAHOO.util.YUILoader({
	require: ['selector', 'event-delegate'], 
	base: 'http://yui.yahooapis.com/2.8.0r4/build/',
	onSuccess: function() { formHandler(formDetails); },
	onFailure: function(o) {
		alert("error: " + YAHOO.lang.dump(o));
	}
});
form_loader.insert();

function formHandler(formConfig) {	
	inputFields = formConfig.textInputs;
	prevRadio = '';
	n = 1;
	var divInputFields = document.getElementById('input_fields');
	for (var i = 0; i < inputFields.length; i++) {
		var displayField = true;
		if (inputFields[i].type) {
			if (inputFields[i].type == 'break') {
				newBreak = newEl('br',[]);
				divInputFields.appendChild(newBreak);
				displayField = false;
			} 
			else if (inputFields[i].type == 'label') {
				newLabel = newEl('label', []);
				newLabel.innerHTML = inputFields[i].label;
				if (inputFields[i].name) {
					newLabel.setAttribute('for', 'inp_'+inputFields[i].name);
				}
				divInputFields.appendChild(newLabel);
				displayField = false;
			}
		}
		if (displayField) {
			newInput = newEl('input', [
				['id', 'inp_' + inputFields[i].name],
				['type', (inputFields[i].type?inputFields[i].type:'text')],
				['class', 'text'],
				['alt', inputFields[i].label],
				['name', inputFields[i].name],
				['value', inputFields[i].label],
				['style', (inputFields[i].width?'width:'+inputFields[i].width+'px':'')]
			]);
			newInput = divInputFields.appendChild(newInput);
			if (inputFields[i].type) {
				if (inputFields[i].type == 'radio') {
					if (prevRadio == inputFields[i].name) {
						n ++;
					} else {
						n = 1;
						prevRadio = inputFields[i].name;
						newInput.setAttribute("checked", true);
					}
					newInput.setAttribute('id', 'inp_' + inputFields[i].name + n);
					if (inputFields[i].value) 
						newInput.setAttribute('value', inputFields[i].value);
				}
				if (inputFields[i].type == 'checkbox' || 
					inputFields[i].type == 'radio') {
					newLabel = newEl('label', [
						['for', 'inp_'+inputFields[i].name+n]
					]);
					newLabel.innerHTML = inputFields[i].label;
					divInputFields.appendChild(newLabel);
				}
				if (inputFields[i].type == 'checkbox') {
					newInput.setAttribute('value', 'yes');
				}
			}
		}
	}
	iframeName = 'formloader';
	newIframe = newEl('iframe', [
		['name', 'formloader'],
		['id', iframeName],
		['style','display:none']
	]);
	document.body.appendChild(newIframe);
	
	var parentForm = document.getElementById('form_container');
	parentForm.setAttribute('action', "/form-handler.php?process" + "&sskey=" + formConfig.ssKey + "&wsid=" + formConfig.wsID);
	parentForm.setAttribute('target', iframeName);
	
	var submitButton = newEl('input', [
		['type',"submit"],
		['name',"submit"], 
		['value',formConfig.submitText],
		['class',"button"]
		
	]);
	parentForm.appendChild(submitButton);
	
	var onFieldClick = function(e, matchedEl, container) {
		if (matchedEl.value == matchedEl.getAttribute('alt') && matchedEl.getAttribute('type') == 'text') {
			matchedEl.value='';
		} 
	}
	var updateFields = function(ev, el) {
		if (el.value.length == 0) el.value = el.getAttribute('alt');
	}
	YAHOO.util.Event.delegate('input_fields','click', onFieldClick, 'input');
	YAHOO.util.Event.delegate('input_fields','focusout', updateFields, 'input');
}

function newEl(elTag, elAttribs) {
	var el = document.createElement(elTag);
	for (o = 0; o < elAttribs.length; o++) {
		el.setAttribute(elAttribs[o][0],elAttribs[o][1]);
	}
	return el;
}
