﻿
var productID=null;//商品ID
var lodo_hp_list=[];//商品节日列表
var goods_price_list={};//商品原价格信息

function dayset(year,month)
{
	switch(month)
	{
		case "2":{
			jQuery("#hp_day>option[value='31'],#hp_day>option[value='30'],#hp_day>option[value='29']").remove();
			if((year%4==0&&year%100!=0)||(year%400==0)){jQuery("#hp_day").append("<option value='29'>29</option>");}
			break;
			}
		case "1":
		case "3":
		case "5":
		case "7":
		case "8":
		case "10":
		case "12":{
			if(jQuery("#hp_day>option[value='31']").size()<=0){jQuery("#hp_day").append("<option value='31'>31</option>");}
			break;
			}
		case "4":
		case "6":
		case "9":
		case "11":{
			jQuery("#hp_day>option[value='31']").remove();
			break;
			}
		
		}
	
	
	
	}

jQuery(function(){
	//加载节日信息
	//var tmpul=jQuery("<ul></ul>");
	var tmphpul=jQuery("<ul></ul>")
	jQuery(lodo_hp_list).each(function(){
									   //var newli=jQuery("<li></li>");
									   //newli.html(this.hpname+"("+this.stime+"-"+this.etime+")节日期间价格有变化");
									   //判断今日是否此节日价格
									   var myDate=new Date();
									  var myyear=myDate.getFullYear();
									  var mymonth=myDate.getMonth()+1;
									  var myday=myDate.getDate();
									   var nowdate=myyear+"-"+mymonth+"-"+myday;
									   var sdate=this.stime;
									   var edate=this.etime;
									   if((nowdate>=sdate)&&(nowdate<=edate))
									   {
											jQuery("#pro_market_price").html(this.market);//市场价
											jQuery("#pro_siteprice_price").html(this.siteprice);//本站价
											jQuery("#pro_member_price").html(this.vipprice);//会员价
											jQuery("#pro_vip_price").html(this.viplevelprice);//VIP会员价
											jQuery("#pro_your_price").html(this.yourprice);//您的价格
											jQuery("#goods_your_price").val(this.yourprice);//您的价格
										   	//newli.css({"background-color":"#FF0000","color":"#FFFFFF"});
										   }
										   
									   var newhpli=jQuery("<li stime='"+this.stime+"' etime='"+this.etime+"'>("+this.stimeinfo+"-"+this.etimeinfo+")"+this.hpname+"价格："+this.market+"元		现在订购优惠价:"+this.yourprice+"元	<input class='btn_goods_hp_add' type=button name='btn_goods_hp_add' id='btn_goods_hp_add_"+this.id+"' value='点此订购"+this.hpname+"'></li>");
									   tmphpul.append(newhpli);

									   });
	//jQuery("#hp_html_title").append(tmpul);
	jQuery("#hp_html_title").show();
	jQuery("#buy_goods_hp_info").append(tmphpul);
	
	jQuery("input:button[name='btn_goods_hp_add']").click(function(event){
																   var GoodsNum=jQuery("#GoodsNum"+productID+"").val();
																   var url="addGoodsCar.asp?Gid="+productID+"&GoodsNum"+productID+"="+GoodsNum+"";
																   jQuery("select.box[name^='selOA']").each(function(){url+="&"+this.id+"="+escape(jQuery(this).val());});
																	   var myDate=new Date();
																	  var myyear=myDate.getFullYear();
																	  var mymonth=myDate.getMonth()+1;
																	  var myday=myDate.getDate();
																	   var nowdate=myyear+"-"+mymonth+"-"+myday;
																	   var sdate=jQuery(this).parent().attr("stime");
																	   var edate=jQuery(this).parent().attr("etime");
																	   if((nowdate<sdate)||(nowdate>edate))
																	   {
																		   var t=sdate.split("-");
																		   myyear=t[0];
																		   mymonth=t[1];
																		   myday=t[2];
																		   }
																	   
																   url+="&hp_year="+myyear;
																   url+="&hp_month="+mymonth;
																   url+="&hp_day="+myday;
																   submitform(event,url);
																   });
	
	
	//为送达时间设置事件
	jQuery("select#hp_year,select#hp_month,select#hp_day").change(function(){
																		   
																	if(this.id!="hp_day"){dayset(jQuery("select#hp_year").val(),jQuery("select#hp_month").val());}	   
																	
																	var tmp_select_date=jQuery("select#hp_year").val()+"-"+jQuery("select#hp_month").val()+"-"+jQuery("select#hp_day").val();
																	
																 	var tmp_hp_list=jQuery.grep(lodo_hp_list,function(n,i){
																													  return tmp_select_date>=n.stime&&tmp_select_date<=n.etime;
																													  })[0];

																	if(tmp_hp_list)
																	{
																		//修改商品的价格
																		jQuery("#pro_market_price").html(tmp_hp_list.market);//市场价
																		jQuery("#pro_siteprice_price").html(tmp_hp_list.siteprice);//本站价
																		jQuery("#pro_member_price").html(tmp_hp_list.vipprice);//会员价
																		jQuery("#pro_vip_price").html(tmp_hp_list.viplevelprice);//VIP会员价
																		jQuery("#pro_your_price").html(tmp_hp_list.yourprice);//您的价格
																		jQuery("#goods_your_price").val(tmp_hp_list.yourprice);//您的价格
																		jQuery("#hp_html_title").html(""+tmp_hp_list.hpname+"("+tmp_hp_list.stime+"-"+tmp_hp_list.etime+")节日期间价格有变").show();
																		}
																	else{
																		jQuery("#pro_market_price").html(goods_price_list.market);//市场价
																		jQuery("#pro_siteprice_price").html(goods_price_list.siteprice);//本站价
																		jQuery("#pro_member_price").html(goods_price_list.vipprice);//会员价
																		jQuery("#pro_vip_price").html(goods_price_list.viplevelprice);//VIP会员价
																		jQuery("#pro_your_price").html(goods_price_list.yourprice);//您的价格
																		jQuery("#goods_your_price").val(goods_price_list.yourprice);//您的价格
																		jQuery("#hp_html_title").hide();
																		}
																	goods_total();
		});
	
	jQuery("select.box[name^='selOA']").change(goods_total);
	//jQuery("select#hp_day").change();
	goods_total();
	loadgoodscommentinfo();
	jQuery("#btn_goodscomment").click(function(){
		var options={
			url:"/savecomment.asp?bn="+Math.random(),
			dataType:"json",
			success:handle,
			beforeSubmit:checkform,
			resetForm:true
			};
		jQuery("#UpComment").ajaxSubmit(options);
		return false;
	});

	
})

function goods_total()
{
	var OA_price=0;
	jQuery("select.box[name^='selOA']>option:selected[value*='@']").each(function(){
					var tmp_split=this.value.split("@");	
					var tmp_price=tmp_split[1].replace("元","");
					tmp_price*=1;
					OA_price+=tmp_price;
													  });
	var goods_your_price=jQuery("#goods_your_price").val();
	var goods_your_points=jQuery("#goods_your_points").val();
	var GoodsNum=jQuery("#GoodsNum"+productID).val();
	goods_your_price*=1;
	goods_your_price+=OA_price;
	jQuery("#goods_total_price").html(eval(goods_your_price*GoodsNum));
	jQuery("#goods_total_points").html(eval(goods_your_points*GoodsNum));
	}


//加载商品点评
function loadgoodscommentinfo()
{
	jQuery("#goodsloading").show();
	jQuery.get("ajax_data.asp?action=goodscommentinfo&gid="+productID+"&bn="+Math.random(),function(data){
															jQuery("#ob_codeimg").click();
														   jQuery("#goods_comment_list").html(data);
														   jQuery("#goodsloading").hide();
														   })
	}

function checkform(formdata,jqForm,options)
{
	if(jQuery("#UpComment #CName").val()=="")
	{
		alert("请留下您的大名,谢谢！");
		jQuery("#UpComment #CName").focus();
		return false;
		}
	if(!checkemail(jQuery("#UpComment #CEmail").val()))
	{
		alert("请输入一个正确的E-mail地址！");
		jQuery("#UpComment #CEmail").focus();
		return false;
		}
	if(jQuery("#UpComment #Cyode1").val()=="")
	{
			alert("验证码不能为空！");
			jQuery("#UpComment #Cyode1").focus();
			return false;
		}
	if(jQuery("#UpComment #Contents").val()=="")
	{
			alert("请输入要留言的内容！");
			jQuery("#UpComment #Contents").focus();
			return false;
		}
	
	}

function handle(data)
{
		if(data.errno){alert(data.msg);if(data.gotono){location.href=data.gotourl};return false;}
		if(data.geno==1){alert("商品评论成功，请等待审核通过!");}else{alert("商品评论成功!");loadgoodscommentinfo();}
		return false;
}


function good_cal(s,Gid) 
{ 
var tempGN=document.getElementById('GoodsNum'+Gid); 
if ((eval(tempGN.value)+eval(s))>0) 
{ 
tempGN.value=eval(tempGN.value)+eval(s); 
goods_total();
}
} 
function isnumber(e) 
{ 
var patrn=/^[0-9]{1,20}$/; 
if (!patrn.exec(e.value)) {e.value=1;alert('只能由数字组成!');return false;} 
return true; 

} 



function chknumber(num)
{
var patrn=/^[0-9]{1,20}$/;
if (!patrn.exec(num))
	{
		return false;
	}
 return true;	
} 
function chkDozen(e,lv)
{
if(!chknumber(e.value)){
		alert("购买数量只能由整数组成！");
		e.value=lv;
		e.focus();
}

if ( e.value<=0 || (e.value % lv) != 0)
	{
		alert("购买数量只能是"+lv+"的整数倍！");
		e.value=lv;
		e.focus();
	}
goods_total();
}