function FormVal(){}
FormVal.prototype.validate = function(){return false;}

function MsgObj()
{
	this.errorTitle = "";
	this.message = "";
	this.fields = [];
	this.separator = "";
}

function ValCntlr(n,f)
{
	this.formData = new FormVal();
	this.dlg = new ShadDlg();
	this.errorInfo = new MsgObj();
	this.nom = n;
	this.fNom = f;
	this.warnMsg;
	this.errMsg;
}

ValCntlr.prototype.subForm = function()
{
	var formEl = document.getElementById( this.fNom );
	formEl.submit();
}

ValCntlr.prototype.warnMsg = function()
{
	return '<div class="warn"><h2>Review Form</h2><p><strong>Note: Your form has not been submitted yet.</strong></p></p><p>Your form information has changed. If you wish to change any entries, choose "Cancel" now to do so. Remember to submit your form again after reviewing your information.</p><p>If you do not need to change any information and are ready to submit your form, choose "Submit" now.</p><form action="" id="dlgForm" method="post" onSubmit="return false;"><div class="buttonRow"><input class="okBut" type="submit" value="Submit" onclick="'+this.nom+'.subForm();" /><input class="okBut" type="submit" value="Cancel" onclick="'+this.nom+'.dismissDlg();" /></div></form></div>';
}

ValCntlr.prototype.errMsg = function()
{
	var rMsg = '<div class="error">';
	rMsg += '<h2>'+this.errorInfo.errorTitle+'</h2>';
	rMsg += '<p>' + this.errorInfo.message + '</p>';
	rMsg += '<form action="" id="dlgForm" method="post" onsubmit="return false;"><div class="row">'; 
	
	if( this.errorInfo.fields[0] )
	{
		rMsg += '<label for="val_'+this.errorInfo.fields[0][0]+'">'+this.errorInfo.label+'</label>';
	}
	
	var inp;
	var oLen;
	var maxL;
	var fLen = this.errorInfo.fields.length;
	for( var i = 0; i < fLen; ++i )
	{
		if( i )
		{
			rMsg += this.errorInfo.separator;
		}
		inp = document.getElementById( this.errorInfo.fields[i][0] );
		if( inp.type == 'text' )
		{
			if( inp.maxLength > 0 )
			{
				maxL = ' maxlength="'+inp.maxLength+'"';
			}
			else
			{
				maxL = '';
			}
			rMsg += '<input id="val_'+inp.id+'"'+maxL+' type="'+inp.type+'" value="'+inp.value+'" '+this.errorInfo.fields[i][1]+' />';
		}
		else if( inp.type == 'select-one' )
		{
			rMsg += '<select id="val_'+inp.id+'" '+this.errorInfo.fields[i][1]+'>';
			oLen = inp.length;
			for( var j = 0; j < oLen; ++j )
			{
				rMsg += '<option';
				if(inp.options[j].selected)
					rMsg += ' selected="selected"';
				rMsg +=' value="'+inp.options[j].value+'">'+inp.options[j].text+'</option>';
			}
			rMsg += '</select>';
		}
		else if( inp.type == 'radio' )
		{
			rMsg += '<div class="radios"><label for="val_'+inp.id+'"><input id="val_'+inp.id+'" type="'+inp.type+'" '
			if( inp.checked )
			{
				rMsg += 'checked="checked" ';
			}
			rMsg += this.errorInfo.fields[i][1]+' name="'+this.errorInfo.fields[i][2]+'" /> '+this.errorInfo.fields[i][3]+'</label></div>';
		}
		else if( inp.type == 'textarea' )
		{
			rMsg += '<textarea id="val_'+inp.id+'" rows="'+inp.rows+'" cols="'+inp.cols+'" '+this.errorInfo.fields[i][1]+'>'+inp.value+'</textarea>';
		}
		else if( inp.type == 'checkbox' )
		{
			rMsg += '<div class="checkboxes"><label for="val_'+inp.id+'"><input id="val_'+inp.id+'" type="'+inp.type+'" '
			if( inp.checked )
			{
				rMsg += 'checked="checked" ';
			}
			rMsg += this.errorInfo.fields[i][1]+' name="'+this.errorInfo.fields[i][2]+'" /> '+this.errorInfo.fields[i][3]+'</label></div>';
		}
		
	}
	
	rMsg += '</div><div class="buttonRow"><input style="display:none" id="ieFormFix" type="text" /><input class="subBut" type="submit" value="Submit" onclick="return '+this.nom+'.subDlgForm();" /><input class="canBut" type="submit" value="Cancel" onclick="'+this.nom+'.dismissDlg();" /></div></form></div>';
	return rMsg;
}

ValCntlr.prototype.subMainForm = function()
{
	var rval = false;
	this.errorInfo = new MsgObj();
	
	if( this.formData.validate( this.errorInfo ) )
	{
		rval = true;
	}
	else
	{
		this.dlg.setContent( this.errMsg() );
		this.dlg.show();
		if( document.getElementById( 'dlgForm' ).elements )
		{
			document.getElementById( 'dlgForm' ).elements[0].focus();
			if( document.getElementById( 'dlgForm' ).elements[0].select )
				document.getElementById( 'dlgForm' ).elements[0].select();
		}
	}

	return rval;
}

ValCntlr.prototype.subDlgForm = function()
{
	if( document.getElementById( 'dlgForm' ) )
	{
		e = document.getElementById( 'dlgForm' ).elements;
		len = e.length;
		for( var i = 0; i < len; ++i )
		{
			if( e[i].type == 'text' )
			{
				id = e[i].id.substr(4);
				if( document.getElementById( id ) )
				{
					document.getElementById( id ).value = e[i].value;
				}
			}
			else if( e[i].type == 'select-one' )
			{
				id = e[i].id.substr(4);
				if( document.getElementById( id ) )
				{
					document.getElementById( id ).selectedIndex = e[i].selectedIndex;
				}
			}
			else if( e[i].type == 'radio' )
			{
				id = e[i].id.substr(4);
				if( document.getElementById( id ) )
				{
					document.getElementById( id ).checked = e[i].checked;
				}
			}
			else if( e[i].type == 'textarea' )
			{
				id = e[i].id.substr(4);
				document.getElementById( id ).value = e[i].value;
			}
			else if( e[i].type == 'checkbox' )
			{
				id = e[i].id.substr(4);
				if( document.getElementById( id ) )
				{
					document.getElementById( id ).checked = e[i].checked;
				}
			}
		}
	}
	
	this.dlg.hide();
	
	this.errorInfo = new MsgObj();
	if( this.formData.validate( this.errorInfo ) )
	{
		this.dlg.setContent( this.warnMsg() );
		this.dlg.show();
		if( document.getElementById( 'dlgForm' ).elements )
		{
			document.getElementById( 'dlgForm' ).elements[0].focus();
		}
	}
	else
	{
		this.dlg.setContent( this.errMsg() );
		this.dlg.show();
		if( document.getElementById( 'dlgForm' ).elements )
		{
			document.getElementById( 'dlgForm' ).elements[0].focus();
			if( document.getElementById( 'dlgForm' ).elements[0].select )
				document.getElementById( 'dlgForm' ).elements[0].select();
		}
	}
	
	return false;
}

ValCntlr.prototype.dismissDlg = function()
{
	this.dlg.hide();
}