/*
 *  SPL - The SPL Programming Language
 *  Copyright (C) 2004, 2005  Clifford Wolf <clifford@clifford.at>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  wsfdemo.webspl: A simple WebSPL forms application
 */

load "wsf";
load "encode_xml";

object Calculator WsfComponent
{
	var currentvalue = 0;
	var title = "Calculator";
	var offmessage = "TURNED OFF";

	method get_html()
	{
		if ( offmessage ~== "" )
			return #file-as-template calc_on.spltpl;
		else
			return #file-as-template calc_off.spltpl;
	}

	method main()
	{
		while (1)
		{
			task_co_return();

			if ( declared cgi.param.turnon )
				offmessage = "";

			if ( declared cgi.param.act ) {
				if ( cgi.param.act ~== 'add' )
					currentvalue = currentvalue + cgi.param.num;

				if ( cgi.param.act ~== 'sub' )
					currentvalue = currentvalue - cgi.param.num;

				if ( cgi.param.act ~== 'mul' )
					currentvalue = currentvalue * cgi.param.num;

				if ( cgi.param.act ~== 'div' ) {
					if ( cgi.param.num == 0 ) {
						currentvalue = 0;
						offmessage = "DIV BY ZERO";
					} else
						currentvalue = currentvalue / cgi.param.num;
				}
			}

			dirty = 1;
		}
	}
}

object Calclist WsfComponent
{
	method get_html()
	{
		var html = '<div id="$id">';
		html ~= '<b>Active DOM Update mechanism: '~
				'${WsfDocument.domupdate}</b><br/>\n';
		var domupdlist = [ "none" => 0, "xmlhttprequest" => 0, "iframe" => 0 ];
		foreach x ( domupdlist )
			html ~= '&nbsp; <small><b>[ <a href="${cgi.url}?' ~
					'wsf_domupdate=$x">$x</a> ]</b></small>\n';
		html ~= '&nbsp; <small><b>[ <a href="${cgi.url}?' ~
				'wsf_domupdate=iframe&wsf_showiframe=1' ~
				'">iframe+debug</a> ]</b></small>\n';
		html ~= '&nbsp; <small>(not all methods work with all browsers, Mozilla/Firefox recommended)</small>';
		html ~= '<p/>\n<table>\n';
		for (var i=0; i<32; i++) {
			if ( i%4 == 0 )
				html ~= "<tr><td>${ children.[i].get_html_cached() }</td>\n";
			else
			if ( i%4 == 3 )
				html ~= "<td>${ children.[i].get_html_cached() }</td></tr>\n";
			else
				html ~= "<td>${ children.[i].get_html_cached() }</td>\n";
		}
		html ~= '</table></div>\n';
		return html;
	}

	method init()
	{
		*WsfComponent.init();
		for (var i=0; i<32; i++) {
			children.[i] = new Calculator();
			children.[i].title = "Calc #$i";
		}
		return this;
	}
}

var page = new WsfDocument();
page.root = new Calclist();
page.main();

