//function checkTerms() { // Check T & C box is ticked before proceding to paypal
//	missinginfo = '';
//	if (!document.agreement_form.agreement.checked) 
//	{
//		missinginfo += '\n - You must agree to the Terms and Conditions';
//	}
//	
//	if (missinginfo != '') 
//	{
//		missinginfo ='Required information is missing: \n' +
//		missinginfo + '\n' + '';
//		alert(missinginfo);
//		/*return false;*/
//	}
//	else 
//	{
//		document.checkout.submit();
//		return true;
//	}
//}
//checkTerms.addEvent(window, 'load', checkTerms.init, false);
// Check T & C box is ticked before proceding to paypal
//function checkTerms() { 
//	missinginfo = '';
//	if (!document.agreement_form.agreement.checked) 
//	{
//		missinginfo += '\n - You must agree to the Terms and Conditions';
//	}
//	
//	if (missinginfo != '') 
//	{
//		missinginfo ='Required information is missing: \n' +
//		missinginfo + '\n' + '';
//		alert(missinginfo);
//		/*return false;*/
//	}
//	else 
//	{
//		document.checkout.submit();
//		return true;
//	}
//}

fp = {
	// *********************** Global properties ************************* //
	// Page section IDs
	boundaryId:'interface',
	// Used for the image popup
	hideClass:'hide',
	// Link text
	closeLabel : 'Close window',
	openLabel : 'Open window',
	//global parameters
	closelink:null,
	// *********************** Global properties ************************* //
	// *********************** Popup properties ************************* //
	// Container id for popup
	popUpContainer:'agreement-container', //agreement-container js-popup-wrapper
	// ID of generated popup container (ensure a class exists for this)
	popunderId:'popup_window',
	// ID of generated popup container (ensure a class exists for this)
	headingId:'popup-heading',
	// Class applied to 'close window' link
	closeWindowClass:'close_window', 
	// Content to display in window
	returnedContent:null,
	// *********************** Popup properties ************************* //
	
	// *********************** Image Popup properties ************************* //
	// Page section IDs
		//boundaryId:'interface',
	// Container id for popup image
	popImageContainer:'image-popup',
	// ID of generated popup image container
	imagePopupID:'popUpImage',
	// Used for the image popup
	// *********************** Image Popup properties ************************* //
	
	// *********************** Page popup properties ************************* //
	// Popup arguments
	// See http://www.accessify.com/features/tutorials/the-perfect-popup/ for an overall explanation
	popUpType : "standard",
	popUpWidth : "780",
	popUpHeight : "580",
	newWindow : null,
	// *********************** Page Popup properties ************************* //

	// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Popup window
	// Check to see if a popup win is available
  	init:function()
	{
		// Get image container
		var container=document.getElementById(fp.popUpContainer);
		// Check to see if image container exists
		if(container)
		{
			// Create div element to store image
		    fp.popunder=document.createElement('div');
		    // Assign id to new div
			fp.popunder.id=fp.popunderId;
			// Add hideclass to the new div (containing image)
			fp.cssjs('add',fp.popunder,fp.hideClass);
			// Add the new div as a child to the main page
			document.getElementById(fp.boundaryId).appendChild(fp.popunder);
			
			/****************** Build close window link ****************************/
			// Build links for each image by looping through each link from the 
			var links=container.getElementsByTagName('a');		
			// Loop through links and assign the click event with the openwin method
			for(var i=0;i<links.length;i++)
			{
				// Some links are direct to .php files - we don't want to open these within a seperate window so check for them and ignore event
				var string = links[i].href.split(".");
				var fileType = string[string.length-1];
				if (fileType !='php' && fileType !='php?checkout')
				{
					fp.addEvent(links[i],'click',fp.openWin,false);
				}
			}
		}
		// Initialise popup image function (for viewing large images on the product details page etc)
		fp.initImagePopup();
		// Initialise popup window function (for viewing link in new page)
		fp.initNewWindowPopup();
	},
	
	// Function thats applied to links to open windows
	openWin:function(e)
	{
		// HACK - In IE 6, dropdown elements are displayed above popups. To eliminate this we must hide the dropdown
		//var searchAllProducts = document.getElementById("search-all-products");
		//searchAllProducts.style.visibility = 'hidden';
		var linkElement = fp.getTarget(e);
		var uniqueNameArray = linkElement.href.split("/");
		var uniqueNameLength =uniqueNameArray.length-1;
		var uniqueName = uniqueNameArray[uniqueNameLength];
		// Obtain content from database using unique-name (returns result in fp.returnedContent)
		fp.createXMLHttpRequest(uniqueName);
		// if there is no image in popup yet, create one.
		// +++++++++++++++++++++++++++++++++++++++++++++++++ Close link	
		// Create link element and assign to variable
		var closeLink=document.createElement('a');
		fp.cssjs('add',closeLink,fp.closeWindowClass);
		// Use the setAttribute method to set the href attribute to '#'
		closeLink.setAttribute('href','#');
		// Set text of link
		closeLink.innerHTML=fp.closeLabel;
		// Add an event to the link (closewindo event)
		fp.addEvent(closeLink,'click',fp.closeWin,false);
		fp.fixSafari(closeLink);
		// Apply the new link to the image div so the user can shut the window
		fp.popunder.appendChild(closeLink);
		// ++++++++++++++++++++++++++++++++++++++ Insert popup heading
		var divElement=document.createElement('div');
		divElement.setAttribute('id',fp.headingId);
		var title = uniqueName.replace(/-/g, " ");
		var text = document.createTextNode(title);
		divElement.appendChild(text);
		fp.popunder.appendChild(divElement);
		// ++++++++++++++++++++++++++++++++++++++ Insert popup heading
		// +++++++++++++++++++++++++++++++++++++++++++++++++ Close link	

		// Create text element
		var pElement=document.createElement('div');
		//var contentText=document.createTextNode(fp.returnedContent);
		// innerHTML must be used becasue the returned text includes html formatting
		pElement.innerHTML = fp.returnedContent;
		// pElement.appendChild(contentText);
		fp.popunder.appendChild(pElement);

		// Remove the fp.hideclss so we can show the large image
		fp.cssjs('remove',fp.popunder,fp.hideClass);
		// Stop link from being followed and 'event bubbling' by using the cancelClick() method
		fp.cancelClick(e);
	},
	
	// Function used to close popup window
	// Note: When we use the addEvent function the event listener function (fp.closeWin)
	// has an object applied which can be used to obtain specific properties
	// e is used to obtain these parameters.
	// A very important one is 'target' (the element that has the eventhandler attatched to it - in this case 'closeLink')
	closeWin:function(e)
	{
		
		// hide popunder div (large image container) and don't follow link
		//fp.cssjs('add',fp.popunder,fp.hideClass);
		while (fp.popunder.firstChild) 
		{
			fp.popunder.removeChild(fp.popunder.firstChild);
		}
		fp.cssjs('add',fp.popunder,fp.hideClass);
		// fp.popunder.parentNode.removeChild(fp.popunder); 
		
		// To eliminate 'event bubbling' (starts all events for parent elements of the current element?!?!)
		// use the cancelclickk function
		fp.cancelClick(e);
	},
	// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Popup window
	
	// Function used by the popup window to close large image container
	// Note: When we use the addEvent function the event listener function (fp.closeWin)
	// has an object applied which can be used to obtain specific properties
	// e is used to obtain these parameters.
	// A very important one is 'target' (the element that has the eventhandler attatched to it - in this case 'closeLink')
	closeWin:function(e)
	{
		// HACK - In IE 6, dropdown elements are displayed above popups. To eliminate this we must hide the dropdown
		//var searchAllProducts = document.getElementById("search-all-products");
		//searchAllProducts.style.visibility = 'visible';
		// hide popunder div (large image container) and don't follow link
		//fp.cssjs('add',fp.popunder,fp.hideClass);
		while (fp.popunder.firstChild) 
		{
			fp.popunder.removeChild(fp.popunder.firstChild);
		}
		fp.cssjs('add',fp.popunder,fp.hideClass);
		// fp.popunder.parentNode.removeChild(fp.popunder); 
		
		// To eliminate 'event bubbling' (starts all events for parent elements of the current element?!?!)
		// use the cancelclickk function
		fp.cancelClick(e);
	},
	// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Popup image
	// Check to see if a popup image is available
  	initImagePopup:function()
	{
		
		// Get image container
		var container=document.getElementById(fp.popImageContainer);
		// Check to see if image container exists
		if(container)
		{
			// Create div element to store image
		    fp.popunder=document.createElement('div');
		    // Assign id to new div
			fp.popunder.id=fp.imagePopupID;
			// Add hideclass to the new div (containing image)
			fp.cssjs('add',fp.popunder,fp.hideClass);
			// Add the new div as a child to the main page
			document.getElementById(fp.boundaryId).appendChild(fp.popunder);
			
			/****************** Build close image link ****************************/
			
			// Build links for each image by looping through each link from the 
			// image container
			var links=container.getElementsByTagName('a');		
			// Create link element and assign to variable
			var closeLink=document.createElement('a');
			// Use the setAttribute method to set the href attribute to '#'
			closeLink.setAttribute('href','#');
			// Set text of link
			closeLink.innerHTML=fp.closeLabel;
			// Add an event to the link (closewindo event)
			fp.addEvent(closeLink,'click',fp.closeWin,false);
			fp.fixSafari(closeLink);
			// Apply the new link to the image div so the user can shut the window
			fp.popunder.appendChild(closeLink);
			
			// Loop through links and assign the click event with the openwin method
			for(var i=0;i<links.length;i++)
			{
				fp.addEvent(links[i],'click',fp.openImageWin,false);
			}
		}
	},
		// Function thats applied to links to open windows
	openImageWin:function(e)
	{
		var linkElement = fp.getTarget(e);
		// alert(linkElement.parentNode);
		//alert(fp.getTarget(e).href);
		// if there is no image in popup yet, create one.
		if(!fp.popunder.getElementsByTagName('img')[0])
		{
			// +++++++++++++++++++++++++++++++++++++++++++++++++ Close link	
			// Create link element and assign to variable
			var closeLink=document.createElement('a');
			// Use the setAttribute method to set the href attribute to '#'
			closeLink.setAttribute('href','#');
			// Set text of link
			closeLink.innerHTML=fp.closeLabel;
			// Add an event to the link (closewindo event)
			fp.addEvent(closeLink,'click',fp.closeWin,false);
			fp.fixSafari(closeLink);
			// Apply the new link to the image div so the user can shut the window
			fp.popunder.appendChild(closeLink);
			// +++++++++++++++++++++++++++++++++++++++++++++++++ Close link	
			// create image element
			var shot=document.createElement('img');
			// Get the link element for the small image using the fp.getTarget function 
			// (we use the link element to obtain the href value which is the source of the image)
			//var bigpic=fp.getTarget(e);
			//shot.setAttribute('src',bigpic.href);
			// set large image src attribute to the href value of the small image link
			shot.setAttribute('src',linkElement.parentNode);
			// Append the image element to the div element (popunderId)
			fp.popunder.appendChild(shot);
			// Apply close functionality to image
			fp.addEvent(shot,'click',fp.closeWin,false);
			//
			fp.fixSafari(shot);
		}
		// Remove the fp.hideclss so we can show the large image
		fp.cssjs('remove',fp.popunder,fp.hideClass);
		// Stop link from being followed and 'event bubbling' by using the cancelClick() method
		fp.cancelClick(e);
	},
	// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Popup image
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Popup new window
	// Check to see if a popup window is available
	// http://www.accessify.com/features/tutorials/the-perfect-popup/ for an overall explanation
  	initNewWindowPopup:function()
	{
		var popups = document.getElementsByTagName("a");
		for (i=0;i<popups.length;i++)
		{
			if (popups[i].rel.indexOf("popup")!=-1)
			{
				fp.addEvent(popups[i],'click',fp.doPopUp,false);
			}
		}
	},
	
	doPopUp:function(e)
	{
		// Get rel data
		var linkElement = fp.getTarget(e);
		
		var attribs = linkElement.rel.split(" ");
		if (attribs[1]!=null) {fp.popUpType = attribs[1];}
		if (attribs[2]!=null) {fp.popUpWidth = attribs[2];}
		if (attribs[3]!=null) {fp.popUpHeight = attribs[3];}
		//calls function to close pop-up if already open, 
		//to ensure it's re-opened every time, retainining focus
		fp.closeNewWindow();
		fp.popUpType = fp.popUpType.toLowerCase();

		if (fp.popUpType == "fullscreen")
		{
			fp.popUpWidth = screen.availWidth;
			fp.popUpHeight = screen.availHeight;
		}
		var tools='';
		if (fp.popUpType == "standard"){
			tools = "resizable,toolbar=yes,location=yes,scrollbars=yes,menubar=yes, width="+fp.popUpWidth+",height="+fp.popUpHeight+",top=0,left=0";
		}
		if (fp.popUpType == "console" || fp.popUpType == "fullscreen"){
			tools = "resizable,toolbar=no, location=no,scrollbars=no,width="+fp.popUpWidth+",height="+fp.popUpHeight+",left=0,top=0";
		}
		fp.newWindow = window.open(linkElement.href, 'newWin', tools);
		fp.cancelClick(e); // PLacing the canel function below 'fp.newWindow.focus();' creates an error in IE7 where the pdf loads in the popup AND the original window 
		fp.newWindow.focus();
		
	},
	closeNewWindow:function() 
	{
		if (fp.newWindow != null)
		{
			if(!fp.newWindow.closed)
			fp.newWindow.close();
		}
	},
	// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Popup new window
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////
	/************************************* helper methods ********************************************************/
	/************************************* AJAX helper methods ********************************************************/
	// Create an Instance of the XMLHttpRequest Object. This is paramount to Ajax and allows the browser to send and process responses.
	createXMLHttpRequest:function(uniqueName) // The createXMLHttpRequest creates the actual object, the conditions within the method just determine how to create it.
	{
		// If the brwser supports ActiveX objects, use these to create the XMLHttpRequest object
		if (window.ActiveXObject) 
		{
			fp.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
		}
		else if (window.XMLHttpRequest) 
		{
			fp.xmlHttp = new XMLHttpRequest();
		}
		
		// Set the URL which links to the resource (ProgressBarServlet.java), passing a name/value pair task/create
		var url = "/library/ajaxRequest.php?unique_name="+uniqueName;
		
		// Set up a call to the server. 
		// Info: 'open' method assigns HTTP type (GET, POST or PUT), URL can be relative or absolute
		// Last argument determines is call should by asynchronous or not
		fp.xmlHttp.open("GET", url, false);
		// The event handler that fires on every state change (onreadystatechange stores the pointer to the callback function so the callback knows where its at and what its got to do).
		fp.xmlHttp.onreadystatechange = fp.goCallback; 
		// Send the request to the detination resource (url var)
		// null is used when no data is to be sent as part of the request body.
		fp.xmlHttp.send(null);
	},
	
	// Check the readyState property on the XMLHttpRequest object
	goCallback:function() 
	{
		// Once the response is complete, check the response header to make sure everything is ok
		if (fp.xmlHttp.readyState == 4) 
		{
			// If all is well, run the function (200 is server code 'ok')
			if (fp.xmlHttp.status == 200) 
			{
				//alert(ajax.xmlHttp.responseText);
				fp.returnedContent = fp.xmlHttp.responseText;
				//return returnedContent;
				//setTimeout("pollServer()", 1000); // Call pollserver() function
			}
		}
	},

	/************************************* AJAX helper methods ********************************************************/

	addEvent: function(elm, evType, fn, useCapture)
	{
		if (elm.addEventListener) 
		{
			elm.addEventListener(evType, fn, useCapture);
			return true;
		} else if (elm.attachEvent) {
			var r = elm.attachEvent('on' + evType, fn);
			return r;
		} else {
			elm['on' + evType] = fn;
		}
	},
	
	// cssjs mthod that adds, removes or swaps classes
	// see http://www.onlinetools.org/articles/unobtrusivejavascript/cssjsseparation.html
	cssjs:function(a,o,c1,c2)
	{
		switch (a)
		{
			case 'swap':
				o.className=!fp.cssjs('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);
			break;
			case 'add':
				if(!fp.cssjs('check',o,c1)){o.className+=o.className?' '+c1:c1;}
			break;
			case 'remove':
				var rep=o.className.match(' '+c1)?' '+c1:c1;
				o.className=o.className.replace(rep,'');
			break;
			case 'check':
				return new RegExp("(^|\\s)" + c1 + "(\\s|$)").test(o.className)
			break;
		}
	},
	// Use cancelClick method to cancel 'event bubbling' and default actions associated to elements
	cancelClick:function(e)
	{
		if (window.event)
		{
			window.event.cancelBubble = true;
			window.event.returnValue = false;
			return;
		}
		if (e)
		{
			e.stopPropagation();
			e.preventDefault();
		}
	},
	// New getTargetFunction (http://lists.evolt.org/archive/Week-of-Mon-20060717/183797.html)
	getTarget:function(e)
	{
		var target = window.event ? window.event.srcElement : e ? e.target : null;
		if (!target){return false;}
		while(target.nodeType!=1 && target.nodeName.toLowerCase()!='body')
		{
			target=target.parentNode;
		}
		return target;
	},
	
	fixSafari:function(node)
	{
//		node.onclick = function() { return false; }; // Safari
	},
	
	getElementsByClass : function(searchClass,node,tag) {
		var classElements = new Array();
		if ( node == null )
			node = document;
		if ( tag == null )
			tag = '*';
		var els = node.getElementsByTagName(tag);
		var elsLen = els.length;
		var pattern = new RegExp('(^|\\\\s)'+searchClass+'(\\\\s|$)');
		for (i = 0, j = 0; i < elsLen; i++) {
			if ( pattern.test(els[i].className) ) {
				classElements[j] = els[i];
				j++;
			}
		}
		return classElements;
	}
}
// addEvent is a method used to sort of stack functions up after the page has loaded.
// On this page this method is watching the window and waiting till its loaded.
// After which the fp.init object is initialized
fp.addEvent(window, 'load', fp.init, false);
