﻿// Exchange RATES
euro_rate = 744.36; // EUROS
dollars_rate = 558.53; // DOLLARS





function getExchangeRates(_fromRate, _toRate){
	var fromRate = document.getElementById(_fromRate);
	var toRate = document.getElementById(_toRate);
	var currentRate = 0;
	var beforeRate;
	var afterRate;
	if(!fromRate){
		alert("fromRate does not exist");
	}
	
	switch(_toRate){
		case "amount_euro":
			currentRate = parseInt(euro_rate);
			beforeRate = "€";
			afterRate = ",-";
			break;
		case "amount_dollars":
			currentRate = parseInt(dollars_rate);
			beforeRate = "$";
			afterRate = ",-";
			break;
	}
	
	fromInt = replaceAmountToInt(fromRate.innerHTML);
	var returnRate  = roundVal(((fromInt / currentRate) * 100), 0);
//	document.write(beforeRate + " " + ins1000Sep(rem1000Sep(returnRate + "")) + afterRate )
	toRate.innerHTML = beforeRate + " " + ins1000Sep(rem1000Sep(returnRate + "")) + afterRate;
	
}

function roundVal(val, dec){
	var result = Math.round(val*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}


function replaceAmountToInt(value){
	value = value.replace("kr.", "");
	value = value.replace(".", "");
	value = value.replace(",", "");
	return parseInt(value);
}

function ins1000Sep(val){
	 val = val.split(",");
	 val[0] = val[0].split("").reverse().join("");
	 val[0] = val[0].replace(/(\d{3})/g,"$1.");
	 val[0] = val[0].split("").reverse().join("");
	 val[0] = val[0].indexOf(".")==0?val[0].substring(1):val[0];
	 return val.join(",");
}
function rem1000Sep(value){
 	return value.replace(/\./g,"");
}

function do_Init(){
	if(document.getElementById("amount_dk") && document.getElementById("amount_euro")){
		getExchangeRates("amount_dk", "amount_euro");
	}

	if(document.getElementById("amount_dk") && document.getElementById("amount_dollars")){
		getExchangeRates("amount_dk", "amount_dollars");
	}
}
window.onload = do_Init; 
