﻿
var FireFox = (document.layers) ? true : false;

var IE = (document.all) ? true : false; 

var TransportProcess = null;

var scroller  = null;
var scrollbar = null;

 function correctPNG(img) // correctly handle PNG transparency in Win IE 5.5 or higher.       
 {       
     if (IE)
     {               
         var imgName = img.src.toUpperCase()       
         if (imgName.substring(imgName.length-3, imgName.length) == "PNG")       
         {       
             var imgID = (img.id) ? "id='" + img.id + "' " : ""      
             var imgClass = (img.className) ? "class='" + img.className + "' " : ""      
             var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "      
             var imgStyle = "display:inline-block;" + img.style.cssText       
             var imgAttribs = img.attributes;       
             for (var j=0; j<imgAttribs.length; j++)       
             {       
                 var imgAttrib = imgAttribs[j];       
                 if (imgAttrib.nodeName == "align")       
                 {       
                     if (imgAttrib.nodeValue == "left") imgStyle = "float:left;" + imgStyle       
                     if (imgAttrib.nodeValue == "right") imgStyle = "float:right;" + imgStyle       
                     break      
                 }       
             }       
             var strNewHTML = "<span " + imgID + imgClass + imgTitle       
             strNewHTML += " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"      
             strNewHTML += "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"      
             strNewHTML += "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"      
             img.outerHTML = strNewHTML  

         }       
     }       
 }  

function scrollbarEvent (o, type) {
//	if (type == "mousedown") {
//		if (o.className == "Scrollbar-Track") o.style.backgroundColor = "#E3E3E3";
//		else o.style.backgroundColor = "#BBB";
//	} else {
//		if (o.className == "Scrollbar-Track") o.style.backgroundColor = "#EEE";
//		else o.style.backgroundColor = "#CCC";
//	}
}

//异步请求成功后 处理返回
function OnSuccess(result,context) {
        
    EndWebRequest();
   
    if (result.trim() == "")
    {
        if (context == null)
        return ;
        
        var obj =  $get(context);
        
        if (obj != null)        
            obj.innerHTML = "";
               
        return;
    }
   
    result = result.replace("&lt;","<");
    result = result.replace("&gt;",">");

    //判断是否返回结果中是否包括程序异常信息
    if (OccuredException(result) == true)
    {
        ////为了抛异常时刷新区域块，测试中
        if (result.indexOf("<script language") != -1)  //判断返回结果中是否包含需要执行的JS代码
        {
            ParseResult(context,result);
        }        
        ////
        result = CatchException(result);
        
        result+="<div style='text-align:center;'><span class='MallButton1 Hand' onclick='CloseWindow();'>关闭</span></div>";
        
        PopupErrorMessage(result);
    }
    else  //没有抛出异常
    {
        
        if (context == null)
            return;
            
        // 将生成的HTML填充这个div位置    
        var obj =  $get(context);    
                      
        if (obj == null)
            return ;
        //清空原来内容
        obj.innerHTML = "";

        if (result.indexOf("<form") != -1)                                   
            result = result.replace(/<\/?form([^<]*?)>/ig,"");                            

        if (result.indexOf('<input type="submit"') != -1)            
            result = result.replace('<input type="submit"','<input type="button"');       
        
        if (result.indexOf("<script language") != -1)  //判断返回结果中是否包含需要执行的JS代码
        {
            ParseResult(context,result);
        }
        else
       {                                                       
            obj.innerHTML = result;
                                     
        }              
    }
    
    if ( context == "WindowDIV")
    {
        var topCenter,middleCenter,bottomCeneter;
        topCenter = $get("TopCenter");
        middleCenter = $get("WindowDIV");
        bottomCeneter = $get("BottomCenter");
        
        if(topCenter != null)
            topCenter.style.width = topCenter.style.width;
        
        if(bottomCeneter != null)
            bottomCeneter.style.width = topCenter.style.width;
    }
    
    var ucObj =$get('ucArea'); 
    
    if( ucObj != null)
    {
        if(ucObj.onresize)
            ucObj.onresize();
    }
}


function SetScroller(obj,width,height)
{        
    if(scrollbar == null)
    {
        scroller  = new jsScroller(obj, width, height);
        scrollbar = new jsScrollbar (document.getElementById("Scrollbar-Container"), scroller, true, scrollbarEvent);            
    }
    else
    {
        var top = parseInt(document.getElementById("scrollbarHandle").style.top);
        
        scrollbar.swapContent(obj);
        
        if(top > 0)
            scrollbar.scrollTo(0, top);
        
    }
}

function PopupErrorMessage(msg)  //弹出错误信息提示框
{
    OpenCustomWindow(null,'WindowDIV',300,100,'');
    $get("WindowDIV").innerHTML = msg;
}

function getScrollTop()
{
    var scrollTop=0;
    if(document.documentElement&&document.documentElement.scrollTop)
    {
        scrollTop=document.documentElement.scrollTop;
    }
    else if(document.body)
    {
        scrollTop=document.body.scrollTop;
    }
    return scrollTop;
}

function OnSuccessAndSaveHistory(result,context)
{
    OnSuccess(result,context);       
}


function OnSuccessThenEvalresult(result) { 
    eval(result);
    EndWebRequest();
}

function OnSuccessThenEval(result,context) { 
    
    if (OccuredException(result) == true)
    {        
        result = CatchException(result);        
        PopupErrorMessage(result);
    }
    else if (context != "")
        eval(context);
    EndWebRequest();
}

//异步请求成功后 调用指定方法
function OnSuccessThenCloseWindowAndEvalJS(result,context) { 
    CloseWindow('WindowDIV');
    eval(context);
    EndWebRequest();
}

//异步请求失败 处理
function OnFailure(error) {
  
    EndWebRequest();
    // 如果失败了，要做点什么。比如重试，等
//    OpenCustomWindow(null,'WindowDIV',300,200,'');
//    $get("WindowDIV").innerHTML = error.get_message();
 
}

//异步请求时显示loader
function BeginWebRequest()
{    
    //CloseWindow();   
    
    BeginWebRequestWin();
}

//异步请求时显示loader
function BeginWebRequestWin()
{
    if (TransportProcess != null)
        window.clearInterval(TransportProcess);
        
    setProcessing(true);
}

//异步请求成功返回时隐藏loader
function EndWebRequest()
{
    setProcessing(false);
}


//控制loader显示及隐藏
function setProcessing(isProcessing)
{
    if($get('AsyncPostBackProgress'))
        $get('AsyncPostBackProgress').style.display = isProcessing ?"block" :"none";
}


//检查返回结果中是否有程序异常
function OccuredException(result)
{
    if (result == "")
        return false;
        
    var xmlStr = "<Root> \n"+ result +" \n </Root>";
    var xml = XPath.parseXML(xmlStr);  
    if (xml != null)
    {                      
        var exList = XPath.selectSingleNode(xml,"ExceptionList");
        
        if (exList != null)
        {
            var exCount = exList.getAttribute("Count");
            if ( exCount > 0)
                return true;
        } 
    }
    return false;
}

function CatchException(result)
{
    var str = "<div class='ExceptionMsg'><div>错误信息：</div>";
    
    var xmlStr = "<Root> \n"+ result +" \n </Root>";
    
    var xml = XPath.parseXML(xmlStr);  
        
    if (xml != null)
    {       
        var ex = XPath.selectNodes(xml,"ExceptionList","Exception");
        if (ex != null)
        {
            for (var i=0;i<ex.length;i++)
                str += "<div>"+ (i+1)+" . "+XPath.getNodeValue(ex[i])  +"</div>";
        }
    }
    
    str +="</div>";
    
    return str;
}


function $GET(obj)
{
    return document.getElementById(obj);
}

//显示或隐藏DIV
function ShowORHideDIV(obj)
{
    var div = $get(obj);
   
    if (div != null)
    {
        div.style.display = (div.style.display == "none" || div.style.display == "") ? "block" :"none";
    }
        
}

function ShowDIV(obj)
{
    var div = $GET(obj);
   
   if (div != null)
        div.style.display = "block";        
}



function HideDIV(obj)
{
    var div = $get(obj);
   
   if (div != null)
        div.style.display = "none";

}

//是否可见
function IsVisible(obj)
{
    var div = $get(obj);
   
   return div.style.display == "block";
   
}

//显示或隐藏DIV
function ShowORHideDIVAndChangeText(obj,myself,trueText,falseText)
{
    var div = $get(obj);
          
   if (div != null)
   {
        div.style.display = (div.style.display == "none" || div.style.display == "") ? "block" :"none";
        myself.innerHTML = (div.style.display == "none" || div.style.display == "") ? falseText: trueText;
   }
   
}

//高度显示选中项 
function SelectMenuItem(index,count,objName,class1,class2)
{   
    for(var i=1;i<=count;i++)
    {
        var obj = $get(objName+i);
              
        obj.className = i == index ? class1 : class2;                   
    }
}

//高度显示选中项 
function SelectMenuItemForServerControl(index,count,objID,class1,class2)
{   
    //ctl00_rptTagList_ctl00_TagListMenu
    
    //ctl00_rptTagList_ctl01_TagListMenu
    
    for(var i=0;i<=count;i++)
    {
        var obj = $get(objID.replace("_ctl0"+index+"_","_ctl0"+i+"_"));
              
        obj.className = i == index ? class1 : class2;
    }
}

//获取加载条
function GetLoader()
{        
    //return "<div style='width:100px;height:100px;margin:30% auto;'><img src='/Images/Main/ajax-loader2.gif' border='0' /></div>";
    return "<div style='width:100px;height:100px;margin:auto auto;'><img src='/Images/Main/ajax-loader2.gif' border='0' /></div>";
}

function HighlightRouteItem(dom)
{
    dom.style.height = "98px";
}

function NoneHighlightRouteItem(dom)
{
    dom.style.height = "22px";
}

//格式化参数
function FormatParameter(parameter)
{
    return parameter.toString().replace(/,/g,"$");
}

//解析被格式化的参数
function ParseParameter(parameter)
{
    return parameter.toString().replace(/$/g,",");    
}


function GetRadioValue(radio)
{
    
    var obj = document.getElementsByName(radio);
     
    for (var i=0;i<obj.length;i++)
    {        
         if (obj[i].checked == true)
        {                                   
            return obj[i].value;
        }
    }
}

function GetCheckBoxValue(checkBox)
{
    
    var result = '';
    
    var objCheckBox;
    
    var obj = document.getElementById(checkBox);
    
    for (var i=0;i<obj.rows.length;i++)
    {        
        objCheckBox = document.getElementById(checkBox+"_" +i); 
        
        if (objCheckBox.checked == true)
        {                                   
            result += objCheckBox.value+",";
        }
    }
    
    result = result.substring(0,result.lastIndexOf(","));

    return result;
}

function GetCheckBoxValueByName(checkBox)
{
    
    var result = '';
    
    var obj = document.getElementsByName(checkBox);
    
    for (var i=0;i<obj.length;i++)
    {        
        
        if (obj[i].checked == true)
        {                                   
            result += obj[i].value+"$";
        }
    }
    
    result = result.substring(0,result.lastIndexOf("$"));

    return result;
}

function SelectCheckBox(obj,selValue,hideControl)
{            
    if (obj == null)
        return ;
        
    var selList = $get(hideControl);
                                          
    var isChecked = obj.checked;
          
    var item = selValue+",";
                              
    if (isChecked == true)
    {    
        if (selList.value.indexOf(item) == -1)
         
            selList.value += item;        
    }
    else
    {        
        selList.value = selList.value.replace(item,"");                  
    }                        
}


function SelectAll(obj,container)
{        
    var el = document.getElementsByTagName('input');
        
    var len = el.length;
    
    for(var i=0; i<len; i++)    
    {         
        if(el[i].type=="checkbox" && el[i].id.indexOf(container) > -1)
            el[i].checked = obj.checked;
    }
}

function GetCheckBoxValues(container)
{        
    var ids = "";
    
    var el = document.getElementsByTagName('input');
        
    var len = el.length;
    
    for(var i=0; i<len; i++)    
    {         
        if(el[i].type=="checkbox" && el[i].id.indexOf(container) > -1)
        {
            if ( el[i].checked == true)
                ids+=el[i].value+"$";
        }            
    }
    
    return ids;
}

function SelectRadio(nameregex,obj,selValue,hideControl)
{            

    if (obj == null)
        return ;
        
    var re = new RegExp(nameregex);

    for(i = 0; i < document.forms[0].elements.length; i++)
    {
      elm = document.forms[0].elements[i];
      
      if (elm.type == 'radio')
      {
         if (re.test(elm.name))
         {
            elm.checked = false;
         }
      }
    }
    obj.checked = true;             
    
    var selList = $get(hideControl);
                                                    
    var item = selValue+",";
                              
    selList.value = item;
 
    
}

  
function getEvent() //同时兼容ie和ff的写法
    { 
        if(document.all)  return window.event;   
        func=getEvent.caller;       
        while(func!=null){ 
            var arg0=func.arguments[0];
            if(arg0)
            {
              if((arg0.constructor==Event || arg0.constructor ==MouseEvent) || (typeof(arg0)=="object" && arg0.preventDefault && arg0.stopPropagation))
              { 
              return arg0;
              }
            }
            func=func.caller;
        }
        return null;
    } 

function mouseX(evt) {
    if (evt.pageX) return evt.pageX;
    else if (evt.clientX)
       return evt.clientX + (document.documentElement.scrollLeft ?
       document.documentElement.scrollLeft :
       document.body.scrollLeft);
    else return 0;
}
function mouseY(evt) {
    if (evt.pageY) return evt.pageY;
    else if (evt.clientY)
       return evt.clientY + (document.documentElement.scrollTop ?
       document.documentElement.scrollTop :
       document.body.scrollTop);
    else return 0;
} 


   function HTMLEnCode(str)  
   {  
         var    s    =    "";  
         if    (str.length    ==    0)    return    "";  
         s    =    str.replace(/&/g,    "&gt;");  
         s    =    s.replace(/</g,        "&lt;");  
         s    =    s.replace(/>/g,        "&gt;");  
         s    =    s.replace(/ /g,        "&nbsp;");  
         s    =    s.replace(/\'/g,      "&#39;");  
         s    =    s.replace(/\"/g,      "&quot;");  
         s    =    s.replace(/\n/g,      "<br>");  
         return    s;  
   }  

   function    HTMLDeCode(str)  
   {  
         var    s    =    "";  
         if    (str.length    ==    0)    return    "";  
         s    =    str.replace(/&amp;/g,    "&");
         //s    =    s.replace(/&gt;/g,    "&"); 
         s    =    s.replace(/&lt;/g,        "<");  
         s    =    s.replace(/&gt;/g,        ">");  
         s    =    s.replace(/&nbsp;/g,        " ");  
         s    =    s.replace(/&#39;/g,      "\'");  
         s    =    s.replace(/&quot;/g,      "\"");  
         s    =    s.replace(/<br>/g,      "\n");  
         return    s;  
   }
   
   function ToFriendlyText(str)
   {
        return FormatParameter(HTMLEnCode(str));
   }
   
function ParseResult( containerId, htmlContent ) 
{ 
    var oDiv = document.getElementById( containerId );
     
    while( oDiv.childNodes.length > 0 ) 
    { 
        oDiv.removeChild( oDiv.firstChild ); 
    } 

    if( window.ActiveXObject) 
    { 
       // 在html代码开头添加一段空白代码，主要是为了解决script放在开头而被IE忽略的问题 
        htmlContent = '<span style="display: none">&nbsp;</span>' + htmlContent; 
       // 去掉html代码中script tag的defer属性，防止函数被执行两次 
        htmlContent = htmlContent .replace(/<script(.*)defer([^\s|^>]*)([^>]*)>/gi,'<script$1$3>'); 
    }	

    try 
    { 
        // 先将新的节点append到DOM中 
        var container = document.createElement( "div" ); 
       container.innerHTML = htmlContent; 
       oDiv.appendChild( container ); 
    } 
    catch ( error ) 
    { 
        // ignore exception 
    } 

    var scripts = container.getElementsByTagName( "script" ); 
    for( var i = 0; i < scripts.length; i++ ) 
    { 
        if( scripts[i].src ) 
        {	
            // 添加script src到head中 
              if( BrowserUtility.isIE( ) ) 
            { 
                var scriptObj = document.createElement( "script" ); 
                scriptObj.setAttribute( "type", "text/javascript" ); 
                scriptObj.setAttribute( "src", scripts[i].src ); 

                var head = document.getElementsByTagName( "head" )[0]; 
                if( head ) 
                    head.appendChild( scriptObj ); 
            }	
        } 
        else if ( scripts[i].innerHTML ) 
        {	    
             //  IE支持该方法去动态执行javascript代码 
             if ( window.execScript ) 
               window.execScript( scripts[i].innerHTML ); 
        } 
    }
    
    // 如果是safari或是KHTML浏览器，需要将html代码中包含的css style代码显示的append到head中。 
//    if ( BrowserUtility.isSafari() || BrowserUtility.isKHTML() ) 
//    { 
//        var styles = container.getElementsByTagName("style"); 
//        for ( var i = 0; i < styles.length; i++ ) 
//        { 
//            var styleContent = styles[i].innerHTML; 
//            if ( styleContent ) 
//            { 
//                var element = document.createElement("style"); 
//                element.type = "text/css"; 
//                if ( element.styleSheet ) 
//                { 
//                    element.styleSheet.cssText = styleContent; 
//                } 
//                else 
//                { 
//                    element.appendChild( document.createTextNode( styleContent ) ); 
//                } 

//                var head = document.getElementsByTagName( "head" )[0];	
//                if( head ) 
//                { 
//                    head.appendChild( element ); 
//                }        
//            } 
//        } 
//    }        
}         

//创建遮罩层
	function CreateCoverLayer(){

	   /**创建DIV遮罩层。*/
		var sWidth,sHeight;
		sWidth = window.screen.availWidth-16;
		//屏幕可用工作区高度： window.screen.availHeight;
		//屏幕可用工作区宽度： window.screen.availWidth;
		//网页正文全文宽：     document.body.scrollWidth;
		//网页正文全文高：     document.body.scrollHeight;
		if(window.screen.availHeight > document.body.scrollHeight){  //当高度少于一屏
			sHeight = window.screen.availHeight;
		}else{//当高度大于一屏
			sHeight = document.body.scrollHeight;   
		}
		//创建遮罩背景
		var maskObj = document.createElement("div");
		maskObj.setAttribute('id','BigDiv');
		maskObj.style.position = "absolute";
		maskObj.style.top = "0";
		maskObj.style.left = "0";
		maskObj.style.background = "#777";
		maskObj.style.filter = "Alpha(opacity=30);";
		maskObj.style.opacity = "0.3";
		maskObj.style.width = sWidth + "px";
		maskObj.style.height = sHeight + "px";
		maskObj.style.zIndex = "997";		
		document.body.appendChild(maskObj);
	}

		/**关闭窗口和遮罩层。*/
	function RemoveCoverLayer(){
		var Bigdiv = document.getElementById("BigDiv");
		if(Bigdiv != null)
		    document.body.removeChild(Bigdiv); 
	}
	

//var prm = Sys.WebForms.PageRequestManager.getInstance();
//prm.add_beginRequest(onBeginRequest);
//prm.add_pageLoaded(onPageLoaded);

//var request = new Sys.Net.WebRequest();


//异步请求时显示loader
function onBeginRequest(sender,args)
{
    setProcessing(true);
}

//加载完成时隐藏loader
function onPageLoaded(sender,args)
{
    setProcessing(false);
}	



var expendEntry = null;

function EntryClick(objID,id)
{
    var item = $get(objID);
                   
    Shrink(expendEntry);
    
    if(expendEntry != objID)
    {
        Expend(item,id);
        expendEntry = objID;
    }
    else
        expendEntry = null;

}

function Expend(item,id)
{        
    item.className ="Entry InFront Expend";
    
    var append = document.createElement("div");
    
    append.id = "appendBody_"+id;
    
    item.appendChild(append);
    
    GetHTML('WarBulletin/ReadArticle','ObjID||'+ id +'',append.id);
}

function Shrink(objID)
{
    if(objID == null)
        return;
        
    var item = $get(objID);
    
    var childNode = null;
    
    for(var i=0;i<item.childNodes.length;i++)
    {
        if(item.childNodes[i].id !=undefined)
        {
            if(item.childNodes[i].id.indexOf("appendBody_") > -1)
            {
                childNode = item.childNodes[i];
                break;
            }
        }
    }
    
    //var childNode =item.childNodes[1];
    
    if(childNode != null)
    {
        item.removeChild(childNode);
        item.className ="Entry InFront Shrink";
    }    
}

var currentSelectHomeMenu = null;

function SelectHomeMenu(menu)
{
    if(currentSelectHomeMenu != null)
    {
        currentSelectHomeMenu.onmouseover="this.className='"+ currentSelectHomeMenu.id +"_HighLight';";
        currentSelectHomeMenu.onmouseout="this.className='"+ currentSelectHomeMenu.id +"';"
    }
    
    menu.className = "'"+menu.id +"_HighLight';";
    
    menu.onmouseover= null;
    menu.onmouseover= null;
    
    currentSelectHomeMenu = menu;
}

function isNUM(e)
{
    var iKeyCode = window.event?e.keyCode:e.which;
    if(!( (iKeyCode >= 48 && iKeyCode <= 57) || iKeyCode == 8))
    {  
        if (IE)
        {
            e.returnValue=false;
        }
        else
        {
            e.preventDefault();   //重要 firefox是用prevenDefault()来取消事件的
        }
    }
}

function GetCountryNameByID(countryID)
{
    var countryName = "";
    switch(countryID) {
    case 1:
        countryName = "联邦";
        break;
    case 2:
        countryName = "地典";
    	break;
    case 4:
        countryName = "羽山";
    	break;
    case 8:
        countryName = "风后";
        break;
    }
    
    return countryName;
}

function UpdateCharacterMoney()
{
    GetHTML('Character/Character','','characterInfoDiv');
}


function ShowTheContent(name,idx,num)  //显示当前选中菜单项正文
{
    for(var i=1;i<=num;i++)
    {
        var menuItem = $get(name+'Menu'+i);
        var contentItem = $get(name+'Content'+i);
        
        if(menuItem != null)
        {
            menuItem.idx = i;
            var js="";
            if(idx == i)
            {
                menuItem.className = "Menu"+i+"_Sel";
                menuItem.onmouseover = null;
                menuItem.onmouseout = null;
            }
            else
            {
                menuItem.className = "Menu"+i;
                menuItem.onmouseover = function(){ this.className='Menu'+ this.idx +'_Sel';};
                menuItem.onmouseout = function(){ this.className='Menu'+ this.idx;};
            }
        }
        
        if(contentItem != null)
        {
            if(idx == i)
                contentItem.style.display = 'block';
            else
                contentItem.style.display = 'none';
        }
    }
}

function BindEvent(obj,eventName,js)
{
    if(IE)
        obj.setAttribute(eventName,function(){js});
    else
        obj.setAttribute(eventName,js);
}
