﻿var blank = "";
var space = " ";
var comma = ",";
var underscore = "_";

/******** Array Prototype Routines ********/

Array.prototype.indexof = function (obj) {
    for (var i = 0; i < this.length; i++) {
        if (((this [i].equals) && (this [i].equals (obj))) || (this [i] == obj)) return i;
    }// for;
    return -1;
}// indexOf;

/********/

Array.prototype.value = function (name) {
	for (var i = 0; i < this.length; i++) {
		if (this [i].name.equals (name)) return this [i].value;
	}// for;
	return blank;
}// value;

/********/

Array.prototype.remove = function (item) {
	if (isnotnumeric (item)) item = this.indexof (item);
	if (item < 0) return;
	this.splice (item, 1);
}// remove;

/******** Date Prototype Routines ********/

Date.prototype.set_date = function (date_string) {
	this.setTime (Date.parse (date_string));
}// set_date;

/********/

Date.prototype.add_days = function (days) {
	this.setUTCDate (this.getUTCDate () + days);
}// add_days;

/********/

Date.prototype.add_months = function (months) {
	this.setUTCMonth (this.getUTCMonth () + months);
}// add_months;

/********/

Date.prototype.add_years = function (years) {
	this.setUTCFullYear (this.getUTCFullYear () + years);
}// add_years;

/******** String Prototype Routines ********/

String.prototype.contents = function (start_char, end_char) {
    var start_index = this.indexof (start_char) + 1;
    var end_index = this.indexof (end_char, start_index);
    return this.substring (start_index, end_index);
}// contents;

/******** encapsulated: Determines if this string starts with start_char and ends with end_char ********/

String.prototype.encapsulated = function (start_char, end_char) {
	if (this.charAt (0) != start_char) return false;
	if (this.charAt (this.length - 1) != end_char) return false;
	return true;
}// encapsulated;

/******** equals / matches: Compares this string to another, excluding case and leading / trailing spaces ********/

String.prototype.equals = function (value) {
	return (this.trim ().toLowerCase () == value.trim ().toLowerCase ());
}// equals;

/********/

String.prototype.matches = function (value) {
	return this.equals (value);
}// matches;

/******** stripped: Removes non-numeric characters from the string ********/

String.prototype.stripped = function () {
	var result = blank;
	for (var i = 0; i < this.length; i++) {
		var next_char = this.charAt (i);
		if (isnumeric (next_char)) result += next_char;
	}// for;
	return result.trim ();
}// stripped;

/********/

String.prototype.trim = function () {
	if (this == null) return this;
	return this.replace(/^\s+|\s+$/g, '');
}// trim;

/********/

String.prototype.indexof = function (value, index) {
	return this.indexOf (value, index);
}// indexof;

/******** Number Prototype Routines ********/

Number.prototype.equals = function (value) {
	return (this == value);
}// equals;

/********/

Number.prototype.padded = function (decimal_places) {
	return this.toFixed (decimal_places);
}// padded;

/******** Common Functions ********/

function attribute (name, control) {
	if (isundefined (control.getAttribute (name))) return blank;
	return control.getAttribute (name);
}// attribute;

/********/

function change_page (target, parameters) {
	var transfer_form = document.createElement ("form");
	document.body.appendChild (transfer_form);
	transfer_form.action = target;
	transfer_form.method = "post";
	if (parameters != undefined) {
		for (var i = 0; i < parameters.length; i++) {
			var hidden_parameter = document.createElement("input");
			hidden_parameter.type = "hidden";
			hidden_parameter.id = parameters[i].name;
			hidden_parameter.value = parameters[i].value;
			hidden_parameter.name = hidden_parameter.id;
			transfer_form.appendChild(hidden_parameter);
		} // for;
	}// if;
	transfer_form.submit ();
	return false;
}// change_page;

/********/

function form () {
	return document.forms [0];
}// form;

/********/

function get_field_value (element) {
	if (!element.type) return null;
	if (element.type.toLowerCase () == "checkbox") {
		if (element.getAttribute ("byvalue")) {
			switch (element.checked) {
				case true: return element.value; break;
				default: return null;
			}// switch;
		}// if;
		return element.checked
	}// if;
	switch (element.type.toLowerCase ()) {
		case "text": return element.value;
		case "password": return element.value;
		case "textarea": return element.value;
		case "hidden": return element.value;
		case "radio": return get_radio_value (element);
		case "select-one": return element.value;
		// put other form types in here
	}// switch;
	return blank;
}// get_field_value;

/********/

function field_value (element) {
	return get_field_value (element);
}// field_value;

/********/

function lowercase (string) {
	try {
		return string.toLowerCase ();
	} catch (exception) {
		return string;
	}// try;
}// lowercase;

/********/

function isdefined (object) {
	return (!isundefined (object));
}// isdefined;

/********/

function isundefined (object) {
	return ((object == null) || (object == undefined));
}// isundefined;

/********/

function isnumeric (object) {
	if (isundefined (object)) return false;
	if (isNaN (object)) return false;
	if (isundefined (object.indexof)) return false;
	if (object.indexof (space) != -1) return false;
	return true;
}// isnumeric;

/********/

function isnotnumeric (object) {
	return (!isnumeric (object));
}// isnotnumeric;

/********/

function isobject (object) {
	return (typeof (object) == "object");
}// isobject;

/********/

function add_event (object, name, handler) {
	if (object.addEventListener != undefined) {
		if (name.indexOf ("on") > -1) name = name.substring (2);
		object.addEventListener (name, handler, true);
		return;
	}// if;
	if (!object.handlers) {
		object.handlers = {};
		object.attachEvent (name, function () {
			for (var i = 0; i < object.handlers [name].length; i++) {
				object.handlers [name] [i] ();
			}// for;
		});
	}// if;
	if (object.handlers [name] == undefined) object.handlers [name] = new Array;
	object.handlers [name].push (handler);
}// add_event;

/********/

function remove_event (object, name, handler) {
	if (object.removeEventListener != undefined) {
		if (name.indexOf ("on") > -1) name = name.substring (2);
		object.removeEventListener (name, handler, true);
		return;
	}// if;
	if ((object.handlers == undefined) || (object.handlers [name] == undefined)) return;
	for (var i = 0; i < object.handlers [name].length; i++) {
		if (object.handlers [name] [i].toString () == handler.toString ()) {
			object.handlers [name].splice (i, 1);
			return;
		}// if;
	}// for;
}// remove_event;

/********/

function create_event_handler (name) {
	if (document.all == undefined) {
		HTMLElement.prototype [name] = function () { eval (this.getAttribute (name)); }
	} else {
		add_event (window, "onload", function () { 
			for (var i = 0; i < document.all.length; i++) {
				if (document.all [i].getAttribute (name) == undefined) continue;
				var command = document.all [i].getAttribute (name);
				document.all [i] [name] = function () { eval (command); }
			}// for;
		});
	}// if;
}// create_event_handler;

/********/

function create_event (name, attribute, property, value) {
	create_event_handler (name);
	if (document.addEventListener != undefined) {
		document.addEventListener ("DOMAttrModified", function (parameters) {
			var properties = parameters.newValue.split (";");
			for (var i = 0; i < properties.length; i++) {
				var parts = properties [i].split (":");
				if (!parts [0].equals (property)) continue;
				if (parts [1].equals (value)) eval (parameters.target [name]);
			}// for;
		}, true);
	} else {
		add_event (window, "onload", function () {
			for (var i = 0; i < document.all.length; i++) {
				if (document.all [i] [name] == undefined) continue;
				var command = document.all [i] [name];
				add_event (document.all [i], "onpropertychange", function () { 
					var parts = event.propertyName.split (".");
					if (!parts [0].equals (attribute)) return;
					if ((parts.length > 1) && (!parts [1].equals (property))) return;
					switch (isundefined (property)) {
						case true: if (!event.srcElement [attribute].equals (value)) return; break;
						case false: if (!event.srcElement [attribute] [property].equals (value)) return;
					}// switch;
					command ();
				});
			}// for;
		});// add_event;
	}// if;
}// create_event;

/********/

function show_control (control, floating) {
	if (isundefined (control)) return;
	if (isvisible (control)) return;
	if (!floating) control.style.position = "static";
	control.style.visibility = "visible";
	eval (control.getAttribute ("onshow"));
	return false;
}// show_control;

/********/

function hide_control (control, floating) {
	if (isundefined (control)) return;
	if (ishidden (control)) return;
	if (!floating) control.style.position = "absolute";
	control.style.visibility = "hidden";
	eval (control.getAttribute ("onhide"));
	return false;
}// hide_control;

/********/

function page_width () {
	try {
		if (isdefined (window.innerWidth)) return window.innerWidth;
		if ((isdefined (document.documentElement.clientWidth)) && (document.documentElement.clientWidth != 0)) return document.documentElement.clientWidth;
		return document.getElementsByTagName('body')[0].clientWidth;
	} catch (except) {
		return 0;
	}// try;
}// page_width;

/********/

function page_height () {
	try {
		if (isdefined (window.innerHeight)) return window.innerHeight;
		if ((isdefined (document.documentElement.clientHeight)) && (document.documentElement.clientHeight != 0)) return document.documentElement.clientHeight;
		return document.getElementsByTagName('body')[0].clientHeight;
	} catch (except) {
		return 0;
	}// try;
}// page_height;

/********/

function center_object (object) {
	var left = parseInt ((page_width () - object.offsetWidth) / 2);
	var top = parseInt ((page_height () - object.offsetHeight) / 2);
	object.style.left = left + document.body.scrollLeft;
	object.style.top = top + document.body.scrollTop;
	object.centered = "true";
}// center_object;

/********/

function update_image_load (target_control) {
	if (target_control.complete) return;
	setTimeout (function () { update_image_load (target_control); }, 100);
}// update_image_load;

/********/

function load_image (image_name, target_control, failure_command, success_command) {
	if (isdefined (failure_command)) target_control.onerror = failure_command;
	if (isdefined (success_command)) target_control.onload = success_command;
	target_control.src = image_name;
	update_image_load (target_control);
}// load_image;

/********/

function get_style_sheet (name) {
	for (var i = 0; i < document.styleSheets.length; i++) {
		var rules = null;
		if (isdefined (document.styleSheets [i].rules)) rules = document.styleSheets [i].rules;
		if (isdefined (document.styleSheets [i].cssRules)) rules = document.styleSheets [i].cssRules;
		if (rules == null) continue;
		for (var j = 0; j < rules.length; j++) {
			if (rules [j].selectorText.substring (1).equals (name)) return rules [j].style;
		}// for;
	}// for;
	return null;
}// get_style_sheet;

/********/

function style_sheet_value (control, attribute) {
	var active_value = null;
	var class_names = control.className.split (space);
	for (var i = 0; i < class_names.length; i++) {
		var style_sheet = get_style_sheet (class_names [i]);
		if (style_sheet == null) continue;
		if (isdefined (style_sheet [attribute])) active_value = style_sheet [attribute];
	}// for;
	return active_value;
}// class_value;

/********/

function isvisible (control) {
	if (control.style.visibility.equals ("visible")) return true;
	if (control.style.visibility.equals ("hidden")) return false;
	var class_setting = style_sheet_value (control, "visibility");
	if (isdefined (class_setting)) return class_setting.equals ("visible");
	return true;
}// isvisible;

/********/

function ishidden (control) {
	return (!isvisible (control));
}// ishidden;

/********/

function record_string (record) {
	var result = blank;
	for (var item in record) {
		if (result != blank) result += comma;
		result += item + ':' + record [item];
	}// for;
	return "{" + result + "}";
}// record_string;

/********/

function left_position (object) {
	var value = object.offsetLeft;
	object = object.parentNode;
	if (isdefined (object)) value += left_position (object);
	if (isdefined (value)) return value;
	return 0;
}// left_position;

/********/

function top_position (object) {
	var value = object.offsetTop;
	object = object.parentNode;
	if (isdefined (object)) value += top_position (object);
	if (isdefined (value)) return value;
	return 0;
}// top_position;

/********/

function repeated_text (character, length) {
	var result = blank;
	for (var i = 0; i < length; i++) {
		result += character;
	}// for;
	return result;
}// repeated_text;

/********/

function elapsed_time (start_time, end_time) {
	return (end_time - start_time);
}// elapsed_time;

/********/

function set_attribute (control, attribute, value) {
	control.setAttribute (attribute, value);
}// set_attribute;

/********/

function get_attribute (control, attribute) {
	return control.getAttribute (attribute);
}// get_attribute;

/********/

function clear_attribute (control, attribute) {
	control.removeAttribute (attribute);
}// clear_attribute;

/******** Removes and item from a select list by the item's value ********/

function remove_list_item (listbox, value, all) {
	for (var i = 0; i < listbox.length; i++) {
		if (listbox [i].value == value) {
			listbox.remove (i);
			if (isundefined (all)) return;
		}// if;
	}// for;
}// remove_select_item;

/******** Adds an item to a select list - value is optional ********/

function add_list_item (list, name, value) {
	var option = document.createElement ("option");
	option.text = name;
	option.value = value;
	try {
		list.add (option, null);
	} catch (except) {
		list.add (option);
	}// try;
}// add_list_item;

/********/
