// currencies.js
MCcookie="MC4_6202290A";

function setArrayOfCurrencyObjects(){
money = new Array();

 // money[x]  = new Currency(Currency_name,Code,Symbol,Decimals,Prefix,spacing)
 // Get these definitions by cut-n-paste from formulae in MULTI-CURRENCY.xls column G


// paste below this line --------------

money['aud'] = new Currency('Australian Dollar','aud',36,'$',true,false,'' );
money['gbp'] = new Currency('Sterling (UK)','gbp',826,'&pound;',false,false,'' );
money['usd'] = new Currency('US Dollar','usd',840,'$',false,false,'' );
money['eur'] = new Currency('European Euro','eur',978,'&euro;',false,false,'' );
money['jpy'] = new Currency('Japanese Yen','jpy',392,'&yen;',false,false,'' );
money['sgd'] = new Currency('Singapore Dollar','sgd',702,'$',false,false,'' );
money['nzd'] = new Currency('New Zealand Dollar','nzd',554,'$',false,false,'' );
money['hkd'] = new Currency('Hong Kong Dollar','hkd',344,'$',false,false,'' );


// paste above this line --------------
return(money);
} //


// Alternatively, delete from the above the currencies your site will NOT be offering

/////////
currenciesReady=true;

function initCurrencyObjects(){
while(!currenciesReady){top.delay(500);}
initCurrencyObjects1();
}

// Construction top.MONEY and top.baseCurrency
function initCurrencyObjects1(){
top.MONEY=new Array();
this.money=setArrayOfCurrencyObjects();
this.addProps=addMONEYproperties;
this.addProps();
this.baseCurrency=this.money[this.base];
this.baseCurrency.Format=Format;
this.baseCurrency.roundMoney=roundMoney;
} //

function addMONEYproperties(){
this.base="aud";
this.fixedPriceDelimiter=":";
this.customPriceDelimiter="[";
this.cMult=1;
this.BaseIndex="";
this.MC_codes=getMC_codes();
this.isMC=this.MC_codes.length>4;
this.numberOfCurrencies=Math.round((this.MC_codes.length+1)/4);
top.isMC=this.isMC;
this.getBaseIndex=getBaseIndex;
this.pricefix=pricefix;
this.inCents=inCents;
this.Price=Price;
this.Format=Format
this.eFormat=eFormat
this.wSelect=wSelect;
//this.wLinkList=wLinkList;
this.baseprice=baseprice;
this.ChangeCurrency=ChangeCurrency;
this.setPrice=setPrice;
this.setInputPrice=setInputPrice;
this.comparePrice=comparePrice;
this.setSelect=setSelect;
this.CurrencySelectIndex=0;
this.roundMoney=roundMoney;
// this.ChangeCurrency(this.base);
//this.emailFormat=emailFormat;
//this.staticScript=top.SYS.rootpath+top.sp.staticScript;

//this.dynamicRates=top.sp.dynamicRates;

//var rootURL=top.sp.rootURL;
//this.dynamicRates=(this.dynamicRates && top.SYS.rootpath.indexOf(rootURL)==0)?true:this.dynamicRates;

//this.dynaScript=rootURL+top.sp.dynaScript;
//this.scriptURL=(top.SYS.connected && this.isMC && this.dynamicRates)?this.dynaScript:this.staticScript;
//this.getRates=getRates;
this.cMult=1;
//this.getRates();
}//

function getMC_codes(){ // MONEY.MC_codes := e.g. 'usd;gbp;eur;'
ret=''; for(var ccd in money) { ret += ccd +";" ; }
return(ret);
}//

function getBaseIndex(){ // redundant function?
this.BaseIndex=this.MC_codes.indexOf(base)/4+1;
if(this.BaseIndex==.75){alert("Base currency code "+base+" not installed in money.htm data")}
if(top.Debug){alert("MC_codes = "+this.MC_codes+"\n"+"base = "+this.base+"\n"+ "BaseIndex = "+this.BaseIndex);}
this.CurrencySelectIndex=this.MC_codes.indexOf(this.base)/4; // start point for currency SELECTs
}//

function inCents(amt){
var aMult = this.Decimals ? 100 : 1;
return(Math.floor(0.001+(aMult*amt)));
}//



function additionalLoad(){ // called from system/index.html : fn ToStartPage()
//MCcookie="MC4_"+top.SassLicence;
var tmp=""+top.getCookie(MCcookie);
if(tmp!=="undefined" && tmp!=="null" &&  tmp!=this.base && money[tmp])
	{ this.ChangeCurrency(tmp); }
else
	{ this.ChangeCurrency(top.MONEY.base); }


// Euro Symbol
// makeImage(iSRC,iH,iW,iB,iA,iALT,iHSP,iVSP)
//if(this.money['eur'])
//	{this.money['eur'].Symbol=top.makeImage(top.SYS.pixpath+"euro.gif",10,10,0,"","Euro currency symbol");}
}

function setCurrencyCookie(code){
var exp = new Date();
exp.setTime(exp.getTime() + (1000 * 60 * 60 * 24 * 365)); // 1 year
document.cookie = MCcookie + "=" + escape(code) + "; path=/" + ("; expires=" + exp.toGMTString());
}

function getCookie(Name) {
var search=Name+"="; 
if (document.cookie.length>0){
offset=document.cookie.indexOf(search);
if(offset!=-1){ 
offset+=search.length; end=document.cookie.indexOf(";",offset);
if(end==-1){end=document.cookie.length;} return unescape(document.cookie.substring(offset,end));
}
}
}



  // Prices may be stored as delimited string or converted with rate for current currency.
 function pricefix(pVal,basketDisplay){ // added parameter 01 February 2003
 basketDisplay=(basketDisplay==null)?false:basketDisplay;
 var ret="_";pStr=""+pVal;
 if(pStr.indexOf(this.fixedPriceDelimiter)>-1)               // FIXED PRICE ?
  {
  var PR=pStr.split(this.fixedPriceDelimiter);
  if(PR.length - this.numberOfCurrencies != 0 ){alert("DEVELOPERS ALERT :\nMismatch of price string to number of currencies");}
  ret=PR[this.CurrencySelectIndex];
  // Price may not be fixed for the chosen currency, but sent as "_"
  ret = (ret=="_")?parseFloat(baseprice(pStr))*this.cMult: parseFloat(ret);
  }
 else                                   // FLOATING RATE PRICES
  {

 // CUSTOM
 if(pStr.indexOf(this.customPriceDelimiter)>-1)  // CUSTOM PRICE STRING DETECTED
 	{
 	if(top.X.customPriceResolve)
 		{
 		// added parameter 01 February 2003 - true if called from review or summary
 		pStr=top.X.customPriceResolve(pStr,this.customPriceDelimiter,basketDisplay);
 		}
 	else{alert('function top.X.customPriceResolve() missing');}
 	}

  ret=parseFloat(pStr)*this.cMult;
  }
 return(ret);
 }

 function baseprice(Price){
 var ret="_";Price=""+Price;
 if(Price.indexOf(this.fixedPriceDelimiter)>-1)
  { var PR=Price.split(this.fixedPriceDelimiter);
  ret = PR[this.BaseIndex];
  if (ret=="_")
   {
    alert("ALERT for SITE DEVELOPER\n\nERROR in Price string : \""+Price+"\"\n\nPrice for "+this.base+" in position "+this.BaseIndex+" cannot be \"_\"");
	ret="0";
   }
  ret = parseFloat(ret);                    // FIXED PRICES
  }
 else
 {
 ret = parseFloat(Price)*1;                // FLOATING RATE PRICES
 }
 return(ret);
 }

function Currency(Cname,Code,isoNumber,Symbol,Decimals,EuFormat,cBetween)
{
this.Cname=Cname;
this.Code=Code;
this.isoNumber=isoNumber;
this.Symbol=Symbol;
this.Decimals=Decimals;
this.EuFormat=EuFormat;
this.cBetween=cBetween;
return this;
}//

function ChangeCurrency(code,ReloadPage,W){
	if(this.money[code])
		{
		ReloadPage=(ReloadPage==null)?false:ReloadPage;// default false
	//	W=(W==null)?top.SYS.bizFrame():W;
		W=this.window.document;
		this.Symbol=this.money[code].Symbol;
		this.isoNumber=this.money[code].isoNumber;
		this.Decimals=this.money[code].Decimals;
		this.EuFormat=this.money[code].EuFormat;
		this.cBetween=this.money[code].cBetween;
		this.Cname=this.money[code].Cname;
		this.Code=this.money[code].Code;
		this.cMult=1;
		this.CurrencySelectIndex=this.MC_codes.indexOf(code)/4;
		CurrencySelectIndex=this.CurrencySelectIndex // 3.x compatibility
		setCurrencyCookie(code);
		if(ReloadPage){W.location=W.location.href;}
		
	// top.counter();
   }
}

// put current MC price into form input 'I', based on price P
function setPrice(I,P){this.setInputPrice(I,P);}

function setInputPrice(I,P,Txt){P=1*this.pricefix(P);
Txt=(Txt==null)?"":Txt; Txt=(Txt.charAt(0)==" ")?Txt:" "+Txt;
var symbol=(this.Symbol.toLowerCase().indexOf("img")>-1)?this.Code:this.Symbol;
var ctxt=toUpperCase(this.Code);
I.value=symbol+" "+this.roundMoney(P)+Txt+" "+ctxt;}

// Set MC select box 'List' to current currency
function setSelect(List){List.selectedIndex=this.CurrencySelectIndex;}

// Used in Review and Order Summary to format amounts already converted with pricefix
function Format(V){return(this.Symbol + this.cBetween + this.roundMoney(V));}
function eFormat(V){return(this.Code + this.cBetween + this.roundMoney(V));}
function Price(V){
var asterix;
if (this.Code=="aud"){
asterix=''}
else {
//asterix=' *'
asterix=''}
 return(this.Symbol + this.cBetween + this.roundMoney( this.pricefix(V) ) )+ ' ' +this.Code.toUpperCase()+asterix;}
function comparePrice(V){ var ret='<span class="inforx">'+this.Price(V)+'</span>';
// alert("this.base = "+this.base + "\n" + "this.Code = "+this.Code ) ; //!!
if(this.base!=this.Code) { ret+= '<span class="inbase"> (' + top.baseCurrency.Format(V) + ')</span>'; } return(ret);}


function wSelect(W){
var WD = W.document;
var write_form_tags=wSelect.arguments.length>1

if(write_form_tags){WD.write('<form>');}

WD.write('<select name="MCSelect" class="currencySelect" onchange="top.MONEY.ChangeCurrency(top.listvalue(this));if(window.changePrice){changePrice(this.form,this);}">');
for(ccd in this.money)
{
WD.write('<option value="', ccd , '">', this.money[ccd].Cname , '</option>');
}
WD.write("</select>");

if(write_form_tags){WD.write('</form>');}

}//


function roundMoney(Val){ /* revised 23rd August 2002 to use split() */
var Ret,tmp,D,I,neg;
neg=(Val<0);if(neg){Val=Math.abs(Val);}
if(!this.Decimals){Ret=formatCommas(""+Math.ceil(1*Val));}
else{Ret=formatCommas(""+Math.ceil(1*Val))+".00";}
//else{if((""+Val).indexOf(".")==-1){Ret=""+formatCommas(Val)+".00";}
//else{tmp=("0"+Val).split(".");I=1*tmp[0];D=tmp[1];
//while(D.length<3){D+="0";}D=Math.round(D.substring(0,3)/10);
//if(D==100){D="00";I++;} else{D=D<10?"0"+D:""+D;}
//while(D.length<2){D+="0";} Ret=formatCommas(I)+"."+D;}}
if(this.EuFormat){Ret=doEuFormat(Ret);}
if(neg){Ret="-"+Ret;}
return(Ret);}


function formatCommas(Val){ /* revised Dec 99 */ var Os,Ts,len;Val=""+Val;Os="";Ts="";len=Val.length;if(len>=3){while(len>0){Ts=Val.substring(len-3,len);if(Ts.length==3){Os=","+Ts+Os;len=len-3;}else{Os=Ts+Os;len=0;}} if(Os.substring(0,1)==","){Val=Os.substring(1,Os.length)}else{Val=Os}} return(Val);}

function doEuFormat(numstring){
var SPACE=" ";var COMMA=","; var FSTOP=".";
numstring=top.swapstring(numstring, COMMA , SPACE );
numstring=top.swapstring(numstring, FSTOP , COMMA );
return(numstring);
}

function cleanCommas(A){
var SPACE=" ";var COMMA=","; var FSTOP=".";
if(top.MONEY.EuFormat)
	{
	A=top.swapstring(A,SPACE,""); /* remove spaces */
	A=top.swapstring(A,COMMA,FSTOP); /* make decimal a point */
	}
else { A=top.swapstring(A,COMMA,""); /* remove commas */ }
return(A); }

if(!top.INSASS)
	{
initCurrencyObjects()}



