var xmlhttp = false;
var handlerUrl = 'ConfigHandler.ashx';
var waitForContent = false;
var waitForConfigurator = false;

try{ xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e){}
try{ xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e){}
try{ xmlhttp = new XMLHttpRequest(); } catch(e){}

if( !xmlhttp ) 
    alert('Your browser does not support XMLHTTPRequest!');

function SendMessage( message, parameters )
{
    // if viewpoint is not ready don't send any messages..
    if( !IsMTSInstalled() )
        return;

    // if we are waiting response from configurator OR from content -> bounce back!
    if( waitForConfigurator || (waitForContent && contentLoading) )
        return;
        
    if( m_measureVisible )
    {
        m_measureVisible = false;
        HideMeasures();
    }

    
    var url = handlerUrl + '?CMD=';
    if( parameters == null )
        url += message;
    else
        url += message + ':' + parameters;
        
	xmlhttp.open( 'GET', url, true );
	xmlhttp.onreadystatechange = function()
	{
		if( xmlhttp.readyState == 4 )
			if( xmlhttp.status == 200 )
				ReceiveResponse();
	}
  	
	waitForConfigurator = true;
	xmlhttp.send( null );

	HideMessage();
}

function ReceiveResponse()
{
	var actionReport = xmlhttp.responseXML;
	
	handleError( actionReport );
	handleInitialize( actionReport );
	handleRemove( actionReport );
	handleShowAllowedItems( actionReport );
	handleShowMessage( actionReport );
	handleUpdateContent( actionReport );
	handleAllowedPlacements( actionReport );
	handleShowMeasure( actionReport );
	handleShowAllowedMaterials( actionReport );
	handleShowMaterialGroups( actionReport );
	handleActivateMaterialPlacement( actionReport );
	handleFloatButtonVisibility( actionReport );
	
	// config message is handled -> no more waiting!
	waitForConfigurator = false;
}

function handleError( actionReport )
{
    var message = getMessage( 'ConfigError', actionReport );  
    if( message != null ) {
        message = message.replace('&amp;','&')
        alert( message );
    }
} 

function handleInitialize( actionReport )
{   
    var message = getMessage( 'InitializeApplication', actionReport );  
    if( message != null ) {
        WindowResized(); // sent the client parameters to the server.
    }
}


function handleFloatButtonVisibility( actionReport )
{
    var message = getMessage( 'SetFloatButtonVisibility', actionReport );
    if( message != null ){
        if( message == 'show' )
            EnableFloatElementButton( true );
        else
            EnableFloatElementButton( false );
    }
}

function handleShowAllowedItems( actionReport )
{
    var message = getMessage( 'ShowAllowedItems', actionReport );
    if( message != null ){
        ShowAllowedItems( message );
    }
}

function handleShowMaterialGroups( actionReport )
{
    var message = getMessage( 'ShowMaterialGroups', actionReport );
    if( message != null ){
        ShowMaterialGroups( message );
    }
}

function handleActivateMaterialPlacement( actionReport )
{
    var message = getMessage( 'ActivateMaterialPlacement', actionReport );
    if( message != null ){
        SetActivateMaterialPlacement( message );
    }
}


function handleShowMeasure( actionReport )
{
    var message = getMessage( 'ShowMeasure', actionReport );
    if( message != null ){
        ShowMeasure( message );
    }
}

function handleShowAllowedMaterials( actionReport )
{
    var message = getMessage( 'ShowAllowedMaterials', actionReport );
    if( message != null ){
        ShowAllowedMaterials( message );
    }
}

function handleShowMessage( actionReport )
{
    if( getMessage( 'Message', actionReport ) != null ){}
}

function handleUpdateContent( actionReport )
{
    var message = getMessage( 'UpdateContent', actionReport );
    if( message != null )
    {
        if( message == 'update' )
        {
            waitForContent = true;
            UpdateContent();
        }
    }
}

function handleRemove( actionReport )
{
    var message = getMessage( 'RemoveSelected', actionReport );
    if( message != null )
    {
        ShowError( message );
    }
}

function handleAllowedPlacements( actionReport )
{
    var message = getMessage( 'GetAvailableMaterialPlacements', actionReport );
    if( message != null )
        SetMaterialPlacementData( message );
}

function getMessage( key, actionReport )
{
    var actionsToHandle = actionReport.getElementsByTagName( key );
    var message = null;
    
    if( actionsToHandle.length > 0 )
    {
        var messageAttribute = actionsToHandle[ 0 ].attributes[ 1 ];
    
        if( messageAttribute != null )
            message = messageAttribute.value;
        else
            message = actionsToHandle[ 0 ].childNodes[ 0 ].xml;
    }
        
    return message;
}


