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

//This is the normal indexOf function, but IE8 doesnt support it, so re-define it
Array.prototype.indexOf = function(str)
{
	//go thru this object (array) looking for the string passed in
	
	for (var i = 0; i < this.length; i++)
	{
		if (this[i] == str)
			return i;
	}
	
	return -1;
}

function IsIE()
{
	return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
}


function GetCheckedRadioValue(radioObj)
{
	if(!radioObj)
		return "";
		
	var radioLength = radioObj.length;
	
	if(radioLength == undefined)
	{
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	}
	
	for(var i = 0; i < radioLength; i++)
	{
		if(radioObj[i].checked)
			return radioObj[i].value;
	}
	return "";
}


function getPageSize()
{
	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY)
	{
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	}
	else if	(document.body.scrollHeight > document.body.offsetHeight)
	{ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	}
	else
	{ // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;

	if (self.innerHeight)
	{	// all except Explorer
		if(document.documentElement.clientWidth)
			windowWidth = document.documentElement.clientWidth;
		else
			windowWidth = self.innerWidth;
		
		windowHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
	{ // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	}
	else if (document.body)
	{ // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight)
		pageHeight = windowHeight;
	else
		pageHeight = yScroll;

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth)
		pageWidth = xScroll;
	else
		pageWidth = windowWidth;

	return [pageWidth,pageHeight];
}


function getScrollXY()
{
	var scrOfX = 0, scrOfY = 0;
	
	if(typeof(window.pageYOffset) == 'number')
	{
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	}
	else if(document.body && (document.body.scrollLeft || document.body.scrollTop))
	{
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	}
	else if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop))
	{
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}

	return [scrOfX, scrOfY];
}

function DisableScreen()
{
	var arrayPageSize = getPageSize();
	var PageWidth = arrayPageSize[0];
	var PageHeight = arrayPageSize[1];

	var TransparentLayer = document.createElement('div');
	TransparentLayer.id = "TransparentLayer";
	TransparentLayer.style.width = "100%";
	TransparentLayer.style.height = PageHeight + "px";
	TransparentLayer.style.backgroundColor = "black";
	TransparentLayer.style.position = "absolute";
	TransparentLayer.style.top = "0";
	TransparentLayer.style.left = "0";
	TransparentLayer.style.zIndex = "2";
//	document.childNodes[1].appendChild(TransparentLayer);				//append to page
//	window.document.childNodes[1].childNodes[1].appendChild(TransparentLayer);	//append to page
//	document.childNodes[1].childNodes[1].appendChild(TransparentLayer);		//append to page
	document.getElementsByTagName('body')[0].appendChild(TransparentLayer);		//append to page
	FadeInElement(id="TransparentLayer", Opacity=0, MaxOpacity=80, TransitionPause=5, StepSize=10);

	return TransparentLayer;
}

function EnableScreen()
{
	var TransparentLayer = document.getElementById('TransparentLayer');
	
	if (TransparentLayer)
	{
		FadeOutElement(id="TransparentLayer", Opacity=80, MinOpacity=0, TransitionPause=5, StepSize=10);
		setTimeout("DeleteElement('TransparentLayer')", 500);
	}
}

function DeleteElement(id)
{
	var Element = document.getElementById(id);
	
	if(Element)
	{
		//document.childNodes[1].removeChild(Element);
		//window.document.childNodes[1].childNodes[1].removeChild(Element);
		document.getElementsByTagName('body')[0].removeChild(Element);
	}
	else
		ShowPopup(true, "ERROR", "Element '"+ id +"' does not exist.");
}


function FadeInElement(id, Opacity, MaxOpacity, TransitionPause, StepSize)
{
	if (Opacity > MaxOpacity)
		Opacity = MaxOpacity;

	if (Opacity < MaxOpacity)
	{
		Opacity += StepSize;
		SetOpacity(id, Opacity);
		setTimeout("FadeInElement('"+id+"', "+Opacity+", "+MaxOpacity+", "+TransitionPause+", "+StepSize+")", TransitionPause);
	}
}


function FadeOutElement(id, Opacity, MinOpacity, TransitionPause, StepSize)
{
	if (Opacity < MinOpacity)
		Opacity = MinOpacity;

	if (Opacity > MinOpacity)
	{
		Opacity -= StepSize;
		SetOpacity(id, Opacity);
		setTimeout("FadeOutElement('"+id+"', "+Opacity+", "+MinOpacity+", "+TransitionPause+", "+StepSize+")", TransitionPause);
	}
}


function SetOpacity(id, Value)
{
	if (document.getElementById(id))
	{
		var object = document.getElementById(id).style;

		object.opacity = (Value / 100);
		object.MozOpacity = (Value / 100);
		object.KhtmlOpacity = (Value / 100);
		object.filter = "alpha(opacity=" + Value + ")";
	}
}

function ShowPopup(Title, Msg)
{
	//Create a modal pop-up window
	
	// 1. Disable window
	// 2. Create layer on top.  It should be in centre of page and have a 'close' button that re-enables screen
	
	// 1.
	DisableScreen();
	
	// 2.
	var PageDimensionsArray = getPageSize();
	var PageWidth = PageDimensionsArray[0];
	var PopupWidth = 432;

	var PageScrollArray = getScrollXY();
	var ScrollY = PageScrollArray[1];
	
	var ErrorWrapper = document.createElement("div");
	ErrorWrapper.id = "ErrorWrapper";
	ErrorWrapper.style.top = ScrollY + 200 + "px";			//get height automatically
	//ErrorWrapper.style.top = "394px";				//set at certain spot on page
	ErrorWrapper.style.left = (PageWidth-PopupWidth)/2 + "px";

	var ErrorHeader = document.createElement("div");
	ErrorHeader.id = "ErrorHeader";

	var ErrorTitle = document.createElement("div");
	ErrorTitle.id = "ErrorTitle";
	ErrorTitle.innerHTML = Title;

	var ErrorCloseLink = document.createElement("div");
	ErrorCloseLink.id = "ErrorCloseLink";

	var MyLink = document.createElement("a");
	MyLink.id = "Links";
	MyLink.innerHTML = "close";
	MyLink.href = "javascript: HidePopup();";

	var ErrorBodyWrapper = document.createElement("div");
	ErrorBodyWrapper.id = "ErrorBodyWrapper";

	var ErrorBodyText = document.createElement("div");
	ErrorBodyText.id = "ErrorBodyText";
	ErrorBodyText.innerHTML = Msg;

	var ErrorFooter = document.createElement("div");
	ErrorFooter.id = "ErrorFooter";

	ErrorWrapper.appendChild(ErrorHeader);
	ErrorHeader.appendChild(ErrorTitle);
	ErrorHeader.appendChild(ErrorCloseLink);
	ErrorCloseLink.appendChild(MyLink);
	ErrorWrapper.appendChild(ErrorBodyWrapper);
	ErrorBodyWrapper.appendChild(ErrorBodyText);
	ErrorWrapper.appendChild(ErrorFooter);

	document.getElementsByTagName('body')[0].appendChild(ErrorWrapper);		//append to page
}


function HidePopup()
{
	// 1. Destroy the help window that was created
	// 2. Re-enable screen
	
	var ErrorWrapper = document.getElementById('ErrorWrapper');
	//document.childNodes[1].removeChild(ErrorWrapper);
	//window.document.childNodes[1].childNodes[1].removeChild(ErrorWrapper);
	document.getElementsByTagName('body')[0].removeChild(ErrorWrapper);
	
	EnableScreen();
}


function IsNumeric(value)
{
	if (value == null || !value.toString().match(/^[-]?\d*\.?\d*$/) || value == "")
		return false;

	return true;
}


function IsValidEmail(str)
{
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})+$/;

	return reg.test(str);
}


function IsValidPostalCode(str)
{
	//Format is "A1B 2C3" or A1B2C3"
	
	var reg = /^[A-Za-z][0-9][A-Za-z]([ ])?[0-9][A-Za-z][0-9]$/;

	return reg.test(str);
}

function IsValidPhoneNumber(str)
{
	/*
		Valid formats are:
		1231231234
		123-123-1234
		123-123-1234 12345 (extension)
		
	*/
	var reg = /^([0-9])+$/;
	
	if (str.length == 10 && reg.test(str))
		return true;
	else if (
		(str.length >= 12) &&
		(str.length <= 18) &&
		(str.charAt(3) == "-") &&
		(str.charAt(7) == "-") &&
		reg.test(str.substr(0,3)) &&
		reg.test(str.substr(4,3)) &&
		reg.test(str.substr(8,4))
	   )
	{
		if (str.length > 12)
		{
			if ((str.charAt(12) == " ") && reg.test(str.substr(13)))
				return true;
			else
				return false;
		}
		else
			return true;
	}
	else
		return false;
}


function SetOnClick(object, action)
{
	if (IsIE())
		object.onclick = new Function(action);
	else
		object.setAttribute('onclick',action)
}

