//*****************************************************************
// 
// IceColdinAlex.co.uk javascript functions
//
//*****************************************************************
//
// function goSelectedPage(pSelect)
// function goCheckedPage(pSelect)
// function goUrl(pUrl, pWinAction, pPopupName, pAnchor)
// function isDate(pFrmDateField)
// function isInteger(pFrmIntField)
// function isEmail(pFrmEmailField)
// function isUrl(pFrmUrlField)
// function isCode(pFrmCodeField)
// function textLength(pField, pMaxLength)
// function startTicker()
// function runTheTicker()
// function whatWidget()
// function addNewDropdownOption(pForm, pField, pValue, pName)
//*****************************************************************


//*****************************************************************
//
// Set window name
//
var gWinPrefix = 'ffix';
var gHomeWinName = gWinPrefix + 'Home';
var gPopupWinNamePattern = eval("/^" + gWinPrefix + "/");
if (!gPopupWinNamePattern.test(window.name)) window.name = gHomeWinName;
if (window.name == gHomeWinName) var gHomeWin = 'Y';


//*****************************************************************
//
// Function to go to URL of selected page in drop down list
//
function goSelectedPage(pSelect)
{
	var lUrl = pSelect.options[pSelect.selectedIndex].value;
	if (lUrl != '')
	{
		window.top.location.href = lUrl;
	}
	pSelect.selectedIndex = 0;
}


//*****************************************************************
//
// Function to go to URL of page identified by checking a radio button
//
function goCheckedPage(pSelect)
{
	var lUrl = pSelect.value;
	if (lUrl != '')
	{
		window.top.location.href = lUrl;
	}
}


//*****************************************************************
//
// Function to link to new URL (in a new window or in a popup window, if required)
//
// pWinAction = 'C',    Close the current window
//              'X',    Print the current window
//              'P',    Open url in a popup window
//              'H',    Open url in the 'home' window
//              null,   Open url in the current window
//              <name>, Open url in a named window
//
function goUrl(pUrl, pWinAction, pPopupName, pAnchor)
{
	// Set Url to current page, if not supplied
	var lUrl = pUrl;
	if (lUrl == '')
		lUrl = self.location.pathname + self.location.search;

	// Set up 'popup' Url by adding flag to Url, if not already there
	var lPopupPattern = /gPopup/;
	var lPopupUrl = lUrl;
	if (!lPopupPattern.test(lPopupUrl))
	{
		if (lUrl.indexOf('?') == -1)
			lPopupUrl += '?gPopup=Y';
		else
			lPopupUrl += '&gPopup=Y';
	}

	// Add anchor to Urls
	if (pAnchor != '')
	{
		lUrl += '#' + pAnchor;
		lPopupUrl += '#' + pAnchor;
	}

	// Set up popup name
	var lPopupName = gWinPrefix + 'Popup';
	if (pPopupName != '')
		lPopupName += pPopupName;

	// Close the current window
	if (pWinAction == 'C')
	{
		self.close();
	}

	// Print the current window (if not in a popoup, display in popup first and print that)
	else if (pWinAction == 'X')
	{
		if (gHomeWin == 'Y')
		{
			newWin = window.open(lPopupUrl, lPopupName, 'toolbar=0,scrollbars=1,location=0,status=0,menubar=0,resizable=1,directories=0,width=800,height=600');
			newWin.focus();
			newWin.print();
		}
		else
		{
			window.print();
		}
	}

	// Open url in a popup window
	else if (pWinAction == 'P')
	{
		newWin = window.open(lPopupUrl, lPopupName, 'toolbar=0,scrollbars=1,location=0,status=1,menubar=0,resizable=1,directories=0,width=800,height=600');
		newWin.focus();
	}

	// Open url in the 'home' window
	else if (pWinAction == 'H')
	{
		newWin = window.open(lUrl, gHomeWinName);
		newWin.focus();
	}

	// Open url in the current window
	else if (pWinAction == '')
	{
		if (gHomeWin == 'Y')
		{
			window.top.location.href = lUrl;
		}
		else
		{
			window.top.location.href = lPopupUrl;
		}
	}

	// Open url in a named window
	else
	{
		newWin = window.open(lUrl, pWinAction);
		newWin.focus();
	}
}


//*****************************************************************
//
// Function to validate a date field (dd/mm/yy or dd/mm/yyyy) - field is passed so that reformatted value can be wriiten back to form
//
function isDate(pFrmDateField)
{
	// If field is null, do not validate
	if (!pFrmDateField.value) return true;

	// Date must match pattern (d)d/(m)m/(y)y or (d)d/(m)m/yyyy (allows leading and trailing spaces)
	var lDate = pFrmDateField.value;
	var lDatePattern = /^\s*(0?[1-9]|[1-2][0-9]|3[0-1])\/(0?[1-9]|1[0-2])\/(19|20)?[0-9][0-9]\s*$/;
	if (!lDatePattern.test(lDate)) return false;

	// Remove all spaces and split into day, month and year
	lDate = lDate.replace(/\s/g, '');
	lDateParts = lDate.split('/');
	var lDay = parseInt(lDateParts[0], 10);
	var lMonth = parseInt(lDateParts[1], 10);
	var lYear = parseInt(lDateParts[2], 10);

	// Change 2 digit to 4 digit years
	if (lYear < 60) lYear += 2000;
	else if (lYear <100) lYear += 1900;

	// Convert to and from js date object (months run from 0 to 11!)
	var lDateObj = new Date(lYear, lMonth-1, lDay);
	lDayChk = lDateObj.getDate();
	lMonthChk = lDateObj.getMonth()+1;
	lYearChk = lDateObj.getYear();

	// Adjust year to account for js 'quirk' (returns 19nn years as nn!)
	if (lYearChk <1000) lYearChk += 1900;

	// Compare original and 'check' elements - if they don't all match, date is invalid
	if ((lDay != lDayChk) || (lMonth != lMonthChk) || (lYear != lYearChk)) return false;

	// Output valid (reformatted) date to form
	pFrmDateField.value = lDay + '/' + lMonth + '/' + lYear;
	return true;
}


//*****************************************************************
//
// Function to validate a time field (hh:mm) - field is passed so that reformatted value can be wriiten back to form
//
function isTime(pFrmTimeField)
{
	// If field is null, do not validate
	if (!pFrmTimeField.value) return true;

	// Time must match pattern (h)h:mm (allows leading and trailing spaces)
	var lTime = pFrmTimeField.value;
	var lTimePattern = /^\s*(0?[0-9]|1[0-9]|2[0-3]):[0-5][0-9]\s*$/;
	if (!lTimePattern.test(lTime)) return false;

	// Remove all spaces and split into hour and minute
	lTime = lTime.replace(/\s/g, '');
	lTimeParts = lTime.split(':');
	var lHour = parseInt(lTimeParts[0], 10);
	var lMinute = parseInt(lTimeParts[1], 10);

	// Check hour and minute are valid
	if (lHour > 23) return false;
	if (lMinute > 59)  return false;
	
	// Output valid (reformatted) time to form
	if (lMinute < 10)  lMinute = '0' + lMinute;
	pFrmTimeField.value = lHour + ':' + lMinute;
	return true;
}


//*****************************************************************
//
// Function to validate an integer field - field is passed so that reformatted value can be wriiten back to form
//
function isInteger(pFrmIntField)
{
	// If field is null, do not validate
	if (!pFrmIntField.value) return true;

	// Integer must match pattern (optional +/-; allows leading and trailing spaces)
	var lInt = pFrmIntField.value;
	var lIntPattern = /^\s*[-+]?[0-9]+\s*$/;
	if (!lIntPattern.test(lInt)) return false;

	// Remove all spaces and output valid integer to form
	lInt = lInt.replace(/\s/g, '');
	lInt = parseInt(lInt);
	pFrmIntField.value = lInt;
	return true;
}


//*****************************************************************
//
// Function to validate an e-mail field - field is passed so that reformatted value can be wriiten back to form
//
function isEmail(pFrmEmailField)
{
	// If field is null, do not validate
	if (!pFrmEmailField.value) return true;

	// Email must match pattern (allows leading and trailing spaces)
	var lEmail = pFrmEmailField.value;
	var lEmailPattern = /^\s*([a-zA-Z0-9_]|\-|\.)+@(([a-zA-Z0-9_]|\-)+\.)+[a-zA-Z]{2,4}\s*$/;
	if (!lEmailPattern.test(lEmail)) return false;

	// Remove all spaces and output valid email to form
	lEmail = lEmail.replace(/\s/g, '');
	pFrmEmailField.value = lEmail;
	return true;
}


//*****************************************************************
//
// Function to validate a url field - field is passed so that reformatted value can be wriiten back to form
//
function isUrl(pFrmUrlField)
{
	// If field is null, do not validate
	if (!pFrmUrlField.value) return true;

	// Url must match pattern (allows leading and trailing spaces)
	var lUrl = pFrmUrlField.value;
	var lUrlPattern = /^\s*http:\/\/[^']*\s*$/;
	if (!lUrlPattern.test(lUrl)) return false;

	// Remove all spaces and output valid Url to form
	lUrl = lUrl.replace(/\s/g, '');
	pFrmUrlField.value = lUrl;
	return true;
}


//*****************************************************************
//
// Function to validate a code field - field is passed so that reformatted value can be wriiten back to form
//
function isCode(pFrmCodeField)
{
	// If field is null, do not validate
	if (!pFrmCodeField.value) return true;

	// Code must match pattern (allows leading and trailing spaces)
	var lCode = pFrmCodeField.value;
	var lCodePattern = /^\s*[a-zA-Z0-9_]*\s*$/;
	if (!lCodePattern.test(lCode)) return false;

	// Remove all spaces and output valid Code to form
	lCode = lCode.replace(/\s/g, '');
	pFrmCodeField.value = lCode;
	return true;
}


//*****************************************************************
//
// Function to ensure length of field does not exceed its maximum (used for textareas)
//
function textLength(pField, pMaxLength)
{
	if (pField.value.length > pMaxLength) pField.value = pField.value.substring(0, pMaxLength);
	return;
}


//*****************************************************************
//
// Function to start Ticker
//
function startTicker()
{
	// Define run time values
	theCurrentStory     = -1;
	theCurrentLength    = 0;

	// Locate base objects
	if (document.getElementById)
	{	
		theAnchorObject = document.getElementById("tickerAnchor");
		runTheTicker();   	
	}
	else
	{
		document.write("<style>.ticki{display:none;}.ticko{border:0px; padding:0px;}</style>");
		return true;
	}
}


//*****************************************************************
//
// Function to run Ticker
//
function runTheTicker()
{
	var theCharacterTimeout = 50;
	var theStoryTimeout = 5000;
	var myTimeout;  

	// Go for the next story data block
	if(theCurrentLength == 0)
	{
		theCurrentStory++;
		theCurrentStory = theCurrentStory % theItemCount;
		theStorySummary = theSummaries[theCurrentStory].replace(/&quot;/g,'"');		
		theAnchorObject.href = theSiteLinks[theCurrentStory];
	}

	// Stuff the current ticker text into the anchor
	theAnchorObject.innerHTML = theStorySummary.substring(0,theCurrentLength) + whatWidget();

	// Modify the length for the substring and define the timer
	if(theCurrentLength != theStorySummary.length)
	{
		theCurrentLength++;
		myTimeout = theCharacterTimeout;
	}
	else
	{
		theCurrentLength = 0;
		myTimeout = theStoryTimeout;
	}

	// Call up the next cycle of the ticker
	setTimeout("runTheTicker()", myTimeout);
}


//*****************************************************************
//
// Function to generate 'widget' for Ticker
//
function whatWidget()
{
	var theWidgetOne        = "_";
	var theWidgetTwo        = "-";
	var theWidgetNone       = "";

	if(theCurrentLength == theStorySummary.length)
	{
		return theWidgetNone;
	}

	if((theCurrentLength % 2) == 1)
	{
		return theWidgetOne;
	}
	else
	{
		return theWidgetTwo;
	}
}


//*****************************************************************
//
// Function to add a new option to a dropdown list
//
function addNewDropdownOption(pForm, pField, pValue, pName)
{
	lNewOption = new Option(pName, pValue, false, false);
	lNewOptionNo = eval("document." + pForm + "." + pField + ".length");
	eval("document." + pForm + "." + pField + ".options[lNewOptionNo] = lNewOption");
	eval("document." + pForm + "." + pField + ".selectedIndex = lNewOptionNo");
}