// ************************
// initialise popup menus *
// ************************
/*
sfHover = function() {
	var sfEls = document.getElementById("nav2").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);
*/
// ***********************************
// set the page footer to the bottom *
// **********

function setFooter() {
	try {
		var home_id = document.URL.indexOf("1170748476664");
		var footerElement = document.getElementById('footer');
		var contentHeight = document.getElementById('page').offsetHeight;
		var navHeight = document.getElementById('nav3');
		var bannerHeight = document.getElementById('banners');
		var homepage = document.getElementById('home_container');
		var hubpage = document.getElementById('cfshub1');
		var ethicshome = document.getElementById('ethics_container');
		
		if  ((hubpage != null) || (ethicshome != null)) {
			contentHeight = contentHeight - 30;
		}
		
		if (homepage != null) {
			contentHeight = contentHeight - 50;
			footerElement.style.top = contentHeight + 'px';
		}


		if ((navHeight != null) && (footerElement != null) && (bannerHeight != null)) {   // if page id doesn't equal the homepage
		
		var navHeightF = navHeight.offsetHeight + 130;
		var bannerHeightF = bannerHeight.offsetHeight + 150;

				if ((contentHeight >= navHeightF) && (contentHeight >= bannerHeightF)) {
					footerElement.style.top = contentHeight + 'px';
					}
				if ((navHeightF >= contentHeight) && (navHeightF >= bannerHeightF)) {
					footerElement.style.top = navHeightF + 'px';
				}
				if ((bannerHeightF >= contentHeight) && (bannerHeightF >= navHeightF)) {
					footerElement.style.top = bannerHeightF + 'px';	
				}
		}
	} catch (err) {}
}

function sizePDFBox() {
	var pdfBox = document.getElementById('pdf_box2');
	var pdfImg = document.getElementById('pdfimg');

	if ((pdfImg != null) && (pdfBox != null)) {
		var box_size = pdfBox.offsetHeight;
		var img_size = pdfImg.offsetHeight;

		if (box_size > img_size) {
			pdfImg.style.margin = ((box_size - img_size) / 2) + 'px 0px';
		}
	}
}



// *********************
// IE6 flicker bug fix *
// *********************
try {
	document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}

//*************************************
//* Quick Search javascript function **
//*************************************

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_jumpMenuGo(selName,targ,restore){ //v3.0
  var selObj = MM_findObj(selName); if (selObj) MM_jumpMenu(targ,selObj,restore);
}


/******************************************************************************/
/*** THE FOLLOWING HAS BEEN COPIED DIRECTLY FROM GENERAL.JS OF THE OLD SITE ***/
/*** RAJ PARMAR - 16/11/2007                                                ***/
/******************************************************************************/

//////////////////////////////////////////////////
//
//  generatePreAmbleFunnelLink()
//
//  Generates a media-coded hyperlink to the loans pre-amble which will send it on to the app.
//  This function must be called inline (where the link is required).
//
//  Author:   Raj Parmar
//  History:  
//
//  Parameters:
//    mCode - A default media code to use if none is detected.
//    link_text - The text of the link.
//		cssClass - css class to apply
//		targetWindow - name of target window, defaults to _self.
//
//////////////////////////////////////////////////
function generatePreAmbleFunnelLink(mCode, link_text, cssClass, targetWindow) {
	//Set absolute URL of Pre-Amble page here
	var preAmbleURL = '/servlet/Satellite?c=Page&cid=1195458940806&pagename=CFScombi%2FPage%2FCFSCtplStandard';
	
	//Check for media code in url, if it exists, its value will override the parameter.
	var qs = location.href.split('?')[1];
	for (var i=0; i < qs.split('&').length; i++){
		if (qs.split('&')[i].indexOf('media=') > -1){
			mCode = (qs.split('&')[i].split('=')[1].length > 0)? qs.split('&')[i].split('=')[1] : mCode;
			break;
		}
	}

	//Validate value of link text
  if (link_text == undefined) {
		link_text = 'online';
	}

	//Validate target
	if (targetWindow == undefined) {
		targetWindow = '_self';
	}

	//Create link element.
	var myLink = document.createElement('a');
	myLink.href = preAmbleURL + '&media=' + mCode;
	myLink.className = cssClass;
	myLink.innerHTML = (link_text == undefined)? 'online' : link_text;
	myLink.title = myLink.innerHTML;
	myLink.target = targetWindow;

  //Insert DOM element at current location.
  var e = document.getElementsByTagName('*');
	var pos = e[e.length-1];
	pos.parentNode.appendChild(myLink);
}

//////////////////////////////////////////////////
//
//  generateFunnelLink()
//
//  Generates a media-coded hyperlink to funnel.jsp that uses the media code present in the current page's URL
//
//  Author:   Aden Millen 
//  History:  18/10/2007 (Andy Hames) - Moved to external file and parameterised
//
//  Parameters:
//    default_media   A default media code to use if none is detected
//
//////////////////////////////////////////////////
function generateFunnelLink(default_media, link_text) {
  if (link_text == undefined) {link_text='online';}
  // Create variable is_input to see if there is a media= in the url
  var is_input = document.URL.indexOf('media=');
  // Check the position of the media= in the url
  if (is_input != -1){ 
    // Write line from media= in the url to the end of the string
    document.write ('<a href="http://apps.co-operativebank.co.uk/funnel.jsp?'+document.URL.substring(is_input, document.URL.length)+'">'+link_text+'</a>');
  }
  else {
    document.write ('<a href="http://apps.co-operativebank.co.uk/funnel.jsp?media='+default_media+'">'+link_text+'</a>');
  }
}

//Below is for links to new app forms

function generateFunnelLink2(default_media, link_text) {
  if (link_text == undefined) {link_text='online';}
  // Create variable is_input to see if there is a media= in the url
  var is_input = document.URL.indexOf('media=');
  // Check the position of the media= in the url
  if (is_input != -1){ 
    // Write line from media= in the url to the end of the string
    document.write ('<a href="https://applications.co-operativebank.co.uk/apps/funnel.faces?'+document.URL.substring(is_input, document.URL.length)+'">'+link_text+'</a>');
  }
  else {
    document.write ('<a href="https://applications.co-operativebank.co.uk/apps/funnel.faces?media='+default_media+'">'+link_text+'</a>');
  }
}



//////////////////////////////////////////////////
//
//  generateFunnelURL()
//
//  Generates the URL for a media-coded hyperlink to funnel.jsp that uses the media code present in the current page's URL
//
//  Author:   Andy Hames 18/10/2007
//
//  Parameters:
//    default_media   A default media code to use if none is detected
//
//////////////////////////////////////////////////
function generateFunnelURL(default_media) {
  // Create variable is_input to see if there is a media= in the url
  var is_input = document.URL.indexOf('media=');
  var result; // Return value 

  // Check the position of the media= in the url
  if (is_input != -1){ 
    // Write line from media= in the url to the end of the string
    result = 'http://apps.co-operativebank.co.uk/funnel.jsp?'+document.URL.substring(is_input, document.URL.length);
  }
  else {
    result = 'http://apps.co-operativebank.co.uk/funnel.jsp?media='+default_media;
  }
  
  return result;
}

function generateFunnelURL2(default_media) {
  // Create variable is_input to see if there is a media= in the url
  var is_input = document.URL.indexOf('media=');
  var result; // Return value 

  // Check the position of the media= in the url
  if (is_input != -1){ 
    // Write line from media= in the url to the end of the string
    result = 'https://applications.co-operativebank.co.uk/apps/funnel.faces?'+document.URL.substring(is_input, document.URL.length);
  }
  else {
    result = 'https://applications.co-operativebank.co.uk/apps/funnel.faces?media='+default_media;
  }
  
  return result;
}

//////////////////////////////////////////////////
//
//  generateTechnoPreambleURL()
//
//  Generates the URL for a media-coded hyperlink to product preamble page
//
//  Author:   Andy Hames 10/07/2008
//
//  Parameters:
//    pageCid      cid of the target preamble page
//    mediaCode    The media code to pass through
//
//////////////////////////////////////////////////
function generateTechnoPreambleURL(pageCid, mediaCode) {
	var result = null;
	if ((pageCid == undefined) || (mediaCode == undefined)) {
		result = '#<!-- Error: parameter not specified -->';
	} 
	else if((pageCid == '1193206375241'))
	{
//		result = 'https://applications.co-operativebank.co.uk/apps/funnel.faces?media='
//	+ mediaCode;	
		result = 'https://applications.co-operativebank.co.uk/apps/funnel.faces?media='
	+ mediaCode;	

	}
	else {
		result = '/servlet/Satellite/' + pageCid + ',CFSweb/Page/ImportantInformation&amp;media=' + mediaCode;
	}
	return result;
}

function generateTechnoPreamble2URL(pageCid, mediaCode) {
	var result = null;
	if ((pageCid == undefined) || (mediaCode == undefined)) {
		result = '#<!-- Error: parameter not specified -->';
	} 
	else if((pageCid == '1193206375241'))
	{
//		result = 'https://applications.co-operativebank.co.uk/apps/funnel.faces?media='
//	+ mediaCode;	
		result = 'https://applications.co-operativebank.co.uk/apps/funnel.faces?media='
	+ mediaCode;	

	}
	else {
		result = '/servlet/Satellite/' + pageCid + ',CFSweb/Page/ImportantInformation2&amp;media=' + mediaCode;
	}
	return result;
}


//////////////////////////////////////////////////
//
//  generateTechnoPreambleButton()
//
//  Generates an Apply button with a media-coded hyperlink to product preamble page
//
//  Author:   Andy Hames 10/07/2008
//
//  Parameters:
//    pageCid      cid of the target preamble page
//    mediaCode    The media code to pass through
//
//////////////////////////////////////////////////
function generateTechnoPreambleButton(pageCid, mediaCode) {
	document.write('<p><a class="but_apply" href="' + generateTechnoPreambleURL(pageCid, mediaCode) + '" target="_top">apply now</a></p>');
}

function generateTechnoPreambleButton2(pageCid, mediaCode) {
	document.write('<p><a class="but_apply2" href="' + generateTechnoPreambleURL(pageCid, mediaCode) + '" target="_top">Apply now</a></p>');
}

function generateTechnoPreambleButton3(pageCid, mediaCode) {
	document.write('<p><a class="but_mem" href="' + generateTechnoPreambleURL(pageCid, mediaCode) + '" target="_top">Apply now</a></p>');
}

function generateTechnoPreambleButton4(pageCid, mediaCode) {
	document.write('<p><a class="but_nonmem" href="' + generateTechnoPreambleURL(pageCid, mediaCode) + '" target="_top">Apply now</a></p>');
}

function generateTechnoPreambleButtonNew(pageCid, mediaCode) {
	document.write('<p><a class="but_apply2" href="' + generateTechnoPreamble2URL(pageCid, mediaCode) + '" target="_top">Apply now</a></p>');
}



//////////////////////////////////////////////////
//
//  generateTechnoFunnelURL()
//
//  Generates the URL for a media-coded hyperlink to online applications
//
//  Author:   Andy Hames 11/10/2007
//
//  Parameters:
//    mediaCode   The media code to use for the link
//
//////////////////////////////////////////////////
function generateTechnoFunnelURL(mediaCode) {
	return 'https://applications.co-operativebank.co.uk/apps/funnel.faces?media=' + mediaCode;
	//return 'http://apps.co-operativebank.co.uk/funnel.jsp?media=' + mediaCode;
}


function generateApplyButton(mediaCode) {
	document.write('<p><a class="but_apply" href="' + generateTechnoFunnelURL(mediaCode) + '" target="_top">apply now</a></p>');
}



//////////////////////////////////////////////////
//
//  checkMediaCode()
//
//  Checks for a media code in the current page's url and returns that value if found, or the specified default if it isn't
//
//  Author:   Andy Hames 10/07/2008
//
//  Parameters:
//    defaultCode   The default media code to return if not in the page URL
//
//////////////////////////////////////////////////
function checkMediaCode(defaultCode) {
	var result = (defaultCode == undefined) ? null : defaultCode;
	var qs = location.href;
	for (var i = 0; i < qs.split('&').length; i++) {
		var param = qs.split('&')[i];
		if (param.indexOf('media=') > -1) {
			result = (param.split('=')[1].length > 0) ? param.split('=')[1] : result;
			break;
		}
	}
	return result;
}



function doPopup(winName, winURL, w, h) {
	eval('popUp_' + winName + ' = window.open(winURL, winName, \'width=' + w + ',height=' + h + ',status=0,scrollbars=1,resizable=1\')');
}

//////////////////////////////////////////////////

//

//  generateFunnelLink()

//

//  Generates a media-coded hyperlink to funnel.jsp that uses the media code present in the current page's URL

//

//  Author:   Aden Millen 

//  History:  18/10/2007 (Andy Hames) - Moved to external file and parameterised

//

//  Parameters:

//    default_media   A default media code to use if none is detected

//

//////////////////////////////////////////////////

function generateFunnelLink(default_media, link_text) {

  if (link_text == undefined) {var link_text='online';}

  // Create variable is_input to see if there is a media= in the url

  var is_input = document.URL.indexOf('media=');

  // Check the position of the media= in the url

  if (is_input != -1){ 

    // Write line from media= in the url to the end of the string

    document.write ('<a href="http://apps.co-operativebank.co.uk/funnel.jsp?'+document.URL.substring(is_input, document.URL.length)+'">'+link_text+'</a>');

  }

  else {

    document.write ('<a href="http://apps.co-operativebank.co.uk/funnel.jsp?media='+default_media+'">'+link_text+'</a>');

  }

}



//////////////////////////////////////////////////

//

//  generateFunnelURL()

//

//  Generates the URL for a media-coded hyperlink to funnel.jsp that uses the media code present in the current page's URL

//

//  Author:   Andy Hames 18/10/2007

//

//  Parameters:

//    default_media   A default media code to use if none is detected

//

//////////////////////////////////////////////////

function generateFunnelURL(default_media) {

  // Create variable is_input to see if there is a media= in the url

  var is_input = document.URL.indexOf('media=');

  var result; // Return value 



  // Check the position of the media= in the url

  if (is_input != -1){ 

    // Write line from media= in the url to the end of the string

    result = 'http://apps.co-operativebank.co.uk/funnel.jsp?'+document.URL.substring(is_input, document.URL.length);

  }

  else {

    result = 'http://apps.co-operativebank.co.uk/funnel.jsp?media='+default_media;

  }
  return result;

}

/**********************************************************************************************************************************/
/*** Function to convert certain html list items into expanding text      *********************************************************/
/*** USAGE: Should be called on page load (window.onload)                 *********************************************************/
/*** List child span element (to click) should have a class of "expClick" *********************************************************/
/*** List child div element to expand should have a class of "expText"    *********************************************************/
/**********************************************************************************************************************************/
/*** Raj Parmar - 11/12/07                                                *********************************************************/
/**********************************************************************************************************************************/
var listImagePath = '/bank/img/';
var listItemTitle = 'click to expand/hide';

function enableExpandingListTexts() {
	var d = document.getElementsByTagName('li');
	for (var i = 0; i < d.length; i++) {
		for (var s = 0; s < d[i].childNodes.length; s++) {
			if ((d[i].childNodes[s].nodeType == 1) && (d[i].childNodes[s].tagName == 'SPAN')){
				if (d[i].childNodes[s].className.indexOf('expClick') > -1) {
					var clickObj = d[i].childNodes[s];
					clickObj.visible = true;
					clickObj.style.cursor = 'pointer';
					clickObj.style.paddingLeft = '20px';
					clickObj.style.marginLeft = '-20px';
					clickObj.title = listItemTitle;
					if (document.attachEvent) {
						s = doIEFix(d[i],s);
						clickObj['onclick'] = function(){toggleExpText(this)};
						clickObj['onmouseover'] = function(){this.style.textDecoration='underline'};
						clickObj['onmouseout'] = function(){this.style.textDecoration='none'};
					}
					else if(document.addEventListener) {
						clickObj.addEventListener('click', function(){toggleExpText(this);}, false);
						clickObj.addEventListener('mouseover', function(){this.style.textDecoration='underline'}, false);
						clickObj.addEventListener('mouseout', function(){this.style.textDecoration='none'}, false);
					}
					else {
						//Doesnt work?
					}
					toggleExpText(clickObj);
				}
			}
		}
	}
}
/**********************************************************************************************************************************/
function toggleExpText(clickObj){
	clickObj.visible = !clickObj.visible;
	obj = getExpandingSibling(clickObj);
	if (obj.className.indexOf('expText') > -1) {
		obj.style.display = (clickObj.visible)? 'block' : 'none';
		obj.parentNode.style.listStyleImage = 'url(\'' + listImagePath + ((clickObj.visible)? 'collapse' : 'expand') + '.gif\')';
	}
//sjc 7th July 2009 - setFooter(); called to stop footer wrapping over copy at bottom of page
setFooter();
}
/**********************************************************************************************************************************/
function doAllExpText(ulObj, x){
	var ul = document.getElementById(ulObj);
	d = ul.getElementsByTagName('li');
	for (var i = 0; i < d.length; i++) {
		for (var s = 0; s < d[i].childNodes.length; s++) {
			if ((d[i].childNodes[s].nodeType == 1) && (d[i].childNodes[s].tagName == 'SPAN')){
				if (d[i].childNodes[s].className.indexOf('expClick') > -1) {
					var clickObj = d[i].childNodes[s];
					var obj = getExpandingSibling(clickObj);
					if (obj.className.indexOf('expText') > -1) {
						if (x == 1) {
							clickObj.visible = true;
							obj.style.display = 'block';
							obj.parentNode.style.listStyleImage = 'url(\'' + listImagePath + 'collapse.gif\')';
						}
						else {
							clickObj.visible = false;
							obj.style.display = 'none';
							obj.parentNode.style.listStyleImage = 'url(\'' + listImagePath + 'expand.gif\')';
						}
					}
				}
			}
		}
	}
//sjc 7th July 2009 - setFooter(); called to stop footer wrapping over copy at bottom of page
setFooter();	
}
/**********************************************************************************************************************************/
function getExpandingSibling(obj) {
	p = obj.parentNode;
	result = null;
	for (var a=0;a<p.childNodes.length;a++){
		if ((p.childNodes[a].nodeType == 1) && (p.childNodes[a].className.indexOf('expClick') == -1)){
		  if (p.childNodes[a].className.indexOf('expText') > -1) {
				result = p.childNodes[a];
			}
		}
	}
	return result;
}
/************************************************************************************************************/
/* Resize flash function */

  function resizeCalc(pixelHeight) {
    if (document.getElementById) {
      var subNavDiv = document.getElementById('loancalc');
	  var banners = document.getElementById('banners');
      if (subNavDiv != null) {
        subNavDiv.style.height = pixelHeight + 'px';
		banners.style.top = pixelHeight + 205 + 'px';
        if (navigator.userAgent.indexOf('Firefox') != -1) {
          // Force redraw - fix for Firefox 2 bug
          var container = document.getElementById('copy');
          var ffToggle = container.style.marginTop == '1px' ? true : false;
          if (ffToggle) {
            container.style.marginTop = '0px';
          } else {
            container.style.marginTop = '1px';
          }
          setFooter();
        }
      }
    }
    return;
  }


/**********************************************************************************************************************************/
function doIEFix(o,s){
  if(navigator.appVersion.indexOf('MSIE') > 0){
    var z = document.createElement('div');
    z.innerHTML = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
		z.title = listItemTitle;
    z['onclick'] = function(){toggleExpText(o.childNodes[1]);this.blur();};
    z['onmouseover'] = function(){o.childNodes[1].style.textDecoration='underline'};
    z['onmouseout'] = function(){o.childNodes[1].style.textDecoration='none'};
    z.style.cssText = 'cursor:pointer;position:absolute;width:12px;height:12px;margin-left:-18px;font-size:small;overflow:hidden';
    //alert(o.parentNode.firstChild.tagName);
    o.insertBefore(z,o.firstChild);
    //parents childnodes has expanded, compensate for loop.
    s++;
  }
  return s;
}

/**********************************************************************************************************************************/
/***[ IB DROPDOWN - IE6 FIX  ******************************************************************************************************/
/**********************************************************************************************************************************/


startDropdown = function() {
if (document.all&&document.getElementById) {
navRoot = document.getElementById("nav");
for (i=0; i<navRoot.childNodes.length; i++) {
node = navRoot.childNodes[i];
if (node.nodeName=="LI") {
node.onmouseover=function() {
this.className+=" down";
  }
  node.onmouseout=function() {
  this.className=this.className.replace;
	(" down", "");
   }
   }
  }
 }
}


//window.onload = enableExpandingListTexts;
/**********************************************************************************************************************************/
/***[ END EXPANDING LIST FUNCTIONS ************************************************************************************************/
/**********************************************************************************************************************************/

/**********************************************************************************************************************************/
/*** Open METAFAQS links in sized new windows  ************************************************************************************/
/**********************************************************************************************************************************/

function openMetafaq(mfUrl){
     window.name  = "mfOpener";
     winHandle    = window.open(mfUrl, "mfWindow",
"width=600,height=600,screenX=15,screenY=10,top=15,left=20,scrollbars=yes,resizable=yes");
     winHandle.focus();
     return false;
   }
/**********************************************************************************************************************************/
/*** End METAFAQS  ****************************************************************************************************************/
/**********************************************************************************************************************************/

	function setCookie(ref){
		var refer = ref;
		var today = new Date();
		today.setTime( today.getTime() );
		var expires = 30 * 1000 * 60 * 60 * 24;
		var expires_date = new Date( today.getTime() + (expires) );
		document.cookie = "referer=" +refer+ "; expires="+ expires_date.toGMTString() +"; path=/";
	}



window.onload = function() {
	setFooter();
	sizePDFBox();
	enableExpandingListTexts();
	startDropdown();
}
window.onresize = function() {
	sizePDFBox();
	setFooter();
}














