//<script>
var request_module = 'mag';var request_job = 'mag';var request_action = 'content';
plugin('topdiv');
plugin('jquery>form');
includeCss(web_dir+'/css/loginbox.css');
var redirect;
var authfunction;
function auth(url)
{
	redirect=url;
	if(typeof(url) == 'function')
	{
		authfunction = url;
		redirect='javascript:authfunction()';
	}
	_auth(url,show_login);
}
function show_login()
{
	var html="<div class='loginbox' id='login_form'><div class='tit drag'><span>用户登录</span><span class='close'>关闭</span></div>"
	+"<div class='con'>"
	+'<form id="loginform" method="post" target="ifm_hidden_login" action="'+HOST_PASSPORT+'/index.php/login/fastlogin"><ul><li><span>用户名：</span><input type="text" name="username" id="username" validate="required:true" value="" size="20"></li><li><span>密&nbsp; 码：</span><input type="password" name="password" id="password"  value="" validate="required:true" size="20"><input name="redirect" id="redirect" value="'+redirect+'" type="hidden" /></li><li><input type="button"  onclick="login(\'loginform\');" id="login_btn" value="登  录" /></li><li><span id="login_error"></span></li></ul></form><iframe name="ifm_hidden_login" id="ifm_hidden_login" height="0" width="0" style="display:none "></iframe>'
									 +"</div>"

									 +"<div class='ft'></div></div>";
	topdiv.show(html,300,120);
	JQ('#loginform').validate();
}
function close_login()
{
	topdiv.hide();
}
function login(form)
{
	/*JQ('#'+form).validate({
		submitHandler:function() {
			//JQ('#login_btn').attr('disabled',true);
			//_login(ajax.getFormValues(form));
		}
	});*/
	JQ('#'+form).submit();
}
function init_login()
{
		user_id=0;user_name='';
		if(JQ('#login_status')[0])login_status();
}
function login_success(id,name)
{
		user_id=id;user_name=name;
		close_login();
		if(JQ('#login_status')[0])login_status();
}
function logout()
{
	//_logout();
	if(!JQ('#ifm_hidden_login')[0]) JQ('body').append('<iframe name="ifm_hidden_login" id="ifm_hidden_login" height="0" width="0" style="display:none;"></iframe>');
	JQ('#ifm_hidden_login').attr('src',HOST_PASSPORT+'/index.php/login/fastlogout?login_needed='+LOGIN_NEEDED);
}
function login_status()
{
	if(!JQ('#login_status')[0]) echo('<span id="login_status"></span>');
	JQ('#login_status').html((iflogin()?'<strong>'+user_name+'</strong>|<a href="/index.php/user/edit">修改资料</a>|<a href="javascript:logout();"><b>安全退出</b></a>':'<a href="javascript:show_login();">登录</a>|<a href="/index.php/user/register">注册</a>')+'|<a href="http://china.u.cn">看天下首页</a>|<a href="http://china.u.cn/html/help.htm">帮助</a>'); 
}
// JavaScript Document
/**
* shop cart
* author:gavin yang
* last mod:08/04/1
**/
plugin('topdiv');
plugin('jquery>date');
includeCss(web_dir+'/css/shopcart.css');
//var fast_menu = '<div id="fast_menu"><a href="javascript:shopCart.show();">购物车</a><br><a href="javascript:shopCart.order();">收银台</a></div>';
//JQ(function(){topdiv.show(fast_menu,200,200,JQ(document).scrollTop(),JQ(document).scrollLeft(),true,'fast_menu');});
var shopCart = {
	goodsNum: 0,
	cart_list:[],
	inited:false,
	getGoodsNum: function()
	{
		this.goodsNum = isNaN(parseInt($cookie('goodsNum'))) ? 0 : $cookie('goodsNum');
		//JQ('#goodsNumInCart').html(this.goodsNum);
		//return this.goodsNum;
		//_getGoodsNum();
	},
	setGoodsNum: function(num)
	{
		this.goodsNum = isNaN(parseInt($cookie('goodsNum'))) ? 0 : $cookie('goodsNum');
		//this.goodsNum = num;
		//JQ('#goodsNumInCart').html(this.goodsNum);
		//_setGoodsNum(num);
	},
	getCartList: function()
	{
		//_getCartList();//
		this.cart_list = $cookie('cart_list') ? JSON.parse($cookie('cart_list')) : [];
	},
	setCartList: function()
	{
		//_setCartList(this.cart_list,this.goodsNum);//
		$cookie('cart_list',JSON.stringify(this.cart_list));
		$cookie('goodsNum',this.goodsNum);
		//
		JQ('#goodsNumInCart').html(this.goodsNum);
	},
	add:function()//id,name,price,num,discount,start_date,min_date
	{
		if (!this.inited) this.init();
		var good_list = {'id':arguments[0],'name':arguments[1],'price':arguments[2],'num':parseInt(arguments[3]),'lid':arguments[4]};
		var newer = true;
		good_list['num'] = isNaN(good_list['num']) ? 0 : good_list['num'];
		if (this.cart_list)
		{
			var total_glist=this.cart_list.length;

			for (var i=0; i<total_glist; i++)
			{
				if (good_list['id'] == this.cart_list[i]['id']&&good_list['lid'] == this.cart_list[i]['lid'])
				{
					newer = false;
				}
			}
		}
		if (newer)
		{
			this.cart_list.push(good_list);
			this.goodsNum = parseInt(this.goodsNum)+parseInt(good_list['num']);
		}

		this.setCartList();

		this.show();
	},
	update:function()//id,start_date,num,new_date
	{
		if (!this.inited) this.init();

		var good_list = {'id':arguments[0],'start_date':arguments[1],'num':parseInt(arguments[2]),'start_date_new':arguments[3]};
		good_list['num'] = isNaN(good_list['num']) ? 0 : good_list['num'];
		if (this.cart_list)
		{
			var total_glist=this.cart_list.length;

			for (var i=0; i<total_glist; i++)
			{
				if (good_list['id'] == this.cart_list[i]['id'] && good_list['start_date'] == this.cart_list[i]['start_date'])
				{	
					this.goodsNum = parseInt(this.goodsNum)-parseInt(this.cart_list[i]['num'])+parseInt(good_list['num']);
					this.cart_list[i]['num']= good_list['num'];

					if(good_list['start_date_new'])
					{
						var notexit=true;
						for (var j=0; j<total_glist; j++)
						{
							if (good_list['id'] == this.cart_list[j]['id'] && good_list['start_date_new'] == this.cart_list[j]['start_date'])
							{//alert(this.cart_list[j]['num']);alert(this.cart_list[i]['num'])
								this.cart_list[j]['num'] = parseInt(this.cart_list[j]['num'])+parseInt(this.cart_list[i]['num']);
								
								this.cart_list.splice(i,1);//alert(this.cart_list[j]['num'])
								notexit=false;
								break;
							}
							
						}
						
						if(notexit)this.cart_list[i]['start_date']= good_list['start_date_new'];
					}
					if (this.cart_list[i]['num']==0) this.cart_list.splice(i,1);
					break;
				}
			}
		}
		this.setCartList();

		this.show();
	},
	del:function(id,lid)
	{
		if (this.cart_list)
		{
			var total_glist=this.cart_list.length;

			for (var i=0; i<total_glist; i++)
			{
				if (id == this.cart_list[i]['id']&&lid == this.cart_list[i]['lid'])
				{
					this.goodsNum = parseInt(this.goodsNum)-parseInt(this.cart_list[i]['num']);
					this.cart_list.splice(i,1);
					break;
				}
			}
		}
		this.setCartList();

		this.show();
	},
	get:function(id,lid)
	{
		if (this.cart_list)
		{
			var total_glist=this.cart_list.length;

			for (var i=0; i<total_glist; i++)
			{
				if (id == this.cart_list[i]['id']&&lid == this.cart_list[i]['lid'])
				{
					return this.cart_list[i];
				}
			}
		}
	},
	countPrice:function(num,price,discount)
	{
		num=parseInt(num);
		var tprice=0;
		if(isNaN(num)||num==0){
			JQ('.shopcart .ft input').attr('disabled',true);
		}
		else {
			JQ('.shopcart .ft input').attr('disabled',false);
		
		if(discount)
		{
			for(var i in discount)
			{
				tprice +=parseFloat(discount[i]*Math.floor(num/i));
				num = num%i;
			}
		}
		tprice += parseFloat(price*num);
		}
		return tprice;
	},
	clear: function()
	{
		this.cart_list = [];
		this.goodsNum = 0;
		this.setCartList();

		this.show();
	},
	goon: function ()
	{
		this.close();//if(request_module=='shop'){this.close();void(0);}else{redirect('/shop.htm');}
	},
	pay:function (form)
	{
		_pay(form,JSON.stringify(this.cart_list));
	},
	pay_back:function (type)
	{
		switch (type)
		{
			case 'kq': 
			var form='<form id="frm"  name="frm" method="post" action="https://www.99bill.com/webapp/receiveMerchantInfoAction.do" ><input type=HIDDEN name="merchant_id" value="'+merchant_id+'"><input type=HIDDEN name="orderid" value="'+orderID+'"><input type=HIDDEN name="amount" value="'+strAmount+'"><input type=HIDDEN name="commodity_info"  value="'+commodity_info+'"><input type=HIDDEN name="merchant_url"  value="'+merchant_url+'"><input type=HIDDEN name="merchant_param"  value="'+merchant_param+'"><input type=HIDDEN name="pname"  value="'+pname+'"><input type=HIDDEN name="currency" value="'+currency+'"><input type=HIDDEN name="isSupportDES" value="2"><input type=HIDDEN name="mac" value="'+mac+'"></form>';
			JQ(document.body).append(form);
			JQ('#frm').submit();
			break;
			case 'c_mprice': list[i]['type']='一月';break;
			case 'c_qprice': list[i]['type']='一季度';break;
			case 'c_yprice': list[i]['type']='一年';break;
			default:list[i]['type']='一期';			
		}
	},
	order: function ()
	{
		if (!this.inited) this.init();
			var list = this.cart_list;

			var htm ="<div id='order_form' class='shopcart'><div class='tit drag'><span>"; 
			
			var total = list.length;
			//var total_mprice=0;
			var total_price=0;
			if (total)
			{
				htm+="您购物车中的商品如下</span><span class='close'>关闭</span></div>"
					+"<div class='con'><ul><li>期刊编号</li><li>期刊名称</li><li>价格</li></ul>";
				for (var i=0; i<total; i++)
				{
						var tprice = this.countPrice(parseInt(list[i]['num']),parseFloat(list[i]['price']));
						//total_mprice = parseInt(total_mprice) + parseInt(tmprice);
						total_price = parseFloat(total_price) + parseFloat(tprice);

						 htm+="<ul><li>"+list[i]['id']+"-"+list[i]['lid']+"</li><li>"+list[i]['name']+"</li><li>"+list[i]['price']+" 元";

										htm+="</li>"
						 
						//"<li>" +list[i]['num']+" 期</li><li>"
										// +list[i]['start_date']+'</li><li>'
										// +number_format(tprice)+" 元</li>"
										 +"</ul>"

				}	
				
				htm +='</div><div class="ft">以上商品合计总价：'+number_format(total_price)+'元<br></div>';
				
				htm +='<div class="pay"><form id="payform"><div><input type="hidden" name="totalprice" value="'+total_price+'"><input type="radio" id="pay_type_u_point" name="pay_type" checked="checked" value="u_point">此次消费共需积分<span style="font-weight:bold;color:red">'+number_format(total_price*1000)+'</span>(<a href="" target="_blank">关于积分详情点击查看</a>)<br><div id="user_point" class="loading-getUpoint"><script>_getUpoint('+(total_price*1000)+');</script></div></div>';
			htm +='<div><input type="radio" name="pay_type" value="money">使用人民币结算：<select name="pay_third"><option value="kq"> 快钱（支持所有银联卡）</option><option value="zfb">支付宝</option></select></div>';
			htm +='<div><input type="button" id="btn_pay" name="btn_pay" value="下一步" onclick="this.disabled=true;shopCart.pay(ajax.getFormValues(\'payform\'))"></div></form></div>';
			htm +="</div>";
			}
			else
			{
				htm +="您的购物车里还没有商品</span><span class='close'>关闭</span></div><div class='ft'>";
				htm += "<input type='button' value='继续购物' id='btn_order' onclick='shopCart.goon();'></div>";
			}
			
		topdiv.show(htm,680,256);
		//redirect('/index.php/order');
	},
	close:function ()
	{
		topdiv.hide();
	},
	show:function ()
	{
		if (!this.inited) this.init();
			var list = this.cart_list;

			var htm ="<div id='order_form' class='shopcart'><div class='tit drag'><span>"; 
			
			var total = list.length;
			//var total_mprice=0;
			var total_price=0;
			if (total)
			{
				htm+="您购物车中的商品如下</span><span class='close'>关闭</span></div>"
					+"<div class='con'><ul><li>期刊编号</li><li>期刊名称</li><li>价格</li><li>操作</li></ul>";
				for (var i=0; i<total; i++)
				{
						//var tmprice = parseInt(list[i]['num'])*parseInt(list[i]['mprice']);
						var tprice = this.countPrice(parseInt(list[i]['num']),parseFloat(list[i]['price']));
						//total_mprice = parseInt(total_mprice) + parseInt(tmprice);
						total_price = parseFloat(total_price) + parseFloat(tprice);

					htm+="<ul><li>"+list[i]['id']+"-"+list[i]['lid']+"</li><li>"+list[i]['name']+"</li><li>"+list[i]['price']+" 元";
											htm+="</li><li>"
						 
						 //+"<input size='4' type='text' value='"+list[i]['num']+"' id='num"+i+"' name='num"+i+"' onkeyup='shopCart.update("+list[i]['id']+",\""+list[i]['start_date']+"\",this.value)'>期</li><li>"
										// +'<input class="date-pick" id="start_date'+i+'" name="start_date'+i+'" value="'+list[i]['start_date']+'" readonly="readonly"></li><li>'+number_format(tprice)+" 元</li>";
										 +"<a href='javascript:shopCart.del("+list[i]['id']+","+list[i]['lid']+")'>删除</a></li></ul>"
										 //+"<script> JQ('#start_date"+i+"').datePicker({startDate:'"+list[i]['min_date']+"'}).bind('dateSelected',function(e, selectedDate,$td){shopCart.update("+list[i]['id']+",'"+list[i]['start_date']+"',JQ(\"#num"+i+"\").val(),JQ(\"#start_date"+i+"\").val());});</ script>";

				}	
				
				htm +='</div><div class="ft">以上商品合计总价：<b>'+number_format(total_price)+'</b> 元<br>'
					+"<input type='button' value='清空购物车' id='btn_order' onclick='shopCart.clear();'>&nbsp; <input type='button' value='继续购物' id='btn_order' onclick='shopCart.goon();'>&nbsp; <input type='button' value='到收银台' id='btn_order' onclick='shopCart.order();'></div>";
			}
			else
			{
				htm +="您的购物车里还没有商品</span><span class='close'>关闭</span></div><div class='ft'>";
				htm += "<input type='button' value='继续购物' id='btn_order' onclick='shopCart.goon();'></div>";
			}
			
			htm +="</div>";

		topdiv.show(htm,680,256);
	},
	showOne:function (id,name,price,lid)
	{
		var html="<div id='order_form' class='shopcart'><div class='tit drag'><span>您所选择的期刊为付费订阅刊物，订阅价格如下</span><span class='close'>关闭</span></div>"
				+"<div class='con'><ul><li>期刊编号</li><li>期刊名称</li><li>价格</li></ul>"
									+"<ul><li>"+id+"-"+lid+"</li><li>"+name+"</li><li>"+price+" 元<br />";

										html+="</li></ul>"

	
	
										 +"<div class='ft'><input type='button' value='放入购物车' id='btn_shopcart' onclick='shopCart.add("+id+",\""+name+"\",\""+price+"\",1,\""+lid+"\")'>&nbsp; <input type='button' value='直接到收银台' id='btn_order' onclick='shopCart.add("+id+",\""+name+"\",\""+price+"\",1,\""+lid+"\");shopCart.order();'></div></div>";
		topdiv.show(html,680,256);

	},
	init: function()
	{
		this.getGoodsNum();
		this.getCartList();
		this.inited = true;
	}
};

plugin('topdiv');
function order(cid,lid)
{
	_sub(cid,lid);
}

plugin('topdiv');
function comment(c_id)
{
		var html="<div id='comment_form' class='shopcart'><div class='tit drag'>请输入为该报纸的评价内容：</div>"
									 +"<form name='comment_form' method='post' action='javascript:void(null)'>"
									 +"<div class='con'><textarea cols='65' rows='4' name='content' id='content' validate='required:true'></textarea>"
									 +"</ul></div>"
									 +"<div class='ft'><input type='button' value='提交' name='B1' "
									 +"onclick='if(trim(JQ(\"#content\").val())!=\"\"){_addComment(JQ(\"#content\").val(),"+c_id+");topdiv.hide(\"comment_form\");}else{alert(\"评论内容不能为空！\");}'>&nbsp; "
									 +"<input type='button' value='关闭' name='close_comment_form' id='close_comment_form' onclick='topdiv.hide(\"comment_form\")'/></div></form></div>";
	topdiv.show(html,500,256);
}
document.write("<script type=\"text/javascript\">\n");
document.write("var PamsAjaxConfig = {\n");
document.write("requestURI: \"/index.php/mag/ajaxRun?request_action=content&_=1312854567\",\n");
document.write("method: \"POST\",\n");
document.write("statusMessages: \"1\",\n");
document.write("debug: \"\"\n");
document.write("};\n");
document.write("var PamsAjaxLoaded=false;\n");
document.write("function _login(){return ajax.call(\"login\", arguments);}\n");
document.write("function _logout(){return ajax.call(\"logout\", arguments);}\n");
document.write("function _pay(){return ajax.call(\"pay\", arguments);}\n");
document.write("function _getUpoint(){return ajax.call(\"getUpoint\", arguments);}\n");
document.write("function _getTopHits(){return ajax.call(\"getTopHits\", arguments);}\n");
document.write("function _sub(){return ajax.call(\"sub\", arguments);}\n");
document.write("function _addComment(){return ajax.call(\"addComment\", arguments);}\n");
document.write("</script>\n");
document.write("<script type=\"text/javascript\" src=\"/tmp/PamsAjax.js?_=1312854567\"></script>\n");
document.write("\n");
