//*****************************************************************
// 
// IceColdinAlex.co.uk javascript functions
//
//*****************************************************************
//
// function ...
//
//*****************************************************************


//*****************************************************************
//
// Function to set up page navigator
//
var photoCount = 0;

function setPageNav()
{
	// Enable/disable next and prev page links as appropriate
	document.getElementById('pagelinkP').className=(firstPageInd == 'Y')? 'inactivecolor' : 'unselectedcolor';
	document.getElementById('pagelinkR').className= 'unselectedcolor';
	document.getElementById('pagelinkN').className=(lastPageInd == 'Y')? 'inactivecolor' : 'unselectedcolor';

	// Process page photos
	if (photoCount > 0)
		loadPhoto();
}


//*****************************************************************
//
// Functions to switch photos
//
var currPhotoNo;
var photoCount;
var maxDivHeight;

function loadPhoto()
{
	// Display (and then hide) all photo divs in turn to calculate max height
	for(var i=1; i<=photoCount; i++)
	{
		photoDiv = document.getElementById('photo' + i);
		photoDiv.style.display = "block";
		divHeight = photoDiv.offsetHeight;
		photoDiv.style.display = "none";
		maxDivHeight=(divHeight<=maxDivHeight)? maxDivHeight : divHeight;
	}
			
	// Set height of all photo divs to max height and set photo links to unselected
	for(var i=1; i<=photoCount; i++)
	{
		document.getElementById('photo' + i).style.height = maxDivHeight + 'px';
		if (photoCount > 1) document.getElementById('photolink' + i).className = 'unselectedcolor';
	}
			
	// Hide 'loading' message and display first photo
	document.getElementById('photoload').style.display = "none";
	switchPhoto(1);
}

function switchPhoto(photoNo)
{
	// Hide current photo div and display new photo div
	if (currPhotoDiv = document.getElementById('photo' + currPhotoNo)) currPhotoDiv.style.display = "none";
	document.getElementById('photo' + photoNo).style.display = "block";

	// If photo links are displayed ...
	if (photoCount > 1)
	{
		// ... set current photo link to unselected and new photo link to selected ...
		if (currPhotoLink = document.getElementById('photolink' + currPhotoNo)) currPhotoLink.className = 'unselectedcolor';
		document.getElementById('photolink' + photoNo).className = 'selectedcolor';

		// ... and enable/disable next and prev photo links as appropriate
		document.getElementById('photolinkP').className=(photoNo <= 1)? 'inactivecolor' : 'unselectedcolor';
		document.getElementById('photolinkN').className=(photoNo >= photoCount)? 'inactivecolor' : 'unselectedcolor';
	}

	currPhotoNo = photoNo;
}

function prevPhoto()
{
	if (currPhotoNo > 1)
		switchPhoto(currPhotoNo - 1);
}

function nextPhoto()
{
	if (currPhotoNo < photoCount)
		switchPhoto(currPhotoNo + 1);
}


//*****************************************************************
//
// Functions to change photo and page nav links on rollover
//
function linkOver(target)
{
	if (target.id == 'photolinkP')
		target.className=(currPhotoNo <= 1)? 'inactivecolor' : 'selectedcolor';
	else if (target.id == 'photolinkN')
		target.className=(currPhotoNo >= photoCount)? 'inactivecolor' : 'selectedcolor';
	else if (target.id == 'pagelinkP')
		target.className=(firstPageInd == 'Y')? 'inactivecolor' : 'selectedcolor';
	else if (target.id == 'pagelinkN')
		target.className=(lastPageInd == 'Y')? 'inactivecolor' : 'selectedcolor';
	else
		target.className = 'selectedcolor';
}

function linkOut(target)
{
	if (target.id == 'photolinkP')
		target.className=(currPhotoNo <= 1)? 'inactivecolor' : 'unselectedcolor';
	else if (target.id == 'photolinkN')
		target.className=(currPhotoNo >= photoCount)? 'inactivecolor' : 'unselectedcolor';
	else if (target.id == 'pagelinkP')
		target.className=(firstPageInd == 'Y')? 'inactivecolor' : 'unselectedcolor';
	else if (target.id == 'pagelinkN')
		target.className=(lastPageInd == 'Y')? 'inactivecolor' : 'unselectedcolor';
	else
		target.className = 'unselectedcolor';

	if (photoCount > 1)
		document.getElementById('photolink' + currPhotoNo).className = 'selectedcolor';
}


//*****************************************************************
//
// Functions to display / hide 'divs'
//
var currDivId = new Array();
var graphicDir;

function switchDiv(divSet, newDivId)
{
	if (currDivId[divSet])
	{
		if (newDivId == currDivId[divSet])
		{
//			toggleDiv(newDivId);
		}
		else
		{
			toggleDiv(currDivId[divSet]);
			toggleDiv(newDivId);
			currDivId[divSet] = newDivId;
		}
	}
	else
	{
		toggleDiv(newDivId);
		currDivId[divSet] = newDivId;
	}
}

function hideDiv(divSet, DivId)
{
	if (currDivId[divSet])
	{
		currDivId[divSet] = '';
	}

	toggleDiv(DivId);
}

function toggleDiv(divId, imgId)
{
	if (document.getElementById(divId))
	{
		target = document.getElementById(divId);
		target.style.display = (target.style.display == "none")? "block" : "none";
	}

	if (imgId && document.getElementById(imgId))
	{
		img = document.getElementById(imgId);
		img.src = (target.style.display == "none")? "/" + graphicDir + "/toggle-show.gif" : "/" + graphicDir + "/toggle-hide.gif";
	}
}


//*****************************************************************
//
// Function to open a URL in a named window (if popup is set, window is sized and toolbars removed)
//
function goUrl(url, winName, popup)
{
	if (popup)
		winOptions = 'toolbar=0,scrollbars=1,location=0,status=0,menubar=0,resizable=1,directories=0,width=650,height=500';
//		winOptions = '';
	else
		winOptions = '';

	if (winName)
	{
		newWin = window.open(url, winName, winOptions);
		newWin.focus();
	}
	else
	{
		window.top.location.href = url;
	}

}


//*****************************************************************
//
// 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 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)
{return true;
	// 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*[-+]?\s*\d+\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 a decimal field - field is passed so that reformatted value can be wriiten back to form
//
function isDecimal(pFrmDecField)
{
	// If field is null, do not validate
	if (!pFrmDecField.value) return true;

	// Decimal must match pattern (optional +/-; allows leading and trailing spaces)
	var lDec = pFrmDecField.value;
	var lDecPattern = /^\s*[-+]?\s*(\d+)|(\d*\.\d+)\s*$/;
	if (!lDecPattern.test(lDec)) return false;

	// Remove all spaces and output valid decimal to form
	lDec = lDec.replace(/\s/g, '');
	lDec = parseFloat(lDec);
	pFrmDecField.value = lDec;
	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;
}
