/*
 * Copyright 2001,2002 Dain Kaplan
 *
 * This file is part of the FarCode Library.
 *
 * The FarCode Library  is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * FarCode 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Foobar; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * * * * * *
 * FarCode::CheckForm - Used for validating user inputs from webpages.
 */

function CheckForm (formAttr) {
        //alert("value for formAttr"+formAttr.length);
	if (formAttr == null) {
	} else if (formAttr == "[object]") { // its the actual form
		// if the form is passed by reference save it, otherwise get the reference to it
		this.form = formAttr;
	} else {
		this.form = document.forms[formAttr]; //eval("document.forms[" + formAttr);
	}
	if (this.form) {
		//this.form.onsubmit = this.validate;
		// this isn't "implemented" yet, they (the JS PEOPLE) suck
	}


	// psuedo constants, hey its JavaScript, what do you want? ^-^
	this.NotEmpty = 1;
	this.CheckValue = 2;
	this.MatchValue = 3;
	this.CheckLength = 4;

	this.fields = new Array();
	//alert ( this );

	// need this for sure
	if (navigator.family == "nn" ) {
		this.addField = CFAddField;
		this.addFieldCheck = CFAddFieldStd;
		this.addFieldCheckByValue = CFAddFieldVal;
		this.addFieldCheckByMatch = CFAddFieldMtch;
		this.addFieldCheckByLength = CFAddFieldLen;
		this.message = CFMessage;
		this.validate = __validate;
	}
	//alert (this.message);

	return this;
}

// this function is internal, this never needs to be called from the page.
// it creates a new object for the array for keeping track of all the fields.

function Field (name, type, msg, val, match, len) {
	this.name = name;
	this.type = type;
	this.msg  = msg;
	this.val = val;
	this.match = match;
	this.len = len;
}

function CFAddField (type) {
	a = arguments;

	arr = this.fields;

	switch (type) {
	case 1:
		CFAddFieldStd (arr, a[1], a[2]);
		break;
	case 2:
		CFAddFieldVal (arr, a[1], a[2], a[3], a[4]);
		break;
	case 3:
		CFAddFieldMtch (arr, a[1], a[2], a[3]);
		break;
	case 4:
		CFAddFieldLen (arr, a[1], a[2], a[3]);
		break;
	default:
		// do nothing
	}
}

function CFAddFieldStd (arr, name, msg) {
	arr[arr.length] = new Field(name, 1, msg, "", "", "");
}

function CFAddFieldVal (
	arr,     // field array
	name, 	// name of first field
	value,	// name of value to match to field
	msg,	// message to display
	cond	// whether to match this value, or exclude this value
	) {

	if (cond == 1) cond = 3;
	else cond = 4;
	arr[arr.length] = new Field(name, cond, msg, value, "", "");
}

function CFAddFieldMtch (
	arr,    // field array
	name, 	// name of first field
	match,	// name of value to match to field
	msg		// message to display
	) {
	arr[arr.length] = new Field(name, 2, msg, "", match, "");
}

function CFAddFieldLen (
	arr,    // field array
	name, 	// name of first field
	len,	// name of value to match to field
	msg		// message to display
	) {
	arr[arr.length] = new Field(name, 5, msg, "", "", len);
}

function CFMessage (mesg) {
	alert ("Please fill in the field '" + mesg + "'");
}

function __validate (fName) {
	if (fName != null) {
		if (fName == "[object]") {
			this.form = fName;
		} else {
			this.form = eval("document." + fName);
		}
	}
	if (this.form == "") return false;

	return CFValidate (this.form, this.fields);
}

function CFValidate (form, fields) {
	// go through list and check to see if all the conditions are met

	for (var x = 0 ; x < fields.length ; x++) {
		currentField = form.elements[fields[x].name];
		if (fields[x].type == 1) {
			if (currentField.value == "") {
				if (fields[x].msg == null) CFMessage (fields[x].name);
				else {
					alert (fields[x].msg);
				}
				currentField.focus();
				return false;
			}
		}
		if (fields[x].type == 2) {
			currentField2 = form.elements[fields[x].match];
			if (currentField.value != currentField2.value) {
				if (fields[x].msg == null) CFMessage (fields[x].name);
				else {
					alert (fields[x].msg);
				}
				currentField.focus();
				return false;
			}
		}
		if (fields[x].type == 3) {
			if (currentField.value != fields[x].val) {
				if (fields[x].msg == null) CFMessage (fields[x].name);
				else {
					alert (fields[x].msg);
				}
				currentField.focus();
				return false;
			}
		}
		if (fields[x].type == 4) {
            if (currentField.value == fields[x].val || ( ( currentField.selectedIndex || currentField.selectedIndex == 0 ) && currentField.options[currentField.selectedIndex].value == fields[x].val)) {
				if (fields[x].msg == null) CFMessage (fields[x].name);
				else {
					alert (fields[x].msg);
				}
				currentField.focus();
				return false;
			}
		}
		if (fields[x].type == 5) {
			if ((!currentField.length) || (currentField.length < fields[x].len)) {
				if (fields[x].msg == null) CFMessage (fields[x].name);
				else {
					alert (fields[x].msg);
				}
				currentField.focus();
				return false;
			}
		}
	}
	return true;
}

// Auto tab when a field is full
function KeyPress(what,e,max,action) {
    if (document.layers) {
        if (e.target.value.length >= max)
            eval(action);
    }
    else if (document.all) {
        if (what.value.length > (max-1))
            eval(action);
    }
}