//从接口的com后面拼接到后面
var base_api = "/shop/?r=";
//base_api = "http://fx.zhizhuchuxing.cn/shop/?r=";
//base_api = '';
//版本号
var version = 'version=2.0.4';
//错误码
//code  SHOP-100001   未登录
var codeNotLogin = 'SHOP-100001';
//提示信息归整
var infoApiError = "后台接口错误";
//分销商的uid   fx_uid
var fx_uid = 'fx_uid';
//授权页面
var auth_login_url = base_api + 'weChat/we-chat/auth-login&redirect=';

//延迟函数  需要将延迟处理的函数放在delay的回调中
//$('#id').keyup(function() {
//			delay(function() {
//				console.log('test');
//			}, 1000);
//		});

var delay = (function() {
	var timer = 0;
	return function(callback, time) {
		clearTimeout(timer);
		timer = setTimeout(callback, time);
	};
})();
//得到随机的字符串
function getRdStr(len) {
	//2-36封顶
	var rdmString = "";
	for(; rdmString.length < len; rdmString += Math.random().toString(36).substr(2));
	return rdmString.substr(0, len);
}

function getFxUid() {
	var cookie_uid = getPar(fx_uid);
	//处理首页锚点#prod
	var index_end = cookie_uid.indexOf('#prod');
	if(index_end!=-1) cookie_uid = cookie_uid.substring(0,index_end);
	
	if(cookie_uid) {
		//存个180天
		setCookie(fx_uid, cookie_uid, 'd180');
	} else {
		cookie_uid = getCookie(fx_uid);
		if(!cookie_uid) setCookie(fx_uid,0,'d180');
	}
	return cookie_uid;
}

function ifLogin(callback, toURL) {
	getFxUid();
	$.ajax({
		type: "post",
		url: base_api + 'user/login/check-login',
		dataType: "json",
		async: false,
		success: function(res_data) {
			ZZLog(res_data);
			if(false == res_data['flag']) {
				if(codeNotLogin == res_data['code']) {
					if(toURL) {
						toURL = encodeURIComponent('/web/shop/' + toURL);
						location.href = res_data['url'] + toURL;
					} else {
						location.href = res_data['url'] + '/web/shop/reserve_prod.html ';
					}
				} else {
					alert(res_data['msg']);
				}
			} else {
				if(typeof(callback) == "function") {
					callback(true);
				}
			}
		},
		error: function() {
			alert(infoApiError);
		}
	});
}

function setCookie(name, value, time) {
	var strsec = getsec(time);
	var exp = new Date();
	exp.setTime(exp.getTime() + strsec * 1);
	document.cookie = name + "=" + escape(value) + ";path=/;expires=" + exp.toGMTString();

	function getsec(str) {
		var str1 = str.substring(1, str.length) * 1;
		var str2 = str.substring(0, 1);
		if(str2 == "s") {
			return str1 * 1000;
		} else if(str2 == "h") {
			return str1 * 60 * 60 * 1000;
		} else if(str2 == "d") {
			return str1 * 24 * 60 * 60 * 1000;
		}
	}
}

function delCookie(name) {
	var exp = new Date();
	exp.setTime(exp.getTime() - 1);
	var cval = getCookie(name);
	if(cval != null)
		document.cookie = name + "=" + cval + ";path=/;expires=" + exp.toGMTString();
}

function getCookie(name) {
	var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
	if(arr = document.cookie.match(reg))
		return unescape(arr[2]);
	else
		return null;
}

//读取sessionStorage缓存
function setSessionStorage(objName, objValue) {
	var sto = window.sessionStorage;
	if(sto)
		sto.setItem(objName, objValue);
}
//读取字符串类型的本地缓存
function getSessionStorage(objName) {
	var ret = '';
	var sto = window.sessionStorage;
	if(sto)
		ret = sto.getItem(objName);
	return ret;
}
//清除本地缓存，如没指定名称则为清空所有缓存
function clearSessionStorage(objName) {
	var sto = window.sessionStorage;
	if(sto) {
		if(objName)
			sto.removeItem(objName);
		else
			sto.clear();
	}
}
//设置Json类型的sessionStorage缓存
function setJsonSessionStorage(objName, json) {
	if(json)
		setSessionStorage(objName, JSON.stringify(json));
}
//读取Json类型的sessionStorage缓存
function getJsonSessionStorage(objName) {
	var ret = null;
	var str = getSessionStorage(objName);
	if(str)
		ret = JSON.parse(str);
	return ret;
}
//设置字符串类型的本地缓存
function setStorage(objName, objValue) {
	var sto = window.localStorage;
	if(sto)
		sto.setItem(objName, objValue);
}
//读取字符串类型的本地缓存
function getStorage(objName) {
	var ret = '';
	var sto = window.localStorage;
	if(sto)
		ret = sto.getItem(objName);
	return ret;
}

//清除本地缓存，如没指定名称则为清空所有缓存
function clearStorage(objName) {
	var sto = window.localStorage;
	if(sto) {
		if(objName)
			sto.removeItem(objName);
		else
			sto.clear();
	}
}
//设置Json类型的本地缓存
function setStorJson(objName, json) {
	if(json)
		setStorage(objName, JSON.stringify(json));
}
//读取Json类型的本地缓存
function getStorJson(objName) {
	var ret = null;
	var str = getStorage(objName);
	if(str)
		ret = JSON.parse(str);
	return ret;
}
// 打印日志
//function ZZLog(...type) {
//	var isLog = true; //是否打印
//	if(isLog) {
//		type.forEach(v => {
//			if(typeof(v) == 'object') {
//				console.log(v);
//			} else {
//				console.log('%c' + v, 'color:#666699');
//			}
//		})
//	}
//}
function ZZLog() {
	var isLog = true; //是否打印
	var isSign = false; //是否签名
	if(isLog) {
		if(isSign) {
			//如果要加前缀
			var args = Array.prototype.slice.call(arguments);
			args.unshift('[ZZCX:DeBug:Log]');
			console.log.apply(console, args);
		} else {
			console.log.apply(console, arguments);
		}
	}
}

//js判断一个日期是星期几 传入 年/月/日
function getWeekDay(day) {
	var a = new Array("日", "一", "二", "三", "四", "五", "六");
	var week = new Date(day).getDay();
	var str = "星期" + a[week];
	return str;
}

//获取前一天日期 传入指定时间
function before(d) {
	d = new Date(d);
	d = +d - 10006060 / 3 * 24;
	d = new Date(d);
	//格式化
	var year = ";" + d.getFullYear() + ";";
	var month = ";" + (d.getMonth() + 0 + 1) + ";";
	var day = ";" + d.getDate() + ";";
	if(year.length < 4) {
		year = "0" + year;
	}
	if(month.length < 4) {
		month = "0" + month;
	}
	if(day.length < 4) {
		day = "0" + day;
	}
	var datestr = year + "-" + month + "-" + day;
	datestr = datestr.replace(/;/g, "");
	return datestr;
}

// 获取指定日期的前后几天
function getDateByDay(addDayCount, curDate) {
	var d = new Date(curDate);
	d.setDate(d.getDate() + addDayCount); //获取AddDayCount天后的日期
	//格式化
	var year = ";" + d.getFullYear() + ";";
	var month = ";" + (d.getMonth() + 0 + 1) + ";";
	var day = ";" + d.getDate() + ";";
	if(year.length < 4) {
		year = "0" + year;
	}
	if(month.length < 4) {
		month = "0" + month;
	}
	if(day.length < 4) {
		day = "0" + day;
	}
	var datestr = year + "-" + month + "-" + day;
	datestr = datestr.replace(/;/g, "");
	return datestr;
}

//获取后一天日期  传入指定时间
function after(d) {
	d = new Date(d);
	d = +d + 10006060 / 3 * 24;
	d = new Date(d);
	//格式化
	var year = ";" + d.getFullYear() + ";";
	var month = ";" + (d.getMonth() + 0 + 1) + ";";
	var day = ";" + d.getDate() + ";";
	if(year.length < 4) {
		year = "0" + year;
	}
	if(month.length < 4) {
		month = "0" + month;
	}
	if(day.length < 4) {
		day = "0" + day;
	}
	var datestr = year + "-" + month + "-" + day;
	datestr = datestr.replace(/;/g, "");
	return datestr;
}

// 从URL上获得参数
function getPar(par) {
	//获取当前URL
	var local_url = document.location.href;
	local_url = decodeURI(local_url);
	//获取要取得的get参数位置
	var get = local_url.indexOf(par + "=");
	if(get == -1) {
		return "";
	}
	//截取字符串
	var get_par = local_url.slice(par.length + get + 1);
	//判断截取后的字符串是否还有其他get参数
	var nextPar = get_par.indexOf("&");
	if(nextPar != -1) {
		get_par = get_par.slice(0, nextPar);
	}
	return get_par;
}

//得到时间组合
function getDateTime(nTypeFlag) {
	var tNowTime = new Date();
	var myYear = ';' + tNowTime.getFullYear() + ';';
	var myMonth = ';' + (tNowTime.getMonth() + 1 - 0) + ';';
	var myDay = ';' + tNowTime.getDate() + ';';
	var myHour = ';' + tNowTime.getHours() + ';';
	var myMinu = ';' + tNowTime.getMinutes() + ';';
	var mySecond = ';' + tNowTime.getSeconds() + ';';

	if(myMonth.length < 4) myMonth = '0' + myMonth;
	if(myDay.length < 4) myDay = '0' + myDay;
	if(myHour.length < 4) myHour = '0' + myHour;
	if(myMinu.length < 4) myMinu = '0' + myMinu;
	if(mySecond.length < 4) mySecond = '0' + mySecond;

	var cNewTimeStr;
	//alert(tNowTime);
	switch(nTypeFlag + 1 - 1) {
		case 0:
			cNewTimeStr = myYear + '-' + myMonth + '-' + myDay;
			break;
		case 1:
			cNewTimeStr = myYear + myMonth + myDay;
			break;
		case 2:
			cNewTimeStr = myHour + ':' + myMinu + ':' + mySecond;
			break;
		case 3:
			cNewTimeStr = myHour + myMinu + mySecond;
			break;
		case 4:
			cNewTimeStr = myYear + myMonth + myDay + myHour + myMinu + mySecond;
			break;
		case 5:
			cNewTimeStr = myYear + '年' + myMonth + '月' + myDay + '日';
			break;
		case 6:
			cNewTimeStr = myYear;
			break;
		case 7:
			cNewTimeStr = myYear + '-' + myMonth;
			break;
		case 8: //得到上一个月的今天
			var date = getDateTime(0);
			var arr = date.split('-');
			var year = arr[0]; //获取当前日期的年份
			var month = arr[1]; //获取当前日期的月份
			var day = arr[2]; //获取当前日期的日
			var days = new Date(year, month, 0);
			days = days.getDate(); //获取当前日期中月的天数
			var year2 = year;
			var month2 = parseInt(month) - 1;
			if(month2 == 0) {
				year2 = parseInt(year2) - 1;
				month2 = 12;
			}
			var day2 = day;
			var days2 = new Date(year2, month2, 0);
			days2 = days2.getDate();
			if(day2 > days2) {
				day2 = days2;
			}
			if(month2 < 10) {
				month2 = '0' + month2;
			}
			cNewTimeStr = year2 + '-' + month2 + '-' + day2;
			break;
		case 9: //得到下一个月的今天
			var date = getDateTime(0);
			var arr = date.split('-');
			var year = arr[0]; //获取当前日期的年份
			var month = arr[1]; //获取当前日期的月份
			var day = arr[2]; //获取当前日期的日
			var days = new Date(year, month, 0);
			days = days.getDate(); //获取当前日期中的月的天数
			var year2 = year;
			var month2 = parseInt(month) + 1;
			if(month2 == 13) {
				year2 = parseInt(year2) + 1;
				month2 = 1;
			}
			var day2 = day;
			var days2 = new Date(year2, month2, 0);
			days2 = days2.getDate();
			if(day2 > days2) {
				day2 = days2;
			}
			if(month2 < 10) {
				month2 = '0' + month2;
			}

			var t2 = year2 + '-' + month2 + '-' + day2;
			return t2;
			break;
		case 10: //昨日日期
			//获取前一天日期
			return before(getDateTime(0));

			function before(d) {
				d = new Date(d);
				d = +d - 10006060 / 3 * 24;
				d = new Date(d);
				//return d;
				//格式化
				var year = ";" + d.getFullYear() + ";";
				var month = ";" + (d.getMonth() + 0 + 1) + ";";
				var day = ";" + d.getDate() + ";";
				if(year.length < 4) {
					year = "0" + year;
				}
				if(month.length < 4) {
					month = "0" + month;
				}
				if(day.length < 4) {
					day = "0" + day;
				}
				var datestr = year + "-" + month + "-" + day;
				datestr = datestr.replace(/;/g, "");
				return datestr;
				// return d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate();

			}
			break;
		case 11: //明日日期
			//获取后一天日期
			return after(getDateTime(0));

			function after(d) {
				d = new Date(d);
				d = +d + 10006060 / 3 * 24;
				d = new Date(d);
				//return d;
				//格式化
				var year = ";" + d.getFullYear() + ";";
				var month = ";" + (d.getMonth() + 0 + 1) + ";";
				var day = ";" + d.getDate() + ";";
				if(year.length < 4) {
					year = "0" + year;
				}
				if(month.length < 4) {
					month = "0" + month;
				}
				if(day.length < 4) {
					day = "0" + day;
				}
				var datestr = year + "-" + month + "-" + day;
				datestr = datestr.replace(/;/g, "");
				return datestr;
				// return d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate();

			}

			break;
		case 12: //本周起始日期
			return getWeekStartDate()

			function getWeekStartDate() {
				var now = new Date(); //当前日期
				var nowDayOfWeek = now.getDay(); //今天本周的第几天
				var nowDay = now.getDate(); //当前日
				var nowMonth = now.getMonth(); //当前月
				var nowYear = now.getYear(); //当前年
				nowYear += (nowYear < 2000) ? 1900 : 0; //

				var lastMonthDate = new Date(); //上月日期
				lastMonthDate.setDate(1);
				lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
				var lastYear = lastMonthDate.getYear();
				var lastMonth = lastMonthDate.getMonth();

				var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
				return formatDate(weekStartDate);
			}

			break;
		case 13: //本周结束日期
			return getWeekEndDate()

			function getWeekEndDate() {
				var now = new Date(); //当前日期
				var nowDayOfWeek = now.getDay(); //今天本周的第几天
				var nowDay = now.getDate(); //当前日
				var nowMonth = now.getMonth(); //当前月
				var nowYear = now.getYear(); //当前年
				nowYear += (nowYear < 2000) ? 1900 : 0; //

				var lastMonthDate = new Date(); //上月日期
				lastMonthDate.setDate(1);
				lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
				var lastYear = lastMonthDate.getYear();
				var lastMonth = lastMonthDate.getMonth();
				var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));
				return formatDate(weekEndDate);
			}

			break;
		case 14: //本月开始日期
			return getMonthStartDate()

			function getMonthStartDate() {
				var now = new Date(); //当前日期
				var nowDayOfWeek = now.getDay(); //今天本周的第几天
				var nowDay = now.getDate(); //当前日
				var nowMonth = now.getMonth(); //当前月
				var nowYear = now.getYear(); //当前年
				nowYear += (nowYear < 2000) ? 1900 : 0; //

				var lastMonthDate = new Date(); //上月日期
				lastMonthDate.setDate(1);
				lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
				var lastYear = lastMonthDate.getYear();
				var lastMonth = lastMonthDate.getMonth();
				var monthStartDate = new Date(nowYear, nowMonth, 1);
				return formatDate(monthStartDate);
			}

			break;

		case 15: //本月结束日期
			return getMonthEndDate();

			function getMonthEndDate() {
				var now = new Date(); //当前日期
				var nowDayOfWeek = now.getDay(); //今天本周的第几天
				var nowDay = now.getDate(); //当前日
				var nowMonth = now.getMonth(); //当前月
				var nowYear = now.getYear(); //当前年
				nowYear += (nowYear < 2000) ? 1900 : 0; //

				var lastMonthDate = new Date(); //上月日期
				lastMonthDate.setDate(1);
				lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
				var lastYear = lastMonthDate.getYear();
				var lastMonth = lastMonthDate.getMonth();
				//获得某月的天数
				function getMonthDays(myMonth) {
					var monthStartDate = new Date(nowYear, myMonth, 1);
					var monthEndDate = new Date(nowYear, myMonth + 1, 1);
					var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
					return days;
				}
				var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));
				return formatDate(monthEndDate);
			}

			break;

		default:
			cNewTimeStr = myYear + '-' + myMonth + '-' + myDay + ' ' + myHour + ':' + myMinu + ':' + mySecond;
			break;
	}

	cNewTimeStr = cNewTimeStr.replace(/;/g, "");

	return cNewTimeStr;
}
//case 12,13的规整函数
function formatDate(date) {
	var myyear = date.getFullYear();
	var mymonth = date.getMonth() + 1;
	var myweekday = date.getDate();

	if(mymonth < 10) {
		mymonth = "0" + mymonth;
	}
	if(myweekday < 10) {
		myweekday = "0" + myweekday;
	}
	return(myyear + "-" + mymonth + "-" + myweekday);
}

//得到url上的参数
function getPar(par) {
	var local_url = document.location.href;
	local_url = decodeURI(local_url);
	var get = local_url.indexOf(par + "=");
	if(get == -1) {
		return "";
	}
	var get_par = local_url.slice(par.length + get + 1);
	var nextPar = get_par.indexOf("&");
	if(nextPar != -1) {
		get_par = get_par.slice(0, nextPar);
	}
	return get_par;
}
//截取日期参数
function getMonthAndDayByDate(datestr, info) {
	var dateAry = datestr.split('-');
	var cNewTimeStr;
	switch(info) {
		case 'year':
			cNewTimeStr = dateAry[0];
			break;
		case 'month':
			cNewTimeStr = dateAry[1];
			break;
		default:
			cNewTimeStr = dateAry[2];
			break;
	}
	return cNewTimeStr;
}

//html存入json
function setJsonHtml(data) {
	if(typeof(data) == "object") {
		var jsonArrayFinal = JSON.stringify(data);
		jsonArrayFinal = jsonArrayFinal.replace(/ /g, '');
		//		jsonArrayFinal = jsonArrayFinal.replace(/"/g, '&quot;');
		return jsonArrayFinal;
	}
}

//取出html存入的json
function getJsonHtml(data) {
	if(data) {
		var data_info = JSON.parse(data);
		return data_info;
	}
}

//比较全的身份证验证
function isCardID(sId) {
	var aCity = {
		11: "北京",
		12: "天津",
		13: "河北",
		14: "山西",
		15: "内蒙古",
		21: "辽宁",
		22: "吉林",
		23: "黑龙江",
		31: "上海",
		32: "江苏",
		33: "浙江",
		34: "安徽",
		35: "福建",
		36: "江西",
		37: "山东",
		41: "河南",
		42: "湖北",
		43: "湖南",
		44: "广东",
		45: "广西",
		46: "海南",
		50: "重庆",
		51: "四川",
		52: "贵州",
		53: "云南",
		54: "西藏",
		61: "陕西",
		62: "甘肃",
		63: "青海",
		64: "宁夏",
		65: "新疆",
		71: "台湾",
		81: "香港",
		82: "澳门",
		91: "国外"
	}
	var iSum = 0;
	var info = "";
	if(!/^\d{17}(\d|x)$/i.test(sId)) return "你输入的身份证长度或格式错误";
	sId = sId.replace(/x$/i, "a");
	if(aCity[parseInt(sId.substr(0, 2))] == null) return "你的身份证地区非法";
	sBirthday = sId.substr(6, 4) + "-" + Number(sId.substr(10, 2)) +
		"-" + Number(sId.substr(12, 2));
	var d = new Date(sBirthday.replace(/-/g, "/"));
	if(sBirthday != (d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate())) return "身份证上的出生日期非法";
	for(var i = 17; i >= 0; i--) iSum += (Math.pow(2, i) % 11) * parseInt(sId.charAt(17 - i), 11);
	if(iSum % 11 != 1) return "你输入的身份证号非法";
	//aCity[parseInt(sId.substr(0,2))]+","+sBirthday+","+(sId.substr(16,1)%2?"男":"女");//此次还可以判断出输入的身份证号的人性别
	return true;
}