
/*
 * checkNum(widgetName,lastNumLimitLength)自动截取小数点后几位，并且判断是否是数字
 */
function checkNum(widgetName, lastNumLimitLength) {
    var widgetValue = "";
    if (widgetName == "[object]") {
        widgetValue = widgetName.value;
    } else {
        widgetValue = document.all(widgetName).value;
    }
    lastNumLimitLength = parseInt(lastNumLimitLength);
    var dotCount = 0;
    if (isNaN(widgetValue) && widgetValue != "-") {
        for (i = 0; i < widgetValue.length; i++) {
            var tempStr = widgetValue.charAt(i);
            if (tempStr == ".") {
                dotCount++;
            }
            if (dotCount > 1) {
                var dotPlace = widgetValue.lastIndexOf(".");
                var preValue = widgetValue.substring(0, dotPlace);
                var nextValue = widgetValue.substring(dotPlace + 1,
                        widgetValue.length);
                if (nextValue.length > lastNumLimitLength)
                    nextValue = nextValue.substring(0, lastNumLimitLength);
                if (widgetName == "[object]")
                    widgetName.value = preValue + nextValue;
                else
                    document.all(widgetName).value = preValue + nextValue;
                dotCount--;
            }

            if (isNaN(tempStr) && tempStr != ".") {
                if (widgetName == "[object]") {
                    var tempWidgetValue = widgetName.value;
                    widgetName.value = tempWidgetValue.replace(tempStr, "");
                } else {
                    var tempWidgetValue = document.all(widgetName).value;
                    document.all(widgetName).value = tempWidgetValue.replace(
                            tempStr, "");
                }
            }

            if (tempStr == " ") {
                if (widgetName == "[object]") {
                    var tempWidgetValue = widgetName.value;
                    widgetName.value = tempWidgetValue.replace(tempStr, "");
                } else {
                    var tempWidgetValue = document.all(widgetName).value;
                    document.all(widgetName).value = tempWidgetValue.replace(
                            tempStr, "");
                }
            }
        }
    }
    var dotWidgetValue = "";
    if (widgetName == "[object]") {
        dotWidgetValue = widgetName.value;
    } else {
        var dotWidgetValue = document.all(widgetName).value;
    }
    if (lastNumLimitLength == 0) {
        var dotPlace = dotWidgetValue.indexOf(".");
        if (dotPlace > 0) {
            var preValue = dotWidgetValue.substring(0, dotPlace);
            var nextValue = dotWidgetValue.substring(dotPlace + 1,
                    widgetValue.length);
            if (nextValue.length > lastNumLimitLength) {

                nextValue = nextValue.substring(0, lastNumLimitLength);
            }
            if (widgetName == "[object]")
                widgetName.value = preValue + nextValue;
            else
                document.all(widgetName).value = preValue + nextValue;
        }
    } else {
        var dotPlace = dotWidgetValue.indexOf(".");
        if (dotPlace > 0) {
            var preValue = dotWidgetValue.substring(0, dotPlace + 1);
            var nextValue = dotWidgetValue.substring(dotPlace + 1,
                    widgetValue.length);
            if (nextValue.length > lastNumLimitLength) {

                nextValue = nextValue.substring(0, lastNumLimitLength);
            }
            if (widgetName == "[object]")
                widgetName.value = preValue + nextValue;
            else
                document.all(widgetName).value = preValue + nextValue;
        }
    }
}
function LTrim(str)
/*
 * PURPOSE: Remove leading blanks from our string. IN: str - the string we want
 * to LTrim
 */
{
    var whitespace = new String(" 　\t\n\r");

    var s = new String(str);

    if (whitespace.indexOf(s.charAt(0)) != -1) {
        // We have a string with leading blank(s)...

        var j = 0, i = s.length;

        // Iterate from the far left of string until we
        // don't have any more whitespace...
        while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
            j++;

        // Get the substring from the first non-whitespace
        // character to the end of the string...
        s = s.substring(j, i);
    }
    return s;
}

/*
 * ==================================================================
 * RTrim(string) : Returns a copy of a string without trailing spaces.
 * ==================================================================
 */
function RTrim(str)
/*
 * PURPOSE: Remove trailing blanks from our string. IN: str - the string we want
 * to RTrim
 * 
 */
{
    // We don't want to trip JUST spaces, but also tabs,
    // line feeds, etc. Add anything else you want to
    // "trim" here in Whitespace
    var whitespace = new String(" 　\t\n\r");

    var s = new String(str);

    if (whitespace.indexOf(s.charAt(s.length - 1)) != -1) {
        // We have a string with trailing blank(s)...

        var i = s.length - 1; // Get length of string

        // Iterate from the far right of string until we
        // don't have any more whitespace...
        while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
            i--;

        // Get the substring from the front of the string to
        // where the last non-whitespace character is...
        s = s.substring(0, i + 1);
    }

    return s;
}

/*
 * ============================================================= Trim(string) :
 * Returns a copy of a string without leading or trailing spaces
 * =============================================================
 */
function Trim(str)
/*
 * PURPOSE: Remove trailing and leading blanks from our string. IN: str - the
 * string we want to Trim
 * 
 * RETVAL: A Trimmed string!
 */
{
    return RTrim(LTrim(str));
}

/*
 * 返回字符串长度
 */
function strLen(str) {
    var totallength = 0;
    for (var i = 0; i < str.value.length; i++) {
        var strVaule = str.value;
        var intCode = strVaule.charCodeAt(i);

        if (intCode >= 0 && intCode <= 128) {
            totallength = totallength + 1;
        } else {
            totallength = totallength + 2;
        }
    }
    return totallength;
}

/*
 * 返回字符串长度ORACLE VARCHAR2
 */
function strLen_varchar2(strVaule) {
    var totallength = 0;
    for (var i = 0; i < strVaule.length; i++) {
        var intCode = strVaule.charCodeAt(i);

        if (intCode >= 0 && intCode <= 128) {
            totallength = totallength + 2;
        } else {
            totallength = totallength + 3;
        }
    }
    return totallength;
}


/*
 * **建议从数据库中取公用号段（已实现的前台文件common_mc.jsp#checkMobileCommon）
 * 判断手机的合法性
 */
function checkMobile(str) {
    // 如果为空，则不通过校验
    if (str == '') {
        alert("手机号码不能为空");
        return false;
    }

    if (/^\d+$/.test(str)) {
        if (str.length == 11) {
            strs = str.toLowerCase();
            lens = strs.length;
            extname = strs.substring(0, 3);
            if (extname != "134" && extname != "135" && extname != "136"
                    && extname != "137" && extname != "138" && extname != "139"
                    && extname != "158" && extname != "159" && extname != "150") {
                alert("手机号码只能为移动号码，包括134-139，以及150、158、159的号段!");
                return false;
            }
            return true;
        }

        else {
            alert("请输入正确的11位移动号码");
            return false;
        }
    } else {
        alert("请输入正确的11位移动号码");
        return false;
    }

} 
/**判断易购卡密码*/
function checkEgoPass(str){
	var re = new RegExp("^[0-9]*$");
	if(str.length!=6){
		alert("密码不能为空且长度只能是6位的数字!");
        return false;
	}
	if (!re.test(str)){
		alert("密码不能为空且长度只能是6位的数字!");
        return false;
	}else{
		return true;
	}
}
String.prototype.lengthch = function() {
    var cArr = this.match(/[^\x00-\xff]/ig);
    return this.length + (cArr == null ? 0 : cArr.length);
}
function checkName(str) {
    // 如果为空，则不通过校验
    if (str == '') {
        alert("用户名不能为空");
        return false;
    } else if (str.lengthch() <= 32)
        return true;
    else {
        window.alert("输入的用户名不能超出32个字母");
        return false;
    }
}

/** 判断EMail的合法性 */
function checkEmail(str) {
    // 如果为空，则不通过校验
    if (str == '') {
        alert("email不能为空");
        return false;
    }
    if (str.charAt(0) == "." || str.charAt(0) == "@"
            || str.indexOf('@', 0) == -1 || str.indexOf('.', 0) == -1
            || str.lastIndexOf("@") == str.length - 1
            || str.lastIndexOf(".") == str.length - 1) {
        alert("Email的格式不正确！必须包含字符 @ 和 . 但是不能以@ 或.开头或结尾");
        return false;
    } else {
        return true;
    }
}

/** 判断EMail的合法性，但允许可以为空的情况 */
function checkEmail2(str) {
    if (str != "") {
        if (str.charAt(0) == "." || str.charAt(0) == "@"
                || str.indexOf('@', 0) == -1 || str.indexOf('.', 0) == -1
                || str.lastIndexOf("@") == str.length - 1
                || str.lastIndexOf(".") == str.length - 1) {
            alert("Email的格式不正确！必须包含字符@和.但是不能以@ 或.开头或结尾!");
            return false;
        }else{
        	return true;
        }
    } else {
        return true;
    }
}

/** 有模窗体 */
function openDialog(url, args, width, height, left, top) {
    //可以参考这个页面：http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/showModalDialog.htm
    return window
            .showModalDialog(
                    url,
                    args,
                    "dialogHeight: " + height+ "; dialogWidth: " + width + "; dialogTop: " + top + "; dialogLeft: " + left + "; edge: Raised; center: Yes; resizable: No; status: No;");
}

function openWindow(url, name) {
    window
            .open(url, name,
                    'left=102,top=84,width=800,height=600;status=no;scroll=no;help=no;toolbar=no;');
}

/**
  拷贝文本到剪贴板
*/
function copyToClipboard(txt) {    
     if(window.clipboardData) {    
             window.clipboardData.clearData();    
             window.clipboardData.setData("Text", txt);    
     } else if(navigator.userAgent.indexOf("Opera") != -1) {    
          window.location = txt;    
     } else if (window.netscape) {    
          try {    
               netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");    
          } catch (e) {    
               alert("被浏览器拒绝！\n请在浏览器地址栏输入'about:config'并回车\n然后将'signed.applets.codebase_principal_support'设置为'true'");    
          }    
          var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);    
          if (!clip)    
               return;    
          var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);    
          if (!trans)    
               return;    
          trans.addDataFlavor('text/unicode');    
          var str = new Object();    
          var len = new Object();    
          var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);    
          var copytext = txt;    
          str.data = copytext;    
          trans.setTransferData("text/unicode",str,copytext.length*2);    
          var clipid = Components.interfaces.nsIClipboard;    
          if (!clip)    
               return false;    
          clip.setData(trans,null,clipid.kGlobalClipboard);  
     }    
}   

//按原图比例自动拉伸
function DrawImage(ImgD,iwidth,iheight){
	var image=new Image();
	image.src=ImgD.src;
	if(image.width>0 && image.height>0){
		if(image.width >= image.height){
			if(image.width>iwidth){ 
				ImgD.width=iwidth;
				ImgD.height=image.height*(iwidth/image.width);
			}else{
				ImgD.width=iwidth; 
				ImgD.height=image.height*(iwidth/image.width);
			}
		}else{
			if(image.height>iheight){ 
				ImgD.height=iheight;
				ImgD.width=image.width*(iheight/image.height); 
			}else{
				ImgD.width=image.width*(iheight/image.height); 
				ImgD.height=iheight;
			}
		}
	}
}

//得到图片的压缩副本，也即是wf:getWebPic标签的js实现方式
function getWebPic(pic,type){
	var index = pic.lastIndexOf(".");
	var subStr1 = pic.substring(0,index);
	var subStr2 = pic.substring(index);
	var newPic = subStr1+"_"+type+subStr2;
	return newPic;	    
}

//设置COOKIE
function setCookie(name,value,expires,path,domain,secure){
	var expDays = expires*24*60*60*1000;
	var expDate = new Date();
	expDate.setTime(expDate.getTime()+expDays);
	var expString = ((expires==null) ? "" : (";expires="+expDate.toGMTString()));
	var pathString = ((path==null) ? "" : (";path="+path));
	var domainString = ((domain==null) ? "" : (";domain="+domain));
	var secureString = ((secure==true) ? ";secure" : "" );
	document.cookie = name + "=" + escape(value) + expString + pathString + domainString + secureString;
} 

//获取指定名称的cookie值
function getCookie(name){
	var result = null;
	var myCookie = document.cookie + ";";
	var searchName = name + "=";
	var startOfCookie = myCookie.indexOf(searchName);
	var endOfCookie;
	if (startOfCookie != -1)
	{
	startOfCookie += searchName.length;
	endOfCookie = myCookie.indexOf(";",startOfCookie);
	result = unescape(myCookie.substring(startOfCookie, endOfCookie));
	}
	return result;
} 

//删除指定名称的cookie
function clearCookie(name){
	var ThreeDays=3*24*60*60*1000;
	var expDate = new Date();
	expDate.setTime(expDate.getTime()-ThreeDays);
	document.cookie=name+"=;expires="+expDate.toGMTString();
} 
