﻿var blank = "";
var space = " ";
var comma = ",";
var underscore = "_";

/******** Array Prototype Routines ********/

if (!Array.indexOf) {
    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;
}// if;

/********/

if (!Array.value) {
	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;
}// if;

/********/

if (!Array.remove) {
	Array.prototype.remove = function (index) {
		this.splice (index, 1);
	}// remove;
}// if;

/******** Date Prototype Routines ********/

if (!Date.set_date) {
	Date.prototype.set_date = function (date_string) {
		this.setTime (Date.parse (date_string));
	}// set_date;
}// if;

/********/

if (!Date.add_days) {
	Date.prototype.add_days = function (days) {
		this.setUTCDate (this.getUTCDate () + days);
	}// add_days;
}// if;

/********/

if (!Date.add_months) {
	Date.prototype.add_months = function (months) {
		this.setUTCMonth (this.getUTCMonth () + months);
	}// add_months;
}// if;

/********/

if (!Date.add_years) {
	Date.prototype.add_years = function (years) {
		this.setUTCFullYear (this.getUTCFullYear () + years);
	}// add_years;
}// if;

/******** String Prototype Routines ********/

if (!String.equals) {
	String.prototype.equals = function (value) {
		return (this.trim ().toLowerCase () == value.trim ().toLowerCase ());
	}// equals;
}// if;

/********/

if (!String.trim) {
	String.prototype.trim = function () {
		if (this == null) return this;
		return this.replace(/^\s+|\s+$/g, '');
	}// trim;
}// String.trim;

/********/

if (!String.indexof) {
	String.prototype.indexof = function (value, index) {
		return this.indexOf (value, index);
	}// indexof;
}// if;

/******** Integer Prototype Routines ********/

if (!Number.equals) {
	Number.prototype.equals = function (value) {
		return (this == value);
	}// equals;
}// if;

/******** 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 get_field_value (element) {
	if (!element.type) return null;
	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 "checkbox": return element.checked;
		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 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) || (object == blank));
}// isundefined;

/********/

function isnumeric (object) {
	return (!isNaN (object));
}// isnumeric;

/********/

function isnotnumeric (object) {
	return (isNaN (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"));
}// 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"));
}// hide_control;

/********/

function page_width () {
	try {
		if (isdefined (window.innerWidth)) return window.innerWidth;
		if (isdefined (document.documentElement.clientWidth)) 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)) 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);
//	if (isdefined (object.offsetParent)) {
//		left -= object_left (object.offsetParent);
//		top -= object_top (object.offsetParent);
//	}// if;
	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 = function () { eval (failure_command); };
	if (isdefined (success_command)) target_control.onload = function () { eval (success_command); };
	target_control.src = image_name;
	update_image_load (target_control);
}// load_image;

/********/

function play_sound (sound_name) {
	var sound_object = document.getElementById (sound_name);
	sound_object.Play ();
}// play_sound;

/********/

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 open_window (width, height) {
	var left = parseInt ((screen.width - width) / 2);
	var top = parseInt ((screen.height - height) / 2);
	var options = "scrollbars=no,location=no,status=no,resizable=no,width=" + width + ",height=" + height + ",left=" + left + ",top=" + top;
	var popup = window.open ("links.htm", "popup_window", options);
	popup.trigger_field = parameters.trigger_field;
	popup.opener = window;
	return false;
}// open_window;

/********/


