/**
 * [OEmarry] (C)2010-2099 OEMARRY.COM Inc.
 * Email: service@phpcoo.com, phpcoo@qq.com
 * This is NOT a freeware, use is subject to license terms
 * $ Last update 2016-07-13 by OEdev $
*/
/*----------- 支持div滚动条 方式1 touch begin  noBarsOnTouchScreen(divid)---------------*/
function noBarsOnTouchScreen(arg) {
	var elem, tx, ty;
	if('ontouchstart' in document.documentElement ) {
		if (elem = document.getElementById(arg)) {
			//elem.style.overflow = 'hidden';
			elem.ontouchstart = ts;
			elem.ontouchmove = tm;
		}
	}

	function ts(e) {
		var tch;
		if(e.touches.length == 1) {
			e.stopPropagation();
			tch = e.touches[ 0 ];
			tx = tch.pageX;
			ty = tch.pageY;
		}
	}
	function tm(e) {
		var tch;
		if (e.touches.length == 1 ) {
			e.preventDefault();
			e.stopPropagation();
			tch = e.touches[ 0 ];
			this.scrollTop +=  ty - tch.pageY;
			ty = tch.pageY;
		}
	}
}


function isTouchDevice(){
    try{
        document.createEvent("TouchEvent");
        return true;
    }catch(e){
        return false;
    }
}
//支持div滚动条 方式2  touchScroll("MyElement");
function touchScroll(id){
    if(isTouchDevice()){ //if touch events exist...
        var el=document.getElementById(id);
        var scrollStartPos=0;

        document.getElementById(id).addEventListener("touchstart", function(event) {
            scrollStartPos=this.scrollTop+event.touches[0].pageY;
            event.preventDefault();
        },false);

        document.getElementById(id).addEventListener("touchmove", function(event) {
            this.scrollTop=scrollStartPos-event.touches[0].pageY;
            event.preventDefault();
        },false);
    }
}
/*----------- andriod touch end ---------------*/


//判断字符长度，一个汉字为2个字符
function strlen(s){
	var l = 0;
	var a = s.split("");
	for (var i=0;i<a.length;i++){
		if (a[i].charCodeAt(0)<299){
			l++;
		}else{
			l+=2;
		}
	}
	return l;
}

//判断字体个数
function strQuantity(s){
	var l = 0;
	var a = s.split("");
	for (var i=0;i<a.length;i++){
		if (a[i].charCodeAt(0)<299){
			l++;
		}else{
			l++;
		}
	}
	return l;
}

//随机数
function get_rndnum(n) {
	var chars = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
	var res = "";
	for(var i = 0; i < n ; i ++) {
		var id = Math.ceil(Math.random()*35);
		res += chars[id];
	}
	return res;
}

/*----------------------- 公共弹出层 Begin ----------------------*/
//popbox position
function popMakerCenter(){
	//$('.pop-layout').css("display","block");
	$('.pop-layout').css("position","absolute");
	$('.pop-layout').css("top", Math.max(0, (($(window).height() - $('.pop-layout').outerHeight()) / 2) + $(window).scrollTop()) + "px");
	$('.pop-layout').css("left", Math.max(0, (($(window).width() - $('.pop-layout').outerWidth()) / 2) + $(window).scrollLeft()) + "px");
}

//popbox close
function layoutPopClose() {
	//$('.pop-layout').css("display","none");
	$('.pop-shade').fadeOut(200);
	$('.pop-layout').fadeOut(200);
}

//popbox
function layoutPop() {
	$('.pop-shade').fadeIn(100);
	popMakerCenter();
	$('.pop-layout').fadeIn(200);

}
/*----------------------- 公共弹出层 End -----------------------*/


//url location to
function goUrl(url) {
	$("#loading").fadeIn(1);
	window.location.href = url;
}

//url reload
function urlReload() {
	$("#loading").fadeIn(1);
	window.location.reload();
}

//url go back
function goBack() {
	window.history.go(-1);
	$("#loading").fadeOut();
}

//url go back refresh
function goBackRefresh() {
	window.history.go(-1);
	opener.location.reload();
}

/*----------------------- 单选、多选 弹出层 Begin ----------------------*/
//tabpop position
/**
 * 层自适应居中
 * @param:: string $tabid DIV 
 * @return:: 
*/
function tabPopMarginAuto(tabid){
	$('#'+tabid).css("position","absolute");
	$('#'+tabid).css("top", Math.max(0, (($(window).height() - $('#'+tabid).outerHeight()) / 2) + $(window).scrollTop()) + "px");
	$('#'+tabid).css("left", Math.max(0, (($(window).width() - $('#'+tabid).outerWidth()) / 2) + $(window).scrollLeft()) + "px");
}

/**
 * 关闭弹出层
 * @param:: string $shade 遮罩层ID
 * @param:: string $tabid DIV
 * @return:: NULL;
*/
function tabPopClose(shade, tabid) {
	//$('#'+shade).fadeOut(200);
	$('#'+shade).show();
	$('#'+tabid).fadeOut(200);
	document.body.style.overflow = "";//启用浏览器滚动条
	document.documentElement.style.overflow = "";//启用浏览器滚动
	androidOpenScroll();
}

/**
 * 弹出层
 * @param:: string $shade 遮罩层ID
 * @param:: string $tabid DIV
 * @return:: NULL;
*/
function tabPopup(shade, tabid) {
	//$('#'+shade).fadeIn(100); //遮罩效果
	$('#'+shade).show(); //遮罩效果
	tabPopMarginAuto(tabid); //自适应居中
	$('#'+tabid).fadeIn(200); //显示
	document.body.style.overflow = "hidden";//禁止浏览器滚动
	document.documentElement.style.overflow = "hidden";//禁止浏览器滚动
	androidCloseScroll();
	$('html,body').animate({scrollTop: '0px'}, 100);
	$('#tab_shade').bind("touchmove",function(e){
        e.preventDefault();
    });
	$('#tab_shade').bind("touchmove",function(e){  
		e.stopPropagation();  
	});
}
/*----------------------- 单选、多选 弹出层 End ----------------------*/



/*----------------------- 单选、多选 弹出层 Begin ----------------------*/
/**
 * 层自适应居中
 * @param:: string $tabid DIV 
 * @return:: 
*/
function varPopMarginAuto(tabid){
	$('#'+tabid).css("position","absolute");
	$('#'+tabid).css("top", "100px");
	$('#'+tabid).css("left", Math.max(0, (($(window).width() - $('#'+tabid).outerWidth()) / 2) + $(window).scrollLeft()) + "px");
}


/**
 * 关闭var参数层 单选
 * @param:: string $shade 遮罩层ID
 * @param:: string $tabid DIV
 * @return:: NULL;
*/
function varRadioPopClose() {
	//$("#varpop_shade").fadeOut(200);
	$("#varpop_shade").hide();
	$("#var_box").fadeOut(200);
	document.body.style.overflow = "";//启用浏览器滚动条
	document.documentElement.style.overflow = "";//启用浏览器滚动
	//androidOpenScroll();
	$("#var_loading").text("");
	$("#var_data").html("");
}

/**
 * 弹出var参数层 单选
 * @param:: string $title 标题
 * @param:: string $item 参数
 * @param:: string $input inputID
 * @param:: string $a 动作 默认为
 * @param:: string $module 模块 profile/passport/search/cond...
 * @param:; string $text 默认的option 显示文本
 * @return:: NULL;
*/
function varRadioPopup(title, item, input, a, module, text) {
	if(typeof(input) == 'undefined') {input = item;}
	if(typeof(a) == 'undefined') {a = '';}
	if(typeof(module) == 'undefined') {module = 'profile';}
	if(typeof(text) == 'undefined') {text = '';}
	$('#varpop_shade').show(); //遮罩效果
	varPopMarginAuto("var_box"); //自适应居中
	$("#var_box").fadeIn(200); //显示
	document.body.style.overflow = "hidden";//禁止浏览器滚动
	document.documentElement.style.overflow = "hidden";//禁止浏览器滚动
	noBarsOnTouchScreen("var_data");
	$("#var_loading").text("Loading...");
	$("#var_title").text(title);
	var value = $("#"+input).val();
	//POST
	$.ajax({
		type: "POST",
		url: WAP_APPFILE+"&c=var",
		cache: false,
		data: {a:a, module:module, item:item, input:input, value:value, text:text,  r:get_rndnum(8)},
		dataType: "json",
		success: function($data) {
			$json = eval($data);
			$response = $json.response;
			$result = $json.result;
			if ($response == '1') {
				$("#var_loading").text("");
				$("#var_data").html($result); //加载数据
				tabPopMarginAuto("var_box"); //重新居中
				noBarsOnTouchScreen("var_box"); //支持DIV滚动条
			}
			else {
				$("#var_loading").text("");
				ToastShow("系统繁忙，请稍后再试");
			}
		},
		error: function() {
		}
	});
}


/**
 * 关闭var参数层 多选
 * @param:: string $shade 遮罩层ID
 * @param:: string $tabid DIV
 * @return:: NULL;
*/
function varCheckboxPopClose() {
	$("#varpop_shade").hide();
	$("#multi_box").fadeOut(200);
	document.body.style.overflow = "";//启用浏览器滚动条
	document.documentElement.style.overflow = "";//启用浏览器滚动
	$("#multi_loading").text("");
	$("#multi_data").html("");
}

/**
 * 弹出var参数层 多选
 * @param:: string $title 标题
 * @param:: string $item 参数
 * @param:: string $input inputID
 * @param:: string $module 模块 profile/passport/search/cond...
 * @return:: NULL;
*/
function varCheckboxPopup(title, item, input, module) {
	if(typeof(input) == 'undefined') {input = item;}
	if(typeof(module) == 'undefined') {module = 'profile';}
	$('#varpop_shade').show();
	varPopMarginAuto("multi_box"); //自适应居中
	$("#multi_box").fadeIn(200); //显示
	$("#multi_loading").text("Loading...");
	$("#multi_title").text(title);
	var value = $("#"+input).val();
	//POST
	$.ajax({
		type: "POST",
		url: WAP_APPFILE+"&c=var&a=varmulti",
		cache: false,
		data: {module:module, item:item, input:input, value:value,  r:get_rndnum(8)},
		dataType: "json",
		success: function($data) {
			$json = eval($data);
			$response = $json.response;
			$result = $json.result;
			if ($response == '1') {
				$("#multi_loading").text("");
				$("#multi_data").html($result); //加载数据
				tabPopMarginAuto("multi_box"); //重新居中
				noBarsOnTouchScreen("varpop-list"); //支持DIV滚动条
			}
			else {
				$("#multi_loading").text("");
				ToastShow("系统繁忙，请稍后再试");
			}
		},
		error: function() {
		}
	});
}

/**
 * 参数范围选框，如性别、年龄、身高、学历...等 关闭层
 * @return:: NULL;
*/
function varRangePopClose() {
	$("#varpop_shade").hide();
	$("#range_box").fadeOut(200);
	document.body.style.overflow = "";//启用浏览器滚动条
	document.documentElement.style.overflow = "";//启用浏览器滚动
	$("#range_loading").text("");
	$("#range_data").html("");
}

/**
 * 参数范围选框，如性别，年龄、身高、学历...等 打开层 用于高级搜索、择友条件等
 * @param:: string $title 标题
 * @param:: string $item 参数
 * @param:: string $input inputID
 * @return:: NULL;
*/
function varRangePopup(title, item, input1, input2, text) {
	if(typeof(input1) == 'undefined') {input1 = item;} //支持性别、是否有头像等选项
	if(typeof(input2) == 'undefined') {input2 = "";}
	if(typeof(text) == 'undefined') {text = "";}
	$('#varpop_shade').show(); //遮罩效果
	varPopMarginAuto("range_box"); //自适应居中
	$("#range_box").fadeIn(200); //显示
	$("#range_loading").text("Loading...");
	$("#range_title").text(title);
	var value1 = $("#"+input1).val(); //值1
	if (input2 == "") {
		var value2 = "";
	}
	else {
		var value2 = $("#"+input2).val(); //值2
	}
	//POST
	$.ajax({
		type: "POST",
		url: WAP_APPFILE+"&c=var&a=varrange",
		cache: false,
		data: {item:item, input1:input1, input2:input2, value1:value1, value2:value2, text:text, r:get_rndnum(8)},
		dataType: "json",
		success: function($data) {
			$json = eval($data);
			$response = $json.response;
			$result = $json.result;
			if ($response == '1') {
				$("#range_loading").text("");
				$("#range_data").html(result); //加载数据
				tabPopMarginAuto("range_box"); //重新居中
				noBarsOnTouchScreen("varpop-list"); //支持DIV滚动条
			}
			else {
				$("#range_loading").text("");
				ToastShow("系统繁忙，请稍后再试");
			}
		},
		error: function() {
		}
	});
}
/*----------------------- 单选、多选 弹出层 End ----------------------*/

/**
 * 关闭所在地区层
 * @return:: NULL;
*/
function areaPopClose() {
	$("#varpop_shade").hide();
	$("#area_box").fadeOut(200);
	$("#area_loading").text("");
	$("#area_data").html("");
}

/**
 * 弹出所在地区选项层
 * @param:: string $title 标题
 * @param:: string $item 参数
 * @param:: string $a 动作 默认为
 * @param:: string $module 模块 profile/passport/search/cond...
 * @return:: NULL;
*/
function areaPopup(title, item, a, module) {
	if(typeof(a) == 'undefined') {a = 'area';}
	if(typeof(module) == 'undefined') {module = 'profile';}
	//$('#varpop_shade').fadeIn(100); //遮罩效果
	$('#varpop_shade').show(); //遮罩效果
	varPopMarginAuto("area_box"); //自适应居中
	$("#area_box").fadeIn(200); //显示
	$("#area_loading").text("Loading...");
	$("#area_title").text(title);
	var dist1 = $("#"+item+"1").val(); //一级地区value
	var dist2 = $("#"+item+"2").val(); //二级地区value
	var dist3 = $("#"+item+"3").val(); //三级地区value
	//POST
	$.ajax({
		type: "POST",
		url: WAP_APPFILE+"&c=var",
		cache: false,
		data: {a:a, module:module, item:item, dist1:dist1, dist2:dist2, dist3:dist3, r:get_rndnum(8)},
		dataType: "json",
		success: function($data) {
			$json = eval($data);
			$response = $json.response;
			$result = $json.result;
			if ($response == '1') {
				$("#area_loading").text("");
				$("#area_data").html(result); //加载数据
				tabPopMarginAuto("area_box"); //重新居中
			}
			else {
				$("#area_loading").text("");
				ToastShow("系统繁忙，请稍后再试");
			}
		},
		error: function() {
		}
	});
}



//上传图片 AJAX提交 支持IE
function oemarryUploadImg($file, $url, $audit) {
	if (typeof($audit) == "undefined") {
		$audit = "0";
	}
	if (_oemarryCheckImgType($file) == true) {
		$obj_loading = $("#"+$file+"_loading");
		if ($obj_loading.length > 0) {
			$obj_loading.show();
		}
		$.ajaxFileUpload({
			"url": $url,
			"secureuri": false,
			"fileElementId": $file,
			"dataType": "json",
			"data": {
				uploadpart: $file
			},
			async: this.async,
			beforeSend: function ($xmlobj) {},
			complete: function ($data) {},
			success: function ($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				$imgthumb = $json.thumbimg;
				$imgurl = $json.uploadimg;

				if ($response == "1") {
					if ($("#"+$file+"_thumb").length > 0) {
						$("#"+$file+"_thumb").val($imgthumb);
					}
					if ($("#"+$file+"_url").length > 0) {
						$("#"+$file+"_url").val($imgurl);
					}
					if ($("#"+$file+"_preview").length > 0) {
						$img = _ROOT_PATH+$imgurl+"?r="+get_rndnum(8);
						$("#"+$file+"_preview").attr("src", $img);
					}
					if ($obj_loading.length > 0) {
						$obj_loading.hide();
					}
					ToastShow("上传成功");
					if ($audit == "1") {
						$("#"+$file+"_flag_tips").html("审核中");
					}
					else {
						$("#"+$file+"_flag_tips").html("已通过");
					}
				}
				else {
					if ($obj_loading.length > 0) {
						$obj_loading.hide();
					}
					if ($result.length > 0 ) {
						ToastShow($result);
					}
					else {
						ToastShow("上传失败，请检查图片。");
					}
				}

			},
			error: function ($data, e, b) {
				if ($obj_loading.length > 0) {
					$obj_loading.hide();
				}
				ToastShow("系统繁忙，请稍后再试");
			}
		});

	}
}


//判断图片格式
function _oemarryCheckImgType(filepart) {
	$res = true;
	$oe_file_obj = $("#"+filepart);
	$oe_file_val = $oe_file_obj.val();
	$oe_file_size = 0;
	//$oe_sys_max_size = (_UPLOAD_MAXSIZE*1024*1024);
	if ($oe_file_val.length == 0) {
		ToastShow("请选择要上传的图片");
		$res = false;
	}
	if(!/.(gif|GIF|jpg|JPG|jpeg|JPEG|png|PNG)$/.test($oe_file_val)){ 
		ToastShow("图片格式不正确");
		$res = false;
	}
	return $res;
}


//获取当前时间 yyyy-MM-dd HH:MM:SS 2016.07.14
function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
            + " " + date.getHours() + seperator2 + date.getMinutes()
            + seperator2 + date.getSeconds();
    return currentdate;
}


/**
 * 判断是否为合法中国手机号码
 * @param:: string mobile 手机号码
 * @return:: bool true/false
*/
function isMobile(mobile) {
	if (mobile.length != 11) {
		return false;
	}
	else {
		var is = false;
		var reg0 = /^13\d{5,9}$/; //13段号
		var reg1 = /^15\d{5,9}$/; //15段号
		var reg2 = /^18\d{5,9}$/; //18段号
		var reg3 = /^14\d{5,9}$/; //14段号
		var reg4 = /^17\d{5,9}$/; //17段号
		

		if (reg0.test(mobile)) is = true;
		if (reg1.test(mobile)) is = true;
		if (reg2.test(mobile)) is = true;
		if (reg3.test(mobile)) is = true;
		if (reg4.test(mobile)) is = true;

		if (!is) {
			return false;
		}
		else {
			return true;
		}
	}
}

/**
* 判断是否为合法QQ号码 
* @param:: string qq QQ号码 5-11位数字
* @return:: bool true/false
*/
function isQQ(qq){
	if (qq.length < 5 || qq.length > 11) {
		return false;
	}
	else {
		var is = false;
		var reg = /^[0-9]\d{4,12}$/;
		if (reg.test(qq))is = true;

		if (!is) {
			return false;
		}
		else {
			return true;
		}
	}
}