// JavaScript Document

//////////////////////////////////////////////////////////////////////////////////////////////
//                    MISCELLANEOUS VARIABLE AND FUNCTION DECLARATIONS                      //
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;
}

function popUp(URL)
{
   day = new Date();
   id = day.getTime();
   eval("page" + id + " =window.open(URL, '" + id + "', 'toolbar=0, scrollbars=0, location=0, statusbar=1, menubar=0, resizable=0, width=800, height=600, left=20, top=20');");
}

function loadInPopup(page, wName, w, h, errMsg)
{
	var defaultErrorMsg = "Your browser settings are prohibiting this page from displaying a " +
	                      "necessary popup window. You may need to modify your browser settings.";
	if (jcPopUp(page, wName, w, h, 0, 0, 0, 0, 0) == null)
		window.alert((errMsg != null) ? errMsg : defaultErrorMsg);
} // loadInPopup

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

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

function __trimString()
{
	return this.replace(/^\s+|\s+$/g, "");
} // __trimString

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 index = (arguments.length > 1) ? arguments[1] : 0;
	var returnArray = new Array();

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

	// Remove the element at the specified index from this array.
	if (index == 0) {
		returnArray = my_array.slice(0, my_array.length);
		returnArray.shift();
	}
	else if (index == (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 != index)
				returnArray.push(my_array[i]);
	}
	return returnArray;
} // Array_removeItemAtIndex
//                    MISCELLANEOUS VARIABLE AND FUNCTION DECLARATIONS                      //
//////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////
//                                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 = __my_getElementById;
//                                DOCUMENT OBJECT REFERENCING                               //
//////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////
//                                 FORM VALIDATION ROUTINES                                 //
function checkEmailAddress(field, msg, displayError)
{
	var email_addr = field.value.trim_self(" ");   // remove surrounding whitespace
	var error_msg = (msg == null) ? 'Please enter a valid e-mail address in the text field provided' : msg;
	var regExp1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;
	var regExp2 = /^[\S]+@(\[?)[A-Za-z0-9\-\.]+\.([A-Za-z0-9]+)(\]?)$/;

	if (email_addr)
	{
		if (!(regExp1.test(email_addr)) && (regExp2.test(email_addr))) {
			field.value = email_addr;
			return true;    // e-mail address has valid format
		}
	}
	if (displayError) window.alert(error_msg);     // display error message
	return false;                // email address does not have valid format
} // checkEmailAddress

function checkContactForm (frm)
{
	var is_valid = false;
	var contactName = frm.c_name.value.trim_self(" \"\'");
	var contactEmail = frm.c_email.value.trim_self(" \"\'");
	var contactSubject = frm.c_subject.value.trim_self(" \"\'");
	var contactMsg = frm.c_message.value.trim_self(" \"\'");
	var errors = ['Request denied: One or more of the required fields is empty.',
	              'Request denied: Your email address has an invalid format. An example of a ' +
	              'correct format is somebody@somedomain.com.'];

	if (!contactName || !contactEmail || !contactMsg)
		window.alert(errors[0]);
	else if (!checkEmailAddress(frm.c_email, null, false))
		window.alert(errors[1]);
	else {
		frm.c_name.value = contactName;
		frm.c_email.value = contactEmail;
		frm.c_subject.value = contactSubject;
		frm.c_message.value = contactMsg;
		is_valid = true;
	}
	return is_valid;
} // checkContactForm
//                                 FORM VALIDATION ROUTINES                                 //
//////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////
//                                   IMAGE CREATION ROUTINE                                 //
if (!document.newImage) document.newImage = __my_newImage;
//                                   IMAGE CREATION ROUTINE                                 //
//////////////////////////////////////////////////////////////////////////////////////////////

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

function __my_getElementById(id)
{
	if (window.isOperaWB) return document.getElementById(id);
	else if (document.all) return document.all[id];
	else if (document.w3cdom()) return document.getElementById(id);
	else return document[id];
} // __my_getElementById
//                                 INTERNAL HELPER FUNCTIONS                                //
//////////////////////////////////////////////////////////////////////////////////////////////