//* CForm - Copyrights (c) Atilla Software Engineering BV
//* 2000-05-02 - Added Post/Get functionality

//* Select: add( name, label, array, "select", widthpixels, heightrows, selectedindex )

function CForm( name, formtitle, width )
{
	//* data
	this.item = new Array();		//* Array of items in this form
	this.func = new Array();		//* Array of extra functions
	this.name = name;
	this.method = "get";
	this.width = width ? width: 0;

	this.submitlabel = "Submit";	//* Label of Submit-buttom
	this.action = ".";				//* Form action

	this.formstyle = "{font-size:10px;font-family:verdana,arial,helvetica;text-decoration:none;border-style:none}";
	this.buttonstyle = "{font-size:10px;font-family:verdana,arial,helvetica;text-decoration:none}";
	this.formtitle = formtitle;
	this.formbgcolor = windowbgcolor;
	this.formtitlecolor = windowtitle;

	//* methods
	this.add = CForm_add;					//* add( name, label, value, type, cols, rows, selected )
	this.addblank = CForm_addblank;			//* addblank()
	this.addparam = CForm_addparam;			//* addparam( name, value )
	this.addfunction = CForm_addfunction;	//* addfunction( label, action )
	this.print = CForm_print;				//* print( doc )
	this.setsubmit = CForm_setsubmit;		//* setsubmit( label, action )
	this.setcolor = CForm_setcolor;			//* setcolor( titlecolor, backgroundcolor )
	this.setmethod = CForm_setmethod;		//* setmethod( method )
}

function CForm_add( name, label, value, type, cols, rows, selected )
{
	this.item[this.item.length] = new CFormItem( this, name, label, value, type, cols, rows, this.width, selected );
}

function CForm_addblank()
{
	this.add( "", "&nbsp", "&nbsp", "nonedit" );
}

function CForm_addparam( name, value )
{
	this.add( name, "", value, "hidden" );
}

function CForm_addfunction( label, action )
{
	this.func[this.func.length] = new CFormItem( this, "", label, action, "button" );
}

function CForm_print( doc )
{
	doc.write( "<form name=\""+this.name+"\" action=\""+this.action+"\" method=\""+this.method+"\"><table border=0 cellspacing=0 bgcolor=" + this.formbgcolor + " style=" + this.formstyle + ">" );
	doc.write( "<tr><td colspan=2 bgcolor="+this.formtitlecolor+" align=center>"+this.formtitle+"</td></tr>" );

	for( formi = 0; formi < this.item.length; formi++ )
		this.item[formi].print( doc );

	doc.write( "<tr><td align=right colspan=2>" );

	for( formi = 0; formi < this.func.length; formi++ )
		this.func[formi].print( doc );

	doc.write( "<input type=submit value=\""+this.submitlabel+"\" style="+this.buttonstyle+"></td></tr>" );

	doc.write( "</table></form>" );
}

function CForm_setsubmit( label, action )
{
	this.submitlabel = label;
	this.action = action;
}

function CForm_setcolor( title, body )
{
	this.formtitlecolor = title;
	this.formbgcolor = body;
}

function CForm_setmethod( method )
{
	this.method = method;
}

function CFormItem( parent, name, label, value, type, cols, rows, width, selected )
{
	//* data
	this.parent = parent;
	this.name = name;
	this.label = label;
	this.type = type;
	this.value = value;
	this.cols = cols;
	this.rows = rows;
	this.width = width;
	this.selected = selected;

	//* methods
	this.print = CFormItem_print;
}

function CFormItem_print( doc )
{
	switch( this.type )
	{
		case "text":
		case "password":
			doc.write( "<tr><td align=right"+( this.width ? " width=" + this.width: "")+">" + this.label + "</td>" );
			doc.write( "<td><input type=" + this.type + " style="+this.parent.formstyle+" name=" + this.name + " value=\"" + this.value + "\" size=" + this.cols + "></td>" );
			doc.write( "</tr>" );
			break;

		case "nonedit":
			doc.write( "<tr><td align=right"+( this.width ? " width=" + this.width: "" ) + ">" + ( this.label ? this.label : "&nbsp" ) + "</td>" );
			doc.write( "<td" + (this.cols ? " width=" + this.cols : "" ) + ">" + this.value + "</td>" );
			doc.write( "</tr>" );
			break;

		case "textarea":
			doc.write( "<tr><td align=right valign=top"+( this.width ? "width=" + this.width: "")+">" + this.label + "</td>" );
			doc.write( "<td><textarea style="+this.parent.formstyle+" name=" + this.name + " cols="+this.cols+" rows="+this.rows+">" );
			doc.write( this.value );
			doc.write( "</textarea></td>" );
			doc.write( "</tr>" );
			break;

		case "checkbox":
			doc.write( "<tr><td align=right"+( this.width ? "width=" + this.width: "")+">" + this.label + "</td>" );
			doc.write( "<td><input type=checkbox name=" + this.name + ( !this.value || this.value == 0 ? "" : " checked" ) + "></td>" );
			doc.write( "</tr>" );
			break;

		case "select":
			var i;
			doc.write( "<tr><td align=right valign=top"+( this.width ? "width=" + this.width: "")+">" + this.label + "</td>" );
			doc.write( "<td><select name=" + this.name + " size=" + this.rows + " style="+this.parent.formstyle );
			if( this.cols ) doc.write( " style={width:"+this.cols+"}" );
			doc.write( ">" );
			for( i = 0; i < this.value.length; i++ )
				doc.write( "<option value=\"" + this.value[i] + "\"" + ( this.selected == i ? " selected" : "" ) + ">" + this.value[i] );
			doc.write( "</select></td></tr>" );
			break;

		case "hidden":
			doc.write( "<input type=hidden name="+this.name+" value=\""+this.value+"\">" );
			break;

		case "button":
			doc.write( "<input type=button value=\""+this.label+"\" onClick=\""+this.value+"\" style="+this.parent.buttonstyle+">" );
			break;
	}
}


