var vSIM = null;
var PAGE = "./vSIM/z.html";

var WIN = "win";
var MAC = "mac";
var NIX = "nix";

var OPERA	= "opera";
var MS		= "microsoft";
var NS		= "netscape";
var AOL		= "aol";
var MOZ		= "mozilla";
var COMP	= "compuserve";
var FOX     = "firefox";

var IE3		= "ie3";
var IE4UP	= "ie4";
var GECKO	= "gecko";
var NN		= "nn";

function launchSim( sSrchParams, bStatusBar, bScrollBars, bResizable )
{
    var bp = new _vSIMBrowserProfile();
    ENV.init( bp );
    
    var iRes = ERRNO;
    if ( !ENV.BROWSERVALID )        iRes = ERRGEN;
    else if ( !ENV.JAVAENABLED )    iRes = ERRJAVAEN;
    else if ( !ENV.JAVAINSTALLED)   iRes = ERRJAVAINS;

    var sStatusBar = ( typeof bStatusBar != "undefined"? bStatusBar : false )? "yes":"no";
    var sScrollBars = ( typeof bScrollBars != "undefined"? bScrollBars : true )? "yes":"no";
    var sResizable = ( typeof bResizable != "undefined"? bResizable : true )? "yes":"no";

    var sSrch = sSrchParams || window.location.search;
    if( !sSrch ) sSrch = "";
    else if ( sSrch.charAt( 0 ) != "?" ) sSrch = "?" + sSrch;
    if (iRes == ERRNO) openSim( sSrch, sStatusBar, sScrollBars, sResizable );
    else ENV.browserError( iRes );
}

var oVSIMPop = null;
function openSim( sSrch, sStatusBar, sScrollBars, sResizable )
{
    var a = launchsize();
    if ( a && a.length && a.length == 2 )
    {
        var l = (screen.availWidth - a[0]) / 2;
        var t = (screen.availHeight - a[1]) / 2;
        
        if (!vSIM || vSIM==null || vSIM.closed)
        {
        	PAGE = "./vSIM/" + (sSrch.indexOf("cb") > 0 ? "zCB.html" : "zYB.html");
            var sURL = PAGE + sSrch;
            
            var strPosition = "left=" + l + ",top=" + t;
            var sFeatures = "fullscreen=" + (ENV.FULLSCREEN?"yes":"no") + ",toolbar=no,menubar=no,location=no,status="+sStatusBar+",scrollbars="+sScrollBars+",resizable="+sResizable+"," + (ENV.FULLSCREEN?"":"width=" + a[0] + ",height=" + a[1] + ")")  + " ," + strPosition;
            oVSIMPop = new vSIMPWin( "oVSIMPop", ENV.WINDOWNAME, sURL, sFeatures, ENV.ADVPOPCHECK, openSimCb, false )
            oVSIMPop.resizeOnOpen( a[ 0 ], a[ 1 ] );
            oVSIMPop.open();
        }
        else
        {
            if ( vSIM.focus )
            {
                vSIM.focus();
                return false;
            }
        }
    }
}

function openSimCb( oObjs )
{
    vSIM = oObjs.win;
    if ( vSIM == null ) ENV.browserError( ERRPOPUP );
}

function launchsize()
{
    var a = null;
    if ( screen )
    {
        var sw = screen.availWidth;
        var sh = screen.availHeight;

        a = new Array(sw, sh);
        var res = ENV.SCREENSIZES.split(";");
        if (res.length)
        {
            var iNoRes = res.length;
            var iMaxW,iMaxH,iMinW,iMinH;
            var i = 0;
            do
            {
                res[i] = res[i].split("x");
                if (!iMaxW || res[i][0] > iMaxW)
                {
                    iMaxW = parseInt( res[i][0] );
                    iMaxH = parseInt( res[i][1] );
                }
                
                if (!iMinW || res[i][0] < iMinW)
                {
                    iMinW = parseInt( res[i][0] );
                    iMinH = parseInt( res[i][1] );
                }
            }
            while (++i < iNoRes)

            if ( sw < iMinW )
            {
                ENV.browserError(ERRSCREEN);
                a = null;
            }
            else if( sw > iMaxW && !ENV.FILLSCREEN )  a = new Array ( iMaxW, iMaxH );
        }
    }
    return a;
}

function _vSIMBrowserProfile()
{
    this.OS         = this.getOS();
    this.org        = "";
    this.family     = "";
    this.version    = 0;

    var i = 0;
    var ua = window.navigator.userAgent.toLowerCase();
    
    if ( ua.indexOf(OPERA) != -1 )
    {
        i = ua.indexOf(OPERA);
        this.family     = OPERA;
        this.org        = OPERA;
        this.version    = parseFloat( "0" + ua.substr( i+6 ), 10 );
    }
    else if ( (i = ua.indexOf("msie")) != -1 )
    {
        this.org        = MS;
        this.version    = parseFloat( "0" + ua.substr( i+5 ), 10 );

        if ( navigator.version < 4 ) this.family = IE3;
        else this.family = IE4UP;
    }
    else if (typeof(window.controllers) != "undefined" && typeof(window.locationbar) != "undefined")
    {
        i = ua.lastIndexOf("/")
        this.version = parseFloat("0" + ua.substr(i+1), 10);
        this.family = GECKO;

        if (ua.indexOf(NS) != -1) this.org = NS;
        else if (ua.indexOf(COMP) != -1) this.org = COMP;
        else if (ua.indexOf(FOX) != -1)
        {
            this.org = FOX;
            this.version = parseFloat( "0" + window.navigator.vendorSub, 10 );
        }
        else
        {
            this.org = MOZ;
            var re = /(rv:)([0-9\.]*)([,;)])/i;
            var resArr = ua.match(re);
            if ( resArr && resArr[ 2 ] ) this.version = parseFloat( "0" +  resArr[ 2 ], 10 );
        }
    }
    else if ((ua.indexOf(MOZ) !=-1) && (ua.indexOf("spoofer")==-1) && (ua.indexOf("compatible") == -1) && (ua.indexOf(OPERA)==-1)&& (ua.indexOf("webtv")==-1) && (ua.indexOf("hotjava")==-1))
    {
        var is_major = parseFloat( navigator.appVersion );
        if (is_major < 4) this.version = is_major;
        else
        {
            i = ua.lastIndexOf("/")
            this.version = parseFloat("0" + ua.substr(i+1), 10);
        }
        this.org = NS;
        this.family = NN;
    }
    else if ((i = ua.indexOf(AOL)) != -1 )
    {
        this.family		= AOL;
        this.org        = AOL;
        this.version    = parseFloat("0" + ua.substr(i+4), 10);
    }
}
_vSIMBrowserProfile.prototype.getOS         = _vSIMBrowserProfile_getOS;
_vSIMBrowserProfile.prototype.javaEnabled   = _vSIMBrowserProfile_javaEnabled;
_vSIMBrowserProfile.prototype.javaInstalled = _vSIMBrowserProfile_javaInstalled;
_vSIMBrowserProfile.prototype.nsJavaTest    = _vSIMBrowserProfile_nsJavaTest;

function _vSIMBrowserProfile_getOS()
{
    var os = "";
    if ( typeof( window.navigator.platform ) != "undefined" )
    {
        var platform = window.navigator.platform.toLowerCase();
        if (platform.indexOf(WIN) != -1) os = WIN;
        else if (platform.indexOf(MAC) != -1) os = MAC;
        else if (platform.indexOf("unix") != -1 || platform.indexOf("linux") != -1 || platform.indexOf("sun") != -1) os = NIX;
    }
    return os;
}

function _vSIMBrowserProfile_javaEnabled()
{
    return navigator.javaEnabled();
}

function _vSIMBrowserProfile_javaInstalled()
{
    if ( this.OS == WIN && this.org == MS && this.version >= 5.0 ) return oClientCaps.isComponentInstalled( "{08B0E5C0-4FCB-11CF-AAA5-00401C608500}", "ComponentID" );
    else if ( this.OS == WIN && this.org == MS ) return true;
    else return this.nsJavaTest();
}

function _vSIMBrowserProfile_nsJavaTest()
{
    var np = navigator.plugins;
    var iNp = np.length;
    bS = false;
    for ( var i = 0; i < iNp; i++ )
    {
        var jp = np[i].description.indexOf( "1." );

        if ( np[i].description.indexOf( "Java" ) != -1  )
        {
            if ( jp == -1 )
            {
                bS = true;
                break;
            }

            var sv = parseInt( np[i].description.substring( jp + 2, jp + 3 ) );
            if ( !isNaN( sv ) && sv > 3 )
            {
                bS = true;
                break;
            }
            else if ( !isNaN( sv ) && sv == 3 )
            {
                sv = parseInt( np[i].description.substring( jp + 4, jp + 5 ) );
                if ( !isNaN( sv ) )
                {
                    if ( sv == 1 )
                    {
                        sv = parseInt( np[i].description.substring( jp + 6, jp + 8 ) );
                        
                        if ( !isNaN( sv ) && sv >= 2 )
                        {
                            bS = true;
                            break;
                        }
                    }
                    else if ( sv > 1 )
                    {
                        bS = true;
                        break;
                    }
                }
            }
        }
    }
    return( bS );
}

function vSIMPWin( sNameSpace, sName, sURL, sFeatures, bAdvCheck, fCallback, bLoadInCurrentOnFail )
{
    this.sNS        = sNameSpace;
    this.sName      = sName;
    this.sURL       = sURL;
    this.sFeatures  = sFeatures;
    this.bAdvCheck  = bAdvCheck;
    this.fCB        = fCallback || null;
    this.bLICOF     = bLoadInCurrentOnFail;

    this.bResize    = false;
    this.iW         = null;
    this.iH         = null;
    this.oWin       = null;
}
vSIMPWin.prototype.resizeOnOpen = vSIMPWin_resizeOnOpen;
vSIMPWin.prototype.open         = vSIMPWin_open;
vSIMPWin.prototype.popup        = vSIMPWin_popup;
vSIMPWin.prototype.popupCheck   = vSIMPWin_popupCheck;
vSIMPWin.prototype.popupFailed  = vSIMPWin_popupFailed;

function vSIMPWin_resizeOnOpen( iW, iH )
{
    this.bResize = true;
    this.iW = iW;
    this.iH = iH;
}

function vSIMPWin_open()
{
    if ( this.bAdvCheck ) setTimeout( this.sNS + ".popup()", 0 );
    else this.popup();
}

function vSIMPWin_popup()
{
    this.oWin = window.open( this.sURL, this.sName, this.sFeatures );
    if ( this.oWin )
    {
        if ( this.oWin.attachEvent )
        {
            this.oWin.attachEvent( "onerror", new Function( "", this.sNS + ".popupFailed()" ) ); // Requires further testing
            this.oWin.attachEvent( "onload", new Function( "", this.sNS + ".popupCheck()" ) );
        }
        else if ( this.oWin.addEventListener )
        {
            this.oWin.addEventListener( "error", new Function( "", this.sNS + ".popupFailed()" ), false ); // Requires further testing
            this.oWin.addEventListener( "load", new Function( "", this.sNS + ".popupCheck()" ), false );
        }
    }
    else if ( this.fCB ) this.fCB( { win:this.oWin, url: this.sURL } );
}

function vSIMPWin_popupCheck()
{
    try 
    {
        if ( this.bResize ) this.oWin.resizeTo( iW, iH );           
        this.oWin.focus();
    }
    catch (e)
    {
        if ( this.bLICOF )
        {
            window.location.href = this.sURL;
            this.oWin = null;
        }
    }

    if ( this.fCB ) this.fCB( { win:this.oWin, url: this.sURL } );
}

function vSIMPWin_popupFailed()
{
    if ( this.fCB ) this.fCB( { win:this.oWin, url: this.sURL } );
}

