/**
 * global - Defines a set of JavaScript variables and functions used throughout
 *          the entire beava.com web site.
 *
 * Copyright (C) 2006-2007 BEAVA. ALL RIGHTS RESERVED.
 * Author: Owan Hunte <ohunte@geoorbis.com>
 * $Rev: 1 $
 */

// The website domain
var DOMAIN = 'beavainc.com';
var ALL_DOMAINS = '.beavainc.com';

if (!window.m_isIE_defined && navigator.userAgent) {
	var agent = navigator.userAgent.toLowerCase();
	window.m_isIE = (agent.indexOf("opera") == -1 && agent.indexOf("msie") != -1);
	window.m_isWin = (agent.indexOf("win") != -1 || agent.indexOf("16bit") != -1);
	window.m_isIE_defined = true;
}

// Add method to string class.
if (!String.prototype.trim_self) String.prototype.trim_self = __trimString;

// Image creation routine.
if (!document.newImage) document.newImage = __newImage;

function Array_getIndexOfElement(my_array, searchElement)
{
	// Determine the starting index for the search
	var fromIndex = (arguments.length > 2) ? arguments[2] : 0;
	if (fromIndex >= my_array.length)
		return -1;
	if (fromIndex < 0) {
		fromIndex = my_array.length + fromIndex;
		if (fromIndex < 0) fromIndex = 0;
	}
	// Execute the search for searchElement
	for (var pos in my_array)
		if (my_array[pos] == searchElement)
			return pos;

	// Return -1 if element not found
	return -1;
} // Array_getIndexOfElement

function Array_removeItemAtIndex(my_array)
{
	var el_indexio = (arguments.length > 1) ? arguments[1] : 0;
	var returnArray = new Array();

	// Check for invalid index.
	if (el_indexio < 0 || el_indexio >= my_array.length)
		return returnArray;

	// Remove the element at the specified index from this array.
	if (el_indexio == 0) {
		returnArray = my_array.slice(0, my_array.length);
		returnArray.shift();
	}
	else if (el_indexio == (my_array.length-1)) {
		returnArray = my_array.slice(0, my_array.length);
		returnArray.pop();
	}
	else {
		for (var i = 0; i < my_array.length; i++)
			if (i != el_indexio)
				returnArray.push(my_array[i]);
	}
	return returnArray;
} // Array_removeItemAtIndex

//////////////////////////////////////////////////////////////////////////////////////////////
//                                     COOKIE MANIPULATION                                  //
function readCookie(name)
{
	var cookieValue = "";
	var searchStr = name + "=";

	if (document.cookie.length > 0) {
		offset = document.cookie.indexOf(searchStr);
		if (offset != -1) {
			offset += searchStr.length;
			end = document.cookie.indexOf(";", offset);
			if (end == -1) end = document.cookie.length;
			cookieValue = unescape(document.cookie.substring(offset, end))
		}
	}
	return cookieValue;
} // readCookie

function writeCookie(name, value, expires, path, domain, secure)
{
	var expire = (expires) ? "; expires=" + expires.toGMTString() : "";
	document.cookie = name + "=" + escape(value) + expire + ((path) ? "; path=" + path : "") +
	                 ((domain) ? "; domain =" + domain : "") + ((secure) ? "; secure" : "");
} // writeCookie

function removeCookie(name, path, domain)
{
	if (readCookie(name))
		document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain =" +
		                  domain : "") + "; expires=Fri, 11 May 2001 05:00:00 GMT";
} // removeCookie

function jsDateFix(date)
{
	var control = new Date(0);
	var surplus = control.getTime();
	if (surplus)
		date.setTime(date.getTime() - surplus);
} // jsDateFix
//                                     COOKIE MANIPULATION                                  //
//////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////
//                                DOCUMENT OBJECT REFERENCING                               //
if (!document.w3cdom)
	document.w3cdom = function() {
		return (document.getElementById && document.createElement) ? true : false;
	}

if (!window.isOperaWB) {
	var user_agent = navigator.userAgent.toLowerCase();
	window.isOperaWB = (user_agent.indexOf ("opera") != -1);
}

if (!document.getElementById)
	document.getElementById = __getElementById;
//                                DOCUMENT OBJECT REFERENCING                               //
//////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////
//                                  FORM VALIDATION ROUTINES                                //
function validateLogin(f)
{
	f.passwd.value = hex_md5(f.passwd.value);
	return true;
} // validateLogin
//                                  FORM VALIDATION ROUTINES                                //
//////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////
//                                 INTERNAL HELPER FUNCTIONS                                //
function __trimString(delimiters)
{
	var first = 0, last = this.length;
	while (first < last && delimiters.indexOf(this.charAt(first)) != -1) ++first;
	while (first < last && delimiters.indexOf(this.charAt(last-1)) != -1) --last;
	return this.substr(first, last - first);
} // __trimString

function __newImage(imageUrl)
{
	var result = null;
	if (document.images) { result = new Image(); result.src = imageUrl; }
	return result;
} // __newImage

function __getElementById(id)
{
	if (document.all) return document.all[id];
	else return null;
} // __getElementById
//                                 INTERNAL HELPER FUNCTIONS                                //
//////////////////////////////////////////////////////////////////////////////////////////////