/*
**
**	This section is for functions relevant to the 'page lifecycle'.
**
*/
//A better way of programmatically adding javascript OnLoad events.
//Using this method, previously defined events are not overwritten
//instead they're 'stacked up' so they all get run at page load time.
function addLoadEvent(func)
{
	var oldonload = window.onload;

	if (typeof window.onload != 'function')
	{
		window.onload = func;
	} else {
		window.onload = function()
		{
			oldonload();
			func();
		}
	}
}

function addBeforeUnloadEvent(func)
{
	var oldonbeforeunload = window.onbeforeunload;

	if (typeof window.onbeforeunload != 'function')
	{
		window.onbeforeunload = func;
	} else {
		window.onbeforeunload = function()
		{
			oldonbeforeunload();
			func();
		}
	}
}




/*
**
**	This section is for general util functions that may be useful in many situations.
**
*/
function GetPageElement( id )
{
	var element;

	if (document.getElementById)
		element = document.getElementById( id );
	else if (document.layers)
		element = document.layers[ id ];
	else
		element = document.all[ id ];

	return element;
}


//This function used the capture the [enter] key being used inside a form field, and 'redirect' the action to a specified control.
var defaultControlId;
function DefaultSubmit(controlId, evt)
{
	if (navigator.appName == "Netscape")
		charCode = evt.which 
	else 
		charCode = evt.keyCode;

	if (charCode == 13)
	{
		var defaultControl = GetPageElement(controlId);
		if (defaultControl != null)
		{
			defaultControl.focus();
			defaultControl.click();

			//Stop the browser from processing the event any further...
			return false;
		}
	}

	return true;
}


//trim( str ):  Returns str with leading and trailing white space characters removed.
function trim( str )
{
	return str.replace(/^\s*|\s*$/g,"");
}

//SetHtmlContent( elemId, value ): Sets the innerHTML property of the element with id elemId, to value.
function SetHtmlContent( elemId, value )
{
	var contentDiv = GetPageElement(elemId);
	contentDiv.innerHTML = value;
}

//ClearHtmlContent( elemId  ): Clears the innerHTML property of the element with id elemId.
function ClearHtmlContent( elemId )
{
	var contentDiv = GetPageElement(elemId);
	contentDiv.innerHTML = "";
}

function DelayRedirect(numberOfMinutes, url)
{
	setTimeout("Redirect('" + url + "')", numberOfMinutes*60*1000 );
}

function Redirect(url)
{
	window.location.replace( url );
}





/*
**
**	This section is for useful classes/object modifications.
**
*/
Array.prototype.inArrayIndex = function (value)
// If the passed value is found in the array, returns
// it's index within the array.  Returns -1 if it is not.
{
	var i;
	for (i=0; i < this.length; i++) {
		// Matches identical (===), not just similar (==).
		if (this[i] === value) {
			return i;
		}
	}
	return -1;
};


Array.prototype.inArrayBool = function (value)
// Returns true if the passed value is found in the
// array.  Returns false if it is not.
{
	return ( this.inArrayIndex(value) > -1 );
};


Array.prototype.remove = function (element)
// Removes an element from the array, if it exists.
// Returns a boolean indicating if the element was removed.
{
	var result = false;
	var array = [];

	for (var i = 0; i < this.length; i++)
	{
		if (this[i] == element)
			result = true;
		else
			array[array.length] = this[i];
	}

	this.clear();
	for (var i = 0; i < array.length; i++)
	{
		this[this.length] = array[i];
	}

	array = null;
	return result;
};


Array.prototype.removeFrom = function (index)
// Removes an element from the array at the specified index.
{
	var array = [];

	for (var i = 0; i < this.length; i++)
	{
		if (i != index)
			array[array.length] = this[i];
	}

	this.length = 0;
	for (var i = 0; i < array.length; i++)
	{
		this[this.length] = array[i];
	}

	array = null;
};


function ensureHttps()
{
	//If the incoming request is http (on a recognised host name), get the browser to re-request using https...
	var secureHosts = new Array('www.sell2wales.co.uk', 'www.sell2wales.com', 'www.selltowales.co.uk', 'www.selltowales.com', 'www.gwerthwchigymru.co.uk');

	if (window.location.protocol == 'http:' && secureHosts.inArrayBool(window.location.host))
	{
		window.location.replace('https://' + window.location.host + window.location.pathname + window.location.search + window.location.hash)
	}
}

ensureHttps();
