﻿// JavaScript Document
/* Create a new XMLHttpRequest object to talk to the Web server */
var xmlHttp = false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
  try {
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (e2) {
    xmlHttp = false;
  }
}
@end @*/
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
  xmlHttp = new XMLHttpRequest();
}

function Float(str)
{var LarCode=document.fmsearch.LarCode;
	self.location='#';
	if(str=='off')
	{
	document.getElementById('floatLayerFather').style.display='none';
	document.getElementById('floatLayer').style.display='none';
	
	if(LarCode!=null)
	{LarCode.style.display='block';}
	//清空floatChildCH
	//document.getElementById('floatChildCH').innerHTML='';
	}
	else
	{document.getElementById('floatLayerFather').style.display='block';
	document.getElementById('floatLayer').style.display='block';
	if(LarCode!=null)
	{LarCode.style.display='none';}
	}
}

function KeyPress(objTR)
{//只允許輸入數字及小數點
		//var objTR = element.document.activeElement;		
		var txtval=objTR.value;		
		
		var key = event.keyCode;
		if((key < 48||key > 57)&&key != 46)
		{		
			event.keyCode = 0;
		}
		else
		{
			if(key == 46)
			{
				if(txtval.indexOf(".") != -1||txtval.length == 0)
					event.keyCode = 0;
			}
		}
}

//開始商品處理函數 by Kmars
//構造獲取型號，應該說是商品的編號才對。然後是它的種類，還有就是價格方案。返回格式為 ‘編號##型號##商品名稱##價格方案##商品種類'
//提交格式為'id&價格方案&數量
var getinfo;
function getPro(ProdId)
{
	getinfo='';//清空值
	// Build the URL to connect to
	var url = "comm/app.asp?act=1&ProdId="+escape(ProdId);
	// Open a connection to the server
  xmlHttp.open("GET", url, true);
  // Setup a function for the server to run when it's done
  xmlHttp.onreadystatechange = updatePage;
  // Send the request
  xmlHttp.send(null);
	
	}
	function updatePage() {
  if (xmlHttp.readyState == 4) {
    var response = xmlHttp.responseText;
	if(response=="ERR")
    {alert('您的請求非法');}
	else
	{getinfo=response;
		ProProcess();}
  }
}

//下面開始對商品的信息進行處理
	function ProProcess(ProId,pricetype,quality)
	{//alert(getinfo);
	//返回格式為 ‘編號##型號##商品名稱##價格方案##商品種類'
	//輸出到頁面,最後顯示出來
	
	//ProdId,Model,ProdName,PriceItem,ProdDisc
	//FormR 為顯示內容
	//Model為型號
	//ProdDisc商品種類，如一類
	//MsgShow為顯示的結果提示
	//ProdName商品的名稱
	//PriceItem價格表
	
	var templist=getinfo.split("##");//取出元素
	var floatChildCH=document.getElementById('floatChildCH');//首先獲得各個對象
	var Model=document.getElementById('Model');
	var ProdDisc=document.getElementById('ProdDisc');
	var ProdName=document.getElementById('ProdName');
	var MsgShow=document.getElementById('MsgShow');
	var TabContent=document.getElementById('TabContent');
	//首先初始化
	
	Model.innerHTML=templist[1];//賦值
	ProdName.innerHTML=templist[2];
	ProdDisc.innerHTML=templist[4];
	var PriceItem=templist[3].split("|");//返回值類似'170$$2.2$$0$$1|0$$0$$0$$0|0$$0$$0$$1'
	var i;
	var tmpvalue,tempstr,Price,RP,eV;
	var IsCuhiao=0;
	if(PriceItem[1].split("$$")[3]!=0){IsCuhiao=1}
	for(i=0;i<PriceItem.length;i++)
	{
		
		//alert(PriceItem[i]); //監視返回值
		//輸出各類價格數據
		tmpvalue=PriceItem[i].split("$$");
		//mycurrent_row = document.createElement("tr"); 
		//清空chd
		chd='';
		if(tmpvalue[3]!=0)
		{
			if(quality==null||quality<1)
				{quality=1;}
			//開始初始化計算
			if(i==IsCuhiao)
			{chd=' checked="true" ';}
			else
			var defaulslt; //默認的值,用於選定
			var defaulprice;//缺省值
			var defauleV,defaulRP
			
			Price=tmpvalue[0];eV=quality*tmpvalue[1];RP=tmpvalue[2];
			Price=Math.round(Price.replace("￥",""));
			switch(i)
		{
			case 0:PriceName='原價';defaulslt=PriceName;defauleV=eV;defaulRP=RP;defaulprice=Price;break;
			case 1:PriceName='促銷價';defaulslt=PriceName;defauleV=eV;defaulRP=RP;defaulprice=Price;break;
			case 2:
			PriceName='RP價';
			Price=(PriceItem[0].split("$$"))[0].replace("￥","");
			Price=Math.round((Price)*0.7);//一類二類打六折-->2008.10.1改七折
			
			if(backclass(templist[4])==1)
			{RP=quality*2;}
			else if(backclass(templist[4])==3)
			{RP=quality*1;Price=Math.round(PriceItem[0].split("$$")[0].replace("￥","")*0.85);}//三類打八折-->2008.10.1改八五折
			else
			{RP=quality*1;}
			break;
			}
			//alert(PriceItem[i]); //監視返回值
			
			tempstr=tempstr+'<TR><TD align="middle" bgColor="#ffffff" width="10%"><INPUT id="radio'+i+'" type="radio" '+chd+' value="'+i+'" name="PriceItem" onclick="updat('+i+',document.getElementById(\'Quatity'+i+'\'))"><\/TD><TD align="middle" bgColor="#ffffff" width="45%">'+PriceName+'<\/TD><TD align="middle" bgColor="#ffffff" width="13%">'+Price+'<\/TD><TD align="middle" bgColor="#ffffff" width="10%"><span id="eV'+i+'">'+eV+'</span><\/TD><TD align="middle" bgColor="#ffffff" width="10%"><span id="RP'+i+'">'+RP+'</span><\/TD><TD align="middle" bgColor="#ffffff" width="12%"><INPUT onfocus="document.getElementById(\'radio'+i+'\').checked=true;this.select()" type="text" size="6" value="1" name="Quatity'+i+'" onChange="updat('+i+',this)" onclick="updat('+i+',this)" onkeypress="javascript:KeyPress(this);" id="Quatity'+i+'" \/><\/TD> <\/TR>';
			//alert(tempstr);
			chd='';
		}
		//更換內容
		
	//顯示出來彈示窗
	
	}

	MsgShow.innerHTML='您現在選擇的是「<span class="style4">'+defaulslt+'</span>」．小計<font color="B0266D" size="+1" style="text-decoration:underline">NT: '+defaulprice+'&nbsp;</font>元<br />回饋eV:<font color="blue" size="+1" style="text-decoration:underline">'+defauleV+'</font>&nbsp;&nbsp;&nbsp;扣除RP:<font color="blue" size="+1" style="text-decoration:underline">'+defaulRP+'</font>';
	tempstr='<table width="95%" border="0" cellpadding="0" cellspacing="1" bgcolor="#999999"'+tempstr+'</table><div id="savetemplist">'+getinfo+'</div>';
	TabContent.innerHTML=tempstr;
	//floatChildCH.innerHTML=floatChildCH.innerHTML.replace("$內容$",tempstr);
	Float('on');
	tempstr='';
}
	 
function backclass(ProdDisc)
{
	 if(ProdDisc.indexOf("一類")!=-1)
      {return 1;}
	  else if(ProdDisc.indexOf("二類")!=-1)
      {return 2;}
	  else if(ProdDisc.indexOf("三類")!=-1)
      {return 3;}
	  else
	  {return 0;}
	}
function updat(pricetype,obj)
{
	//開始識別
	var ObjeV=document.getElementById('eV'+pricetype);
	var ObjRP=document.getElementById('RP'+pricetype);
	var pricelist=document.getElementById('savetemplist');
	var MsgShow=document.getElementById('MsgShow');
	var ProdDisc=pricelist.innerText.split('##')[4];
	var DefaulPrice=pricelist.innerText.split('##')[3].split('|')[0].split('$$')[0];
	DefaulPrice=DefaulPrice.replace("￥","")
	pricelist=pricelist.innerText.split('##')[3].split('|')[pricetype];
	
	
	//alert((1.2*3).toFixed(1));
	//下面變得簡單,僅需更新eV,及RP計數和結果
	var eV,Price,RP,PriceName
	Price=pricelist.split('$$')[0];
	Price=Price.replace("￥","");
	Price=parseInt(obj.value)*Math.round(Price);
	
	eV=parseInt(obj.value)*pricelist.split('$$')[1];
	eV=eV.toFixed(1)
	RP=parseInt(parseInt(obj.value)*pricelist.split('$$')[2]);
	switch(pricetype)
	{
		case 0:
			//這里只要變更eV及結果即可
			//得到價格和eV量
			PriceName='原價';
			
		break;
		
		case 1:
			PriceName='促銷價';
		break;
		case 2:
			PriceName='RP價';
			Price=parseInt(obj.value)*Math.round(DefaulPrice*0.7);//一類二類打六折-->2008.10.1改七折
			if(backclass(ProdDisc)==1)
			{RP=Math.ceil(parseInt(obj.value)*1.5);}
			else if(backclass(ProdDisc)==0)
			{RP=0;Price=DefaulPrice;}
			else
			{
				if(DefaulPrice>100)
				{RP=Math.ceil(parseInt(obj.value)*1);}
				else
				{RP=Math.ceil((parseInt(obj.value)/2)*1);}
				if(backclass(ProdDisc)==3)
				{Price=parseInt(obj.value)*Math.round(DefaulPrice*0.85);//三類打八折-->2008.10.1改八五折
				//alert(DefaulPrice);
				}
			}

		break;
		}
		if(eV==0.0){eV=0;}
		if(PriceName=='RP價'){PriceName='RP價 (需有RP供扣除)';}
	MsgShow.innerHTML='您現在選擇的是「<span class="style4">'+PriceName+'</span>」．小計<font color="B0266D" size="+1" style="text-decoration:underline">NT: '+Price+'&nbsp;</font>元<br />回饋eV:<font color="blue" size="+1" style="text-decoration:underline">'+eV+'</font>&nbsp;&nbsp;&nbsp;扣除RP:<font color="blue" size="+1" style="text-decoration:underline">'+RP+'</font>';
	
	ObjeV.innerText=eV;
	ObjRP.innerText=RP;
	}
function SubEnd()
{
	//僅提供ID,數量及價格體系號
	var pricetype=getRadioValue('PriceItem');
	//alert(pricetype);
	var Quality=document.getElementById('Quatity'+pricetype).value;
	var pricelist=document.getElementById('savetemplist');
	var ProId=pricelist.innerText.split('##')[0];
	var url = "comm/app.asp?act=2&ProdId="+escape(ProId)+"&Quality="+escape(Quality)+"&pricetype="+escape(pricetype);
	xmlHttp.open("GET", url, true);
	xmlHttp.onreadystatechange = SubEndthis;
	xmlHttp.send(null);
	
	}
	function SubEndthis() {
  if (xmlHttp.readyState == 4) {
    var response = xmlHttp.responseText;
	if(response=="ERR")
    {alert('您的請求非法');}
	else
	{Float('off');}
  }
}

function getRadioValue(radioName){
 var obj=document.getElementsByName(radioName);
 for(var i=0;i<obj.length;i++){
  if(obj[i].checked){
   return obj[i].value;
  }
 }
}

document.writeln('<div id="floatLayerFather"></div><div id="floatLayer"><div id="floatChild"><div id="floatChildCH">');
document.writeln('<div style="position:relative;"><form action="" metod="post" name="MainWe"><table width="95%" border="0" cellpadding="0" cellspacing="1" bgcolor="#999999" style="margin:10px;margin-bottom:0;"><TR><TD bgColor="#cccccc" colSpan="6"><STRONG>型號:<span  id="Model"></span></STRONG><STRONG>種類:<span id="ProdDisc"></span></STRONG></TD></TR><TR><TD bgColor="#cccccc" colSpan="6"><STRONG >名稱:<span id="ProdName"></span></STRONG></TD></TR><TR><TD align="middle" width="10%" bgColor="#cccccc"><STRONG>選擇</STRONG></TD><TD align="middle" bgColor="#cccccc" width="45%"><STRONG>價格種類</STRONG></TD><TD align="middle" width="13%" bgColor="#cccccc"><STRONG>單價(NT)</STRONG></TD><TD align="middle" width="10%" bgColor="#cccccc"><STRONG>回饋eV</STRONG></TD><TD align="middle" width="10%" bgColor="#cccccc"><STRONG>使用RP</STRONG></TD><TD align="middle" width="12%" bgColor="#cccccc"><STRONG>數量</STRONG></TD></TR></table><div Id="TabContent"></div>');
document.writeln('<table width="95%" border="0" cellpadding="0" cellspacing="1" bgcolor="#999999"><TR><TD align="middle" bgColor="#ffffff" colSpan="6"><INPUT type="submit" value="更新商品" name="UpdateSub" onclick="return false;"><INPUT type="button" value="加入購物車" name="Addsub" onclick="SubEnd();"><INPUT onclick="Float(\'off\');" type="button" value="關閉視窗" name="closeme"></TD></TR></TABLE></form><DIV style="margin:5px;"><DIV id="MsgShow"></DIV><UL><LI><span class="style5">修改商品數量</span>，請輸入新的數量，再單擊「更新商品」按鈕</li><LI><span class="style5">選擇商品價格</span>，請點擊左側的單選項，再單擊「加入購物車」按鈕</li><LI><span class="style5">查看購物車</span>，請在關閉視窗後找到頁面頂部的「購物車」圖標,打開</LI></UL></DIV></div></div></div></div>');

