var pageCart = new function() {
	this.arealist = {};//json格式，指定id包函的子地区
	this.areainfo = {};//对应id地区详细信息
	this.depth = 0;//当前地区深度
	this.sendprice = 0;//配送费
	this.sendprice_default = 0;//默认配送费
	this.sendinfo = [];
	this.address_id = 0;
	this.is_postage = false;
	this.address_oldid = 0;
	//地区下拉发生改变时触发
	this.changearea = function(val , index , defautval) {
		var obj,i,ii;
		index++;
		//当index大于深度时跳出
		if(index>this.depth) return;
		//发生改变后，重置之后的地区下拉
		for(i = index ; i < this.depth; i++) {
			obj = kj.obj("#id_area_" + i);
			if(!obj) break;
			obj.options.length = 0;
			if(i>index) {
				if(kj.obj("#id_area_" + i)) kj.obj("#id_area_" + i).style.display = 'none';
			}
		}
		var key = "id_" + val;
		var key2 = key;
		if(val!='') {
			for(i = 0; i <10 ;i++) {
				if(key in this.areainfo && kj.toint(this.areainfo[key]["sendprice"])>0) {
					this.sendprice = this.areainfo[key]["sendprice"];
					break;
				}
				if(key in this.areainfo && 'pid' in this.areainfo[key] && this.areainfo[key]["pid"]) {
					key = "id_" + this.areainfo[key]["pid"];
				} else {
					this.sendprice = this.sendprice_default;
					break;
				}
			}
		}
		if(!(key2 in this.arealist) || !("length" in this.arealist[key2]) || !kj.obj("#id_area_" + index)) {
			//跳出则刷新当前地区值
			if(kj.obj("#id_area_" + index)) kj.obj("#id_area_" + index).style.display = 'none';
			this.refresh_area_val();
			return;
		}
		kj.add_option("#id_area_" + index , '' , '');
		for(i = 0 ; i < this.arealist[key2].length ; i++ ) {
			obj = kj.obj("#id_area_" + index);
			ii = this.arealist[key2][i];
			if( !("id_" + ii in this.areainfo ) ) continue;
			kj.add_option(obj , this.areainfo["id_" + ii]["name"] , ii);
			//选中默认值
			if(obj.options[i+1].value == defautval) {
				obj.options[i+1].selected=true;
			}
		}
		if(kj.obj("#id_area_" + index)) kj.obj("#id_area_" + index).style.display = '';
		this.changearea(obj.value , index);
		this.refresh_area_val();
	}
	this.refresh_area_val = function() {
		var obj = kj.obj(":address_area_id[]");
		var arr_id = [];
		var arr_val = [];
		var val = '';
		var id = 0;
		for(var i = 0 ; i < obj.length ; i++) {
			if(obj[i].value != '') {
				if( !("id_" + obj[i].value in this.areainfo ) ) continue;
				arr_id[arr_id.length] = obj[i].value;
				val = ( 'val' in this.areainfo["id_" + obj[i].value] ) ? this.areainfo["id_" + obj[i].value]['val'] : this.areainfo["id_" + obj[i].value]['name'];
				arr_val[arr_val.length] = val;
			} else {
				break;
			}
		}
		if(arr_id.length>0) {
			kj.obj("#id_area_id").value = arr_id[arr_id.length-1];
		} else {
			kj.obj("#id_area_id").value = '';
		}
		kj.obj("#id_area_allid").value = arr_id.join(",");
		kj.obj("#id_area").value = arr_val.join(" ");
		//同步配送方式
		arr = kj.obj(".Tareaselect select");
		for(i = arr.length-1;i>=0;i--) {
			if(arr[i].style.display == '') {
				if(arr[i].selectedIndex>0) {
					break;//表示可以刷新配送方式
				} else {
					kj.obj("#id_sendinfo").innerHTML = '<li>请选择配送地区</li>';
					return;
				}
			}
		}
	}
	//提交订单
	this.submit = function() {
		//检测配送地区
		var arr_info = kj.obj("#id_cainfo .xsel");
		if(!arr_info || arr_info.length<1) {
			alert("请填写配送地址");
			return;
		}
		var obj_form = {};
		var obj_info = kj.json(kj.getAttribute(arr_info[0],'kjdata'));
		jscart.save_cookie();
		//收货信息
		obj_form.address_id = obj_info.id;
		//支付方式
		var arr = kj.obj("#id_paymethod .ysel");
		obj_form.paymethod = '';
		if(arr && arr.length>0) obj_form.paymethod=kj.getAttribute(arr[0] , 'kjdata')
		if(obj_form.paymethod=='') {
			alert("请选择支付方式");
			return;
		}
		//配送方式
		arr = kj.obj("#id_sendinfo .ysel");
		obj_form.sendinfo_id = '';
		if(arr && arr.length>0) obj_form.sendinfo_id=kj.getAttribute(arr[0] , 'kjdata')
		if(obj_form.sendinfo_id=='') {
			alert("请选择配送方式");
			return;
		}
		//配送时间
		if(kj.obj("#id_arrivetime").style.display == '') {
			obj_form.arrivetime = document.frmMain.arrivetime.value;
		}
		kj.ajax.post('?app=ajax&app_act=save.order' , obj_form , function(data) {
			var obj_data = kj.json(data);
			if(obj_data.isnull) {
				alert("下单失败，原因：系统繁忙");return;
			}
			if(obj_data.code == '0') {
				jscart.no_save = true;
				kj.alert.show("下单成功" , function() {
					window.open(kj.cfg('baseurl') + "/index.php?app_act=cart.ok&id="+obj_data.id,"_self");
				});
			} else {
				alert(obj_data.msg);
			}
		});
	}

	//选择收货地址时
	this.infosel = function(o ,id){
		kj.delClassName("#id_cainfo ul" , "xsel");
		kj.addClassName(o , "xsel");
		//同步配送区域
		if(id == 0) {
			arr = kj.obj(".Tareaselect select");
			for(i = arr.length-1;i>=0;i--) {
				if(arr[i].style.display == '') {
					if(arr[i].selectedIndex>0) {
						break;//表示可以刷新配送方式
					} else {
						kj.obj("#id_sendinfo").innerHTML = '<li>请选择配送地区</li>';
						return;
					}
				}
			}
		}
		this.load_sendinfo();
	}
	this.load_sendinfo = function() {
		var arr = kj.obj("#id_address_box .xsel .Tareaids");
		var areaids = '';
		if(arr && arr.length>0) areaids = arr[0].value;
		kj.ajax.get(kj.cfg("baseurl") + "/common.php?app=mall.ajax&app_act=sendinfo&areaids="+areaids , function(data) {
			pageCart.sendinfo = kj.json(data);
			pageCart.refresh_sendinfo();
		});
	}

	this.refresh_sendinfo = function() {
		var arr = kj.obj("#id_paymethod .ysel");
		var paymethod = '';
		if(arr && arr.length>0) paymethod=kj.getAttribute(arr[0] , 'kjdata')
		arr = kj.obj("#id_sendinfo .ysel");
		var sendinfo_val = '';
		if(arr && arr.length>0) sendinfo_val=kj.getAttribute(arr[0] , 'kjdata')
		var obj_data = this.sendinfo;
		var is_payment = false , ischeck = '';
		var html = '' , html2 = '' , html3 = '';
		for(var i = 0 ; i < obj_data.length ; i++) {
			ischeck = (sendinfo_val == obj_data[i].id) ? " class='ysel'" : "";
			html = '<label onclick="pageCart.refresh_time(this);"'+ischeck+' kjdata="' + obj_data[i].id + '"><font>' + obj_data[i].name + '</font>&nbsp;&nbsp;<font class="Tsendprice"></font></label>';
			html2 += html;
			if(obj_data[i].payment == '1') is_payment = true;
			if(paymethod == 'afterpayment' && obj_data[i].payment != '1' ) continue;
			html3 += html;
		}
		if(html3 == '' || is_payment == false ) {//说明是货到付款，但是没有支持货到付款的配送
			kj.hide(".Tafterpayment");
			html3 = html2;
		} else {
			kj.show(".Tafterpayment");
		}
		kj.obj("#id_sendinfo").innerHTML = html3;
		kj.set(".Ttotalsendprice","innerHTML" ,kj.cfg("coinsign")+"0");
		this.get_sendprice();
	}

	this.refresh_time = function(o) {
		kj.delClassName("#id_sendinfo label" , "ysel");
		kj.addClassName(o , "ysel");
		var arrobj = kj.obj("#id_sendinfo label");
		arr = kj.obj("#id_sendinfo .ysel");
		var sendinfo_val = '';
		if(arr && arr.length>0) sendinfo_val=kj.getAttribute(arr[0] , 'kjdata');
		if(sendinfo_val == '') { 
			kj.hide(".Tarrivetime");
			kj.set(".Ttotalsendprice","innerHTML" ,kj.cfg("coinsign")+"0");
			jscart.refresh_price();
			return;
		}
		var ii = kj.index(arrobj , arr[0] , true);
		//更新配送费用
		arr= kj.obj("#id_sendprice_" + sendinfo_val);
		if(arr) {
			kj.set(".Ttotalsendprice","innerHTML" ,kj.cfg("coinsign")+kj.toint(arr.value));
			this.refresh_price();
		}
		if( !('length' in this.sendinfo[ii].time) || this.sendinfo[ii].time.length <1 ) {
			kj.hide(".Tarrivetime");
			return;
		}
		var option = document.frmMain.arrivetime.options;
		option.length = 0;
		for(var j = 0 ; j < this.sendinfo[ii].time.length ; j++) {
			kj.add_option(document.frmMain.arrivetime , this.sendinfo[ii].time[j] , this.sendinfo[ii].time[j]);
		}
		kj.show(".Tarrivetime");
	}
	this.in_areas = function(ids) {
		var obj = {'is':false,'price':0,'isadd':false};
		if(ids == '') return obj;
		var arrx = ids.split('>');
		if(arrx.length!=2) return obj;
		var isadd = arrx[1];
		var arr = arrx[0].split("|");
		arrx = [];
		var arrid = [],arrval = [];
		for(var i = 0 ; i < arr.length ; i++) {
			arrx = arr[i].split('=');
			arrid[arrid.length] = arrx[0];
			arrval[arrval.length] = arrx[1];
		}

		var arr_info = kj.obj("#id_cainfo .xsel");
		if(!arr_info || arr_info.length<1) return obj;
		var obj_info = kj.json(kj.getAttribute(arr_info[0],'kjdata'));
		var arr_ids = obj_info.allid.split(',');
		for(var i = 0 ; i < arr_ids.length ; i++) {
			index = arrid.indexOf(arr_ids[i]);
			if(index>=0) {
				obj.is = true;
				obj.price = arrval[index];
				obj.isadd = isadd;
			}
		}
		return obj;
	}
	this.get_sendprice = function() {
		var arr_weight = kj.obj(":weight[]");
		var arr_num = kj.obj(".TcartNum");
		var arr_postage = kj.obj(":postage[]");
		if(!arr_weight || !arr_num) return;
		if(arr_weight.length != arr_num.length) return;
		var weight = 0;
		this.is_postage = true;
		var goods_sendprice = 0 , objx;
		for(var i = 0 ; i < arr_weight.length ; i++) {
			objx = this.in_areas(arr_postage[i].value);
			if(objx.is) {
				if(objx.isadd) {
					goods_sendprice += objx.price * kj.toint(arr_num[i].value);
				} else {
					goods_sendprice += objx.price;
				}
			} else {
				this.is_postage = false;
				weight += kj.toint(arr_weight[i].value) * kj.toint(arr_num[i].value);
			}
		}
		this.weight = weight;
		kj.set("#id_total_weight" , "innerHTML" , "重：" + weight + "克");
		var price = 0 , weight_next = 0 , objp;
		var arr_sendprice = kj.obj(".Tsendprice");
		for( i = 0 ; i < this.sendinfo.length ; i++ ) {
			weight_next = weight - kj.toint(this.sendinfo[i].unit);
			price = this.sendinfo[i].price;
			if( weight_next > 0 ) {
				x = weight_next/kj.toint(this.sendinfo[i].unit_next);
				if(x > parseInt(x)) x = parseInt(x) + 1;
				price += x * kj.toint(this.sendinfo[i].next);
			}
			if(this.is_postage) {
				price = goods_sendprice;
			} else {
				price += goods_sendprice;
			}
			html = (price == 0) ? "免运费" : "运费：" + price;
			if(arr_sendprice[i]) {
				arr_sendprice[i].innerHTML = html + '<input type="hidden" name="sendprice[]" value="' + price + '" id="id_sendprice_' + this.sendinfo[i].id + '">';
				objp = kj.parent(arr_sendprice[i],'label');
				if(objp.className.indexOf("ysel")>=0) {
					kj.set(".Ttotalsendprice","innerHTML" , price);
				}
			}
		}
		var arr_sendinfo = kj.obj("#id_sendinfo .ysel");
		if(!arr_sendinfo || arr_sendinfo.length<1) {
			arr_sendinfo = kj.obj("#id_sendinfo label");
			if(arr_sendinfo && arr_sendinfo.length>0) pageCart.refresh_time(arr_sendinfo[0]);
		}
	}

	this.address_edit = function(id) {
		this.address_id = id;
		if(kj.obj("#id_edit_info")) {
			this.editaddress_html = kj.obj("#id_edit_info").innerHTML;
			kj.remove("#id_edit_info");
		}
		kj.dialog({'html':this.editaddress_html,'id':'editinfo','type':'html','title':'收货信息','w':600,'showbtnmax':false});
		if(id>0) {
			var objdata = kj.getAttribute("#id_address_"+id , "kjdata");
			if(objdata) objdata = kj.json(objdata);
			if(objdata && 'id' in objdata) {
				document.frmEditinfo.address.value=objdata.address;
				document.frmEditinfo.name.value=objdata.name;
				document.frmEditinfo.tel.value=objdata.tel;
				this.address_seldefault(objdata.allid);
			}
		}
	}
	this.address_del = function(id) {
		if(!confirm("确定要删除吗？")) {
			return;
		}
		kj.ajax.get(kj.cfg('baseurl') + "/index.php?app=ajax&app_act=address.del&id="+id , function(data) {
			var obj_data=kj.json(data);
			pageCart.submiting = false;
			if(obj_data.isnull) {
				alert("操作失败");
			} else {
				if('code' in obj_data && obj_data.code=='0') {
					kj.remove("#id_address_"+id);
					var arr = kj.obj("#id_cainfo ul");
					if(arr.length>0) {
						kj.event(arr[0],'click');
					}
					if(arr.length<5) kj.show("#id_cainfo .xnew");
				}else{
					if("msg" in obj_data){
						alert(obj_data.msg);
					}else{
						alert("操作失败");
					}
				}
			}
		});
	}
	this.address_cancel = function() {
		kj.dialog.close("#wineditinfo");
	}

	this.address_seldefault = function(allid) {
		if(!allid || allid=='') return;
		var arr = allid.split(",");
		//加载默认值选中
		this.select_sel(kj.obj("#id_area_0") , arr[0]);
		for(var i=0 ; i < this.depth ; i++) {
			//if(arr.length-1<=i) break;
			this.changearea(arr[i],i,arr[i+1]);
		}
		if(arr.length<2) this.refresh_area_val();
	}
	this.select_sel = function(obj,val) {
		var is_sel = false;
		for(var i = 0 ; i < obj.length; i++) {
			if(obj[i].value == val) {
				obj[i].selected = true;
				is_sel = true;
				break;
			}
		}
		if(!is_sel && obj.length>0 ) obj[0].selected = true;
	}
	//检查用户信息合法性
	this.chk_info = function() {
			if(document.frmEditinfo.name.value=="") {
				alert("请填写联系人信息");
				document.frmEditinfo.name.focus();
				return false;
			}

			if(document.frmEditinfo.tel.value=='') {
				alert("请填写联系电话");
				document.frmEditinfo.tel.focus();
				return false;
			}
			if(!kj.rule.types.tel(document.frmEditinfo.tel.value)) {
				alert("输入电话格式不正确");
				document.frmEditinfo.tel.focus();
				return false;
			}
			for(var i = 0 ; i < this.depth ; i++) {
				x = kj.obj("#id_area_" + i);
				if(x && x.value=="" && x.style.display=='') {
					alert("请选择您所在地区范围");
					x.focus();
					return false;
				}
			}
			if(document.frmEditinfo.address.value=="") {
				alert("请填写详细收货地址");
				document.frmEditinfo.louhao1.focus();
				return false;
			}
			return true;
	}
	this.address_save = function() {
		var area = kj.obj("#id_area").value;
		var area_allid = kj.obj("#id_area_allid").value;
		var area_id = kj.obj("#id_area_id").value;
		if(this.chk_info() == false) return;
		var data = {"id":this.address_id,"name":document.frmEditinfo.name.value,"area":area,"area_id":area_id,"area_allid":area_allid,"address":document.frmEditinfo.address.value,"tel":document.frmEditinfo.tel.value};
		kj.ajax.post(kj.cfg('baseurl') + "/index.php?app=ajax&app_act=address.save" , data ,function(data){
			var obj_data=kj.json(data);
			pageCart.saveinfo = false;
			if(obj_data.isnull) {
				alert("保存失败");
			} else {
				if('code' in obj_data && obj_data.code=='0') {
					var id = "#id_address_" + pageCart.address_id;
					if(pageCart.address_id>0) {
						kj.set(id + " .xname" , "innerHTML" ,document.frmEditinfo.name.value );
						kj.set(id + " .xtel" , "innerHTML" ,document.frmEditinfo.tel.value );
						kj.set(id + " .xaddress" , "innerHTML" ,document.frmEditinfo.address.value );
						kj.set(id + " .xarea" , "innerHTML" ,kj.obj("#id_area").value );
					} else {
						kj.delClassName("#id_cainfo ul" , 'xsel');
						var html = '<div class="xbox">';
						html += '<li><font class="xname">' + document.frmEditinfo.name.value + '</font>&nbsp;&nbsp;&nbsp;<b>Tel：</b><font class="xtel">' + document.frmEditinfo.tel.value + '</font><span><a href="javascript:pageCart.address_edit('+obj_data.id+');"><img src="'+kj.cfg('basecss')+'/default/images/edit.png"></a> | <a href="javascript:pageCart.address_del('+obj_data.id+');"><img src="'+kj.cfg('basecss')+'/default/images/drop.png"></a></span></li>';
						html += '<li><font class="xarea">' + kj.obj("#id_area").value + '</font></li><li class="xaddress">' + document.frmEditinfo.address.value + '</li>';
						html += '<li class="xtag"></li>';
						var obj = document.createElement("ul");
						obj.id = 'id_address_'+obj_data.id;
						obj.className = 'xsel';
						obj.innerHTML = html;
						obj.onclick = function() {
							pageCart.infosel(this);
						}
						var arrul = kj.obj("#id_cainfo ul");
						kj.insert_before(kj.obj("#id_cainfo") , arrul[arrul.length-1] , obj);
						var arr = kj.obj("#id_cainfo ul");
						if(arr.length>3) kj.hide("#id_cainfo .xnew");
					}
					var kjdata = "{'id':"+pageCart.address_id+",'name':'"+document.frmEditinfo.name.value+"','tel':'"+document.frmEditinfo.tel.value+"','address':'"+document.frmEditinfo.address.value+"','allid':'"+document.frmEditinfo.area_allid.value+"'}"
					kj.setAttribute("#id_address_"+obj_data.id , "kjdata" , kjdata);
					kj.dialog.close("#wineditinfo");
					pageCart.load_sendinfo();
					pageCart.refresh_price();
				}else{
					if("msg" in obj_data){
						kj.alert(obj_data.msg);
					}else{
						kj.alert("操作失败");
					}
				}
			}
		});
	}
	this.delgoods = function(id) {
		var row = kj.obj("#id_goods_row"+id);
		var index = row.rowIndex;
		var table = kj.parent(row , "table");
		if(index || index == 0) table.deleteRow(index);
		if(jscart) jscart.del(id);
	}
	this.paysel = function(o) {
		kj.delClassName("#id_paymethod label" , "ysel");
		kj.addClassName(o , "ysel");
		this.refresh_sendinfo();
	}
	this.refresh_price = function(price) {
		var arr_pay = kj.obj("#id_paymethod .ysel");
		if(!arr_pay || arr_pay.length<1) {
			arr_pay = kj.obj("#id_paymethod label");
			if(arr_pay && arr_pay.length>0) pageCart.paysel(arr_pay[0]);
		}
		this.get_sendprice();
		if(!price) {
			var obj_cartprice = kj.obj(".TcartTotal");
			price = 0;
			if(obj_cartprice && obj_cartprice.length>0) price = kj.toint(obj_cartprice[0].innerHTML);
		}
		var obj_send = kj.obj(".Ttotalsendprice");
		if(obj_send && obj_send.length>0) price += kj.toint(obj_send[0].innerHTML);
		var obj_t = kj.obj(".Ttotalprice");
		if(obj_t && obj_t.length>0) {
			kj.set(".Ttotalprice" , "innerHTML" ,  kj.cfg("coinsign")+price);
		}
	}

}