//****************************************************************************
function buildAjaxObj() {
	var ajaxobj = null;
	
	if(window.XMLHttpRequest) {
		ajaxobj = new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		try {
			ajaxobj = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(err) {
			try {
				ajaxobj = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(err) {}
		}
	}
	return ajaxobj;
}
//****************************************************************************
function load(url, divid) {
	var bustcachevar=1;
	var bustcacheparameter="";
	var ajaxobj = buildAjaxObj();
	var origdivid = divid;
		
	//default div id (when none is specified)
	if(!divid) {
		var divid = 'middle';
	}

	ajaxobj.onreadystatechange = function() { loadpage(ajaxobj, divid, url); };
  
  	//loading animation
	document.getElementById(divid).innerHTML='<center><img src="http://erx.erepublik.ws/images/loading.gif"></center>';

	if(bustcachevar) {
		bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime();
		ajaxobj.open('GET', url+bustcacheparameter, true);
		ajaxobj.send(null);
	}
}

function loadpage(ajaxobj, divid, origurl) {
	if(ajaxobj.readyState == 4 && (ajaxobj.status==200 || window.location.href.indexOf("http")==-1)) {
		document.getElementById(divid).innerHTML=ajaxobj.responseText;
	}
}
//****************************************************************************
function post(formid, posturl, divid) {
	var theForm = document.getElementById(formid);
	var query = '';
	var ajaxobj = buildAjaxObj();
	
	if(!divid) {
		var divid = 'middle';
	}
	      
	for(i=0; i<theForm.length; i++) {
		var newvalue = theForm.elements[i].value;
		newvalue = escape(newvalue);
		query = query+theForm.elements[i].name+'='+newvalue+'&'
	}
	      
	if(divid != 'response' && divid != 'magicbox') { 
		document.getElementById(divid).innerHTML = '<center><img src="http://erx.erepublik.ws/images/loading.gif" style="position: relative; top: 10px; padding-bottom: 10px;"></center>';
	}
	
	ajaxobj.onreadystatechange = function() {
		if(ajaxobj.readyState == 4) {
			document.getElementById(divid).innerHTML = ajaxobj.responseText; 
		}
	};
	      
	ajaxobj.open('POST', posturl, true);
	ajaxobj.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
	ajaxobj.send(query);
}
//****************************************************************************
function buySell(action) {
	if(action == 0) {
		document.getElementById("word1").innerHTML = 'Price:';
		document.getElementById("word2").innerHTML = 'Total Profit:';
		document.getElementById("sign").innerHTML = '-';
	} else {
		document.getElementById("word1").innerHTML = 'Max. Price:';
		document.getElementById("word2").innerHTML = 'Total Cost:';
		document.getElementById("sign").innerHTML = '+';
	}
}
//****************************************************************************
function loginerror() {
	var form_username = document.getElementById('username');
	form_username.style.background = '#FA1908';
	
	var form_password = document.getElementById('password');
	form_password.style.background = '#FA1908';
}
//****************************************************************************
function is_numeric(value) {
  if (value == null || !value.toString().match(/^[-]?\d*\.?\d*$/)) return false;
  return true;
}
//****************************************************************************
function updateSelection(id) {
	for(i=1;i<5;i++) {
		if('m' + i == id) {
			var obj = document.getElementById('m' + i);
			obj.style.color = '#3C8FA7';
		} else {
			var obj = document.getElementById('m' + i);
			obj.style.background = '#F4F4F4';
			obj.style.color = '#666';
		}
	}	
}
//****************************************************************************
function setPrice(val) {
	val = val.split('*');
	
	//set price
	document.getElementById('price').value = val[0];
	
	//if given, set quantity
	if(val[1]) {
		document.getElementById('quantity').value = val[1];
	} else {
		document.getElementById('quantity').value = '';	
	}
	totalCost();
}
//****************************************************************************
function totalCost() {

	var quantity = document.getElementById('quantity').value;
	var price = document.getElementById('price').value;
	var fee = document.getElementById('fee').innerHTML;

	if(quantity >= 0 && is_numeric(quantity) != false && price > 0 && is_numeric(price) != false) {
		quantity = document.getElementById('quantity').value = Math.round(quantity); //set new quantity
		price = document.getElementById('price').value = (Math.round((price)*1000)/1000).toFixed(3); //set new price
		document.getElementById('cost').innerHTML = Math.round((quantity * price)*1000)/1000; //set cost
		
		var cost = (Math.round((quantity * price)*1000)/1000).toFixed(3);
		var feecost = (Math.round((cost * (fee/100))*1000)/1000).toFixed(3);

	if(document.getElementById("sign").innerHTML == '+') {
		document.getElementById('totalcost').innerHTML = Math.round((parseFloat(cost) + parseFloat(feecost))*1000)/1000; //set total cost
	} else {
		document.getElementById('totalcost').innerHTML = Math.round((parseFloat(cost) - parseFloat(feecost))*1000)/1000; //set total cost
	}
	
	} else {
		document.getElementById('quantity').value = quantity;
		document.getElementById('cost').innerHTML = '0.000';
		document.getElementById('totalcost').innerHTML = '0.000';
	}
}
//****************************************************************************
function time() {
	var element = document.getElementById('ertime');
	var Digital = new Date();
	var hours=Digital.getHours();
	var minutes=Digital.getMinutes();
	var seconds=Digital.getSeconds();
	var dn="PM"
	
	if (hours<12)
	    { dn="AM" }
	if (hours>12)
	    { hours=hours-12 }
	if (hours==0)
	    { hours=12 }
	if (minutes<=9)
	    { minutes="0"+minutes }
	if (seconds<=9)
	    { seconds="0"+seconds }
	    
	var ctime=hours+":"+minutes+":"+seconds+" "+dn;
	element.innerHTML=ctime;
	setTimeout("time()",1000);
}
//****************************************************************************
function serverTime() {
	if(!window['curtime']) {
		window['curtime'] = document.getElementById('timestamp').innerHTML;	
	} else {
		window['curtime']++;
	}
	
	//format time 
	var d = new Date(window['curtime']*1000);
	var minutes = d.getMinutes();
	var seconds = d.getSeconds();
	
	if(minutes < 10) {	
		minutes = "0"+minutes;
	}
	
	if(seconds < 10) {	
		seconds = "0"+seconds;
	}
	
	var output = d.getHours() + ':' + minutes + ':' + seconds;
	
	//display time
	document.getElementById('servertime').innerHTML = output;
	setTimeout("serverTime();", 1000);
}