var Expires = 7776000000;	// (7.8 billion milliseconds is ninety days apparantly (90 * 24 * 60 * 60 * 1000)																			

var aHide = new Array(2);
aHide[0] = '/buttons/hide-lo.gif';
aHide[1] = '/buttons/hide-hi.gif';
aHide[2] = '/buttons/open-lo.gif';
aHide[3] = '/buttons/open-hi.gif';

var aShow = new Array(2);
aShow[0] = '/buttons/show-lo.gif';
aShow[1] = '/buttons/show-hi.gif';
aShow[2] = '/buttons/closed-lo.gif';
aShow[3] = '/buttons/closed-hi.gif';

var aShowNot = new Array(3);
aShowNot[0] = '/buttons/btnShowNone0.png';
aShowNot[1] = '/buttons/btnShowMine0.png';
aShowNot[2] = '/buttons/btnShowAll0.png';

var aShowHot = new Array(3);
aShowHot[0] = '/buttons/btnShowAll1.png';
aShowHot[1] = '/buttons/btnShowMine1.png';
aShowHot[2] = '/buttons/btnShowAll1.png';

/*
	ReadyMenu(MenuMask)
	Menu - The anded value of the menus required on a page
	May be called during body OnLoad to preload any images 
	that the page may required (for hot menu items).
*/
function ReadyMenu(MenuMask) {	
	// Preload the main menu buttons
	PreloadMain();
	if ((MenuMask & 1) == 1) {
		// Preload the Hide/Show hot buttons
		Preload(aHide[1]);
		Preload(aShow[1]);
	}
	if ((MenuMask & 2 ) == 2) {
		// Preload the Product Menu buttons
		PreloadProduct();
	}
	if ((MenuMask & 4) == 4) {
		// Preload the FAQs/Videos/Updates filter hot buttons
		for (i=0; i < aShowNot.length; i++) {
			Preload(aShowHot[i]);
		}
	}
}

function PreloadMain() {
	for (i=0; i < 2; i++) {
		Preload('/buttons/btsc6gm_' + i + '.png');	//Home
		Preload('/buttons/bt3c6gm_' + i + '.png');	//Product
		Preload('/buttons/btrc6gm_' + i + '.png');	//Services
		Preload('/buttons/bt6c6gm_' + i + '.png');	//Support
		Preload('/buttons/bt2c6gm_' + i + '.png');	//About Us
		Preload('/buttons/bttc6gm_' + i + '.png');	//Contact
		Preload('/buttons/btpc6gm_' + i + '.png');	//View
		Preload('/buttons/btoc6gm_' + i + '.png');	//Search
	}
	Preload('/images/bgMenuBar28.png');	//Background
}

function PreloadProduct() {
	for (i=0; i < 3; i++) {
		Preload('/buttons/bt14kyi_' + i + '.png');	//Overview
		Preload('/buttons/btc4kyi_' + i + '.png');	//Guide
		Preload('/buttons/btd4kyi_' + i + '.png');	//Details
		Preload('/buttons/bt74kyi_' + i + '.png');	//Videos
		Preload('/buttons/btq4kyi_' + i + '.png');	//FAQs
		Preload('/buttons/btm4kyi_' + i + '.png');	//Updates
	}
	Preload('/images/bgProduct53.png');	//Background
}

function Preload(Name) {
	var File = new Image();
	File.src = Name;
	File.alt = Name;
}

/*
	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";
  	}
}

/*
	HotSwap(ID,Index)
	Image - The ID of the image to change the graphic on
	Index - The graphic in the aHide/aShow arrays
*/
function HotSwap(ID, Index) {
	var Table =  document.getElementById('txt'+ID);
	var Image =  document.getElementById('img'+ID);
	if (Table.style.display == 'block') {
		Image.src = aHide[Index];
	} else {
		Image.src = aShow[Index];
	}
}

/*
	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));
}

/*
	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, Expiry, Path, Domain, Secure) {
	var curCookie = Name + "=" + escape(Value) +
	((Expiry) ? "; Expiry=" + Expiry.toGMTString() : "") +
	((Path) ? "; Path=" + Path : "") +
	((Domain) ? "; Domain=" + Domain : "") +
	((Secure) ? "; Secure" : "");
	document.cookie = curCookie;
}

/*
	Swap(Image,Index)
	Name - The name of the image file
	Index - The state index (0 normal, 1 hot)
*/
function Swap(Name,Index) {
	var Image = document.getElementById('img' + Name);
	Image.src = '/buttons/btn' + Name + Index + '.png';
}

/*
	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.Subject.value == '') {
		alert("Please enter a subject for your enquiry in the subject field.");
		return false;
	} 
	else if (Form.Message.value == '') {
		alert("Please enter details of your enquiry in the message field.");
		return false;
	} 
	else if (Form.password.value == '') {
		alert("Please enter the code hidden in the CAPTCHA image at the top of the form.");
		return false;
	} 
	else {
		Form.Name.value = Form.Name.value.toLower;
		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()
   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() {
	var Input =  document.getElementById("MyQuery");
	if (Input.value == '') {
		var Image = document.getElementById("hint");
		Image.src="/images/hint-empty.png";
		Image.alt="Try entering somthing to search for in the text box."
		Image.title="Click to remove hint"
		Image.style.cursor="pointer";
		Input.focus()
		return false;
	} else {
		//alert("Search For: " + Input.value);
		window.location="/search/default.asp?Query=" + Input.value;
		return true;
	}
}

/*
	ResetHint()
	Removes the search hint box and sets the focus to the input field.
*/
function ResetHint() {
	var Input =  document.getElementById("MyQuery");
	var Image = document.getElementById("hint");
	Image.src="/images/hint-off.png";
	Image.style.cursor="default";
	Image.alt=""
	Image.title=""
	Input.focus()
}

function SubmitForm(ID) {
	var Form =  document.getElementById(ID);
	Form.submit();
}

/*
	WebPartExpand(ID)
	ID - The WebPart ID
	Togles the WebPart or SidePart state between Collapsed and Expanded
	Saves the ID to a cookie if expanded, so state can be restored at a 
	later time. Retires the cookie if collapsed to keep things tidy.
*/
function WebPartExpand(Type, ID, Index) {
	var Key = Type+ID
	var Table =  document.getElementById('txt'+ID);
	var Image =  document.getElementById('img'+ID);
	var Cell =  document.getElementById(Key);
	var Now = new Date();
	FixDate(Now);
	if (Table.style.display == 'block') {
		Table.style.display = 'none';
		Image.src = aShow[Index];
		Image.alt = Image.alt.replace(/Hide/,"Show");
		Now.setTime(Now.getTime() - Expires);
	} else {
		Table.style.display = 'block';
		Image.src = aHide[Index];
		Image.alt = Image.alt.replace(/Show/,"Hide");
		Now.setTime(Now.getTime() + Expires);
	}
	SetCookie(Key, Table.style.display, Now, '/');
	Image.title = Image.alt;
	Cell.title = Image.alt;
}
