/**
 * local groupsite javascript function library
 */

function blurLink(objLink){if(objLink.blur){objLink.blur()}}

/**
 * Used by forms with multiple submit buttons to switch value of hidden field 'formid'
 * which controls the server action carried out in response
 */
function setFormId(name, id){
	var frm=document.forms[name]
	frm.elements['formid'].value=id
}

/**
 * Based on original code by Paul@YellowPencil.com and Scott@YellowPencil.com
 * 
 * Determines the heights of left-nav and page-body then equalizes them
 */
function setPageColHeight(){
	var browser=checkBrowser();
	if (document.getElementById){
		// the divs array contains references to each column's div element.  
		var divs=new Array(document.getElementById('page-body'),document.getElementById('left-nav') );
		// Determine maximum height of columns specified
		var maxHeight=0;
		for(var i=0; i<divs.length; i++){
			if(divs[i]){
				switch (browser){
					case "ie":
						if (navigator.userAgent.indexOf("MSIE 7")>0) divs[i].style.minHeight='auto';
						else divs[i].style.height='auto'; break;
					case "ns":
						divs[i].style.minHeight=0; break;
				}
				if(divs[i].offsetHeight>maxHeight){maxHeight=divs[i].offsetHeight}
			}
		}
		// Set all columns to that maximum height
		for(var i=0; i<divs.length; i++) {
			if(divs[i]){
				switch (browser){
					case "ie":
						if(navigator.userAgent.indexOf("MSIE 7")>0) divs[i].style.minHeight=maxHeight+'px';
						else divs[i].style.height=maxHeight+'px'; break;
					case "ns":
						divs[i].style.minHeight=maxHeight+'px';
						divs[i].style.height='auto'; break;
				}
				// Now, if the browser's in standards-compliant mode, the height property
				// sets the height excluding padding, so we figure the padding out by subtracting the
				// old maxHeight from the new offsetHeight, and compensate!  So it works in Safari AND in IE 5.x
				if(divs[i].offsetHeight>maxHeight){
					divs[i].style.minHeight=(maxHeight-(divs[i].offsetHeight-maxHeight))+'px';
				}
			}
		}
	}
}

// Based on code from http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
function pageHeight(){
  var myWidth = 0, myHeight = 0;
  if(typeof(window.innerWidth)=='number'){
    //Non-IE
    myWidth=window.innerWidth;
    myHeight=window.innerHeight;
  }else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)){
    //IE 6+ in 'standards compliant mode'
    myWidth=document.documentElement.clientWidth;
    myHeight=document.documentElement.clientHeight;
  }else if(document.body && (document.body.clientWidth || document.body.clientHeight)){
    //IE 4 compatible
    myWidth=document.body.clientWidth;
    myHeight=document.body.clientHeight;
  }
  return myHeight;
}

// [iain] Javascript for showing and hiding divs
// Determines browser used - can therefore choose correct algorithm for positioning and modifying div's etc
function whichBrowser(){
	if(document.getElementById){return 1;} //N6,IE5
	if(document.layers){return 2;} //N4
	if(document.all){return 3;} //IE4+
}
// action is either 'block' or 'none'
function switchDiv(divName,action){
	switch (whichBrowser()){
		case 1: 
			if (document.getElementById(divName)) { document.getElementById(divName).style.display=action; } 
			break;//N6,IE5
	    case 2:
			if (document.layers[divName]) { document.layers[divName].display=action; }
			break;//N4
		case 3:
			if (document.all.divName) { document.all.divName.style.display=action; }
			break;//IE4+
	}	
}

// Clear all existing errors shown 
function showError(e){
	hideErrors();
	if(document.getElementById(e)){document.getElementById(e).style.display='block';}
	else alert("Error '"+e+"' occurred but the error message couldn't be displayed!");
}

function hideErrors(){
	lyrs=document.getElementsByTagName('div');
	for(i=0; i<lyrs.length; i++){
		var class_name = lyrs[i].className;
		if(class_name=='error' || class_name.indexOf('error-box') != -1){
			lyrs[i].style.display='none';
		}
	}
}

function checkBrowser(){
	if(navigator.appName=="Netscape"){return "ns";}
	else if(navigator.appName=="Microsoft Internet Explorer"){return "ie";}
}

function disableMe(element, imgsrc){
	if(element.style){element.style.cursor='wait';}
	if(element.type){
		switch(element.type.toLowerCase()){
			case 'submit': element.disabled=true; break;
			case 'image': if(imgsrc!=''){element.src=imgsrc; element.disabled=true;} break;
			case 'button': element.disabled=true; break;
		}
	}
}

//[matt w] Retreives element by id or returns false if element doesn't exist
function getElement(e){
	if( !document.getElementById ) return false;
	return ( !document.getElementById(e) )? false : document.getElementById(e);
}

var btn_col; // button col

// [matt w] disable / enable elements and apply new styling - used with new button styling
function disableElement(e){
	var disable = true;
	var obj = false;
	
	if(typeof(e) != 'object' && typeof(e) == 'string'){
		obj = getElement(e);
		if(obj != false) e = obj;
	}else{
		obj = true;
	}	
	
	if(obj == false) return false;
	
	if(e.type){
		switch(e.type.toLowerCase()){
			case 'submit':
			case 'button':
				var class_list = (e.className).split('-');
				
				if(e.className.indexOf('disable') != -1){
					disable = false;
					class_list.pop();
				}else{		
					btn_col = class_list.pop();	
				}
				
				class_list.push((disable)? 'disable' : btn_col);
				
				e.className = class_list.join('-');
			break;
		}
	}
	
	if(e.style){
		e.style.cursor = disable == true ? 'wait' : 'default';
		e.disabled = disable;
	}
}

function is_int(num){
	if(num == null || num == '') return false; 
	if(parseInt(num) == num) return true;
	return false;
}
function is_float(num){
	if(num == null || num == '') return false; 
	if(parseFloat(num) == num) return true;
	return false;
}


// [js 23/10/06] Modded from global/scr/functions.js::popUp
// [js 23/10/06] Altered to close any previous open help window before opening this one.
// generic pop up opener, allows complete control over window size, scrolling, etc.
// JT 22/04/08 generic my foot! Clearly inferior to openWin(), see below for example. 
// This function is used in forms for little help boxes that explain an individual field.
function helpPopUp( fileName, theOptions ){
	// If we are on a new page we need to reclaim the handle
	helppopup_win=window.open('',"helppopup",theOptions);
	// If the window is already open, close it.
	if(helppopup_win.location.href!="about:blank"){helppopup_win.close();}
	// Either open a new window or load in the right url into the handle.
	helppopup_win=window.open(fileName,"helppopup",theOptions);
	if(helppopup_win.opener==null){helppopup_win.opener=self;}
	helppopup_win.focus();
}

// JT 22/04/08 For the ? icons in the top corner to open the Help section
function openHelpWin(href){
	return openWin(href,'Help',0,0,780,300,'toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,scrollbars=yes');
}

function leavingHLPopup(url,tosite){	
	if (url != "" && tosite != ""){
		var w=500, h=300;
		switch(tosite){
			case "hlmarkets":w=500;h=305;break;
			case "scottishwidows":w=500;h=240;break;
			case "halifax":w=500;h=240;break;
		}
		var newwindow=window.open(url+'?tosite='+tosite,'','height='+h+',width='+w+',resizable=0,scrollbars=0');
		if(window.focus){newwindow.focus();}
	}
}

// function detects if adobe acrobat / reader is installed, and which version
function detectAdobe(){
	acrobat=new Object();
	acrobat.installed=false;
	acrobat.version='0.0';
	// Firefox check
	if(navigator.plugins && navigator.plugins.length){
		for (x=0; x < navigator.plugins.length; x++){
			if (navigator.plugins[x].name.indexOf('Adobe') != -1){
				// Try to get version from description - only works up to + inc v6
				if(!isNaN(parseFloat(navigator.plugins[x].description.split('Version ')[1]))){
					acrobat.version = parseFloat(navigator.plugins[x].description.split('Version ')[1]);
					if (acrobat.version.toString().length == 1){acrobat.version+='.0';}
				}
				acrobat.installed=true;
				break;
			}
		}
	}
	// IE check
	else if(window.ActiveXObject){
		for (x=2; x<10; x++){
			try{
				oAcro=eval("new ActiveXObject('PDF.PdfCtrl."+x+"');");
				if (oAcro){
					acrobat.installed=true;
					acrobat.version=x+'.0';
				}
			}catch(e){}
		}
		try{
			oAcro4=new ActiveXObject('PDF.PdfCtrl.1');
			if (oAcro4){
				acrobat.installed=true;
				acrobat.version='4.0';
			}
		}catch(e){}
		try{
			// If AcroPDF.dll exists, version is 7+. No differentiation between versions above 7.
			oAcro7=new ActiveXObject('AcroPDF.PDF.1');
			if (oAcro7){
				acrobat.installed=true;
				acrobat.version='7';
			}
		}catch(e){}
	}
	var adb=new Array();
	adb['installed']=acrobat.installed;
	adb['version']=acrobat.version;
	return adb;
}

//function detects if adobe acrobat / reader is installed and latest version
function adobeIsLatest(){
	latest=7;
	adobe=detectAdobe();
	if(adobe['version'] && adobe['version']>=latest){return true;}
	else{return false;}
}

//	function detects if adobe acrobat / reader is installed, and is the minimum required version
function adobeIsMinimum(){
	minimum=5;
	adobe=detectAdobe();
	if(adobe['version'] && adobe['version']>=minimum){return true;}
	else{return false;}
}

//	function returns an integer used in adobe_reader_link.tpl to display relevant message
function getAdobeLinkId(){
	adobe=detectAdobe();
	// Adobe installed and latest detectable version
	if(adobe['installed'] && adobeIsLatest()){adobe['status']=1; return adobe;}
	// Adobe installed but can't detect version
	else if(adobe['installed'] && adobe['version']<1){adobe['status']=2; return adobe;}
	// Adobe installed and old version
	else if(adobe['installed'] && !adobeIsLatest()){adobe['status']=3; return adobe;}
	// Adobe not detected
	else{adobe['status']=4; return adobe;}
}

// function removes trailing zeros from end of a number
function remTrailingZeros(x) {
	var decPos=x.indexOf(".");
	if(decPos>-1){
		first=x.substring(0,decPos);
		second=x.substring(decPos,x.length);
		while (second.charAt(second.length-1)=="0"){
			second=second.substring(0,second.length-1);
		}
		if(second.length>1){ return first+second; }
		else{ return first; }
	}
	return x;
}

// format a post code with spaces
function pad_postcode(postcode){
	postcode = postcode.replace(' ','').toUpperCase();
	if(postcode.length==5)postcode=postcode.substr(0,2)+' '+postcode.substr(2,3);
	else if(postcode.length==6)postcode=postcode.substr(0,3)+' '+postcode.substr(3,3);
	else if(postcode.length==7)postcode=postcode.substr(0,4)+' '+postcode.substr(4,3);
	return postcode;
}
// format a postcode
function format_postcode(postcode){
	postcode = postcode.replace(' ','').toUpperCase();
	postcode = postcode.substr(0,(postcode.length-3))+' '+postcode.substr((postcode.length-3),3);
	return postcode;
}
// format a postcode for bf
function format_postcode_bf(postcode){
	postcode = postcode.replace(' ','').toUpperCase();
	postcode = postcode.substr(0,(postcode.length-3))+str_repeat(' ',7-postcode.length)+postcode.substr((postcode.length-3),3);
	
	if(postcode.length==5) postcode=postcode.substr(0,2)+'  '+postcode.substr(2,3);
	else if(postcode.length==6) postcode=postcode.substr(0,3)+' '+postcode.substr(3,3);
	else if(postcode.length==7) postcode=postcode.substr(0,4)+''+postcode.substr(4,3);
	return postcode;
}
function str_repeat(str,num_repeat){
	var r=0;
	var out='';
	while(r<num_repeat){out+=str;r++;}
	return out;
}

// Functions done with jQuery ----------------------------------
$(function() {

	// ---------------------------------------------------------
	// Name: Search Terms
	// Description: Used for search boxes and other input boxes
	// 				with a default value that should vanish when
	//              a user clicks within the box
	// Useage:      Give any text input a class of 'default-input-text'
	// 		        and make sure it has an ID.
	// ---------------------------------------------------------
	
	// Used to store default search terms
	var default_input_terms = new Array();
	
	// hides default search terms from a input box
	$('.default-input-text').focus(function() {
		
		// Store the default term if it hasnt already been stored
		if(!default_input_terms[this.id]) {
			default_input_terms[this.id] = $(this).val();
		}
		
		// If the current value is the default value, hide it
		if($(this).val() == default_input_terms[this.id]) {
			$(this).val('').removeClass('default-input-text').addClass('non-default-input-text');
		}
		
		// when the event is blured, put the default message back if they havent written anything
		$(this).blur( function () {
			default_term = default_input_terms[this.id];
			if($(this).val() == '') $(this).removeClass('non-default-input-text').addClass('default-input-text').val(default_term);
		} );
		
	});
	// --- End of "Search Terms" -------------------------------
	
});


// Article font size control panel
function hand(label){label.style.cursor='hand';}

function setArticleText(size){
	var art=document.getElementById('article');
	art.className="article-box article-"+size;
	setPageColHeight();
}



// Form jump menus
function faq_jump () {
	var frm=document.forms['frmJump'];
	var url=window.location.toString();
	var ix=url.indexOf("#",0);
	if (ix != -1){url=url.substring(0,ix);}
	url=url+frm.question.value;
	window.location=url;
	return false;
}


// Hide / Show	
function set_visibility(e_id){
	var e = document.getElementById(e_id);
	e.style.display = (e.style.display == "block")? "none" : "block";
}


