﻿/* 
判断指定的内容是否为空，若为空则弹出 警告框 
*/
function isEmpty(theValue, strMsg) {
    if (theValue == "") {
        alert(strMsg + "不能为空!");
        return true;
    }
    return false;
}

/*
* 函数说明：去除头尾空格
* 参数：	字符串
* 返回值：	无
*/
function trim(inputString) {
    return inputString.replace(/^ +/, "").replace(/ +$/, "");
}

/* 
中文判断函数，允许生僻字用英文“*”代替 
返回true表示是符合条件，返回false表示不符合 
*/
function isChinese(str) {
    var badChar = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    badChar += "abcdefghijklmnopqrstuvwxyz";
    badChar += "0123456789";
    badChar += " " + "　"; //半角与全角空格 
    badChar += "`~!@#$%^&()-_=+]\\\\|:;\\\\\'<,>?/"; //不包含*或.的英文符号 
    if ("" == str) {
        return false;
    }
    for (var i = 0; i < str.length; i++) {
        var c = str.charAt(i); //字符串str中的字符 
        if (badChar.indexOf(c) < 0) {
            return false;
        }
    }
    return true;
}
/* 
数字判断函数，返回true表示是全部数字，返回false表示不全部是数字 
*/
/*function isNumber(str){ 
if(""==str){ 
return false; 
} 
var reg = /\\D/; 
return str.match(reg)==null; 
} */

function isNumber(str) {
    if (str == "")
    { return false; }
    if (str * 1 + "" == "NaN")
    { return false; }
    return true;
}
/* 
判断给定的字符串是否为指定长度的数字 
是返回true，不是返回false 
*/
function isNumber_Ex(str, len) {
    if ("" == str) {
        return false;
    }

    if (str.length != len) {
        return false;
    }

    if (!isNumber(str)) {
        return false;
    }
    return true;
}

function checkEmail(EmailAddress) {
    //--------------------------------------------------------------------------------
    //  Name				Mode		Description
    //	EmailAddress		Input		the email address
    //
    //  Return Value        :   true/false
    //	Return Type         :   boolean
    //--------------------------------------------------------------------------------	
    var i = 0;
    var chr;
    var len;
    var Numbers = "0123456789";
    var UpperLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    var LowerLetters = "abcdefghijklmnopqrstuvwxyz";
    var EmailChar = Numbers + UpperLetters + LowerLetters + "@-_.";

    len = EmailAddress.length;

    //validate all characters
    for (i = 0; i < len; i++) {
        chr = EmailAddress.charAt(i);
        if (EmailChar.indexOf(chr) == -1) {
            return false;
        }
    }

    //Email address must include "@"
    if (EmailAddress.indexOf("@") == -1) return false;
    //Email address must include "."
    if (EmailAddress.indexOf(".") == -1) return false;
    //Email address must include only one "@"
    if (EmailAddress.lastIndexOf("@") != EmailAddress.indexOf("@")) return false;
    //"@" must not be the first or last character
    if (EmailAddress.indexOf("@") == 0 || EmailAddress.lastIndexOf("@") == len - 1) return false;
    if (EmailAddress.indexOf("@.") > -1) return false;
    //"." must be not the first or last character
    if (EmailAddress.indexOf(".") == 0 || EmailAddress.lastIndexOf(".") == len - 1) return false;
    //OK
    return true;
}
/*
图片缩放函数
参数说明：
ImgD --图片地址
width --目标宽度
height --目标高度
*/
var flag = false;
function DrawImage(ImgD, width, height) {
    var image = new Image();
    var iwidth = width;  //定义允许图片宽度
    var iheight = height;  //定义允许图片高度
    image.src = ImgD.src;
    if (image.width > 0 && image.height > 0) {
        flag = true;
        if (image.width / image.height >= iwidth / iheight) {
            if (image.width > iwidth) {
                ImgD.width = iwidth;
                ImgD.height = (image.height * iwidth) / image.width;
            } else {
                ImgD.width = image.width;
                ImgD.height = image.height;
            }
            //ImgD.alt=image.width+"×"+image.height;
        }
        else {
            if (image.height > iheight) {
                ImgD.height = iheight;
                ImgD.width = (image.width * iheight) / image.height;
            } else {
                ImgD.width = image.width;
                ImgD.height = image.height;
            }
            //ImgD.alt=image.width+"×"+image.height;
        }
    }
}

/*
函数:Allcheck
作用：用于（全选/取消全选）
参数说明：form --表单名
*/
function Allcheck(form) {
    for (var i = 0; i < form.elements.length; i++) {
        var e = form.elements[i];
        if (e.name != "checkall") {
            e.checked = form.checkall.checked;
        }
    }
}

/*
函数：ChangeImage
参数说明：
imgpath --图片地址
2005-6-9 郭立江
*/
var imagego = false;
function ChangeImage(imgpath) {
    var image = new Image();
    image.src = imgpath;
    if ((image.width > 0) && (image.height > 0)) {
        if ((image.width > 500) || (image.height > 400)) {
            alert("为了保证您的信息能被访问者迅速找到，建议您上传的图片大小不要超过500*400，\n该图片大小为：" + image.width + "*" + image.height);
            imagego = false;
        }
        else {
            var arrstr = new Array();
            arrstr = imgpath.split(".")
            var len = arrstr.length;
            var strext = arrstr[len - 1].toLowerCase();
            if (strext == "gif" || strext == "jpg") {
                imagego = true;
            }
            else {
                alert("图片格式不正确，只允许上传JPG或GIF图片");
                imagego = false;
            }
        }
    }
    else {
        imagego = false;
    }
    return imagego;
}


var checkimg = false;
function CheckImage(imgpath) {
    var image = new Image();
    image.src = imgpath;
    if ((image.width > 0) && (image.height > 0)) {
        if ((image.width > 160) || (image.height > 116)) {
            alert("为了保证您的信息能被访问者迅速找到，建议您上传的图片大小不要超过160*116，\n该图片大小为：" + image.width + "*" + image.height);
            imagego = false;
        }
        else {
            var arrstr = new Array();
            arrstr = imgpath.split(".")
            var len = arrstr.length;
            var strext = arrstr[len - 1].toLowerCase();
            if (strext == "gif" || strext == "jpg") {
                checkimg = true;
            }
            else {
                alert("图片格式不正确，只允许上传JPG或GIF图片");
                checkimg = false;
            }
        }
    }
    else {
        checkimg = false;
    }
    return checkimg;
}



var Chkgo = false;
function CheckWord(filepath) {
    if (filepath != "") {
        lowerext = filepath.substring(filepath.length - 4, filepath.length);
        lowerext = lowerext.toLowerCase();
        if ((lowerext != ".doc") && (lowerext != ".txt")) {
            alert("上传格式不正确，只允许上传doc或txt图片");
            Chkgo = false;
        }
        else {
            Chkgo = true;
        }
    }
    return Chkgo;
}
/*
函数：CheckDel()
作用：用于确定是否删除
*/
function CheckDel() {
    if (confirm("你确定要删除吗？")) {
        return true;
    }
    else {
        return false;
    }
}

/*
函数：CheckMoney()
作用：用于判断输入的是否为货币类型，返回结果为boolean类型，正确返回true;错误返回false;
参数说明：str--需要判断的字符串
*/
function CheckMoney(str) {
    var p = /^\d+\.?\d+$/;
    var ismoney = p.test(str);
    return ismoney;
}

/*
函数：CheckLength
作用：检测字符是否超过规定长度
参数：str	-字符串
len	-规定长度
返回值：true;false
时间：2006-7-21
Author：郭立江
*/
function CheckLength(str, len) {
    if (str.length > len) {
        alert("不要超过" + len + "个字符，现字符长度" + str.length);
        return true;
    }
    else {
        return false;
    }
}

function GetContent(FileUrl) {
    var xmlurl = new ActiveXObject("MicroSoft.XMLHttp");
    xmlurl.open("get", FileUrl, "false");
    xmlurl.send();
    return xmlurl.responseText;
    //return xmlurl.responseBody;
}

function SetFrm(EleName, Value) {
    eval(EleName).innerHTML = Value;
}

function errorImg(imgD) {
    //var img=new Image();
    imgD.src = "/upload/baby_Photo/err_default.jpg";
}

function copy_clip(meintext) {
    if (window.clipboardData) {

        // the IE-manier
        window.clipboardData.setData("Text", meintext);

        // waarschijnlijk niet de beste manier om Moz/NS te detecteren;
        // het is mij echter onbekend vanaf welke versie dit precies werkt:
    }
    else if (window.netscape) {

        // dit is belangrijk maar staat nergens duidelijk vermeld:
        // you have to sign the code to enable this, or see notes below 
        netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');

        // maak een interface naar het clipboard
        var clip = Components.classes['@mozilla.org/widget/clipboard;1']
                 .createInstance(Components.interfaces.nsIClipboard);
        if (!clip) return;

        // maak een transferable
        var trans = Components.classes['@mozilla.org/widget/transferable;1']
                  .createInstance(Components.interfaces.nsITransferable);
        if (!trans) return;

        // specificeer wat voor soort data we op willen halen; text in dit geval
        trans.addDataFlavor('text/unicode');

        // om de data uit de transferable te halen hebben we 2 nieuwe objecten 
        // nodig om het in op te slaan
        var str = new Object();
        var len = new Object();

        var str = Components.classes["@mozilla.org/supports-string;1"]
                .createInstance(Components.interfaces.nsISupportsString);

        var copytext = meintext;

        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);

    }
    //alert("Following info was copied to your clipboard:\n\n" + meintext);
    return false;
}

function ErrImgReplace(obj, pic) {
    obj.src = pic;
}

function CanDelte() {
    if (confirm('您确定要删除吗?')) {
        return true;
    }
    return false;
}


//+--------------------------------------------------- 
//| 比较日期差 dtStart dtEnd 格式为日期型或者 有效日期格式字符串 
//+------------------- -------------------------------- 
function dateStartEnd(strInterval, dtStart, dtEnd) {
    if (typeof dtStart == 'string')//如果是字符串转换为日期型 
    {
        dtStart = StringToDate(dtStart);
    }
    if (typeof dtEnd == 'string')//如果是字符串转换为日期型 
    {
        dtEnd = StringToDate(dtEnd);
    }
    switch (strInterval) {
        case 's': return parseInt((dtEnd - dtStart) / 1000);
        case 'n': return parseInt((dtEnd - dtStart) / 60000);
        case 'h': return parseInt((dtEnd - dtStart) / 3600000);
        case 'd': return parseInt((dtEnd - dtStart) / 86400000);
        case 'w': return parseInt((dtEnd - dtStart) / (86400000 * 7));
        case 'm': return (dtEnd.getMonth() + 1) + ((dtEnd.getFullYear() - dtStart.getFullYear()) * 12) - (dtStart.getMonth() + 1);
        case 'y': return dtEnd.getFullYear() - dtStart.getFullYear();
        case 'd-h': return parseFloat((dtEnd - dtStart) / 86400000).toFixed(2);
    }
}
//+--------------------------------------------------- 
//| 字符串转成日期类型   
//| 格式 YYYY-MM-dd-HH-mm-ss
//+--------------------------------------------------- 
function StringToDate(DateStr) {
    var converted = Date.parse(DateStr);
    var myDate = new Date(converted);
    if (isNaN(myDate)) {
        var arys = DateStr.split('-');
        myDate = new Date(arys[0], --arys[1], arys[2], arys[3], arys[4], arys[5]);
    }
    return myDate;
}


//匹配固定电话或小灵通，例如：031185907468或02185907468格式
function isTel(tel) {
    var num = tel;
    var partten = /^0(([1-9]\d)|([3-9]\d{2}))\d{8}$/;
    if (partten.test(num)) {
        return true;
    } else {
        return false;
    }
}

