function showhide(e){ if(document.getElementById(e).style.display!='none') hide(e); else show(e); }
function show(e){ if(document.getElementById(e).style.display=='none') document.getElementById(e).style.display = ''; }
function hide(e){ if(document.getElementById(e).style.display!='none') document.getElementById(e).style.display = 'none'; }

function tab(kind,box){
	var con = document.getElementById('container'+(box ? '_'+kind : ''));
	if(!document.getElementById((box ? kind+'_' : '')+'main')){
		con.setAttribute('name','main');
		var div = document.createElement('div');
		document.body.appendChild(div);
		div.id = (box ? kind+'_' : '')+'main';
		div.className = 'hidden';
		div.innerHTML = con.innerHTML;
	}
	document.getElementById('tab'+(box ? kind : '')+con.getAttribute('name')).className = 'bg2';
	document.getElementById('tab'+kind+(box ? box : '')).className = '';
	con.innerHTML = document.getElementById(kind+(box ? '_'+box : '')).innerHTML;
	con.setAttribute('name',box ? box : kind);
}

function getTimeLeft(sec){
	h = Math.floor(sec/3600);
	m = Math.floor((sec-(h*3600))/60);
	s = Math.floor(sec-((h*3600)+(m*60)));
	if(h && m < 10) m = '0'+m;
	if((h || m) && s < 10) s = '0'+s;
	return (h ? h+':' : '')+(m ? m+':' : '')+s;
}

function updateStats(mph,vph,m,v){
	m += mph/36000;
	v += vph/36000;
	document.getElementById('cash').innerHTML = addCommas(Math.floor(m));
	for (var i = 2; i < 7; i++) if(document.getElementById('cash'+i)) document.getElementById('cash'+i).innerHTML = document.getElementById('cash').innerHTML;
	document.getElementById('volk').innerHTML = addCommas(Math.floor(v));
	setTimeout('updateStats('+mph+','+vph+','+m+','+v+')',100);
}

function addCommas(nStr){
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while(rgx.test(x1)) x1 = x1.replace(rgx, '$1' + '.' + '$2');
	return x1 + x2;
}

function ajax(str,val,repl,caller){
	var xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
	var result;
	xmlhttp.open("GET",'/ajax/'+str+'?val='+val+'&symbol=1&target='+repl+'&caller='+caller+'&rand='+Math.floor(Math.random()*1000),true); 
	xmlhttp.onreadystatechange = function(){ 
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
			if(repl) result = document.getElementById(repl).innerHTML = xmlhttp.responseText;
		}
	} 
	xmlhttp.send(null);
	return result;
}

function ajaxSearch(obj,box,mode){
	if(!obj.value){
		hide(box);
		return;
	}
	el = document.getElementById(box);
	el.style.width = (obj.offsetWidth-10)+'px';
	el.style.left = obj.offsetLeft+'px';
	el.style.top = (obj.offsetTop+obj.offsetHeight-1)+'px';
	result = ajax('search'+(mode ? '/'+mode : ''),obj.value,box,obj.id);
	if(result != '') show(box);
	else hide(box);
}
