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 = 'Please wait, connecting right now';

var xml_http_sending = 'Please wait, data is being sent';

var xml_http_loading = 'Please wait, data is being sent';

var xml_http_load_failed = 'http_load_failed please try again';

var xml_http_data_in_processed = 'Successful';



//Add event

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); 

}



// Remove spaces

String.prototype.trim = function() {  

    return this.replace(/(^\s*)|(\s*$)/g, "");  

}



// Get value by 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;       

}



// Check number

function is_numeric(str) {

	var patn = /^[0-9-\/]+$/;

	if(!patn.test(str)) return false;

	return true;

}



//Check email address format

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 code

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;

}





function isEqual(objid1,objid2){

		if(objid1.value == objid1.value){

				return 1;

		}else{

				return 0;

		}

}





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;

} 



//Check browers

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;

  }

}



//Dialog Show box

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 = "5";

  oS.background = "#FFF";

  oS.border = "solid #CCCCCC 1px";

  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>";

	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=\"Ok\" 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 + ",Please Login first, or register now for free!</div><div class=\"dlgclose\"><input type=\"button\" value=\" Login \" onclick=\"location.href='"+login+"'\" class=\"btn-dlg\" />&nbsp;<input type=\"button\" value=\" Register for free \" onclick=\"location.href='"+reg+"'\" class=\"btn-dlg\" />&nbsp;<input type=\"button\" value=\" Cancel \" onclick=\"DialogHide();\" class=\"btn-dlg\" /></div>";

  DialogShow(ShowDiv,300,100);

}   





//Check All(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('Help!','Please choose at least option.');

	return ischecked;

}



//Check 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并BackErrorMsg

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;

	}

}



//取得XMLBack的操作信息

function getMessage(xmlDoc) {

	var isMsg = xmlDoc.documentElement.nodeName;

	if(isMsg=='message'){

		return xmlDoc.documentElement.firstChild.text;

	}else {

		return;

	}

}



//显示验证码

function show_seccode () {

    var sec = $('seccode');

    if(!sec.style.display || sec.style.display == 'none') {

        sec.innerHTML = '<img src="seccode.php" width="80" height="25" onclick="this.src=\'seccode.php\';" /></a>';

        sec.style.display = 'block';

    }

}



//URIComponent编码实现 2008-09-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('');    

}  



//解码实现 2008-09-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('');    

}
