/**
 * [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-04-22 by OEdev $
*/

//document tips
function dc() {
  var elements = new Array();
  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string') element = document.getElementById(element);
    if (arguments.length == 1) return element;
    elements.push(element);
  }
  return elements;
}

//随机数
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;
}


//close message
function closemsg() {
	try{dc('msgbox').innerHTML = '';dc('msgbox').style.display = 'none';}catch(e){}
}

//dmsg
function dmsg(str, id, s, t) {
	var t = t ? t : 5000;
	var s = s ? true : false;
	try{if(s){window.scrollTo(0,0);}dc('d'+id).innerHTML = '<img src="'+_ROOT_PATH+'tpl/static/images/check_error.gif" width="12" height="12" align="absmiddle"/> '+str+sound('tip');$(id).focus();}catch(e){}
	window.setTimeout(function(){dc('d'+id).innerHTML = '';}, t);
}

//d sound
function sound(file) {
	return '<div style="float:left;"><embed src="'+_ROOT_PATH+'tpl/static/images/'+file+'.swf" quality="high" type="application/x-shockwave-flash" height="0" width="0" hidden="true"/></div>';
}

//全选
function checkAll(e, itemName){
  var aa = document.getElementsByName(itemName);
  for (var i=0; i<aa.length; i++)
   aa[i].checked = e.checked;
}
function checkItem(e, allName){
  var all = document.getElementsByName(allName)[0];
  if(!e.checked) all.checked = false;
  else{
    var aa = document.getElementsByName(e.name);
    for (var i=0; i<aa.length; i++)
     if(!aa[i].checked) return;
    all.checked = true;
  }
}

//判断是否为数字
function isDigit(){
    return ((event.keyCode >= 48) && (event.keyCode <= 57));
}
function isDigit2(){
    return ((event.keyCode >= 48) && (event.keyCode <= 57) || (event.keyCode = 46));
}

//只能由汉字，字母，数字和下横线组合
function check_userstring(str){  
    var re1 = new RegExp("^([\u4E00-\uFA29]|[\uE7C7-\uE7F3]|_|[a-zA-Z0-9])*$");
	if(!re1.test(str)){
		return false;
	}else{
		return true;
	}
}

//判断字符长度，一个汉字为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 check_maxnum(id, my , num){
	var oEvent = document.getElementById('em_' + id + '_edit');
	var chks = oEvent.getElementsByTagName("INPUT");
	var count = 0;
	for(var i=0; i<chks.length; i++){
		if(chks[i].type=="checkbox"){
			if(chks[i].checked == true){
				count ++;
			}
			if(count > num){
				my.checked = false;
				alert('最多只能选择' + num + '项');
				return false;
			}
		}
	}
}


//valid Email
function isEmail(str) {
	var re = /^([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
	return re.test(str);
}


//获取当前文件名
function get_curl() {
	var curl = document.URL;
	var filename = curl.split("?")[0];
	return filename;
}


//只能输入整数
function validIntval() { 
    var keyCode = event.keyCode;  
    if ((keyCode >= 48 && keyCode <= 57)) {  
		event.returnValue = true;  
    } 
	else {  
		event.returnValue = false;  
    }  
}

//允许输入浮点数
function validFloat() {
    var keyCode = event.keyCode;  
    if ((keyCode >= 48 && keyCode <= 57) || (keyCode == 46)) {  
		event.returnValue = true;  
    } 
	else {  
		event.returnValue = false;  
    }
}


/**
 * 检测上传图片后缀是否符合
 * @param:: string $fileurl 图片地址
 * @return:: bool true/false
*/
function validImageType(fileurl) {
	var result = false;
	var right_type = new Array(".jpg", ".jpeg", ".png", ".gif");
	var right_typeLen = right_type.length;
	var imgUrl = fileurl.toLowerCase();
	var postfixLen = imgUrl.length;
	var len4 = imgUrl.substring(postfixLen-4, postfixLen);
	var len5 = imgUrl.substring(postfixLen-5, postfixLen);
	for (i = 0; i<right_typeLen; i++){
		if((len4 == right_type[i]) || (len5==right_type[i])) {
			result = true;
		}
	}
	return result;
}

/**
 * 检测是否合法的11位手机号码
 * @param:: string $mobile 手机号码
 * @return:: bool true/false
*/
function validMobile(mobile) {
	var partten = /^1[3,4,5,8]\d{9}$/;
	if (partten.test(mobile)) {
		return true;
	}
	else {
		return false;
	}
}

/**
 * 刷新验证码
 * @param:: string $id
 * @return:: string
*/
function refresh_code(id) {
	if (typeof(id) == 'undefined') {id = 'checkCodeImg';}
	var ccImg = document.getElementById(id);
	if (ccImg) {
		ccImg.src= ccImg.src + '&' +Math.random();
	}
}

/**
 * 限制输入字符个数
 * 函数，3个参数，表单名字，表单域元素名，限制字符；  
*/
function textCounter(field, countfield, maxlimit) {  
	var contentvalue = $.trim($('#'+field).val());
	if (contentvalue.length > maxlimit) {
		$('#'+field).val(contentvalue.substring(0, maxlimit)); 
	}
	else  
		{  
		var cannum = (maxlimit - contentvalue.length);
		$('#'+countfield).html(cannum); 
	}
}

/**
 * 判断是否为合法中国手机号码
 * @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;
		}
	}
}


/**
 * post ajax
 * return:: string 
*/
function oeAjaxPost(url) {
	$response = 0;
	$result = "请求失败，请检查网络！";
	if (url.length > 0) {
		$.ajax({
			type: "POST",
			url: url,
			cache: false,
			data: {r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
			},
			error: function() {

			}
		});
	}
	$rs = $response+"|"+$result;
	return $rs;

}

//时间倒计时 2016.04.22
function countDownTime($time, tips, $color, $overtime_text) {
	if (typeof($color) == "undefined") {
		$color = "blue";
	}
	if (typeof($overtime_text) == "undefined") {
		$overtime_text = "已超时";
	}
	$_html = "";
	if ($time > 0) {
		$_date = new Date();
		$_now_time = $_date.getTime()/1000; //当前时间
		$_diff_time = ($time-$_now_time); //间隔时间

		if ($_diff_time > 0) { //未过时
			$_d = parseInt($_diff_time/(24*3600));//天
			$_h = parseInt(parseInt($_diff_time%(24*3600))/3600);//小时
			$_m = parseInt(parseInt($_diff_time%(24*3600)%3600)/60);//分
			$_s = parseInt(($_diff_time%(24*3600)%60)%60);//秒

			if ($_d > 0) {
				$_html += $_d+"天 ";
			}
			if ($_h > 0) {
				$_html += $_h+"小时 ";
			}
			if ($_m > 0) {
				$_html += $_m+"分钟 ";
			}
			$_html += $_s+"秒 ";
			if ($("#"+tips).length > 0) {
				$("#"+tips).html("<font color='"+$color+"'>"+$_html+"</font>");
			}
			setTimeout(function(){
				countDownTime($time, tips, $color);
			}, 1000); //循环跳动
		}
		else {
			$_html = "<font color='"+$color+"'>"+$overtime_text+"</font>";
			if ($("#"+tips).length > 0) {
				$("#"+tips).html($_html);
			}
			return false; //终止循环
		}
	}
}
