// -----------------------------------------------------------------
// Function    : Banner Code, 
//		 Formula driver code - allows for a list of formulas to be applied to different modules.
// Language    : JavaScript
// Description : Checks if given email address is of valid syntax
// Copyright   : (c) 1999 Aydin Akcasu
// http://www.A-V-I.com, AVision@A-V-I.com
// -----------------------------------------------------------------
// Ver    Date    Description of modification
// --- ---------- --------------------------------------------------
// 1.0 10/10/1999 Original write, formulas list
// 1.1            Add number validation, parameters list
//			Handle multiple length formulas, 
// -----------------------------------------------------------------
// -----------------------------------------------------------------


function format_Click() {
}

var formatDef = 33334.4;
var formatUsed = "";
var lastIndex = 0;

function format_Change() {
	with (this.document.form.Format) {
		var t = options[selectedIndex].value;
		if (t == "-1") {
			var r = prompt("Enter mm =",formatUsed);
			if (r== null) 
				{
				selectedIndex = lastIndex;
				}
			else
				{
				options[selectedIndex].text =  r + " mm (Custom)";
				formatDef = r;
				formatUsed = r;
				}
		}
		else {
			formatUsed = t;
	   		}
	this.document.form.Sensor.v = 	formatUsed ;
	this.document.form.Sensor.value = 	formatUsed ;
	lastIndex = selectedIndex;
	}
}

function description_set(t) {

//alert("AAA"+t);
this.document.form.description.value  = t;
//	this.doc.form.description.value  = t;
}

function row(name,label, description,doc) {
if (doc == null) doc = document;
	var treturn="";
	 treturn += '<tr>';

    treturn += '<td width="65" height="18" align="right"';
    treturn += 'onMouseOver=\'description_set("'+name+': '+description+'")\' ';
    treturn += ' >';
    treturn += label;
    treturn += '</td>';
    treturn += '<td width="16" height="18">';
    treturn += '<input type="text" name="'+name+'" size="8"';
 //   treturn += ' value=""';
    treturn += ' v=""';
    treturn += ' flag="111" touched=0';
    treturn += ' onFocus=\'description_set("'+name+': '+description+'")\' ';
    treturn += ' >';
    treturn += 'mm';
    treturn += '</td>';
    treturn += '</tr>';
//alert(treturn);	
	doc.write (treturn);
}

function avar(name,val) {
this.name = name;
this.value = val;
}

function param(name,label,desc) {
	this.name 	= name;
	this.label 	= label;
	this.desc 	= desc;
}

function params(){
	var argc 	= params.arguments.length-2;
	var argv 	= params.arguments;
	this.length 	= argc;
	this.doc 	= argv[0];
	this.validate 	= _params_validate;
	for (var i=0; i < argc; i++)
		{
		argv[i].index 	= i;
		this[i] 	= argv[i+1];
		row(this[i].name, this[i].label,this[i].desc, this.doc);	
		}
	
}
function _params_validate() {
	var ret = true;
	for (var i=0; i < this.length; i++)
	{
	var e = "validate(this.doc.form."+this[i].name+")";
	ret = ret && eval(e);
	}
	return ret;
}


var aa = new avar("aa",1);
var bb = new avar("bb",2);


function formula(frm,err,errdesc,setf,a,b,c) {
	this.frm 	= frm;
	this.err 	= err;
	this.errdesc	= errdesc;
	this.setf 	= setf;
	this.setf.flag 	= 0;
	this.params 	= formula.arguments.length - 4;
	this.a 		= a;
	this.b 		= b;
	this.c 		= c;
	this.reset 	= _form_reset;
	this.copy	= _form_copy;
	this.check 	= _form_check;
	this.calc 	= _form_calc;
}

function _form_reset() {
with (this) {
	setf.v 		= ""; 
	setf.value 	= ""; 
	setf.flag 	= 0;
	}}

function _form_copy() {
with (this) {
	setf.v 		= setf.value; 
//	if ((params >= 1) && (a.v == ""))	a.v	= a.value; 
//	if ((params >= 2) && (b.v == ""))	b.v	= b.value; 
//	if ((params >= 3) && (c.v == ""))	c.v	= c.value; 
	if ((params >= 1) )	a.v	= a.value; 
	if ((params >= 2) )	b.v	= b.value; 
	if ((params >= 3) )	c.v	= c.value; 
	//setf.flag 	= 0;
	}}

function _form_check() {
with (this) {
	//if (setf.v != "") 			return false;
	if ((params >= 1) && (a.v == "")) 	return false; 
	if ((params >= 2) && (b.v == "")) 	return false; 
	if ((params >= 3) && (c.v == "")) 	return false;
	return true;}}

function round(t) {
	return Math.round(t*100)/100;
}

function _form_calc() {
with (this) {
	setf.v = frm(); 
	setf.value = round(setf.v); 
	setf.flag =1
//alert("CALC: " + this.index);
	}}

function formulas(){
	var argc 	= formulas.arguments.length-1;
	var argv 	= formulas.arguments;
	var ml 		= 1;
	this.length 	= argc/ml;
	this.calc 	= _formulas_calc;
	this.index 	= 0;
	this.textbox 	= null;
	for (var i=0; i < argc; i+=ml)
		{
		argv[i].index 	= i;
		this[i/ml] 	= argv[i];
		}
}

function _formulas_reset(){
	for (var i=0; i < this.length; i++)
		this[i].reset();
	}
function _formulas_calc(){
	//ps.check();
	for (var i=0; i < this.length; i++)
		this[i].copy();
	for (var i=0; i < this.length; i++)
	{
		if (this[i].check())
		{
		if (this[i].err()) 
			alert("ERROR("+i+"): "+ this[i].errdesc);
		else	
			this[i].calc();
		}
	}}


function validate(field) {
	var valid = ".0123456789"
	var error = "";
	if ( field.value.lastIndexOf('.') 
		!= field.value.indexOf('.'))
		error = "one '.' is allowed"
	for (var i=0; i<field.value.length; i++) {
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == -1) 
			error = "numbers are allowed";
	}
	if (error != "") {
		alert("Invalid entry!  Only " + error);
		field.focus();
		field.select();
	}
	return (error == "");
}


