// 本js在jquery后加载
var charset = document.charset;
var userAgent = navigator.userAgent.toLowerCase();
var is_opera = userAgent.indexOf('opera') != -1 && opera.version();
var is_moz = (navigator.product == 'Gecko') && userAgent.substr(userAgent.indexOf('firefox') + 8, 3);
var is_ie = (userAgent.indexOf('msie') != -1 && !is_opera) && userAgent.substr(userAgent.indexOf('msie') + 5, 3);
var is_chrome = userAgent.indexOf('chrome') != -1 && userAgent.substr(userAgent.indexOf('chrome') + 7, 3);
var is_safari = userAgent.indexOf('safari') != -1 && userAgent.substr(userAgent.indexOf('safari') + 7, 3);

// 去空格
String.prototype.trim = function() {  
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

// 判断是否为数字
function is_numeric(str) {
	var patn = /^[0-9-\/]+$/;
	if(!patn.test(str)) return false;
	return true;
}

//判断E-mail格式是否正确
function is_email(str) {
	var patn = /^[_a-zA-Z0-9\-]+(\.[_a-zA-Z0-9\-]*)*@[a-zA-Z0-9\-]+([\.][a-zA-Z0-9\-]+)+$/;
	if(!patn.test(str))	return false;
	return true;
}

//取随机值
function getRandom() {
	return Math.floor(Math.random()*1000+1);
}

//字符串长度
function mb_strlen(str) {
	var len = 0;
	for(var i = 0; i < str.length; i++) {
		len += str.charCodeAt(i) < 0 || str.charCodeAt(i) > 255 ? (document.charset == 'utf-8' ? 3 : 2) : 1;
	}
	return len;
}

function myDialog(title, body, width, height, resizable) {
	var obj = $("<div></div>").attr("title",title).attr("id","ShowDialog");   
	obj = obj.append(body);
	obj.dialog({ 
		width: width,
		height: height,
		//resizable: 'auto',
		modal: true,
		overlay: { opacity: 0.4, background: "black" } 
	});
}

// 关闭Div对话框
function myDialogClose() {
	$('#ShowDialog').dialog( "destroy" );
}

// 提示对话框
function myAlert(data) {
	var mymsg = eval('('+data+')'); //JSON转换
	if(mymsg.extra == 'login') {
		if(window.confirm(mymsg.caption+'，需要“登录”后才能继续。点击“确定”，登录网站。')) {
			window.location.href = mymsg.url;
		}
	} else if(mymsg.extra == 'myDialog') {
		myDialog(mymsg.caption, mymsg.message.replace("{LF}","\r\n"), 400, 300, true);
	} else {
		alert(mymsg.message);
	}
}

//向目标加入TAG（感谢eonbell测试）
function addtag(Id, tag) {
	var str = $("#"+Id).val();
	if ((" "+str+" ").indexOf(" "+tag+" ") == -1) {
		str += str ? ' ' + tag : tag;
		$("#"+Id).val(str);
	}
}

//增加行数(textarea)
function addrows(obj, num) {
	obj.rows += num;
}

//减少行数
function decrows(obj, num) {
	if (obj.rows>num) {
		obj.rows -= num;
	}
}

//checkbox反选
function allchecked() { 
	var check = document.getElementsByTagName('input');
	for (var i=0; i<check.length; i++) {
		if (check[i].type == 'checkbox' && !check[i].disabled) {
			check[i].checked = !check[i].checked;
		}
	}
}

//name相同的checkbox反选
function checkbox_checked(name) { 
	var check = document.getElementsByTagName('input');
	for (var i=0; i<check.length; i++) {
		if (check[i].type == 'checkbox' && check[i].name == name && !check[i].disabled) {
			check[i].checked = !check[i].checked;
		}
	}
}

//检测checkbox是否有选中
function checkchecked() {
	var check = document.getElementsByTagName('input');
	var ischecked = false;
	for (var i=0; i<check.length; i++) {
		if (check[i].type == 'checkbox' && check[i].checked && !check[i].disabled) {
			ischecked = true;
		}
	}
	if (!ischecked)
		alert('请至少选择一项！');
	return ischecked;
}

//检测radio是否有选择
function checkradio(obj) {
	if(obj) {
		var check=obj.getElementsByTagName('input');
	} else {
		var check=document.getElementsByTagName('input');
	}
	var ischecked=false;
	for (var i=0; i<check.length; i++) {
		if (check[i].type == 'radio' && check[i].checked) {
			ischecked=true;
		}
	}
	return ischecked;
}

//取单选框radio的值
function getRadio(from,name) {
	if(from) {
		var radios = from.getElementsByTagName('input');
	} else {
		var radios = document.getElementsByTagName('input');
	}
	if(!radios) return;
	var value='';
	for (var i=0; i<radios.length; i++) {
		if (radios[i].type == 'radio' && radios[i].name == name && radios[i].checked) {
			value=radios[i].value;
			break;
		}
	}
	return value;
}

//显示验证码
function show_seccode() {
    var sec = document.getElementById('seccode');
    if(!sec.style.display || sec.style.display == 'none') {
		sec.childNodes.length > 0 && sec.removeChild(sec.childNodes[0]);
		var img = document.createElement('img');
		img.src = siteurl+'seccode.php?x='+getRandom();
		img.width = 80;
		img.height = 25;
		img.style.cursor = 'pointer';
		img.title = '点击更新验证码';
		img.onclick = function() {
			this.src= siteurl+'seccode.php?x='+getRandom();
		};
		sec.appendChild(img);
		sec.style.display = 'block';
    }
}

function tot(mobnumber){
	while(mobnumber.indexOf("０")!=-1){           
		mobnumber = mobnumber.replace("０","0");        
	}                       
	while(mobnumber.indexOf("１")!=-1){             
		mobnumber = mobnumber.replace("１","1");}       
	while(mobnumber.indexOf("２")!=-1){             
		mobnumber = mobnumber.replace("２","2");}       
	while(mobnumber.indexOf("３")!=-1){             
		mobnumber = mobnumber.replace("３","3");}       
	while(mobnumber.indexOf("４")!=-1){             
		mobnumber = mobnumber.replace("４","4");}       
	while(mobnumber.indexOf("５")!=-1){             
		mobnumber = mobnumber.replace("５","5");}       
	while(mobnumber.indexOf("６")!=-1){             
		mobnumber = mobnumber.replace("６","6");}       
	while(mobnumber.indexOf("７")!=-1){             
		mobnumber = mobnumber.replace("７","7");}       
	while(mobnumber.indexOf("８")!=-1){             
		mobnumber = mobnumber.replace("８","8");}       
	while(mobnumber.indexOf("９")!=-1){             
		mobnumber = mobnumber.replace("９","9");}       
	return mobnumber;       
}

function checkByteLength(str,minlen,maxlen) {
	if (str == null) return false;
	var l = str.length;
	var blen = 0;
	for(i=0; i<l; i++) {
		if ((str.charCodeAt(i) & 0xff00) != 0) {
			blen ++;
		}
		blen ++;
	}
	if (blen > maxlen || blen < minlen) {
		return false;
	}
	return true;
}

//var unlen = username.replace(/[^\x00-\xff]/g, "**").length;

function isEqual(objid1, objid2) {
	return objid1.value == objid1.value;
}

function tabSelect(showId,idpre) {
	for(i=1; i<=10; i++) {
        var tab = $("#"+idpre+i);
        if(!tab[0]) break;
		if (i==showId) { 
			$("#btn_"+idpre+i).attr("className","selected");
			$("#"+idpre+i).css("display","");
		 } else {
			$("#btn_"+idpre+i).attr("className","unselected");
			$("#"+idpre+i).css("display","none");
		}
	}
}