//window.onerror = function() {return true;}

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 loadPopup(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);
} // loadPopup

// 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;

if (!document.getElementRef)
   document.getElementRef = document.getElementById;

function disableRightClick(e)
{
	if (!document.rightClickDisabled)   // initialise
	{
		if (document.all)
			document.oncontextmenu = disableRightClick;
		else if (document.layers || document.addEventListener) {
			document.onmousedown = disableRightClick;
			document.oncontextmenu = function() {return false;}
		}
		return document.rightClickDisabled = true;
	}
	if (document.all)
		return false;
	else if (document.addEventListener || document.layers) {
		if (e.button == 2 || e.button == 3)
			return false;
	}
} // disableRightClick

// Validate e-mail address entered in a text field.
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 address = frm.c_address.value.trim_self(" ");
	var phone = frm.c_phone.value.trim_self(" \"\'");
	var fax = frm.c_fax.value.trim_self(" \"\'");
	var contactEmail = frm.c_email.value.trim_self(" \"\'");
	var contactQuery = frm.c_query.value.trim_self(" \"\'");
	var errors = ['Request denied: One or more of the form entries (name, email address or query) is empty.',
	              'Request denied: Your email address has an invalid format. An example of a ' +
	              'correct format is somebody@hotmail.com.'];

	if (!contactName || !contactEmail || !contactQuery)
		window.alert(errors[0]);
	else if (!checkEmailAddress(frm.c_email, null, false))
		window.alert(errors[1]);
	else
		is_valid = true;

	if (is_valid) {
		frm.c_name.value = contactName;
		frm.c_address.value = address;
		frm.c_phone.value = phone;
		frm.c_fax.value = fax;
		frm.c_email.value = contactEmail;
		frm.c_query.value = contactQuery;
	}
	return is_valid;
} // checkContactForm

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

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 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

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

// AJAX-based routines.
var tracker = 0;
function refreshPhoto()
{
	$('#widget-property').load('/ajax/home-slideshow.php', { __t: String(tracker) }, function() {tracker++;});
	window.setTimeout("refreshPhoto()", 8000);
} // refreshPhoto

//////////////////////////////////////////////////////////////////////////////////////////////
//                                 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 (document.all) return document.all[id];
	else return null;
} // __my_getElementById
//                                 INTERNAL HELPER FUNCTIONS                                //
//////////////////////////////////////////////////////////////////////////////////////////////