// JavaScript Document

function formObject(name) {
	this.name     = name;
	this.elements = new Array();
	
	this.onCompleteLstnrs = new Array();
	
	this.getName = function () {
		return this.name;
	}
	
	this.addListener = function (listener) {
		this.onCompleteLstnrs[this.onCompleteLstnrs.length] = listener;
	}
	
	this.addElement = function (objElement) {
		this.elements[this.elements.length] = objElement;
	}
	
	this.draw = function () {
		var result = '';
		result += '<table cellpadding="0" cellspacing="5" border="0">';
		result += '<form name="form_' + this.name + '">';
		for (var i = 0; i < this.elements.length; i++) {
			result += this.elements[i].draw();
		}
		result += '<tr><td align="right">Рассчитать&nbsp;&nbsp;<input name="" id="sbmImg" align="absmiddle" type="image" src="/i/calc-subm.gif" width="44" height="21" class="img" onClick="calculate(); return false;"></td></tr>';
		result += '</form>';
		result += '<table>';
		return result;
	}
	
	this.isComplete = function() {
		for (var i = 0; i < this.elements.length; i++) {
			if (this.elements[i].isFilled() == 0) {
				return false;
			}
		}
		return true;
	}
	
	this.onComplete = function() {
		if (this.isComplete()) {
			for (var i = 0; i < this.onCompleteLstnrs.length; i++) {
				eval(this.onCompleteLstnrs[i])(this.getValues());
			}
		}
	}
	
	this.getValues = function () {
		var values = new Array();
		for (var i = 0; i < this.elements.length; i++) {
			sv = new Object();
			sv.name  = this.elements[i].getId();
			sv.value = this.elements[i].getValue();
			values[values.length] = sv; 
		}
		return values;
	}
}

function notifyForm(formObj) {
	formObj.onComplete();
}
