var priceReq = null;
var rpb=document.getElementById("refreshPrice");

function getRealPrice(IDStock,market){
	priceReq=createReq();
	if(priceReq){
		countTime(30);
		priceReq.open("GET","refreshPrice.inc.php?IDStock="+IDStock+"&market="+market,true);
		priceReq.onreadystatechange = handleGetRealPrice;
		priceReq.send(null);
	}
	
}
function handleGetRealPrice(){
	if(priceReq.readyState == 4&&priceReq.status == 200){	
		parseRealPriceResults();	
	}
}
function parseRealPriceResults(){
    var price=null;
	var change=null;
	var changerate=null;
	var value=null;
	var perEarning=null;
	var cost=null;
	var earningPrice=null;
	var dividendEarnings=null;
	var high=null;
	var low=null;
	var yesterdayPrice=null;
	var open=null;
	var DividendPayout=null;
	var properties = priceReq.responseXML.getElementsByTagName('property');
	if(properties){
	    for(var i = 0; i < properties.length; i++) {
			property = properties[i];
			price = property.getElementsByTagName("price")[0].firstChild.nodeValue;
			change = property.getElementsByTagName("change")[0].firstChild.nodeValue;
			changerate = property.getElementsByTagName("changerate")[0].firstChild.nodeValue;
			value = property.getElementsByTagName("value")[0].firstChild.nodeValue;
			perEarning = property.getElementsByTagName("perEarning")[0].firstChild.nodeValue;
			cost = property.getElementsByTagName("cost")[0].firstChild.nodeValue;
			earningPrice = property.getElementsByTagName("earningPrice")[0].firstChild.nodeValue;
			dividendEarnings = property.getElementsByTagName("shouYiLv")[0].firstChild.nodeValue;
			high = property.getElementsByTagName("topPrice")[0].firstChild.nodeValue;
			low = property.getElementsByTagName("downPrice")[0].firstChild.nodeValue;
            open = property.getElementsByTagName("open")[0].firstChild.nodeValue;
		    DividendPayout = property.getElementsByTagName("DividendPayout")[0].firstChild.nodeValue;
			yesterdayPrice =property.getElementsByTagName("yesterdayPrice")[0].firstChild.nodeValue;
		}
		if(price!= null){
			updateRealPriceItem(price,change,changerate,value,perEarning,cost,earningPrice,dividendEarnings,high,low,yesterdayPrice,open,DividendPayout);
		}
	}
}

function updateRealPriceItem(price,change,changerate,value,perEarning,cost,earningPrice,dividendEarnings,high,low,yesterdayPrice,open,DividendPayout){
	var color="#ccc";
	var mark="";
	if(change==0){
		mark="-";
		color="#ccc";
	}else if(change>0){
		mark="↑";
		color="red";
	}else{
		mark="↓";
		color="green";
		
		yesterdayPrice =(price-change).toFixed(3);
	}
	var guxi=document.getElementById("guxi").innerText;
	if(!guxi) guxi=document.getElementById("guxi").textContent;
	var xilv=0;
	guxi=parseFloat(guxi);
	price=parseFloat(price);
	if(parseInt(price)!=0) xilv=guxi/price;
	var v=price+"&nbsp;&nbsp;"+mark+"&nbsp;&nbsp;"+change;
	document.getElementById("last").innerHTML="<font color=\""+color+"\">"+price+"</font>";//现价
	document.getElementById("change").innerHTML="<font color=\""+color+"\">"+change+"</font>";//升跌$
	document.getElementById("changerate").innerHTML="<font color=\""+color+"\">"+changerate+"</font>";//升跌%
	document.getElementById("newPriceReport").innerHTML="<font color=\""+color+"\">"+price+" "+mark+" "+change+"</font>";//
	document.getElementById("uplow1").innerHTML="<font color=\""+color+"\">"+mark+"</font>";
	document.getElementById("uplow2").innerHTML="<font color=\""+color+"\">"+mark+"</font>";
	document.getElementById("uplow3").innerHTML="<font color=\""+color+"\">"+mark+"</font>";
	document.getElementById("perEarning").innerHTML=perEarning=="N/A"?"--":perEarning;//每股盈利
	document.getElementById("cost").innerHTML=cost;//市值
	document.getElementById("earningPrice").innerHTML=earningPrice=="N/A"?"--":earningPrice;//市盈率(倍)
	var c =parseInt(cost);
	var pb = getnum((price/c),2);
	document.getElementById("PB").innerHTML=pb;//市净率
	xilv=getFloat(xilv);
	document.getElementById("profitability").innerHTML=xilv;//息率
	document.getElementById("yesterdayPrice").innerHTML=yesterdayPrice;//上日收市
	if(high>open){
		document.getElementById("high").innerHTML=high;//今日最高
	}
	if(high=="--"){
		document.getElementById("high").innerHTML=high;//今日最高
	}
	document.getElementById("open").innerHTML=open;//开市价
	document.getElementById("low").innerHTML=low;//今日最低
	document.getElementById("DividendPerShare").innerHTML=DividendPayout;//派息比率
}
function countTime(waitSecond){
	rpb.disabled=!rpb.disabled;
	for(var i=0;i<=waitSecond;i++) {
		window.setTimeout("sTimer("+i+")",i*1000);
	}
}
function sTimer(second){
	if(second!=30){
	   secs=30-second;
       rpb.value="请等待"+secs+"秒"; 
	}else{
	   rpb.value="尝试实时";
	   rpb.disabled=!rpb.disabled;
	}
}
function createReq(){
	 var httpReq;
	 if(window.XMLHttpRequest){
		 httpReq = new XMLHttpRequest();
		 if(httpReq.overrideMimeType){
			 httpReq.overrideMimeType('text/xml');
		 }
	 }
	 else if(window.ActiveXObject){
		 try{
			  httpReq = new ActiveXObject('Msxml2.XMLHTTP');
		 } catch(e){
			 try{
			    httpReq = new ActiveXObject('Microsoft.XMLHTTP');
			 } catch(e2){
			 }
		 } 
	 }
	 return httpReq;
} 

/***********************************转换精度*************************************/
function getFloat(f){
	f=f.toString();
	var index=f.indexOf(".");
	var int=f.substring(0,index);
	var num=f.substring(index+6,index+7);
	var float=f.substring(0,index+6);
	var rs="";
	if(parseInt(num)>4){
		rs=(parseFloat(float)+0.00001)*100;
	}else
		rs=parseFloat(float)*100;
	rs=rs.toString();
	index=rs.indexOf(".");
	rs=rs.substring(0,index+4);
	return rs+"%";
}
function getnum(f, c)
{ 
	var t = Math.pow(10, c); 
	return Math.round(f * t) / t;
	//alert(getnum(12.3456789, 2));
}
