/**
 *  scca.js: this is the scca client-side application
 *
 *  Last Checked In By $Author: rich $
 *  $Date: 2005/01/17 12:39:05 $
 *  $Revision: 1.14 $
 *
 *  (c) 2004 Copyright Basis Applied Technology
 *  CONFIDENTIAL INFORMATION
 *  All rights are reserved. Copying or other reproduction of
 *  this program except for archival purposes is prohibited
 *  without the prior written consent of Basis Applied Technology
 *
 *  Basis Applied Technology
 *  Gibsons, BC
 *  604.886.0721
 *
 *  REVISION HISTORY
 *
 *  $Log: scca.js,v $
 *  Revision 1.14  2005/01/17 12:39:05  rich
 *  *** empty log message ***
 *
 *  Revision 1.13  2005/01/17 12:36:24  rich
 *  *** empty log message ***
 *
 *  Revision 1.12  2005/01/14 17:46:08  rich
 *  *** empty log message ***
 *
 *  Revision 1.11  2005/01/08 13:36:57  larryg
 *  Bug Fixes
 *
 *  Revision 1.10  2004/12/12 20:25:18  larryg
 *  removed alert
 *
 *  Revision 1.9  2004/12/07 19:05:44  rich
 *  Modified scca.onError to check for core.doDebug
 *
 *  Revision 1.8  2004/11/23 11:24:41  larryg
 *  *** empty log message ***
 *
 *  Revision 1.7  2004/11/12 16:44:52  larryg
 *  *** empty log message ***
 *
 *  Revision 1.6  2004/09/21 14:26:26  larryg
 *  *** empty log message ***
 *
 *  Revision 1.5  2004/09/21 10:45:08  larryg
 *  *** empty log message ***
 *
 *  Revision 1.4  2004/09/15 00:09:47  larryg
 *  *** empty log message ***
 *
 *  Revision 1.3  2004/08/04 05:38:04  larryg
 *  *** empty log message ***
 *

 *
 *
 */

// replace the core system theme.
//core.loadUnit("../../../scca/scripts/themes");

//////////////////////////////////////////////////////////////////////////////////////////////

/** */
scca = {};

/** set to true to turn on alerts */
scca.doDebug    = true;

/** a settable callback for when the user logs in or out */
scca.onLoginStateChanged = {};

/** the 'ping' frequency in milliseconds. this is the frequency at which the client refreshes the session.
* in order to prevent session timeouts when in the application, call scca.pingServer(). calling pingServer(false)
* will reset the ping timer and make a call immediately.
*
* new messages are also checked along with the ping so set it to whatever your message check frequency is at...
*
**/
scca.ping_frequency = 300000; // 5 minutes
scca.wait_for_next_ping = true;
scca.active_session = false;

//////////////////////////////////////////////////////////////////////////////////////////////

/**
* initialize scca with a properties object (same one as base)
*/
scca.init = function(obj)
{
    // setup my BASE options.
    base.cbMessageCount = scca.onMessageCountRefresh;
    base.cbError        = scca.onError;
    base.status         = scca.onStatus;

    base.system_server  = 'services/svc_system.php';
    base.ping_frequency = 300000;

    // initialize base properly.
    obj.theme = scca_themes.App;

    base.init(obj);
	base.obj = obj.obj;
	base.view = obj.view;

    // attach page events
    oLogin = core.getElm('login_block_login');
    oLogout = core.getElm('logout_block_logout');

    if( oLogin ) {
    	core.addEvent( oLogin , 'click', scca.onLoginClicked );
	}

    if( oLogout ) {
    	core.addEvent( oLogout , 'click', scca.onLogoutClicked );
	}

    // setup ui
    scca.updateLoginUI();

};
/////////////////////////////////////////////////////////
// event handlers and callbacks

/** */
scca.onLoginClicked = function()
{
	//alert( core.getElm('login_username').value +":" + core.getElm('login_password').value );

    var r = base.login( core.getElm('login_username').value, core.getElm('login_password').value, null, null, true);

    if ( !(r instanceof XMLRPCFault) )
    {
//		alert( base.current_user['Username'] );
   		window.location = 'index.php?obj=Login&user=' + base.current_user['Username'];
    }
};

/** */
scca.onLogoutClicked = function()
{
	//alert("logout");
    var r = base.logout(null, null, true);
    if ( !(r instanceof XMLRPCFault) )
    {
        document.location.replace('./index.php');
        //Force a refresh
        document.location.href = './index.php';
    }
};

/** */
scca.onError = function ( err )
{
    if (err.faultCode==-1004) {
        if (!this._alerted1004) {
            this._alerted1004=true;
            alert('No session was found (Your session may have expired). The window will now reload.');
            window.location.href = window.location.href;
        }
    } else if(core.doDebug ) {
        core.getElm('error_div').innerHTML = err.faultString.replace("\n", "<br>");
        alert('ERROR (' + err.faultCode + '): ' + err.faultString );
        //base.status('Error (' + err.faultCode + '): ' + err.faultString);
    }
};

/** */
scca.onStatus = function ( s )
{
    window.status = s;
};

/** */
scca.onMessageCountRefresh = function( n )
{
   //core.getElm('main_message_summary_div').innerHTML = "you have " + ((n>0)?("<b>"+n+"</b>"):"no") + " new message" + ((n!=1)?"s":"");
};

/** */
scca.updateLoginUI = function(  )
{
	oLoginBlock = core.getElm('login_block');
	oLogoutBlock = core.getElm('logout_block');
    if (base.loggedIn())
    {
		//alert("testing updateLoginUI - logged in");
        //scca.onMessageCountRefresh( base.newMessageCount() );
        if( oLoginBlock ) {
            core.getElm("main_div").style.width = "750px";
        	oLoginBlock.style.display = 'none';
            //core.getElm( "divLogInContainer" ).style.display="none";
		}

        if( oLogoutBlock ) {
            core.getElm("main_div").style.width = "800px";
        	oLogoutBlock.style.display = 'inline';
            //core.getElm( "divLogInContainer" ).style.display="block";
		}

        //core.getElm('main_content_header_message_bar').style.display = 'block';
        //core.getElm('navigation_bar').style.display = 'block';
    }
    else
    {
        core.getElm("main_div").style.width = "750px";
        oLogoutBlock.style.display = 'none';

		//alert("testing updateLoginUI - not logged in");
        if( oLoginBlock ) {
        	oLoginBlock.style.display = 'inline';
		}

        if( oLogoutBlock ) {
        	oLogoutBlock.style.display = 'none';
		}
    }

    core.getElm('error_div').innerHTML = '';
}

scca.browse_server = function(_input, url) {
    scca.__server_input_target = _input;
    url = url+('&NoTarget='+(_input == null));
    var width  = screen.width * 0.7;
    var height = screen.height * 0.7;
    var sOptions = "toolbar=no,status=no,resizable=yes,dependent=yes" ;
    sOptions += ",width=" + width ;
    sOptions += ",height=" + height ;
    sOptions += ",left=" + ((screen.width  - width) / 2) ;
    sOptions += ",top=" + ((screen.height - height) / 2) ;
    var oWindow = window.open( url, "FCKBrowseWindow", sOptions ) ;
    try { oWindow.focus();}
    catch(e){;}
};
function SetUrl(url) {
    if (scca.__server_input_target) {
        scca.__server_input_target.value = url;
        scca.__server_input_target = null;
    }
};