﻿var current_currency = 'EUR';

function str_replace(str,searchstr,replacestr,trim) {
	var tstring = str;
	
	if (typeof(searchstr) == 'object') {
		for (a=0;a<searchstr.length;a++) {
			if (str.indexOf(searchstr[a]) != -1) {
				tstring = tstring.split(searchstr[a]);
				tstring = tstring.join(replacestr[a]);
			}
		}
	} else {
		if (str.indexOf(searchstr) != -1) {
			tstring = str.split(searchstr)
			tstring = tstring.join(replacestr);
		} else {
			tstring = str;
		}
	}
	
	switch (trim) {
		case 'ltrim':
			tstring = tstring.replace(/^\s+/,'');
			break;
		case 'rtrim':
			tstring = tstring.replace(/\s+$/,'');
			break;
		case 'trim':
			tstring = tstring.replace(/^\s+|\s+$/,'');
			break;
	}
	
	return tstring;	
}

function get_total(tracks,singleprice,currency) {
	total = (currency == 'EUR') ? (tracks*singleprice) : ((singleprice*tracks)*get_exchangerate(currency));
	return total.toFixed(2);
}

function get_eur_price(currency,price) {
	return (price/get_exchangerate(currency));
}

function get_price(tracks) {
	discount = new Array('75.00','70.00','67.00','64.00');
	
	if (tracks > 1  && tracks < 5) {
		dp = discount[1];
	} else if (tracks > 4 && tracks < 10) {
		dp = discount[2];
	} else if (tracks > 9) {
		dp = discount[3]
	} else {
		dp = discount[0];
	}
	
	return dp;
}

function bv() {
	return navigator.appVersion.indexOf('MSIE');
}

function calc_track_price(tracks,currency) {	
	var currency 	= currency.toUpperCase();
	var price 		= get_price(tracks);
	
	if (currency == 'USD') {
		/* PRICE */
		$('calcprice').firstChild.nodeValue = get_total(tracks,price,currency) + ' ' + currency;
		/* SET PRICE PER TRACK */
		$('price1').firstChild.nodeValue = get_total(1,get_price(1),currency) + ' ' + currency;
		$('price2').firstChild.nodeValue = get_total(1,get_price(2),currency) + ' ' + currency;
		$('price3').firstChild.nodeValue = get_total(1,get_price(5),currency) + ' ' + currency;
		$('price4').firstChild.nodeValue = get_total(1,get_price(10),currency) + ' ' + currency;
	} else {
		/* PRICE */
		$('calcprice').firstChild.nodeValue = str_replace(get_total(tracks,price,currency),'.',',') + ' ' + currency;
		/* SET PRICE PER TRACK */
		$('price1').firstChild.nodeValue = str_replace(get_total(1,get_price(1),currency),'.',',') + ' ' + currency;
		$('price2').firstChild.nodeValue = str_replace(get_total(1,get_price(2),currency),'.',',') + ' ' + currency;
		$('price3').firstChild.nodeValue = str_replace(get_total(1,get_price(5),currency),'.',',') + ' ' + currency;
		$('price4').firstChild.nodeValue = str_replace(get_total(1,get_price(10),currency),'.',',') + ' ' + currency;
	}
	
	/* SET PRICERANGE */
	if(tracks > 1  && tracks < 5) {
		pricerange = 2;
	} else if (tracks > 4 && tracks < 10) {
		pricerange = 3;
	} else if (tracks > 9) {
		pricerange = 4;
	} else {
		pricerange = 1;
	}
	
	/* IE CLASS CHILD */
	class_attribute = (bv() == -1) ? 'class' : 'className';	
	
	/* REMOVE CLASS */
	for (a=1;a<5;a++) {
		if ($('pricerange'+a).getAttribute(class_attribute) == 'pricepointer') {
			$('pricerange'+a).removeAttribute(class_attribute);	
		}
	}
	
	/* SET CLASS */
	$('pricerange'+pricerange).setAttribute(class_attribute,'pricepointer');
}

function get_tracks() {
	ordertracks = 0;
	el_tracks = $('filelist').getElementsByTagName('input');
	for (a=0;a<el_tracks.length;a++) {
		if (el_tracks[a].getAttribute('type')== 'checkbox' && el_tracks[a].checked == true) {
			ordertracks++;
		}
	}
	return ordertracks;
}

function get_placeorder_total(currency) {
	currency 		= currency.toUpperCase();
	count_tracks 	= get_tracks();
	
	if (count_tracks < 1) {
		el_tracks = $('filelist').getElementsByTagName('input');
		for (a=0;a<el_tracks.length;a++) {
			if (el_tracks[a].getAttribute('type') == 'checkbox') {
				el_tracks[a].checked = 'checked';
				break;
			}
		}
		count_tracks = get_tracks();
	}
	
	$('tracks').firstChild.nodeValue 		= count_tracks;
	$('price').firstChild.nodeValue 		= (currency == 'EUR') ? str_replace(get_price(count_tracks),'.',',','trim') : str_replace(get_total(1,get_price(count_tracks),currency),'.',',','trim');
	$('totalprice').firstChild.nodeValue 	= str_replace(get_total(count_tracks,get_price(count_tracks),currency),'.',',','trim') + ' ' + currency;
}

function set_current_currency(currency) {
	current_currency = currency.toUpperCase();
}

function get_total_sum(currency) {
		currency	= currency.toUpperCase();
		price_value	= str_replace($('price').firstChild.nodeValue,',','.','trim');
		total		= (current_currency != 'EUR') ? get_eur_price(current_currency,price_value) : price_value;
		set_current_currency(currency);
		$('price').firstChild.nodeValue = str_replace(get_total(1,total,currency),'.',',','trim');
}

function init_lb_placeorder() {
	try {
		el_tracks = $('filelist').getElementsByTagName('input');
		count_tracks = get_tracks();
	
		$('tracks').firstChild.nodeValue 		= count_tracks;
		$('price').firstChild.nodeValue			= str_replace(get_price(count_tracks),'.',',','trim');
		$('totalprice').firstChild.nodeValue 	= str_replace(get_total(count_tracks,get_price(count_tracks),'EUR'),'.',',','trim') + ' EUR';
	
		if (bv() == -1) {
			for (a=0;a<el_tracks.length;a++) {
				if (el_tracks[a].getAttribute('type') == 'checkbox') {
					el_tracks[a].setAttribute('onclick','get_placeorder_total(currency.options[currency.options.selectedIndex].value)');
				}
			}
		
			$('currency').setAttribute('onclick','get_placeorder_total(currency.options[currency.options.selectedIndex].value)');
		} else {	
			for (a=0;a<el_tracks.length;a++) {
				if (el_tracks[a].getAttribute('type') == 'checkbox') {
					el_tracks[a].onclick = new Function("get_placeorder_total(this.form.currency.options[this.form.currency.options.selectedIndex].value)");
				}
			}
			$('currency').onclick = new Function("get_placeorder_total(this.options[this.options.selectedIndex].value)");
		}
	} catch(e) {
		// fail silently
	}
}
