function $(id){
	return document.getElementById(id);
}

function getFile(url, callback, post_keys, post_values, xml){
	try{ var xmlHttp=new XMLHttpRequest(); }   // Firefox, Opera 8.0+, Safari
	catch (e){
		// Internet Explorer
		try{ var xmlHttp=new ActiveXObject('MSXML2.XMLHTTP.3.0'); }
		catch(e){
			try{ var xmlHttp=new ActiveXObject('MSXML3.XMLHTTP'); }
			catch(e){
				try{ var xmlHttp=new ActiveXObject('Msxml2.XMLHTTP'); }
				catch(e){
					try{ var xmlHttp=new ActiveXObject('Microsoft.XMLHTTP'); }
					catch(e){
						throw new Error("Could not find an XMLHttpRequest alternative.");
					}
				}
			}
		}
	}

	var post_string='';
	if(post_keys){
		if(!post_values){
			for(var i in post_keys)
				post_string+=escape(i)+'='+post_keys[i]+'&';
		}
		else
			for(var i=0;i<post_keys.length;i++)
				post_string+=escape(post_keys[i])+'='+escape(post_values[i])+'&';
		post_string=post_string.substring(0, post_string.length-1);
	}

	xmlHttp.open(post_string?'POST':'GET', url, callback?true:false);

	if(post_string){
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", post_string.length);
		xmlHttp.setRequestHeader("Connection", "close");
		var method='POST';
	}

	if(callback){
		xmlHttp.onreadystatechange=function(){
			if(xmlHttp.readyState==4){
				if(xmlHttp.status==200)
					callback(xml?xmlHttp.responseXML.documentElement:xmlHttp.responseText);
				else
					throw new Error('Page returned error '+xmlHttp.status+' '+xmlHttp.statusText);
			}
		}
	}

	xmlHttp.send(post_string);
	if(!callback)
		return xml?xmlHttp.responseXML.documentElement:xmlHttp.responseText;
	else
		return 'Wait for async';
}

function absPos(obj){
	var pos={
		'left': 0,
		'top': 0
	};
	do{
		pos['left']+=obj.offsetLeft;
		pos['top']+=obj.offsetTop;
	} while(obj=obj.offsetParent);
	return pos;
}

function puneCurs(params, rezultat){
	if(!rezultat){
		getFile('_serveme.php'+params, function(txt){ puneCurs(params, txt); });
		return;
	}
	var lines=rezultat.split('\r\n');
	// prima linie e rezultatul
	if(lines[0]!='OK'){
		alert(lines[0]);
		return;
	}
	// a doua linie e moneda/banca
	$('val').tHead.rows[0].cells[1].innerHTML=lines[1];
	// a treia linie e graficul
	$('graph').src=lines[2];
	// apoi rezultatele
	for(var i=3;i<lines.length;i++){
		var row=lines[i].split('\t');
		for(var j=0;j<row.length;j++)
			$('val').tBodies[0].rows[i-3].cells[j].innerHTML=row[j];
	}
}

onload=function(){
	if($('val'))
		for(var i=0;i<$('val').tBodies[0].rows.length;i++){
			$('val').tBodies[0].rows[i].mod=i%2;
			$('val').tBodies[0].rows[i].marked=false;
			$('val').tBodies[0].rows[i].onmouseover=function(){
				if(!this.marked)
					this.className='r'+this.mod+'h';
			}
			$('val').tBodies[0].rows[i].onmouseout=function(){
				if(!this.marked)
					this.className='r'+this.mod;
			}
			$('val').tBodies[0].rows[i].onclick=function(){
				this.marked=!this.marked;
				this.className='r'+this.mod+(this.marked?'m':'');
			}
		}

	// put the menu where it belongs
	var i=0;
	while($('n'+(++i))){
		if(!$('sn'+i))
			continue;
		$('n'+i).oBBorder=$('n'+i).style.borderBottom;
		$('n'+i).onmouseover=function(){
			this.style.borderBottom='1px #f8f8f8 solid';
			$('s'+this.id).style.display='block';
		}
		$('n'+i).onmouseout=function(){
			this.style.borderBottom=this.oBBorder;
			$('s'+this.id).style.display='none';
		}
		$('sn'+i).onmouseover=function(){ $(this.id.substring(1)).onmouseover(); }
		$('sn'+i).onmouseout=function(){ $(this.id.substring(1)).onmouseout(); }
		$('sn'+i).style.left=$('n'+i).offsetLeft+'px';
		$('sn'+i).style.display='none';
	}

	if($('sub')){
		for(var i=0;i<$('sub').childNodes.length;i++){
			if($('sub').childNodes[i].nodeName.toUpperCase()=='A'){
				$('sub').childNodes[i].onclick=function(){
					puneCurs(this.search+'&filter='+$('sub').className);
					return false;
				}
			}
		}
	}

	// sunt la convertoru' valutar
	if(typeof(cvCurs)=='object'){
		$('vand').onchange=cvLoad;
		$('cumpar').onchange=cvLoad;
		$('moneda').onchange=cvLoad;
		$('suma').onkeyup=function(){
			var s=parseFloat(this.value);
			if(isNaN(s))
				s=0;
			for(var i in cvCurs)
				$('b'+i).cells[1].innerHTML=cvCurs[i]==0?'-':(s*cvCurs[i]).toFixed(4)+' <small>RON</small>';
		}
		$('moneda').onchange();
		$('suma').onkeyup();
	}
}

/* functiile care tin de convertoru' valutar */
function cvLoad(feed){
	if(typeof(feed)!='string'){
		getFile('_serveme.php?t='+($('vand').checked?'v':'c')+'&v='+$('moneda').value, function(txt){ cvLoad(txt); });
		return;
	}
	for(var i in cvCurs)
		cvCurs[i]=0;
	var lines=feed.split('\r\n');
	for(var i=0;i<lines.length;i++){
		if(lines[i].length==0)
			continue;
		var tmp=lines[i].split('\t');
		cvCurs[tmp[0]]=tmp[1];
	}
	for(var i in cvCurs){
		if(i=='RBNR')
			continue;
		else if(cvCurs[i]==0)
			$('b'+i).cells[2].innerHTML='-';
		else
			$('b'+i).cells[2].innerHTML=(cvCurs[i]/cvCurs['RBNR']*100-100).toFixed(4)+' %';
	}
	$('suma').onkeyup();
}

/*
function cvNumber(num){
	if(!num)
		return '0.0000';
	var s=''+Math.round(num*10000);
	return s.replace(/(.+)(....)$/, '$1.$2');
}
*/
