﻿// 获取浏览器版本。
function getBrowserAndVersion()
{
    var Sys = {};
        var ua = navigator.userAgent.toLowerCase();
        var s;
         (s = ua.match(/msie ([\d.]+)/)) ? Sys.ie = s[1] :
         (s = ua.match(/firefox\/([\d.]+)/)) ? Sys.firefox = s[1] :
         (s = ua.match(/chrome\/([\d.]+)/)) ? Sys.chrome = s[1] :
         (s = ua.match(/opera.([\d.]+)/)) ? Sys.opera = s[1] :
         (s = ua.match(/version\/([\d.]+).*safari/)) ? Sys.safari = s[1] : 0;
        //以下进行测试
        if(Sys.ie) return ('IE: '+Sys.ie);
        if(Sys.firefox) return('Firefox: '+Sys.firefox);
        if(Sys.chrome) return('Chrome: '+Sys.chrome);
        if(Sys.opera) return('Opera: '+Sys.opera);
        if(Sys.safari) return('Safari: '+Sys.safari);
}

//获取对象。
function $(id){return document.getElementById(id);}
function $$(name){return document.getElementsByName(name);}

//屏蔽click事件
document.onkeydown=function(event)
{
    if(getKeyCode(event)==13)
        return false;
}

//复制到剪切板。
function doCopy(txt)
{
    if(copyToClipboard(txt))
    {
        alert("已经复制到剪切板了，您可以按Ctr+V粘贴到您想粘贴的地方哦！");
    }
    else
    {
        alert("请选中文本，按Ctr+C进行复制！");
    }
}
function copyToClipboard(txtJS)
{
    if(window.clipboardData)
    {
        window.clipboardData.clearData();
        window.clipboardData.setData("Text",txtJS);
        return true;
    }
    else if(getBrowserAndVersion().indexOf("Opera") != -1)
    {
        window.location = txtJS;
        return true;
    }
    /*else if(window.netscape)
    {
         try {
                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
                return true;
           } catch (e) {
                alert("被浏览器拒绝！\n请在浏览器地址栏输入'about:config'并回车\n然后将'signed.applets.codebase_principal_support'设置为'true'");
                return false;
           }
    }*/
}
//定义一个运行代码的函数。
function runCode(obj)  
{
    var code=$(obj).value;
    if (code!="")
    {
	    var newwin=window.open('','','');  
	    newwin.opener = null 
	    newwin.document.write(code);  
	    newwin.document.close();
    }
}
//字符串转化为日期。
function StringToDate(DateStr)  
{   
    var converted = Date.parse(DateStr);  
    var myDate = new Date(converted);  
    if (isNaN(myDate))  
    {   
        var newDate = DateStr.replace("-","/");  
        myDate = new Date(newDate);
    }  
    return myDate;  
} 
//js sleep
function sleep(millisecond)
{
    var _now = new Date();
    var _expirTime = _now.getTime()+millisecond;
    while(true)
    {
        var tmpNow = new Date();
        if(tmpNow.getTime()>_expirTime)
        {
            return;
        }
    }
}
//获取keyCode
function getKeyCode(evt)
{
    return window.event?window.event.keyCode:evt.which?evt.which:evt.which.charCode;
}
//调整图片
function DownImage(ImgD,MaxWidth,MaxHeight)
{ 
    var image=new Image();
    image.src=ImgD.src;
    if(image.width>0 && image.height>0)
    { 
	    var rate = (MaxWidth/image.width < MaxHeight/image.height)?MaxWidth/image.width:MaxHeight/image.height;
	    if(rate <= 1)
	    {
		    ImgD.width = image.width*rate;
		    ImgD.height =image.height*rate;
	    }
	    else
	    {
		    ImgD.width = image.width;
		    ImgD.height =image.height
	    }
    }
}
function CreateXMLHttpRequest()
{
    try{return new XMLHttpRequest();}catch(e){};
    try{return new ActiveXObject("Msxml2.XMLHTTP");}catch(e){};
    try{return new ActiveXObject("Microsoft.XMLHTTP");}catch(e){};
    return null;
}
function AjaxPost(_url,_arg_key,_arg_value,_callback,_param)
{
    if(_arg_key.length==_arg_value.length)
    {
        var xhr = CreateXMLHttpRequest();
        xhr.onreadystatechange = function()
        {
            if(xhr.readyState==4)
            {
                if(xhr.status==200)
                {
                    _callback(xhr.responseText,_param);
                }
                else
                {
                    alert("Page Status Error!");
                }
            }
        }
        
        xhr.open("POST",_url,true);
        xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
        var arg = "";
        for(i=0;i<_arg_key.length;i++)
        {
            arg+=_arg_key[i]+"=";
            arg+=typeof(_arg_value[i])!="undefined"?_arg_value[i]+"&":"0&";
        }
        arg = arg==""?arg:arg.substring(0,arg.length-1);
        xhr.send(arg);
    }
    else
    {
        alert("arrry length Error!");
    }
}
function openNewDiv(_targetID,_width,_height)
{
    var m = "mask";
    var i = "iframMaske";
    if ($(m)) document.body.removeChild($(m));
    if ($(i)) document.body.removeChild($(i));
    _scrollWidth = Math.max(document.body.scrollWidth,document.documentElement.scrollWidth);
    _scrollHeight = Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);
   
    //iframe 防止select 控件在层上的问题。
    var newiframeMask = document.createElement("iframe");
    newiframeMask.id = i;
    newiframeMask.style.position = "absolute";
    newiframeMask.style.zIndex = "1";
    newiframeMask.style.width = _scrollWidth + "px";
    newiframeMask.style.height = _scrollHeight + "px";
    newiframeMask.style.top = "0px";
    newiframeMask.style.left = "0px";
    newiframeMask.style.filter = "alpha(opacity=40)";
    newiframeMask.style.opacity = "0.40";
    document.body.appendChild(newiframeMask);
    
    //mask遮罩层
    var newMask = document.createElement("div");
    newMask.id = m;
    newMask.style.position = "absolute";
    newMask.style.zIndex = "2";
    
    newMask.style.width = _scrollWidth + "px";
    newMask.style.height = _scrollHeight + "px";
    newMask.style.top = "0px";
    newMask.style.left = "0px";
    newMask.style.background = "#33393C";
    newMask.style.filter = "alpha(opacity=40)";
    newMask.style.opacity = "0.40";
    document.body.appendChild(newMask);
    
    //新弹出层
    var newDiv = null;
    if($(_targetID)==null)
    {
        newDiv = document.createElement("div");
        newDiv.id = _targetID;
    }
    else
    {
        newDiv = $(_targetID)
    }
    
    newDiv.style.position = "absolute";
    newDiv.style.zIndex = "3";
    newDiv.style.width = _width + "px";
    newDiv.style.height = _height + "px";
    newDiv.style.top = (document.documentElement.scrollTop + document.documentElement.clientHeight/2 - (_height+4)/2) + "px";
    newDiv.style.left = (document.documentElement.scrollLeft + document.documentElement.clientWidth/2 - (_width+4)/2) + "px";
    newDiv.style.background = "#EFEFEF";
    newDiv.style.border = "2px solid #cccccc";
    newDiv.style.padding = "5px";
    newDiv.style.display = "block";
    
    if($(_targetID)==null)
    {
        document.body.appendChild(newDiv);
    }
    //弹出层滚动居中
    function newDivCenter()
    {
        newDiv.style.top = (document.documentElement.scrollTop + document.documentElement.clientHeight/2 - (_height+4)/2) + "px";
        newDiv.style.left = (document.documentElement.scrollLeft + document.documentElement.clientWidth/2 - (_width+4)/2) + "px";
    }
    if(document.all)
    {
        window.attachEvent("onscroll",newDivCenter);
    }
    else
    {
        window.addEventListener('scroll',newDivCenter,false);
    }
    document.onkeydown = function(event)
    {
        if(getKeyCode(event)==27&&$(m)!=null&&$(i)!=null)
        {
            newDiv.style.display = "none";
            $(m).parentNode.removeChild($(m));
            $(i).parentNode.removeChild($(i));
        }
    }
    
}

//拖动层。
function Drag(p_id)
{
	this.Initialize(p_id);
	var objDrag = null;
	var LastPosition_obj={};
	var LastPosition_event={};
	var NowPosition_event={};
}

Drag.prototype.Initialize=function(p_id)
{
	draged=false;
	if(getBrowserAndVersion().indexOf("IE")>-1)
	{
	    $(p_id).attachEvent("onmousedown",Drag.prototype.dragStart);
	}
	else if(getBrowserAndVersion().indexOf("Chrome")>-1)
	{
	    $(p_id).addEventListener("mousedown",Drag.prototype.dragStart,false);
	}
	document.onmousemove=Drag.prototype.dragIng;
	document.onmouseup=Drag.prototype.dragEnd;
}
Drag.prototype.dragStart=function()
{
	draged=true;
	document.onselectstart=new Function("event.returnValue=false;");
	
	objDrag=event.srcElement;
	objDrag=objDrag.parentNode;
	LastPosition_obj={x:Drag.prototype.getInfo(objDrag).left,y:Drag.prototype.getInfo(objDrag).top};
	LastPosition_event={x:window.event?event.x:event.pageX,y:window.event?event.y:event.pageY};
}

Drag.prototype.dragIng=function()
{
	if(draged)
	{
	    NowPosition_event={x:window.event?event.x:event.pageX,y:window.event?event.y:event.pageY};
	    
		objDrag.style.left=(parseInt(LastPosition_obj.x)+NowPosition_event.x-LastPosition_event.x)+"px";
		objDrag.style.top =(parseInt(LastPosition_obj.y)+NowPosition_event.y-LastPosition_event.y)+"px";
		$("tip").innerHTML = objDrag.style.top;
	}
}

Drag.prototype.dragEnd=function()
{
	draged=false;
	document.onselectstart=new Function("event.returnValue=true;");
}
Drag.prototype.getInfo=function(oO)
{
	var oN=new Object();
	oN.left=oN.top=oN.right=oN.bottom=0;
	oN.offsetWidth=oO.offsetWidth;
	oN.offsetHeight=oO.offsetHeight;
	while(oO)
	{
		oN.left+=oO.offsetLeft;
		oN.top+=oO.offsetTop;
		oO=oO.offsetParent;
	}
	oN.right=oN.left+oN.offsetWidth;
	oN.bottom=oN.top+oN.offsetHeight;
	return oN;
}

