/*
copyright 2005 Zoltan Milosevic
*/

function highlight_namespace ( namespace, b_is_error ) {
	var color_back = "";
	var color_fore = "";
	if (b_is_error) {
		color_back = "#dddddd";
		color_fore = "#cc0000";
		}
	if (document.getElementById) {
		var i = 0;
		for (;;) {
			var cell = document.getElementById( namespace + "_" + i );
			if (cell == null) {
				break;
				}
			cell.style.backgroundColor = color_back;
			cell.style.color = color_fore;
			i++;
			}
		}
	}

function validate_by_schema ( form_object, schema, b_alert ) {
	var stub_errors = new Array();
	for (var i = 0; i < schema.length; i++) {
		var codename = schema[i][0];
		var value = get_form_value_by_name( form_object, codename );
		var stub = fd_validate( value, schema[i][1], schema[i][2], schema[i][3], schema[i][4], schema[i][5], schema[i][6] );
		var b_is_error = (stub != "");
		if (b_is_error) {
			stub_errors.push( stub );
			}
		highlight_namespace( codename, b_is_error );
		}

	if (stub_errors.length == 0) {
		return true;
		}
	var master = "";
	for (var i = 0; i < stub_errors.length; i++) {
		master += "Error: " + stub_errors[i] + ".\n\n";
		}
	if (b_alert) {
		alert( master );
		}
	return false;
	}

function get_form_value_by_name ( form_object, name, b_no_recurse ) {
	var value = null;
	if (form_object == null) {
		alert( "Error: get_form_value_by_name was passed a null form object." );
		}
	else {
		for (var i = 0; i < form_object.length; i++) {
			var field_object = form_object[i];
			if (field_object.name != name) {
				continue;
				}
			var type = field_object.type;
			if ((type == "radio") || (type == "checkbox")) {
				if (!field_object.checked) continue;
				}
			if (field_object.type == "select") {
				value = field_object[field_object.selectedIndex].value;
				}
			else {
				value = field_object.value;
				}
			break; // no support for multi-name
			}
		if ((value == null) && (!b_no_recurse)) {
			value = get_form_value_by_name( form_object, name + "_udav", true );
			}
		}
	return value;
	}


function fd_validate ( value, type, min, max, b_allow_null, b_allow_blank, display_name, b_core_error_only ) {
	var error = "";
	var value_string = "" + value;
	for (;;) {

		if (value == null) {
			if (b_allow_null) break;
			error = "value cannot be null";
			break;
			}

		// from here on out, we are guaranteed that $value is defined

		if ((type == "int") || (type == "real")) {
			value_string = value_string.replace( "+", "" );
			}
		else if ((type == "httpurl") && (value_string.toLowerCase() == "http://")) {
			value_string = "";
			}

		var len = value_string.length;

		if (len == 0) {
			if (b_allow_blank) break;
			error = "value cannot be blank";
			break;
			}

		var sys_max_length = null;

		if (type == "email") {
			sys_max_length = 255;
			}
		else if (type == "hostname") {
			sys_max_length = 255;
			}
		else if (type == "regex") {
			sys_max_length = 65535;
			}
		else if (type == "file") {
			sys_max_length = 255;
			}
		else if (type == "folder") {
			sys_max_length = 255;
			}

		if (sys_max_length != null) {
			if (sys_max_length < len) {
				error = "value is " + len + " characters long but the maximum supported by this software is " + sys_max_length + " characters";
				break;
				}
			}

		var re;

		if ((type == "int") || (type == "real")) {
			if (type == "int") {
				re = new RegExp( "^-?\\d+$" );
				if (!value_string.match( re )) {
					error = "must match [-][number]";
					break;
					}
				}
			else {
				re = new RegExp( "^-?\\d*\\.?\\d*$" );
				if (!value_string.match( re )) {
					error = "must match [-][number][.number]";
					break;
					}
				}
			if (min != null) {
				if (value_string < min) {
					error = "minimum acceptable value is " + min;
					break;
					}
				}
			if (max != null) {
				if (value_string > max) {
					error = "maximum acceptable value is " + max;
					break;
					}
				}
			}
		else if (type == "string") {
			if (min != null) {
				if (len < min) {
					error = "string is " + len + " characters, but minimum acceptable length is " + min + " characters";
					break;
					}
				}
			if (max != null) {
				if (len > max) {
					error = "string is " + len + " characters, but maximum acceptable length is " + max + " characters";
					break;
					}
				}
			}
		else if (type == "stringplain") {
			var Forbid = new Array( "&", ">", "<", "\"" );
			for (var i = 0; i < Forbid.length; i++) {
				if (-1 == value_string.indexOf( Forbid[i] )) continue;
				error = "string cannot contain any of the following four characters: & > < \"";
				break;
				}
			if (error != "") break;
			error = fd_validate( value_string, "string", min, max, b_allow_null, b_allow_blank, display_name, true );
			if (error != "") break;
			}
		else if (type == "email") {
			re = new RegExp( "\\s" );
			if (value_string.match( re )) {
				error = "value cannot contain whitespace";
				break;
				}
			re = new RegExp( "^(.+?)\\@(.+?)$" );
			if (!value_string.match( re )) {
				error = "must be of the format \"user\@host\"";
				break;
				}
			re = new RegExp( "\\.\\d+$" );
			if (value_string.match( re )) {
				error = "this system does not accept email addresses whose hostnames end with numbers";
				break;
				}
			var i = value_string.indexOf( "@" );
			var hostname = value_string.substring( i + 1 );
			error = fd_validate( hostname, "hostname", 1, 0, 0, 0, "", true );
			if (error != "") break;
			}
		else if (type == "hostname") {
			re = new RegExp( "[^a-zA-Z0-9\\.\\-]" );
			if (value_string.match( re )) {
				error = "value contains characters outside the allowed character set of A-Z, 0-9, . and -";
				break;
				}
			re = new RegExp( "(^-|--|-$)" );
			if (value_string.match( re )) {
				error = "hostname cannot begin with a hyphen, end with a hyphen, or have two hyphens next to each other";
				break;
				}
			re = new RegExp( "\\.\\d+$" );
			if (value_string.match( re )) {
				// okay -- is IP
				}
			else {
				var i = value_string.lastIndexOf( "." );
				var tld = value_string.substring( i + 1 );
				tld = tld.toLowerCase();
				var all_valid_tld = new Array("com","net","org","edu","gov","mil","aero","arpa","biz","coop","info","int","museum","name","pro","mobi","jobs","post","travel",				"ac","ad","ae","af","ag","ai","al","am","an","ao","aq","ar","as","at","au","aw","az","ax","ba","bb","bd","be","bf","bg","bh","bi","bj","bm","bn","bo","br","bs","bt","bv","bw","by","bz","ca","cc","cd","cf","cg","ch","ci","ck","cl","cm","cn","co","cr","cs","cu","cv","cx","cy","cz","de","dj","dk","dm","do","dz","ec","ee","eg","eh","er","es","et","eu","fi","fj","fk","fm","fo","fr","ga","gb","gd","ge","gf","gg","gh","gi","gl","gm","gn","gp","gq","gr","gs","gt","gu","gw","gy","hk","hm","hn","hr","ht","hu","id","ie","il","im","in","io","iq","ir","is","it","je","jm","jo","jp","ke","kg","kh","ki","km","kn","kp","kr","kw","ky","kz","la","lb","lc","li","lk","lr","ls","lt","lu","lv","ly","ma","mc","md","mg","mh","mk","ml","mm","mn","mo","mp","mq","mr","ms","mt","mu","mv","mw","mx","my","mz","na","nc","ne","nf","ng","ni","nl","no","np","nr","nu","nz","om","pa","pe","pf","pg","ph","pk","pl","pm","pn","pr","ps","pt","pw","py","qa","re","ro","ru","rw","sa","sb","sc","sd","se","sg","sh","si","sj","sk","sl","sm","sn","so","sr","st","sv","sy","sz","tc","td","tf","tg","th","tj","tk","tl","tm","tn","to","tp","tr","tt","tv","tw","tz","ua","ug","uk","um","us","uy","uz","va","vc","ve","vg","vi","vn","vu","wf","ws","ye","yt","yu","za","zm","zw");
				var b_match = false;
				for (var i = 0; i < all_valid_tld.length; i++) {
					if (tld == all_valid_tld[i]) {
						b_match = true;
						break;
						}
					}
				if (!b_match) {
					error = "unrecognized top-level domain \"" + tld + "\"";
					break;
					}
				}
			}
		else if (type == "bool") {
			if ((value_string != "0") && (value_string != "1")) {
				error = "value must be \"0\" or \"1\"";
				break;
				}
			}
		else if (type == "httpurl") {
			re = new RegExp( "^http://" );
			if (!value_string.match( re )) {
				error = "URL must start with \"http://\"";
				break;
				}
			}
		else if ((type == "regex") || (type == "file") || (type == "folder")) {
			// okay... no client-side validation defined
			}
		else {
			error = "unrecognized validation type \"" + type + "\"";
			break;
			}
		break;
		}
	if ((error) && (!b_core_error_only)) {
		var type_name = "value";
		if (display_name) {
			type_name = display_name;
			}
		else if (type == "int") {
			type_name = "integer value";
			}
		else if (type == "real") {
			type_name = "real number value";
			}
		else if (type == "string") {
			type_name = "string";
			}
		else if (type == "email") {
			type_name = "email address";
			}
		else if (type == "hostname") {
			type_name = "hostname value";
			}
		if (value_string.length > 0) {
			type_name += " \"" + value_string + "\"";
			}
		error = type_name + " failed validation; " + error;
		}
	return error;
	}








/*
Returns the DOM object for the given ID.  Should work on MSIE and Netscape.  Returns null if no matching object can be found.

*/

function object_by_id (id) {
	var obj = null;
	if (document) {
		if (document.getElementById) { // netscape + ie5
			obj = document.getElementById(id);
			}
		else if (document.all) { // ie4
			obj = document.all[id];
			}
		}
	return obj;
	}

