﻿//maxlab common script 1.0
var Max = {
	Version: '1.0.0',
	Browser: {
		IE:     !!(window.attachEvent && !window.opera),
		Opera:  !!window.opera,
		WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1,
		Gecko:  navigator.userAgent.indexOf('Gecko') > -1
	}
}
function $(id)
{
    return document.getElementById(id);
}
//跳转到新地址
function go(url)
{
    location.href = url;
}

//post提交到新地址
function postTo(url)
{
    document.forms[0].action = url;
    document.forms[0].submit();
}
function CheckInt(objID,alertStr)
{
    var obj = document.getElementById(objID);
    var patrn=/^(\d{0,12})$/; 
    if (!patrn.exec(obj.value)) 
    {
        obj.focus();
        return false;
    }
    else
    {
        return true;
    }
    
}
function Collapsed(showObjID,imgico)
{
    var showObj = document.getElementById(showObjID);
    var imgicosrc = imgico.src;
    var icostatus = true;
    var srcPath ='';
    if(imgicosrc.lastIndexOf('/')>-1)
    {
        srcPath = imgicosrc.substring(0,imgicosrc.lastIndexOf('/')+1);;
        if(imgicosrc.indexOf('no')>0)
        {
            icostatus = true;
        }
        if(imgicosrc.indexOf('yes')>0)
        {
            icostatus = false;
        }
    }
    if(icostatus)
    {
        showObj.style.display ="none";
        imgico.src = srcPath+"collapsed_yes.gif";
        imgico.alt = "展开";
        imgico.title = "展开";
    }
    else
    {
        showObj.style.display ="";
        imgico.src = srcPath+"collapsed_no.gif";
        imgico.alt = "收起";
        imgico.title = "收起";
    }
    
}

function getScrollOffset() {
	var point;
	if (document.body.scrollTop != 0) {
		point = { x : document.body.scrollLeft, y : document.body.scrollTop };
	} else {
		point = { x : document.documentElement.scrollLeft, y : document.documentElement.scrollTop };
	}
	return point;
}

var tooltip;
document.write("<div id='js_tooltip' style='position:absolute; z-index:1000; visibility:hidden'></div>");
function popLayer(e) {
	var el = e.srcElement ? e.srcElement : e.target;
	if (!el) {
		return;
	}
	if (el.alt != null && el.alt != '') {
		el.tooltip = el.alt;
		el.alt = '';
	}
	if (el.title != null && el.title != '') {
		el.tooltip = el.title;
		el.title = '';
	}
	if (el.tooltip != tooltip) {

		tooltip = el.tooltip;
		var container = document.getElementById('js_tooltip');
		if (tooltip == null || tooltip == '') {
			container.style.display = 'none';
			container.style.width = "0px";
			container.style.visibility = 'hidden';
		} else {
			container.style.display = '';
			container.style.width = "auto";
			container.innerHTML = tooltip.replace(/\n/g, '<br />');
			if (container.clientWidth > 280) {
				container.style.width = "280px";
			}
			var mouse = { x : e.clientX, y : e.clientY };
			var scrollOffset = getScrollOffset();
			var size = { w : container.clientWidth, h : container.clientHeight };
			var adjustLeft  = mouse.x + 12 + size.w > document.body.clientWidth ?  - size.w - 12 : 12;
			var adjustRight = mouse.y + 12 + size.h > document.body.clientHeight ? - size.h - 12 : 12;
			container.style.left = (mouse.x + scrollOffset.x + adjustLeft) + 'px';
			container.style.top  = (mouse.y + scrollOffset.y + adjustRight) + 'px';
			container.style.visibility = 'visible';
		}
	}
}
document.onmouseover = function(e) {
	if (typeof popLayer == "function") {
		popLayer(Max.Browser.IE ? event : e);
	}
}


// --------------------------------------


function QQChat(qqnumber) {
    try {
        new ActiveXObject("TimwpDll.TimwpCheck");
        location.href = 'Tencent://Message/?menu=yes&exe=&uin=' + qqnumber + '&websiteName=bbsmax&info=';
    } catch (e) {
        alert("你没有安装QQ或浏览器不支持，不能与该用户直接对话");
    }
}

function WLMChat(WLID) {
    try {
        new ActiveXObject("MSNMessenger.P4QuickLaunch");
        location.href = 'msnim:chat?contact=' + WLID;
    } catch (e) {
        alert("你没有安装Windows Live Messenger或浏览器不支持，不能与该用户直接对话");
    }
}

//添加到QQ表情
function AddQQEmotion(url)
{
    try
    {
	    var axo = new ActiveXObject("QQCPHelper.CPAdder");
	    if(axo)
        {
    	    if(axo.AddCustomEmotion(url)!=0)
		    {
			    alert("添加表情失败");
		    }
        }
    }
    catch(e)
    {
        alert('添加失败，可能未安装腾讯QQ！');
    }
}

// --------------------------------------


//改变CSS样式
function ChangeCss(sClassName ,obj )
{
	obj.className = sClassName;
}
//显示一个层
function DivShow(divID,divClassName)
{
   var objDiv = document.getElementById(divID);
   ChangeCss(divClassName,objDiv);
}
//显示隐藏文字
function ShowOrHidden(objID,objClassName)
{
    var tempObj = document.getElementById(objID);
    ChangeCss(objClassName,tempObj);
}
//
function keyDownEvent(ev,buttionID,action) 
{
    var e = ev?ev:window.event;
    if(action=='CtrlAndEnter')
    {
	    if((e.ctrlKey && e.keyCode == 13)|| (e.altKey && e.keyCode == 83) ) 
	    {
		    document.getElementsByName(buttionID)[0].click();
        }
    }
    else if(action=='Enter')
    {
        if(e.keyCode == 13) 
	    {
		    document.getElementsByName(buttionID)[0].focus();
        }
    }
}
//打开一个对话框
function OpenDialog( dialogName, pageUrl, dialogWidth, dialogHeight, parentWindow )
{
    var dialogInfo = self;
    dialogInfo.PageUrl = pageUrl;
    dialogInfo.DialogName = dialogName
    
    var dialogFileUrl = root + "/Common/html/Dialog.html";
    if (Max.Browser.IE)
    {
       parentWindow.showModalDialog( dialogFileUrl, dialogInfo, "dialogWidth:" + dialogWidth + "px;dialogHeight:" + dialogHeight + "px;help:no;scroll:no;status:no") ;
    }
    else
    {
        var sOption  = "location=no,menubar=no,toolbar=no,dependent=yes,dialog=yes,minimizable=no,modal=yes,alwaysRaised=yes,resizable=no" +
		    ",width="  + dialogWidth +
		    ",height=" + dialogHeight;
        var oWindow = parentWindow.open( '', 'MaxDialog_' + dialogName, sOption, true ) ;
        oWindow.moveTo( '250', '150' ) ;
	    oWindow.resizeTo( dialogWidth, dialogHeight+50 ) ;
	    oWindow.focus() ;
	    oWindow.location.href = pageUrl ;
	    oWindow.dialogArguments = dialogInfo ;
    }
}

/**
 * 获取IE版本
 */
function fGetxxIEVer()
{
	var iVerNo = 0;
	var sVer = navigator.userAgent;
	if(sVer.indexOf("MSIE")>-1){
		var sVerNo = sVer.split(";")[1];
		sVerNo = sVerNo.replace("MSIE","");
		iVerNo = parseFloat(sVerNo);
	}
	return iVerNo;
}
//表单不能为空验证
function formValid(elems)
{
    for(i = 0; i < elems.length; i++)
    {
        with(document.getElementsByName(elems[i][0])[0])
        {
            if(eval(elems[i][1]))
            {
                window.alert(elems[i][2]);
                focus();
                return false;
            }
        }
    }
    return true;
}
function formValidNoFocus(elems)
{
    for(i = 0; i < elems.length; i++)
    {
        with(document.getElementsByName(elems[i][0])[0])
        {
            if(eval(elems[i][1]))
            {
                window.alert(elems[i][2]);
                return false;
            }
        }
    }
    return true;
}


//-----------------------------

//选中所有复选框
function checkAll(checkBoxs,checkAllName) {
    var boxs=document.getElementsByName(checkBoxs);
    var checkAllBox=document.getElementsByName(checkAllName)[0];
    for (var i=0;i<boxs.length;i++) 
    { 
        if(checkAllBox.checked) {
            boxs[i].checked=true;}
        else{
            boxs[i].checked=false; }
    } 
}

//选中所有复选框
function checkAllByID(StartIndex,count,checkAll) {

    var chkall= document.getElementsByName(checkAll)[0];
    var chkitem= document.getElementsByTagName("input");
    var len;
    var ct = count;
    
    for (var i=0;i<chkitem.length;i++)
    {
        if( chkitem[i].type=="checkbox")
        {
            if(ct==0)
            {
                break;
            }
            
            if(chkitem[i].id.indexOf('Checkbox')>-1)
            {
                len = chkitem[i].id.lastIndexOf('box')+3;
                var index = chkitem[i].id.substring(len);
                if(index>=StartIndex)
                {
                    if(chkall.checked==true)
                    {
                        
                        chkitem[i].checked=true;
                    }
                    else
                    {
                        chkitem[i].checked=false;
                    }
                    ct--;
                }
            }
        }
    }
 }
 
 
 
 
function CheckAllInControlByName(checkAllInput,controlname)
{
    var chkall= document.getElementsByName(checkAllInput)[0];
    var chkitem= document.getElementsByTagName("input");
    for (var i=0;i<chkitem.length;i++)
    {
        if( chkitem[i].type=="checkbox")
        {
            if(chkitem[i].name == controlname)
            {
                if(chkitem[i].disabled == true)
                    continue;
                if(chkall.checked)
                {
                    chkitem[i].checked=true;
                }
                else
                {
                    chkitem[i].checked=false;
                }
            }
        }
    }
}
 
//选中所有复选框
function CheckAllInServerControl(checkAllInput,controlID)
{
    var chkall= document.getElementsByName(checkAllInput)[0];
    var chkitem= document.getElementsByTagName("input");
    for (var i=0;i<chkitem.length;i++)
    {
        if( chkitem[i].type=="checkbox")
        {
            if(chkitem[i].id.indexOf(controlID)>-1)
            {
                if(chkall.checked)
                {
                    chkitem[i].checked=true;
                }
                else
                {
                    chkitem[i].checked=false;
                }
            }
        }
    }
}

//--------------------??

 //选中所有复选框
function CheckAllInCBList(checkAllInput,cbListID)
{
    var chkall= document.getElementsByName(checkAllInput)[0];
    var chkitem= document.getElementsByTagName("input");
    for (var i=0;i<chkitem.length;i++)
    {
        if( chkitem[i].type=="checkbox")
        {
            if(chkitem[i].id.indexOf(cbListID+"_")>-1)
            {
                if(chkall.checked==true)
                {
                    chkitem[i].checked=true;
                }
                else
                {
                    chkitem[i].checked=false;
                }
            }
        }
    }
}

function checkHadCheckedInlist(cbListID) {

    var chkitem= document.getElementsByTagName("input");
    for (var i=0;i<chkitem.length;i++)
    {
        if( chkitem[i].type=="checkbox")
        {
            if(chkitem[i].id.indexOf(cbListID+"_")>-1)
            {
                if(chkitem[i].checked)
                    return true;
            }
        }
    }
    return false;
 }

//检查复选框是否有至少一项被选中
function checkHadChecked(checkBoxs) {
    
    var boxs=document.getElementsByName(checkBoxs);
    for (var i=0;i<boxs.length;i++) 
    { 
        if(boxs[i].checked)
        {
            return true;
        }
    } 
    return false;
 }

function getCheckedValueInRadioList(radioName)
{
    var radioList=document.getElementsByName(radioName);
    for(var   i=0;   i<radioList.length;   i++)   
    {   
        if(radioList[i].checked)
        {
            return radioList[i].value;
        }   
    } 
}

function isNumber(string)
{
    var partn=/^[0-9]+$/;
    if (!partn.exec(trim(string)))
        return false;
    return true;
}
function isNumberAndNotZero(string)
{
    if(trim(string)=="0")
        return false;
    else if(!isNumber(string))
        return false;
    return true;
}
function isEmail(string)
{
    var partn=/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/;
    if (!partn.exec(string))
        return false;
    return true;

}
function isUrl(string)
{
    var partn="^[a-zA-z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$";
    var r = new RegExp(partn, "g");
    return r.test(string);
}

function trim(str)
{
    if(str == null) return "" ;

    // 去除前面所有的空格
    while( str.charAt(0)  == ' ' )
    {
        str = str.substring(1,str.length);
    }

    // 去除后面的空格
    while( str.charAt(str.length-1)  == ' ' )
    {
        str = str.substring(0,str.length-1);
    }

    return str ;
}


//获取地址栏中的URL参数
function getParameterFromUrl(paramName)
{  
      var url = window.location.search;
      var find = eval("/" + paramName + "=([^&]*)/")  
      if(find.test(url))
      {  
          return RegExp.$1  
      }  
      else
      {  
          return null  
      }  
}


function ClickButton(buttonName) {
    var buttonProxy = document.getElementById("ButtonProxy");
    if (buttonProxy) {
        buttonProxy = document.getElementsByName("buttonProxy")[0];
        buttonProxy.setAttribute("value", buttonName);
    }
    else {
        buttonProxy = document.createElement("input");
        buttonProxy.setAttribute("type", "hidden");
        buttonProxy.setAttribute("id", "ButtonProxy");
        buttonProxy.setAttribute("name", "ButtonProxy");
        buttonProxy.setAttribute("value", buttonName);
        document.getElementsByTagName('form')[0].appendChild(buttonProxy);
    }
    document.forms[0].submit();
}
function getMsIeVersion()
{
     if(navigator.userAgent.indexOf("MSIE")>0){   
        var version=navigator.userAgent.match(/MSIE ([^;]+)/)[1];
        return version.substring(0,version.indexOf("."));
      }
     else
        return -1;
}



//=====================================================================================
var menuTimer;
var menuTimeout = 500;
function initMenu(baseID,showObjID)
{
    var baseObj = $(baseID);
    var showObj = $(showObjID);
    //要show的对象初始化
    if(baseObj&&!baseObj.initialized)//对象没有初始化，则初始化。
    {
        baseObj.initialized = true;
        var overfunction = typeof(baseObj.onmouseover)=="function" ? baseObj.onmouseover:null;
        var outfunction = typeof(baseObj.onmouseout)=="function" ? baseObj.onmouseout:null;
        
        //重写mouseover事件
        baseObj.onmouseover = function (e)
        {
            if(overfunction)
            {
                overfunction();
                if(menuTimer) clearTimeout(menuTimer);
            }
        }
        
        //重写mouseout事件
        baseObj.onmouseout = function (e)
        {
            if(outfunction)
            {
                outfunction();
            }
            menuTimer = setTimeout('hideMenu(\'' + showObjID + '\')',menuTimeout);
        }
    }
    //show内容对象初始化
    if(showObj&&!showObj.initialized)//对象没有初始化，则初始化。
    {
        showObj.initialized = true;
        showObj.style.position = 'absolute';
        showObj.style.zIndex = 1001;
        //注册mouseover事件
        showObj.onmouseover = function (e)
        {
            clearTimeout(menuTimer);
        }
        //注册mouseout事件
        showObj.onmouseout = function (e)
        {
            menuTimer = setTimeout('hideMenu(\'' + showObjID + '\')',menuTimeout);
        }
    }
}
function hideMenu(showObjID)
{
    var showObj = $(showObjID);
    if(menuTimer)
    {
        clearTimeout();
    }
    if(showObj)
    {
        showObj.style.display ='none';
    }
}
function getOffset(objWho)
{
    var leftOffset = objWho.offsetLeft;
	var topOffset = objWho.offsetTop;
	while((objWho = objWho.offsetParent) != null) {
		leftOffset += objWho.offsetLeft;
		topOffset += objWho.offsetTop;
	}
	return { 'left' : leftOffset, 'top' : topOffset };
}
// 相对尺寸
function GetOffsetTop (el, p) {
    var _t = el.offsetTop;
    var _p = el.offsetParent;

    while (_p) {
        if (_p == p) break;
        _t += _p.offsetTop;
        _p = _p.offsetParent;
    }

    return _t;
};
function GetOffsetLeft (el, p) {
    var _l = el.offsetLeft;
    var _p = el.offsetParent;

    while (_p) {
        if (_p == p) break;
        _l += _p.offsetLeft;
        _p = _p.offsetParent;
    }

    return _l;
};

function showMenu (baseID, divID) {
    baseID = $(baseID);
    divID  = $(divID);

    var l = GetOffsetLeft(baseID);
    var t = GetOffsetTop(baseID);

    divID.style.left = l + 'px';
    divID.style.top = t + baseID.offsetHeight + 'px';
    if (divID.timer) clearTimeout(divID.timer);
    divID.style.display = 'block';

    if (! divID.isCreate) {
        divID.style.position = 'absolute';
        divID.isCreate = true;
        divID.timer = 0;
        divID.onmouseover = function () {
            if (divID.timer) clearTimeout(divID.timer);
            divID.style.display = 'block';
        };

        function hide () {
            divID.timer = setTimeout(function () {divID.style.display = 'none'}, 500);
        }

        divID.onmouseout = hide;
        baseID.onmouseout = hide;
    }
}

function HtmlEncode(text)
{
    if (typeof(text) != "string" )
       text = text.toString();
    text = text.replace(/\<br \/\>/g,"xxx");
    text = text.replace(/&/g, "&amp;").replace(/''/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/xxx/g,"<br/>");
    return text ;
}


///剪切板操作
function setClipboardDataTimer(text,objInner,times)
{
    timer = setInterval(function(){setClipboardData(text,objInner);},times);
}
function setClipboardData(textstr,objInner)
{
    if(document.all)
    {
        window.clipboardData.setData("Text",textstr);
    }
    else
    {
        netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect'); 
        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 = textstr; 
        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); 

    }
    objInner.innerHTML = '自动备份:'+Date();
}
function getDataLength(str)  
{  
  var   j   =   0;  
  if   (str=="")return 0; 
  for   (var   i=0;   i<str.length;   i++)  
  {  
          if   (str.charCodeAt(i)   >   255)   j   =   j   +   2;  
          else   j++  ;
  }  
  return j;  
} 
function mousePosition(ev)
{     
    if(ev.pageX || ev.pageY)
    {
        return {x:ev.pageX, y:ev.pageY};
    }
    return {x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,y:ev.clientY + document.body.scrollTop  - document.body.clientTop};
}
function getScroll()
{     
    var top, left, width, height;          
    if (document.documentElement && document.documentElement.scrollTop) 
    {         
        top = document.documentElement.scrollTop;
        left = document.documentElement.scrollLeft; 
        width = document.documentElement.scrollWidth;
        height = document.documentElement.scrollHeight;
    } 
    else if (document.body) 
    {         
        top = document.body.scrollTop;         
        left = document.body.scrollLeft;         
        width = document.body.scrollWidth;         
        height = document.body.scrollHeight;     
    }     
    return { top: top, left: left, width: width, height: height};
} 
function getPosX(elt)
{ 
    return getOffset(elt)['left']; 
}   
function getPosY(elt)
{
    return getOffset(elt)['top']; 
}   

function hidePopUp(parms)
{
    var showObj;
    if(arguments.length>0)
    {
        showObj = $(parms); 
        if(showObj)
        {
            showObj.style.display = 'none';
        }
    }
    else
    {
        showObj = $('pop_div'); 
        if(showObj)
        {
            showObj.style.display = 'none';
        }
    }
}
function displayPopUp()
{
    var showObj = $('pop_div'); 
    if(showObj)
    {
        showObj.style.display = 'block';
    }
}
function showPopUp(divClassName)
{
    var showObj = $('pop_div'); 
    if(!showObj)
    {
        showObj = document.createElement('div');
        showObj.id = 'pop_div';
        document.body.appendChild(showObj);
    }
    showObj.className = divClassName;
    showObj.style.position = 'absolute';
	showObj.style.display = 'block';
	return showObj;
}

//----验证码--------------------------
var vcodeTimer =null;
var vcodeFormName = '';
var vcodeImgUrl = '';
function showVCode(objx,imgUrl)
{
    vcodeImgUrl = imgUrl;
    if(objx!=null)
    {
        vcodeFormName = objx.name;
    }
    var showObj = document.getElementById('pop_div_'+vcodeFormName);
    if(!showObj)
    {
        showObj = document.createElement('div');
        showObj.id = 'pop_div_'+vcodeFormName;
        document.body.appendChild(showObj);
        showObj.innerHTML = '<img src="'+imgUrl+'" title="看不清，点击刷新。" style="cursor:pointer;border:0px" onclick="refreshImg(this,vcodeFormName);" />';
    }
    showObj.className = 'vcodeClass';
    showObj.style.position = 'absolute';
    showObj.style.display = 'block';
    showObj.style.left = getPosX(objx) +'px';
    showObj.style.top = getPosY(objx)-showObj.offsetHeight-5 +'px';
    
}
function delayHidden(time)
{
    vcodeTimer = setTimeout(function(){hidePopUp('pop_div_'+vcodeFormName);},time);
}
function setFocus(tbName)
{
    clearTimeout(vcodeTimer);
    document.getElementsByName(tbName)[0].focus();
}
function refreshImg(imgObj,tbValidateName)
{
    setFocus(tbValidateName);
    imgObj.src= vcodeImgUrl+'&'+Math.random();
}



///AJAX
function Ajax () {};
Ajax.req = null;
// set callback
Ajax.callback = function (str) {alert(str)};
// listener
Ajax.listener = function () {
    if (Ajax.req.readyState == 4) {
        if (Ajax.req.status == 200) {
            // right
            Ajax.callback(Ajax.req.responseText);
        }
        else {
            // error
            Ajax.callback('错误！');
        }
    }
}
Ajax.init = function () {
    if (window.XMLHttpRequest) {
        Ajax.req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        // isIE = true;
        try {
            Ajax.req = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            try {
                Ajax.req = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(e) {
                Ajax.req = false;
            }
        }
    }
    if (Ajax.req) {
        Ajax.req.onreadystatechange = Ajax.listener;
    }
}
// send 
Ajax.send = function (url, content) {
    Ajax.req.open("post", url, true);
    Ajax.req.setRequestHeader('Connection', 'close');
    Ajax.req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    Ajax.req.send(content);
}
function checkAjax (str,type,func,pagePath) 
{
    Ajax.callback = func;
    Ajax.init();
    if(pagePath==null)
    {
        Ajax.send('Ajax.aspx', 'str=' + str + '&type='+type);
    }
    else
    {
        Ajax.send(pagePath, 'str=' + str + '&type='+type);
    }
}


