/*查询实时价格*/
function queryPA(stockId){
	//try{
		document.getElementById("row"+stockId).innerHTML = "<img src=\"/images/loading.gif\" />";
		var url = '/api/price.php?stockId='+stockId;			
		var newCallback = new Object();
		newCallback.success = successPriceRowFunction;
		newCallback.failure = failurePriceRowFunction;
		newCallback.timeout = 25000;
		newCallback.argument = stockId;	
		var transaction = YAHOO.util.Connect.asyncRequest('GET', url, newCallback);
	/*}catch{
		document.getElementById("row"+stockId).innerHTML = "通讯故障<a href='javascript:queryPA("+stockId+");'>重试</a>";
	}*/
}

function hidePA(stockId){
	var matRow = document.getElementById('price'+stockId);
  	matRow.innerHTML = "";
  	matRow.style.display = 'none';

	var priceButtonRow = document.getElementById("row"+stockId);
	priceButtonRow.innerHTML = "<a href=\"javascript:queryPA("+stockId+");\">查看</a>";
}

function queryST(packId){
	//try{
		
		var stock = document.getElementById("stock"+packId);
		stock.innerHTML = "<img src=\"/images/loading.gif\" />";
		
		//验证合法性
		var stockCount = document.getElementById("packge"+packId);

		if(checkRate(stockCount.value) == false){
			stock.innerHTML = "<span class=\"redfont\">输入有效的数量后点击查询</span>";
			return;
		}

		var url = '/api/stock.php?packId='+packId+'&req='+stockCount.value;		
		var newCallback = new Object();
		newCallback.success = successStockRowFunction;
		newCallback.failure = failureStockRowFunction;
		newCallback.timeout = 25000;
		newCallback.argument = packId;	
		var transaction = YAHOO.util.Connect.asyncRequest('GET', url, newCallback);
	/*}catch{
		document.getElementById("row"+stockId).innerHTML = "通讯故障<a href='javascript:queryPA("+stockId+");'>重试</a>";
	}*/
}

function addtocart(obj,stockId){
	//try{
		
		var postData = "dataclass=pid";
		var hasData = false;

		for(i=0;i<obj.elements.length;i++)
		{
			if(obj.elements[i].type == "text" && checkRate(obj.elements[i].value))
			{
				postData+="&packge["+obj.elements[i].name+"]="+obj.elements[i].value;
				hasData = true;
			}
		}

		//如果没有数据
		if(hasData == false)
		{
			alert("请先填写需要订购的数量");
			return false;
		}

		var url = '/topic.php?channelID=4&topicID=125&action=incart';		
		var newCallback = new Object();
		
		newCallback.success = successCartFunction;
		newCallback.failure = failureCartFunction;
		newCallback.timeout = 25000;
		newCallback.argument = stockId;	
		var transaction = YAHOO.util.Connect.asyncRequest('POST', url, newCallback,postData);
		return false;
	/*}catch{
		document.getElementById("row"+stockId).innerHTML = "通讯故障<a href='javascript:queryPA("+stockId+");'>重试</a>";
	}*/
}

function addtocartstock(obj,stockId){
	//try{
		
		var postData = "dataclass=sid";
		var hasData = false;

		for(i=0;i<obj.elements.length;i++)
		{
			if(obj.elements[i].type == "text" && checkRate(obj.elements[i].value))
			{
				postData+="&stock["+obj.elements[i].name+"]="+obj.elements[i].value;
				hasData = true;
			}
		}

		//如果没有数据
		if(hasData == false)
		{return false;}
alert(postData);
		var url = '/topic.php?channelID=4&topicID=125&action=incart';		
		var newCallback = new Object();
		
		newCallback.success = successCartFunction;
		newCallback.failure = failureCartFunction;
		newCallback.timeout = 25000;
		newCallback.argument = stockId;	
		var transaction = YAHOO.util.Connect.asyncRequest('POST', url, newCallback,postData);
		return false;
	/*}catch{
		document.getElementById("row"+stockId).innerHTML = "通讯故障<a href='javascript:queryPA("+stockId+");'>重试</a>";
	}*/
}

/*统计首页促销的几个链接*/
function countCommend(id){
	//try{
		var url = '/api/countCommend.php?id='+id;			
		var newCallback = new Object();
		//newCallback.success = successPriceRowFunction;
		//newCallback.failure = failurePriceRowFunction;
		newCallback.timeout = 25000;
		newCallback.argument = id;	
		var transaction = YAHOO.util.Connect.asyncRequest('GET', url, newCallback);
	/*}catch{
		document.getElementById("row"+stockId).innerHTML = "通讯故障<a href='javascript:queryPA("+stockId+");'>重试</a>";
	}*/
}

function stringToArray(splitChar,sourceString){
	tarGetString = sourceString.split(splitChar);
	return tarGetString;
}


function successPriceRowFunction(o)
{	
	    var stockId = o.argument;
		var xmlDoc = o.responseText;
		
		if(xmlDoc == null ||xmlDoc==''){
		   failurePriceRowFunction(o);
		}
		else{

			var matRow = document.getElementById("price"+stockId);

  			matRow.innerHTML = xmlDoc;
			
  			matRow.style.display = '';
			
			var priceButtonRow = document.getElementById("row"+stockId);
			priceButtonRow.innerHTML = "<a href=\"javascript:hidePA("+stockId+");\">隐藏</a>";
		}
}
	
function failurePriceRowFunction(o)
{
		var stockId = o.argument;		
		var matRow = document.getElementById('row'+stockId);
		matRow.innerHTML = "请求超时 <a href='javascript:queryPA("+stockId+");'>重试</a>";
}


function successStockRowFunction(o)
{	
	    var packgeId = o.argument;
		var xmlDoc = o.responseText;
		if(xmlDoc == null ||xmlDoc==''){
		   failureStockRowFunction(o);
		}
		else{
			var matRow = document.getElementById("stock"+packgeId);
  			matRow.innerHTML = xmlDoc;

		}
}
	
function failureStockRowFunction(o)
{
		var packgeId = o.argument;		
		var matRow = document.getElementById('stock'+packgeId);
		matRow.innerHTML = "请求超时 <a href='javascript:queryST("+packgeId+");'>重试</a>";
}

function successCartFunction(o)
{	
	    var stockId = o.argument;
		var xmlDoc = o.responseText;
		if(xmlDoc == null ||xmlDoc==''){
		   failureCartFunction(o);
		}
		else{
			if(xmlDoc == "1")
			{
				//var matRow = document.getElementById('carttip'+stockId);
				//matRow.innerHTML = "您的项目已被添加到购物车。";
				window.location.href="/order/125/list";
			}
			
		}
}
	
function failureCartFunction(o)
{
		var packgeId = o.argument;		
		var matRow = document.getElementById('stock'+packgeId);
		matRow.innerHTML = "请求超时 <a href='javascript:queryST("+packgeId+");'>重试</a>";
}

//判断正整数  
function checkRate(value)  
{  
     var re = /^[1-9]+[0-9]*]*$/;  
  
     if (!re.test(value))  
     {
        return false;
     }else{return true;}
} 
