function AddInputFromCookie(cookiename) {
	document.write("<input type='hidden' name='"+cookiename+"' value='"+GetCookieValue(cookiename)+"'>");
}

function GetSymbol(i) {
	var symbol;
	if (preFixArray[i]=="") {
		symbol=postFixArray[i];
	} else {
		symbol=preFixArray[i];
	}
	return symbol;
}

function CurrencySelect(store, searchstring, page) {
	var cookieCurrency = GetCookieValue("CURRENCY");

	if ( typeof(CartURLIncluded) != "undefined" ) {	
		document.write("<IFRAME src='"+GetCartURL()+"/mall/secure/WriteCartCurrencyCookie.cfm?CURRENCY="+cookieCurrency+"' WIDTH='1' height='1' NAME='currency' MARGINWIDTH='0' MARGINHEIGHT='0' HSPACE='0' VSPACE='0' FRAMEBORDER='0' SCROLLING='NO' ></IFRAME>");
	}
	document.write("<form name='currencyform' action='"+window.location.href+"' method='post'>");
	document.write("<input type='hidden' name='store' value='"+store+"'>");
	if (typeof searchstring != "undefined"){
		document.write("<input type='hidden' name='searchstring' value='"+searchstring+"'>");
		document.write("<input type='hidden' name='page' value='"+page+"'>");
		document.write("<input type='hidden' name='ACTION' value='search'>");
		document.write("<input type='hidden' name='CHECKFIELD' value=''>");
		document.write("<input type='hidden' name='DISPLAYORDER' value='descending'>");
		document.write("<input type='hidden' name='PRICERANGE' value=''>");
		document.write("<input type='hidden' name='SEARCHPRICE' value=''>");
		document.write("<input type='hidden' name='USEVERITY' value='1'>");
		document.write("<input type='hidden' name='SEARCHPRICE' value=''>");
		document.write("<input type='hidden' name='SEARCHPRICE' value=''>");						
	}
	for(var i=0; i<displayArray.length; i++) {
		var symbol = GetSymbol(i);
		document.write("<a href='javascript:ChangeCurrency(\"/\", \""+symbol+"\")'>");
		if (typeof imgArray=="undefined") {
			document.write(displayArray[i]);
		} else {
			if (imgArray[i]=="") { 
				document.write(" "+displayArray[i]+" ");
			} else {
				document.write("<img alt='"+displayArray[i]+"' src='"+imgArray[i]+"'>");
			}
		}
		document.write("</a>");

		/*document.write("<input type='radio' name='CURRENCY' value='"+symbol+"'");
		if (cookieCurrency==symbol) document.write(" checked");
		document.write(" onClick='javascript:ChangeCurrency(this, \"/mall\")'>")
		if (typeof imgArray=="undefined") {
			document.write(displayArray[i]);
		} else {
			if (imgArray[i]=="") { 
				document.write(displayArray[i]);
			} else {
				document.write("<img alt='"+displayArray[i]+"' src='"+imgArray[i]+"'>");
			}
		}*/
	}
	document.write("</form>");
}

function ChangeCurrency(cookiepath, symbol) {
	SetCookie("CURRENCY", symbol, "path", cookiepath);
	document.currencyform.submit();
}

function ConvertPrice(basePreFix, basePrice, basePostFix) {
	/*Used to switch vat on and off on store pages*/
	var vatAdded = GetCookieValue("SDLHASVAT");

	var price = basePrice;
	
	/*Used to switch vat on and off on store pages*/
	if (vatAdded.length>0 && vatAdded == 'Y') 
	{
		var price = price * 1.20;
	}
	
	var preFix = basePreFix;
	var postFix = basePostFix;

	var cookieCurrency = GetCookieValue("CURRENCY");

	if (cookieCurrency.length>0) {
		for(var i=0; i<displayArray.length; i++) {
			var symbol = GetSymbol(i);
			if (cookieCurrency==symbol) {
				price = price * exchangeArray[i];
				preFix = preFixArray[i];
				postFix = postFixArray[i];;
				break;
			}
		}
	}
	
	//unbelievably you can't trust javascript to get the decimal places right.  e.g. 39.995 * 100 = 3999.49999, causing the round to go wrong!
	//lets try using string manipulation instead to get our decimal instead
	string_price = new String(price);
			
	if (string_price.indexOf(".") >= 0) {
		string_price_bits = string_price.split(".");
		
		//get the left side of the decimal place
		new_string_price = string_price_bits[0];
		//get up to 2 bits of the right hand side
		new_string_price = new_string_price + string_price_bits[1].slice(0, 2);
		//pad it if there wasn't enough
		if (string_price_bits[1].length < 2) {
			new_string_price = new_string_price + '0';
		}
		//add decimal place and any remaining numbers
		new_string_price = new_string_price + "." + string_price_bits[1].slice(2);
		
		//now do the rounding and dividing.  I think it should be okay travelling in this direction (i.e. smaller number, if not do the reverse of above!
		price = Math.round(new_string_price) / 100;
	}
	
	var ext="";
	
	if (Math.round(price)==price) {
		ext=".00";
	} else if (Math.round(price*10)==price*10) {
		ext="0";
	}
	if(preFix=='EUR')
	{
		var preFix='&euro;';
	}
	
	if(preFix=='€')
	{
		var preFix='&euro;';
	}
	document.write(preFix + price + ext + postFix);
}

function GetCookieValue(cookiename) {
	var cook = document.cookie;
	/*document.write("<br>"+cook+"<br>");*/
	var result = "";
	var pos = cook.indexOf(cookiename + "=");
	if (pos!=-1) {
		var start = pos + cookiename.length + 1;
		var end = cook.indexOf(";", start);
		if (end==-1) end = cook.length;
		result = unescape(cook.substring(start, end));
	}
	/*document.write("<br>"+name+"="+result+"<br>");*/
	return result;
}

function SetCookie(cookiename, val) {
	var cook = cookiename + "=" + val;
	var foundpath = false;
	
	for (var i=2; i<arguments.length; i+=2) {
		if (i+1<arguments.length) {
			if (arguments[i].toUpperCase() == "PATH")
				foundpath = true;
			if (arguments[i+1]!="") {
				cook += "; "+arguments[i].toUpperCase()+"="+arguments[i+1];
			}
		}
	}
	/* If we didn't specify a path then default to slash to prevent 
	   duplicate cookies getting written to /mall/secure etc
	*/
	
	if (!foundpath)
	{
		cook += "; path=/";
	}
	document.cookie = cook;
	/*debugmsg = "Cookie from currencyjavascript2 SetCookie is " + cook;
	document.write(debugmsg);*/
}

