
/// <reference path="Extjs_Intellisense.js" /> 
/// <reference path="src/data/XmlStore.js" /> 

//      取得对象   
function $(id)
{
	try
	{
		var obj=null;
		if(document.getElementById)
		{
			obj=document.getElementById(id);
		}else if(document.all)
		{
			obj=document.all[id];
		}else if(document.layers)
		{
			obj=document.layers[id];
		}else if(window.getElementById)
		{
			obj=window.getElementById(id);
		}
		if(obj==null || obj==undefined)
		{
			if(document.getElementsByName)
			{
				obj=document.getElementsByName(id);
				if(obj.length>=1)
				{
					obj=obj[0];
				}
			}
		}
		if(obj==null || obj.getAttribute==undefined)
		{
		    obj=undefined;
		} 
		return obj;
	}catch(e){}
	return undefined;
}


function GetPlayerById(playerid)
{
    var obj=top.$(playerid);
    if(obj && obj.width && obj.width>0)
    {
        return obj;
    }    
    if(obj && obj.tagName!="")
    {
        return obj;
    }
    try
    {
        var tempobj=top.playerlist.$(playerid);
        if(tempobj && tempobj.width && tempobj.width>0)
        {
            return tempobj;
        }
        if(tempobj && tempobj.tagName!="")
        {
            return tempobj;
        }
    }catch(eee){}
    
    return obj;
}


//test1,192.168.88.132,20001,MA==,true,false,1,devid
function SaveConfigToFile(idx,str)
{
    try
    {
         var play=GetPlayerById("player1");
         var userdata=play.GetUserData(__configfilefullpath);         
        if(userdata=="" || userdata==undefined)
        {
            userdata=play.GetUserData(__configfilefullpath2);
        }
         userdata=""+userdata;
         userdata=userdata.replace(/[\r\n]+/g,"\r\n");
         var usls=userdata.split("\r\n"); 
         var temp=base64decode(usls[0]);
         var ls=temp.split(itemsplitobj);
         ls[idx]=str;
         var writemsg=ls.join(itemsplitobj);
         usls[0]=base64encode(writemsg);
         writemsg=usls.join("\r\n");
         writemsg=writemsg.replace(/[\r\n]+/g,"\r\n");
         var userdata=play.SaveUserData(__configfilefullpath,writemsg);
         if(userdata!=="0" && userdata!==0)
         {
            userdata=play.SaveUserData(__configfilefullpath2,writemsg);
         }
     }catch(eee){}
}

//返回保存的Idx位置
function SaveConfigData(msg)
{
    try
    {
         var nowusermsgls=msg.split(itemsplitobj);
         var play=GetPlayerById("player1");
         var userdata=play.GetUserData(__configfilefullpath);
         if(userdata=="" || userdata==undefined)
         {
             userdata=play.GetUserData(__configfilefullpath2);
         }
         userdata=""+userdata;
         userdata=userdata.replace(/[\r\n]+/g,"\r\n");
         var usls=userdata.split("\r\n");
         var retidx=0;
         for(retidx=0;retidx<usls.length;retidx++)
         {
            var temp=usls[retidx];
            try
            {
                temp=base64decode(usls[retidx]);
            }catch(eee){}
            var usmsgls=temp.split(itemsplitobj);
            if(nowusermsgls[0]==usmsgls[0])
            {//找到保存位置
                usls[retidx]="";
                break;
            }
         }
         if(usls.length>4)
         {
            for(var idx=4;idx<usls.length;idx++)
            {
                usls[idx]="";
            }
         }
         var writemsg=base64encode(msg)+"\r\n"+usls.join("\r\n");
         writemsg=writemsg.replace(/[\r\n]+/g,"\r\n");
         var userdata=play.SaveUserData(__configfilefullpath,writemsg);
         if(userdata!=="0" || userdata!==0)
         {
             userdata=play.SaveUserData(__configfilefullpath2,writemsg);
         }
     }catch(eee){}
}


function StopAudioByDeviceId(devid)
{
    if(devid==null || devid=="")
    {
        return false;
    }
    for(var idx=1;idx<=Max_play_Count;idx++)
    {
        try
        {
            var nowplayid="player"+idx;
            if(devid==playobjDevIdList[nowplayid])
            {
                GetPlayerById(nowplayid).SetAudioOn(0);
                main_but_stopsound.setText(GetDefaultLang("lang_playsound","播放声音"));
            }
        }catch(eee){}
    }
}




 var isMoz = false;
 function XmlDom()
 {
    //通过对象/属性检测法，判断是IE来是Mozilla
    if (window.ActiveXObject)
    {
        var arrSignatures = ["MSXML2.DOMDocument.6.0","MSXML2.DOMDocument.5.0", "MSXML2.DOMDocument.4.0",
                             "MSXML2.DOMDocument.3.0", "MSXML2.DOMDocument","Microsoft.XmlDom"];
        for (var i=0; i < arrSignatures.length; i++)
        {
            try
            {
                var oXmlDom = new ActiveXObject(arrSignatures[i]);
                return oXmlDom;
            }catch (oError) 
            {
            }
        }
        return null;
    } 
    else if (document.implementation && document.implementation.createDocument) 
    {   
        var oXmlDom = document.implementation.createDocument("","",null);
        //创建Mozilla版本的parseError对象
        oXmlDom.parseError = 
        {
            valueOf: function () { return this.errorCode; },
            toString: function () { return this.errorCode.toString() }
        };        
        //初始化parseError对象
        oXmlDom.__initError__();        
        oXmlDom.addEventListener("load", function () {
            this.__checkForErrors__();
            this.__changeReadyState__(4);
        }, false);
        isMoz = true;
        return oXmlDom;
    }else 
    {
        return null;
    }
}




//此处用到了该书中一个浏览器系统检测js文件，如果是Mozilla
if (isMoz) 
{
    Document.prototype.readyState = 0;
    Document.prototype.onreadystatechange = null;    
    Document.prototype.__changeReadyState__ = function (iReadyState)
    {
        this.readyState = iReadyState;
        if (typeof this.onreadystatechange == "function")
        {
            this.onreadystatechange();
        }
    };
    //初始化parseError对象
    Document.prototype.__initError__ = function ()
    {
        this.parseError.errorCode = 0;
        this.parseError.filepos = -1;
        this.parseError.line = -1;
        this.parseError.linepos = -1;
        this.parseError.reason = null;
        this.parseError.srcText = null;
        this.parseError.url = null;
    };
    
    Document.prototype.__checkForErrors__ = function ()
    {
        if (this.documentElement.tagName == "parsererror")
        {
            var reError = />([\s\S]*?)Location:([\s\S]*?)Line Number (\d+), Column (\d+):<sourcetext>([\s\S]*?)(?:\-*\^)/;
            reError.test(this.xml);
            this.parseError.errorCode = -999999;
            this.parseError.reason = RegExp.$1;
            this.parseError.url = RegExp.$2;
            this.parseError.line = parseInt(RegExp.$3);
            this.parseError.linepos = parseInt(RegExp.$4);
            this.parseError.srcText = RegExp.$5;
        }
    };
    
     //定义Mozilla的loadXML方法   
    Document.prototype.loadXML = function (sXml)
    {
        this.__initError__();
        this.__changeReadyState__(1);
        var oParser = new DOMParser();
        var oXmlDom = oParser.parseFromString(sXml, "text/xml");
        //删除原文档内容
        while (this.firstChild)
        {
            this.removeChild(this.firstChild);
        }
        //导入新的文档内容
        for (var i=0; i < oXmlDom.childNodes.length; i++)
        {
            var oNewNode = this.importNode(oXmlDom.childNodes[i], true);
            this.appendChild(oNewNode);
        }
        //载入后检查错误
        this.__checkForErrors__();        
        //没有问题，设置readyState属性为4
        this.__changeReadyState__(4);

    };
    
    Document.prototype.__load__ = Document.prototype.load;
    Document.prototype.load = function (sURL)
    {
        this.__initError__();
        this.__changeReadyState__(1);
        this.__load__(sURL);
    };    
    Node.prototype.__defineGetter__("xml", function ()
    {
        var oSerializer = new XMLSerializer();
        return oSerializer.serializeToString(this, "text/xml");
    });
}



 

__LastXmlDoc=null;
//从字符串加载

function CreateXmlDom(strXml)
{
    var oXmlDom = new XmlDom();
    oXmlDom.async = false;
    oXmlDom.loadXML(strXml);
    return oXmlDom;
}

function CreateXml(strXml)
{
    var oXmlDom = CreateXmlDom(strXml);
    var docElem = oXmlDom.documentElement; //strXml
    __LastXmlDoc=docElem;
    return docElem;
}



//从文件加载
function GetXmlDom(filePath)
{
    var oXmlDom = new XmlDom();
    oXmlDom.async = false;
    oXmlDom.resolveExternals = false;
    var nsucc = oXmlDom.load(filePath);
    if(!nsucc)      return null;
    var docElem = oXmlDom.documentElement; //strXml 设备列表的xml
    __LastXmlDoc=docElem;
    return docElem;
}

 //取得Doc中xpath的namevalue对应的第id个
function GetValue(xmldoc,xPath,namevalue,id)
{
    try
    {
		if(xmldoc==null)
		{
			xmldoc=__LastXmlDoc;
		}
        if(id==undefined)
        {
            id=0;
        }
	    var obj=xmldoc.getElementsByTagName(xPath);
	    if(obj)
	    {
	        if(id>=obj.length)
	        {
	            id=obj.length-1;
	        }
	        if(id<0)
	        {
	            id=0;
	        }
	        return obj[id].getAttribute(namevalue);
	    }
	}catch(eee){}
	return null;
}


//取得xmldoc中xpath位置的第idx个节点
function GetObjByName(xmldoc,xPath,idx)
{
    try
    {
		if(xmldoc==null)
		{
			xmldoc=__LastXmlDoc;
		}
		if(idx==null || idx==undefined)
		{
			idx=0;
		}
	    var obj=xmldoc.getElementsByTagName(xPath);
	    if(obj)
	    {
			if(idx>=obj.length)
			{
				return null;
			}else
			{
	        	return obj[idx];
			}
	    }
	}catch(eee){}
	return null;
}

function CreateXmlByStr(str)
{
	__LastXmlDoc= CreateXml(str);
	return __LastXmlDoc;
}


function WriteWebLog(str)
{
    var player=GetPlayerById("player1");
    try
    {
        player.WriteWebLog("网页:"+str);
    }catch(ee){}
}





function SetEnabled(strls,isenable,cssclassname)
{
    var ls=strls.split(",");
    var idx;
    for(idx=0;idx<ls.length;idx++)
    {
        var obj=$(ls[idx]);
        try
        {
            obj.disabled=!isenable;
            if(cssclassname!="")
            {
                obj.className=cssclassname;
            }
        }catch(eee){}
    }
    return true;
}




function GetIpFromUrl()
{

    try
    {
        var urlstr="" + top.location;
        urlstr=urlstr.replace("http://","");
        urlstr=urlstr.replace("http:\\","");
        urlstr=urlstr.replace("https://","");
        urlstr=urlstr.replace("https:\\","");
        urlstr=urlstr.replace(/[/]+/g,"\\");
        var ls=urlstr.split("\\");
        var ipport=ls[0].split(":");
        return ipport[0];
    }catch(eeee){}
    return "notserverip";
}