/*17 june 2010 :: www.kennardlim.com*/
function ajaxpricing(symbols,spanid,attributes,period,varname)
{
	this.symbollist=symbols.split(",");
	this.symboltosend='';
	this.totalsymbol=this.symbollist.length;
	this.targetspan=spanid;
	this.period=period;
	this.attributelist=attributes.split(",");
	this.attribute=new Array();
	this.attribute['width']="100%";
	this.attribute['headercolor']="#C27C2A";
	this.attribute['alternatecolor1']="#FFFFFF";
	this.attribute['alternatecolor2']="#E3E1C8";
	this.attribute['font-family']="Lucida Sans Unicode,Lucida Grande,Sans-Serif";
	this.attribute['font-size']="12px";
	for (var i=0;i<this.attributelist.length;++i)
	{
		var att=this.attributelist[i].split("=");
		this.attribute[att[0]]=att[1];
	}
	this.bgcolor=new Array(this.attribute['alternatecolor1'],this.attribute['alternatecolor2']);
	this.XMLHttpRequestObject=false;
	if (window.XMLHttpRequest) this.XMLHttpRequestObject=new XMLHttpRequest();
	else if (window.ActiveXObject) this.XMLHttpRequestObject=new ActiveXObject("Microsoft.XMLHTTP");
	this.XMLHttpRequestObject.targetspan=this.targetspan;
	this.XMLHttpRequestObject.dirpic=new Array();
	this.XMLHttpRequestObject.dirpic[0]="<img src='./system/javascript/priceslib/up.gif' />";
	this.XMLHttpRequestObject.dirpic[1]="<img src='./system/javascript/priceslib/down.gif' />";
	this.createtable();
	setTimeout(varname+".getpriceperiodic()",0);
	this.interval=setInterval(varname+".getpriceperiodic()",this.period);
}
	ajaxpricing.prototype.createtable=function()
	{
		var content="<table class='ajaxprice' width='"+this.attribute['width']+"' style='font-family:"+this.attribute['font-family']+";font-size:"+this.attribute['font-size']+"'><tr bgcolor='"+this.attribute['headercolor']+"' align='center' style='font-style:bold;font-weight:700;color:#FFFFFF'><td>Symbol</td><td>Bid</td><td>Ask</td><td>Time</td></tr>";
		for (var i=0;i<this.totalsymbol;++i)
		{
			var s=this.symbollist[i];
			this.symboltosend=this.symboltosend+",'"+s+"'";
			content=content+'<tr bgcolor='+this.bgcolor[i%2]+'><td id=symbol.'+s+'.'+this.targetspan+'>'+s+'</td><td id=bid.'+s+'.'+this.targetspan+' align=right></td><td id=ask.'+s+'.'+this.targetspan+' align=right></td><td id=time.'+s+'.'+this.targetspan+' align=center></td>';
		}
		this.symboltosend=this.symboltosend.substring(1);
		content=content+"</table>";
		document.getElementById(this.targetspan).innerHTML=content;
	}
	ajaxpricing.prototype.getpriceperiodic=function()
	{
		if(this.XMLHttpRequestObject) 
		{
			this.XMLHttpRequestObject.open("GET","./system/javascript/priceslib/getprices.php?symbols="+this.symboltosend);
			//XMLHttpRequestObject.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			this.XMLHttpRequestObject.send(null);
			this.XMLHttpRequestObject.updateprice=function(superstring)
			{
				var lines=superstring.split("@");
				for (var i=0;i<lines.length-1;++i)
				{
					var data=lines[i].split("|");
					data[1]=Math.round(parseFloat(data[1])*Math.pow(10,data[7]))/Math.pow(10,data[7]);
					updatecolor('bid.'+data[0]+'.'+this.targetspan,data[1]);
					data[2]=Math.round(parseFloat(data[2])*Math.pow(10,data[7]))/Math.pow(10,data[7]);
					updatecolor('ask.'+data[0]+'.'+this.targetspan,data[2]);
					//document.getElementById('dir.'+data[0]+'.'+this.targetspan).innerHTML=this.dirpic[data[5]];
					var time=data[6].split(" ")[1];
					time=modifytime(time,-3,0,0);
					document.getElementById('time.'+data[0]+'.'+this.targetspan).innerHTML=time;
				}				
			}
			this.XMLHttpRequestObject.onreadystatechange=function()
			{
				if (this.readyState==4 && this.status==200)
				{
					this.updateprice(this.responseText);
				}
			}
		}
	}
	function modifytime(time,hour,minute,second)
	{
		var stime=time.split(":");

		stime[0]=parseInt(stime[0])+hour;
		if (stime[0]<0) stime[0]=24+stime[0];
		else if (stime[0]>24) stime[0]=stime[0]-24;
		if (stime[0]<10) stime[0]="0"+stime[0];

		stime[1]=parseInt(stime[1])+minute;
		if (stime[1]<0) stime[1]=60+stime[1];
		else if (stime[1]>60) stime[1]=stime[1]-60;
		if (stime[1]<10) stime[1]="0"+stime[1];

		stime[2]=parseInt(stime[2])+second;
		if (stime[2]<0) stime[2]=60+stime[2];
		else if (stime[2]>60) stime[2]=stime[2]-60;
		if (stime[2]<10) stime[2]="0"+stime[2];
		
		return stime[0]+":"+stime[1]+":"+stime[2];
	}
	function updatecolor(id,newvalue)
	{
		if (parseFloat(document.getElementById(id).innerHTML)>parseFloat(newvalue)) document.getElementById(id).style.background="#FFB9A8";
		if (parseFloat(document.getElementById(id).innerHTML)<parseFloat(newvalue)) document.getElementById(id).style.background="#A5F8CE";
		setTimeout("returncolor('"+id+"')",1000);
		document.getElementById(id).innerHTML=newvalue;
	}
	function returncolor(id)
	{
		document.getElementById(id).style.background="";
	}

/* how to use

<!-- index and forex prices Provide by: PT.Millennium Penata Futures-->
<style>
.ajaxprice td{ padding:1px 5px 1px 5px;}
</style>
<span id="spanidajaxprice1"></span><br>
<span id="spanidajaxprice2"></span><br>
<span style="font-family:arial; font-size:12px; padding-left:5px;">Prices provide by. Millennium Penata Futures.</span>
<span id="additionalinfo"></span>
<script>
	var a=new ajaxpricing("EURUSD,GBPUSD,USDJPY,USDCHF,AUDUSD,EURGBP,USDCAD,NZDUSD","spanidajaxprice1","width=350px,headercolor=#223e6e",15000,"a");
	var b=new ajaxpricing("HANGSENG,NIKKEI,KOSPI,_DJI,NASDAQ","spanidajaxprice2","width=350px,headercolor=#223e6e",15000,"b");
</script>
<!-- index and forex prices Provide by: PT.Millennium Penata Futures-->

*/

