// for IE
var d = document.all;

// for NN6
var g = document.getElementById;

// for the first two functions below
var lastText;

// for the address string manipulation below
var userChoice = window.location.search.toString().substring(1);

// strips the address box string of all but whatever is after "&&"
var strIndex = userChoice.indexOf("&&");

// decides whether the variable is being passed by the CMS or the user
if (strIndex == -1)
{
	lastText = "default";
}

else
{
	strIndex = strIndex+2;
	lastText = ('t' + userChoice.substring(strIndex));
}

// generic hiding of lumps of text function
// noPara is the number of paragraphs of text there are
function hideText(noPara)
{
	if(isNaN(noPara))
	{
		// do nothing
	}

	else
	{
		if(d)
		{
			for (i=1; i<=noPara; i++)
			{
				document.all['t'+i].style.display="none";
				document.all['default'].style.display="none";
			}
			document.all[lastText].style.display="block";
		}

		else if(g)
		{
			for (i=1; i<=noPara; i++)
			{
				document.getElementById('t'+i).style.display="none";
				document.getElementById('default').style.display="none";
			}
			document.getElementById(lastText).style.display="block";
		}
	}
}

// this variable is used in the below function and is changed value elsewhere in the page.
// it is initialised as NaN purposely, so if pages lack DHTML, the automated printing functions do nothing.
var textNo = "v";

// displays the lumps of text again for printing
function showPrintText(noPara)
{
	if(isNaN(noPara))
	{
		// do nothing
	}

	else
	{
		if(d)
		{
			for (i=1; i<=noPara; i++)
			{
				document.all['t'+i].style.display="block";
			}
			document.all['default'].style.display="none";
		}

		else if(g)
		{
			for (i=1; i<=noPara; i++)
			{
				document.getElementById('t'+i).style.display="block";
			}
			document.getElementById('default').style.display="none";
		}
	}
}


// generic showing of the same lumps of text function
function showText(textNo)
{
	if(d)
	{
		document.all[lastText].style.display="none";
		document.all['t'+textNo].style.display = 'block';
		lastText = 't'+textNo;
	}

	else if(g)
	{
		document.getElementById(lastText).style.display="none";
		document.getElementById('t'+textNo).style.display = 'block';
		lastText = 't'+textNo;
	}
}

// writes the print link to the page
function printButton()
{
	if (window.print)
	{
		document.write('<h1><a href="javascript:showPrintText(textNo);window.print();hideText(textNo);"><img src="/img/products/icon_print.gif" width="21" height="19" border="0" alt="print this page" /></a> <a href="javascript:showPrintText(textNo);window.print();hideText(textNo);">print this page</a></h1>');
	}
}

// writes the print link to the page where there is no DHTML
function printButtonSimple()
{
	if (window.print)
	{
		document.write('<h1><a href="javascript:window.print();"><img src="/img/products/icon_print.gif" width="21" height="19" border="0" alt="print this page" /></a> <a href="javascript:window.print();">print this page</a></h1>');
	}
}

// for browsers where window.print doesn't work
// writes a link which can show all the answers for printing
function show4Print(textNo)
{
	if (window.print == null)
	{
		document.write('<h1><a href="javascript:showPrintText(textNo);"><img src="/img/products/icon_showall.gif" width="21" height="19" border="0" alt="show all answers" /></a> <a href="javascript:showPrintText(textNo);">show all answers</a></h1>');
	}
}

// for the CMS
function windowOpenSized(windowname, width, height)
{
	var thiswin = open('',windowname,'width=' + width + ',height=' + height + ',resizable=yes,scrollbars=yes,left=5,top=5');
	if (thiswin.focus) thiswin.focus();
}

// returns a variable lsBrowser, which is the name of the web client
function getBrowserName()
{
   var lsBrowser = navigator.appName;
   if (lsBrowser.indexOf("Microsoft") >= 0)
   {
      lsBrowser = "MSIE";
   }
   else if (lsBrowser.indexOf("Netscape") >= 0)
   {
      lsBrowser = "NETSCAPE";
   }
   else
   {
      lsBrowser = "UNKNOWN";
   }
   return lsBrowser;
}

// returns a variable browserVersion, which is the version of the browser
function getBrowserVersion()
{
   var findIndex;
   var browserVersion = 0;
   var browser = getBrowserName();

   if (browser == "MSIE")
   {
   browserVersion = navigator.userAgent;
   findIndex = browserVersion.indexOf(browser) + 5;
   browserVersion = parseInt(browserVersion.substring(findIndex,findIndex + 1));
   }
   else
   {
      browserVersion = parseInt(navigator.appVersion.substring(0,1));
   }
   return browserVersion;
}


// this writes a link to a style sheet especially to fix a bug in IE4 and 5
function ieFix()
{
	if(getBrowserName() == "MSIE" && (getBrowserVersion() < 6))
	{
		document.write('<link rel="stylesheet" href="/css/products_ie45.css" media="screen" type="text/css" />');
	}
}

// this is to launch the link on forms with a postcode to address retreval
// it steals the values of a couple of form fields, and sticks them in a querystring.
function findPC() {
	var strPostcode = document.getElementById("xf_arPostcode").value;
	strPostcode = strPostcode.replace(/\s/g,"");
	var strHouseNo = document.getElementById("xf_houseNumber").value;
	if (strPostcode.length>0)
	{
		window.open("/content/applications/slac/contact/address.html?postcode="+strPostcode+"&houseNumber="+strHouseNo,"address","width=630,height=455,scrollbars=no");
		return false
	}
	else
	{
		alert('Please enter your postcode.');
		return false
	}
}

// this is called in the popup page which handles the prefilling
function setAddress(selObj) {
	var selectedAddress = selObj.selectedIndex+1;
	if(opener.document.forms[0].elements['/address1']){

		opener.document.forms[0].elements['/address1'].value = selObj.options[selObj.selectedIndex].text;
	}
	
	if(opener.document.forms[0].elements['/address2']){
	opener.document.forms[0].elements['/address2'].value = document.getElementsByName("addr"+selectedAddress+".2")[0].value;
	}

	
	if(opener.document.forms[0].elements['/address3']){
	opener.document.forms[0].elements['/address3'].value = document.getElementsByName("addr"+selectedAddress+".3")[0].value;
	}

	
	if(opener.document.forms[0].elements['/address4']){
	opener.document.forms[0].elements['/address4'].value = document.getElementsByName("addr"+selectedAddress+".4")[0].value;
	}

	
	if(opener.document.forms[0].elements['/telephone']){

		opener.document.forms[0].elements['/telephone'].value = document.getElementsByName("addr"+selectedAddress+".5")[0].value;
	}

	if(opener){
		opener.focus();
		self.close();
	}
}

// This automatically updates the value of the 'real' postcode field,
// once focus is list from the prefill postcode field.
function updatePostcode(myPC)
{
	document.forms[0].elements['/postcode'].value = myPC;
}