//=====================================================================||
//                JavaScript currency exchange Module                  ||
//                                                                     ||
//---------------------------------------------------------------------||

htmlData = "";
FXRates = null;

// cgiServerURL should end up pointing to
// the folder thats holding currency.pl

cgiServerURL = "http://www.smegsite.co.uk/albionbears/cgi-bin/";

//---------------------------------------------------------------------||

function isLocal( dom )
{
	return (document.domain == "" || document.domain == "127.0.0.1" || document.domain == "localhost" || document.domain == "charon");
}

/* --------------------------------------------------------------- */

function doChange( istr , fstr , rstr)
{
	do {
		if((p = istr.indexOf(fstr)) != -1)
		{
			istr = istr.substring(0,p) + rstr + istr.substring(p+fstr.length,istr.length) ;
		}
	} while (p != -1) ;
	
	return istr;
}
						
/* --------------------------------------------------------------- */

function setSource( cgiFile )
{
	if(document.getElementById("cgiscript"))
	{
		if(isLocal())
		{
			document.getElementById("cgiscript").src = "http://127.0.0.1/albionbears/cgi-bin/" + cgiFile;
		}
		else
		{
			document.getElementById("cgiscript").src = cgiServerURL + cgiFile;
		}
	}
}

/* --------------------------------------------------------------- */

function setCurrencySource( )
{
	setSource( "currency.pl" );
}

/* --------------------------------------------------------------- */

function createRates( baseCurr )
{
	FXRates = new Rates( htmlData , baseCurr );
}

/* --------------------------------------------------------------- */

function getFXData( htmlData , baseCurr )
{
	var Rates = new Array();

	iStr = htmlData.indexOf("<Cube currency");
	iEnd = htmlData.indexOf("</Cube");
	
	htmlBase = htmlData.substring(iStr,iEnd);
	
	htmlBase = doChange(htmlBase,"<Cube currency=~sq~","");
	htmlBase = doChange(htmlBase,"~sq~ rate=~sq~","=");
	htmlBase = doChange(htmlBase,"~sq~/>~lf~",",");
	htmlBase = doChange(htmlBase,"\t","");
	
	
	pairs = htmlBase.split(","); 
   	
   	for(i = 0 ; i < pairs.length ; i++) 
   	{
    	if((pos = pairs[i].indexOf('=')) >= 0)
		{
			Rates[Rates.length] = new Rate(pairs[i].substring(0,pos), parseFloat(pairs[i].substring(pos+1)),getSymbol(pairs[i].substring(0,pos)));
    	}
   	}

	Rates[Rates.length] = new Rate("EUR",1.0,getSymbol("EUR"));
	
	pos = -1
	for(i = 0 ; i < Rates.length ; i++)
	{
		if(Rates[i].iso == baseCurr)
		{
			pos = i;
			break;
		}
	}
	
	if(pos != -1)
	{
		cRate = Rates[pos].rate;
		for(i = 0 ; i < Rates.length ; i++)
		{
			Rates[i].rate /= cRate;
		}
	}

	return Rates;
	
}

/* --------------------------------------------------------------- */

function Rate( iso , rate , symbol )
{
	this.iso = iso;
	this.rate = rate;
	this.symbol = symbol;
}

/* --------------------------------------------------------------- */

function getSymbol( iso )
{
	if(iso == "GBP")
	{
		return "£";
	}
	else if(iso == "USD" || iso == "CAD")
	{
		return "$";
	}
	else if(iso == "AUD")
	{
		return "A$";
	}
	else if(iso == "EUR")
	{
		return "€";
	}
	else if(iso == "ZAR")
	{
		return "R";
	}
	else
	{
		return "";
	}
}

/* --------------------------------------------------------------- */

function Rates( htmlData , baseCurr)
{
	this.allRates = getFXData( htmlData , baseCurr );
	this.baseCurr = baseCurr;
	this.Convert = Convert;
	this.Symbol = Symbol;
}

/* --------------------------------------------------------------- */

function Convert( value , iso )
{
	if(iso == this.baseCurr) return value;

	for(i = 0 ; i < this.allRates.length ; i++)
	{
		if(this.allRates[i].iso == iso)
		{
			return value * this.allRates[i].rate * 1.025;
		}
	}

	return 0.0;
}

/* --------------------------------------------------------------- */

function Symbol( iso )
{
	for(i = 0 ; i < this.allRates.length ; i++)
	{
		if(this.allRates[i].iso == iso)
		{
			return this.allRates[i].symbol;
		}
	}

	return "";
}

/* --------------------------------------------------------------- */

