function fillDeliveryAddress() {	
	var $address1 = document.getElementById("address1_billing").value;
	document.getElementById("address1").value = $address1;
	var $address2 = document.getElementById("address2_billing").value;
	document.getElementById("address2").value = $address2;
	var $town = document.getElementById("town_billing").value;
	document.getElementById("town").value = $town;
	var $country_id = document.getElementById("country_id_billing").value;
	document.getElementById("country_id").value = $country_id;
	var $postcode = document.getElementById("postcode_billing").value;
	document.getElementById("postcode").value = $postcode;
}

function clearTextField(field_name) 
{	
	if(document.getElementById(field_name).value == 'Enter a product name')
	{
		document.getElementById(field_name).value = '';
	}
	return true;
}

function checkStockLevel(stock_level)
{
   if(!stock_level)
   {
      alert("Sorry, this product has sold out.");
      return false;
   }
}
// This function runs the funtion (defined as an argument) after the page has loaded (it is used so we dont overwrite additional onLoad functions that could be located in the body tag
function onPageLoad(func) 
{
	var oldonLoad = window.onload; // Set variable to the window.onload event
	// If the window.onload event does not already have a function attached, run checkCommentForm() function (or whatever the argument is)
	if (typeof window.onload != 'function') 
	{
		window.onload = func;
	} 
	else  // If the onload event has functions attached to it, run them first before runnning the checkCommentForm() function
	{
		window.onload = function() 
		{
			oldonLoad();
			func();
		}
	}
}

// Eliminate flicker. This uses the DOM model to build the style for the javascript-hide-me class.
function flickerControl() {
	js = document.createElement('style');
	js.appendChild(document.createTextNode('.javascript-hide-me{display:none;}'));
	js.type = 'text/css';
	document.getElementsByTagName('head').item(0).appendChild(js);
}

// Used on the conference-info page
function dispHandle() 
{
	if (document.getElementById('inside-email-friend').style.display == "none")
	{
		document.getElementById('inside-email-friend').style.display = "";
	}
	else
	{
		document.getElementById('inside-email-friend').style.display = "none";
	}
	// Need this because the below code doesnt workin IE. Checks to see
	// if browser is firefox and if so uses the DOM to display hide class (eliminates flicker)
    if(navigator.userAgent.indexOf("Firefox") != -1) 
	{
		js = document.createElement('style');
		js.appendChild(document.createTextNode('.javascript-hide-me{display:block;}'));
		js.type = 'text/css';
		document.getElementsByTagName('head').item(0).appendChild(js);
	}
}

// Product-info page function which opens new image window for large product shots
function getImageXtraLarge(pExistingImageID, ImageURL, imageWidth, imageHeight)
{
	$imageLink = document.getElementById(pExistingImageID).src; // Get image link using the img id (placeholder)
	
	$imageName=$imageLink.split("/"); // Because the link contains directorys, use split to create an array
	
	$arrayLength=$imageName.length-1; // We only need the last array value so obtain it using length minus 1.
	
	// Open new window with the url created with [PRD_XTRA_LARGE_LINK] and the image name (taken from the created array)
	newwindow=window.open("/library/image_popup/popup.php?url="+ImageURL+$imageName[$arrayLength]+"&clTxt=Click on image to close window", "mywindow","menubar=1,resizable=1"); // ,'name','height='+imageWidth+', width='+imageHeight+', location=yes'

	if (window.focus) 
	{
		newwindow.focus()
	}
}

function checkTerms() { // Check T & C box is ticked before proceding to paypal
	missinginfo = '';
	if (!document.agreement_form.agreement.checked) 
	{
		missinginfo += '\n - You must agree to the Terms and Conditions';
	}
	
	if (missinginfo != '') 
	{
		missinginfo ='Required information is missing: \n' +
		missinginfo + '\n' + '';
		alert(missinginfo);
		/*return false;*/
	}
	else 
	{
		document.checkout.submit();
		return true;
	}

}
function doNothing() {
}

function MM_openBrWindow(theURL,winName,features) { // Used to open new window
  window.open(theURL,winName,features);
}

function CodeEmailAddress($email)
{
	var outString = "";
	var inString = $email;
	for(i=0;i<inString.length;i++){
	outString+="&#"+inString.charCodeAt(i)+";"
	}
	
	var firstPart="<script type=\"text/javascript\">\n";
	firstPart+="<!--\n document.";
	firstPart+="write(\"<li><a href='mailto:";
	firstPart+=outString;
	firstPart+="'>";
	firstPart+=$email;
	//firstPart+='contact us';
	firstPart+="</a></li>\")\n";
	firstPart+="//"
	firstPart+=" -->"
	firstPart+="\n<"
	firstPart+="/script>";
	outString=firstPart
	return outString;
}

function flickerControlDisplay() 
{
	document.getElementById('review-product-form').style.display = "block";
	// Need this because the below code doesnt workin IE. Checks to see
	// if browser is firefox and if so uses the DOM to display hide class (eliminates flicker)
	if(navigator.userAgent.indexOf("Firefox") != -1) 
	{
		js = document.createElement('style');
		js.appendChild(document.createTextNode('#review-product-form{display:block;}'));
		js.type = 'text/css';
		document.getElementsByTagName('head').item(0).appendChild(js);
	}
	Effect.BlindDown('review-product-form');
}

function flickerControlHide() 
{
	document.getElementById('review-product-form').style.display = "none";

	// Need this because the below code doesnt workin IE. Checks to see
	// if browser is firefox and if so uses the DOM to display hide class (eliminates flicker)
	if(navigator.userAgent.indexOf("Firefox") != -1) 
	{
		js = document.createElement('style');
		js.appendChild(document.createTextNode('#review-product-form{display:none;}'));
		js.type = 'text/css';
		document.getElementsByTagName('head').item(0).appendChild(js);
	}
}

function checkEnter(e) // Enable forms to be submitted with 'enter' even if the buttons a graphic
{ //e is event object passed from function invocation
	var characterCode
	if(e && e.which)
	{ //if which property of event object is supported (NN4)
		e = e
		characterCode = e.which //character code is contained in NN4's which property
	}
	else
	{
		e = event
		characterCode = e.keyCode //character code is contained in IE's keyCode property
	}
	
	if(characterCode == 13)
	{ //if generated character code is equal to ascii 13 (if enter key)
		document.forms[0].submit() //submit the form
		return false 
	}
	else
	{
		return true 
	}
}

// for gift message length
function CheckMessageLength() 
{
	var ARLineCount
	var maxLen = 100;                                // the maximum length of message allowed
	var maxWidth = 20;                                // the maximum width of message allowed
	var maxLine = 5;                                // the maximum line number of message allowed
	var MsgLen = document.message_form.ord_card_msg.value.length;
	var strMessage= document.message_form.ord_card_msg.value;
	var strNewMessage = new String()
	strMessage = document.message_form.ord_card_msg.value.replace(/\r\n/g, "\r")
	strMessage = strMessage.replace(/\n\r/g, "\r")
	strMessage = strMessage.replace(/\r/g, "\r")
	strMessage = strMessage.replace(/\n/g, "\r")
	var stringArray = strMessage.replace(/\r/g, " ").split(" ");
	ARLineCount = strMessage.split("\r");
	
	if(MsgLen > maxLen) {
			document.message_form.ord_card_msg.value = document.message_form.ord_card_msg.value.substring(0, maxLen);
			alert("Maximum Card Message length is " + maxLen + " characters");
	}
	
	for (var i=0; i < stringArray.length; i++)
	{
		if(stringArray[i].length >= maxWidth){
			strNewMessage = strMessage.replace(stringArray[i], stringArray[i].substring(0, maxWidth-1))
			document.message_form.ord_card_msg.value = strNewMessage
			alert("In order for your message to print correctly on the card, please limit the word length to " + maxWidth + " characters");
			break;
		}
	}
	
	if(ARLineCount.length > maxLine) {
		alert("Maximum Number of lines in Card Message length is " + maxLine + " lines");
		strNewMessage = ""
		for(var i=0; i<(ARLineCount.length-1); i++) {
			if(i+1 == maxLine) {
				strNewMessage = strNewMessage + ARLineCount[i];
			} else {
				strNewMessage = strNewMessage + ARLineCount[i] + "\n";
			}
		}
		document.message_form.ord_card_msg.value = strNewMessage
	}
	
	 document.message_form.MessageLength.value = document.message_form.ord_card_msg.value.length;
}

fp={
	init:function(){
		// Display left menu after instanstiating the list (displays menu ONLY when its fully built - eliminates flicker)
		var leftMenu = document.getElementById("refine-search");
		if(!leftMenu) 
		{
			return;
		}
		leftMenu.style.display='block';
	},
	
	addEvent: function(elm, evType, fn, useCapture){
		if (elm.addEventListener) 
		{
			elm.addEventListener(evType, fn, useCapture);
			return true;
		} else if (elm.attachEvent) {
			var r = elm.attachEvent('on' + evType, fn);
			return r;
		} else {
			elm['on' + evType] = fn;
		}
	}
}
// start the show.
fp.addEvent(window, 'load', fp.init, false);
