var arrSmokeProduct = new Array();
var arrSmokeCnt = new Array();
var arrProduct = new Array();
var arrProductContent = new Array();
var moneys=0;
var yandaimoneys=0;
function addsmoke(){

	var smokeval = 0;
	var money = 0;
	var content = '';
	var smoke = document.getElementById('smoke');
	var number = document.getElementById('number');
	var smokeview = document.getElementById('smokeview');
	var smokevalue = smoke.options[smoke.selectedIndex].value.split('|');
	var smokeid = smokevalue[0];
	var smokeprice = smokevalue[1];
	var smoketext = smoke.options[smoke.selectedIndex].text;

moneys=moneys+Number(smokeprice*Number(number.value));
document.getElementById('moneys').value=moneys;

    if(arrProduct[smokeid]){
		arrSmokeCnt[smokeid] =Number(arrSmokeCnt[smokeid])+Number(number.value);
	}else{
		arrProduct[smokeid] = true;
		arrSmokeCnt[smokeid] = number.value;
	}

	if(arrSmokeCnt[smokeid]>0){
		arrSmokeProduct[smokeid] = '→'+smoketext+'*'+arrSmokeCnt[smokeid]+'个'+'。小计：'+smokeprice*arrSmokeCnt[smokeid]+'元 <input type="button" value="删除" onclick="delSmoke('+smokeid+','+smokeprice+')">';
		arrProductContent[smokeid] = '→'+smoketext+'*'+arrSmokeCnt[smokeid]+'个'+'。小计：'+smokeprice*arrSmokeCnt[smokeid]+'元';
	}

    for(smokeval in arrSmokeProduct){
		if(arrSmokeProduct[smokeval]){
			content += arrSmokeProduct[smokeval]+'<br>';
		}
	}
	smokeview.innerHTML = content;
	number.value=1;
	Smokes(arrProductContent);
}
function delSmoke(id,obj){

	
	moneys=moneys-Number(obj*arrSmokeCnt[id]);
	document.getElementById('moneys').value=moneys;
	
	var smokeval = 0;
	var content = '';
	var smokeview = document.getElementById('smokeview');
	arrSmokeProduct[id] = '';
	arrProductContent[id] ='';
	for(smokeval in arrSmokeProduct){
		if(arrSmokeProduct[smokeval]){
			content += arrSmokeProduct[smokeval]+'<br>';
		}
	} 
	arrProduct[id] = '';
	arrSmokeCnt[id] = 0;
	smokeview.innerHTML = content;
	Smokes(arrProductContent);
}
function Smokes(arr){
 
	var smokeval = 0;
	var content = '';
	var smokes = document.getElementById('smokes');
	for(smokeval in arr){
		if(arr[smokeval]){
			content += arr[smokeval]+"\n";
		}
	}
    smokes.value = content;
}

var arrYandaiProduct = new Array();
var arrYandaiCnt = new Array();
var arrYandaiValue = new Array();
var arrYandaiContent = new Array();
function addyandai(){
	var yandaival = 0;
	var content = '';
	var yandai = document.getElementById('yandai');
	var yandai_cnt = document.getElementById('yandai_cnt');
	
	var yandaiview = document.getElementById('yandaiview');
	var yandaivalue = yandai.options[yandai.selectedIndex].text;
	
	var yandaiwei = document.getElementById('yandaiwei');
	var yandaiweivalue = yandaiwei.options[yandaiwei.selectedIndex].text;
	
	var smokevalue = yandai.options[yandai.selectedIndex].value.split('|');
var smokevalue1 = yandaiwei.options[yandaiwei.selectedIndex].value.split('|');
	var smokeprice = smokevalue[1];
	
yandaimoneys=yandaimoneys+Number(smokeprice*Number(yandai_cnt.value));
document.getElementById('yandaimoneys').value=yandaimoneys;

var ydid=Number(smokevalue[0])+Number(smokevalue1[0]);
    if(arrYandaiValue[ydid]){
		arrYandaiCnt[ydid] = Number(arrYandaiCnt[ydid])+Number(yandai_cnt.value);
	}else{
		arrYandaiValue[ydid] = true;
		arrYandaiCnt[ydid] = yandai_cnt.value;
	}

	if(arrYandaiCnt[ydid]>0){
		arrYandaiProduct[ydid] = '→'+yandaivalue+'*'+yandaiweivalue+'*'+arrYandaiCnt[ydid]+'盒小计：'+smokeprice*arrYandaiCnt[ydid]+'元 <input type="button" value="删除" onclick="delyandai('+ydid+','+smokeprice+')">';
		arrYandaiContent[ydid] = '→'+yandaivalue+'*'+yandaiweivalue+'*'+arrYandaiCnt[ydid]+'盒小计：'+smokeprice*arrYandaiCnt[ydid]+'元';
	}

    for(yandaival in arrYandaiProduct){
		if(arrYandaiProduct[yandaival]){
			content += arrYandaiProduct[yandaival]+'<br>';
		}
	}
	yandaiview.innerHTML = content;
	yandai_cnt.value=1;
	Yandais(arrYandaiContent);
}
function delyandai(id,obj){

		yandaimoneys=yandaimoneys-Number(obj*arrYandaiCnt[id]);
	document.getElementById('yandaimoneys').value=yandaimoneys;
	
	var yandaival = 0;
	var content = '';
	var yandaiview = document.getElementById('yandaiview');
	arrYandaiProduct[id] = '';
	arrYandaiContent[id] ='';
	

	
	
	for(yandaival in arrYandaiProduct){
		if(arrYandaiProduct[yandaival]){
			content += arrYandaiProduct[yandaival]+'<br>';
		}
	}
	arrYandaiProduct[id] = '';
	arrYandaiCnt[id] = 0;
	yandaiview.innerHTML = content;
	Yandais(arrYandaiContent);
}
function Yandais(arr){
	var yandaival = 0;
	var content = '';
	var yandais = document.getElementById('yandais');
	for(yandaival in arr){
		if(arr[yandaival]){
			content += arr[yandaival]+"\n";
		}
	}
    yandais.value = content;
}


function SubmitBuy(){
	var sv=document.getElementById("smokeview").innerHTML;//产品列表
	var as=document.getElementById("smokeadd");//产品添加按钮
	
	var yv=document.getElementById("yandaiview").innerHTML;//烟弹列表
	var ay=document.getElementById("yandaiadd");//烟弹添加按钮
	
	var ordName=document.getElementById("ordName");//订购人姓名
	var addre=document.getElementById("addre");//详细地址
	var postid=document.getElementById("postid");//邮政编码
	var handphone=document.getElementById("handphone");//移动电话
	var phone=document.getElementById("phone"); //固定电话
  
// $("form").submit(function(){
		if (sv==""&& yv==""){
			alert("请添加产品或烟弹");
			as.focus();
			return false;
		}
		else if (ordName.value==""){
			alert("请填写订购人姓名");
			ordName.focus();
			return false;
		}
		else if (addre.value==""){
			alert("请填写详细地址");
			addre.focus();
			return false;
		}
		else if (postid.value==""){
			alert("请填写邮政编码");
			postid.focus();
			return false;
		}
		else if (handphone.value=="" && phone.value==""){
			alert("移动电话、固定电话必须填写一个");
			handphone.focus();
			return false;
		}
		else if(confirm("请确认您所填写的资料：\n\n收货人姓名："+ordName.value+"\n\n移动　电话："+handphone.value+"\n\n固定　电话："+phone.value+"\n\n收货　地址："+addre.value+"\n\n物品："+document.getElementById("smokes").value+"\n\n烟弹："+document.getElementById("yandais").value)==1)
   {  
            return true  ;
   }  
   else {  return false;}
//	});
	
}
$(function(){
	function scrollAd(dom){
		var offset = $(window).height() - $(dom).height() + $(document).scrollTop() - 15;
		$(dom).animate({top:offset},{duration:300,queue:false});
	}	
	scrollAd("#p");
	$(window).scroll(function(){
		scrollAd("#p");
	 });
	$(window).resize(function(){
		scrollAd("#p");
	 });
});

function JHshNumberText()
{
   if ( !((window.event.keyCode >= 48) && (window.event.keyCode <= 57)))
   {
      window.event.keyCode = 0 ;
   }
} 
