// javascript regular expressions for pattern checking
// must have a 4.0 browser
// expect a global ns4/ie4, otherwise, don't call these functions
// and let the server handle the type checking

// types array, no need to create anew each time
// add as many as you need
// currently, types can be function pointers that take one argument
// or re's

var focus = true;

var types = new Array();
types["w"] = /\w/;	//word
types["w1"] = /\w+/;	//word
types["d"] = /^\d*$/;
types["d1"] = /^\d+$/;
types["file"] = /\w/;
types["date"] = _is_date;
types["null_date"] = _is_null_date;
types["email"] = /^.+\@\[?\w+\.\w+\]?/;
types["state"] = _is_state;
types["password"] = _check_password;
types["password_blankok"] = _password_blankok;
types["password_verify"] = _check_password_verify;
types["phone"] = /\w+/;

function TypeChecker () {
    this.check_value = _check_value;
    this.check_form = _check_form;
}

function _check_password(val) {
	return (val.length > 5);
}

function _password_blankok(val) {
	if (val.length > 0) {
		return _check_password(val);
	} else {
		return true;
	}
}

function _check_password_verify(vals) {
x=vals[0].toString();
y=vals[1].toString();
	if (x == y) {
		return(true);
	} else { 
		return(false);
	}
}

function _is_state(val){
	return (val.length == 2);
}

function _is_date (val) {
    return Date.parse(val);
}

function _is_null_date (val) {
    if (val == "") {
	return true;
    } else {
	return Date.parse(val);
    }
}

function _check_value (vals, typ) {
	if (vals.length > 1) {
		return types[typ](vals);
	} else {
		var qq = new String(val);
		switch (typeof(types[typ])) {
	    case "undefined":
		return false;
	    case "object":
		return qq.match(types[typ]);
	    case "function":
		return types[typ](qq);
	    default:
		return false;
    }
	}
}


// this function expects to be passed
// an array of arrays of field names and expected types
// it will alert use and return true/false
// so the form knows whether or not to submit
// each sub array is in the form: new Array("field_name", "field_type", "error message"[, boolean ignore_empty_value]);
function _check_form (frm, fields) {
    var i, val, field, j;
    for(i=0; i<fields.length; i++) {
	val = "";
	
	strFieldnames = fields[i][0];
	fieldnames = strFieldnames.split("|");

	vals = new Array();
	
	has_val=0;
	for(m=0; m<fieldnames.length; m++) {
		//aryField[m] = frm[fieldnames[m]];
		vals[m] = new String(_get_val(frm[fieldnames[m]]));
		if(vals[m].length>0)
			has_val=1;
	}	
	if(fields[i][3] && !has_val) //skip if we are accepting empty values for this field
		continue;	

	if (! this.check_value(vals, fields[i][1])) {
	    alert(fields[i][2]);
	    if (focus) { frm[fieldnames[0]].focus() }
	    return false;
	}
    }
    return true;
}


function _get_val(field) {
    // first we catch radio buttons
	if (!field.type) {  // it's a radio or checkbox group
	    if (field.length > 0) {
		if (field[0].type.match(/radio/i)) {
		    focus = false;
		    val = ""; // was val = null;
		    for(j=0; j<field.length; j++) {
			if (field[j].checked) {
			    val = field[j].value;
			    
			}
		    }
		}
	    }
	} else if (field.type.match(/select/i)) {
	    val = new String(field.options[field.selectedIndex].value);
	} else if ((field.type.match(/text/)) || (field.type.match(/password/))) {
	    val = new String(field.value);
	} else if (field.type.match(/file/)) {
	    val = new String(field.value);
	} else if (field.type.match(/radio/)) {
	    val = new String(field.value);
		//alert(val);
	}
	
	return val;
	
}