En anden tilgangsvinkel:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"
http://www.w3.org/TR/html4/loose.dtd"><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Test gange med faktor</title>
<script type="text/javascript">
var durations=new Array(3);
var active=0;
durations[0]=new Array(3);
durations[0]["shorttext"]='DKK'
durations[0]["longtext"]='Pricelist is showing in Danish kroner'
durations[0]["duration"]='100'
durations[1]=new Array(3);
durations[1]["shorttext"]='US$'
durations[1]["longtext"]='Pricelist is showing in US dollar'
durations[1]["duration"]='584.94'
durations[2]=new Array(3);
durations[2]["shorttext"]='Euro'
durations[2]["longtext"]='Pricelist is showing in Euro'
durations[2]["duration"]='600.50'
function switchDuration(number) {
document.getElementById("shorttext"+active).style.fontWeight="";
document.getElementById("shorttext"+active).style.color="";
document.getElementById("shorttext"+number).style.fontWeight="bold";
document.getElementById("shorttext"+number).style.color="red";
document.getElementById("desc").innerHTML=durations[number]["longtext"]+" - rate of exchange "+durations[number]["duration"];
active=number;
factorNums();
}
function factorNums()
{
//init
factor=100/durations[active]["duration"];
numberInitPattern=/\d+\.\d{0,2}/g;
numberWithoutDecimals=/\d+/g;
talSpans=new Array;
results=new Array;
spans=document.getElementsByTagName("span"); //læg alle spans i dokumentet ind i spans
for(i=0;i<spans.length;i++) //læg de spans hvis id=tal ind i talSpans
{
if(spans[i].className.indexOf("tal")==0)
{
talSpans.push(spans[i]);
}
}
alert(talSpans.length);
//tjek om hver enkelt talspan indeholder et nummer efter numberInitPattern-skabelonen,
//og læg dem der gør ind i results.
for(i=0;i<talSpans.length;i++)
{
/*if(!talSpans[i].className.replace(/tal/,"").match(numberWithoutDecimals).length<1) talSpans[i].className+=".00";
results[i]=talSpans[i].className.replace(/tal/g,"").match(numberInitPattern);*/
results[i] = talSpans[i].className.replace(/tal/,"");
}
//gang alle results med factor og indsæt dem i dokumentet.
for(i=0;i<results.length;i++)
{
alert(results[i]);
results[i]=results[i]*factor;
talSpans[i].innerHTML=readingComma(correctDecimals(results[i]));
}
}
function correctDecimals(tal)
{
tal=tal+"";
if (typeof(tal)==String)tal=parseFloat(tal);
if(tal.substring(tal.length-4).match(/\,[0-9][0-9]/)) return tal;
talTx="_"+Math.round( 100.0*tal)+"_";
if (talTx.length<5)
{
preZero="_000";
talTx=preZero.substring(0,6-talTx.length)+talTx.substring(1);
}
talTx=talTx.substring(1,talTx.length-3)+","+talTx.substring(talTx.length-3,talTx.length-1);
tal=talTx;
return tal;
}
function readingComma(num)
{
num+="";
splitted=num.split('.');
spl1=splitted[0];
spl2=splitted.length>1?"."+splitted[1]:'';
var pattern=/(\d+)(\d{3})/;
while(pattern.test(spl1))
{
spl1=spl1.replace(pattern,'$1'+'.'+'$2');
}
return spl1+spl2;
}
//hvis Array.push ikke findes
if (typeof Array.prototype.push == 'undefined') {
Array.prototype.push=function(txt)
{
return this[this.length]=txt;
}
}
</script>
</head>
<body onload="factorNums()">
<input onclick="switchDuration(0);" type="radio" value="0" name="duration">
<span id="shorttext0">DKK</span>
<input onclick="switchDuration(1);" type="radio" value="1" name="duration">
<span id="shorttext1">US$</span>
<input onclick="switchDuration(2);" type="radio" value="2" name="duration">
<span id="shorttext2">Euro</span><br>
<span id="desc">Pricelist is showing in Danish kroner</span>
<p> </p>
<table width="200" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><div align="right">DKK</div></td>
<td><div align="right">omregnet</div></td>
</tr>
<tr>
<td><div align="right">100,00</div></td>
<td><div align="right"><span class="tal100.00">100,00</span></div></td>
</tr>
<tr>
<td><div align="right">2000,00 </div></td>
<td><div align="right"><span class="tal2000.00">2000,00</span></div></td>
</tr>
<tr>
<td><div align="right">3231,25</div></td>
<td><div align="right"><span class="tal3231.25">3231,25</span></div></td>
</tr>
<tr>
<td><div align="right">12234,12</div></td>
<td><div align="right"><span class="tal12234.12">12234,12</span></div></td>
</tr>
</table>
</body>
</html>