/* 
-----------------------------------------------------------------
	biz2328
	biz2328.js
	
	Unique site functions

	Created 02.10.2007 by DS
	Last Updated: See SVN	
-----------------------------------------------------------------
*/

	function linksToPopups(urlString, popWidth, popHeight, scrollBars)
	{
		var links = $tag('a');
		
		if (links)
		{		
			for (var i=0; i<links.length; i++)
			{
				if (links[i].href.indexOf(urlString) != -1)
				{
					links[i].onclick = function () 
					{		
						popUp(this.href, popWidth, popHeight, scrollBars);						
						return false;		
					}					
				}
			}
		}
	}
	
	function checkIfPopup()
	{
	//	NAM
		if (top.location.href == self.location)
		{
			// if not a pop-up
			popWin = null; // parent
		}		
	}
	
	function changeBackLinkToCloseLink()
	{
		if (window.opener)
		{
			if (document.getElementById('back-link'))
			{
				document.getElementById('back-link').getElementsByTagName('a')[0].onclick = function () 
				{				
					window.close();
				}	
			}
		}
	}	

	
	function insertPopUpCloser()
	{
		if (window.opener)
		{
		//	if launched as a new window
		
			// add 'Close window' fauxlink
			
			var closelink = document.createElement('p');
				closelink.className = 'a closer';
			var closelinkText = document.createTextNode('Close window');
			
			closelink.onclick = function () 
			{				
				window.close();
			}	
			
			closelink.appendChild(closelinkText);
		
			$id('main').appendChild(closelink); // form
			
			// disable home link on h1 (so home doesn't open in popup)
			
			var h1Link = document.getElementsByTagName('a')[0];
			h1Link.title = 'Close window. ';
			
			h1Link.onclick = function () 
			{				
				window.close();
			}	
		}
	}
		
	function popUp(url, popWidth, popHeight, scrollBars)
	{
	//	NAM variation	
		
		day = new Date();
		winName = ('pop' + day.getTime());	
		
		var scrollBarsString = '';
		
		if (scrollBars == 'yes')
		{
			scrollBarsString = ',scrollbars=yes';
		}
		
		var str = ("height=" + popHeight + ",innerHeight=" + popHeight + ",width=" + popWidth + ",innerWidth=" + popWidth + scrollBarsString);
		 
		// close and create a new window EVERY time
		if (popWin != null && !popWin.closed)
		{
			popWin.close();
		}	
		
		popWin = window.open(url, winName, str);;
		popWin.focus();	
		
		return popWin;
	}

	function moveFooter()
	{
		if ((bodyClass != 'popup') && (bodyId != 't-index'))
		{	
		//	Push the footer down to the foot of the page, by setting a minimum/height on the preceding div#main
			var shellHeight = document.getElementById('shell').offsetHeight;
			var browserHeight = getViewportDimension('h');			
			var main = document.getElementById('main');
			
			if (shellHeight < browserHeight)
			{	
				newHeight = (main.offsetHeight + (browserHeight - shellHeight));
				newHeightPx = (newHeight + 'px');
				
				// main height is less than the browser height
				// so stretch the height of main to push the footer down.
				// increasing the height by the amount that the shell is shorter than the browser window			

				if (whichbrowser.isIE6x)
				{						
					main.style.height = newHeightPx;									
				}
				else
				{		
					main.style.minHeight = newHeightPx;					
				}					
			}
			else if (shellHeight > browserHeight)
			{	
				newHeight = (main.offsetHeight - (shellHeight - browserHeight));
				newHeightPx = (newHeight + 'px');				
				
				// main height is stretched to push footer down
				// this makes it too high when the window is resized down
				// so reduce the height by the amount that is now higher than the browser window

				if (newHeight > 0) // else error generated when scaling window right down
				{
					if (whichbrowser.isIE6x)
					{					
						main.style.height = newHeightPx;									
					}
					else
					{							
						main.style.minHeight = newHeightPx;					
					}		
				}
			}
		}
	}

	function enhanceForm() 
	{
	/*	variation on AK's function */
	/*	you may also be able to populate the defaultValue with the associated label's value onload,
		(which might go someway to making up for the fact that said labels have been hidden to match the design),
		but the ssc might prefer that the inputs have a default value in noscript browsers */	
	
		// formTextFields = $class('text'); ~ $class() not supported by ie5
		
		if ($tag('form'))
		{	
			formInputs = $tag('input');
			
			for (var i=0; i<formInputs.length; i++)
			{
				if (formInputs[i].type == 'text')
				{				
					if (formInputs[i].defaultValue)
					{
						(formInputs[i].className ? (formInputs[i].className += ' default-value') : (formInputs[i].className = 'default-value'));
					}
					
					formInputs[i].onfocus = function () 
					{
						if (this.value == this.defaultValue) 
						{
							this.value = '';
							
							thisClass = this.className;
							
							this.className = thisClass.replace(/default-value/, 'focussed');							
							
						}
					}
					formInputs[i].onblur = function () 
					{
						if (!this.value) 
						{
							this.value = this.defaultValue;	
							
							if (this.id != 'qty')
							{
								this.className = thisClass.replace(/focussed/, 'default-value');
							}							
						}
						
						// 31.07.2008: else if removed - so that qty can be updated if field is cleared
						
						if (this.id == 'qty')
						{
							displayQtyPrice(this.value);	
						}
					}
					// 31.07.2008 - displayQtyPrice() if the qty field was populated while the page was still loading
					if (document.getElementById('qty').value != '')
					{
						displayQtyPrice(document.getElementById('qty').value);
					}
				}
				else if ((formInputs[i].type == 'radio') && (formInputs[i].name == 'payment_method'))
				{
					var bankTransferWrap = document.getElementById('bank-transfer-wrap');	
					bankTransferWrap.className = ('inputs nav access'); // hide
					
					formInputs[i].onclick = function () 
					{			
						var bankTransferWrap = document.getElementById('bank-transfer-wrap');						
						//alert(this.id);
					
						if (this.id == 'bank-transfer')
						{						
							bankTransferWrap.className = ('inputs nav'); // show
						}
						else 
						{				
							bankTransferWrap.className = ('inputs nav access'); // hide
						}						
					}
					
				}
			}
			
			formSelects = $tag('select');
			
			if (formSelects)
			{			
				for (var i=0; i<formSelects.length; i++)
				{
					if ((formSelects[i].selected) && (formSelects[i].value == ''))
					{
						// default option is selected					
						(formSelects[i].parentNode.className ? (formSelects[i].parentNode.className += ' default-value') : (formSelects[i].parentNode.className = 'default-value'));					
					}
					
					formSelects[i].parentNode.onfocus = function () 
					{						
						if (this.value == '') 
						{					
							var thisClass = this.className;
						
							this.className = thisClass.replace(/default-value/, 'focussed');													
						}
					}
					formSelects[i].parentNode.onblur = function () 
					{
						if (this.value == '')
						{		
							var thisClass = this.className;					
						
							this.className = thisClass.replace(/focussed/, 'default-value');								
						}
					}				
				}
			}
		}
	}
	
	function displayQtyPrice(qty)
	{	
		if ((qty != '') && (!isNaN(qty)))
		{		
			var totalPrice = roundNumber((qty * (pricePer + postageAndPackaging)), 2);		
		
			document.getElementById('price').innerHTML = ('<abbr title="multiplied by">x</abbr> ($' + pricePer + ' + ' + '$' + postageAndPackaging + ' <abbr title="Postage &amp; Packaging.  ">p&amp;p</abbr>)' + '<span class="access"> = </span><strong>Total $' + parseNumberToCurrencyFormat(totalPrice + "") + '</strong>');		
		}
		else
		{
			document.getElementById('price').innerHTML = '';
		}
	}
	
	function roundNumber(num, dec) 
	{
	//	http://forums.devarticles.com/showpost.php?p=71368&postcount=2.
		var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
		return result;
	}	
	
	var wd;
	
	function parseNumberToCurrencyFormat(amt)
	{
		/*
			By Website Abstraction (www.wsabstract.com)
			and Java-scripts.net (www.java-scripts.net)
			Reformatted and edited by DS
		*/				
		
		if (amt.charAt(0) == "$")
		{
			return;
		}
		
		wd = "w";
		
		var tempnum = amt;
		for (i=0; i<tempnum.length; i++)
		{
			if (tempnum.charAt(i) == ".")
			{
				wd="d";
				break;
			}
		}
		if (wd == "w")
		{
			amt2 = tempnum + ".00";
		}
		else
		{		
			if (tempnum.charAt(tempnum.length-2) == ".")
			{
				amt2 = tempnum + "0";
			}
			else
			{
				tempnum = Math.round(tempnum*100)/100;
				amt2 = tempnum;
			}
		}
		
		return amt2;
	}

    function initBankTransferWrap() 
	{
        var bankTransferWrap = document.getElementById('bank-transfer-wrap');
        var bankTransferCheckbox = document.getElementById('bank-transfer');
    	if ( bankTransferCheckbox.checked ) 
		{				
			bankTransferWrap.className = bankTransferWrap.className.replace(/ access/, '')
		}
		else 
		{				
			bankTransferWrap.className = (bankTransferWrap.className + ' access');
		}
	}
