/*document.querySelector('body').addEventListener('touchstart', function(e) {
    e.preventDefault();
});*/
var domanUrl = "http://" + window.location.host;
var base_path = "/iamberry/wechat";
mui.each(document.querySelectorAll('#footer-bar .mui-tab-item'), function(index, el) {
	el.addEventListener('tap', function() {
		switch (index) {
			case 0:
				mui.openWindow({
					//首页
					url: base_path + '/index' //'index.html',
				});
				break;
			case 1:
				//打开购物车
				mui.openWindow({
					url: base_path + '/wxCart/goCart'
				});
				break;
			case 2:
				//订单列表
				mui.openWindow({
					url: '/iamberry/pay/goOrderList'
				});
				break;
			case 3:
				//会员中心
				mui.openWindow({
					url: base_path + '/agentWechat/member' //vip_center.html',
				});
				break;
			default:
				break;
		}
	}, false);
});
mui.ajax(base_path+'/cart/getCartNumByCartOpenId', {
	                data:{openid:'230'},
	                dataType: 'json', //服务器返回json格式数据
					type: 'post', //HTTP请求类型
					timeout: 15000, //超时时间设置为15秒；
					success: function(dt) {
						//服务器返回响应
						//console.log(dt);
						if(dt.status){
							dt.data=="0"? $('.icon-gouwuche span').hide():$('.icon-gouwuche span').text(dt.data).show();
						}else{
							
						}
					},
					error: function(xhr, type, errorThrown) {
						//异常处理；
						errorfn(xhr);
					}
				});
mui('body').on('tap', '.go_index', function() {
	//去首页
	mui.openWindow({
		url: base_path + '/index',
		id: 'index',
	});
});

mui('body').on('tap', '.go_gwc', function() {
	//去购物车
	mui.openWindow({
		url: base_path + '/wxCart/goCart', //shopping_cart.html
		id: 'shopping_cart'
	});
});

mui('body').on('tap', '.go_web_shop', function() {
	//去产品列表
	mui.openWindow({
		url: base_path + '/wxCart/goProductList' //chanpin_list.html
	});
});
//产品详情
mui('body').on('tap', '.go_chanpin_details', function() {
	mui.openWindow({
		url: base_path + '/product/desc?productId=' + this.getAttribute("proid") //chanpin_details.html
	});
});
//个人全部优惠券列表
mui('body').on('tap', '.go_couponList', function() {
	mui.openWindow({
		url: base_path + '/couponWechat/couponList' //couponList。html
	});
});

//立即购买 结算
mui('body').on('tap', '.go-clearing', function() {
    var th=this;
    mui.ajax(base_path + '/cart/addProductToCart', {
        data: {"productId":th.getAttribute("proid"),"colorId":th.getAttribute("colorid")},
        dataType: 'json',
        type: 'GET',
        timeout: 15000,
        success: function(dt) {
            if(dt.status) {
                mui.openWindow({
                    url: '/tooth/pay/settlement?cartId=' + dt.data + "&tiemstamp=" + new Date().getTime()
                })
            } else {
                mui.alert(dt.message)
            }
        },
        error: function(xhr, type, errorThrown) {
            console.log(xhr)
        }
    })
});
//加入购物车
mui('body').on('tap', '.go_gwc_pro', function() {
	var param = "productId=" + this.getAttribute("proid");
	//去购物车
	mui.ajax(base_path + '/cart/addProductToCart?' + param, {
		dataType: 'json',
		type: 'GET',
		timeout: 15000,
		success: function(dt) {
			if (dt.status) {
				// mui.alert("添加成功!");
				window.location.href = base_path + '/wxCart/goCart';
			} else {
				mui.alert(dt.message);
			}
		},
		error: function(xhr, type, errorThrown) {
			errorfn(xhr);
		}
	});
});
mui('body').on('tap', '.go_order_details', function() {
	//去订单详情
	mui.openWindow({
		url: '/iamberry/pay/goOrderInfo?orderId=' + this.getAttribute("value") //order_details.html
	});
});

//待支付订单
mui('body').on('tap', '.go_pay_count', function() {
	mui.openWindow({
		url: '/iamberry/pay/goOrderList?salesStatus=1'
	});
});
//已支付订单
mui('body').on('tap', '.go_deliver_count', function() {
	mui.openWindow({
		url: '/iamberry/pay/goOrderList?salesStatus=2'
	});
});
//已发货订单
mui('body').on('tap', '.go_delivered_count', function() {
	mui.openWindow({
		url: '/iamberry/pay/goOrderList?salesStatus=5'
	});
});
//全部订单
mui('body').on('tap', '.go_all_count', function() {
	mui.openWindow({
		url: '/iamberry/pay/goOrderList?salesStatus=-1'
	});
});
mui('body').on('tap', '.go_tixian_guize', function() {
	//提现规则
	mui.openWindow({
		url: base_path + '/integralWechat/withdrawRule' //tixian_guize.html'
	});
});

//微代理规则
mui('body').on('tap', '.go_agent_rule', function() {
	mui.openWindow({
		url: base_path + '/agentWechat/agentRule' //'daili_guize.html'
	});
});
//冲奶机宣传片
mui('body').on('tap', '.go_publicity', function() {
	mui.openWindow({
		url: base_path + '/agentWechat/goPublicity?userType=' + this.getAttribute("userType") + "&isDrp=" + this.getAttribute("isDrp")//'iamberry_video.html'	
	});
});

mui('body').on('tap', '.go_daili_order_list', function() {
	//代理订单列表
	mui.openWindow({
		url: base_path + '/agentWechat/order' //'daili_order_list.html'
	});
});
mui('body').on('tap', '.go_daili_rebackOrder_list', function() {
	//代理订单列表
	mui.openWindow({
		url: base_path + '/agentWechat/reback' //'daili_order_list.html'
	});
});
mui('body').on('tap', '.go_order_list', function() {
	//订单列表
	mui.openWindow({
		url: '/iamberry/pay/goOrderList' //order_list.html
	});
});
mui('body').on('tap', '.go_xiaxian_list', function() {
	//我的下线
	mui.openWindow({
		url: base_path + '/agentWechat/agentNext' //'xiaxian_list.html'
	});
});
mui('body').on('tap', '.go_tixian_apply', function() {
	//提现申请<积分>
	mui.openWindow({
		url: base_path + '/withdraw/index' //'tixian_apply.html',
	});
});
mui('body').on('tap', '.go_tixian_cash', function() {
	//提现申请<现金>
	mui.openWindow({
		url: base_path + '/withdraw/cash' //'tixian_apply_cash.html'
	});
});
mui('body').on('tap', '.go_daili_2wm', function() {
	//查看二维码
	mui.openWindow({
		url: base_path + '/member/showQrcode'//'daili_2wm.html'
	});
});

//现金记录
mui('body').on('tap', '.go_cash_log', function() {
	mui.openWindow({
		url: base_path + '/cashWechat/log'	//cash_log.html
	});
});

mui('body').on('tap', '.go_jifen_log', function() {
	//积分记录
	mui.openWindow({
		url: base_path + '/integralWechat/log' //jifen_log.html'
	});
});
mui('body').on('tap', '.go_jifen_guize', function() {
	//积分规则
	mui.openWindow({
		url: base_path + '/integralWechat/integralRule' //jifen_guize.html'
	});
});
//积分提现记录
mui('body').on('tap', '.go_tixian_log', function() {
	mui.openWindow({
		url: base_path + '/integralWechat/withdrawLog' //tixian_log.html'
	});
});
//可用金额提现记录
mui('body').on('tap', '.go_tixian_cash_log', function() {
	mui.openWindow({
		url: base_path + '/agentWechat/withdrawCashLog' //tixian_apply_cash.html
	});
});

//查看物流信息
mui('body').on('tap', '.show_post_info', function() {
	var urlStr = document.domain;
	mui.openWindow({
		//url: 'http://m.kuaidi100.com/index_all.html?type=' + this.getAttribute("postFirm") + '&postid=' + this.getAttribute("postNum") + '&callbackurl=http://' + urlStr + this.getAttribute("gourl")  + "&tiemstamp=" + new Date().getTime()
        url: 'http://tuisong.wx.sto.cn/Track/Result163?billCode=' + this.getAttribute("postNum")
	});
});

//=====================================分销===========================================
/*mui('body').on('tap', '.go_fenxiao_guize', function() {
	//分销的规则
	mui.openWindow({
		url: '/iamberry/wechat/drp/rulePage' //'fenxiao_guize.html'
	});
});*/
mui('body').on('tap', '.go_fenxiao_tixian_apply', function() {
	//分销提现申请
	mui.openWindow({
		url: base_path + '/drp/withdrawalsPage?openid=' //'fenxiao_tixian_apply.html'
	});
});
mui('body').on('tap', '.go_fenxiao_tixian_log', function() {
	//分销提现记录
	mui.openWindow({
		url: base_path + '/drp/withdrawalsList' //'fenxiao_tixian_log.html'
	});
});
mui('body').on('tap', '.fenxiao_vip', function() {
	//分销二维码
	mui.openWindow({
		url: base_path + '/drp/rulePage' //'fenxiao_2wm.html'
	});
});
mui('body').on('tap', '.to-up-img', function() {
	//上传图片入口
	mui.openWindow({
		url: base_path + '/sharePage/upShare?userType=1'//'up.html',
	});
});
//商户个人中心 查看个人信息
mui('body').on('tap', '.fenxiao_info', function() {
	//上传图片入口
	mui.openWindow({
		url: base_path + '/drp/placeInfo_detail'//'personal_information.html',
	});
});

//===============================大转盘==============================
//进入大转盘页面
mui('body').on('tap', '.go_luck_draw', function() {
	mui.openWindow({
		url: base_path + '/activity/go_luck_draw?orderId=' + this.getAttribute("value") //zhuanpan.html
	});
});

//进入中奖纪录页面
mui('body').on('tap', '.go_prize_log', function() {
	mui.openWindow({
		url: base_path + '/activity/go_prize_log?gameId=' + this.getAttribute("value") //wheel_log.html
	});
});

function errorfn(xhr){
	//异常处理；
	if(xhr.readyState == 4 && xhr.status == 200){ 
    }else{
    	var ref=location.href.split("#")[0],src='';
    	if (ref.indexOf('?') != -1) {
    		src = ref + "&date=" + new Date().getTime();
    	} else {
    		src = ref + "?date=" + new Date().getTime();
    	}
    	var r=confirm("页面出错啦！请刷新重试");
    	if (r==true){
    		location.href=src;
    	}
    	return;
    }	
}
//判断图片是否真实存在
function CheckImgExists(imgurl) {
	var ImgObj = new Image(); //判断图片是否存在
	ImgObj.src = imgurl;
	//没有图片，则返回-1
	if (ImgObj.fileSize > 0 || (ImgObj.width > 0 && ImgObj.height > 0)) {
		return ImgObj;
	} else {
		return false;
	}
}
/** 
 ** 除法函数，用来得到精确的除法结果
 ** 说明：javascript的除法结果会有误差，在两个浮点数相除的时候会比较明显。这个函数返回较为精确的除法结果。
 ** 调用：accDiv(arg1,arg2)
 ** 返回值：arg1除以arg2的精确结果
 **/
function accDiv(arg1, arg2) {
    var t1 = 0, t2 = 0, r1, r2;
    try {
        t1 = arg1.toString().split(".")[1].length;
    }
    catch (e) {
    }
    try {
        t2 = arg2.toString().split(".")[1].length;
    }
    catch (e) {
    }
    with (Math) {
        r1 = Number(arg1.toString().replace(".", ""));
        r2 = Number(arg2.toString().replace(".", ""));
        return (r1 / r2) * pow(10, t2 - t1);
    }
}

//给Number类型增加一个div方法，调用起来更加方便。
Number.prototype.div = function (arg) {
  return accDiv(this, arg);
};
/**
 ** 加法函数，用来得到精确的加法结果
 ** 说明：javascript的加法结果会有误差，在两个浮点数相加的时候会比较明显。这个函数返回较为精确的加法结果。
 ** 调用：accAdd(arg1,arg2)
 ** 返回值：arg1加上arg2的精确结果
 **/
function accAdd(arg1, arg2) {
    var r1, r2, m, c;
    try {
        r1 = arg1.toString().split(".")[1].length;
    }
    catch (e) {
        r1 = 0;
    }
    try {
        r2 = arg2.toString().split(".")[1].length;
    }
    catch (e) {
        r2 = 0;
    }
    c = Math.abs(r1 - r2);
    m = Math.pow(10, Math.max(r1, r2));
    if (c > 0) {
        var cm = Math.pow(10, c);
        if (r1 > r2) {
            arg1 = Number(arg1.toString().replace(".", ""));
            arg2 = Number(arg2.toString().replace(".", "")) * cm;
        } else {
            arg1 = Number(arg1.toString().replace(".", "")) * cm;
            arg2 = Number(arg2.toString().replace(".", ""));
        }
    } else {
        arg1 = Number(arg1.toString().replace(".", ""));
        arg2 = Number(arg2.toString().replace(".", ""));
    }
    return (arg1 + arg2) / m;
}

//给Number类型增加一个add方法，调用起来更加方便。
Number.prototype.add = function (arg) {
    return accAdd(arg, this);
};
/**
 ** 减法函数，用来得到精确的减法结果
 ** 说明：javascript的减法结果会有误差，在两个浮点数相减的时候会比较明显。这个函数返回较为精确的减法结果。
 ** 调用：accSub(arg1,arg2)
 ** 返回值：arg1加上arg2的精确结果
 **/
function accSub(arg1, arg2) {
    var r1, r2, m, n;
    try {
        r1 = arg1.toString().split(".")[1].length;
    }
    catch (e) {
        r1 = 0;
    }
    try {
        r2 = arg2.toString().split(".")[1].length;
    }
    catch (e) {
        r2 = 0;
    }
    m = Math.pow(10, Math.max(r1, r2)); //last modify by deeka //动态控制精度长度
    n = (r1 >= r2) ? r1 : r2;
    return ((arg1 * m - arg2 * m) / m).toFixed(n);
}

// 给Number类型增加一个mul方法，调用起来更加方便。
Number.prototype.sub = function (arg) {
    return accMul(arg, this);
};
/**
 ** 乘法函数，用来得到精确的乘法结果
 ** 说明：javascript的乘法结果会有误差，在两个浮点数相乘的时候会比较明显。这个函数返回较为精确的乘法结果。
 ** 调用：accMul(arg1,arg2)
 ** 返回值：arg1乘以 arg2的精确结果
 **/
function accMul(arg1, arg2) {
    var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
    try {
        m += s1.split(".")[1].length;
    }
    catch (e) {
    }
    try {
        m += s2.split(".")[1].length;
    }
    catch (e) {
    }
    return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
}

// 给Number类型增加一个mul方法，调用起来更加方便。
Number.prototype.mul = function (arg) {
    return accMul(arg, this);
};
function toApplyAgent(){
	var flag = false;
	var orderid = '';
	mui.ajax(base_path + '/agent/checkApply?dates=' + new Date().getTime(),{
		dataType:'json',//服务器返回json格式数据
		async: false,	//同步
		type:'post',//HTTP请求类型
		timeout:15000,//超时时间设置为15秒；
		success:function(dt){
			if(dt.status){
				flag = true;
				if(dt.data != null  && typeof(dt.data)!="undefined" && dt.data != ''){
					orderid = dt.data;
				}
			}else{
				if(dt.resultCode != 400){
					if(dt.resultCode == 600){
						window.location.href= base_path + '/agentWechat/member';
					}else{
						var nowHref = window.location.href;
						var strs = new Array();
						strs=nowHref.split("?");
						var nowHrefStr = '';
						if(strs.length > 0){
							nowHrefStr = strs[0];
						}else{
							nowHrefStr = nowHref;
						}
						
						if(nowHrefStr == 'http://test.iamberry.com/iamberry/wechat/agentWechat/agentRule'){
						//if(nowHrefStr == 'http://localhost:8080/iamberry/wechat/agentWechat/agentRule'){
							mui.alert(dt.message);
						}else{
							window.location.href= base_path + "/agentWechat/agentRule";
						}
					}
				}else{
					mui.alert(dt.message,' ','确定');
				}
			}
		},
		error:function(xhr,type,errorThrown){
			//异常处理；
			errorfn(xhr);
		}
	});
	
	var btnArray = ['否', '是'];
	if(orderid != ''){
		mui.confirm('若继续强制申请，则单号为：'+orderid+'的订单将不允许退/换货', ' ', btnArray, function(e) {
			if (e.index == 1) {
				createApply(orderid);
			}
		});
	}else{
		if(flag){
			createApply(orderid);
		}
	}
}
function switchcolor(color){
	var pro_color="";
	switch (color) {
	case 1:pro_color = "蓝色";break;
	case 2:pro_color = "粉色";break;
	case 3:pro_color = "银色";break;
	case 4:pro_color = "香槟金";break;
	case 5:pro_color = "苹果绿";break;
	case 6:pro_color = "西瓜红";break;
	case 7:pro_color = "品蓝";break;
	case 8:pro_color = "品黑";break;
	default:pro_color = "香槟金";break;
}
	 return pro_color;
}

//获取地址栏url参数
function getParam(paramName) {
	paramValue = "", isFound = !1;
	if (this.location.search.indexOf("?") == 0 && this.location.search.indexOf("=") > 1) {
		arrSource = unescape(this.location.search).substring(1, this.location.search.length).split("&"), i = 0;
		while (i < arrSource.length && !isFound) arrSource[i].indexOf("=") > 0 && arrSource[i].split("=")[0].toLowerCase() == paramName.toLowerCase() && (paramValue = arrSource[i].split("=")[1], isFound = !0), i++
	}
	return paramValue == "" && (paramValue = null), paramValue
}
//封装document.getElementById
function dc(objectid) {
	if(document.getElementById && document.getElementById(objectid)) {
		return document.getElementById(objectid);// w3c dom
	} else if (document.all && document.all(objectid)) {
		return document.all(objectid);// msie 4 dom
	} else if (document.layers && document.layers[objectid]) {
		return document.layers[objectid];// nn 4 dom.. note: this won't find nested layers
	} else {
		return false;
	}
}
function   formatDate(now)   {     
	now = new Date(now);
    var   year=now.getFullYear();     
    var   month=now.getMonth()+1;     
    var   date=now.getDate();     
    var   hour=now.getHours();     
    var   minute=now.getMinutes();     
    var   second=now.getSeconds();     
    return   year+"-"+month+"-"+date+"   "+hour+":"+minute+":"+second;     
 } 
var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");document.write(unescape("%3Cspan id='cnzz_stat_icon_1260421959'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s4.cnzz.com/z_stat.php%3Fid%3D1260421959%26show%3Dpic1' type='text/javascript'%3E%3C/script%3E"));