/* common.js - Javascript code used throughout rexrealtybarbados.com. */
//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 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

// Document object referencing
if (!document.w3cdom) document.w3cdom = function() {
                         return (document.getElementById) ? true : false;
                      }

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

if (!document.getElementRef)
   document.getElementRef = function(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];
   } // document.getElementRef

function disableRightClick(e)
{
	var message = "Request denied; right-click feature has been disabled for this site.";

	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) {
		window.alert(message);
		return false;
	}
	else if (document.addEventListener || document.layers) {
		if (e.button == 2 || e.button == 3) {
			window.alert(message);
			return false;
		}
	}
} // disableRightClick

function writeDate(dateObj)
{
	var days = [ 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' ];
	var months = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July',
	               'August', 'September', 'October', 'November', 'December' ];
	var dateTimeString = days[dateObj.getDay()] + '&nbsp;' + months[dateObj.getMonth()] + '&nbsp;' +
	                     dateObj.getDate() + ',&nbsp;' + dateObj.getFullYear();
	document.writeln(dateTimeString);
} // writeDate

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

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

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 regExpNumber = /^\([0-9]{3}\)\s?[0-9]{3}-[0-9]{4}$/;
	var errors = ['Request denied: One or more of the form entries (name or email address) is empty.',
	              'Request denied: Your email address has an invalid format. An example of a ' +
	              'correct format is sender@hotmail.com.', 'Request denied: Your phone number has an invalid format. ' +
	              'The correct format is (555) 555-5555, for example (246) 266-1000.',
	              'Request denied: Your fax number has an invalid format. The correct format is (555) 555-5555, ' +
	              'for example (866) 244-1000.'];
	
	if (phone == "Format: (555) 555-5555")
		phone = "";
	if (fax == "Format: (555) 555-5555")
		fax = "";
	if (!contactName || !contactEmail)
		window.alert(errors[0]);
	else if (!checkEmailAddress(frm.c_email, null, false))
		window.alert(errors[1]);
	else if (phone || fax) {
		// If a telephone # and/or fax number was/were given, do minimal validation on format.
		if (phone && !(regExpNumber.test(phone)))
			window.alert(errors[2]);
		else if (fax && !(regExpNumber.test(fax)))
			window.alert(errors[3]);
		else
			is_valid = true;
	}
	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

function checkBookingsForm(f)
{
		return true;
} // checkBookingsForm