var windowFeaturesMinor = 'width=320,height=160,resizable=yes,scrollbars=yes,status=yes';
var windowFeaturesMajor = 'width=800,height=600,resizable=yes,scrollbars=yes,status=yes';
var windowFeaturesStatic = 'width=396,height=200,resizable=no,scrollbars=no,status=no';
var imageExts = new Array(".bmp", ".gif", ".jpe", ".jpeg", ".jpg", ".png", ".pnm", ".pnt", ".psd", ".tif", ".tiff");
var flashExts = new Array(".swf");
var imageFlashExts = imageExts.concat(flashExts);


/**
 *检验文件上载输入框中的文件名称是否在指定范围内
 *form 文件上载输入框所在的form
 *file 文件上载输入框的值
 *extArray 包含扩展名的数组，如 new Array(".bmp", ".gif", ".jpg", ".png")
 */
function CheckAttach(form, file, extArray) {
    return true;
    allowSubmit = false;
    if (!file) {
//        alert('请选择要上载的文件');
//        return false;
        return true;
    }
    while (file.indexOf("\\") != -1)
        file = file.slice(file.indexOf("\\") + 1);
    ext = file.slice(file.indexOf(".")).toLowerCase();
    for (var i = 0; i < extArray.length; i++) {
        if (extArray[i] == ext) { allowSubmit = true; break; }
    }
    if (!allowSubmit) {
        alert("只能上载以下类型的文件:  " + (extArray.join("  ")));
        return false;
    }

    return true;
}


/**
*限制上传的文件类型
*
*file 文件输入框
*extArray 扩展名数组
*
*允许上传返回true, 否则返回false
*/
function LimitAttach(file, extArray,isalert) {
    return LimitAttachName(file.value,extArray,isalert);

}
function LimitAttachName(fileName, extArray,isalert) {

    if (isBlank(fileName)){
        return true;
    }
    ext = fileName.slice(fileName.lastIndexOf(".")).toLowerCase();
    for (var i = 0; i < extArray.length; i++) {
        if (extArray[i].toLowerCase() == ext) {
            return true;
        }
    }
    if(isalert){
        alert("只能上传以下几种类型的文件:  "
            + (extArray.join("  ")) + "\n 请重新选择文件");
            }
        return false;
}

/**
  *检查输入框中的值是否为日期型
  *
  *field 输入框对象,注意：不是名称，正确的格式为document.form.XXX或this.form.XXX
  *caption 输入框上的标注文字
  */
function CheckDate(field, caption){

    if(isBlank(field.value)) return;
    var DateValue = "";
    var seperator = "-";
    var day;
    var month;
    var year;
    var leap = 0;
    var err = 0;
    err = 0;
    DateValue = field.value;

    var re = /[^0-9\-]+/gi;

    DateValue =  DateValue.replace(re, "");   //去除所有数字和'-'以外的字符

    var parts = DateValue.split('-');

    VALIDATION: {
        var len = parts.length;
        if(len != 3) {
            err = 19;
            break VALIDATION;
        }

        if(parts[0].length == 2) {
            parts[0] = '20' + parts[0];
        }
        year = parseInt(jsTrimLeadingZero(jsTrim(parts[0])), 10);

        if ((isNaN(year))|| (year == 0)) {
            err = 20;
            break VALIDATION;
        }
        /* Validation of month*/
        month = parseInt(jsTrimLeadingZero(jsTrim(parts[1])), 10);
        if ((isNaN(month))|| (month < 1) || (month > 12)) {
            err = 21;
            break VALIDATION;
        }
        /* Validation of day*/
        day = parseInt(jsTrimLeadingZero(jsTrim(parts[2])), 10);
        if ((isNaN(day))|| (day < 1)) {
            err = 22;
            break VALIDATION;

        }

        /* Validation leap-year / february / day */
        if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
            leap = 1;
        }
        if ((month == 2) && (leap == 1) && (day > 29)) {
            err = 23;
            break VALIDATION;
        }
        if ((month == 2) && (leap != 1) && (day > 28)) {
            err = 24;
            break VALIDATION;
        }
        /* Validation of other months */
        if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
            err = 25;
            break VALIDATION;
        }
        if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
            err = 26;
            break VALIDATION;
        }
        /* if 00 ist entered, no error, deleting the entry */
        if ((day == 0) && (month == 0) && (year == 00)) {
            err = 0; day = ""; month = ""; year = ""; seperator = "";
        }
    }
    /* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
    if (err == 0) {
        field.value = year + seperator + month + seperator + day;
    }
    /* Error-message if err != 0 */
    else {
        alert(caption + '为日期型\n日期的格式为yyyy-mm-dd\n并请注意月份与日期的有效范围');
        field.value='';
        field.focus();
        return;
    }
}

function trim(str) {
    while(str.charAt(0)==' ') str=str.substring(1,str.length);   //trim leading spaces
    while(str.charAt(str.length-1)==' ') str=str.substring(0,str.length-1);  //trim trailing spaces
    return str;
}

function TrimField(field) {
    field.value = trim(field.value);
}


/**
  *检查输入框中的值是否为整数型
  *
  *form 输入框所在的form
  *names 输入框名称数组
  *captions 输入框上的标注文字数组
  */
function NoEmpty(form, names, captions){
    for(var i=0; i<names.length; i++) {
        var obj = form.elements[GetObjID(form, names[i])];
        if((obj!=null) && (isBlank(obj.value))) {
            alert(captions[i] + '不能为空');
            obj.focus();
            return false;
        }
    }

    return true;
}

/**
  *检查checkbox的选择情况是否合乎要求
  *
  *cb checkbox对象
  *caption checkbox标注文字
  *multiple 是否允许多选
  */
function CheckCB(cbname, caption, multiple){
    var checkNum = 0;

    for (var ObjID=0; ObjID < document.forms[0].elements.length; ObjID++) {
        var obj = document.forms[0].elements[ObjID];
        if (( obj.name == cbname ) && (obj.type == 'checkbox')) {
            if(obj.checked) checkNum++;
        }
    }
//
//    if(cb.length==undefined) { //如果checkbox不够成数组
//        if(cb.checked) {
//            return true;
//        }
//        else {
//            alert("请选择" + caption);
//          return false;
//        }
//    }
//
//    for(var i=0; i<cb.length; i++) {
//        if(cb[i].checked) checkNum++;
//    }

    if(checkNum == 0) {
//        alert("请选择" + caption);
        alert("没有选择条目");
        return false;
    }
    if((checkNum > 1) && (!multiple)) {
        alert(caption + "只能选择一项");
//        alert("请选择要删除的条目");
        return false;
    }

    return true;
}

/**
  *获取对象在form的element数组中的index，如未找到则返回-1
  *form 对象所在的form
  *ObjName 对象的名称
  */

function GetObjID(form, ObjName) {
    for (var ObjID=0; ObjID < form.elements.length; ObjID++) {
        if ( form.elements[ObjID].name == ObjName ) {
            return(ObjID);
            break;
        }
    }
    return(-1);
}


/**
 *根据激发change事件的select对象的selectedIndex，在text,value数组中
 *读取对应的数据填充接收接收change事件的select对象
 *
 *Obj  激发change事件的select对象
 *DesObj  接收change事件的select对象
 *ItmShowArray select对象option的text数组
 *ItmValueArray select对象option的value数组
 *ZeroValid 激发change事件的select对象的第一个option是否有效
*/
function NotifyChange(Obj, DesObj, ItmShowArray, ItmValueArray, ZeroValid){

    var sel = Obj.selectedIndex;

    var len = DesObj.length;
    for(i = len - 1; i >= 0; i--)
        DesObj.options[i] = null;

    if (sel>=0) {
       if(ZeroValid) {
           for(i = 0; i < ItmShowArray.length; i++){
                NewOptionName = new Option(ItmShowArray[sel][i], ItmValueArray[sel][i],0,0);
                DesObj.options[i] = NewOptionName;
            }
            DesObj.options[0].selected = true;
        }else if(sel >0){
            for(i = 0; i < ItmShowArray.length; i++){
                NewOptionName = new Option(ItmShowArray[sel-1][i], ItmValueArray[sel-1][i],0,0);
                DesObj.options[i] = NewOptionName;
            }
            DesObj.options[0].selected = true;
        }
    }
}


/**
 *将来源select对象被选定的option添加到目标select中
 *
 *Obj 来源select
 *DesObj 目标select
 *ZeroValid 来源select对象的第一个option是否有效
 *Jump 是否在添加完成之后，将来源select向下选定
*/
function AddItem(Obj, DesObj, ZeroValid,Jump) {
    if(ZeroValid == null){
      ZeroValid = true;
    }
    if(Jump == null){
      Jump = true;
    }

    ObjLength = Obj.length;
    DesObjLength = DesObj.length;

    var addedCount = 0;
    var lastSelectedIndex = -1;

  for(var i=0; i<ObjLength; i++){
    var opt = Obj.options[i];

    if (opt.selected){
        lastSelectedIndex = opt.index;
        if(i==0 && !ZeroValid) { //如果第一个option无效却被选择
            flag = false;
        }else {
            flag = true;
        }
      for (var j=0; j<DesObjLength; j++){
        var myopt = DesObj.options[j];
        if (myopt.value == opt.value){
          flag = false;
        }
      }
      if(flag){
        DesObj.options[DesObjLength+addedCount] = new Option(opt.text, opt.value, 0, 0);
        addedCount++;
      }
    }
  }

  if(Jump) {
        if(lastSelectedIndex < (ObjLength-1)) {
            Obj.selectedIndex = lastSelectedIndex +1;
        }else {
            Obj.selectedIndex =0;
        }

    }
}

function AddAll(Obj, DesObj, ZeroValid) {

    if(!(Obj.length >0)) return;

    Obj.selectedIndex = 0;
    do {
        AddItem(Obj, DesObj, ZeroValid,true);
    }while(Obj.selectedIndex!=0)

}

function DeleteItem(listField, Jump) {
    if(Jump == null){
      Jump = true;
    }
    if ( listField.length <= 0) {  // If the list is empty
      return ;
    }

    if((listField.selectedIndex == -1)){
      listField.selectedIndex = 0;
      return ;
    }

    minselected=0;

    for (i= listField.length-1; i>=0; i--) {
          if (listField.options[i].selected) {
            if (minselected==0 || i<minselected) {
                minselected=i;
            }
            listField.options.remove(i);
        }
    }

    len=listField.length;

    if (len>0)  {
        if (minselected>=len) {
            minselected=len-1;
        }
        if(Jump){
          listField.options[minselected].selected=true;
        }
    }
}

function Populate(Obj, ItmShowArray,ItmValueArray) {
    Reset(Obj);

    for(i = 0; i < ItmShowArray.length; i++){
        NewOptionName = new Option(ItmShowArray[i], ItmValueArray[i]);
        Obj.options[i] = NewOptionName;
    }

    if(Obj.length > 0)
        Obj.options[0].selected = true;

}

function Reset(Obj) {

    len = Obj.length;
    for(i = len - 1; i >= 0; i--)
        Obj.options[i] = null;
}

function ComposeOptions(Obj) {

    var values = '';
    var texts = '';

    len = Obj.length;
    for(i = 0; i < len; i++) {
        opt = Obj.options[i];
        if(i > 0) values += ', ';
        values += opt.value;
        if(i > 0) texts += ', ';
        texts += opt.text;
    }

    return (new Array(values, texts));
}


function NoDupl(SelObjFrom, SelObjTo) {
    var OptLstTxt = new Array;
    var OptLstVal = new Array;
    var OptLen = 0;

    var OldToVal = SelObjTo.options[SelObjTo.selectedIndex].value;
    if (OptLen == 0) {
        OptLen = SelObjFrom.length;
        for (var i = 1; i < OptLen; i++) {
            OptLstTxt[i] = SelObjFrom.options[i].text;
            OptLstVal[i] = SelObjFrom.options[i].value;
        }
    }
    var j = 1;
    for (var i = 1; i < OptLen; i++) {
        if (OptLstVal[i] != SelObjFrom.options[SelObjFrom.selectedIndex].value) {
            if (j == SelObjTo.length) {
                SelObjTo.options[j] = new Option(OptLstTxt[i]);
            }
            else {
                SelObjTo.options[j].text = OptLstTxt[i];
            }
            SelObjTo.options[j].value = OptLstVal[i];
            if (OptLstVal[i] == OldToVal) {
                SelObjTo.selectedIndex = j;
            }
            j++;
        }
    }
    if (SelObjTo.length > j)
        SelObjTo.options[(SelObjTo.length - 1)] = null;
}


/**
  *选中select中所有的选项
  *
  *form select所在的form
  *names select名称数组
  */
//function SelectAll(form,names){
//    for(var i=0; i<names.length; i++) {
//        var obj = form.elements[GetObjID(form, names[i])];
//        if(obj!=null) {
//            for(var j=0; j<obj.options.length; j++) {
//                obj.options[j].selected = true;
//            }
//        }
//    }
//
//}

/**
  *选中select中所有的选项
  *
  *form select所在的form
  *names select名称数组
  */
function SelectAll(form,names){
    var cInput=document.all.tags('select');
    for(var i in cInput) {
        var obj = cInput[i];
        for(var j=0; j<names.length; j++) {
            if(obj.name != names[j]) continue;
            for(var k=0; k<obj.options.length; k++) {
                obj.options[k].selected = true;
            }
        }
    }
}

/**
* 从一个字符串中提取属性值
*
*例如 ExtractPropValue('width=320,height=160,resizable=yes,scrollbars=yes,status=yes', 'height', ',')
*返回160
*
*rawStr 字符串
*prop 属性名称
*delim 属性名－值对(name-value)的分隔符
*/
function ExtractPropValue(rawStr,prop, delim){
    var pairs = rawStr.split(delim);

    for(var i=0; i< pairs.length; i++) {
        var pair = pairs[i].split('=');
        if(pair.length != 2) {
            continue;
        }
        if(pair[0] == prop) {
            return pair[1];
        }
    }
}

function OpenWindow(URL,windowName, windowFeatures){
    window.open(URL,windowName, windowFeatures);
}

function OpenWindowCentered(URL,windowName, windowFeatures){
    var width = ExtractPropValue(windowFeatures, 'width', ',');
    var height = ExtractPropValue(windowFeatures, 'height', ',');
    height=height-30;
    c_x = screen.width - width - 40;
    if (c_x>0){c_x=c_x/2;}
    else{c_x=0;}
    c_y = screen.height - height- 180;
    if (c_y>0){c_y=c_y/2;}
    else{c_y=0;}

    window.open(URL,windowName, 'left='+ c_x +',top='+ c_y +','+windowFeatures);
}

function CheckEmail(emailStr) {

    /* The following variable tells the rest of the function whether or not
    to verify that the address ends in a two-letter country or well-known
    TLD.  1 means check it, 0 means don't. */

    var checkTLD=1;

    /* The following is the list of known TLDs that an e-mail address must end with. */

    var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

    /* The following pattern is used to check if the entered e-mail address
    fits the user@domain format.  It also is used to separate the username
    from the domain. */

    var emailPat=/^(.+)@(.+)$/;

    /* The following string represents the pattern for matching all special
    characters.  We don't want to allow special characters in the address.
    These characters include ( ) < > @ , ; : \ " . [ ] */

    var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

    /* The following string represents the range of characters allowed in a
    username or domainname.  It really states which chars aren't allowed.*/

    var validChars="\[^\\s" + specialChars + "\]";

    /* The following pattern applies if the "user" is a quoted string (in
    which case, there are no rules about which characters are allowed
    and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
    is a legal e-mail address. */

    var quotedUser="(\"[^\"]*\")";

    /* The following pattern applies for domains that are IP addresses,
    rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
    e-mail address. NOTE: The square brackets are required. */

    var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

    /* The following string represents an atom (basically a series of non-special characters.) */

    var atom=validChars + '+';

    /* The following string represents one word in the typical username.
    For example, in john.doe@somewhere.com, john and doe are words.
    Basically, a word is either an atom or quoted string. */

    var word="(" + atom + "|" + quotedUser + ")";

    // The following pattern describes the structure of the user

    var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

    /* The following pattern describes the structure of a normal symbolic
    domain, as opposed to ipDomainPat, shown above. */

    var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

    /* Finally, let's start trying to figure out if the supplied address is valid. */

    /* Begin with the coarse pattern to simply break up user@domain into
    different pieces that are easy to analyze. */

    var matchArray=emailStr.match(emailPat);

    if (matchArray==null) {

        /* Too many/few @'s or something; basically, this address doesn't
        even fit the general mould of a valid e-mail address. */

        alert("Email address seems incorrect (check @ and .'s)");
        return false;
    }
    var user=matchArray[1];
    var domain=matchArray[2];

    // Start by checking that only basic ASCII characters are in the strings (0-127).

    for (i=0; i<user.length; i++) {
        if (user.charCodeAt(i)>127) {
            alert("Ths username contains invalid characters.");
            return false;
        }
    }
    for (i=0; i<domain.length; i++) {
        if (domain.charCodeAt(i)>127) {
            alert("Ths domain name contains invalid characters.");
            return false;
        }
    }

    // See if "user" is valid

    if (user.match(userPat)==null) {

        // user is not valid

        alert("The username doesn't seem to be valid.");
        return false;
    }

    /* if the e-mail address is at an IP address (as opposed to a symbolic
    host name) make sure the IP address is valid. */

    var IPArray=domain.match(ipDomainPat);
    if (IPArray!=null) {

        // this is an IP address

        for (var i=1;i<=4;i++) {
            if (IPArray[i]>255) {
                alert("Destination IP address is invalid!");
                return false;
            }
        }
        return true;
    }

    // Domain is symbolic name.  Check if it's valid.

    var atomPat=new RegExp("^" + atom + "$");
    var domArr=domain.split(".");
    var len=domArr.length;
    for (i=0;i<len;i++) {
        if (domArr[i].search(atomPat)==-1) {
            alert("The domain name does not seem to be valid.");
            return false;
        }
    }

    /* domain name seems valid, but now make sure that it ends in a
    known top-level domain (like com, edu, gov) or a two-letter word,
    representing country (uk, nl), and that there's a hostname preceding
    the domain or country. */

    if (checkTLD && domArr[domArr.length-1].length!=2 &&
        domArr[domArr.length-1].search(knownDomsPat)==-1) {
            alert("The address must end in a well-known domain or two letter " + "country.");
            return false;
        }

    // Make sure there's a host name preceding the domain.

    if (len<2) {
        alert("This address is missing a hostname!");
        return false;
    }

    // If we've gotten this far, everything's valid!
    return true;
}

function CheckEmailBasic(field, caption) {
    if(isBlank(field.value)) return;

    if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(field.value))){
//      alert("Invalid E-mail Address! Please re-enter.");
        alert(caption + "中不是有效的E-mail地址，请重新输入");
        field.value='';
        field.focus();
        return false;
    }
    return true;
}


function CheckAge(field, caption){
    distillPositive(field, caption);
    if(isBlank(field.value)) return;

    if((field.value < 0) || (field.value > 100)) {
        alert("年龄的有效范围为0-100");
        field.value='';
        field.focus();
        return;
    }
}

function distillInt(field, caption){
    if(isBlank(field.value)) return;

    var re = /[-|+]?[0-9]+/g;
    var found= re.exec(field.value);

    if(found != null) {
        if(isZero(found[0])) {
            field.value = 0;
        }
        else {
            field.value = found[0];
        }
    }
    else {
        alert(caption + '为整数\n');
        field.value='';
        field.focus();
    }
}

function distillPositive(field, caption){
    if(isBlank(field.value)) return;

    var re = /[0-9]+/g;
    var found= re.exec(field.value);

    if(found != null) {
        if(isZero(found[0])) {
            field.value = 0;
        }
        else {
            field.value = found[0];
        }
    }
    else {
        alert(caption + '为正整数\n');
        field.value='';
        field.focus();
    }
}


function CheckPostal(field, caption){
    if(isBlank(field.value)) return;

    var re = /[0-9]{6}/g;
    var found= re.exec(field.value);

    if(found != null) {
        field.value = found[0];
    }
    else {
        alert(caption + '为六位整数\n');
        field.value='';
        field.focus();
    }
}

/**
* 全选或全不选checkbox
* cb checkbox对象
* checkit wanna check it or not
*/
function ToggleCheck(cbname, checkit,checkdisabled){
    var elements = document.forms[0].elements;
    for (var ObjID=0; ObjID < elements.length; ObjID++) {
        var obj = elements[ObjID];
        if (( obj.name == cbname ) && (obj.type == 'checkbox')) {
			if((checkdisabled==false)&&(obj.disabled==true)){   
				continue;     
			}
			obj.checked = checkit;
        }
    }
//    if(cb==undefined) return;
//
//    if(cb.length==undefined) { //如果checkbox不够成数组
//        cb.checked =  checkit;
//    }
//    else {
//        for(var i=0; i<cb.length; i++) {
//            cb[i].checked = checkit;
//        }
//    }
}


function FindEmailAddresses(StrObj) {
    var separateEmailsBy = ", ";
    var email = ""; // if no match, use this
    var emailsArray = StrObj.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
    if (emailsArray) {
        email = "";
        for (var i = 0; i < emailsArray.length; i++) {
            if (i != 0) email += separateEmailsBy;
            email += emailsArray[i];
        }
    }
    return email;
}

function CheckRadio(radioname, caption){
    var isChecked = false;

    for (var ObjID=0; ObjID < document.forms[0].elements.length; ObjID++) {
        var obj = document.forms[0].elements[ObjID];
        if (( obj.name == radioname ) && (obj.type == 'radio')) {
            isChecked = (isChecked || obj.checked);
        }
    }

    if(!isChecked) alert('请选择' + caption);

    return isChecked;
}

function CompareDate(dateOne, dateTwo){
    var partsOne = dateOne.split('-');
    var partsTwo = dateTwo.split('-');

    if((partsOne.length != 3) || (partsTwo.length != 3)) {
        return NaN;
    }

    if ((parseInt(jsTrimLeadingZero(jsTrim(partsOne[0])), 10) - parseInt(jsTrimLeadingZero(jsTrim(partsTwo[0])), 10)) > 0) return 1;
    if ((parseInt(jsTrimLeadingZero(jsTrim(partsOne[0])), 10) - parseInt(jsTrimLeadingZero(jsTrim(partsTwo[0])), 10)) < 0) return -1;

    if ((parseInt(jsTrimLeadingZero(jsTrim(partsOne[1])), 10) - parseInt(jsTrimLeadingZero(jsTrim(partsTwo[1])), 10)) > 0) return 1;
    if ((parseInt(jsTrimLeadingZero(jsTrim(partsOne[1])), 10) - parseInt(jsTrimLeadingZero(jsTrim(partsTwo[1])), 10)) < 0) return -1;

    if ((parseInt(jsTrimLeadingZero(jsTrim(partsOne[2])), 10) - parseInt(jsTrimLeadingZero(jsTrim(partsTwo[2])), 10)) > 0) return 1;
    if ((parseInt(jsTrimLeadingZero(jsTrim(partsOne[2])), 10) - parseInt(jsTrimLeadingZero(jsTrim(partsTwo[2])), 10)) < 0) return -1;

    return 0;
}

/**
*选定SelObj第一个文字中包含prefix的选项
*
*
*/
function scrollSelect(prefix, SelObj) {
    if(isBlank(prefix)) return;

    prefix = jsTrim(prefix);

    var OptLen = SelObj.length;
    for (var i = 0; i < OptLen; i++) {
        var txt = SelObj.options[i].text;

        if(txt.indexOf(prefix) != -1)  {
            SelObj.options[i].selected = true;
            break;
        }
    }

}

function jsTrim(s) {
    return s.replace(/(^\s+)|(\s+$)/g, "");
}

function jsTrimLeadingZero(s) {
    return s.replace(/(^0+)/g, "");
}

function isBlank(s) {
    var re = /^\s*$/g;

    return re.test(s);
}

function isASCII(str) {
    var re = /^[\x00-\x80]+$/gi;

    return re.test(str);
}

function isZero(s) {
    var re = /^[-|+]?0+(\.0*)?$/g;

    return re.test(s);
}


/**
*确认输入框数组fields中的所有输入框至少有一个不为空
*
*有一个以上输入框的值不为空返回true, 否则返回false.
*/
function hasNonblank(fields, titles) {

    var len = fields.length;

    for(var i=0; i<len; i++) {
        var field = fields[i];

        if((field!=null) && (!isBlank(field.value))) {
            return true;
        }

    }

    alert(titles.join("、") + '全部为空');
    return false;
}


/**
*确认输入框数组fields中的所有输入框都不为空
*
*有一个以上输入框的值为空返回false.
*/
function containNoBlank(fields, titles) {

    var len = fields.length;

    for(var i=0; i<len; i++) {
        var field = fields[i];
        var title = titles[i];

        if((field!=null) && (isBlank(field.value))) {
            alert(title + '不能为空');
            field.focus();
            return false;
        }

    }
    return true;
}

function distillDigitalCode(field, caption, maxDigitalCount, digitalOnly){
    if(isBlank(field.value)) return;

    var re =  null;
    if(digitalOnly) {
        re =  new RegExp ("[0-9]{1,"+ maxDigitalCount +"}", "gi");
    }
    else {
        re =  new RegExp ("[A-Za-z0-9]{1,"+ maxDigitalCount +"}", "gi");
    }


     var found= re.exec(field.value);

    if(found != null) {
        field.value=found[0];

        return true;
    }
    else {
        if(digitalOnly) {
            alert(caption + '应为数字编码,最大只允许有'+ maxDigitalCount +'个数字');
        }
        else {
            alert(caption + '应为数字/字母编码,最大只允许有'+ maxDigitalCount +'个数字/字母');
        }
        field.value='';
        field.focus();
    }
}

function distillColor(field, caption){
    if(isBlank(field.value)) return;

    var re = /[0-9a-f]{6}/gi;
    var found= re.exec(field.value);

    if(found != null) {
        field.value = '#' + found[0];
    }
    else {
        alert(caption + '为颜色，应该类似#FFFFFF形式\n');
        field.value='';
        field.focus();
    }
}


function checkIdentify(field, caption){
    if(isBlank(field.value)) return false;

    var reOld = /^[0-9]{15}$/gi;
    var reNew = /^[0-9]{17}[0-9x]{1}$/gi;

    if(reOld.test(field.value)) {
        return true;
    }
    else if(reNew.test(field.value)){
        return true;
    }
    else {
        alert(caption + ' 请输入正确的身份证号码！');
        field.value='';
        field.focus();

        return false;
    }

}

function checkSalary(field, caption){
     if(isBlank(field.value)) return false;


     var re = /^[0-9]+(\.[0-9]{1,2})?$/gi;
     var found= re.exec(field.value);

    if(found != null) {
        if(isZero(found[0])) {
            field.value='0';
        }

        return true;
    }
    else {
        alert(caption + '工资应为数值型，且只允许带两位小数！');
        field.value='';
        field.focus();
    }

}

function checkDecimal(field, caption, decimalFractionCount){
     if(isBlank(field.value)) return false;

	
     var re =  new RegExp ("^[0-9]+(\\.[0-9]{1,"+ decimalFractionCount +"})?$", "gi");

     var found= re.exec(field.value);

    if(found != null) {
		
        if(isZero(found[0])) {
            field.value='0';
        }

        return true;
    }
    else {
        alert(caption + '应为数值型,最大只允许有'+ decimalFractionCount +'位小数');
        field.value='';
        field.focus();
    }

}


/**
*enbale/disable a stack of input fields
*
*
*/
function enableInput(fields, inputEnabled) {

    var len = fields.length;

    for(var i=0; i<len; i++) {
        var field = fields[i];

        if(field!=null) {
           field.disabled = !inputEnabled;
        }

    }
}

function instanceOf(object, constructor) {
    while (object != null) {
        if (object == constructor.prototype) {
            return true;
        }
        object = object.__proto__;
    }

    return false;
}
/**
*检查输入框中字符串的长度
*
*field  输入框对象
*maxlimit 允许的最大长度
*trimExtra 是否去除多余的字符串
*
*return 最终的字符串长度
*/
function textCounterPro(field, maxlimit, trimExtra) {
    if (field.value.length > maxlimit) { // if too long...trim it!
        if(trimExtra) {
            alert('允许的最大长度为' + maxlimit + ',超出长度的字符串将被截去');
            field.value = field.value.substring(0, maxlimit);
        }
    }

    return field.value.length;
}


/**
* 设置输入框的值
*
*/
function setValue(field, value) {
    if((field != null) && (value != null) && !isBlank(value)) {
        field.value = value;
    }
}
function getListValue(field,separator){
	 var str = '';

     var needSeparator = false;
     var objs = field.options;
      for (var i = 0; i<objs.length; i++) {

        if (needSeparator) {
          str += separator;
        }

        str += objs[i].value;
        needSeparator = true;
      }
	 return str;
}