/**
 *  ---------
 * |.##> <##.|  Open Smart Card Development Platform (www.openscdp.org)
 * |#       #|  
 * |#       #|  Copyright (c) 1999-2010 CardContact Software & System Consulting
 * |'##> <##'|  Andreas Schwier, 32429 Minden, Germany (www.cardcontact.de)
 *  --------- 
 *
 *  This file is part of OpenSCDP.
 *
 *  OpenSCDP is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  OpenSCDP 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 OpenSCDP; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @fileoverview Processing forms
 */



function Form1() {
}



Form1.getInstance = function() {
	if (Form1.instance == undefined) {
		Form1.instance = new Form1();
	}
	return Form1.instance;
}



/**
 * Handle the GET method to return the form
 *
 * @param {HTTPRequest} req the request
 * @param {HTTPResponse} res the response
 */
Form1.prototype.sendPage = function(req, res) {
	var page =
		<html>
			<head>
				<title>Scripting Server - Form Example</title>
			</head>
			<body>
				<p>Example Form</p>
				<form action="" method="post" enctype="multipart/form-data">
					<table>
						<tr>
							<td align="right">Your given name:</td>
							<td><input type="text" name="givenname" size="40" maxlength="40"/></td>
						</tr><tr>
							<td align="right">Your surname:</td>
							<td><input type="text" name="surname" size="40" maxlength="40"/></td>
						</tr><tr>
							<td align="right">File for upload:</td>
							<td><input type="file" name="file" size="40"/></td>
						</tr>
					</table>
					<input type="submit" value="Submit"/>
					<input type="reset" value="Cancel"/>
				</form>
			</body>
		</html>
	
	res.print('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n');
	res.print(page.toXMLString());
}



/**
 * Handle the POST method to display the content of the form
 *
 * @param {HTTPRequest} req the request
 * @param {HTTPResponse} res the response
 */
Form1.prototype.processForm = function(req, res) {
	var page =
		<html>
			<head>
				<title>Scripting Server - Form Example</title>
			</head>
			<body>
				<p>Example Form</p>
				<table>
					<tr>
						<td align="right">Your given name:</td>
						<td>{req.parameters.givenname}</td>
					</tr><tr>
						<td align="right">Your surname:</td>
						<td>{req.parameters.surname}</td>
					</tr><tr>
						<td align="right">File for upload:</td>
						<td>{req.parameters.file.toString(HEX)}</td>
					</tr>
				</table>
				<a href="">Back to form</a>
			</body>
		</html>
	
	res.print('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n');
	res.print(page.toXMLString());
}



/**
 * Process an incoming HTTP request
 *
 * @param {HTTPRequest} req the request
 * @param {HTTPResponse} res the response
 */
function handleRequest(req, res) {
	GPSystem.trace("Request received.");
	if (req.method == "GET") {
		Form1.getInstance().sendPage(req, res);
	} else {
		Form1.getInstance().processForm(req, res);
	}
}



GPSystem.trace("Script forms.js completed...");
