/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* coca::forms.js                                                           */
/* 'The DHTML Color Calculator' javascript source                           */
/* Copyright (c) 2002 Sebastian Böthin <boethin@math.fu-berlin.de>          */
/*                                                                          */
/* Permission to use, copy, and distribute for non-commercial purposes,     */
/* is hereby granted without fee, providing that the above copyright        */ 
/* notice appear in all copies and that both the copyright notice and       */
/* this permission notice appear in supporting documentation.               */
/*                                                                          */
/* This software is provided "as is" without any expressed or implied       */
/* warranty. The author shall not be liable for any damages suffered by     */
/* users of this software. USE AT YOUR OWN RISK.                            */
/*                                                                          */
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */

// ---------------------------------------------------------------------------
// FormObject
function FormObject(id) {
	this.id = id;
	this.object = document.getElementById ?
		document.getElementById(id) : // DOM
		document.all ? document.all[id] : // IE
		eval('document.'+id);  // NS4
	this.elems = this.object.elements;
	this.get = FormObject_get;
	this.put = FormObject_put;
	return this;
}

function FormObject_get(i) {
	return i < this.object.length ? this.elems[i].value : null;
}

function FormObject_put(i, v) {
	if (i < this.object.length) this.elems[i].value = v;
}

// ---------------------------------------------------------------------------
// TripleForm
function TripleForm(id, ref, range0, ismod0, range1, ismod1, range2, ismod2) {
	this.id = id;
	this.ref = ref;
	this.form = new FormObject(id);
	this.range = new Array(range0, range1, range2);
	this.ismod = new Array(ismod0, ismod1, ismod2);
	this.tm = 0;
	this.get = TripleForm_get;
	this.put = TripleForm_put;
	this.setval = TripleForm_setval;
	this.changed = TripleForm_changed;
	this.increase = TripleForm_increase;
	return this;
}

function TripleForm_get() {
	var v = new Array(3);
	for (var i = 0; i < 3; i++)	v[i] = parseInt(this.form.get(i));
	return v;
}

function TripleForm_put(v) {
	for (var i = 0; i < 3; i++) this.setval(i, v[i], false);
}

function TripleForm_setval(i, val, update, save) {
	var mod = this.ismod[i] ? (val + this.range[i]) % this.range[i] :
		Math.max(0, Math.min(val, this.range[i]));
	var ret = update && parseInt(this.form.get(i)) != mod;
	this.form.put(i, mod);
	if (update) eval('updateby'+this.id+'('+save+')');
	return ret;
}

function TripleForm_changed(i) {
	if (!loaded) return false;
	this.setval(i, parseInt(this.form.get(i)), true, true);
	this.form.elems[i].focus();
}

function TripleForm_increase(i, inc, chk) {
	if (!loaded) return false;
	clearTimeout(this.tm);
	if (!chk) perform = true;
	if (this.setval(i,
		parseInt(this.form.get(i)) + inc*increasestep,
		true,
		inc == 0 && perform))
	this.tm = setTimeout(this.ref+'.increase('+i+','+inc+',true)',
		increasedelay);
}
