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 xml_http_building_link = '请等待，正在建立连接...';
var xml_http_sending = '请等待，正在发送数据...';
var xml_http_loading = '请等待，正在接受数据...';
var xml_http_load_failed = '通信失败，请刷新重新尝试';
var xml_http_data_in_processed = '通信成功';

//事件加载
function addEvent( obj, type, fn ) { 
  if (obj.attachEvent) { 
    obj['e'+type+fn] = fn; 
    obj[type+fn] = function(){obj['e'+type+fn]( window.event );} 
    obj.attachEvent('on'+type,obj[type+fn]); 
  } else 
    obj.addEventListener(type,fn,false); 
} 

function removeEvent(obj,type,fn) { 
  if (obj.detachEvent) { 
    obj.detachEvent('on'+type,obj[type+fn]); 
    obj[type+fn]=null; 
  } else 
    obj.removeEventListener(type,fn,false); 
}

// 去空格
String.prototype.trim = function() {  
    return this.replace(/(^\s*)|(\s*$)/g, "");  
}

// 根据ID取对象
function $(Id){
	return document.getElementById(Id);
}

// 去对象的value
function $v(Id){
	var obj=$(Id);
	return obj.value;
}
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 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 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;
}

//JS编码
function strcode(str) {
    if(is_ie && document.charset == 'utf-8') {
        if(encodeURIComponent) {
            return encodeURIComponent(str);
        } else {
            return dencodeURIComponent(str);
        }
    } else {
        return str;
    }
}

function ietruebody() {
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}

var t_DiglogX,t_DiglogY,t_DiglogW,t_DiglogH;

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){
		if(objid1.value == objid1.value){
				return 1;
		}else{
				return 0;
		}
}

/*这里开始时UrlEncode和UrlDecode函数*/
function UrlEncode(str){
  var ret="";
  var strSpecial="!\"#$%&'()*+,/:;<=>?[]^`{|}~%";
  for(var i=0;i<str.length;i++){
   var chr = str.charAt(i);
    var c=str2asc(chr);
    if(parseInt("0x"+c) > 0x7f){
      ret+="%"+c.slice(0,2)+"%"+c.slice(-2);
    }else{
      if(chr==" ")
        ret+="+";
      else if(strSpecial.indexOf(chr)!=-1)
        ret+="%"+c.toString(16);
      else
        ret+=chr;
    }
  }
  return ret;
}

function UrlDecode(str){
  var ret="";
  for(var i=0;i<str.length;i++){
   var chr = str.charAt(i);
    if(chr == "+"){
      ret+=" ";
    }else if(chr=="%"){
     var asc = str.substring(i+1,i+3);
     if(parseInt("0x"+asc)>0x7f){
      ret+=asc2str(parseInt("0x"+asc+str.substring(i+4,i+6)));
      i+=5;
     }else{
      ret+=asc2str(parseInt("0x"+asc));
      i+=2;
     }
    }else{
      ret+= chr;
    }
  }
  return ret;
} 

//判断浏览器
function Browser(){
  var ua, s, i;
  this.isIE = false;
  this.isNS = false;
  this.isOP = false;
  this.isSF = false;
  ua = navigator.userAgent.toLowerCase();
  s = "opera";
  if ((i = ua.indexOf(s)) >= 0){
		this.isOP = true;return;
  }
  s = "msie";
  if ((i = ua.indexOf(s)) >= 0) {
		this.isIE = true;
		return;
  }
  s = "netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
		this.isNS = true;
		return;
  }
  s = "gecko";
  if ((i = ua.indexOf(s)) >= 0) {
		this.isNS = true;
		return;
  }
  s = "safari";
  if ((i = ua.indexOf(s)) >= 0) {
		this.isSF = true;
		return;
  }
}

//层对话框
function DialogShow(showdata,w,h){
  var objDialog = document.getElementById("ShowDialog");
  if (!objDialog) 
		objDialog = document.createElement("div");
  t_DiglogW = w;
  t_DiglogH = h;
  DialogLoc();
  objDialog.id = "ShowDialog";
  var oS = objDialog.style;
  oS.display = "block";
  oS.top = t_DiglogY - 50 + "px";
  oS.left = t_DiglogX + "px";
  oS.margin = "0px";
  oS.padding = "0px";
  oS.width = w + "px";
  oS.height = h + "px";
  oS.position = "absolute";
  oS.zIndex = "55";
  oS.background = "#FFF";
  oS.border = "solid #CCCCCC 1px";
  oS.overflow = "auto";
  objDialog.innerHTML = showdata;
  document.body.appendChild(objDialog);
}

function DialogHide() {
  ScreenClean();
  var objDialog = document.getElementById("ShowDialog");
  if (objDialog)
		objDialog.style.display = "none";
}
   
function DialogLoc() {
  var dde = document.documentElement;
  if (window.innerWidth){
		var ww = window.innerWidth;
		var wh = window.innerHeight;
		var bgX = window.pageXOffset;
		var bgY = window.pageYOffset;
  }else{
		var ww = dde.offsetWidth;
		var wh = dde.offsetHeight;
		var bgX = dde.scrollLeft;
		var bgY = dde.scrollTop;
  }
  t_DiglogX = bgX + ((ww - t_DiglogW)/2);
  t_DiglogY = bgY + ((wh - t_DiglogH)/2);
}
   
function ScreenConvert(){
  var browser = new Browser();
  var objScreen = document.getElementById("ScreenOver");
  if(!objScreen) 
		var objScreen = document.createElement("div");
  var oS = objScreen.style;
  objScreen.id = "ScreenOver";
  oS.display = "block";
  oS.top = oS.left = oS.margin = oS.padding = "0px";
  if (document.documentElement.clientHeight&&document.body.clientHeight) {
		if (document.documentElement.clientHeight>document.body.clientHeight) {
			var wh = document.documentElement.clientHeight + "px";
		}else{
			var wh = document.body.clientHeight + "px";
		}
  }else if (document.body.clientHeight){
		var wh = document.body.clientHeight + "px";
	}else if (window.innerHeight){
		var wh = window.innerHeight + "px";
  }else{
		var wh = "100%";
  }
  oS.width = "100%";
  oS.height = wh;
  oS.position = "absolute";
  oS.zIndex = "3";
  if ((!browser.isSF) && (!browser.isOP)){
		oS.background = "#cccccc";
  }else{
		oS.background = "#cccccc";
  }
  oS.filter = "alpha(opacity=20)";
  oS.opacity = 40/100;
  oS.MozOpacity = 40/100;
  document.body.appendChild(objScreen);
  var allselect = document.getElementsByTagName("select");
  for (var i=0; i<allselect.length; i++) 
		allselect[i].style.visibility = "hidden";
}

function ScreenClean(){
  var objScreen = document.getElementById("ScreenOver");
  if (objScreen)
		objScreen.style.display = "none";
  var allselect = document.getElementsByTagName("select");
  for (var i=0; i<allselect.length; i++) 
		allselect[i].style.visibility = "visible";
}

function Dialog(msg) {
	ScreenConvert();
	DialogShow(msg,300,100);
}

function ShowDialog(Caption,ShowDiv,w,h) {
	ScreenConvert();
	var showdata = ShowDiv;
	showdata ="<div class=\"dlgtop\"><div class=\"dlgtitle\">"+Caption+"</div><div class=\"dlgcloseicon\" onclick=\"DialogHide();\"></div></div><div class=\"dlgbody\">" + showdata + "<div class=\"dlgcloseicon\" onclick=\"DialogHide();\"></div></div>";
	DialogShow(showdata,w,h);
}

function ShowMsg(Caption, Message) {
	ScreenConvert();
	var ShowDiv ="<div class=\"dlgtop\"><div class=\"dlgtitle\">" + Caption + "</div><div class=\"dlgcloseicon\" onclick=\"DialogHide();\"></div></div><div class=\"dlgbody\">" + Message + "</div><div class=\"dlgclose\"><input type=\"button\" value=\"确定\" onclick=\"DialogHide();\" class=\"btn-dlg\" /></div>";
	DialogShow(ShowDiv,300,100);
}
   
function NoLogin(Caption){
	ScreenConvert();
	var login = 'login.php';
	var reg = 'reg.php';
	var ShowDiv ="<div class=\"dlgtop\"><div class=\"dlgtitle\">"+Caption+"</div><div class=\"dlgcloseicon\" onclick=\"DialogHide();\"></div></div><div class=\"dlgbody\">" + Caption + "，请先登录或免费注册！</div><div class=\"dlgclose\"><input type=\"button\" value=\" 登录 \" onclick=\"location.href='"+login+"'\" class=\"btn-dlg\" />&nbsp;<input type=\"button\" value=\" 注册 \" onclick=\"location.href='"+reg+"'\" class=\"btn-dlg\" />&nbsp;<input type=\"button\" value=\" 取消 \" onclick=\"DialogHide();\" class=\"btn-dlg\" /></div>";
  DialogShow(ShowDiv,300,100);
}   

//向目标加入TAG（感谢eonbell测试）
function addtag(Id, tag) {
	var str = $(Id).value;
	if ((" "+str+" ").indexOf(" "+tag+" ") == -1) {
		str += str ? ' ' + tag : tag;
		$(Id).value = 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;
		}
	}
}

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)
		ShowMsg('操作提示','请至少选择一项！');
	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;
}

//判断XML为Error并返回ErrorMsg
function checkError(xmlDoc) {
	this.iserr = false;
	var isError = xmlDoc.documentElement.nodeName;
	if(isError&&isError=='error'){
		this.iserr = true;
		this.errcode = xmlDoc.documentElement.firstChild.getAttribute('errcode');
		this.msg = xmlDoc.documentElement.firstChild.text;
	}
}

//取得XML返回的操作信息
function getMessage(xmlDoc) {
	var isMsg = xmlDoc.documentElement.nodeName;
	if(isMsg=='message'){
		return xmlDoc.documentElement.firstChild.text;
	}else {
		return;
	}
}

function getRandom() {
	return Math.floor(Math.random()*1000+1);
}

//显示验证码
function show_seccode () {
    var sec = $('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 = 'seccode.php?x='+getRandom();
		img.width = 80;
		img.height = 25;
		img.style.cursor = 'pointer';
		img.title = '点击更新验证码';
		img.onclick = function() {
			this.src= 'seccode.php?x='+getRandom();
		};
		sec.appendChild(img);
		sec.style.display = 'block';
    }
}

//URIComponent编码实现 2007-11-07
function dencodeURIComponent(str) {   
    var l = ['%00', '%01', '%02', '%03', '%04', '%05', '%06',   
             '%07', '%08', '%09', '%0A', '%0B', '%0C', '%0D',   
             '%0E', '%0F', '%10', '%11', '%12', '%13', '%14',   
             '%15', '%16', '%17', '%18', '%19', '%1A', '%1B',   
             '%1C', '%1D', '%1E', '%1F', '%20', '!', '%22',   
             '%23', '%24', '%25', '%26', "'", '(', ')', '*', '%2B', '%2C',    
             '-', '.', '%2F', '0', '1', '2', '3', '4', '5', '6',    
             '7', '8', '9', '%3A', '%3B', '%3C', '%3D', '%3E', '%3F',    
             '%40', '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', '%5B', '%5C',    
             '%5D', '%5E', '_', '%60', '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', '%7B', '%7C', '%7D', '~', '%7F'];    
    var out, i, j, len, c;    
     
    out = [];    
    len = str.length;    
    for(i = 0, j = 0; i < len; i++) {    
        c = str.charCodeAt(i);    
        if (c <= 0x007F) {    
            out[j++] = l[c];    
            continue;    
        }    
        else if (c <= 0x7FF) {    
            out[j++] = '%' + (0xC0 | ((c >>  6) & 0x1F)).toString(16).toUpperCase();    
            out[j++] = '%' + (0x80 | (        c & 0x3F)).toString(16).toUpperCase();    
            continue;    
        }    
        else if (c < 0xD800 || c > 0xDFFF) {    
            out[j++] = '%' + (0xE0 | ((c >> 12) & 0x0F)).toString(16).toUpperCase();    
            out[j++] = '%' + (0x80 | ((c >>  6) & 0x3F)).toString(16).toUpperCase();    
            out[j++] = '%' + (0x80 |         (c & 0x3F)).toString(16).toUpperCase();    
            continue;    
        }    
        else {    
            if (++i < len) {    
                c2 = str.charCodeAt(i);    
                if (c <= 0xDBFF && 0xDC00 <= c2 && c2 <= 0xDFFF) {    
                    c = ((c & 0x03FF) << 10 | (c2 & 0x03FF)) + 0x010000;    
                    if (0x010000 <= c && c <= 0x10FFFF) {    
out[j++] = '%' + (0xF0 | ((c >>> 18) & 0x3F)).toString(16).toUpperCase();    
out[j++] = '%' + (0x80 | ((c >>> 12) & 0x3F)).toString(16).toUpperCase();    
out[j++] = '%' + (0x80 | ((c >>>  6) & 0x3F)).toString(16).toUpperCase();    
out[j++] = '%' + (0x80 |          (c & 0x3F)).toString(16).toUpperCase();    
continue;    
                    }    
                }    
            }    
        }    
        @if (@_jscript_version < 5)    
            return null;    
        @else   
            var e = new Error(-2146823264, "The URI to be encoded contains an invalid character");    
            e.name = "URIError";    
            e.message = e.description;    
            throw(e);    
        @end    
    }    
    return out.join('');    
}  

//解码实现 2007-11-07
function ddecodeURIComponent(str) {    
    function throwerror() {    
        @if (@_jscript_version < 5)    
            return null;    
        @else   
            var e = new Error(-2146823263, "The URI to be decoded is not a valid encoding");    
            e.name = "URIError";    
            e.message = e.description;    
            throw(e);    
        @end    
    }
    function checkcode() {    
        var d1, d2;    
        d1 = str.charAt(i++);    
        d2 = str.charAt(i++);    
        if (isNaN(parseInt(d1, 16)) || isNaN(parseInt(d2, 16))) {    
            return throwerror();    
        }    
        return parseInt(d1 + d2, 16);    
    }    
    function checkutf8() {    
        var c = str.charCodeAt(i++);    
        if (c == 37) {    
            if ((c = checkcode()) == null) return null;    
        }    
        if ((c >> 6) != 2) {    
            return throwerror();    
        }    
    }
    var out, i, j, len;    
    var c, c2, c3, c4, s;    
     
    out = [];    
    len = str.length;    
    i = j = 0;    
    while(i < len) {    
        c = str.charCodeAt(i++);    
        if (c == 37) {    
            if ((c = checkcode()) == null) return null;    
        }    
        else {    
            out[j++] = String.fromCharCode(c);    
            continue;    
        }    
        switch(c >> 4) {     
            case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: {    
                // 0xxxxxxx    
                out[j++] = String.fromCharCode(c);    
                break;    
            }    
            case 12: case 13: {    
                // 110x xxxx   10xx xxxx    
                if ((c2 = checkutf8()) == null) return null;    
                out[j++] = String.fromCharCode(((c & 0x1F) << 6) | (c2 & 0x3F));    
                break;    
            }    
            case 14: {    
                // 1110 xxxx  10xx xxxx  10xx xxxx    
                if ((c2 = checkutf8()) == null) return null;    
                if ((c3 = checkutf8()) == null) return null;    
                out[j++] = String.fromCharCode(((c & 0x0F) << 12) |    
                    ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0));    
                break;    
            }    
            default: {
                switch (c & 0xf) {    
                    case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: {
// 1111 0xxx  10xx xxxx  10xx xxxx  10xx xxxx    
if ((c2 = checkutf8()) == null) return null;    
if ((c3 = checkutf8()) == null) return null;    
if ((c4 = checkutf8()) == null) return null;    
s = ((c  & 0x07) << 18) |    
    ((c2 & 0x3f) << 12) |    
    ((c3 & 0x3f) <<  6) |    
    (c4 & 0x3f) - 0x10000;    
if (0 <= s && s <= 0xfffff) {    
    out[j++] = String.fromCharCode(((s >>> 10) & 0x03ff) | 0xd800,    
            (s         & 0x03ff) | 0xdc00);    
}
else {    
    return throwerror();    
}
break;    
                    }
                    default: {    
return throwerror();    
                    } 
                }
            }
        }
    }
    return out.join('');    
}