//urchin='';

//**************************//
//          Common          //
//**************************//

function $(element) {
	return document.all ?
		document.all[element]:
		document.getElementById(element);
}



//************************//
//          Ajax          //
//************************//

if (!window.XMLHttpRequest) {
   window.XMLHttpRequest = function() {
      var types = [
        'Microsoft.XMLHTTP',
        'MSXML2.XMLHTTP.5.0',
        'MSXML2.XMLHTTP.4.0',
        'MSXML2.XMLHTTP.3.0',
        'MSXML2.XMLHTTP'
       ];

       for (var i = 0; i < types.length; i++) {
         try             {
           return new ActiveXObject(types[i]);
         } catch(e) {}
       }

       return false; // XMLHttpRequest not supported
    }
}

function Ajax(url,options){
		
	this.setOptions=function(options) {
		this.options = {
			method:       'post',
			asynchronous: true,
			parameters:   ''
		}
		
		options=options || {};
		for (property in options){
			this.options[property] = options[property];
		}
	}
  
	this.getTransport=function() {
		return new XMLHttpRequest()
	}
	
	this.events=['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete'];
	
	this.responseIsSuccess=function() {
		return this.transport.status == undefined
			|| this.transport.status == 0
			|| (this.transport.status >= 200 && this.transport.status < 300);
	}
  
	this.request=function(url) {
    	var parameters = this.options.parameters || '';
		if (parameters.length > 0) parameters += '&_=';
	
		this.url = url;
		if (this.options.method == 'get' && parameters.length > 0)
			this.url += (this.url.match(/\?/) ? '&' : '?') + parameters;
	
		this.transport.open(this.options.method, url, this.options.asynchronous); // method, url, asynchronous
	
		if (this.options.asynchronous) {
			var _self = this;
			this.transport.onreadystatechange = function(){_self.onStateChange.call(_self)};
			setTimeout(function() {_self.respondToReadyState(1)}, 10);
		}
	
		this.setRequestHeaders();
	
		var body = this.options.postBody ? this.options.postBody : parameters;
		this.transport.send(this.options.method == 'post' ? body : null);
	}

	this.setRequestHeaders=function() {
    	var requestHeaders = ['X-Requested-With', 'XMLHttpRequest', 'X-Version', '1'];

		if (this.options.method == 'post') {
			requestHeaders.push('Content-type','application/x-www-form-urlencoded');

			if (this.transport.overrideMimeType)
        		requestHeaders.push('Connection', 'close');
	    }

	    if (this.options.requestHeaders)
    		requestHeaders.push.apply(requestHeaders, this.options.requestHeaders);

		for (var i = 0; i < requestHeaders.length; i += 2)
			this.transport.setRequestHeader(requestHeaders[i], requestHeaders[i+1]);
	},

	this.onStateChange=function() {
		var readyState = this.transport.readyState;
		if (readyState != 1)
			this.respondToReadyState(readyState);
	},


	this.evalResponse=function() {
      return eval(this.transport.responseText);
	},
	
	this.header=function(name) {
		return this.transport.getResponseHeader(name);
	}	

	this.respondToReadyState=function(readyState) {
    	var event = this.events[readyState];
    	var transport = this.transport;

    	if (event == 'Complete') {
			if ((this.header('Content-type') || '').match(/^text\/javascript/i))
				this.evalResponse();
				
			(this.options['on' + transport.status]
			 || this.options['on' + (this.responseIsSuccess() ? 'Success' : 'Failure')]
			 || function(){})(transport);			
		
			/* Avoid memory leak in MSIE: clean up the oncomplete event handler */
			if (event == 'Complete')
			  this.transport.onreadystatechange = function(){};
		}
	}
	
	this.transport = this.getTransport();
	this.setOptions(options);
	this.request(url);
}


//************************//
//          Main          //
//************************//

function twoDigits(v) {
	return (v>=10) ? v : ('0'+v);
}

function formatFigures(v) {
	copecks = Math.floor(v*100)%100;
	return Math.floor(v)+'<small>.'+twoDigits(copecks)+'</small>';
}

$('calc-button').onclick = function() {
	$('calc-table').style.display = 'block';
	selectedCurrency = $('calc-currency').value;
	
	amountOfficial = $('calc-amount').value;
	amountCross = amountOfficial;
	if (selectedCurrency!=defaultCurrency) {
		amountOfficial = amountOfficial * rates[selectedCurrency][0];
		if ($('calc-do').value=='sell') {			
			amountCross = amountCross * rates[selectedCurrency][1];
		} else {
			amountCross = amountCross * rates[selectedCurrency][2];
		}
	}
	
	for (rate in rates) {
		if (rate == selectedCurrency) {
			$('calc-'+rate).style.display = 'none';
		} else {
			$('calc-'+rate+'-official').innerHTML = formatFigures(Math.round(100 * amountOfficial / rates[rate][0]) / 100);
			if ($('calc-do').value=='sell') {
				$('calc-'+rate+'-cross').innerHTML = formatFigures(Math.round(100 * amountCross / rates[rate][2]) / 100);
			} else {
				$('calc-'+rate+'-cross').innerHTML = formatFigures(Math.round(100 * amountCross / rates[rate][1]) / 100);
			}
			$('calc-'+rate).style.display = 'block';
		}
	}
	
	var prm="do="+$('calc-do').value+
	        "&amount="+$('calc-amount').value+
			"&currency="+$('calc-currency').value;
	
	new Ajax(
		"statistic.php?type=calc",{postBody:prm}
		);
}

$('right').style.display = 'block'; // calc will be visible only for js-enabled browsers and only after function binding
