var long = 6;
var currency = [];
currency['USD'] = 7.9871;
currency['EUR'] = 10.566933;
currency['UAH'] = 1;
;
function gebi(id) {
	return 	document.getElementById(id);
}

function curs(mode) {
	if(mode=='NBU') return '<b>1</b> USD = 7.9871 <span title="0.0300" class="dn">&darr;</span><br /><b>1</b> EUR = 10.566 <span title="0.5993" class="up">&uarr;</span><br />';
	if(mode=='MBU') return '<b>1</b> USD = 8.1352 <span title="0.0013" class="up">&uarr;</span><br /><b>1</b> EUR = 10.833 <span title="0.0642" class="up">&uarr;</span><br />';
}

function strstr(haystack, needle, bool) {	// Find first occurrence of a string
	// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	var pos = 0;

	pos = haystack.indexOf( needle );
	if( pos == -1 ){
		return false;
	} else{
		if( bool ){
			return haystack.substr( 0, pos );
		} else{
			return haystack.slice( pos );
		}
	}
}


function formula(c,e,d) {
	num = (100*c*e/d)/100;
	if(strstr(String(num),'.')) {
		_point = String(num).split('.');
		res = _point[0] + '.' + _point[1].substring(0,2);
	} else res = num;
	return res;
}

function formul2(c,e,d) {
	num = (100*c*d/e)/100;
	if(strstr(String(num),'.')) {
		_point = String(num).split('.');
		res = _point[0] + '.' + _point[1].substring(0,2);
	} else res = num;
	return res;
}

function testKey(e)
{
  var key = (typeof e.charCode == 'undefined' ? e.keyCode : e.charCode);

  if (e.ctrlKey || e.altKey || key < 32)
    return true;

  key = String.fromCharCode(key);
  return /[\d\.]/.test(key);
}

function cconvert(dc)
{
	d = document;
	fr = gebi('change_from').value;
	to = gebi('change_to').value;
	_fr = gebi('change_from_currency').value;
	_to = gebi('change_to_currency').value;
	_kurs = formula(1,currency[_fr],currency[_to]);
	if(_fr!=_to) {
		_html = '1 ' + _fr + ' = ' + _kurs + ' ' + _to;
		if(dc!=1) r = formula(fr,currency[_fr],currency[_to]);
		else r = formul2(to,currency[_fr],currency[_to]);
	} else {
		_html = '1 ' + _fr + ' = 1 ' + _to;
		if(dc!=1) r = fr;
		else r = to;
	}
	if(dc!=1) gebi('change_to').value = r;
	else gebi('change_from').value = r;
	gebi('result').innerHTML = _html;
}

function tableconstruct() {
	gebi('NBU').innerHTML = curs('NBU');
	gebi('MBU').innerHTML = curs('MBU');
}

window.onload = function() {
	//setTimeout ("tableconstruct", 1100);
	gebi('NBU').innerHTML = curs('NBU');
	gebi('MBU').innerHTML = curs('MBU');
}

