function shortCuts() {if (event.ctrlKey != true) return;if (event.keyCode == 10) document.forms[0].btnGo.click();}
function randomID(){
var myDate = new Date();
var mt = myDate.getMonth();
var dy = myDate.getDay();
var hr = myDate.getHours();
var mn = myDate.getMinutes();
var sc = myDate.getSeconds();
var UID = mt+""+dy+""+hr+""+mn+"_"+parseInt(((parseInt(sc)*1000)/mt-(parseInt(mt)+43)));
return UID;
}

function changeOptions(){
document.getElementById("radio_options").style.display='none';
document.getElementById("checkbox_options").style.display='none';
document.getElementById("select_options").style.display='none';
document.getElementById("textarea_options").style.display='none';
document.getElementById("text_input_sizes").style.display='none';
document.getElementById("labelling_options").style.display='none';

var opt = document.frmCodeBuilder.ddlElType[document.frmCodeBuilder.ddlElType.selectedIndex].value;
if ((opt=="select")||(opt=="checkbox")||(opt=="radio")||(opt=="textarea"))
	{
	eval("document.getElementById('" + opt + "_options').style.display='block';");
	}
if ((opt=="checkbox")||(opt=="radio"))
	{
	document.getElementById("labelling_options").style.display='block';
	}
if (opt=="text")
	{
	document.getElementById("text_input_sizes").style.display='block';
	}
document.frmCodeBuilder.txtCode.value='';
document.getElementById("preview").innerHTML='';
document.frmCodeBuilder.txtFieldName.value = fileExt();
}

function fileExt()	{
var inputType=document.frmCodeBuilder.ddlElType[document.frmCodeBuilder.ddlElType.selectedIndex].value;
var fieldName='';

if (inputType=="select")	fieldName="ddl";
if (inputType=="checkbox")	fieldName="chk";
if (inputType=="radio")		fieldName="rad";
if (inputType=="textarea")	fieldName="txt";
if (inputType=="text")		fieldName="txt";
return fieldName;
}

function generate()
{
var inputType=document.frmCodeBuilder.ddlElType[document.frmCodeBuilder.ddlElType.selectedIndex].value;
var textLabel=document.frmCodeBuilder.txtLabel.value;
var titleAttrib=document.frmCodeBuilder.txtTitleAttribute.value;
if (textLabel.length==0) textLabel="None_provided";
var fieldName = document.frmCodeBuilder.txtFieldName.value;
var maxLength = document.frmCodeBuilder.txtMaxLength.value;
var size = document.frmCodeBuilder.txtSize.value;
var labelStyle = document.frmCodeBuilder.radFieldsetOptions[0].checked;
if (labelStyle==true) labelStyle="fieldset";
if (labelStyle==false) labelStyle="label";
if (fieldName.length==0)
	{
	fieldName=fileExt();
	}
var s='';
var labelID = randomID();
var loop=1;
if (inputType=="radio")	loop = document.frmCodeBuilder.txtRadHowMany.value;
if (inputType=="checkbox")	loop = document.frmCodeBuilder.txtCheckHowMany.value;
if (inputType=="select")	loop = document.frmCodeBuilder.txtDDLHowMany.value;

if (loop=="") loop=1;

if ((inputType=="checkbox")||(inputType=="radio")) {
		if (labelStyle=="fieldset") {
			s+='<fieldset>\n<legend>';
		}
		else {
			s+='<label>';
		}
	}
if ((inputType!="checkbox")&&(inputType!="radio")) s+='<label for="'+fieldName+'_'+labelID+'">';
if ((inputType=="checkbox")||(inputType=="radio")) {
	if (labelStyle=="fieldset") {
		s+=textLabel+'</legend>';
		}
	else {
		s+=''+textLabel+':</label>\n';
		}
	}
if ((inputType!="checkbox")&&(inputType!="radio")) s=s+ textLabel + '</label> ';

if (inputType=="select") {
	s+='<select name="' + fieldName + '" id="'+ fieldName + "_" + labelID+'" title="' + titleAttrib + '">' + '\n';
	s+='\t<option value="">Please Select ... </option>\n';
	}
for (i=0;i<loop;i++)
	{
	var radID = randomID();
	if ((inputType=="checkbox")||(inputType=="radio"))
		{
		labelID=radID + "_" + i;
		}
		
	if (inputType=="select")
		{
		s+='\t<option value="' + textLabel + '_value_' + (i+1) + '">' + textLabel + '_value_' + (i+1) + '</option>\n';
		}
	else if (inputType=="textarea")
		{
		s+='<textarea name="' + fieldName + '" id="' + fieldName + '_' + labelID + '" rows="5" cols="25" class="input" title="' + titleAttrib + '"></textarea>\n';
		}
	else
		{
		s+='<div><input title="' + titleAttrib + '" class="input" type="'+inputType+'"';
		if (inputType=="text")
			{
			if (maxLength!="") s+=' maxlength="' + maxLength + '"';
			if (size!="") s+=' size="' + size + '"';
			}
			if ((inputType=="checkbox")||(inputType=="radio")) {
				s+=' name="' + fieldName + '_' + textLabel + '"';
			}
			else {
				s+=' name="' + fieldName + '_' + labelID +'" value=""';
			}
		if (inputType=="text")
			{
			s+=' id="' + fieldName + '_' + labelID +'" value=""';
			}
		else
			{
			s+=' id="'+fieldName+'_'+ (i+1) +'" value="">';
			}
		if ((inputType=="checkbox")||(inputType=="radio"))
			{
			s+='<label for="'+fieldName+'_'+ (i+1) +'">';
			s+=" " + textLabel + "_value_" + (i+1);
			s+='</label></div>';
			}
		s+='\n';
		}
	}

if (inputType=="select")	
	{
	s+='</select>' + '\n';
	}

if ((labelStyle=="fieldset")&&((inputType=="checkbox")||(inputType=="radio")))
	{
	s+='</fieldset>';
	}

//escape any troublesome characters
s=s.split('<');
s=s.join('\<');
s=s.split('>');
s=s.join('\>');
s=s.split("'");
s=s.join("\.");

//show in preview area
document.getElementById("preview").innerHTML= s;
//Pass into text area to be copied
document.frmCodeBuilder.txtCode.value=s;
document.frmCodeBuilder.txtCode.select();
}

function init()	{
document.frmCodeBuilder.txtFieldName.value = fileExt();
document.frmCodeBuilder.txtLabel.focus();
}

//let's migrate some of this to jQuery ... it's a bit of a mess!
$(document).ready(function(){
$("#fieldname_options, #text_input_sizes, #title_attribute, #labelling_options, #radio_options, #checkbox_options, #select_options, #textarea_options").hide();
$("#btnGo").click(function(){
	generate();
});
});

