﻿//陈鲲鹏自己编写或收集的Js功能块

//-----------------------------------------------------------------------------------
// 判断某个函数是否存在
/*
    if(typeof(FunName) == "function")
    {
        return true;
    }else{
        return false;
    }
*/

//-----------------------------------------------------------------------------------
//计算两个日期的间隔天数
//sDate1和sDate2是2002-12-18格式 
function DateDiff(sDate1, sDate2)
{
    var aDate, oDate1, oDate2, iDays; 
    aDate = sDate1.split("-"); 
    oDate1 = new Date(aDate[1] + "/" + aDate[2] + "/" + aDate[0]) ; //转换为12-18-2002格式 
    aDate = sDate2.split("-"); 
    oDate2 = new Date(aDate[1] + "/" + aDate[2] + "/" + aDate[0]); 
    //if((oDate2-oDate1)<=0){ 
    //  return oDate2 - oDate1; 
    //} 
    iDays = parseInt((oDate1 - oDate2) / 1000 / 60 / 60 /24);   //把相差的毫秒数转换为天数  Math.abs可以转成绝对值
    return iDays;
}

//计算两个日期是否在同一个月份 需要DateDiff(sDate1, sDate2)函数的支持
//sDate1和sDate2是2002-12-18格式 
function SameMonth(sDate1, sDate2)
{
    var aDate, oDate1, oDate2, iDays;
    aDate = sDate1.split("-");
    oDate1 = aDate[0] + "-" + aDate[1] +  "-1"; //转换为12-18-2002格式
    aDate = sDate2.split("-");
    oDate2 = aDate[0] + "-" +  aDate[1] +  "-1"; //转换为12-18-2002格式
    //alert(oDate1 + ", " + oDate2);
    if(DateDiff(oDate1, oDate2) == 0)
    {
        return true;
    }
    return false;
}

function isValidDate(datevalue)
{
	var SourceDateValue=datevalue.match(/^(\d{1,4})(-|\/\.)(\d{1,2})\2(\d{1,2})$/); 
	if(SourceDateValue==null) return false; 
	var EndDateValue=new Date(SourceDateValue[1],SourceDateValue[3]-1,SourceDateValue[4]); 
	return (EndDateValue.getFullYear()==SourceDateValue[1]&&(EndDateValue.getMonth()+1)==SourceDateValue[3]&&EndDateValue.getDate()==SourceDateValue[4]);
}

//-----------------------------------------------------------------------------------
//判断输入的字符串是否是数字
//string：字符串  sign：正负，可为空
function IsNumber(string, sign)
{
	var number;
	if (string==null) return false;
	if ((sign!=null) && (sign!='-') && (sign!='+'))
	{
		alert('IsNumber(string,sign)的参数出错：\nsign为null或"-"或"+"');
		return false;
	}
	number = new Number(string);
	if (isNaN(number))
	{
		return false;
	}
	else if ((sign==null) || (sign=='-' && number<0) || (sign=='+' && number>0))
	{
		return true;
	}
	else
	return false;
}

//---　循环替换全角数字为半角数字
function cycReplace(vStr, rStr, dStr)
{
    if(vStr.indexOf(rStr) >= 0)
    {
        vStr = vStr.replace(rStr, dStr);
        vStr = cycReplace(vStr, rStr, dStr);
    }
    return vStr;
}

function changeNumeric(vStr)
{
    vStr = cycReplace(vStr, "０", "0");//vStr.replace("０", "0");
    vStr = cycReplace(vStr, "１", "1");
    vStr = cycReplace(vStr, "２", "2");
    vStr = cycReplace(vStr, "３", "3");
    vStr = cycReplace(vStr, "４", "4");
    vStr = cycReplace(vStr, "５", "5");
    vStr = cycReplace(vStr, "６", "6");
    vStr = cycReplace(vStr, "７", "7");
    vStr = cycReplace(vStr, "８", "8");
    vStr = cycReplace(vStr, "９", "9");
    vStr = cycReplace(vStr, "．", ".");
    vStr = cycReplace(vStr, "。", ".");
    return vStr;
}
//-----------------------------------------------------------------------------------
//关于Cookies操作的函数
function SetCookie(name,value)//两个参数，一个是cookie的名子，一个是值
{//写cookies函数 
    var Days = 30; //此 cookie 将被保存 30 天
    var exp  = new Date();    //new Date("December 31, 9998");
    exp.setTime(exp.getTime() + Days*24*60*60*1000);
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
function getCookie(name)        
{//取cookies函数
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
     if(arr != null) return unescape(arr[2]); return null;

}
function delCookie(name)
{//删除cookies
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=getCookie(name);
    if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}


//-----------------------------------------------------------------------------------
// 删除字符串前后的空格
String.prototype.trim=function()
{ //删除前后的空格
	return this.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.ltrim=function()
{ //删除前面的空格
	return this.replace(/(^\s*)/g,"");
}
String.prototype.rtrim=function()
{ //删除后面的空格
	return this.replace(/(\s*$)/g,"");
}

//取得随机数
function getRandom(under, over){ 
    switch(arguments.length){ 
        case 1: return parseInt(Math.random()*under+1); 
        case 2: return parseInt(Math.random()*(over-under+1) + under);  
        default: return 0; 
    } 
}

function getPageScroll(){
  var yScroll;
  if (self.pageYOffset) {
    yScroll = self.pageYOffset;
  } else if (document.documentElement && document.documentElement.scrollTop){   // Explorer 6 Strict
    yScroll = document.documentElement.scrollTop;
  } else if (document.body) {// all other Explorers
    yScroll = document.body.scrollTop;
  }

  arrayPageScroll = new Array('',yScroll) 
  return arrayPageScroll;
}

//获取页面的高度和宽度等参数，返回一个数组
function getPageSize(){  
  var xScroll, yScroll;  
  if (window.innerHeight && window.scrollMaxY) {  
    xScroll = document.body.scrollWidth;
    yScroll = window.innerHeight + window.scrollMaxY;
  } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
    xScroll = document.body.scrollWidth;
    yScroll = document.body.scrollHeight;
  } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
    xScroll = document.body.offsetWidth;
    yScroll = document.body.offsetHeight;
  }

  var windowWidth, windowHeight;
  if (self.innerHeight) {  // all except Explorer
    windowWidth = self.innerWidth;
    windowHeight = self.innerHeight;
  } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
    windowWidth = document.documentElement.clientWidth;
    windowHeight = document.documentElement.clientHeight;
  } else if (document.body) { // other Explorers
    windowWidth = document.body.clientWidth;
    windowHeight = document.body.clientHeight;
  }  
  
  // for small pages with total height less then height of the viewport
  if(yScroll < windowHeight){
    pageHeight = windowHeight;
  } else { 
    pageHeight = yScroll;
  }

  if(xScroll < windowWidth){  
    pageWidth = windowWidth;
  } else {
    pageWidth = xScroll;
  }

  arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
  return arrayPageSize;
} 

///验证邮箱路径
function isEmail(email)
{
	invalidChars = " /;,:{}[]|*%$#!()`<>?";
	if (email == "")
	{
		return false;
	}
	for (i=0; i< invalidChars.length; i++)
	{
		badChar = invalidChars.charAt(i)
		if (email.indexOf(badChar,0) > -1)
		{
			return false;
		}
	}
	atPos = email.indexOf("@",1)
	if (atPos == -1)  {   return false;  }
	if (email.indexOf("@", atPos+1) != -1) {   return false;  }
	periodPos = email.indexOf(".",atPos)
	if(periodPos == -1) {
		return false;  // and at least one "." after the "@"
	}
	if ( atPos +2 > periodPos)  {
		return false;  // and at least one character between "@" and "."
	}
	if ( periodPos +3 > email.length)  {   return false;  }
	return true;
}


/////
///// 客户端Base64编码 解码
/////
var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var base64DecodeChars = new Array(
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
    52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
    -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
    15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
    -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
    41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);
//客户端Base64编码
function base64encode(str)
{
    var out, i, len;
    var c1, c2, c3;

    len = str.length;
    i = 0;
    out = "";
    while(i < len)
    {
        c1 = str.charCodeAt(i++) & 0xff;
        if(i == len)
        {
            out += base64EncodeChars.charAt(c1 >> 2);
            out += base64EncodeChars.charAt((c1 & 0x3) << 4);
            out += "==";
            break;
         }
         c2 = str.charCodeAt(i++);
         if(i == len)
         {
             out += base64EncodeChars.charAt(c1 >> 2);
             out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
             out += base64EncodeChars.charAt((c2 & 0xF) << 2);
             out += "=";
             break;
         }
         c3 = str.charCodeAt(i++);
         out += base64EncodeChars.charAt(c1 >> 2);
         out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
         out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6));
         out += base64EncodeChars.charAt(c3 & 0x3F);
    }
    return out;
}
//客户端Base64解码
function base64decode(str) {
    var c1, c2, c3, c4;
    var i, len, out;

    len = str.length;
    i = 0;
    out = "";
    while(i < len) {
    /* c1 */
    do {
       c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
    } while(i < len && c1 == -1);
         if(c1 == -1)
         break;

    /* c2 */
    do {
        c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
    } while(i < len && c2 == -1);
    if(c2 == -1)
        break;

    out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));

    /* c3 */
    do {
        c3 = str.charCodeAt(i++) & 0xff;
        if(c3 == 61)
            return out;
        c3 = base64DecodeChars[c3];
    } while(i < len && c3 == -1);
     if(c3 == -1)
         break;

     out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));

     /* c4 */
     do {
         c4 = str.charCodeAt(i++) & 0xff;
         if(c4 == 61)
      return out;
         c4 = base64DecodeChars[c4];
     } while(i < len && c4 == -1);
     if(c4 == -1)
         break;
     out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
    }
    return out;
}

/**
* 显示/隐藏指定的对象(style.display方式)
* @param object event 事件，用于捕捉鼠标位置等，可省略
*/
function MouseXY(event)
{
    return new Array(event.clientX, event.clientY) 
}

///
/// 清除 file input 的 value 值  IE下不支持 file.value=""
/// 
function cleanFileInputValue(fileobj)
{
    if(!document.all)
    {
        fileobj.value = "";
    }else{
        fileobj.select();
        document.execCommand("Delete");
    }
}

function doClick(buttonName, e)
{
//the purpose of this function is to allow the enter key to 
//point to the correct button to click.
    var key;
    var btn = document.getElementById(buttonName);
    if(window.event)
    {
        key = window.event.keyCode;     //IE
    }
    else
    {
        key = e.which;     //firefox
        //alert(key);
    }
    if (key == 13)
    {
        //Get the button the user wants to have clicked
        if (btn != null)
        { //If we find the button click it
            btn.click();
            return false;
        }
    }
}

function returnKeyCode(e)
{
    if(window.event)
    {
        return window.event.keyCode;     //IE
    }
    else
    {
        return e.which;     //firefox
    }
}

////--------------------------------------------------
//// 非全局通用Js函数
//// 用于验证码
////
function changeImg(obj)
{
    var Timer=new Date() 
    var hours=Timer.getHours() 
    var minutes=Timer.getMinutes() 
    var seconds=Timer.getSeconds()
    var isrc="imagecode/image.aspx?" + TimeDemo();
    obj.src = isrc;
}
function TimeDemo(){
    var d, s = "";
    var c = ":";
    d = new Date();
    s += d.getHours() + c;
    s += d.getMinutes() + c;
    s += d.getSeconds() + c;
    s += d.getMilliseconds();
    return(s);
}

function changeSlipcover(key)
{
    var SliDiv = document.getElementById("BodySlipcover");
    var SliWidth = getPageSize()[0];
    var SliHeight = getPageSize()[1];
    if(!document.all)
    {
        SliWidth = SliWidth - 20;
    }else{
        SliHeight = SliHeight + 30;
    }
    if(key == "show")
    {
        SliDiv.style.display = "block";
        SliDiv.style.height = SliHeight + "px";
        SliDiv.style.width = SliWidth + "px";
        SliDiv.style.bottom = SliHeight + "px";
    }else{
        SliDiv.style.display = "none";
    }
}

////--------------------------------------------------


/// ------------ for File upload pages 
function checkFilePath(obj, isExist, allowExt)
{
    //alert(allowExt == undefined);
    if(allowExt == "" || allowExt == undefined || allowExt == "undefined") allowExt = "jpg|gif";
    var objX = document.getElementsByTagName("input");
    if(obj.value.trim() == "")
    {
        return;
    }
    if(eval("obj.value.trim().toLowerCase().match(/\.(" + allowExt + ")$/i)==null"))
    {
        cleanFileInputValue(obj);
        eval("document.getElementById(\"" + obj.id + "\").style.backgroundColor=\"#ffcccc\";");
        eval("document.getElementById(\"" + obj.id + "_Div\").style.display=\"block\";");
        return;
    }else{
        eval("document.getElementById(\"" + obj.id + "\").style.backgroundColor=\"\";");
        eval("document.getElementById(\"" + obj.id + "_Div\").style.display=\"none\";");
    }
    //alert(objX.length);
    if(!isExist)
        return;
    for(var i=0; i < objX.length; i++)
    {
        var fileobj = objX[i];
        //alert("obj.id != fileobj.id" + obj.id != fileobj.id + ", fileobj.type == \"file\"" + fileobj.type == "file");
        if(fileobj.type == "file" && obj.id != fileobj.id)
        {
            //alert(i);
            if(obj.value == fileobj.value)
            {
                cleanFileInputValue(obj);
                eval("document.getElementById(\"" + obj.id + "\").style.backgroundColor=\"#ffcccc\";");
                eval("document.getElementById(\"" + obj.id + "_Div\").style.display=\"block\";");
                break;
            }else{
                eval("document.getElementById(\"" + obj.id + "\").style.backgroundColor=\"\";");
                eval("document.getElementById(\"" + obj.id + "_Div\").style.display=\"none\";");
            }
        }
    }
}


///------------------------
var WinM = "";
function showWin(DestUrl, WinWidth, WinHeight)
{
    DestUrl = "new_WinFrame.aspx?url=" + base64encode(DestUrl);
    var mWinH = getPageSize()[1];
    var mWinW = getPageSize()[2];
    if(WinM != "")
    {
        WinM.close();
    }
    if(window.showModelessDialog)
    {
        WinM = window.showModelessDialog(DestUrl, window, "status:no;help:no;resizable:yes;scroll:yes;dialogWidth:" + WinWidth + "px;dialogHeight:" + WinHeight + "px");
    }else{
        var WTop = (mWinH/2-WinHeight/2);
        if(WTop < 200)
        {
            WTop = 200;
        }
        WinHeight = WinHeight - 20;
        WinM = window.open(DestUrl, "named", "width=" + WinWidth + ", height=" + WinHeight + ", top=" + WTop + ", left=" + (mWinW/2-WinWidth/2) + ",scrollbars=auto, resizable=yes");
    }
}

