var aImages = new Array(2);
var aMenuHot = new Array(4);
var aMenuNot = new Array(4);
var Preload = new Image();
var Expires = 7776000000;	// (7.8 billion milliseconds is ninety days apparantly (90 * 24 * 60 * 60 * 1000)

aImages[0] = '/images/go-lo.jpg';
aImages[1] = '/images/go-hi.jpg';

aMenuHot[0] = '/images/about-us-red-hi.jpg';
aMenuHot[1] = '/images/products-red-hi.jpg';
aMenuHot[2] = '/images/services-red-hi.jpg';
aMenuHot[3] = '/images/utilities-red-hi.jpg';

aMenuNot[0] = '/images/about-us-red-lo.jpg';
aMenuNot[1] = '/images/products-red-lo.jpg';
aMenuNot[2] = '/images/services-red-lo.jpg';
aMenuNot[3] = '/images/utilities-red-lo.jpg';

for (i=0; i < aImages.length; i++) {
    Preload.src = aImages[i];
}

for (i=0; i < aMenuHot.length; i++) {
    Preload.src = aMenuHot[i];
}

/*
	DeleteCookie(name, path, domain
	name - name of the cookie
	[path] - path of the cookie (must be same as path used to create cookie)
	[domain] - domain of the cookie (must be same as domain used to create cookie)
	path and domain default if assigned null or omitted if no explicit argument proceeds
*/
function DeleteCookie(name, path, domain) {
	if (getCookie(name)) {
		document.cookie = name + "=" +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
  	}
}

/*
	DoSearch(Form)
	Form - Redirects to the search page with the form query appended
		   as a get request, if the form successfully validates.	
*/
function DoSearch(Form) {
	if (ValidateSearch(Form)) {
		window.location="/search/default.asp?Query=" + Form.Query.value;
	}
}

/*
	IsEmailAddress(Address)
	Address - The enail address to be tested for validity
*/
function IsEmailAddress(Address) {
	var Email=/^[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*\.([A-Za-z]){2,4}$/i;
	return(Email.test(Address));
}


/*
	FixDate(date)
	date - any instance of the Date object
	* hand all instances of the Date object to this function for "repairs"
*/
function FixDate(date) {
	var base = new Date(0);
	var skew = base.getTime();
	if (skew > 0)
		date.setTime(date.getTime() - skew);
}

/*
	GetCookie(Name,Default)
	Name - name of the desired cookie
	Default - value to return if cookie doesn't exist
	return string containing value of specified cookie or null if cookie does not exist
*/
function GetCookie(Name,Default) {
  var dc = document.cookie;
  var prefix = Name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return Default;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

/*
	GetPartState(ID,Default)
	Index		- The ID of the SidePart's chevron image
	Default		- The default state to use if no cookie exists
	Gets the specified SidePart's state from a cookie. Used when creating
	the WebPart to keep the state consisiten across the site
*/
function GetPartState(ID,Default) {
	return GetCookie('SideBar'+ID, Default);
}

/*
	MenuHot(Image, Index)
	Image	- The image to change the graphic on
	Index	- The graphic in the aMenuHot array
	Restores the normal image on the menu bar.
*/
function MenuHot(Image, Index) {
	Image.src = aMenuHot[Index];
}

/*
	MenuNot(Image, Index)
	Image	- The image to change the graphic on
	Index	- The graphic in the aMenuNot array
	Restores the normal image on the menu bar.
*/
function MenuNot(Image, Index) {
	Image.src = aMenuNot[Index];
}

/*
	PartState(Display)
	Display		- WebPart display state (valid values are none or block)
	Returns the text description of the display state, used to set the
	image's alt, title and src properties
*/
function PartState(Display) {
	if (Display == 'none') {
		return 'Expand';
	} else {
		return 'Collapse';
	}
}

/*
	QuickLinks()
	Creates the QuickLinks table from the array defined in the links.sc file
	for the current page.
*/
function QuickLinks() {
	var t = '<table>';
	for (i=0; i < aLinks.length; i++) {
		t = t + '<tr><td><a href="'+ aLinks[i][0] +'">';
		t = t + '<img name="GoQuick'+ i +'" src="/images/go-lo.jpg" border="0" ';
		t = t + 'alt="Go to '+ aLinks[i][1] +'" title="Go to '+ aLinks[i][1] +'" ';
		t = t + 'onmouseover="Swap(GoQuick'+ i +',1);" onmouseout="Swap(GoQuick'+ i +',0);">';
		t = t + '</a></td>';
		t = t + '<td class="qlt"><a href="'+ aLinks[i][0] +'">'+ aLinks[i][1] +'</a></td>';
		t = t + '</tr>';
	}
	t = t + '</table>';
	return t;
}

/*
	SearchForm(LastQuery)
	LastQuery - value to place in the text field for re-use if required
*/
function SearchForm(LastQuery) {
	var t = '<table border="0"><tr>';
	t = t + '<form name="SearchBox" method="GET" action="/search/default.asp" onsubmit="return ValidateSearch(this);" language="JavaScript">';
	t = t + '<td><input type="text" name="Query" value="'+ LastQuery +'" class="search"></td>';
	t = t + '<td><a href="#"><img border="0" name="GoSearch" class="ql" src="/images/go-lo.jpg" alt="Go" title="Go" ';
	t = t + 'onclick="DoSearch(SearchBox);" onmouseover="Swap(GoSearch,1);" onmouseout="Swap(GoSearch,0);"></a></td>';
	t = t + '</form></tr></table>';
	return t;
}

/*
	SetCookie(name, value, expires, path, domain, secure)
	name - name of the cookie
	value - value of the cookie
	[expires] - expiration date of the cookie (defaults to end of current session)
	[path] - path for which the cookie is valid (defaults to path of calling document)
	[domain] - domain for which the cookie is valid (defaults to domain of calling document)
	[secure] - Boolean value indicating if the cookie transmission requires a secure transmission
	* an argument defaults when it is assigned null as a placeholder
	* a null placeholder is not required for trailing omitted arguments
*/
function SetCookie(name, value, expires, path, domain, secure) {
	var curCookie = name + "=" + escape(value) +
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
	document.cookie = curCookie;
}

/*

	Not in use
	
	SetImage(Image,Src,Alt,Title)
	Image	- The image file to be modified
	Src		- The image src graphic
	Alt		- The text to use in the image's alt property
	Title	- The text to use in the image's title property
	Gets the specified SidePart's state from a cookie. Used when creating
	the WebPart to keep the state consisiten across the site

function SetImage(Image,Src,Alt,Title) {
	Image.src=Src
	Image.alt=Alt
	Image.title=Title
}

*/

/*
	SidePart(id,display,title,text)
	Values are the same as per WepPart()
	Creates WebPart for use in the SideBar
*/
function SidePart(id,display,title,text) {
	var s = PartState(display);
	var t = '<table class="sidepart" onclick="WebPartExpand(text'+ id +', SideBar'+ id +')">';
	t = t + '<tr>';
    t = t + '<td><img id="SideBar'+ id +'" alt="'+ s +'" title="'+ s +'" src="/images/b-'+ s+ '.gif" border="0" width="16" height="20"></td>';
    t = t + '<td width="100%"><table><tr>';
    t = t + '<td class="sb-title">'+ title +'</td></tr></table></td>';
    t = t + '</tr></table>';
	t = t + '<table width="100%" class="sidebody" id="text'+ id +'" style="display:'+ display +'" onclick="window.event.cancelBubble=true;">';
  	t = t + '<td class="sb-body" width="*">'+ text +'</td>';
  	t = t + '</tr></table>';
	return t;
}

/*
	Swap(Image,Index)
	Image	- The image to change the graphic on
	Index	- The graphic in the aImages array
	Sets the graphing on the specified image to use the graphic 
	specified in the aImages array
*/
function Swap(Image,Index) {
	Image.src = aImages[Index];
}

/*
	ValidateContact(Form)
	Form - The form to be validated.
*/
function ValidateContact(Form) {
	if (Form.Name.value == '') {
		alert("Please enter your name in the name field!");
		return false;
	} 
	else if (!IsEmailAddress(Form.Email.value)) {
		alert("Please enter a valid email address in the email field!");
		return false;
	} 
	else if (Form.Enquiry.value == '') {
		alert("Please enter details of your enquiry in the message field!");
		return false;
	} 
	else {
		return true;
	}
}

/*
	ValidateDownload(Form)
	Form - The form to be validated.
*/
function ValidateDownload(Form) {
	if (Form.Email.value == '') {
		return true;
	} 
	else if (IsEmailAddress(Form.Email.value)) {
		return true;
	} 
	else {
		alert("Please either enter a valid email address in the email field or leave it empty!");
		return false;
	}
}

/*
	ValidateSearch(Form)
	Form - The form (containing a text box called Query) to be validated
		   May be called by DoSeartch() or as 
		   Only checks that text box isn't empty at present the content validation
		   is done server side, will add more client side validation later.
*/
function ValidateSearch(Form) {
	if (Form.Query.value == '') {
		alert("Please enter something to seach for!");
		return false;
	} else {
		return true;
	}
}

/*
	WebPart(id,display,title,text)
	id 		- ID of the collapse/expand chevron image 
	display - initial display state (valid values are none or block)
	title	- text to display in the title area of the WebPart
	title	- text to display in the body area of the WebPart
*/
function WebPart(id,display,title,text) {
	var s = PartState(display);
	var t = '<table class="wptitle" onclick="WebPartExpand(text'+ id +', WebPart'+ id +')">';
	t = t + '<tr>';
	t = t + '<td><img border="0" src="/images/b-left.gif" width="3" height="20"></td>';
    t = t + '<td><img id="WebPart'+ id +'" alt="'+ s +'" title="'+ s +'" src="/images/b-'+ s +'.gif" border="0" width="16" height="20"></td>';
    t = t + '<td class="wp-title" width="100%">'+ title +'</td>';
	t = t + '<td><img border="0" src="/images/b-right.gif" width="3" height="20"></td>';
    t = t + '</tr></table>';
	t = t + '<table class="wpbody" id="text'+ id +'" style="display:'+ display +'" onclick="window.event.cancelBubble=true;">';
  	t = t + '<td class="wp-body" width="*">'+ text +'</td>';
  	t = t + '</tr></table><br>';
	return t;
}

/*
	WebPartExpand(Table,Image)
	Table	- The WebPart table id to toggle
	Image	- The WebPart image id to toggle
	Togles the WebPart or SidePart state between Collapsed and Expanded
	Also saves the new state to a cookie that can be used later
*/
function WebPartExpand(Table,Image) {
	if (Table.style.display =='block') {
		Table.style.display = 'none';
		Image.src='/images/b-Expand.gif';
		Image.alt='Expand';
		Image.title='Expand';
	} else {
		Table.style.display='block';
		Image.src='/images/b-Collapse.gif';
		Image.alt='Collapse';
		Image.title='Collapse';
	}
	var Now = new Date();
	FixDate(Now);
	Now.setTime(Now.getTime() + Expires);
	SetCookie(Image.id, Table.style.display, Now, '/');
}
