// Prices: Array 0=EUR, 1=DKK
// DKK(Danske kroner) 208
// EUR(Euro) 978
//-----------------------------------------------------------------------------
var _currencyStr		= new Array('EUR ', 'DKK ');
var _currencyStrLong	= new Array('Euro', 'Danish Kroner');
var _currencyIDs		= new Array(  978 , 208);

//-----------------------------------------------------------------------------
// products
// 0.Start-up fee
// 1.TurnToolBox Full.
// 2.TurnToolBox Education.
var _originalNumProducts=3;
var _numProducts	= _originalNumProducts;
var _productStr		= new Array('TurnToolBox2008 Full', 'Upgrade V3 to 2008', 'TurnToolBox2008 Educational');
var _elemNameStr	= new Array('tntfull' , 'v3upgrade' , 'tntedu' );
var _price0			= new Array(798       , 399         , 266      );
var _price1			= new Array(6000      , 2998        , 1980     );
var _quantity		= new Array(0         , 0        , 0           );
var _needPW			= new Array(0         , 0        , 0           );
var _tableRow		= new Array(-1        , -1       , -1          );

var _EU				= new Array('Austria','Belgium','Croatia','Cyprus','Czech Republic','Denmark','Espana','Estonia','Faroe Islands','Finland','France','Germany','Greece','Greenland','Hungary','Ireland','Italy','Latvia','Lithuania','Luxembourg','Malta','Poland','Portugal','Romania','Scotland','Slovakia','Slovenia','Spain','Sweden','The Netherlands','United Kingdom','Wales');

// Notice, update resellers in reseller document too!
var _resellerList	= new Array(
							new Array('South Korea'			,	'Japan'				,	'USA'					,	'United Kingdom'	,	'Sweden'				,	'Finland'			,	'France'		,	'Portugal'				,	'Bangladesh'			,	'India'				,	'Turkey', 'Germany'),	
							new Array('Dreamscape Inc'		,	'Systec Inc'		,	'Vismasters'			,	'TurnTool UK'		,	'Creative Tools AB'		,	'3D Render Oy'		,	'New3s'			,	'INTERESTUDIO'			,	'Win-Win Infosys Ltd'	,	'Rahul Commerce'	,	'IKON Teknoloji'),
							new Array('www.dreamscape.co.kr',	'www.kamnet.co.jp'	,	'www.vismasters.com'	,	'www.turntool.co.uk',	'www.creativetools.se'	,	'www.3drender.fi'	,	'www.new3s.com'	,	'www.interestudio.com'	,	'www.ww-bd.com'			,	'www.rahulcom.com'	,	'www.ikonteknoloji.com'	)	);

// other purchase
//var _bEnableOther	= false;
var _bEnableOther	= true;
var _sOtherProduct = "";
var _sOtherPrice = 0;

var _nErrorCode = -1;
var _nMomsFactor = 0.25;

// Credit card
var _sMerchantNumber = "2963779";

// Money transfer
var _sAccountDetailsHTML = "";
_sAccountDetailsHTML += 'Sydbank<br>';
_sAccountDetailsHTML += 'Postbox 169<br>';
_sAccountDetailsHTML += 'DK 6200<br>';
_sAccountDetailsHTML += 'Aabenraa<br><br>';
_sAccountDetailsHTML += 'SWIFT code: SYBKDK22<br>';
_sAccountDetailsHTML += 'IBAN number: DK6171100000128173<br><br>';

//-----------------------------------------------------------------------------
// Order Details
//-----------------------------------------------------------------------------

var _nAmount = 0;
var _nPayMethod = 0;
var _nCurrency = 0;
var _bLockCurrency = false;

var _sCompany = "";
var _sCvr = "";
var _sContactPerson = "";
var _sAddress = "";
var _sCountry = "";
var _sPhone = "";
var _sEmail = "";
var _sNotes = "";
var _sPwd = "";
var _bNewsletter = false;

var _sOrderID = "";
var _sErrorMsg = "";
var _nStep = 0;

//-----------------------------------------------------------------------------
// Secure Tunnel
//-----------------------------------------------------------------------------
function secureTunnel(url)
{
	return 'https://pay.dandomain.dk/securetunnel.asp?MerchantNumber=' + _sMerchantNumber + '&TunnelURL=' + url;
}

//-----------------------------------------------------------------------------
// Connection
//-----------------------------------------------------------------------------
var _nUrlMaxLength = 2000;
var _bTest = false;
var _sThisUrl = "http://www.turntool.com/buy-turntool.html?";
var _sThisSecureUrl = secureTunnel("http://www.turntool.com/buy-turntool.html?");

// var _bSecure = true;
var _bSecure = false;
if( location.protocol == "https:" )
{
	_bSecure = true;
}

//-----------------------------------------------------------------------------
// Platform
//-----------------------------------------------------------------------------
var ie4 = (document.all && !document.getElementById);
var ie5 = (document.all && document.getElementById);
var ns6 = (!document.all && document.getElementById);

function element(id)
{
	// W3C - Explorer 5+ and Netscape 6+
	if(ie5 || ns6)
	{
		return document.getElementById(id);
	}
	// Explorer 4
	else if(ie4)
	{
		return document.all[id];
	}
	return null;
}

//-----------------------------------------------------------------------------
function getTags(tagname)
{
	// W3C - Explorer 5+ and Netscape 6+
	if(ie5 || ns6)
	{
		return document.getElementsByTagName(tagname);
	}
	// Explorer 4
	else if(ie4)
	{
		return document.all.tags(tagname);
	}
	return null;
}

//-----------------------------------------------------------------------------
// General
//-----------------------------------------------------------------------------
function isEmail(strEmail)
{
	var bEmailValid = false;
	if(strEmail != "")
	{
		// check email
		var strEmailArray = strEmail.split("@");
		if(strEmailArray.length==2)
		{
			// is name ok?
			var strUser = strEmailArray[0];
			if( strUser.length != 0 )
			{
				// is addr part ok
				var strAddr = strEmailArray[1];
				if( (strAddr.length > 0) && strAddr.indexOf(".")!=-1 )
				{
					// 
					var legalChars = ".-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
					bEmailValid = true;
					var i = 0;
					for( i=0; i<strAddr.length; i++ )
					{
						if( legalChars.indexOf( strAddr.charAt(i) ) == -1 )
						{
							bEmailValid = false; // illegal character
							break;
						}
					}
				}
			}
		}
	}
	return bEmailValid;
}


//-----------------------------------------------------------------------------
var symbols = "%.*/+-@_0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

function canGarble(str)
{
	var i = 0;
	for( i=0; i<str.length; i++ )
	{
		if( symbols.indexOf( str.charAt(i) ) == -1 )
		{
			return false; // illegal character
		}
	}
	return true;
}

function garbleString(str)
{
	var strNew = "";
	var i = 0;
	for( i = 0; i < str.length; i++ )
		strNew += symbols.charAt( (symbols.length-1) - symbols.indexOf(str.charAt(i)));
	return strNew;
}

//-----------------------------------------------------------------------------
function replaceChar( str, ch, s2 )
{
	var newStr = "";
	var i = 0;
	for( i=0; i<str.length; i++ )
	{
		var c = str.charAt(i);
		if( c==ch )
		{
			newStr += s2;
		}
		else
		{
			newStr += c;
		}
	}
	return newStr;
} 

//-----------------------------------------------------------------------------
function decode(str)
{
	var outstr = "";

	var subs = unescape(str).split(" ");
	var lngth = subs.length-1;
	if(lngth>=0)
	{
		var i = 0;
		for( i=0; i<lngth; i++ )
		{
			outstr += replaceChar( subs[i], "+", " " );
			outstr += "+";
		}
		outstr += replaceChar( subs[lngth], "+", " " );
		return outstr;
	}
	return str;
}

function encode(str)
{
	var outstr = "";

	var subs = str.split("+");
	var lngth = subs.length-1;
	if(lngth>=0)
	{
		var i = 0;
		for( i=0; i<lngth; i++ )
		{
			outstr += replaceChar( subs[i], " ", "+" );
			outstr += " ";
		}
		outstr += replaceChar( subs[lngth], " ", "+" );
		return escape(outstr);
	}
	return str;
}

//-----------------------------------------------------------------------------
function niceNumberString(n)
{
	var num = 100 * n;
	Math.round(num);
	num /= 100;
	return num.toString(); 
}

//-----------------------------------------------------------------------------
var argArray = new Array();
if(argArray)
{
	// init arg array
	var baseArray = document.URL.split("?");
	if(baseArray.length>1)
	{
		argArray = baseArray[baseArray.length-1].split("&");
	}
}

function getArgumentValue(key)
{
	var i = 0;
	while( i<argArray.length )
	{
		var pairArray = argArray[i].split("=");
		if( (pairArray.length==2) && (pairArray[0]==key) )
		{
			return pairArray[1];
		}
		i++;
	}
	return "";
}

//-----------------------------------------------------------------------------
function allArgsToSearch()
{
	var sSearch = "";
	var i = 0;
	if(_sOrderID != "")
		sSearch += "orderid=" + encode(_sOrderID);
	if(_sErrorMsg != "")
		sSearch += "&errmsg=" + encode(_sErrorMsg);
	if(_nStep > 0)
		sSearch += "&step=" + _nStep;

	if(_numProducts > _originalNumProducts)
	{
		sSearch += "&nprods=" + _numProducts;
		// if  additional products write details
		for( i = _originalNumProducts; i<_numProducts; i++ )
		{
			sSearch += "&" + _elemNameStr[i] + "str=" + encode(_productStr[i]);
			if(_price0[i] > 0)
				sSearch += "&" + _elemNameStr[i] + "price0=" + _price0[i];
			if(_price1[i] > 0)
				sSearch += "&" + _elemNameStr[i] + "price1=" + _price1[i];
		}
	}

	for( i = 0; i<_numProducts; i++ )
	{
		if(_quantity[i]>0)
		{
			sSearch += "&" + _elemNameStr[i] + "=" + _quantity[i];
		}
	}

	if(_nAmount > 0)
		sSearch += "&tamount=" + _nAmount;
	if(_nPayMethod > 0)
		sSearch += "&paymethod=" + _nPayMethod;
	if(_nCurrency != 0)
		sSearch += "&currency=" + _nCurrency;
	if(_sCompany != "")
		sSearch += "&company=" + encode(_sCompany);
	if(_sCvr != "")
		sSearch += "&cvr=" + encode(_sCvr);
	if(_sContactPerson != "")
		sSearch += "&contactperson=" + encode(_sContactPerson);
	if(_sAddress != "")
		sSearch += "&address=" + encode(_sAddress);
	if(_sCountry != "")
		sSearch += "&country=" + encode(_sCountry);
	if(_sPhone != "")
		sSearch += "&phone=" + encode(_sPhone);
	if(_sEmail != "")
		sSearch += "&email=" + encode(_sEmail);
	if(_sNotes != "")
		sSearch += "&notes=" + encode(_sNotes);
	if(_sPwd != "")
		sSearch += "&pwd=" + garbleString(escape(_sPwd));
	if(_bNewsletter != false)
		sSearch += "&newsletter=" + _bNewsletter;
	if(_bLockCurrency != false)
		sSearch += "&lockcurrency=" + _bLockCurrency;
	if(_bTest != false)
		sSearch += "&test=" + _bTest;
	return sSearch;	
}

//-----------------------------------------------------------------------------
function allArgsFromSearch()
{
	var i = 0;
	_sOrderID = decode(getArgumentValue("orderid"));
	_sErrorMsg = decode(getArgumentValue("errmsg"));
	_nStep = parseInt(getArgumentValue("step"));
	if(isNaN(_nStep))
		_nStep = 0;

	_numProducts = parseInt(getArgumentValue("nprods"));
	if( isNaN(_numProducts) || _numProducts < _originalNumProducts)
		_numProducts = _originalNumProducts;

	if(_numProducts > _originalNumProducts)
	{
		// if  additional products read details
		for( i = _originalNumProducts; i<_numProducts; i++ )
		{
			_elemNameStr[i] = "prod" + i;
			_productStr[i] = decode(getArgumentValue(_elemNameStr[i]+"str"));
			_price0[i] = parseFloat(getArgumentValue(_elemNameStr[i] + "price0"));
			if(isNaN(_price0[i]))
				_price0[i] = 0;
			_price1[i] = parseFloat(getArgumentValue(_elemNameStr[i] + "price1"));
			if(isNaN(_price1[i]))
				_price1[i] = 0;
		}
	}

	for( i = 0; i<_numProducts; i++ )
	{
		_quantity[i] = parseInt(getArgumentValue(_elemNameStr[i]));
		if(isNaN(_quantity[i]))
			_quantity[i] = 0;
	}

	_nAmount = parseFloat(decode(getArgumentValue("tamount")));
	if(isNaN(_nAmount))
		_nAmount = 0;
	_nPayMethod = parseInt(getArgumentValue("paymethod"));
	if(isNaN(_nPayMethod))
		_nPayMethod = 0;
	_nCurrency = parseInt(getArgumentValue("currency"));
	if(isNaN(_nCurrency))
		_nCurrency = 0;
	_nErrorCode = parseInt(getArgumentValue("errorcode"));
	if(isNaN(_nErrorCode))
		_nErrorCode = -1;

	_sCompany = decode(getArgumentValue("company"));
	_sCvr = decode(getArgumentValue("cvr"));
	_sContactPerson = decode(getArgumentValue("contactperson"));
	_sAddress = decode(getArgumentValue("address"));
	_sCountry = decode(getArgumentValue("country"));
	_sPhone = decode(getArgumentValue("phone"));
	_sEmail = decode(getArgumentValue("email"));
	_sNotes = decode(getArgumentValue("notes"));
	_sPwd = decode(garbleString(getArgumentValue("pwd")));
	_bNewsletter = getArgumentValue("newsletter");
	_bLockCurrency = getArgumentValue("lockcurrency");
	_bTest = getArgumentValue("test");
}

//-----------------------------------------------------------------------------
// Pricing
//-----------------------------------------------------------------------------
function getProductPrice( product, currency )
{
	switch(currency)
	{
	case 0:
		return _price0[product];
	case 1:
		return _price1[product];
	}
}

function getPrice( quantities, currency )
{
	var prices;
	var totals = new Array(_numProducts);
	switch(currency)
	{
	case 0:
		prices = _price0;
		break;
	case 1:
		prices = _price1;
		break;
	}

	var i = 0;
	for( i = 0; i<_numProducts; i++ )
	{
		totals[i] = prices[i] * quantities[i];
	}
	return totals;
}

//-----------------------------------------------------------------------------
function isEUCountry( sCountry )
{
	var i=0;
	for( i=0; i<_EU.length; i++ )
	{
		if(_EU[i]==sCountry)
		{
			return true;
		}
	}
	return false;
}
//-----------------------------------------------------------------------------
function isResellerCountry( sCountry )
{
	var i=0;
	for( i=0; i<_resellerList[0].length; i++ )
	{
		if(_resellerList[0][i]==sCountry)
		{
			return true;
		}
	}
	return false;
}

//-----------------------------------------------------------------------------
function matchVATPattern(str, pat)
{
	// 9: any digit
	// X: any character
	if(str.length != pat.length)
		return false;

	var sDigits = "0123456789";	
	var i=0;
	for( i=0; i<str.length; i++)
	{
		var patc = pat.charAt(i);
		var strc = str.charAt(i);
		
		if(patc=='9')
		{
			if(sDigits.indexOf(strc)==-1)
				return false;
		}
		else if(patc!='X')
		{
			if(patc!=strc)
				return false;
		}
	}
	return true;
}

//-----------------------------------------------------------------------------
function isVATCodeValid()
{
	// kill whitespace and fill chars
	var i = 0;
	var newCvr = "";
	for( i=0; i<_sCvr.length; i++ )
	{
		var c = _sCvr.charAt(i);
		if( c!=' ' && c!='\n' && c!='\t' && c!='-' )
		{
			newCvr += c;
		}
	}

	if(_sCountry=='Belgium')
	{
		//Belgium 			BE 999999999 9 cifre
		return matchVATPattern(newCvr,"999999999") || matchVATPattern(newCvr,"BE999999999") || matchVATPattern(newCvr,"9999999999") || matchVATPattern(newCvr,"BE9999999999");
	}
	else if(_sCountry=='Germany')
	{
		//Germany 			DE 999999999 9 cifre
		return matchVATPattern(newCvr,"999999999") || matchVATPattern(newCvr,"DE999999999");
	}
	else if(_sCountry=='Greece')
	{
		//Greece	 		EL 999999999 9 cifre
		return matchVATPattern(newCvr,"999999999") || matchVATPattern(newCvr,"EL999999999");
	}
	else if(_sCountry=='Portugal')
	{
		//Portugal 			PT 999999999 9 cifre
		return matchVATPattern(newCvr,"999999999") || matchVATPattern(newCvr,"PT999999999");
	}
	else if(_sCountry=='Denmark')
	{
		//Denmark 			DK 99999999 8 cifre
		return matchVATPattern(newCvr,"99999999") || matchVATPattern(newCvr,"DK99999999");
	}
	else if(_sCountry=='Finland')
	{
		//Finland 			FI 99999999 8 cifre
		return matchVATPattern(newCvr,"99999999") || matchVATPattern(newCvr,"FI99999999");
	}
	else if(_sCountry=='Luxembourg')
	{
		//Luxembourg 		LU 99999999 8 cifre
		return matchVATPattern(newCvr,"99999999") || matchVATPattern(newCvr,"LU99999999");
	}
	else if(_sCountry=='Spain')
	{
		//Spain 			ES X9999999X 9 tegn
		return matchVATPattern(newCvr,"X9999999X") || matchVATPattern(newCvr,"ESX9999999X");
	}
	else if(_sCountry=='France')
	{
		//France 			FR XX999999999 11 tegn
		return matchVATPattern(newCvr,"XX999999999") || matchVATPattern(newCvr,"FRXX999999999");
	}
	else if(_sCountry=='Ireland')
	{
		//Ireland 			IE 9X99999X 8 tegn
		return matchVATPattern(newCvr,"9X99999X") || matchVATPattern(newCvr,"IE9X99999X");
	}
	else if(_sCountry=='Italy')
	{
		//Italy 			IT 99999999999 11 cifre
		return matchVATPattern(newCvr,"99999999999") || matchVATPattern(newCvr,"IT99999999999");
	}
	else if(_sCountry=='The Netherlands')
	{
		//The Netherlands	NL 999999999B99 12 tegn
		return matchVATPattern(newCvr,"999999999B99") || matchVATPattern(newCvr,"NL999999999B99");
	}
	else if(_sCountry=='Austria')
	{
		//Austria 			AT U99999999 9 tegn
		return matchVATPattern(newCvr,"U99999999") || matchVATPattern(newCvr,"ATU99999999");
	}
	else if(_sCountry=='Sweden')
	{
		//Sweden 			SE 999999999999 12 cifre
		return matchVATPattern(newCvr,"999999999999") || matchVATPattern(newCvr,"SE999999999999");
	}
	else if(_sCountry=='Cyprus')
	{
		//Cyprus 			CY 99999999X 8 cifre + 1 tegn
		return matchVATPattern(newCvr,"99999999X") || matchVATPattern(newCvr,"CY99999999X");
	}
	else if(_sCountry=='Czech Republic')
	{
		//Czech Republic 	CZ 99999999 8 cifre
		//				 	CZ 999999999 9 cifre
		//				 	CZ 9999999999 10 cifre
		return matchVATPattern(newCvr,"99999999") || matchVATPattern(newCvr,"CZ99999999") || matchVATPattern(newCvr,"999999999") || matchVATPattern(newCvr,"CZ999999999") || matchVATPattern(newCvr,"9999999999") || matchVATPattern(newCvr,"CZ9999999999") ;
	}
	else if(_sCountry=='Estonia')
	{
		//Estonia 			EE 999999999 9 cifre
		return matchVATPattern(newCvr,"999999999") || matchVATPattern(newCvr,"EE999999999");
	}
	else if(_sCountry=='Hungary')
	{
		//Hungary 			HU 99999999 8 cifre
		return matchVATPattern(newCvr,"99999999") || matchVATPattern(newCvr,"HU99999999");
	}
	else if(_sCountry=='Lithuania')
	{
		//Lithuania 		LT 999999999 9 cifre
		//			 		LT 999999999999 12 cifre
		
		return matchVATPattern(newCvr,"999999999") || matchVATPattern(newCvr,"LT999999999") || matchVATPattern(newCvr,"999999999999") || matchVATPattern(newCvr,"LT999999999999");
	}
	else if(_sCountry=='Latvia')
	{
		//Latvia 			LV 99999999999 11 cifre
		return matchVATPattern(newCvr,"99999999999") || matchVATPattern(newCvr,"LV99999999999");
	}
	else if(_sCountry=='Malta')
	{
		//Malta 			MT 99999999 8 cifre
		return matchVATPattern(newCvr,"99999999") || matchVATPattern(newCvr,"MT99999999");
	}
	else if(_sCountry=='Poland')
	{
		//Poland 			PL 9999999999 10 cifre
		return matchVATPattern(newCvr,"9999999999") || matchVATPattern(newCvr,"PL9999999999");
	}
	else if(_sCountry=='Slovenia')
	{
		//Slovenia 			SI 99999999 8 cifre
		return matchVATPattern(newCvr,"99999999") || matchVATPattern(newCvr,"SI99999999");
	}
	else if(_sCountry=='Slovakia')
	{
		//Slovakia 			SK 9999999999 10 cifre
		return matchVATPattern(newCvr,"9999999999") || matchVATPattern(newCvr,"SK9999999999");
	}
	else if(_sCountry=='United Kingdom')
	{
		//United Kingdom	GB 999999999 9 cifre
		//					GB 999999999999 12 cifre
		//					GB GD999 5 tegn
		//					GB HA999 5 tegn
		return matchVATPattern(newCvr,"999999999") || matchVATPattern(newCvr,"GB999999999") || matchVATPattern(newCvr,"999999999999") || matchVATPattern(newCvr,"GB999999999999") || matchVATPattern(newCvr,"GD999") || matchVATPattern(newCvr,"GBGD999") || matchVATPattern(newCvr,"HA999") || matchVATPattern(newCvr,"GBHA999");
	}
	return false;
}

//-----------------------------------------------------------------------------
function getVAT()
{
	if(_sCountry=="Denmark")
	{
		return _nMomsFactor;
	}
	else if( isEUCountry(_sCountry) )
	{
		if(!isVATCodeValid())
		{
			return _nMomsFactor;
		}
	}
	return 0;
}

//-----------------------------------------------------------------------------
// Section visibility
//-----------------------------------------------------------------------------
var showNoneFlag						= 0;
var detailsDivFlag						= 1;
var customerDetailsDivFlag				= 2;
var productSelectionDivFlag				= 4;
var otherProductDivFlag					= 8;
var customerInfoDivFlag					= 16;
//var passwordsectionFlag					= 32;
var loadingDivFlag						= 64;
var paymentCreditCardDivFlag			= 128;
var paymentMoneyTransferDivFlag			= 256;
var paymentMoneyTransferCompleteDivFlag	= 512;
var paymentCreditCardCompleteDivFlag	= 1024;
var pageHeaderDivFlag					= 2048;
var pageFooterDivFlag					= 4096;
var resellerDivFlag						= 8192;

var currentShowFlags = 0;

function showSections( flags )
{
	element("detailsDiv").style.display							= flags & detailsDivFlag ? "block" : "none";
	element("customerDetailsDiv").style.display					= flags & customerDetailsDivFlag ? "block" : "none";
	element("productSelectionDiv").style.display				= flags & productSelectionDivFlag ? "block" : "none";
	element("otherProductDiv").style.display					= flags & otherProductDivFlag ? "block" : "none";
	element("customerInfoDiv").style.display					= flags & customerInfoDivFlag ? "block" : "none";
//	element("passwordsection").style.display					= flags & passwordsectionFlag ? "block" : "none";
	element("loadingDiv").style.display							= flags & loadingDivFlag ? "block" : "none";
	element("paymentCreditCardDiv").style.display				= flags & paymentCreditCardDivFlag ? "block" : "none";
	element("paymentMoneyTransferDiv").style.display 			= flags & paymentMoneyTransferDivFlag ? "block" : "none";
	element("paymentMoneyTransferCompleteDiv").style.display	= flags & paymentMoneyTransferCompleteDivFlag ? "block" : "none";
	element("paymentCreditCardCompleteDiv").style.display		= flags & paymentCreditCardCompleteDivFlag ? "block" : "none";
	element("pageHeaderDiv").style.display						= flags & pageHeaderDivFlag ? "block" : "none";
	element("pageFooterDiv").style.display						= flags & pageFooterDivFlag ? "block" : "none";
	element("resellerDiv").style.display						= flags & resellerDivFlag ? "block" : "none";
	currentShowFlags = flags;
}

//-----------------------------------------------------------------------------
// Update Functions
//-----------------------------------------------------------------------------
function updateOrderDetails()
{
	// get prices
	_nAmount = 0;
	var addedTotals = getPrice(_quantity,_nCurrency);

	var newRow;
	var tableBody = element("orderDetailsTableBody");

	while(tableBody.rows.length>0)
	{
		tableBody.deleteRow(0);
	}

	var i = 0;
	if (_quantity[1]>0)
	{
		//no more startup fee
		//_quantity[0] = 1;
	}
	for( i = 0; i<_numProducts; i++ )
	{
		if(_quantity[i]>0)
		{
			newRow = tableBody.insertRow(-1);
			if(newRow)
			{
				var newCell;
				newCell = newRow.insertCell(0);
				newCell.innerHTML = "<span class=\"broed\">" + _productStr[i] + "</span>";
				newCell.align = "left";
				newCell = newRow.insertCell(1);
				newCell.innerHTML = "<span class=\"broed\">" + _quantity[i] + "</span>";
				newCell.align = "right";
				newCell = newRow.insertCell(2);
				newCell.innerHTML = "<span class=\"broed\">" + niceNumberString(addedTotals[i]) + "</span>";
				newCell.align = "right";
			}

			_nAmount += addedTotals[i];
		}
	}

	tableBody.insertRow(-1).insertCell(0).innerHTML = "&nbsp;";
	var newCell;

	var momsFactor = getVAT();
	var nMoms = momsFactor * _nAmount;

	newRow = tableBody.insertRow(-1);
	newCell = newRow.insertCell(0);
	newCell.innerHTML = "<span class=\"broed\">Total (excl VAT)</span>";
	newCell.align = "left";
	newRow.insertCell(1);
	newCell = newRow.insertCell(2);
	newCell.innerHTML = "<span class=\"broed\">" + niceNumberString(_nAmount) + "</span>";
	newCell.align = "right";

	newRow = tableBody.insertRow(-1);
	newCell = newRow.insertCell(0);
	newCell.innerHTML = "<span class=\"broed\">VAT/MOMS(" + (momsFactor*100) + "%)</span>";
	newCell.align = "left";
	newRow.insertCell(1);
	newCell = newRow.insertCell(2);
	newCell.innerHTML = "<span class=\"broed\">" + niceNumberString(nMoms) + "</span>";
	newCell.align = "right";

	_nAmount += nMoms;

	newRow = tableBody.insertRow(-1);
	newCell = newRow.insertCell(0);
	newCell.innerHTML = "<span class=\"broed\"><b>Total (incl VAT)</b></span>";
	newCell.align = "left";
	newRow.insertCell(1);
	newCell = newRow.insertCell(2);
	newCell.innerHTML = "<span class=\"broed\"><b>" + niceNumberString(_nAmount) + "</b></span>";
	newCell.align = "right";

	return _nAmount;
}

//-----------------------------------------------------------------------------
function updateCustomerDetails()
{
	var sCustomerText = '';

	if(_sCompany!="")
	{
		sCustomerText += _sCompany + 'Att. ';
	}
	if(_sContactPerson!="")
	{
		sCustomerText += _sContactPerson + '<br>';
	}
	if(_sCvr!="")
	{
		sCustomerText += 'VAT Number: ' + _sCvr + '<br>';
	}
	if(_sAddress!="")
	{
		sCustomerText += replaceChar(_sAddress,"\n","<br>") + "<br>";
	}
	if(_sCountry!="")
	{
		sCustomerText += _sCountry + '<br><br>';
	}
	if(_sEmail!="")
	{
		sCustomerText += 'E-mail: ' + _sEmail + '<br>';
	}
	if(_sPhone!="")
	{
		sCustomerText += 'Phone: ' + _sPhone + '<br>';
	}
	if(_bNewsletter)
	{
		sCustomerText += '<br>Wishes to subscribe to the TurnTool newsletter<br><br>';
	}
	else
	{
		sCustomerText += '<br>Does not wish to subscribe to the TurnTool newsletter<br><br>';
	}
	if(_sNotes!="")
	{
		sCustomerText += 'Notes:<br>' + replaceChar(_sNotes,"\n","<br>") + '<br><br>';
	}

	element("customerdetails").innerHTML = "<p class=\"bodytext\">" + sCustomerText + "</p>";
}

//-----------------------------------------------------------------------------
function AssembleOrder()
{
	// assemble order text
	var i = 0;
	var sIndent = "                                 ";
	var sLine = "------------------------------------------------------";
	var sOrderTxt = "\n";
	sOrderTxt += sLine + "\n";
	sOrderTxt += "OrderID:\n";
	sOrderTxt += sIndent + _sOrderID + "\n";
	sOrderTxt += sLine + "\n";


	if(_sCvr!="")
	{
		sOrderTxt += "VAT Registration Number:\n";
		sOrderTxt += sIndent + _sCvr + "\n";
	}

	if(_sCompany!="")
	{
		sOrderTxt += "Company:\n";
		sOrderTxt += sIndent + _sCompany + "\n";
		sOrderTxt += "Contact Person:\n";
	}
	else
	{
		sOrderTxt += "Name:\n";
	}
	sOrderTxt += sIndent + _sContactPerson + "\n";
	sOrderTxt += "Address:\n";
	sOrderTxt += sIndent;

//	sOrderTxt += _sAddress.replace(/\n/g, "\n" + sIndent) + "\n";
	var i = 0;
	var sTempAddress = "";
	for( i=0; i<_sAddress.length; i++ )
	{
		var c = _sAddress.charAt(i);
		sTempAddress += c;
		if(c=="\n")
		{
			sTempAddress += sIndent;
		}
	}
	sTempAddress += "\n";
	sOrderTxt += sTempAddress;

	sOrderTxt += "Country:\n";
	sOrderTxt += sIndent + _sCountry + "\n";

	sOrderTxt += "E-mail:\n";
	sOrderTxt += sIndent + _sEmail + "\n";

	if(_sPhone!="")
	{
		sOrderTxt += "Phone:\n";
		sOrderTxt += sIndent + _sPhone + "\n";
	}
	
	sOrderTxt += sLine + "\nOrder Details:\n";

	var addedTotals = getPrice(_quantity,_nCurrency);
	var grandTotal = 0;
	var needPW = false;
	for( i = 0; i<_numProducts; i++ )
	{
		if(_quantity[i]>0)
		{
			sOrderTxt += sIndent + _quantity[i] + " " + _productStr[i];
			if(_quantity[i]>1)
				sOrderTxt += "s (" + _currencyStr[_nCurrency] + (addedTotals[i]/_quantity[i]) + " each)";
			sOrderTxt += ": " + _currencyStr[_nCurrency] + addedTotals[i] + "\n";
			grandTotal += addedTotals[i];
			if(_needPW[i])
				needPW = true;
		}
	}

	var momsFactor = getVAT();
	sOrderTxt += "Total Amount (excl VAT):\n";
	sOrderTxt += sIndent + _currencyStr[_nCurrency] + grandTotal + "\n";
	sOrderTxt += "VAT/Moms (" + (momsFactor*100) + "%):\n";
	sOrderTxt += sIndent + _currencyStr[_nCurrency] + (grandTotal*momsFactor) + "\n";
	sOrderTxt += "Total Amount (incl VAT):\n";
	sOrderTxt += sIndent + _currencyStr[_nCurrency] + (grandTotal*(1+momsFactor)) + "\n";
	
	sOrderTxt += "\nPaid by ";
	switch(_nPayMethod)
	{
	case 0:
		sOrderTxt += "credit card";
		break;
	case 1:
		sOrderTxt += "money transfer";
		break;
	}
	sOrderTxt += " in " + _currencyStrLong[_nCurrency] + "\n";
	sOrderTxt += sLine + "\n";

	if(_bNewsletter)
	{
		sOrderTxt += "Subscribe to newsletter!\n\n";
	}
	
	if(needPW)
	{
		sOrderTxt += "Password for TurnTool account (garbled):\n";
		sOrderTxt += sIndent + garbleString(escape(_sPwd)) + "\n\n";
	}

	if(_sNotes)
	{
		sOrderTxt += "Notes:\n" + _sNotes + "\n" + sLine + "\n\n";
	}

	return sOrderTxt;
}

//-----------------------------------------------------------------------------
// Pages
//-----------------------------------------------------------------------------
function LoadProductSelectionPage()
{
	_nStep = 0;
	document.title = "TurnTool - Ordering - Product Selection";
	showSections(productSelectionDivFlag | pageHeaderDivFlag | pageFooterDivFlag);
	element(_elemNameStr[0]).focus();
	element(_elemNameStr[0]).select();
}

//-----------------------------------------------------------------------------
function LoadOtherProductPage()
{
	document.title = "TurnTool - Ordering - Other Product";
	element("otherProductDiv").style.display = "block";
	element("otherproduct").focus();
	element("otherproduct").select();
}

//-----------------------------------------------------------------------------
function LoadCustomerInfoPage()
{
	_nStep = 1;

	document.title = "TurnTool - Ordering - Customer Information";

	if( _bSecure==false && _nPayMethod==0 )
	{
		showSections(loadingDivFlag);
		// switch to secure tunnel
		updateOrderDetails();
		window.open( _sThisSecureUrl + allArgsToSearch() , "_self" );
	}
	else
	{
		showSections(customerInfoDivFlag | detailsDivFlag | pageHeaderDivFlag | pageFooterDivFlag);
	
		var bNeedPW = false;
		var i = 0;
		for( i = 0; i<_numProducts; i++ )
		{
			if( _quantity[i] && _needPW[i] )
			{
				bNeedPW = true;
			}
		}
	
		if(bNeedPW)
		{
			element("passwordsection").style.display = "block";
			element("password").value = _sPwd;
			element("verifypassword").value = _sPwd;
		}
		else
		{
			element("passwordsection").style.display = "none";
		}
	
		element("company").value = _sCompany;
		element("contactperson").value = _sContactPerson;
		element("address").value = _sAddress;
		if(_sCountry)
		{
			var countryListBox = element("country");
			if(countryListBox)
			{
				for(i=0; i< countryListBox.options.length; i++)
				{
					if(countryListBox.options[i].innerHTML==_sCountry)
					{
						countryListBox.selectedIndex = i;
						break;
					}
				}
			}
		}
		element("phone").value = _sPhone;
		element("email").value = _sEmail;
		element("notes").value = _sNotes;
		element("newsletter").checked = _bNewsletter;
	
		var payMethodListBox = element("paymethod");
		if(payMethodListBox)
		{
			for(i=0; i< payMethodListBox.options.length; i++)
			{
				if(payMethodListBox.options[i].value==_nPayMethod.toString())
				{
					payMethodListBox.selectedIndex = i;
					break;
				}
			}
		}
	
		if( updateOrderDetails() == 0 )
			LoadErrorPage();
	
		element("company").focus();
		element("company").select();
	}
}

//-----------------------------------------------------------------------------
function LoadPaymentCreditCardPage()
{
	_nStep = 2;
	document.title = "TurnTool - Ordering - Payment by Credit Card";

	if( _bSecure==false )
	{
		showSections(loadingDivFlag);
		// switch to secure tunnel
		updateOrderDetails();
		window.open( _sThisSecureUrl + allArgsToSearch() , "_self" );
	}
	else
	{
		showSections(customerDetailsDivFlag | detailsDivFlag | paymentCreditCardDivFlag | pageHeaderDivFlag | pageFooterDivFlag);

		if( updateOrderDetails() == 0 )
			LoadErrorPage();
	
		updateCustomerDetails();
	
		element("CurrencyID").value = _currencyIDs[_nCurrency];
	
		var sAmount = "";
		var sDotAmount = _nAmount.toString();
		var i=0;
		for( i=0; i<sDotAmount.length; i++ )
		{
			var c = sDotAmount.charAt(i);
			if(c==".")
			{
				sAmount += ",";
			}
			else
			{
				sAmount += c;
			}
		}

		element("Amount").value = sAmount;
		element("MerchantNumber").value = _sMerchantNumber;
		element("ReferenceText").value = AssembleOrder();
		element("OrderID").value = _sOrderID;


		switch(_nErrorCode)
		{
		case -1:
			_sErrorMsg = "";
			break;
		case 1:
			_sErrorMsg = "Credit card number is invalid. Please reenter the information and verify that it is correct";
			break;
		case 4:
			_sErrorMsg = "Transaction denied. Please reenter the information and verify that it is correct";
			break;
		case 0: // invalid merchant ID
		case 2: // invalid amount
		case 3: // missing order ID
		case 5: // internal server error
			_sErrorMsg = "The shop encountered an internal error. Please contact sales@turntool.com and specify this errorcode: ";
			_sErrorMsg += _nErrorCode;
			break;
		}

	}
}

//-----------------------------------------------------------------------------
function LoadPaymentMoneyTransferPage()
{
	_nStep = 2;
	document.title = "TurnTool - Ordering - Payment by Money Transfer";

	if( _bSecure==true )
	{
		showSections(loadingDivFlag);

		// switch from secure tunnel
		updateOrderDetails();
		window.open( _sThisUrl + allArgsToSearch(), "_self" );
	}
	else
	{
		showSections(customerDetailsDivFlag | detailsDivFlag | paymentMoneyTransferDivFlag | pageHeaderDivFlag | pageFooterDivFlag);

		if( updateOrderDetails() == 0 )
			LoadErrorPage();

		updateCustomerDetails();


		element("accountDetailsDiv").innerHTML = _sAccountDetailsHTML;
		element("orderidmt").innerHTML = _sOrderID;
	}
}

//-----------------------------------------------------------------------------
function LoadErrorPage()
{
	_nStep = 3;
	document.title = "TurnTool - Ordering - Unrecoverable Error";
	showSections(showNoneFlag);
	element("errormsg").innerHTML = '<b>An unrecoverable error occurred</b><br>' + _sErrorMsg;
}
//-----------------------------------------------------------------------------
function LoadCompleteOrderCreditCardPage()
{
	_nStep = 4;

	document.title = "TurnTool - Ordering - Order Complete";
	showSections(paymentCreditCardCompleteDivFlag | pageFooterDivFlag);
}

//-----------------------------------------------------------------------------
function LoadCompleteOrderMoneyTransferPage()
{
	_nStep = 5;
	document.title = "TurnTool - Ordering - Completing Order";
	showSections(paymentMoneyTransferCompleteDivFlag | pageFooterDivFlag);
	element("order").value = AssembleOrder();
	element("OrderMailform").submit();
}

//-----------------------------------------------------------------------------
// Button Handlers
//-----------------------------------------------------------------------------
function OnProductSelectionNext()
{
	// validate form data here
	var bValid = true;
	var nTotalItems = 0;
	var i = 0;
	/*
	// No startup fee required!
	if (parseInt(element(_elemNameStr[1]).value)>0)
	{
		element(_elemNameStr[0]).value = 1;
	}
	*/
	for( i = 0; i<_numProducts; i++ )
	{
		_quantity[i] = parseInt(element(_elemNameStr[i]).value);
		if(!isFinite(_quantity[i]))
		{
			element("errormsg").innerHTML = "Error: Quantities must be numbers<br>";
			bValid = false;
			break;
		}
		else
		{
			nTotalItems += _quantity[i];
		}
	}

	if(bValid)
	{
		if( nTotalItems==0 )
		{
			element("errormsg").innerHTML = "Error: No items in order<br>";
		}
		else
		{
			element("errormsg").innerHTML = "";
			LoadCustomerInfoPage(); // next step
		}
	}
}

//-----------------------------------------------------------------------------
function OnAddProduct(addProduct)
{
	if(addProduct)
	{
		var sProduct = element("otherproduct").value;
		if(sProduct!="")
		{
			var price = parseFloat(element("otherprice").value);
	
			if(isFinite(price))
			{
				var newRow = element("productSelectionTable").insertRow(-1);
				if(newRow)
				{
					_bLockCurrency = true;
					element("currency").disabled = _bLockCurrency;
			
					_productStr[_numProducts]		= sProduct;
					_elemNameStr[_numProducts]	= "prod" + _numProducts;
			
					_price0[_numProducts] = 0;
					_price1[_numProducts] = 0;
					switch(_nCurrency)
					{	
					case 0:
						_price0[_numProducts] = price;
						break;
					case 1:
						_price1[_numProducts] = price;
						break;
					}
					_quantity[_numProducts]		= 0;
					_needPW[_numProducts]		= 0;
		
					var newCell;
					newCell = newRow.insertCell(0);
					newCell.innerHTML = "<span class=\"broed\">" + sProduct + "</span>";
					newCell.align = "left";

					newCell = newRow.insertCell(1);
					newCell.innerHTML = "<span class=\"broed\">" + price + "</span>";
					newCell.align = "right";

					newCell = newRow.insertCell(2);
					newCell.innerHTML = '<input type="text" size="2" id="' + _elemNameStr[_numProducts] + '" name="' + _elemNameStr[_numProducts] + '" value="' + _quantity[_numProducts] + '">';
					newCell.align = "right";

					_tableRow[_numProducts] = newRow.rowIndex;
	
					_numProducts++;
				}
			}
		}
	}
	LoadProductSelectionPage();
}

//-----------------------------------------------------------------------------
function OnPaymethodChanged()
{
	_nPayMethod = parseInt(element("paymethod").value);
}

//-----------------------------------------------------------------------------
function OnCurrencyChanged()
{
	_nCurrency = parseInt(element("currency").value);

	var priceCurrencyText = "Price in " + _currencyStr[_nCurrency];

	element("priceCurrency1").innerHTML = priceCurrencyText;
	element("priceCurrency2").innerHTML = priceCurrencyText;
	
	var i = 0;
	for( i = 0; i<_numProducts; i++ )
	{
		element("productSelectionTable").rows[_tableRow[i]].cells[1].innerHTML = "<span class=\"broed\">" + getProductPrice(i,_nCurrency) + "</span>";
	}
	
	updateOrderDetails();
}

//-----------------------------------------------------------------------------
function OnCvrChanged()
{
	_sCvr = element("cvr").value;
	updateOrderDetails();
}

//-----------------------------------------------------------------------------
function OnCountryChanged()
{
	_sCountry = element("country").options[element("country").selectedIndex].innerHTML;
	if(isEUCountry(_sCountry))
	{
		element("cvrNA").style.display = "none";
		element("cvr").style.display = "inline";
//		element("cvr").disabled = false;
	}
	else
	{
		element("cvr").style.display = "none";
		element("cvrNA").style.display = "inline";
//		element("cvr").disabled = true;
	}
	updateOrderDetails();
}

//-----------------------------------------------------------------------------
function OnCustomerInfoBack()
{
	_sCompany = element("company").value;
	_sCvr = element("cvr").value;
	_sContactPerson = element("contactperson").value;
	_sAddress = element("address").value;
	_sPhone = element("phone").value;
	_sEmail = element("email").value;
	_sNotes = element("notes").value;
	_bNewsletter = element("newsletter").checked;

	var bNeedPW = false;
	var i = 0;
	for( i = 0; i<_numProducts; i++ )
	{
		if( _quantity[i] && _needPW[i] )
		{
			bNeedPW = true;
		}
	}
	if(bNeedPW)
	{
		_sPwd = element("password").value;
	}
	element("errormsg").innerHTML = "";
	LoadProductSelectionPage();
}

//-----------------------------------------------------------------------------
function OnCustomerInfoNext()
{
	var bFormValid = true;
	var strErrorMsg = "There were errors in the data entered (please correct it):";

	_sCompany = element("company").value;
	_sCvr = element("cvr").value;
	_sContactPerson = element("contactperson").value;
	_sAddress = element("address").value;
	_sPhone = element("phone").value;
	_sEmail = element("email").value;
	_sNotes = element("notes").value;
	_bNewsletter = element("newsletter").checked;

	// validate form data here
	if(_sContactPerson == "")
	{
		strErrorMsg += "<br>- You must specify a contact person";
		if(bFormValid)
			element("contactperson").focus();
		bFormValid = false;
	}
	if(_sAddress == "")
	{
		strErrorMsg += "<br>- You must specify an address";
		if(bFormValid)
			element("address").focus();
		bFormValid = false;
	}
	if(_sCountry=="" || _sCountry=="------------------Select Country-------------------")
	{
		strErrorMsg += "<br>- You must specify the country of your address";
		if(bFormValid)
			element("country").focus();
		bFormValid = false;
	}
	else if( (_sCountry == "----Unlisted Country (specify in notes)----") && (_sNotes=="") )
	{
		strErrorMsg += "<br>- If your country is not listed, please specify it in the notes field";
		if(bFormValid)
			element("notes").focus();
		bFormValid = false;
	}
	if(_sCvr!="" && !isVATCodeValid())
	{
		strErrorMsg += "<br>- The VAT registration number appears to be invalid";
		if(bFormValid)
			element("cvr").focus();
		bFormValid = false;
	}
	if(!isEmail(_sEmail))
	{
		strErrorMsg += "<br>- Your e-mail address appears to be invalid. Please verify that it is entered correctly. This is very important";
		if(bFormValid)
		{
			element("email").focus();
			element("email").select();
		}
		bFormValid = false;
	}

	var needPW = false;
	var i = 0;
	for( i = 0; i<_numProducts; i++ )
	{
		if( _quantity[i]>0 && _needPW[i] )
		{
			needPW = true;
		}
	}

	if(needPW)
	{
		_sPwd = element("password").value;
		if( _sPwd == "")
		{
			strErrorMsg += "<br>- You must specify a password to access your TurnTool account";
			if(bFormValid)
				element("password").focus();
			bFormValid = false;
		}
		else if( !canGarble(escape(element("password").value)) )
		{
			strErrorMsg += "<br>- Password contains illegal characters";
			if(bFormValid)
			{
				element("password").focus();
				element("password").select();
			}
			bFormValid = false;
		}
		else if( element("password").value != element("verifypassword").value)
		{
			strErrorMsg += "<br>- Passwords do not match";
			if(bFormValid)
			{
				element("verifypassword").focus();
				element("verifypassword").select();
			}
			bFormValid = false;
		}
	}

	var sUrl = _sThisSecureUrl + allArgsToSearch();
	if(sUrl.length>_nUrlMaxLength)
	{
		strErrorMsg += '<br>- The combined order text is too long. If you need to send us a lengthy message, please do so by emailing <a href="mailto:sales@turntool.com">sales@turntool.com</a>. Contact us if it isn\'t possible for you to shorten the text sufficiently to place an order';
		if(bFormValid)
			element("notes").focus();
		bFormValid = false;
	}

	if(bFormValid)
	{
		element("errormsg").innerHTML = "";

		_sOrderID = new Date().getTime().toString();
		for( i=0; (i<_sEmail.length) && (_sOrderID.length<30); i++ )
		{
			var ch = _sEmail.charAt(i);
			if(ch!="@" && ch!="." && ch!="-")
			{
				_sOrderID += ch;
			}
		}

		switch(_nPayMethod)
		{
		case 0:
			LoadPaymentCreditCardPage()
			break;
		case 1:
			LoadPaymentMoneyTransferPage()
			break;
		}
	}
	else
	{
		element("errormsg").innerHTML = strErrorMsg + "<br>";
	}
}

//-----------------------------------------------------------------------------
function OnPaymentCreditcardCompleteOrder()
{
	_sErrorMsg = "";
	_nStep = 2;
	var sFailUrl = _sThisSecureUrl + allArgsToSearch();
	element("FAILURL").value = sFailUrl;

	_nStep = 4;
	var sSuccessUrl = _sThisSecureUrl + allArgsToSearch();
	element("OKURL").value = sSuccessUrl;

	if(_bTest)
	{
		var success = true;
		if(success)
			window.open( sSuccessUrl, "_self" ); // simulate success
		else
			window.open( sFailUrl, "_self" ); // simulate failure
	}
	else
	{
		element("Form").submit();
	}	
}

//-----------------------------------------------------------------------------
function OnPaymentMoneyTransferCompleteOrder()
{
	LoadCompleteOrderMoneyTransferPage();
}

//-----------------------------------------------------------------------------
function AboutSecurity()
{
	window.open(secureTunnel("http://www.turntool.com/shop/security.html"),"AboutSecurity","height=250,width=300,status=no,toolbar=no,menubar=no,location=no,channelmode=no,scrollbars=yes");
}

//-----------------------------------------------------------------------------
function AboutCVC()
{
	window.open(secureTunnel("http://www.turntool.com/shop/cvchelp.html"),"AboutCVC","height=500,width=500,status=no,toolbar=no,menubar=no,location=no,channelmode=no,scrollbars=yes");
}

//-----------------------------------------------------------------------------
function MailtoSales()
{
	window.open("mailto:sales@turntool.com");
}

//-----------------------------------------------------------------------------
// OnLoad handlers
//-----------------------------------------------------------------------------
function OnPageLoaded()
{
	var i = 0;
	if( _bSecure==true )
	{
		element("dankortimg").src = "https://pay.dandomain.dk/securetunnel-bin.asp?url=http://www.turntool.com/shop/dankort.gif";
		element("visakortimg").src = "https://pay.dandomain.dk/securetunnel-bin.asp?url=http://www.turntool.com/shop/visakort.gif";
		element("euromastercardimg").src = "https://pay.dandomain.dk/securetunnel-bin.asp?url=http://www.turntool.com/shop/euromastercard.gif";

		// replace all references to 'index.php' with 'http://www.turntool.com/index.php'
		var links = getTags("A");
		for( i=0; i<links.length; i++ )
		{
			var linkVar = links[i];
//			linkVar.href = linkVar.href.replace(/https:\/\/pay\.dandomain\.dk\/index\.php/i, "http://www.turntool.com/index.php");

			if(linkVar.href.indexOf("https://pay.dandomain.dk/index.php")>=0)
			{
				var sHref = linkVar.href;
				var sNewHref = "http://www.turntool.com/index.php";
				var n = sHref.indexOf("?");
				if( n>=0 )
				{
					while( n<sHref.length )
					{
						sNewHref += sHref.charAt(n);
						n++;
					}
				}
				linkVar.href = sNewHref;
			}

			if(linkVar.innerHTML == "Shop")
				linkVar.style.color = "#A5C929";
		}
	}	
	else
	{
		element("dankortimg").src = "http://www.turntool.com/shop/dankort.gif";
		element("visakortimg").src = "http://www.turntool.com/shop/visakort.gif";
		element("euromastercardimg").src = "http://www.turntool.com/shop/euromastercard.gif";
	}

	// Process arguments
	allArgsFromSearch();

	// init product selection page
	var currencySelect = element("currency");
	if(currencySelect)
	{
		currencySelect.disabled = _bLockCurrency;
		for(i=0; i< currencySelect.options.length; i++)
		{
			if(currencySelect.options[i].value==_nCurrency.toString())
			{
				currencySelect.selectedIndex = i;
				break;
			}
		}
	}

	for( i = 0; i<_numProducts; i++ )
	{
		var newRow = element("productSelectionTable").insertRow(-1);
		if(newRow)
		{
			var newCell;
			newCell = newRow.insertCell(0);
			newCell.innerHTML = "<span class=\"broed\">" + _productStr[i] + "</span>";
			newCell.align = "left";
			newCell = newRow.insertCell(1); //.innerHTML = getProductPrice(i,_nCurrency);
			newCell.align = "right";
			newCell = newRow.insertCell(2)
			newCell.innerHTML = '<input type="text" size="2" id="' + _elemNameStr[i] + '" name="' + _elemNameStr[i] + '" value="' + _quantity[i] + '">';
			newCell.align = "right";
			_tableRow[i] = newRow.rowIndex;
		}
	}

	OnCurrencyChanged();

	if(_bEnableOther)
	{
		element("otherProductButton").style.display = "block";
	}
	else
	{
		element("otherProductButton").style.display = "none";
	}

	switch(_nStep)
	{
	case 0:
		LoadProductSelectionPage();
		break;
	case 1:
		LoadProductSelectionPage();
		OnProductSelectionNext();
		OnCountryLoadFix();
		break;
	case 2:
		LoadProductSelectionPage();
		OnProductSelectionNext();
		OnCustomerInfoNext();
		break;
	case 3:
		LoadErrorPage();
		break;
	case 4:
		LoadCompleteOrderCreditCardPage();
		break;
	case 5:
		LoadCompleteOrderMoneyTransferPage();
		break;
	}

	if(_sErrorMsg != "")
	{
		element("errormsg").innerHTML = _sErrorMsg + "<br><br>";
	}
}
function OnCountryLoadFix()
{
	_sCountry = decode(getArgumentValue("country"));
	// rest of function is rip from country changed handler (jf 26-06-06)
	if(isEUCountry(_sCountry))
	{
		element("cvrNA").style.display = "none";
		element("cvr").style.display = "inline";
//		element("cvr").disabled = false;
	}
	else
	{
		element("cvr").style.display = "none";
		element("cvrNA").style.display = "inline";
//		element("cvr").disabled = true;
	}
	if(isResellerCountry(_sCountry))
	{
		showSections(currentShowFlags | resellerDivFlag);
	}
	updateOrderDetails();
}