function format_currency(amount) {
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}
// end of function CurrencyFormatted()

function get_localized_currency(amount) {
	if (!window.exchange_rates || !window.prefer_currency || window.prefer_currency=='USD') return '';
	
	var rate = window.exchange_rates[window.prefer_currency];
	if (!rate) return '';

	return window.prefer_currency+format_currency(amount * rate);
}

function get_preferred_currency() {
	if (!document.cookie) return;

	var cookies = document.cookie.split(';');
	for (var i=0; i<cookies.length; i++) {
		var cookiedata = cookies[i].split('=');
		if (cookiedata[0]=='currency') {
			var currency = cookiedata[1];
			if (currency && currency.match && currency.match(/^[A-Z]{3}/)) {
				return currency.substr(0,3);
			}
		}
	}

	return 'USD';
}

function update_license_price(lictype) {
	var streamsfield = window.license_form[lictype+'streams'];
	if (!streamsfield) return;
	var licstreams = streamsfield.options ? streamsfield.options[streamsfield.selectedIndex].value : streamsfield.value;
	if (!licstreams) return;

	var termfield = window.license_form[lictype+'term'];
	if (!termfield) return;
	var licterm = termfield.options ? termfield.options[termfield.selectedIndex].value : termfield.value;
	if (!licterm) return;
	
	var licpackage = window.packages[window.license_software][lictype];
	if (!licpackage) return;
	
	var lictermdetails = licpackage[licstreams];
	if (!lictermdetails) return;
	
	var licdetails = lictermdetails[licterm];
	var el = document.getElementById(lictype+'price');
	if (!el) return;
	
	var licprice = licdetails[1];
	window.active_addons = new Array();
	
	var onetime = 0;
	
	for (addonname in window.packages[window.license_software].addons) {
		var addonfield = window.license_form[lictype+addonname];
		if (!addonfield) continue;
		var addonactive = addonfield.options ? (parseInt(addonfield.options[addonfield.selectedIndex].value)>0) : (parseInt(addonfield.value)>0);
		if (!addonactive) continue;
		
		var addon = window.packages[window.license_software].addons[addonname];
		if (!addon) continue;
		if (addon[licterm]) {
			window.active_addons[window.active_addons.length] = addon[licterm][0];
			licprice += addon[licterm][1];
		} else if (addon['one-time']) {
			window.active_addons[window.active_addons.length] = addon['one-time'][0];
			onetime += addon['one-time'][1];
		}
	}
	
	if ( (onetime>0) && (licterm=='one-time') ) {
		licprice += onetime;
		onetime = 0;
	}

	var localpricestr = '';
	var pricestr = '';
	var amt = '';
	if (onetime>0) {
		pricestr += '$'+format_currency(onetime)+' plus<br />';
		amt = get_localized_currency(onetime);
		if (amt!='') localpricestr += 'Approx. '+amt+' plus<br />';
	}
	pricestr += '$'+format_currency(licprice)+' '+licterm;
	amt = get_localized_currency(licprice);
	if (amt!='') localpricestr += 'Approx. '+amt+' '+licterm;

	if (window.prefer_currency!='USD') {
		el.innerHTML = pricestr + '<br /><div style="font-weight: normal; font-size: 11px">' + localpricestr + '</div>';
	} else {
		el.innerHTML = pricestr;
	}

	window.active_package_id[lictype] = licdetails[0];
}

function calc_initialize() {
	window.active_package_id = new Array();
	window.prefer_currency = get_preferred_currency();

	window.license_form = document.getElementById('license_package_form');
	
	for (lictype in window.license_types) {
		update_license_price(window.license_types[lictype]);
	}
}

function calc_attach() {
	if (window.attachEvent) {
		//IE/Opera
		window.attachEvent("onload", calc_initialize);
	} else if (window.addEventListener) {
		// IE6
		window.addEventListener("load", calc_initialize, false);
	} else {
		//Firefox
		document.addEventListener("load", calc_initialize, false);
	}
}
calc_attach();
