/**
 * check browser version
 */
var ns4=document.layers;
var ie4=document.all;
var ns6=document.getElementById&&!document.all;
var _SARISSA_IS_IE = document.all && window.ActiveXObject && navigator.userAgent.toLowerCase().indexOf("msie") > -1  && navigator.userAgent.toLowerCase().indexOf("opera") == -1;
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}


function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function trimString (str) {
    str = this != window? this : str;
    return str.replace(/^\s+/g, '').replace(/\s+$/g, '').replace(/\r/g,'').replace(/\n/g,' ');
    //return str.replace(/^\s+/g, '').replace(/\s+$/g, '').replace(/\r/g,'').replace(/\n/g,'');
}

function checkStringEmpty (elm, emptyAlertMsg) {
    var value = elm.value;
    value = trimString(value);
    if (value.length == 0) {
        if (emptyAlertMsg != null) {
            alert(emptyAlertMsg);
            elm.focus();
            return false;
        } else {
            return true;
        }
    }
    return true;
}

function checkTextFieldLength(elm, maxBytes, truncatedConfirmMsg, emptyAlertMsg) {
    var value = elm.value;
    var n;

    //if (elm.type.toLowerCase() == 'text'){
    //    value = trimString(value);
    //}
    value = trimString(value);
    if (value.length == 0) {
        if (emptyAlertMsg != null) {
            alert(emptyAlertMsg);
            elm.focus();
            return false;
        } else {
            return true;
        }
    }
    if (maxBytes == -1) return true;
    for(n=0; (n < value.length && maxBytes > 0); n++) {
        var c=value.charCodeAt(n);

        if (c<128) { // 0-127 => 1byte
            maxBytes -= 1;
        } else if((c>127) && (c<2048)) { // 127 -  2047 => 2byte
            maxBytes -= 2;
        } else { // 2048 - 66536 => 3byte
            maxBytes -= 3;
        }
    }

    if ((maxBytes == 0 && (n < value.length)) || maxBytes < 0) {
        if (truncatedConfirmMsg != null && confirm(truncatedConfirmMsg) == false) {
            elm.focus();
            return false;
        }
    }

    elm.value = (maxBytes < 0 && n > 1) ? value.substring(0, n-1) : value.substring(0, n);
    return true;
}

function checkTextFieldLengthEx(elm, maxLength, truncatedConfirmMsg, emptyAlertMsg) {
    var value = trimString(elm.value);
    if (value.length == 0) {
        if (emptyAlertMsg != null) {
            alert(emptyAlertMsg);
            elm.focus();
            return false;
        } else {
            return true;
        }
    }

    if (maxLength == -1) return true;

    if (maxLength < value.length) {
        if (truncatedConfirmMsg != null && confirm(truncatedConfirmMsg) == false) {
            elm.focus();
            return false;
        }
    }

    elm.value = value.substring(0, maxLength);
    return true;
}

function setAllCheckButtons(isChecked, form, checkBtnNamePrefix) {
    if ( form.elements == null ) return;

    for (var i = 0; i< form.elements.length; i++) {
        var elm = form.elements[i];
        if (elm == null || typeof(elm.type) == 'undefined' || elm.type == null) continue;
        if (elm.type.toLowerCase() == "checkbox" && elm.name.indexOf(checkBtnNamePrefix) != -1) {
            elm.checked = isChecked;
        }
    }
}

function isAnyCheckButtonChecked(form, checkBtn1NamePrefix, checkBtn2NamePrefix) {
    if ( form.elements == null ) return true;
    var checkPrefixs = new Array();

    if (checkBtn1NamePrefix != null && checkBtn1NamePrefix.length > 0) {
        checkPrefixs[checkPrefixs.length] = checkBtn1NamePrefix;
    }

    if (checkBtn2NamePrefix != null && checkBtn2NamePrefix.length > 0) {
        checkPrefixs[checkPrefixs.length] = checkBtn2NamePrefix;
    }
    //alert("checkPrefixs.length = " + checkPrefixs.length);
    if (checkPrefixs.length == 0) return true;

    for (var i = 0; i< form.elements.length; i++) {
        var elm = form.elements[i];
        if (elm == null || typeof(elm.type) == 'undefined' || elm.type == null) continue;
        if (elm.type.toLowerCase() != "checkbox") continue;
        for (var j = 0; j < checkPrefixs.length; j++) {
            if (elm.name.indexOf(checkPrefixs[j]) != -1) {
                if (elm.checked == true) return true;
                break;
            }
        }
    }
    //alert("found nothing");
    return false;
}

function isDigit(digStr, fCanEmpty) {
    if (fCanEmpty == null) fCanEmpty = true;
    if (fCanEmpty == true && digStr.length == 0) return true;
    var result = digStr.match(/[^0-9]/g);
    if (result || !digStr) return false;
    return true;
}

function isDigitEx(elm, fCanEmpty, emptyAlertMsg) {
	var digStr = elm.value;
    if (fCanEmpty == null) fCanEmpty = true;
    if (fCanEmpty == true && digStr.length == 0) return true;
    var result = digStr.match(/[^0-9]/g);
    if (result || !digStr) {
		alert(emptyAlertMsg);
        elm.focus();
    	return false;
    }
    return true;
}

function isDecimal(elm, fCanEmpty, emptyAlertMsg, errorAlertMsg) {
	var decStr = elm.value;
	var length = decStr.length;
    if (fCanEmpty == null) fCanEmpty = true;
    if (fCanEmpty == true && length == 0) return true;
    if (length == 0) {
		alert(emptyAlertMsg);
        elm.focus();
    	return false;
    }

    var beginIndex = decStr.indexOf(".");
    var endIndex = decStr.lastIndexOf(".");
    if (beginIndex != -1) {
    	if (beginIndex == endIndex) {
	    	if ( (length - beginIndex) == 3 || (length - beginIndex) == 2) {
				decStr = decStr.replace("." , "");
	    	}
    	}
    }
    if ( !decStr.match(/[^0-9]/g) ) {
		return true;
    }
	alert(errorAlertMsg);
    elm.focus();
    return false;
}

function getInteger(elm) {
	var str = elm.value;
	var ix = str.indexOf(".");
	var position = str.length - ix;
	str = str.replace("." , "");
	if (position == 2 && ix != -1) {
		return str * 10;
	} else if (position == 3 && ix != -1) {
		return str;
	} else {
		return str * 100;
	}
}

function checkFileExtension(elm, exts, emptyAlertMsg) {
	var value = trimString(elm.value);
	var ix = value.lastIndexOf(".");
	if (ix == -1) {
		alert(emptyAlertMsg);
		elm.focus();
		return false;
	}
	var ext = value.substring(ix+1).toLowerCase();
	for (var i = 0; i < exts.length; i++) {
		if(exts[i] == ext){
			return true;
		}
	}
	alert(emptyAlertMsg);
	elm.focus();
	return false;
}

function isAlphabetNumeric(testStr, fCanEmpty) {
    if (fCanEmpty == null) fCanEmpty = true;
    if (fCanEmpty == true && testStr.length == 0) return true;
    var result = testStr.match(/[^0-9a-zA-Z]/g);
    if (result || !testStr) return false;
    return true;
}

function isJavaScriptLegalId(idStr) {
  if (idStr.length == 0) return false;
  for ( i = 0 ; i < idStr.length ; i++ ) {
    if ( idStr.charAt(i) == '\'' || idStr.charAt(i) == '\"' || idStr.charAt(i) == '\\') return false;
  }

  return true;
}

function setRadioChecked(radios, radioValue) {

    if (typeof(radios) == 'undefined' || radios == null) return;
    if (typeof(radios.length) == 'undefined') { //only one element
        if (radios.type != "radio") return;
        radios.checked = true;
    } else {
        for (var i = 0; i < radios.length; i++) {
            if (radios[i].type != "radio") continue;
            radios[i].checked = (radios[i].value == radioValue) ? true : false;
        }
    }
}

function trimAllInputTextValues(form, inputCtrlNamePrefix) {
    for(var i=0; i< form.elements.length; i++) {
        var control = form.elements[i];
        if (control == null || typeof(control.type) == 'undefined' || control.type == null) continue;
        if (control.type.toLowerCase() != 'text') continue;
        if (inputCtrlNamePrefix != null && control.name.indexOf(inputCtrlNamePrefix) == -1) continue;

        //alert("To trim " + control.name);
        control.value = trimString(control.value);
    }
}

function isEmail(elm) {
    var email;
    if (elm.value == null) { //it's just a variable
        elm = trimString(elm);
        email = elm;
    } else { //it's a control
        elm.value = trimString(elm.value);
        email = elm.value;
    }

    if (email.length == 0) return true;

    var atIndex       = email.indexOf("@");
    var firstDotIndex = email.indexOf(".");
    if (atIndex == -1 || atIndex == 0 || atIndex == (email.length-1)) return false;
    if (firstDotIndex == -1 || firstDotIndex == (email.length-1)) return false;
    return true;
}

function truncateTextField(elm, maxBytes, alertMsg) {
    elm.value = getTruncatedTextField(elm.value, maxBytes, alertMsg);
}

function getTruncatedTextField(rawString, maxBytes, alertMsg) {
    var value = trimString(rawString);
    value = value.replace(/\r\n/g,"\n");
    var n;
    for(n=0; (n < value.length && maxBytes > 0); n++) {
        var c=value.charCodeAt(n);
        if (c<128) { // 0-127 => 1byte
            maxBytes -= 1;
        } else if((c>127) && (c<2048)) { // 128 -  2047 => 2byte
            maxBytes -= 2;
        } else { // 2048 - 66536 => 3byte
            maxBytes -= 3;
        }
    }

    if ((maxBytes == 0 && (n < value.length)) || maxBytes < 0) {
        if (alertMsg != null) alert(alertMsg);
    }

    value = (maxBytes < 0 && n > 1) ? value.substring(0, n-1) : value.substring(0, n);
    return value;
}

function getControlValue(control) {
    if (control.type == 'checkbox' || control.type == 'radio') {
        //alert(control.type + "/" + control.name + "/" + control.value + " = " + control.checked);
        return control.checked;
    } else if (control.type == 'select-multiple') {
        var out = "";
        for(var i=0; i<control.options.length; i++) {
            if (control.options[i].selected) {
                out += "|" + control.options[i].value;
            }
        }
        return out;
    } else if (control.type == 'select-one') {
        return control.options.selectedIndex;
    } else {
        return trimString(control.value);
    }
}

function getRadioValue(redioObj) {
	for (i=0; i<redioObj.length; i++) {
		if (redioObj[i].checked) {
			return redioObj[i].value;
		}
	}
}

function HtmlCtrl(ctrl, value) {
    this.ctrl = ctrl;
    this.defaultValue = value;
}

function getHtmlCtrls(form, elmNamePrefix, toCheckHidden, toCheckMultipleSelect) {
    var pairs = new Array();
    for(var i=0; i< form.elements.length; i++) {
        var control = form.elements[i];
        if (elmNamePrefix != null && control.name.indexOf(elmNamePrefix) == -1) continue;
        if (control.type == 'submit') continue;
        if (control.type == 'hidden'         && (toCheckHidden         == null || toCheckHidden         == false)) continue;
        if (control.type == 'select-multiple'&& (toCheckMultipleSelect == null || toCheckMultipleSelect == false)) continue;
        pairs[pairs.length] = new HtmlCtrl(control, getControlValue(control));
        //alert(control.name + "/" + control.value + "-" + getControlValue(control));
    }
    return pairs;
}

function restoreHtmlCtrls(htmlCtrls) {
    if (htmlCtrls == null || htmlCtrls.length == 0) return;
    for (var i = 0; i < htmlCtrls.length; i++) {
        //alert(htmlCtrls[i].ctrl.name + "/" + htmlCtrls[i].ctrl.value + "-" + htmlCtrls[i].ctrl.value);
        restoreHtmlCtrl(htmlCtrls[i]);
    }
}

function getStringTokens(delim, str) {
    var token   = new Array();
    var testStr = trimString(str);

    if (delim == null || delim.length == 0) {
        token[0] = testStr;
        return token;
    }

    var delimIndex = testStr.indexOf(delim);
    var tokenIndex = 0;
    if (delimIndex == -1 && testStr.length != 0) token[0] = testStr;

    while(delimIndex != -1) {
        token[tokenIndex] = testStr.substring(0, delimIndex);
        if (delim != " ") token[tokenIndex] = trimString(token[tokenIndex]);
        tokenIndex += 1;
        testStr = testStr.substring(delimIndex + 1);
        delimIndex = testStr.indexOf(delim);
        if (delimIndex == -1) {
            token[tokenIndex] = testStr;
            if (delim != " ") token[tokenIndex] = trimString(token[tokenIndex]);
        }
    }

    return token;
}

function restoreHtmlCtrl(htmlCtrl) {
    if (htmlCtrl.ctrl.type == 'checkbox' || htmlCtrl.ctrl.type == 'radio') {
        htmlCtrl.ctrl.checked = htmlCtrl.defaultValue;
    } else if (htmlCtrl.ctrl.type == 'select-multiple') {
        //alert("htmlCtrl.defaultValue = " + htmlCtrl.defaultValue);
        var defaultValues  = getStringTokens("|", htmlCtrl.defaultValue);
        var isCheckedArray = newArray(defaultValues.length, false);
        for(var i=0; i< htmlCtrl.ctrl.options.length; i++) {
            var isFound = false;
            if (defaultValues != null && defaultValues.length > 0) {
                for (var j = 0; j < defaultValues.length; j++) {
                    if (isCheckedArray[j] == true) continue;
                    if (htmlCtrl.ctrl.options[i].value != defaultValues[j]) continue;
                    htmlCtrl.ctrl.options[i].selected = true;
                    isCheckedArray[j] = true;
                    isFound = true;
                    break;
                }
            }
            if (isFound == false) htmlCtrl.ctrl.options[i].selected = false;
        }
    } else if (htmlCtrl.ctrl.type == 'select-one') {
        htmlCtrl.ctrl.options.selectedIndex = htmlCtrl.defaultValue;
    } else {
        htmlCtrl.ctrl.value = htmlCtrl.defaultValue;
    }
}
function calcBar(Rx, Gx, Bx, xRange, yRange, bias) {
    X = 0.412391 * Rx + 0.357584 * Gx + 0.180481 * Bx;
    Y = 0.212639 * Rx + 0.715169 * Gx + 0.072192 * Bx;
    Z = 0.019331 * Rx + 0.119195 * Gx + 0.950532 * Bx;

    x = Math.floor((X/(X+Y+Z)) * xRange) + Math.random() * bias;
    y = Math.floor((Y/(X+Y+Z)) * yRange) + Math.random() * bias;
}

function drawColors(finverse, dx, dy, stepSize, xRange, yRange, bias) {
   zindex = -1;
   for (r=1;r<=255;r+=stepSize) {
       for (g=0;g<=255;g+=stepSize) {
           for (b=0;b<=255;b+=stepSize) {
               calcBar(r,g,b,xRange,yRange,bias);
               document.write("<div style='position:absolute;"
                   + "width:8px;height:6px;z-index:" + zindex + ";"
                   + "background-color:rgb("+r+","+g+","+b+");"
                   + "border:1px solid #808080;"
                   + "left:" + (finverse?(1000 - (x+dx)):(x+dx)) + ";top:" + (y+dy) + "'></div>");
           }
       }
   }
}

function checkEmailAddr(elm, fCanEmpty) {
    var email;
    if (elm.value == null) { //it's just a variable
        elm = trimString(elm);
        email = elm;
    } else { //it's a control
        elm.value = trimString(elm.value);
        email = elm.value;
    }

    if (fCanEmpty == null) fCanEmpty = true;
    if (fCanEmpty == true && email.length == 0) return true;
//    alert("email:" + email);
	for ( var i = 0; i < email.length; i++) {
		var c = email.charCodeAt(i);
		if (!isEngDigit(c)&&!(c==64 || c== 46||c==95)) {
				return false;
		}
	}
    var data = email.match(/^\S+@\S+\.\S+$/);
    if (!data || !email) return false;
    return true;
}

function forceNumber() { //only for IE 4.0x, 5.0x, 6.0
    var c = String.fromCharCode( event.keyCode );
    if ("0123456789".indexOf(c, 0) < 0) return false;
    return true;
}

function checkHTTPUrl(dstText, fCanEmpty, fCanSSL) {
    if (fCanEmpty == null) fCanEmpty = true;
    if (fCanEmpty == true && dstText.length == 0) return true;
    dstText = dstText.toLowerCase();
    if (fCanSSL != null && fCanSSL == true) {
        if (fCanEmpty == true && (dstText == "http://" || dstText == "https://")) return true;
        var data1 = dstText.match(/(http|https):\/\/.+/);
        if (!data1 || !dstText) return false;
    } else {
        if (fCanEmpty == true && dstText == "http://") return true;
        var data2 = dstText.match(/http:\/\/.+/);
        if (!data2 || !dstText) return false;
    }
    return true;
}

function checkIP(dstText, fCanEmpty) {
    if (fCanEmpty == null) fCanEmpty = true;
    if (fCanEmpty == true && dstText.length == 0) return true;
    var data = dstText.match(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/);
    if (!data || !dstText) return false;
    var values = getStringTokens(".", dstText);
    for (var i = 0; i < values.length; i++) {
        var chkee = new Number(values[i]);
        if (chkee <= 0 || chkee > 255) return false;
    }
    return true;
}

function isBlankString(testStr) {
    if (testStr.length == 0) return true;
    var result = testStr.match(/[^ ]/g);
    if (result || !testStr) return false;
    return true;
}

function isFormDirty(form, elmNamePrefix, toCheckHidden, toCheckMultipleSelect) {
    var nonDirtyCtrl = null;
    for(var i=0; i<form.elements.length; i++) {
        var control = form.elements[i];
        if (typeof(control.type) == 'undefined' || control.type == null) continue;
        if (elmNamePrefix != null && control.name.indexOf(elmNamePrefix) == -1) continue;
        if (control.type == 'submit') { continue; }
        if (control.type == 'hidden'         && (toCheckHidden == null || toCheckHidden == false)) continue;
        if (control.type == 'select-multiple'&& (toCheckMultipleSelect == null || toCheckMultipleSelect == false)) continue;
        var crntValue    = getControlValue(control);
        var defaultValue = getControlDefaultValue(control);
        if (crntValue != defaultValue) {
            return true;
        }
        if (nonDirtyCtrl == null) nonDirtyCtrl = control;
    }
    if (nonDirtyCtrl != null) nonDirtyCtrl.focus();
    return false;
}

function getControlValue(control) {
    if (control.type == 'checkbox' || control.type == 'radio') {
        //alert(control.type + "/" + control.name + "/" + control.value + " = " + control.checked);
        return control.checked;
    } else if (control.type == 'select-multiple') {
        var out = "";
        for(var i=0; i<control.options.length; i++) {
            if (control.options[i].selected) {
                out += "|" + control.options[i].value;
            }
        }
        return out;
    } else if (control.type == 'select-one') {
        for(var i=0; i<control.options.length; i++) {
            if (control.options[i].selected) {
                return control.options[i].value;
            }
        }
        return control.options[0].value;
    } else {
        return trimString(control.value);
    }
}

function getControlText(control) {
    if (control.type == 'checkbox' || control.type == 'radio') {
        //alert(control.type + "/" + control.name + "/" + control.value + " = " + control.checked);
        return control.checked;
    } else if (control.type == 'select-multiple') {
        var out = "";
        for(var i=0; i<control.options.length; i++) {
            if (control.options[i].selected) {
                out += "|" + control.options[i].text;
            }
        }
        return out;
    } else if (control.type == 'select-one') {
        for(var i=0; i<control.options.length; i++) {
            if (control.options[i].selected) {
                return control.options[i].text;
            }
        }
        return control.options[0].value;
    } else {
        return trimString(control.value);
    }
}

function getControlDefaultValue(control) {
    if (control.type == 'checkbox' || control.type == 'radio') {
        return control.defaultChecked;
    } else if (control.type == 'select-multiple') {
        var out = "";
        for(i=0; i<control.options.length; i++) {
            if (control.options[i].defaultSelected) {
                out += "|" + control.options[i].value;
            }
        }
        return out;
    } else if (control.type == 'select-one') {
        for(var i=0; i<control.options.length; i++) {
            if (control.options[i].defaultSelected) {
                return control.options[i].value;
            }
        }
        return control.options[0].value;
    } else {
        return trimString(control.defaultValue);
    }
}


function checkTaiwanZipCode(dstText) {
    var chkee = dstText.match(/^\d{3}$|^\d{5}$/);
    if (!chkee || !dstText) return false;
    return true;
}
function checkEmail(emailStr) {
   if (emailStr.length == 0) {
       return true;
   }
   var emailPat=/^(.+)@(.+)$/;
   var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
   var validChars="\[^\\s" + specialChars + "\]";
   var quotedUser="(\"[^\"]*\")";
   var ipDomainPat=/^(\d{1,3})[.](\d{1,3})[.](\d{1,3})[.](\d{1,3})$/;
   var atom=validChars + '+';
   var word="(" + atom + "|" + quotedUser + ")";
   var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
   var domainPat=new RegExp("^" + atom + "(\\." + atom + ")*$");
   var matchArray=emailStr.match(emailPat);
   if (matchArray == null) {
       return false;
   }
   var user=matchArray[1];
   var domain=matchArray[2];
   if (user.match(userPat) == null) {
       return false;
   }
   var IPArray = domain.match(ipDomainPat);
   if (IPArray != null) {
       for (var i = 1; i <= 4; i++) {
          if (IPArray[i] > 255) {
             return false;
          }
       }
       return true;
   }
   var domainArray=domain.match(domainPat);
   if (domainArray == null) {
       return false;
   }
   var atomPat=new RegExp(atom,"g");
   var domArr=domain.match(atomPat);
   var len=domArr.length;
   if ((domArr[domArr.length-1].length < 2) ||
       (domArr[domArr.length-1].length > 3)) {
       return false;
   }
   if (len < 2) {
       return false;
   }
   return true;
}

/**
 * equals trim funtion in java
 */
function trim(str){
   return str.replace(/^\s*|\s*$/g,"");
}

/**
 * equals endWith function in java
 */
function endWith(str, str2) {
	if (str.lastIndexOf(str2) == (str.length - str2.length)) {
		return true;
	}
	return false;
}

function isMixedEngDigit(str) {
	var isEng = false;
	var isDigit = false;
	for ( var i = 0; i < str.length; i++) {
		var c = str.charCodeAt(i);
		if (c >= 0x30 && c <= 0x39) {
			isDigit = true;
		} else if ((c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c<= 0x7a)) {
			isEng = true;
		} else {
			return false;
		}
	}
// not need mixed by mia
//	return isEng && isDigit;
	return true;
}

function isEngDigit(c) {
	if ( (c >= 0x30 && c <= 0x39) || (c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c<= 0x7a)) {
		return true
	} else {
		return false;
	}
}

function isMaxWordLength(obj, alertMsg) {
	var maxWordLen=obj.getAttribute? parseInt(obj.getAttribute("maxWordLength")) : ""
	var value = obj.value;
	var wordBInx = -1;
	var wordEInx = -1;
	var errs = new Array();
	for ( var i = 0; i < value.length; i++) {
		if (isEngDigit(value.charCodeAt(i))) {
			wordEInx = i;
		} else {
			//alert("wordBInx=" + wordBInx + " wordEInx=" + wordEInx + " got word: '" + value.substr(wordBInx + 1, wordEInx - wordBInx) + "'");
			if (wordEInx - wordBInx > maxWordLen) {
				errs.push(value.substr(wordBInx + 1, wordEInx - wordBInx));
				//alert("got err: " + err);
			}
			wordBInx = i;
		}
	}
	if (wordEInx - wordBInx > maxWordLen) {
		errs.push(value.substr(wordBInx + 1, wordEInx - wordBInx));
		//alert("got err: " + err);
	}
	if (errs.length > 0) {
		alertMsg += ":\n" + errs.join("\n");
		alert(alertMsg);
		obj.focus();
		return false;
	}
	return true;
}

/**
 * check textArea maxlength. example:
 * <textarea maxlength="1500" onkeyup="return ismaxlength(this)"
 * cols="32" rows="5" name="logNotes" id="logNotes">
 */
function ismaxlength(obj, alertMsg){
	var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : "";
	var objValue = obj.value;
	if (obj.getAttribute && objValue.length>mlength) {
		alert(alertMsg)
		obj.value=objValue.substring(0, mlength);
//		obj.select()
		obj.focus()
	}
}

function ismaxlengthByBytes(obj, alertMsg){
	var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
	if (obj.getAttribute && getStringLengthByBytes(obj.value)>mlength) {
		alert(alertMsg)
		obj.value=leftStringByBytes(obj.value,mlength)
//		obj.select()
		obj.focus()
	}
}

/**
 * return string bytes count
 */
function getStringLengthByBytes(str) {
    value = trimString(str);
    if (value.length == 0) {
    	return 0;
    }
    intLength = 0;
    for(n=0; n < value.length; n++) {
        var c=value.charCodeAt(n);
        if (c<128) { // 0-127 => 1byte
            intLength += 1;
        } else if((c>127) && (c<2048)) { // 127 -  2047 => 2byte
            intLength += 2;
        } else { // 2048 - 66536 => 3byte
            intLength += 3;
        }
    }
    return intLength;
}

/**
 * return left string bytes
 */
function leftStringByBytes(str, len) {
    value = trimString(str);
	var valueLength = getStringLengthByBytes(value);

	if (valueLength < len || len < 0) {
		return value;
	}
	intLength = 0;
	var rtnString = "";
    for(n=0; (n < valueLength && intLength < len); n++) {
        var c=value.charCodeAt(n);
       	rtnString += value.charAt(n);
        if (c<128) { // 0-127 => 1byte
            intLength += 1;
        } else if((c>127) && (c<2048)) { // 127 -  2047 => 2byte
            intLength += 2;
        } else { // 2048 - 66536 => 3byte
            intLength += 3;
        }
    }
	return rtnString;
}

/**
 * Add option to SELECT object
 */
function optionAddChildNode(obj, value, display) {
	//duplicate
    var len=obj.length;
    for(var i=0;i<len;i++)
    {
       if (obj.options[i].value==value) {
       		return;
       }
    }

	//add
	obj.options.add(new Option(display, value))
}

/**
 * Remove options from SELECT object
 */
function optionRemoveChildNodes(obj)
{
    var len=obj.length;
    for(var i=len-1;i>=0;i--)
    {
        if(obj.options[i].selected)
        {
            obj.remove(i);
        }
    }
}

/**
 * Select all options on SELECT object
 */
function optionSelectAll(obj)
{
    var len=obj.length;
    for(var i=0;i<len;i++)
    {
        obj.options[i].selected=true;
    }
}

function optionUnSelectAll(obj)
{
    var len=obj.length;
    for(var i=0;i<len;i++)
    {
        obj.options[i].selected=false;
    }
}



/**
 * Remove all options from SELECT object
 */
function optionClearListObj(obj)
{
    var len=obj.length;
    for(var i=0;i<len;i++) obj.remove(0);
}

/**
 * Remove options from SELECT object when user click 'del' key. example:
 * <SELECT NAME="targetSelect" size="10" style="WIDTH: 12em" multiple
 * onkeydown="optionDelete(this)">
 */
function optionDelete(obj) {
	var ev = window.event
	if (ev.keyCode == 46) {
		optionRemoveChildNodes(obj);
		obj.focus();
	}
}

function optionMoveAll(source, target) {
	optionSelectAll(source);
	optionCopy(source, target);
	optionRemoveChildNodes(source);
}

function optionMove(source, target) {
	optionCopy(source, target);
	optionRemoveChildNodes(source);
}

function optionCopy(source, target) {
	var values = getStringTokens("|", getControlValue(source));
	var labels = getStringTokens("|", getControlText(source));
	for (var i=1; i<values.length; i++) {
		optionAddChildNode(target, values[i], labels[i]);
	}
}


/**
 * get object position
 */
function getAbsolutePos(el) {
	var r = { x: el.offsetLeft, y: el.offsetTop };
	if (el.offsetParent) {
		var tmp = getAbsolutePos(el.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}
	return r;
}

/**
 * get object width
 */
function getWidth(el) {
	return el.offsetWidth
}

/**
 * get object height
 */
function getHeight(el) {
	return el.offsetHeight
}


/**
 * show object tips. example:
 * <div id="tips" style="WORD-BREAK:BREAK-ALL;background-color: #FFFF00;border: 1 solid #000000;position: absolute;visibility: hidden;"></div>
 *
 * <SELECT NAME="targetSelect" size="10" style="WIDTH: 12em" multiple
 * onClick="hideTips();" onkeydown="hideTips()"
 * onmouseover="showTips(this, 'test tips', 'middle', 20)" onmouseout="hideTips()">
 */
function showTips(el, value) {
	showTips(el, value, "left", 0);
}

/**
 * show object tips.
 */
function showTips(el, value, align, width) {
    var tip=ns6? document.getElementById("tips") : document.all.tips
//    value = "<table width='100%' border='0'><tr><td style='WORD-BREAK:BREAK-ALL;'>" + value + "</td></tr></table>";
    writeToObj(tip, value)
    if (width == null || width == 0) {
    	width = getWidth(el)
    }
	tip.style.width = width
    var p = getAbsolutePos(el)
	tip.style.left = p.x
    if (align == null) {
    	align = "left"
    }
    if (align.toLowerCase() == "right") {
		tip.style.left = p.x + getWidth(el) - tip.offsetWidth
	}
    if (align.toLowerCase() == "middle" || align.toLowerCase() == "center") {
		tip.style.left = p.x - (tip.offsetWidth - getWidth(el))/2
	}
	tip.style.top = p.y + getHeight(el)
	if (ie4||ns6)
		tip.style.visibility="visible"
	else if (ns4)
		document.tips.visibility="show"
}

/**
 * show object tips.
 */
function hideTips() {
    var tip=ns6? document.getElementById("tips") : document.all.tips
	if (ie4||ns6)
		tip.style.visibility = "hidden"
	else if (ns4)
		document.tips.visibility="hide"
}

/**
 * set cookie
 */
function setCookie (name, value) {
	var argv = setCookie.arguments;
	var argc = setCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	document.cookie = name + "=" + escape (value) +
		((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
		((path == null) ? "" : ("; path=" + path)) +
		((domain == null) ? "" : ("; domain=" + domain)) +
		((secure == true) ? "; secure" : "");
}

/**
 * delete cookie by name
 */
function deleteCookie (name) {
    var exp = new Date();
    exp.setTime (exp.getTime() - 1);
    var cval = getCookie(name);
    document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

/**
 * clear cookie which name starts with prefix
 */
function clearCookieStartWith(prefix) {
    var temp=document.cookie.split(";");
    var loop3;
    var ts;
    for (loop3 = 0; loop3 < temp.length;loop3++) {
        ts=temp[loop3].split("=")[0];
        if (ts.indexOf(prefix) == 1) {
   			deleteCookie(ts);
        }
    }
}

/**
 * clear cookie
 */
function clearCookie() {
    var temp=document.cookie.split(";");
    var loop3;
    var ts;
    for (loop3=0;loop3<temp.length;loop3++) {
        ts=temp[loop3].split("=")[0];
        deleteCookie(ts);
    }
}
/**
 * get cookie value by offset
 */
function getCookieVal(offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}
/**
 * get cookie value
 */
function getCookie(name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
			return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
			if (i == 0) break;
	}
	return null;
}
/**
 * check cookie support
 */
function isCookieEnabled() {
	setCookie("isCookieEnabled", "enabled");
	var value = getCookie("isCookieEnabled");
	if (value != null && value == "enabled") {
		return true;
	}
	return false;
}

function up(obj) {
  var currerntText;
  var nextText;
  var currerntValue;
  var nextValue;

  if(obj.options[obj.options.selectedIndex].index > 0)
  {
    currerntText = obj.options[obj.options.selectedIndex].text;
    nextText = obj.options[obj.options[obj.options.selectedIndex].index-1].text;
    obj.options[obj.options.selectedIndex].text =  nextText;
    obj.options[obj.options[obj.options.selectedIndex].index-1].text = currerntText;

    currerntValue = obj.options[obj.options.selectedIndex].value;
    nextValue = obj.options[obj.options[obj.options.selectedIndex].index-1].value;
    obj.options[obj.options.selectedIndex].value =  nextValue;
    obj.options[obj.options[obj.options.selectedIndex].index-1].value = currerntValue;

    self.focus();
    obj.options.selectedIndex--;
  }
}

function down(obj) {
  var currerntText;
  var nextText;
  var currerntValue;
  var nextValue;
  if(obj.options[obj.options.selectedIndex].index != obj.length-1)
  {
    currerntText = obj.options[obj.options.selectedIndex].text;
    nextText = obj.options[obj.options[obj.options.selectedIndex].index+1].text;
    obj.options[obj.options.selectedIndex].text =  nextText;
    obj.options[obj.options[obj.options.selectedIndex].index+1].text = currerntText;

    currerntValue = obj.options[obj.options.selectedIndex].value;
    nextValue = obj.options[obj.options[obj.options.selectedIndex].index+1].value;
    obj.options[obj.options.selectedIndex].value =  nextValue;
    obj.options[obj.options[obj.options.selectedIndex].index+1].value = currerntValue;

    self.focus();
    obj.options.selectedIndex++;
  }
}

function checkTextFieldNotEmpty(elm, emptyAlertMsg) {
    var value = trimString(elm.value);
    if (value.length == 0) {
    	alert(emptyAlertMsg);
        elm.focus();
        return false;
    } else {
    	return true;
    }
}

function showIsoColck() {
	document.write('<font  id="calendarClock"  >  </font>&nbsp;');
	setInterval('isodatetime()',1000);

}
//don't call isodatetime function directly. try showisotime function please.
function isodatetime() {
	var today = new Date();
	var year  = today.getYear();
	var month = today.getMonth() + 1;
	var day  = today.getDate();
	var hour = today.getHours();
	var hourUTC = today.getUTCHours();
	var diff = hour - hourUTC;
	var hourdifference = Math.abs(diff);
	var minute = today.getMinutes();
	var minuteUTC = today.getUTCMinutes();
	var minutedifference;
	var second = today.getSeconds();
	var timezone;
	if (year < 2000) {
		year = year + 1900;
	}
	if (minute != minuteUTC && minuteUTC < 30 && diff < 0) {
		hourdifference--;
	}
	if (minute != minuteUTC && minuteUTC > 30 && diff > 0) {
		hourdifference--;
	}
	if (minute != minuteUTC) {
		minutedifference = ":30";
	} else {
		minutedifference = ":00";
	}
	if (hourdifference < 10) {
		timezone = "0" + hourdifference + minutedifference;
	} else {
		timezone = "" + hourdifference + minutedifference;
	}
	if (diff < 0) {
		timezone = "-" + timezone;
	} else {
		timezone = "+" + timezone;
	}
	if (month <= 9) month = "0" + month;
	if (day <= 9) day = "0" + day;
	if (hour <= 9) hour = "0" + hour;
	if (minute <= 9) minute = "0" + minute;
	if (second <= 9) second = "0" + second;
	time = year + "/" + month + "/" + day + "&nbsp;"
		+ hour + ":" + minute + ":" + second + "&nbsp;&nbsp;(GMT " + timezone + ")";
	document.all.calendarClock.innerHTML = time;
}

function fullScreen() {
	window.moveTo(0, 0);
	window.resizeTo(window.screen.availWidth, window.screen.availHeight);

}

function centerWindow() {

	var nleft = (window.screen.availWidth - document.body.clientWidth)/2;
	var ntop = (window.screen.availHeight - document.body.clientHeight)/2;
	window.moveTo(nleft, ntop);
}

var topParent = parent;
var thisSelf = self;
function getTopParent() {
	if (topParent != thisSelf) {
		thisSelf = topParent;
		topParent = topParent.parent;
		getTopParent();
	}
	return topParent;
}
function checkDate(obj, warningMonth, warningDayPrefix, warningDayPostfix, warningDateFormat) {
	var strDate, arrDate
	var lngYear, lngMonth, lngDay
	var strReg
	var strError
	strError = ""
	strReg = /^\d{4}\/\d{2}\/\d{2}$/;
	strDate = obj.value
	arrDate = strDate.split("/")
	if (strReg.test(strDate)) {
		lngYear = parseInt(arrDate[0], 10)
		lngMonth = parseInt(arrDate[1], 10)
		lngDay = parseInt(arrDate[2], 10)
		if (lngMonth < 1 || lngMonth > 12) {
			strError = "" + warningMonth
		}
		else if (lngDay < 1 || lngDay > getDay(lngYear, lngMonth)) {
			strError = "" + warningDayPrefix + getDay(lngYear, lngMonth) + warningDayPostfix
		}
	}else{
		strError = "" + warningDateFormat
	}
	if (strError != "") {
		alert(strError)
		obj.select()
		obj.focus()
		return false;
	}
	return true;
}

function getDay(varYear, varMonth) {
	var lngDay
	varYear = parseInt(varYear)
	varMonth = parseInt(varMonth)
	switch (varMonth) {
		case 1 :
		case 3 :
		case 5 :
		case 7 :
		case 8 :
		case 10 :
		case 12 :
		lngDay = 31
		break
		case 4 :
		case 6 :
		case 9 :
		case 11 :
		lngDay = 30
		break
		case 2 :
		if ((varYear % 4 == 0 && varYear % 100 != 0) || (varYear % 400 == 0))
		lngDay = 29
		else
		lngDay = 28
		break
	} // switch
	return lngDay
}

function checkMsisdn(strMsisdn){
  	if (strMsisdn == '') {
  		return false;
  	}
  	if (strMsisdn.length < 2) {
  		return false;
  	}
  	var strReg;
  	if (strMsisdn.substring(0,2) == "09") {
		strReg = /^09\d{8}$/;
  		if (strReg.test(strMsisdn)) {
    		return true;
  		}
  	} else if(strMsisdn.substring(0,1)=="+"){ //international phone number, followed by 15 number
  		strReg = /^\d{1,15}$/;
  		if (strReg.test(strMsisdn.substring(1,strMsisdn.length))) {
    		return true;
    	}
	}
  	return false;
}

function checkDomesticMsisdn(strMsisdn){
  	if (strMsisdn == '') {
  		return false;
  	}
  	if (strMsisdn.length < 2) {
  		return false;
  	}
  	if (!checkMsisdnForPhs(strMsisdn)) {
  		return false;
  	}
	var strReg;
  	if (strMsisdn.substring(0,2) == "09") {
		strReg = /^09\d{8}$/;
  		if (strReg.test(strMsisdn)) {
    		return true;
  		}
  	} else if(strMsisdn.substring(0,1)=="+"){ //Domestic phone number
    	strReq = /^8869\d{8}$/;
		if(strReq.test(strMsisdn.substring(1,strMsisdn.length))) {
			//Phone number doesn't start with +8869, for instance, +8862
			return true;
		}
	}
  	return false;
}

function checkMsisdnGpcode(pbkStr){ //pbkStr maybe a msisdn ,maybe a pbk group number.
  	var strReg;
  	if (pbkStr == '') {
  		return false;
  	}
  	if (pbkStr.length < 5) {
  		return false;
  	}
  	if(pbkStr.substring(0,1)=="+"){ //international phone number, followed by 15 number
  		strReg = /^\d{5,15}$/;
  		if (strReg.test(pbkStr.substring(1,pbkStr.length))) {
    		strReg = /^886\d{2,12}$/;
	  		if (strReg.test(pbkStr.substring(1,pbkStr.length))) {
	  			//Domestic phone number
	  			strReq = /^8869\d{8}$/;
	  			if(!strReq.test(pbkStr.substring(1,pbkStr.length))) {
	  				//Phone number doesn't start with +8869, for instance, +8862
	  				return false;
	  			}
	  		}
	  		return true;
    	} else {
    		return false;
    	}
  	} else if(pbkStr.length==5){ //group number
  		//if(pbkStr.substring(0,3)=="840"){
		//	oneAddress = oneAddress.substring(3);
		//}
		if (pbkStr == "group") { //If no groupCode from MyHSDB, hardCode it to "group"
			return true;
		}
		strReg = /^840[0-9][0-9]$/;      //two number
		if (strReg.test(pbkStr) && pbkStr!="84000") {  //group code can't be 00
    		return true;
    	}
  	} else { //msisdn
  		strReg = /^09\d{8}$/;
  		if (strReg.test(pbkStr)) {
    		return true;
  		}
  	}
  	return false;
}

function checkDomesticMsisdnGpcode(pbkStr){ //pbkStr maybe a msisdn ,maybe a pbk group number.
  	var strReg;
  	if (pbkStr == '') {
  		return false;
  	}
  	if (pbkStr.length < 5) {
  		return false;
  	}
  	if (!checkMsisdnForPhs(pbkStr)) {
  		return false;
  	}
  	if(pbkStr.substring(0,1)=="+"){ //Domestic phone number
  		strReq = /^8869\d{8}$/;
		if(strReq.test(pbkStr.substring(1,pbkStr.length))) {
			//Phone number doesn't start with +8869, for instance, +8862
			return true;
		}
  	} else if(pbkStr.length==5){ //group number
  		if (pbkStr == "group") { //If no groupCode from MyHSDB, hardCode it to "group"
			return true;
		}
		strReg = /^840[0-9][0-9]$/;      //two number
		if (strReg.test(pbkStr) && pbkStr!="84000") {  //group code can't be 00
    		return true;
    	}
  	} else { //msisdn
  		strReg = /^09\d{8}$/;
  		if (strReg.test(pbkStr)) {
    		return true;
  		}
  	}
  	return false;
}

function checkMsisdnForPhs(strMsisdn) {
	//filter 0968, 0966, 886968, 886966 for PHS
  	if (strMsisdn.length > 4 
		&& (strMsisdn.substring(0,4) == "0968"
			|| strMsisdn.substring(0,4) == "0966")) {
		return false;
	}
	if (strMsisdn.length > 7 
		&& (strMsisdn.substring(0,7) == "+886968"
			|| strMsisdn.substring(0,7) == "+886966")) {
		return false;
	}
	return true;
}

function createBtnsH(buttons) {
	document.write("<table valign='middle' align='center' cellspacing='0' cellpadding='0'><tr><td>");
	for (i=0; i<buttons.length; ) {
		createButton(buttons[i], buttons[i+1]);
		i = i + 2;
		document.write("</td><td nowrap>&nbsp;&nbsp;</td><td>");
	}
	document.write("</td></tr></table>");
}

function createButton(btnName, btnLink) {
	document.write("<table width='100%' valign='middle' align='center' cellspacing='0' cellpadding='0'><tr>");
	document.write("<td><img  width='15' height='25' src='image/icon_01.gif'/></td>");
	document.write("<td nowrap background='image/icon_02.gif'><a href='" + btnLink + "'>" + btnName + "</a></td>");
	document.write("<td><img  width='15' height='25' src='image/icon_03.gif'/></td>");
	document.write("</tr></table>");
}

function setFrameHeight(frame){
	if (document.getElementById){
		if (frame && !window.opera){
			if (frame.contentDocument && frame.contentDocument.body.offsetHeight)
				frame.height = frame.contentDocument.body.offsetHeight;
			else if(frame.Document && frame.Document.body.scrollHeight)
				frame.height = frame.Document.body.scrollHeight;
		}
	}
}

//add by ZouXin

//check date text arange: if it is between beginNum and endNum
function checkDateText(elm,beginNum,endNum){
	elm.value = trim(elm.value);
	var  num = new  Number(elm.value);
	if(num.toString().length==""){
		return;
	}
	if(num.toString() == "NaN"){
		//alert("your input not Number");
		elm.value=elm.value.replace(/\D/gi,"");
		checkDateText(elm,beginNum,endNum);
	}else{
		if(num<beginNum){
			//alert("invalid input!");
			elm.value="";
			elm.focus();
		} else if(num>endNum){
			elm.value = elm.value.substring(0,elm.value.length-1);
			elm.focus();
		}
	}
}

//check Number
function checkNumber(elm) {
	var strReg = /^\d$/;
	if (!strReg.test(elm.value)){
		elm.value=elm.value.replace(/\D/gi,"")
		elm.focus();
		return true;
	}
	return false;
}

//check date validaty,dateStr formate: YYYY/MM/DD
//need to check leap year like 1900/1700/2100
//modify by hjzhou 2007-08-18
function checkDateValidate(dateStr){
	var strTest = /^((\d{2}(([02468][048])|([13579][26]))[\/\/\s]?((((0?[13578])|(1[02]))[\/\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\/\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\/\/\s]?((0?[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([13579][01345789]))[\/\/\s]?((((0?[13578])|(1[02]))[\/\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\/\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\/\/\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))?$/;
        if(strTest.test(dateStr)){
                var y = dateStr.substring(0,dateStr.indexOf("/"));
                var m = dateStr.substring(dateStr.indexOf("/")+1,dateStr.lastIndexOf("/"));
                var d = dateStr.substring(dateStr.lastIndexOf("/")+1);
                if(d==29 && m==2){
                  if(y%100==0 && y%400!=0) return false;
                }
		return true;
	} else {
		return false;
	}
}


 /** constructor

     @param duration integer seconds
     @param <optional> function to run while waiting.

  */
 function Pause(duration, busy){
    this.duration= duration * 1000;
    this.busywork = null; // function to call while waiting.
    this.runner = 0;

    if (arguments.length == 2) {
       this.busywork = busy;
    }

    this.pause(this.duration);

 } // Pause class

 /** pause method

     @param duration: integer in seconds

  */
 Pause.prototype.pause = function(duration){
    if ( (duration == null) || (duration < 0)) {return;}

    var later = (new Date()).getTime() + duration;

    while(true){
       if ((new Date()).getTime() > later) {
          break;
       }

       this.runner++;

       if (this.busywork != null) {
          this.busywork(this.runner);
       }

    } // while

 } // pause method


function isChinese(value) {
    if (value.length == 0) {
    	return false;
    }
    for(n=0; n < value.length; n++) {
        var c=value.charCodeAt(n);
        if (c>127) {
        	return true;
        }
    }
	return false;
}
//move these codes from /pbk/pbk.jsp
function checkContactName(elm,noneNameMsg,tooLongNameMsg) {
	elm.value = trimString(elm.value);
	if (elm.value.length == 0) {
		alert(noneNameMsg);
		elm.focus();
		return false;
	} else if (cacLen(elm.value)>255){//add by zhou huai jin 07/08/14
	  		alert(tooLongNameMsg);
			elm.focus();
			return false;
	}

	return true;
}
//add by zhouhuaijin 2007/08/22
//To Chinese chars,which's length will be considered as 2
function cacLenSpecial(value){
   	if (value=="" || value == null) {
  		return 0;
    }
   	var rtnResult=0;
   	for(n=0; n < value.length; n++) {
       	var c=value.charCodeAt(n);
    	if (c<128) { // 0-127 => 1byte
           	rtnResult++;
       	} else if((c>127) && (c<2048)) { // 127 -  2047 => 2byte
           	rtnResult += 2;
       	} else if((c>=19968)&&(c<=40959)){//Chinese chars
           	rtnResult += 2;
       	} else{
       	    rtnResult += 3;
       	}
    }
   	return rtnResult;
}
//add by zhouhuaijin 2007/08/14
//When Chinese chars were saved into DB,they will use 3 bytes,just in TWMMCU project,
// zhou huaijin 2007-08-22
function cacLen(value){
   	if (value=="" || value == null) {
  		return 0;
    }
   	var rtnResult=0;
   	for(n=0; n < value.length; n++) {
       	var c=value.charCodeAt(n);
    	if (c<128) { // 0-127 => 1byte
           	rtnResult++;
       	} else if((c>127) && (c<2048)) { // 127 -  2047 => 2byte
           	rtnResult += 2;
       	} else { // 2048 - 66536 => 3byte
           	rtnResult += 3;
       	}
    }
   	return rtnResult;
}
//add by zhouhuaijin 2007/08/14
function filterInValidChars(event){
	var e;
	var key ;
	if(!_SARISSA_IS_IE){ //firefox
	    e = event;
	    key = e.which;
	}else {
	    e = window.event;
	    key = e.keyCode;
	}
	//if press shift+3(#),shift+8(*),shift+=(+) then show normally.
	//else will ignore user's input.
	// 51<-->3#;56<-->8*
	//there has a issue puzzles me,In IE and Firefox,for the same key,they return the different keyCode.
	//for key '=',IE returns keyCode 187 and Firefox returns keyCode 61.
    if(e.shiftKey){
       if(key!=51 && key!=56 && key!=187 && key!=61){
         if(!_SARISSA_IS_IE){
            e.preventDefault();
         } else{
            e.returnValue=false;
         }
       }
       return;
     }
     if(e.ctrlKey){
       if(key!=67 && key!=86 && key!=88){
         if(!_SARISSA_IS_IE){
            e.preventDefault();
         } else{
            e.returnValue=false;
         }
       }
       return;
     }
     //48<-->0;57<-->9;
     //96<-->0(in numpad);105<-->9(in numpad)
     //106-->*(in numpad);107<-->+(in numpad);
     //8<-->backspace;46-->delete;37-->left arrow;39-->right arrow
     if(!((key>=48 && key<=57) || (key>=96 && key<=105)
    || key==106 || key==107 ||key==8||key==46||key==37 ||key==39 ||key==9))
     {
         if(!_SARISSA_IS_IE){
            e.preventDefault();
         } else{
            e.returnValue=false;
         }
     }
}
//add by zhou huaijin at 2007/08/14
//now the max length of phone num is 20
//and it can contain 0-9/*/#/+
function checkIfValidUSimPhoneNum(pbkStr){
  	var strReg = /[^0-9\+\*#]/;
  	pbkStr=trimString(pbkStr);
  	if (pbkStr == '' || pbkStr.length>20) {
  		return false;
  	}
  	if (strReg.test(pbkStr)) {//if contains invalid chars then return false
    		return false;
    }
  	return true;
}
//add by zhouhuaijin 2007/08/16
//restrict the input value should be numerals.0-9
function restrictNumeral(event){
	var e;
	var key ;
	if(!_SARISSA_IS_IE){ //firefox
	    e = event;
	    key = e.which;
	}else {
	    e = window.event;
	    key = e.keyCode;
	}
     //48<-->0;57<-->9;
     //96<-->0(in numpad);105<-->9(in numpad)
     //106-->*(in numpad);107<-->+(in numpad);
     //8<-->backspace;46-->delete;37-->left arrow;39-->right arrow
     if(!((key>=48 && key<=57) || (key>=96 && key<=105)||key==8||key==46||key==37 ||key==39 ||key==9))
     {
         if(!_SARISSA_IS_IE){
            e.preventDefault();
         } else{
            e.returnValue=false;
         }
     }
}
// add by zouxin : selectAll or unSelectAll: better than optionSelectAll and optionUnSelectAll
function boundCheckBox(checkBoxObj,checkedStatus) {
	checkBoxField = checkBoxObj;
	if (checkBoxField == null || checkBoxField == "undefined"
             || checkBoxField == undefined) {
		return ;
	}

	if (typeof checkBoxField.length == "undefined" || checkBoxField.length == undefined) {
		checkBoxField.checked = checkedStatus;
	} else {
		for(var i=0; i<checkBoxField.length; i++){
			checkBoxField[i].checked = checkedStatus;
		}
	}
}

function getHttpRequest() {
	var http_request = false;
	if(window.XMLHttpRequest) { //Mozilla
	 	http_request = new XMLHttpRequest();
	} else if (window.ActiveXObject) { // IE
	    try {
	        http_request = new ActiveXObject("Msxml2.XMLHTTP");
	    } catch (e) {
	     	try {
	      		http_request = new ActiveXObject("Microsoft.XMLHTTP");
	     	} catch (e) {
	     		//init failed
	     	}
	    }
	}
	if (!http_request) { // exception, create XMLHttpRequest object failed
	    return false;
	}
	return http_request;
}

/*
 * example: callAjax({url:url,paras:parameters,async:false,cbMethod:callBackMethod});
 * attribute name must matching.
 */
function callAjax(obj){
   new Ajax.Request(
        obj.url,
        {
           method : obj.method||'post',
           onSuccess : obj.cbMethod,
           parameters : obj.paras||'',
           asynchronous : obj.async!=false  ,
           onFailure: function(){
              alert('Something went wrong...');
           }
        }
   );
}