// $Id: public.js 2348 2008-11-19 09:16:10Z Neil $

function addLoadEvent(func)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
	{
		window.onload = func;
	}
	else
	{
		window.onload = function()
		{
			oldonload();
			func();
		}
	}
}

// OPEN NEW WINDOW

var win = null;

function NewWindow(mypage,myname,w,h,scroll)
{
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable=yes';
	win = window.open(mypage,myname,settings);
}

function NewWindowToolbar(mypage,myname,w,h,scroll)
{
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings = 'width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=yes,directories=no,status=yes,menubar=yes,toolbar=yes,resizable=yes';
	win = window.open(mypage,myname,settings);
}

// FILTER INPUT

function filterInput(filterType, evt, allowDecimal, allowCustom)
{
	var keyCode, Char, inputField, filter = '';
	var alpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	var num = '0123456789';
	
	if (window.event)
	{
		keyCode = window.event.keyCode;
		evt = window.event;
	}
	else if (evt)
	{
		keyCode = evt.which;
	}
	else
	{
		return true;
	}
	
	if (filterType == 0)
	{
		filter = alpha;
	}
	else if (filterType == 1)
	{
		filter = num;
	}
	else if (filterType == 2)
	{
		filter = alpha + num;
	}
	
	if (allowCustom)
	{
		filter += allowCustom;
	}
	
	if (filter == '')
	{
		return true;
	}
	
	inputField = evt.srcElement ? evt.srcElement : evt.target || evt.currentTarget;
	
	if ((keyCode==null) || (keyCode==0) || (keyCode==8) || (keyCode==9) || (keyCode==13) || (keyCode==27))
	{
		return true;
	}
	
	Char = String.fromCharCode(keyCode);
	
	if ((filter.indexOf(Char) > -1))
	{
		return true;
	}
	else if (filterType == 1 && allowDecimal && (Char == '.') && inputField.value.indexOf('.') == -1)
	{
		return true;
	}
	else
	{
		return false;
	}
}

// CHECK TERMS

function checkTerms()
{
	var noError = true;
	if (document.getElementById('agree_terms').checked == true)
	{
		showLoading();
	}
	else
	{
		alert('You need to confirm that you agree to the Terms of Service before continuing with the purchase');
		noError = false;
	}
	return noError;
}

// CHECK TERMS AGREE

function checkTermsAgree()
{
	var noError = true;
	if (document.getElementById('agree_terms').selectedIndex == 0)
	{
		alert('You need to confirm whether you agree or disagree to the Terms of Service.');
		noError = false;
	}
	return noError;
}

// SHOW/HIDE LOADER

function showLoading()
{
	if (document.getElementById('gcOverlay').style.display == 'none' && document.getElementById('gcLoading').style.display == 'none')
	{
		document.getElementById('gcOverlay').style.display = 'block';
		document.getElementById('gcLoading').style.display = 'block';
	}
	else
	{
		document.getElementById('gcOverlay').style.display = 'none';
		document.getElementById('gcLoading').style.display = 'none';
	}
}
function hideLoading()
{
	if (document.getElementById('gcOverlay').style.display == 'block' && document.getElementById('gcLoading').style.display == 'block')
	{
		document.getElementById('gcOverlay').style.display = 'none';
		document.getElementById('gcLoading').style.display = 'none';
	}
	else
	{
		document.getElementById('gcOverlay').style.display = 'block';
		document.getElementById('gcLoading').style.display = 'block';
	}
	window.refresh();
}

// SHOW HIDE CONTENT

function HideContent(d)
{
	document.getElementById(d).style.display = "none";
}
function ShowContent(d)
{
	document.getElementById(d).style.display = "block";
}
function ReverseDisplay(d)
{
	if (document.getElementById(d).style.display == "none")
	{
		document.getElementById(d).style.display = "block";
	}
	else
	{
		document.getElementById(d).style.display = "none";
	}
}

// EXTERNAL LINKS

function externalLinks()
{
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++)
	{
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
		{
			anchor.target = "_blank";
		}
	}
}

addLoadEvent(function()
{
	externalLinks();
});
