// JavaScript Document
//JQuery stuff
$(document).ready(function(){

	$('a img').closest('a').css('border-bottom', 'none');

});
//End of JQuery stuff


//Fixtures Live Info (Important!)
var fPassKey = ''; // Replace this with key assigned to you.
var fStartPage = 'whatson:963'; // ...this can be varied - see User Guide
//var fStartPage = 'CLUBSTEAMS:963';
var fWidgetMode = true;

function getPageName()
{
	url = window.location.toString();

	if (url.indexOf("?") >= 0)
		url = url.substr(0, url.indexOf("?"));

	if (url.indexOf("#") >= 0)
		url = url.substr(0, url.indexOf("#"));

	url = url.substr(url.lastIndexOf("/") + 1);

	return url;
}

function runOnLoadTasks()
{
	if (getPageName() == 'findWHC.php')
		loadMap();

	setMenuBorder();
	var xx = setInterval("setMenuBorder()", 250);
	var yy = setTimeout("clearInterval(" + xx + ");", 10000);

	return true;
}

function runUnloadTasks(pageHitID)
{
	if (getPageName() == 'findWHC.php')
		GUnload();

	runAjax('pageHitUnload', 'UPDATE pageHit SET datePageClosed = Now() WHERE pageHitID = ' + pageHitID);

	return true;
}

function runAjax(ajaxRef, sql)
{
	ajaxObj = createAjaxObject(ajaxRef);
	if (ajaxObj == null)
		return false;

	ajaxObj.open("GET", "functions/runAjax.php?sql=" + sql, true);
	ajaxObj.send(null);

	return true;
}

function setMenuBorder()
{
	menuHeight = document.getElementById('sideMenu').offsetHeight;
	bodyHeight = document.getElementById('bodyContainer').offsetHeight;

	if (menuHeight < bodyHeight)
	{
		document.getElementById('bodyContainer').style.borderLeft = 'solid 1px #FF6633';
		document.getElementById('sideMenu').style.borderRight = 'none';
	}
	else
	{
		document.getElementById('bodyContainer').style.borderLeft = 'none';
		document.getElementById('sideMenu').style.borderRight = 'solid 1px #FF6633';
	}

	return;
}

function loadMap()
{
	var Info_HTML = '	<div style="width: 210px; height: 115px; font-size: 0.95em; padding-right: 10px; color: #000;">';
	Info_HTML += '			<p><b>Wilmslow Phoenix Sports Club</b><br/>Styal Rd<br/>Styal<br/>Wilmslow<br/>SK9 4HP</p>';
	Info_HTML += '			Your Post Code:<br />&nbsp;<input type="text" name="txtPostCode" id="txtPostCode" value="" size="8" />&nbsp;';
	Info_HTML += '			<a href="http://maps.google.co.uk/maps?daddr=SK9+4HP&saddr=" target="_blank" onClick="if (document.getElementById(\'txtPostCode\').value == \'\') { alert(\'Please enter your post code.\'); document.getElementById(\'txtPostCode\').focus(); return false; } else { this.href += document.getElementById(\'txtPostCode\').value; return true; }">Get Directions</a>';
	Info_HTML += '		</div>';

	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(53.34512,-2.238979), 13);
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.addControl(new GOverviewMapControl());

		var marker = new GMarker(new GLatLng(53.34512,-2.238979));

		map.addOverlay(marker);
		GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(Info_HTML); });
		marker.openInfoWindowHtml(Info_HTML);
	}
}

function addBookmark(title, url)
{
	if (window.sidebar)							// Mozilla Firefox Bookmark
	{
		window.sidebar.addPanel(title, url,"");
	}
	else if (window.external)					// IE Favorite
	{
		window.external.AddFavorite( url, title);
	}
	else if (window.opera && window.print)		// Opera Hotlist
	{
		return true;
	}
}

var charsPerSec = 15;
var secsToDelayAtEnd = 3;
var currItem = 0;
var maxChars = 85;
var mainTickerID = 0;
var mainTimeoutID = 0;
var thisChar = 0;
var str = '';
var tickerStr = '';
var runTicker = true;
var tickerRunning = false;
var waitingForDelay = false;
var items;
var links;
items = new Array();
links = new Array();

//  START OF:  NEWS TICKER CODE  //
function runNewsTicker(headlines, newsLinks)
{
	if (newsLinks.length < headlines.length)
		for (x = newsLinks.length; x < headlines.length; x++)
			newsLinks[x] = '';

	for (x = 0; x < headlines.length; x++)
		if (headlines[x] != '')
		{
			items[x] = headlines[x];
			links[x] = newsLinks[x];
		}

//		*****   USE THIS TO TRIM THE LENGTH OF A STRING TO FIT ONE LINE   ******
	for (x = 0; x < items.length; x++)
		if (items[x].length >= maxChars)
		{
			items[x] = items[x].substr(0, maxChars);
			items[x] = items[x].substr(0, items[x].lastIndexOf(' ')) + '...';
		}

	startTicker();
	return true;
}

function runNewsTicker1()
{
	args = runNewsTicker.arguments;

	for (x = 0; x < args.length; x++)
		if (args[x] != '')
			items[x] = args[x];

//		*****   USE THIS TO TRIM THE LENGTH OF A STRING TO FIT ONE LINE   ******
	for (x = 0; x < items.length; x++)
		if (items[x].length >= maxChars)
		{
			items[x] = items[x].substr(0, maxChars);
			items[x] = items[x].substr(0, items[x].lastIndexOf(' ')) + '...';
		}

	startTicker();
	return true;
}

function startTicker()
{
	clearTimeout(mainTimeoutID);
	waitingForDelay = false;

	if (currItem >= items.length)
		currItem = 0;

	tickerStr = items[currItem];

	if (runTicker && !tickerRunning)
	{
		mainTickerID = setInterval("newsTicker()", 1000 / charsPerSec);
		tickerRunning = true;
	}
}

function stopTicker()
{
	thisChar = 0;
	currItem++;

	clearInterval(mainTickerID);
	tickerRunning = false;
	mainTickerID = 0;
	waitingForDelay = true;
	mainTimeoutID = setTimeout("startTicker()", secsToDelayAtEnd * 1000);
}

function pauseTicker()
{
	runTicker = false;
}

function restartTicker()
{
	runTicker = true;

	if (!tickerRunning && !waitingForDelay)
	{
		tickerRunning = true;
		clearInterval(mainTickerID);
		mainTickerID = setInterval("newsTicker()", 1000 / charsPerSec);
	}
}

function newsTicker()
{
	//	*****   USE THIS TO STOP THE TICKER MID-SENTENCE ON ROLLOVER   *****
//	if (runTicker)
//	{
		var newsFeed = document.getElementById('newsFeed');

		if (thisChar >= tickerStr.length)
		{
			stopTicker();
			return false;
		}

		if (thisChar == 0)
		{
			newsFeed.innerHTML = '';
			newsFeed.href = 'news.asp';

			if (links[currItem] != '')
				newsFeed.href += '#' + links[currItem];
		}

		currText = newsFeed.innerHTML;

		if (currText.substr(currText.length - 1, 1) == '_')
			currText = currText.substr(0, currText.length - 1);

		currText += tickerStr.substr(thisChar, 1);

		if (thisChar + 1 < tickerStr.length)
			currText += '_';

		newsFeed.innerHTML = currText;
		thisChar++;
//	}
}
//  END OF:    NEWS TICKER CODE  //

function runOnloadTasks(page, headlines, newsLinks)
{
	runNewsTicker(headlines, newsLinks);
}

function checkRegister(form)
{
	alert('checkRegister');
	return false;

	if (!checkField(form.txtFirstName, 'Please enter your first name.'))
		return false;

	if (!checkField(form.txtSurname, 'Please enter your surname.'))
		return false;

	if (!checkDate(form.selDOBDay.value, form.selDOBMonth.value, form.selDOBYear.value))
	{
		alert('Please enter your date of birth. The date entered is not a valid date.');
		form.selDOBDay.focus();
		return false;
	}

	if (!checkField(form.selSex, 'Please select your sex from the list.'))
		return false;

	if (!checkField(form.txtAddress1, 'Please enter your street address.'))
		return false;

	if (!checkField(form.txtPostCode, 'Please enter your post code.'))
		return false;

	if (!checkEmail(form.txtEmail, 'Please enter your email address.', 'Please enter your email address. The value entered is not a valid email address.'))
		return false;

	if (!checkField(form.txtPass, 'Please enter a password.'))
		return false;

	if (form.txtPass.value.length < 6 || form.txtPass.value.length > 16)
	{
		alert('Your password must be between 6 and 16 characters. Please use only letters or numbers (a - z or 0 - 9).');
		form.txtPass.value = '';
		form.txtPass2.value = '';
		form.txtPass.focus();
		return false;
	}

	if (!checkField(form.txtPass2, 'Please confirm your password.'))
		return false;

	if (form.txtPass.value != form.txtPass2.value)
	{
		alert('Passwords do not match. Please re-enter your password.');
		form.txtPass.value = '';
		form.txtPass2.value = '';
		form.txtPass.focus();
		return false;
	}

	str = form.txtPass.value.split('');
	for (x = 0; x < str.length; x++)
	{
		if (!((str[x] >= 'a' && str[x] <= 'z') || (str[x] >= 'A' && str[x] <= 'Z') || (str[x] >= '0' && str[x] <= '9')))
		{
			alert('Your password must be between 6 and 16 characters. Please use only letters or numbers.');
			form.txtPass.value = '';
			form.txtPass2.value = '';
			form.txtPass.focus();
			return false;
		}
	}

	if (form.txtPassHint.value != '')
	{
		if (form.txtPassHint.value.indexOf(form.txtPass.value) != -1)
		{
			alert('Password hint must not contain your password.');
			form.txtPassHint.focus();
			return false;
		}
	}

	return true;
}

function updateSex(titleObj, form)
{
	sexObj = form.selSex;
	title = titleObj.options[titleObj.selectedIndex].text;

	switch(title)
	{
		case 'Mr':
		case 'Sir':
		case 'Lord':
			sex = 'M';
			break;
		case 'Miss':
		case 'Mrs':
		case 'Ms':
		case 'Lady':
			sex = 'F';
			break;
		default:
			sex = '';
			break;
	}

	sexObj.value = sex;

	return true;
}

function popUp(page, width, height)
{
	if (isNaN(width) || width == '') { width = 400; }
	if (isNaN(height) || height == '') { height = 250; }

	window.open(page, 'popUpWindow','width=' + width + ',height=' + height + ',toolbar=0,location=0,directories=0,menubar=0,scrollbars=1');
	return true;
}

function checkChangePass(form)
{
	if (!checkField(form.txtCurrPass, 'Please enter your current password.'))
		return false;

	if (!checkField(form.txtNewPass, 'Please enter your new password.'))
		return false;

	if (!checkField(form.txtConfPass, 'Please confirm your new password.'))
		return false;

	if (form.txtNewPass.value.length < 6 || form.txtNewPass.value.length > 16)
	{
		alert('Your password must be between 6 and 16 characters. Please use only letters or numbers (a - z or 0 - 9).');
		form.txtNewPass.value = '';
		form.txtConfPass.value = '';
		form.txtNewPass.focus();
		return false;
	}

	if (form.txtNewPass.value != form.txtConfPass.value)
	{
		alert('Passwords do not match. Please re-enter your password.');
		form.txtNewPass.value = '';
		form.txtConfPass.value = '';
		form.txtNewPass.focus();
		return false;
	}

	str = form.txtNewPass.value.split('');
	for (x = 0; x < str.length; x++)
	{
		if (!((str[x] >= 'a' && str[x] <= 'z') || (str[x] >= 'A' && str[x] <= 'Z') || (str[x] >= '0' && str[x] <= '9')))
		{
			alert('Your password must be between 6 and 16 characters. Please use only letters or numbers.');
			form.txtNewPass.value = '';
			form.txtConfPass.value = '';
			form.txtNewPass.focus();
			return false;
		}
	}

	if (form.txtPassHint.value != '')
	{
		if (form.txtPassHint.value.indexOf(form.txtNewPass.value) != -1)
		{
			alert('Password hint must not contain your password.');
			form.txtPassHint.focus();
			return false;
		}
	}

	return true;
}

function checkLogin(form)
{
	if (!checkField(form.txtEmail, 'Please enter your email address.'))
		return false;

	if (!checkField(form.txtPass, 'Please enter your password.'))
		return false;

	return true;
}

function updateCharsLeft(textEl, charsLeftName, maxChars)
{
	charsLeftEl = document.getElementById(charsLeftName);

	charsLeft = maxChars - textEl.value.length;

	if (charsLeft < 0)
	{
		textEl.value = textEl.value.substr(0, maxChars);
		charsLeft = 0;
	}

	charsLeftEl.innerHTML = charsLeft + ' characters remaining';

	return true;
}

function showHideEl(elID)
{
	if (document.getElementById(elID).style.display == '')
		document.getElementById(elID).style.display = 'none';
	else
		document.getElementById(elID).style.display = '';

	return;
}
