function isObject(o) {
    return (typeof (o) == "object");
}
function isString(o) {
    return (typeof (o) == "string");
}
function isArray(o) {
    return (isObject(o) && (o.length) && (!isString(o)));
}

function getObj(elementName)
{
	//return $('#' + elementName).get();

if(document.getElementById)
	{
		return document.getElementById(elementName);
	}
	else
	{
		return eval("document.all." + elementName);
	}

}

function populatePostcodeMask()
{
	var array = new Array();
	
	array['DZ']='NNNNN';
	array['AS']='NNNNN';
	array['AD']='AANNN';
	array['AR']='NNNN';
	array['AM']='NNNNNN';
	array['AU']='NNNN';
	array['AT']='NNNN';
	array['AZ']='AA-NNNN';
	array['BD']='NNNN';
	array['BY']='NNNNNN';
	array['BE']='NNNN';
	array['BM']='AA NN';
	array['BA']='NNNNN';
	array['BR']='NNNNN-NNN';
	array['BN']='NNNN,AANNNN';
	array['BG']='NNNN';
	array['CA']='ANA NAN,ANANAN';
	array['CN']='NNNNNN';
	array['CX']='NNNN';
	array['CC']='NNNN';
	array['HR']='NNNNN';
	array['CY']='NNNN';
	array['CZ']='NNN NN,NNNNN';
	array['DK']='NNNN';
	array['TL']='NNNNN';
	array['TP']='NNNNN';
	array['EE']='AANNNN,NNNN,NNNNN';
	array['FO']='AA-NNN';
	array['FI']='NNNNN';
	array['FR']='NNNNN';
	array['GF']='NNNNN';
	array['GE']='NNNN,NNNNNN';
	array['DE']='NNNNN';
	array['GR']='NNN NN';
	array['GL']='NNNN';
	array['GP']='NNNNN';
	array['GU']='NNNNN';
	array['GT']='NNNNN';
	array['GW']='NNNN';
	array['VA']='NNNNN';
	array['HU']='NNNN';
	array['IS']='NNN';
	array['IN']='NNNNNN';
	array['ID']='NNNNN';
	array['IL']='NNNNN';
	array['IT']='NNNNN';
	array['JP']='NNN,NNN NN,NNN NNNN,NNNNNNN';
	array['JO']='NNNNN';
	array['KZ']='NNNNNN';
	array['YK']='NNNNN';
	array['KW']='NNNNN';
	array['KG']='NNNNNN';
	array['LV']='NNNN,AA-NNNN';
	array['LI']='NNNN';
	array['LT']='AA-NNNNN';
	array['LU']='NNNN';
	array['MK']='NNNNN,NNNN';
	array['MG']='NNN';
	array['MY']='NNNNN';
	array['MH']='NNNNN';
	array['MQ']='NNNNN';
	array['YT']='NNNNN';
	array['MX']='NNNNN';
	array['FM']='NNNNN';
	array['MD']='NNNN,NNNNNN';
	array['MC']='NNNNN';
	array['MN']='NNNNNN';
	array['ME']='NNNNN';
	array['MA']='NNNNN';
	array['MM']='NNNNN';
	array['NL']='NNNN,NNNN AA';
	array['NZ']='NNNN';
	array['MP']='NNNNN';
	array['NO']='NNNN';
	array['PW']='NNNNN';
	array['PS']='NNNNN';
	array['PH']='NNNN';
	array['PL']='NN-NNN,NNNNN';
	array['PT']='NNNN-NNN';
	array['PR']='NNNNN';
	array['RE']='NNNNN';
	array['RO']='NNNN,NNNNN,NNN,NNNNNN,NNNNNNN';
	array['RU']='NNNNNN';
	array['PM']='NNNNN';
	array['SM']='NNNNN';
	array['SA']='NNNNN';
	array['RS']='NNNNN';
	array['SG']='NNNNNN';
	array['SK']='NNN NN,NNNNN';
	array['SI']='NNNN';
	array['ZA']='NNNN';
	array['KR']='NNN-NNN';
	array['ES']='NNNNN';
	array['SE']='NNN NN,NNNNN';
	array['CH']='NNNN';
	array['TW']='NNN,NNNNN';
	array['TJ']='NNNNNN';
	array['TH']='NNNNN';
	array['TN']='NNNN';
	array['TR']='NNNNN';
	array['TM']='NNNNNN';
	array['UA']='NNNNNN,NNNNN';
	array['GB']='AAA NAA,AAN NAA,AANA NAA,AANN NAA,AANNN,AN NAA,ANA NAA,ANN NAA';
	array['US']='NNNNN,NNNNNNNNN';
	array['UY']='NNNNN';
	array['UZ']='NNNNNN';
	array['VE']='NNNN';
	array['VN']='NNNNN';
	array['VI']='NNNNN';
	
	return array;
}


function createInputDate(stringMyDate)
{
	// Date format dd/mm/yyyy
	
	var dt = stringMyDate.split("/");
	dt = dt.reverse();
	return new Date(dt[0] + "/" + dt[1] + "/" + dt[2]);
	
}


function formatOutputDate(myDate)
{
	// output formt dd/mm/yyyy
	var day = (myDate.getDate() < 10) ? ('0' + myDate.getDate()) : (myDate.getDate());
	var month = (parseInt(myDate.getMonth()) + 1 < 10) ? ('0' + (parseInt(myDate.getMonth()) + 1)) : (parseInt(myDate.getMonth()) + 1);
	var year = myDate.getFullYear();
	
	return day + "/" + month + "/" + year;
}


//	Safe Firebug debug call
function debug(obj){
	try{
		console.log(obj)
	}catch(e){}
}

//	Simple JS access to cookies
function createCookie(name,value,days){
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

// Read a cookie
function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
//	Delete a cookie
function eraseCookie(name)
{
	createCookie(name,"",-1);
}


//	Setup Mechanism
//	Initialises all javascript

var largestBody	= 0;
var largestFooter  = 0;
var largestLowerBody  = 0;
var largestLowerFooter  = 0;
var firstLeftChild = 1;


function setup(){
	
	//	Top level othersites drop down
	$('#otherSites').unbind().hoverIntent(
		{ sensitivity: 1,
		   interval: 150,
		   over: function(){
		   		$(this).children('ul').find('a').css('height','2.45em');
				$(this).children('ul').slideDown({ duration: 300, easing: 'easeOutCirc',complete:function(){$(this).find('a').css('height','auto');}})},
		   timeout: 150,
		   out: function(){
		   		$(this).children('ul').find('a').css('height','2.45em');
				$(this).children('ul').slideUp({ duration: 300, easing: 'easeInCirc',
				// This keeps arrows visible after drop down slides up in IE
				complete:function(){$(this).find('a').css('height','auto');$('#otherSites').toggleClass('hasMenu').toggleClass('hasMenu')}});
			}
		}
	)
	
	//	Disable other sites click
	//$('#otherSites').click(function(){return false});

	
	//	Main menu drop downs (overrides css)
	$('#mainNavigation').children('li').unbind().hoverIntent(
		{ sensitivity: 1,
		   interval: 100,
		   over: function(){
				$(this).children('ul').slideDown({ duration: 300, easing: 'easeOutCirc'});
			},//
		   timeout: 100,
		   out: function(){
		   		$(this).children('ul').slideUp({ duration: 100, easing: 'easeOutCirc'});
			}
		}
	)
	
	//	Disable section links
	$('#mainNavigation').children('li').children('a').unbind().click(function(){return false});
	
	//	Main menu drop downs (overrides css)
	$('#popup div.body').hoverIntent(
		{ sensitivity: 1,
		   interval: 100,
		   over: function(){},
		   timeout: 100,
		   out: function(){
		   		toggleSites()
				$('#popup div.body').slideUp({ duration: 300, easing: 'easeInOutCirc'});
				
				}
		}
	)

	normaliseHeights();
	if($('#styleSize').length > 0)
	{
	//	Size switcher
 	$('#styleSize').click(function() {
		   switchSize($(this).html());
            return false;
      });
	}

	// Override Main menu CSS
	$('#mainNavigation').find('ul').css('display','none');
	
	//	Country clicker
	$('#siteWideNavigation').find('.change').click(toggleSites);

	//	Left column
	lineHeight();

  // Change style size if cookie exists
  var c = readCookie('style');
  if (c) switchSize(c);

  if (jQuery.browser.msie)
  {
    $('button').mousedown(function() {
            $(this).addClass('active');
          });
    $('button').mouseup(function() {
            $(this).removeClass('active');
          });
  }

	$('input').removeClass('error');
	$('select').removeClass('error');

  // Fix the buttons for firefox so it isn't jagged.
  if (jQuery.browser.mozilla == true)
  {
    $('button').addClass('FireFoxButton');
	$('.application input[type=checkbox]').css({margin: '4px'});
  }
	
  // Create the date picker on the homepage
  $('.datePicker').css('width', 152);
  $('.datePicker').datepicker({ 
	showOn: "button", 
    buttonImage: "/_img/homepage/datepicker.gif", 
    buttonImageOnly: true
    });

  // Cycle through each element with the class of "tabs" 
  $.each($('.tabs'), function() {
  
        var thisObj = $(this);
        
        // Replace the list with just the links.
        thisObj.find('ul').replaceWith(thisObj.find('li:first').html() + thisObj.find('li:last').html());
        
        // Wrap the links with the table information.
        thisObj.find('a:first').wrap('<td class="tdLeft tdLeftActive"></td>');
        thisObj.find('a:last').wrap('<td class="tdRight"></td>');
        thisObj.find('td').wrapAll('<table ><tr></tr></table>');
        
        //alert(thisObj.html());
                
        // Setup the click events for showing and hiding the correct tab.
        thisObj.find('a').click(function() {
            // Check that the user hasn't clicked on an already active tab by the td's class.
            if (!$(this).parent().hasClass('tdLeftActive') && !$(this).parent().hasClass('tdRightActive'))
            {
              // Get the div parent
              var divGroupHolder = $(this).parent().parent().parent().parent().parent();
              // Toggle its siblings to show or hide
              $(divGroupHolder).siblings().toggle();
              // Toggle the active class for these links.
              divGroupHolder.find('td:eq(0)').toggleClass('tdLeftActive');
              divGroupHolder.find('td:eq(1)').toggleClass('tdRightActive');
			        checkboxFix();
            }
            return false;
          });
        
      });
    
  // Setup the popup for the help tip.
  $('.HelpTip').click(function() {
        window.open( $(this)[0].getAttribute('href'), "myWindow", "status = 0, height = 150, width = 300, resizable = 1" )
        return false;
      });

	jQuery.extend({
					 show: function(){
						 			this.style.display='block';
						 			},
					 hide: function(){
						 			this.style.display='none';
						 			}
					 });
	//initLoad();
	
}

function lineHeight(){
	$('#HPWhoAreWe').height($('#HPNews').height())
}

function autoHeights(){
	 largestBody	= 0;
	 largestFooter  = 0;
	 largestLowerBody  = 0;
	 largestLowerFooter  = 0;	
	
	$('.bodyTop').css('height','');
	$('.lowerTop').css('height','');
	$('.bodyFooter').css('height','');
	$('.lowerFooter').css('height','');
	
	lineHeight();
}
//
//$('.bodyTop').each(function(){
//debug($(this).height())
//})


function checkboxFix()
{
    $('div.rememberMe label').css('line-height','100%').hide().show().css('display','inline');
    originalFloat = $('div.rememberMe input:first').css('float');
    $('div.rememberMe input').hide().show().css('float',originalFloat);
    $('div.rememberMe span').hide();
    $('fieldset label').hide().show();
    $('fieldset label select').css('position','relative');
}

function normaliseHeights(){
	//	Set minimum and normalise appplication box height
	$('.bodyTop').each(
		function(){
			div = $(this)
			vis = div.parents('.hpApplicationBox').css('display') != 'none';
			min = div.attr('min-height');
		
			
			if(!vis)
				div.parents('.hpApplicationBox').show();
				
			height = div.height();

			if(!vis)
				debug('show next');

			if(div.height() > largestBody)
				largestBody = div.height();

			if(!vis)
				div.parents('.hpApplicationBox').hide();
								

			if(min)
				if(height <min)
					$('.bodyTop').height(min)


		}
	);
	
	if($('.bodyTop').size())
		$('.bodyTop').height((largestBody/11.6)+'em');

	
	//	Set minimum and normalise lower appplication box height
	$('.lowerTop').each(
		function(){
			div = $(this)
			height = div.height();
			min = div.attr('min-height');
		
			if(min)
				if(height <min)
					$('.bodyTop').height(min)
				
			if(div.height() > largestLowerBody)
				largestLowerBody = div.height();
			
		}
	);
	if($('.lowerTop').size())
		$('.lowerTop').height((largestLowerBody/11.6)+'em');

	//	Set minimum footer sizes;
	$('.bodyFooter').each(
		function(){
			div = $(this)
			
			height = div.height();
			target = 'height';

			if(height < 45)
				$('.bodyFooter').css(target,(50/12)+'em');
				
			if(div.height() > largestFooter)
				largestFooter = div.height();
		}
	);
	
	if($('.bodyFooter').size())
		$('.bodyFooter').height((largestFooter/11.5)+'em');
	
	
	//	Set minimum footer size;
	$('.lowerFooter').each(
		function(){
			div = $(this)
			
			height = div.height();
			target = 'height';

			if(height < 45)
				$('.lowerFooter').css(target,(47/12)+'em');
				
			if(div.height() > largestLowerFooter)
				largestLowerFooter = div.height();
			
		}
	);
	if($('.lowerFooter').size())
		$('.lowerFooter').height((Math.round(largestLowerFooter/11.6 * 10)/10)+'em');
	
	lineHeight();
	checkboxFix();
}

function toggleSites(){
		//	Shows and loads site list in the popup box

		var link = $('#siteWideNavigation').find('.change');
		var open = link.attr('name').split('-')[0];
		var close = link.attr('name').split('-')[1];
				
		if(link.attr('isOpen') != 'true'){
			link.attr('isOpen','true');
			link.html(close)
			
			if($('#popup').find('div.countries').size() > 0){
				$('#popup div.body').slideDown({ duration: 400, easing: 'easeInOutCirc'})
				
			}else{
			  // Load the countries.shtml using ajax.
		  
			  $('#popup div.body').load(
					'/_includes/menus/countries.shtml',
					function(){
						$(this).slideDown({ duration: 400, easing: 'easeInOutCirc'})
					}	
				);
		  }
			toggleIFrame();
		}else{
			$('#popup div.body').slideUp({ duration: 400, easing: 'easeInOutCirc'});
			link.attr('isOpen','false');
			link.html(open)
			toggleIFrame();
		}
		
		return false;
}

//	Simple function to change font size
function switchSize(styleSize)
{
		var large = $('#styleSize').attr('name').split('-')[0];
		var small = $('#styleSize').attr('name').split('-')[1];
		
       if(styleSize ==large){
			$('body').css('font-size','12pt').find('#styleSize').html(small)
			styleSize = large;
			autoHeights();
		}else{
			$('body').css('font-size','10pt').find('#styleSize').html(large)
			styleSize = small;
			autoHeights();
			normaliseHeights();
		}			

        createCookie('style', styleSize, 365);
}

// Show an iFrame under our country dropdown list.
function toggleIFrame()
{
  // If the user has IE 6 then we need to make sure that selects don't show through.
  if (jQuery.browser.msie == true && jQuery.browser.version < 7) 
  {
    // If the iFrame exists...
		if ($('#iFrameHolder').length > 0)
		{
		  // remove it.
		  $('#iFrameHolder').remove();
		} else {
      // Create an iframe and place it in the siteWideNavigation div.
      $('#siteWideNavigation').append('<iframe id="iFrameHolder" src="#" tabindex="-1" '
	 					              +'style="display:block; position:absolute;'
						              +'z-index:98;filter:Alpha(Opacity=\'0\');"></iframe>');
  	  
  	  // Set the location and width of the item.
      $('#iFrameHolder').width($('#page').width()).height($(document).height() - $('#siteWideNavigation').height() - 20);
      $('#iFrameHolder').css($('#siteWideNavigation').css('float'), 0).css('top', $('#siteWideNavigation').height());
    }
  }
}

// Prep the setup routine
$(document).ready(setup);
