﻿/*
Note:
    FileName:       MyIEOS.js
    Component Name: MyIEOS, MyIEOS.Net, MyIEOS.Function, MyIEOS.NameSpace, MyIEOS.Net, MyIEOS.Component
    Version:        1.0
    Create Date:    2007-10-16
    Author:         Guo, Wenjun
    Email:          iceser@gmail.com

Modify:
    Date:           2007-12-03
    Update:         MyIEOS.ErrManager

*/

var __MyIEOS_ComponentReferenceList = [];

function __MyIEOS_Main()
{
    if( typeof document.instanceID != "undefined" )
    {
        document.instance = window.parent.document.getElementById( document.instanceID );
    }
    if( typeof document.instance == "undefined" ) document.instance = null;
    
    var mainFunctionName = document.body.getAttribute( "main" );
    var windowClassName = document.body.getAttribute( "windowclass" )
    var dependComponents = document.body.getAttribute( "dependComponents" );
    
    if( dependComponents != null )
    {
        var dependComponentArray = dependComponents.split( "," );
        for( var i = 0; i < dependComponentArray.length; i ++ )
        {
            var componentName = dependComponentArray[i].replace( /\s/g, "" );
            __MyIEOS_ComponentReferenceList.push( componentName );
            MyIEOS.Component.Import( window.location.href, componentName );
        }
    }
    
    if( document.instance != null )
    {
        if( windowClassName != null )
        {
            try
            {
                if( eval( "typeof " + windowClassName ) != "undefined" )
                {
                    eval( "document.instance.__Inherit( " + windowClassName + " )" );
                }
            }
            catch( ex )
            {
                alert( "Bind base class failed.(" + ex.message + ")" );    
            }    
        }
        document.instance.AttachClientEvent( document );
   }
    
    var invokeString = "";
    if( mainFunctionName == null )
    {
        if( typeof main == "function" )
        {
            invokeString += "main( ";
        }
    }
    else
    {
        invokeString += mainFunctionName + "(";
    }
    
    if( invokeString != "" && document.instance != null )
    {
        if( document.instance.__MainArgs.length > 0 )
        {
            invokeString += " document.instance.__MainArgs[0]";
            for( var i = 1; i < document.instance.__MainArgs.length; i ++ )
            {
                invokeString += ", document.instance.__MainArgs[" + i + "]";
            }
        }
    }
    
    if( invokeString != "" )
    {
        invokeString += " )";
        try{ eval( invokeString ); }
        catch( ex ){ alert( ex.message ) };
    }

    document.configRoot = null;
    var confirXmlUrl = document.body.getAttribute( "configXml" );
    if( confirXmlUrl != null )
    {
        if( !MyIEOS.Net.AsyncLoadXML( confirXmlUrl, function ( xmlDoc )
        {
            var docRoot = xmlDoc.documentElement;
            document.configRoot = docRoot;
            if( docRoot.selectSingleNode( "repeater" ) != null )
            {
                try
                {
                    MyIEOS.Controls.Repeater.BindControls( document, docRoot );
                }
                catch( ex )
                {
                    alert( "bind repeaters failed: " + ex.description );
                }
            }
            if( docRoot.selectSingleNode( "dropDownList" ) != null )
            {
                try
                {
                    MyIEOS.Controls.DropDownList.BindControls( document, docRoot );
                }
                catch( ex )
                {
                    alert( "bind dropdownlists failed: " + ex.description );
                }
            }
            __MyIEOS_AfterMain();
            
        } ) )
        {
            alert( "load config xml failed." );
            __MyIEOS_AfterMain();
        }
    }
    else
    {
        __MyIEOS_AfterMain();
    }
    
    document.body.focus();

}

function __MyIEOS_AfterMain()
{
    if( typeof window.afterload == "function" )
    {
        try
        {
            window.afterload();
        }
        catch( ex )
        {
        alert(ex.message);
        }
    }
}

function __MyIEOS_Exit()
{
    if( typeof exit == "function" )
    {
        try
        {
            exit();
        }
        catch( ex )
        {
            
        }
    }
    while( __MyIEOS_ComponentReferenceList.length > 0 )
    {
        MyIEOS.Component.Remove( "__MyIEOS_Kernel", __MyIEOS_ComponentReferenceList.pop() );    
    }
    
}
        
if( typeof MyIEOS == "undefined" )
{
    //check parent, if parent has load MyIEOS, copy the reference
    if( this.parent != null && this.parent != this && typeof this.parent.MyIEOS != "undefined" )
    {
        MyIEOS = parent.MyIEOS;
    }
    else
    {
        //Load MyIEOS
        MyIEOS = {};
        MyIEOS.Name = "MyIEOS";
        MyIEOS.Version = 1.01213;

        //root path
        if( typeof __MyIEOS_RootPath == "undefined" )
        {
            __MyIEOS_RootPath = "";
        }
       
        MyIEOS.Function = {};
        MyIEOS.Function.None = function(){ return null; };
        MyIEOS.Function.Empty = function(){};
        

        MyIEOS.NameSpace = {};
        MyIEOS.NameSpace.IsReserved = function( nameSpace ){
            //if( typeof nameSpace != "string" || nameSpace == null ) return true;
            //if( nameSpace == "MyIEOS" || nameSpace.indexOf( "MyIEOS." ) == 0 ) return true;
            if( nameSpace.match( /^[a-zA-Z_]+[a-zA-Z0-9_\$\.]*[a-zA-Z0-9_\$]{1}$/ ) == null ) return true;
            //other reserved namespace filter
            return false;
        };
        MyIEOS.NameSpace.Register = function( nameSpace ){
            if( this.IsReserved( nameSpace ) ) return false;
            var subNameSpaceArray = nameSpace.split( "." );
            var curNameSpace = "";
            var parent = "";
            for( var i = 0; i < subNameSpaceArray.length; i ++ )
            {
                if( curNameSpace != "" ) curNameSpace += ".";
                curNameSpace += subNameSpaceArray[i];
                if( eval( "typeof " + curNameSpace ) == "undefined" || eval( curNameSpace ) == null )
                {
                    eval( curNameSpace + " = { type: \"NameSpace\", length: 0 };" );
                    if( parent != "" )
                    {
                        eval( parent + ".length ++;" );
                    }
                }
                parent = curNameSpace;
            }
            return true;
        };
        MyIEOS.NameSpace.Remove = function( nameSpace ){
            if( this.IsReserved( nameSpace ) ) return false;
            if( eval( "typeof " + nameSpace ) == "undefined" ) return false;
            var lastIndexOfPoint = nameSpace.lastIndexOf( "." );
            if( lastIndexOfPoint == -1 )
            {  
                eval( "delete window." + nameSpace + ";" );
            }
            else
            {
                eval( "delete " + nameSpace + ";" );
                var parent = nameSpace.substring( 0, lastIndexOfPoint );
                eval( parent + ".length --;" );
                nameSpace = parent;
                while ( ( lastIndexOfPoint = nameSpace.lastIndexOf( "." ) ) != -1 )
                {
                    if( eval( nameSpace + ".length" ) < 1 )
                    {
                        eval( "delete " + nameSpace + ";" );
                        parent = nameSpace.substring( 0, lastIndexOfPoint );
                        eval( parent + ".length --;" );
                    }
                    nameSpace = nameSpace.substring( 0, lastIndexOfPoint );
                }
                if( eval( nameSpace + ".length" ) < 1 )
                {
                    eval( "delete window." + nameSpace + ";" );
                }
                return true;
            }
        };
        
        //Net
        MyIEOS.Net = {};
        MyIEOS.Net.__Create = MyIEOS.Function.None;
        MyIEOS.Net.__ClientPool = new Array();
        MyIEOS.Net.Recycle = function( client ){
            this.__ClientPool.push( client );
        };
        MyIEOS.Net.Create = function(){
            if( this.__ClientPool.length > 0 )
            {
                var instance = this.__ClientPool.pop();
                instance.onreadystatechange = MyIEOS.Function.Empty;
                return instance;
            }
            return this.__Create();
        };

        if( __browerCategory == "ie" )
        {
            if( typeof ActiveXObject == 'function' )
            {
                var aVersions = [ "Microsoft.XMLHTTP", 
                    "MSXML.XMLHTTP",
                    "Microsoft.XMLHTTP",
                    "Msxml2.XMLHTTP.7.0",
                    "Msxml2.XMLHTTP.6.0",
                    "Msxml2.XMLHTTP.5.0",
                    "Msxml2.XMLHTTP.4.0",
                    "MSXML2.XMLHTTP.3.0",
                    "MSXML2.XMLHTTP" ]; 
                for( var i = 0; i < aVersions.length; i++ )
                {
                    try
                    {
                        var xmlHttp = new ActiveXObject( aVersions[i] );
                        if( xmlHttp != null )
                        {
                            MyIEOS.Net.Recycle( xmlHttp );
                            var activeXVersion = aVersions[i];
                            MyIEOS.Net.__Create = function(){ return new ActiveXObject( activeXVersion ); };
                            break;
                        }
                    }
                    catch( ex ){ }
                }
            }
        }
        else
        {
            try
            {
                var xmlHttp = new XMLHttpRequest();
                if( xmlHttp != null )
                {
                    MyIEOS.Net.Recycle( xmlHttp );
                    MyIEOS.Net.__Create = function(){   
                        return new XMLHttpRequest();
                    };
                };
            }
            catch( ex ){ }
        }
        
        
        MyIEOS.Net.ClientObject = function(){
            var instance = this; 
            var stateObj = null;
            this.xmlClient = MyIEOS.Net.Create();
            this.AsyncLoadData = function( url, callBack, userStateObj ){
                stateObj = userStateObj;
                try{
                    this.xmlClient.open( "GET", url, true );
                    this.xmlClient.onreadystatechange = function(){
                        if( instance.xmlClient.readyState == 4 )
                        {
                            try
                            { 
                                callBack( instance, stateObj );
                            }
                            catch( ex )
                            {
                                
                            }
                        }    
                    };
                    this.xmlClient.send( null );
                    return true;
                }
                catch( ex ){
                    return false;   
                }
            };
            this.Release = function(){
                MyIEOS.Net.Recycle( this.xmlClient );
            };
        };
        
        MyIEOS.Net.LoadText = function( url ){
            var client = this.Create();
            var text = null;
            try{ 
                client.open( "GET", url, false );
                client.onreadystatechange = MyIEOS.Function.Empty;
                client.send( null );
                text = client.responseText;
            }
            catch( ex ){
                text = ex.message;
            }
            this.Recycle( client );
            return text;        
        };

        MyIEOS.Net.AsyncLoadText = function( url, callBack, userStateObj ){
            var client = new MyIEOS.Net.ClientObject();
            var returnValue = client.AsyncLoadData( url, function( instance, stateObj ){
                try{ callBack( instance.xmlClient.responseText, stateObj ); }
                catch( ex ){ callBack( "Error:" + ex.message ); }
                instance.Release();
            }, userStateObj );
            return returnValue;
        };

        MyIEOS.Net.LoadXML = function( url ){
            var client = this.Create();
            var xml = null;
            try{ 
                client.open( "GET", url, false );
                client.onreadystatechange = MyIEOS.Function.Empty;
                client.send( null );
                xml = client.responseXML;
            }
            catch( ex ){
            }
            this.Recycle( client );
            return xml;        
        };

        MyIEOS.Net.AsyncLoadXML = function( url, callBack, userStateObj ){
            var client = new MyIEOS.Net.ClientObject();
            var returnValue = client.AsyncLoadData( url, function( instance, stateObj ){
                try{ callBack( instance.xmlClient.responseXML, instance.xmlClient.status, stateObj ); }
                catch( ex ){ callBack( "Error:" + ex.message ); }
                instance.Release();
            }, userStateObj );            
            return returnValue;
        };

        MyIEOS.Component = {};
        MyIEOS.Component.Base = function( componentName ){
            var __ComponentName = componentName.toString();
            var __dependComponentList = null;
            if( arguments.length > 1 )
            {
                __dependComponentList = new Array();
                for( var i = 1; i < arguments.length; i ++ )
                {
                    var dependComponentInfo = arguments[i].split( "," );
                    MyIEOS.Component.Import( __ComponentName, dependComponentInfo[0], dependComponentInfo[1] );
                    __dependComponentList[ __dependComponentList.length ] = dependComponentInfo[0];
                }
            }
                    
            var __libRefenceLink = {};
            this.AddReference = function( clientName ){
                var counterObj = __libRefenceLink[ clientName ];
                if( counterObj == null )
                {
                    //None Reference
                    counterObj = {};
                    counterObj.count = 1;
                    __libRefenceLink[ clientName ] = counterObj;
                }
                else
                {
                    counterObj.count ++;
                }
                return true;
            };
            this.RemoveReference = function( clientName ){
                var counterObj = __libRefenceLink[ clientName ];
                if( counterObj == null )
                {
                    //None Reference
                    return false;
                }
                else
                {
                    counterObj.count --;
                    if( counterObj.count < 1 )
                    {
                        //Remove Component
                        eval( componentName + ".Release()" );
                        var lastIndexOfPoint = componentName.lastIndexOf( "." );
                        if( lastIndexOfPoint == -1 )
                        {
                            eval( "delete window." + componentName + ";" );
                        }
                        else
                        {
                            eval( "delete " +componentName + ";" );
                        }
                    }
                }
            };
            this.GetComponentName = function(){
                return __ComponentName;
            };
            this.Release = function(){
                if( __dependComponentList != null )
                {
                    for( var i = 0; i < __dependComponentList.length; i ++ )
                    {
                        MyIEOS.Component.Remove( this.GetComponentName(), __dependComponentList[i] );
                    }
                    __dependComponentList = null;
                }    
            };
        };
        
        MyIEOS.Component.Check = function( componentName ){
            try
            {
                if( eval( "typeof " + componentName ) != "undefined" )
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch( ex )
            {
                return false;
            };
        };
        MyIEOS.Component.Import = function( clientName, componentName, resourceUrl ){
            if( !this.Check( componentName ) )
            {
                if( !this.Load( clientName, componentName, resourceUrl ) ) return false;
            }
            var component = null;
            eval( "component = " + componentName + ";" );
            if( typeof component.GetComponentName == "undefined" )
            {
                //Invalid Importable Component
                return false;
            }
            return component.AddReference( clientName );            
        };
        MyIEOS.Component.Remove = function( clientName, componentName ){
            if( !this.Check( componentName ) ) return false;
            var component = null;
            eval( "component = " + componentName + ";" );
            if( typeof component.GetComponentName == "undefined" )
            {
                //Invalid Importable Component
                return false;
            }
            return component.RemoveReference( clientName );
        };
        MyIEOS.Component.Load = function( clientName, componentName, resourceUrl ){
            if( this.Check( componentName ) )
            {
                return eval( componentName + ".AddReference( clientName );" );
            }
            if( resourceUrl == undefined || resourceUrl == "" )
            {
                resourceUrl = componentName.replace( /\./g, "/" ) + ".js";
            }
            if( resourceUrl.indexOf( "?" ) == -1 ) resourceUrl += "?";
            else resourceUrl += "&";
            var curDate = new Date();
            resourceUrl +=  "RT=" + Date.UTC( curDate.getFullYear(), curDate.getMonth(), curDate.getDate(), curDate.getHours(), curDate.getMinutes(), curDate.getSeconds(), curDate.getMilliseconds() );
            
            if( resourceUrl.toLowerCase().indexOf( "http://" ) == -1 )
            {
                resourceUrl = __MyIEOS_RootPath + resourceUrl;
            }
            
            curDate = null;
 
            var text = MyIEOS.Net.LoadText( resourceUrl );
            if( !this.Check( componentName ) )
            {
                try
                {
                    eval( text );
                    result = this.Check( componentName );
                }
                catch( ex )
                {
                    alert( "编译失败: " + ex.message + "\r\nclientName = " + clientName + "\r\ncomponentName = " + componentName + "\r\nresourceUrl = " + resourceUrl );
                    return false;
                }
            }
            return false;
        };
        
        MyIEOS.Cache = new MyIEOS.Component.Base( "MyIEOS.Cache" );
        
        MyIEOS.Cache.CacheObject = function( __data, __expiresTime ){
            
            var createTime = new Date();
            var expiresTime = __expiresTime;
            var data = __data;
            
            this.GetCreateTime = function(){
                return createTime;
            };
            
            this.GetExpiresTime = function(){
                return expiresTime;
            };
            
            this.IsExpires = function(){
                return expiresTime < createTime;
            };
               
            this.GetData = function(){
                return data;    
            }; 
        
        };
        
        MyIEOS.Cache.Manager = {
            
            __CacheDictionary : {},
            
            Insert : function( key, value, expiresTime ){
                if( this.__CacheDictionary.hasOwnProperty( key ) )
                {
                    return false;   
                }
                var cacheObj = new MyIEOS.Cache.CacheObject( value, expiresTime );  
                this.__CacheDictionary[ key ] = cacheObj;     
                return true;            
            },
            
            Remove : function( key ){
                if( this.__CacheDictionary.hasOwnProperty( key ) )
                {
                    delete this.__CacheDictionary[ key ]; 
                    return true;
                }
                return false;                
            },
            
            Get : function( key ){
                 var v = this.__CacheDictionary[ key ];
                 if( v instanceof MyIEOS.Cache.CacheObject )
                 {
                    return v.GetData();
                 } 
                 return null;
            }
        }
    }
}

window.onload = __MyIEOS_Main;
window.onunload = __MyIEOS_Exit;
