/***************************************************************************************************/
/* This is a near duplicate file of bfp.js - the only amendments are the three functions called    */
/* displayall, setUpGuide and initiateGuides.  This is used in snapshot-series.html to display     */ 
/* two sets of articles, rather than six sets as utilised by bfp.js for literature-and-guides.htm. */
/*                      File created by Nick Kimberley on 07.02.08                                 */
/***************************************************************************************************/

// Variables
var ArticleCount = 0;
var AlreadyAddedMsg = "This article is in your clipboard";
var RemoveMsg = "click to remove this article";
var RemoveAllMsg = "Remove all articles from clipboard?";
var RemoveText = "Remove all";
var AddArticleLinkTitle = "Click to add this article to your clipboard";
var AddArticleLinkText = "Add to clipboard";
var ClipBoardFileName = "clipboard.html";
var statusText = "My clipboard ";
var ClipBoardLinkTitle = "Links through to article clipboard"
var emptyClipBoardText = "Your clipboard is empty";
var noCookieMsg = "The clipboard feature requires JavaScript cookies enabled";
var printText = "Print this article";
var cookiesEnabled = false; 
var xmlTitle = "";
var xmlURL = "";
var xmlStoryDate = "";
var theid;
var cookieDetails;

/* Cookie business starts here */
// ---------------------------------------------------------------//
// Check if cookies is enabled    
document.cookie = "Enabled=true";   
var cookieValid = document.cookie;  
if (cookieValid.indexOf("Enabled=true") != -1){ 
	cookiesEnabled = true;   
} else {
	cookiesEnabled = false;
}
// Sets the cookie
function setCookie(c_name,value,expiredays){
	var exdate=new Date()
	exdate.setDate(exdate.getDate()+expiredays)
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString()) 
	// Fixed data used to get around Safari for mac issues with Cookies.
	// ((expiredays==null) ? "" : ";expires=Tue, 25 Jul 2200 12:09:42 GMT");
}
// Retrieves the cookie
function getCookie(c_name){
	if (document.cookie.length>0){
		var c_start=document.cookie.indexOf(c_name + "=")
		if (c_start!=-1){ 
			c_start=c_start + c_name.length+1; 
			var c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		} 
	}
	return "";
}
// Deletes cookie
function deleteCookie(name){                  
	var cookie_date = new Date();  // current date & time
	cookie_date.setTime ( cookie_date.getTime() - 1 );
	document.cookie = name += "=; expires=" + cookie_date.toGMTString();		 
}
// ----------------------------------------------------------------//
/* Cookie business ends here */
/* Just some DOM script function */
//create function, it expects 2 values.
function insertAfter(newElement,targetElement){
	//target is what you want it to go after. Look for this elements parent.
	var parent = targetElement.parentNode;		
	//if the parents lastchild is the targetElement...
	if(parent.lastchild == targetElement) {
			//add the newElement after the target element.
			parent.appendChild(newElement);
		} else {
			// else the target has siblings, insert the new element between the target and it's next sibling.
			parent.insertBefore(newElement, targetElement.nextSibling);
		}
}

/* Gets article count, updates clipboard status: 10.08.2007 */
function displayClipStatus(){		
	if (document.getElementById("clipStatus")){	
		if (cookiesEnabled){
			// Counts the number of cookies available
			var myCookieContent = getCookie("bfpCookie")
			if ((myCookieContent != null)&&(myCookieContent != "")){		
				cookieDetails = myCookieContent.split("][");				
				ArticleCount = cookieDetails.length;						
			} 	
			var statusLink = document.createElement('A');	
			statusLink.setAttribute("href",ClipBoardFileName);
			statusLink.setAttribute("title",ClipBoardLinkTitle);
			statusLink.setAttribute("id","clipBoardLink");
			var statusLinkText = document.createTextNode(statusText+"("+ArticleCount+")");
			statusLink.appendChild(statusLinkText);
			// Insert the clipboard status link		
			document.getElementById("clipStatus").insertBefore(statusLink,document.getElementById("noJavaScriptText"));
		} else {
			var noCookieSpan = document.createElement('SPAN');
			noCookieSpan.setAttribute("id","noCookieCSS");
			var noCookieText = document.createTextNode(noCookieMsg);
			noCookieSpan.appendChild(noCookieText);
			document.getElementById("clipStatus").insertBefore(noCookieSpan,document.getElementById("noJavaScriptText"));
		}
	}
}

/* Updates clipboard status: 10.08.2007 */
function updateStatus(){
	var statusLinkText = statusText+"("+ArticleCount+")";
	document.getElementById("clipBoardLink").innerHTML = statusLinkText;
}

/* Sets up 'Add to clipboard' link: 10.08.2007 */
function setUpArticleLink(){
	if (document.getElementById("articleDIV")){			
		/* HTML being written to screen
		<div id="articleButtons">
			<p id="clipLink"><a href="#" onclick="addArticleLink(thisArticleID)">Add to clipboard</a></p>
			<p id="printLink"><a href="#" onclick="self.print(); return false;">Print this article</a></p>
			<div class="clearBoth"><!-- clears floated elements --></div>
		</div>*/	
		var controlsDIV = document.createElement('DIV');
		controlsDIV.setAttribute("id","articleButtons");			
		if (cookiesEnabled){
			// Check to see if Article is in cookie first				
			var addArticlePara = document.createElement('P');
			addArticlePara.setAttribute("id","clipLink");	
			var addLink = document.createElement('A');
			addLink.setAttribute("href",'#');			
			
			var isClipped = false;			
			var myCookieContent = getCookie("bfpCookie")
			if ((myCookieContent != null)&&(myCookieContent != "")){		
				var cookieDetails = myCookieContent.split("][");				
				for (var g=0;g<cookieDetails.length;g++){
					if(cookieDetails[g] == thisArticleID){
						isClipped = true;				
					}
				}						
			}					
			if (isClipped == false){				
				addLink.setAttribute("title",AddArticleLinkTitle);
				addLink.setAttribute("id","addArticleLink");
				addLink.onclick = function(){
						addArticleLink(thisArticleID);	
						return false;
					};
				var addLinkText = document.createTextNode(AddArticleLinkText)
				addLink.appendChild(addLinkText);		
				addArticlePara.appendChild(addLink);
			} else {						
				addLink.setAttribute("title",AlreadyAddedMsg);
				addLink.setAttribute("id","addedArticleLink");
				addLink.onclick = function(){ alert(AlreadyAddedMsg); return false; };			
				var addLinkText = document.createTextNode(AlreadyAddedMsg)
				addLink.appendChild(addLinkText);		
				addArticlePara.appendChild(addLink);
			}
			
		} else {		
			var addArticlePara = document.createElement('P');
			addArticlePara.setAttribute("id","clipLink");	
			var noCookieText = document.createTextNode(noCookieMsg);
			addArticlePara.appendChild(noCookieText);
			
		}	
		// Adds on the clipboard link
		controlsDIV.appendChild(addArticlePara);			
		// Adds on the print link
		var printPara = document.createElement('P');
		printPara.setAttribute("id","printLink");
		var printA = document.createElement('A'); 
		printA.setAttribute("href","#");
		printA.onclick = function(){
						self.print();
						return false;
					};
		var printLinkText = document.createTextNode(printText)
		printA.appendChild(printLinkText);
		printPara.appendChild(printA);				
		controlsDIV.appendChild(printPara);	
		// Adds on clear float div
		var clearDiv = document.createElement('DIV');
		clearDiv.setAttribute("id","controlClear");
		clearDiv.className = "clearBoth";
		var clearDivText = document.createTextNode(" ");		
		clearDiv.appendChild(clearDivText);
		controlsDIV.appendChild(clearDiv);				
		// Adds control DIV to Article div
		document.getElementById("articleDIV").appendChild(controlsDIV);
	}	
}

/* Adds an article to clipboard: 10.08.2007 */
function addArticleLink(articleID){	
	// Check if cookie exists
	var myCookieContent = getCookie("bfpCookie")
	if ((myCookieContent != null)&&(myCookieContent != "")){
		// Cookie exists so add ID to existing value, write back to machine
		myCookieContent = myCookieContent + "][" + articleID;
		//setCookie("bfpCookie",myCookieContent,365);
		setCookie("bfpCookie",myCookieContent,365);
	} else {				
		// Create new cookie called bfpCookie, inserts ID of aritle and write to machine		
		// setCookie("bfpCookie",articleID,365)
		setCookie("bfpCookie",articleID,365)
	}			
	// Change link to 'added' state
	var clipLink = document.getElementById("addArticleLink");
	clipLink.id = "addedArticleLink";
	clipLink.title = AlreadyAddedMsg;
	clipLink.childNodes[0].nodeValue = AlreadyAddedMsg;
	clipLink.onclick = function(){ alert(AlreadyAddedMsg); return false; };
	//	Updates the clipboard status
	ArticleCount++
	updateStatus()
}

/* Removes one article from clipboard: 10.08.2007 */
function removeClip(clipRowObjRef){	
	// Get TR of article, use for cookie id
	var clipRowObj = clipRowObjRef.parentNode.parentNode;	
	var articleID = clipRowObj.getAttribute("id");	
	// Remove aritle ID from cookie
	var myCookieContent = getCookie("bfpCookie")	
	var cookieDetails = myCookieContent.split("][");
	for (var d=0;d<cookieDetails.length;d++){
		if (cookieDetails[d] == articleID){			
			cookieDetails.splice(d,1);			
		}			
	}
	myCookieContent = cookieDetails.join("][");	
	// setCookie("bfpCookie",myCookieContent,365);
	setCookie("bfpCookie",myCookieContent,365);	
	// Make visual change to Clipboard, remove this one.
	document.getElementById("clipBoardArticles").removeChild(clipRowObj);
	doOddEvenThing();
	// Update article count and status display
	ArticleCount--;
	updateStatus();
	checkBoard();
}

/* Empties the clipboard: 10.08.2007 */
function removeClipsAll(){
	var answer = confirm(RemoveAllMsg);
	if (answer){	
		// Remove all items from cookie
		deleteCookie("bfpCookie");		
		// Make visual change to Clipboard, remove all.
		while(document.getElementById("clipBoardArticles").hasChildNodes()){				
			document.getElementById("clipBoardArticles").removeChild(document.getElementById("clipBoardArticles").lastChild);
		}		
		// Update article count and status display		
		ArticleCount = 0;
		updateStatus();
		checkBoard();	
	}	
}

/* Adds 'Remove all' link: 10.08.2007 */
function setupRemoveLink(){
	if (document.getElementById("clipBoardDiv")){
		if (cookiesEnabled){
			if(ArticleCount>0){		
				var removeAllArticlesLinkPara = document.createElement('P');
				removeAllArticlesLinkPara.setAttribute("id","removeClipPara");
				var removeAllArticlesLink = document.createElement('A');
				removeAllArticlesLink.setAttribute("href","#");
				removeAllArticlesLink.setAttribute("id","removeClipLink");
				removeAllArticlesLink.setAttribute("title",RemoveAllMsg);
				removeAllArticlesLink.onclick = function(){
						removeClipsAll(); 
						return false;
			}
				var removeAllArticlesLinkText = document.createTextNode(RemoveText);
				removeAllArticlesLink.appendChild(removeAllArticlesLinkText);
				removeAllArticlesLinkPara.appendChild(removeAllArticlesLink);
				document.getElementById("clipBoardDiv").appendChild(removeAllArticlesLinkPara);			
			}  else {
				if (document.getElementById("removeClipPara")){
					document.getElementById("clipBoardDiv").removeChild(document.getElementById("removeClipPara"));
				}
				var emptyCB = document.createElement('DIV');
				emptyCB.className = "allGoneMate";
				var emptyCBPara = document.createElement('P');		
				var emptyCBText = document.createTextNode(emptyClipBoardText);
				emptyCBPara.appendChild(emptyCBText);
				emptyCB.appendChild(emptyCBPara);
				document.getElementById("clipBoardDiv").appendChild(emptyCB);			
			}
		} else {
			var emptyCB = document.createElement('DIV');
			emptyCB.className = "allGoneMate";
			var emptyCBPara = document.createElement('P');		
			var emptyCBText = document.createTextNode(noCookieMsg);
			emptyCBPara.appendChild(emptyCBText);
			emptyCB.appendChild(emptyCBPara);
			document.getElementById("clipBoardDiv").appendChild(emptyCB);
		}
	}
}

/* Checks if board is empty: 10.08.2007 */
function checkBoard(){
	if (document.getElementById("clipBoardDiv")){
		if((document.getElementById("clipBoardArticles"))&&(document.getElementById("clipBoardArticles").hasChildNodes())){
			null; // Gunzalez says do nothing
		} else {
			if (document.getElementById("removeClipPara")){
				document.getElementById("clipBoardDiv").removeChild(document.getElementById("removeClipPara"));
			}
			var emptyCB = document.createElement('DIV');
			emptyCB.className = "allGoneMate";
			var emptyCBPara = document.createElement('P');		
			var emptyCBText = document.createTextNode(emptyClipBoardText);
			emptyCBPara.appendChild(emptyCBText);
			emptyCB.appendChild(emptyCBPara);
			document.getElementById("clipBoardDiv").appendChild(emptyCB);			
		}
	}
}

/* Create alternate background colours: 10.08.2007 */
function isOddOrEven(clipRef){
	var binary = clipRef.toString(2);
	if (binary.charAt(binary.length - 1) == "1"){
		return "even";
	} else {
		return "odd";
	}	
}
function doOddEvenThing(){
	if(document.getElementById("clipBoardArticles").hasChildNodes()){
		for (var g=0;g<document.getElementById("clipBoardArticles").childNodes.length;g++){	
			document.getElementById("clipBoardArticles").childNodes[g].className = document.getElementById("clipBoardArticles").childNodes[g].className.replace(" odd","");
			document.getElementById("clipBoardArticles").childNodes[g].className = document.getElementById("clipBoardArticles").childNodes[g].className.replace(" even","");
			document.getElementById("clipBoardArticles").childNodes[g].className = document.getElementById("clipBoardArticles").childNodes[g].className + " " + isOddOrEven(g);			
		}
	}
}

/* Adds an articles to clipboard : 10.08.2007 */
function WriteToClipBoard(clipRef){
	/* HTML being written to screen
	<tr id="clipRow1" class="clipRows odd">
		<td><a href="article_1.html">Why Barclays Financial planning?<a/></td>
		<td>09/07/2007</td>
		<td class="rmvBtn">
			<a href="#" onclick="removeClip(this); return false;" title="click to remove this article">
			<img src="images/gunzalez.jpg" alt="click to remove this article" />
			</a>
		</td>
	</tr> */		
	var myCookieContent = getCookie("bfpCookie");
	var cookieDetails = myCookieContent.split("][");				
	var newRow = document.createElement('TR');	
	newRow.setAttribute("id",cookieDetails[clipRef]); 			// Get id of article from cookie		
	newRow.className = "clipRows";			
	// Create TD tag for article title		
	var articleTitle = document.createTextNode(xmlTitle); 		// Get title of article from XML
	var titleLink = document.createElement('A');
	titleLink.setAttribute("href",xmlURL); 						// Get url of article from XML
	titleLink.setAttribute("title","Link through to "+xmlTitle);// Set the link's title from XML
	titleLink.appendChild(articleTitle);
	var titleTD = document.createElement('TD');
	titleTD.appendChild(titleLink);
	// Create second TD tag for article date
	var articleDate = document.createTextNode(xmlStoryDate);	// Get date of article from XML
	var dateTD = document.createElement('TD');
	dateTD.appendChild(articleDate);	
	// Create link object
	var rmLink = document.createElement('A');
	rmLink.setAttribute("href",'#');
	rmLink.setAttribute("title",RemoveMsg);
	rmLink.onclick = function(){
			removeClip(this);	
			return false;
		};
	// Create IMG object
	var rmImage = document.createElement('IMG');
	rmImage.setAttribute("src","images/gunzalez.gif");
	rmImage.setAttribute("alt",RemoveMsg);
	rmImage.className = "gunzalez";	
	rmLink.appendChild(rmImage);
	// Create thrid TD tag for remove image
	var removeTD = document.createElement('TD');
	removeTD.className = "rmvBtn";
	removeTD.appendChild(rmLink);
	// Insert all 3 TD tags into TR tag
	newRow.appendChild(titleTD);
	newRow.appendChild(dateTD);
	newRow.appendChild(removeTD);
	// Add article to clipboard - visual
	document.getElementById("clipBoardArticles").appendChild(newRow);	
}

/* Functions for all pages: 13.08.2007 */
function getHelpPage(){
	var selectObj = document.getElementById('i-want');	
	var selValue = selectObj.options[selectObj.selectedIndex].value;
	if (selValue != "none"){		
		location.href = selValue;
	}
}
function gotoSolutionPage(){
	var selectObj = document.getElementById('tell-me');	
	var selValue = selectObj.options[selectObj.selectedIndex].value;
	if (selValue != "none"){		
		location.href = selValue;
	}
}
function clearPostCode(){
	var inputObj = document.getElementById('place');		
	if (inputObj.value == "Enter your postcode"){		
		inputObj.value = "";
	};
}
function attachPostCodeFunc(){
	var inputObj = document.getElementById('place');	
	if (inputObj){
		inputObj.onclick = function(){
				clearPostCode();	
				return false;
			};
		inputObj.title = "Enter your postcode"
	};
}

/* Functions for seminar page: 20.08.2007 */
function setUpSeminarBttns(){
	for(var w=1;w<=3;w++){
		if((document.getElementById('seminarBttn'+w))&&(seminarButtonz[w]==true)){
			document.getElementById('seminarBttn'+w).innerHTML = '<a href="https://secure.barclays.co.uk/financialplanning/bookseminar_form.html?seminarRef='+w+'" title="Link through to seminar registration form"><img src="images/btn_bookaplace.gif" alt="Link through to seminar registration form" class="bookbtn" /></a>';	
		}
	}
}

/* Functions for guide page: 17.08.2007 */
function activateJumpNav(){
	if(document.getElementById("i-want-to")){
		document.getElementById("i-want-to").style.display = "block";
	}
	else if(document.getElementById("tell-me-about")){
		document.getElementById("tell-me-about").style.display = "block";
	}
}

/* Displays Know what you're after?: 16.11.2007 */
function activateIwantto(){
	if(document.getElementById("i-want-to2")){
		document.getElementById("i-want-to2").style.display = "block";
	}
}

/* Displays Your nearest Financial planning Manager: 16.11.2007 */
function activateLetsMeetUp(){
	if(document.getElementById("lets-meet-up")){
		document.getElementById("lets-meet-up").style.display = "block";
	}
}

/* Displays BackButton: 16.11.2007 */
function activateBackButton(){
	if(document.getElementById("backbutton")){
		document.getElementById("backbutton").style.display = "block";
	}
}

/* Displays clipboard help section: 17.08.2007 */
function activateClipHelp(){	
	if(document.getElementById("clipboardHelp")){
		document.getElementById("clipboardHelp").style.display = "block";
	}
}

/* Functions for guide page: 17.08.2007 */
function attachDisAll(){
	/*<p align="right" id="displayall"><a href="#" class="find" onclick="displayall(); return false;">Display all guides</a></p>*/
	var thePara = document.createElement('P');
	thePara.setAttribute("align",'right');
	thePara.setAttribute("id",'displayall');	
	var disAlllink = document.createElement('A');
	disAlllink.setAttribute("href",'#');
	disAlllink.className = "find";
	disAlllink.setAttribute("title","click to show all");
	disAlllink.onclick = function(){
			displayall();	
			return false;
		};	
	var disAllText = document.createTextNode("Show all"); 
	disAlllink.appendChild(disAllText);
	thePara.appendChild(disAlllink);	
	insertAfter(thePara,document.getElementById("browseList"));	
}
function hideAll(){
	document.getElementById("browseList").style.display = "block";
	setUpGuide(1);
	var disPara = document.getElementById("displayall");
	var disLink = disPara.childNodes[0];
	disLink.setAttribute("title","click to show all");
	disLink.onclick = function(){
			displayall();	
			return false;
		};
	disLink.innerHTML = "Show all";
}
function disToHideAll(){
	var disPara = document.getElementById("displayall");
	var disLink = disPara.childNodes[0];
	disLink.setAttribute("title","click to hide");
	disLink.onclick = function(){
			hideAll();	
			return false;
		};
	disLink.innerHTML = "Hide all";
}

/* The following 3 functions were created specifically for snapshot-series.html - 07.02.08 */

function displayall(){
	document.getElementById("browseList").style.display = "none";
	for(var q=1;q<=2;q++){
			document.getElementById("guidesDIV"+q).style.display = "block";
	}
	disToHideAll();
}
function setUpGuide(guideRef){
	for(var q=1;q<=2;q++){
			document.getElementById("gud"+q).className = "";
			document.getElementById("guidesDIV"+q).style.display = "none";
	}
	document.getElementById("gud"+guideRef).className = "guideOn";
	document.getElementById("guidesDIV"+guideRef).style.display = "block";
}
function initiateGuides(){
	if (document.getElementById("browseList")){		
		for(var q=1;q<=2;q++){
			document.getElementById("guidesDIV"+q).style.display = "none";
		}
		document.getElementById('browseList').style.display = "block";
		document.getElementById('gud1').className = "guideOn";
		document.getElementById("guidesDIV1").style.display = "block";
		attachDisAll();
	}
}

/* Your nearest Financial planning Manager form  16.11.2007 */
function entsub(form) {
	if (window.event && window.event.keyCode == 13)
		openWin(form);	
	else
		return true;
}
function openWin (form) {
	var placeVar = form.place.value;
	if (form.place.value == "Enter your postcode" || form.place.value == "") {
		popupWin = window.open('http://www.multimap.com/clients/browse.cgi?client=barclays&amp;db=pc&amp;reclimit=10&amp;filter=branch&amp;place=&amp;brand=&amp;pc=','myWin','toolbar,status,resizable,scrollbars,,');
	}
	else {
	popupWin = window.open('http://www.multimap.com/clients/browse.cgi?client=barclays&amp;db=pc&amp;reclimit=10&amp;filter=branch&amp;place=' + placeVar + '&amp;brand=&amp;pc=' + placeVar,'myWin','toolbar,status,resizable,scrollbars,,');
	}
}

/* Run when page has loaded: 17.08.2007 */
window.onload = function(){	
	if (!document.getElementById) {	return false; };
	activateJumpNav();
	activateIwantto();
	attachPostCodeFunc();
	setUpSeminarBttns();
	initiateGuides();
	activateBackButton();
	activateLetsMeetUp();
	if (isSafeForjQuery==true){	
		displayClipStatus(); 
		activateClipHelp(); 
		setUpArticleLink();	
		setupRemoveLink();
   	}	
	if (document.getElementById("clipBoardArticles")&&(ArticleCount>0)){
		
		// Removes empty row, a row used only for accessibility reasons.
		while(document.getElementById("clipBoardArticles").hasChildNodes()){				
			document.getElementById("clipBoardArticles").removeChild(document.getElementById("clipBoardArticles").lastChild);
		}	
		// Uses jQuery functions to retrieve XML
		$.ajax({
			type: "GET",
			url: "xml/articleList.xml",
			dataType: "xml",
			success: function(xml) {								
				var articles = xml.getElementsByTagName("article");
				for (var g=0; g<articles.length; g++){
					theid = cookieDetails[g];
					for (var i=0; i<articles.length; i++){
						xmlid = articles[i].getElementsByTagName("id")[0].childNodes[0].nodeValue;			
						if(theid == xmlid){			
							xmlTitle = articles[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
							xmlURL = articles[i].getElementsByTagName("url")[0].childNodes[0].nodeValue;
							xmlStoryDate = articles[i].getElementsByTagName("date-story")[0].childNodes[0].nodeValue;			
							WriteToClipBoard(g);
						}			
					}		
				}					
				doOddEvenThing();
			}
		});		
	}			
};