// -------------------------------------------------------------------------------------------------------------------------------------------------------------
// Create variables
// -------------------------------------------------------------------------------------------------------------------------------------------------------------
var variables = new Array();
var equationSets = new Array();

CreateVariables();
CreateEquations();


// -------------------------------------------------------------------------------------------------------------------------------------------------------------
function CreateVariables()
{
    v = new Variable( "Rv" );
    v.SetType( 1 ); //deg/min
    variables[ "Rv" ] = v ;

    v = new Variable( "Ra" );
    v.SetType( 1 ); //deg/min    
    variables[ "Ra" ] = v ;    

    v = new Variable( "Rs" );
    v.SetType( 1 ); //deg/min    
    variables[ "Rs" ] = v ;
    
    v = new Variable( "Dv" );
    v.SetType( 1 ); //deg/min    
    variables[ "Dv" ] = v ;
    
    v = new Variable( "Da" );
    v.SetType( 1 ); //deg/min    
    variables[ "Da" ] = v ;
    
    v = new Variable( "dm" );
    v.SetType( 2 ); //deg    
    variables[ "dm" ] = v ;
    
    v = new Variable( "d" );
    v.SetCaption( "&#916;" );    
    v.SetType( 2 ); //deg    
    variables[ "d" ] = v ;
    
    v = new Variable( "ct" );
    v.SetType( 2 ); //deg    
    variables[ "ct" ] = v ;
    
    v = new Variable( "ab" );
    v.SetType( 2 ); //deg    
    variables[ "ab" ] = v ;
    
    v = new Variable( "Zvp" );
    v.SetType( 2 ); //deg    
    variables[ "Zvp" ] = v ;
    
    v = new Variable( "M" );    
    v.SetType( 2 ); //deg    
    variables[ "M" ] = v ;    
}

// -------------------------------------------------------------------------------------------------------------------------------------------------------------
function CreateEquations()
{
    var equationSet, equation;
    
    // ct
    equationSet = new EquationSet();
    equationSets[ equationSets.length ] =  equationSet;
    equation = new Equation( "ct", new Array( "dm", "+", "d" ), new Array( variables[ "dm" ], variables[ "d" ] ) );    
    equation.GetResult().SetType( 2 ); // deg/min
    equationSet.AddEquation( equation );
    equation = new Equation( "ct", new Array( "-", "Zvp" ), new Array( variables[ "Zvp" ] ) );    
    equation.GetResult().SetType( 2 ); // deg
    equationSet.AddEquation( equation );
    equation = new Equation( "ct", new Array( "Rv", "-", "Ra" ), new Array( variables[ "Rv" ], variables[ "Ra" ] ) );    
    equation.GetResult().SetType( 2 ); // deg
    equationSet.AddEquation( equation );
    
    // desvio
    equationSet = new EquationSet();
    equationSets[ equationSets.length ] =  equationSet;
    equation = new Equation( "d", new Array( "ct", "-", "dm" ), new Array( variables[ "ct" ], variables[ "dm" ] ) );    
    equation.GetResult().SetType( 2 ); // deg
    equation.GetResult().SetCaption( "&#916;" ); 
    equationSet.AddEquation( equation );
    
    // declinacion magnetica
    equationSet = new EquationSet();
    equationSets[ equationSets.length ] =  equationSet;
    equation = new Equation( "dm", new Array( "ct", "-", "d" ), new Array( variables[ "ct" ], variables[ "d" ] ) );    
    equation.GetResult().SetType( 2 ); // deg
    equationSet.AddEquation( equation );

    // Zvp
    equationSet = new EquationSet();
    equationSets[ equationSets.length ] =  equationSet;
    equation = new Equation( "Zvp", new Array( "-", "ct" ), new Array( variables[ "ct" ] ) );    
    equation.GetResult().SetType( 2 ); // deg
    equationSet.AddEquation( equation );

    // Rs
    equationSet = new EquationSet();
    equationSets[ equationSets.length ] =  equationSet;
    equation = new Equation( "Rs", new Array( "Rv", "+", "ab" ), new Array( variables[ "Rv" ], variables[ "ab" ] ) );    
    equation.GetResult().SetType( 1 ); // deg/min
    equationSet.AddEquation( equation );

    // Rv
    equationSet = new EquationSet();
    equationSets[ equationSets.length ] =  equationSet;
    equation = new Equation( "Rv", new Array( "Rs", "-", "ab" ), new Array( variables[ "Rs" ], variables[ "ab" ] ) );    
    equation.GetResult().SetType( 1 ); // deg/min
    equationSet.AddEquation( equation );
    equation = new Equation( "Rv", new Array( "Ra", "+", "ct" ), new Array( variables[ "Ra" ], variables[ "ct" ] ) );    
    equation.GetResult().SetType( 1 ); // deg/min
    equationSet.AddEquation( equation );
    equation = new Equation( "Rv", new Array( "Dv", "-", "M" ), new Array( variables[ "Dv" ], variables[ "M" ] ) );    
    equationSet.AddEquation( equation );
    equation.GetResult().SetType( 1 ); // deg/min
    
    // ab
    equationSet = new EquationSet();
    equationSets[ equationSets.length ] =  equationSet;
    equation = new Equation( "ab", new Array( "Rs", "-", "Rv" ), new Array( variables[ "Rs" ], variables[ "Rv" ] ) );    
    equation.GetResult().SetType( 2 ); // deg
    equationSet.AddEquation( equation );

    // Ra
    equationSet = new EquationSet();
    equationSets[ equationSets.length ] =  equationSet;
    equation = new Equation( "Ra", new Array( "Rv", "-", "ct" ), new Array( variables[ "Rv" ], variables[ "ct" ] ) );    
    equation.GetResult().SetType( 1 ); // deg/min
    equationSet.AddEquation( equation );
    equation = new Equation( "Ra", new Array( "Da", "-", "M" ), new Array( variables[ "Da" ], variables[ "M" ] ) );    
    equationSet.AddEquation( equation );
    equation.GetResult().SetType( 1 ); // deg/min
    
    // Dv
    equationSet = new EquationSet();
    equationSets[ equationSets.length ] =  equationSet;
    equation = new Equation( "Dv", new Array( "Rv", "+", "M" ), new Array( variables[ "Rv" ], variables[ "M" ] ) );    
    equationSet.AddEquation( equation );
    equation.GetResult().SetType( 1 ); // deg/min

    // Da
    equationSet = new EquationSet();
    equationSets[ equationSets.length ] =  equationSet;
    equation = new Equation( "Da", new Array( "Ra", "+", "M" ), new Array( variables[ "Ra" ], variables[ "M" ] ) );    
    equation.GetResult().SetType( 1 ); // deg/min
    equationSet.AddEquation( equation );
    
    // M
    equationSet = new EquationSet();
    equationSets[ equationSets.length ] =  equationSet;
    equation = new Equation( "M", new Array( "Dv", "-", "Rv" ), new Array( variables[ "Dv" ], variables[ "Rv" ] ) );    
    equation.GetResult().SetType( 2 ); // deg
    equationSet.AddEquation( equation );
    equationSets[ equationSets.length ] =  equationSet;
    equation = new Equation( "M", new Array( "Da", "-", "Ra" ), new Array( variables[ "Da" ], variables[ "Ra" ] ) );    
    equation.GetResult().SetType( 2 ); // deg
    equationSet.AddEquation( equation );
    
}

function BorrarDatosRumbo()
{
    for ( var i = 0; i < document.forms[ 0 ].elements.length; i++ )
    { 
        if ( document.forms[ 0 ].elements[ i ].type != "button" )
        {
            document.forms[ 0 ].elements[ i ].disabled = false;
            document.forms[ 0 ].elements[ i ].style.backgroundColor = "white"
            document.forms[ 0 ].elements[ i ].value = "";            
        }
    }
    document.getElementById("ResultEquations").innerHTML = "&nbsp;";
}

function GetFormData()
{
    for ( var v in variables )
        variables[ v ].ResetValues();

    GetFormSingleData( variables[ "Rv" ], "DegRv", "MinRv" );DisableFormSingleData( "Rv", false );
    GetFormSingleData( variables[ "Ra" ], "DegRa", "MinRa" );DisableFormSingleData( "Ra", false );
    GetFormSingleData( variables[ "Rs" ], "DegRs", "MinRs" );DisableFormSingleData( "Rs", false );
    
    GetFormSingleData( variables[ "Dv" ], "DegDv", "MinDv" );DisableFormSingleData( "Dv", false );    
    GetFormSingleData( variables[ "Da" ], "DegDa", "MinDa" );DisableFormSingleData( "Da", false );
    
    GetFormSingleData( variables[ "dm" ], "dm", "" );DisableFormSingleData( "dm", false );           
    GetFormSingleData( variables[ "d" ], "d", "" );DisableFormSingleData( "d", false );
    GetFormSingleData( variables[ "ct" ], "ct", "" );DisableFormSingleData( "ct", false );
    GetFormSingleData( variables[ "ab" ], "ab", "" );DisableFormSingleData( "ab", false );
    GetFormSingleData( variables[ "Zvp" ], "Zvp", "" );DisableFormSingleData( "Zvp", false );
    GetFormSingleData( variables[ "M" ], "M", "" );DisableFormSingleData( "M", false );
}

function GetFormSingleData( v, id1, id2 )
{
    var s1, s2;
    if ( id1 != "" )
    {
        if ( id2 == "" )
        {
            // single data (degrees with sign)
            s1 = document.getElementById( id1 ).value;
            if( s1 == "" ) 
                v.ResetValues();
            else
                v.Set( parseFloat( s1 ) );
        }
        else
        {
            // double data (degrees/minutes without sign: 0-360)        
            s1 = document.getElementById( id1 ).value;        
            s2 = document.getElementById( id2 ).value;
            if( s1 != "" || s2 != "" ) 
            {
                if ( s1 == "" )
                    s1 = "0";
                if ( s2 == "" )
                    s2 = "0";
                a = parseFloat( s1 ) + parseFloat( s2 ) / 60.0;
                a = Ang360( a ) ;
                v.Set( a );
            }
            else
            {
                v.ResetValues();
            }
        }           
    }
}

function DisableFormSingleData( s, bDisable )
{
    var c
    c = document.getElementById( s );
    if ( c ) 
    {
        c.disabled = bDisable;
        if ( bDisable )
            c.style.backgroundColor = "#CCC"
        else
            c.style.backgroundColor = "white"            
    }
    c = document.getElementById( "Deg" + s );
    if ( c ) 
    {
        c.disabled = bDisable;
        if ( bDisable )
            c.style.backgroundColor = "#CCC"
        else
            c.style.backgroundColor = "white"                    
    }
    c = document.getElementById( "Min" + s );
    if ( c ) 
    {
        c.disabled = bDisable;        
        if ( bDisable )
            c.style.backgroundColor = "#CCC"
        else
            c.style.backgroundColor = "white"                    
    }
}

// -------------------------------------------------------------------------------------------------------------------------------------------------------------
function CalcBearings( control )
{
    var i;
    sId1 = control.id;
    sId2 = "";
    sVarname = "";
    // get data from form (depending on whether the changed control is a deg/min value or otherwise
    if ( sId1.substr( 0, 3 ) == "Deg" )
    {
        sVarName = sId1.substr( 3, sId1.length - 3 ); 
        sId2 = "Min" + sVarName ; 
    }
    else if ( sId1.substr( 0, 3 ) == "Min" )
    {
        sVarName = sId1.substr( 3, sId1.length - 3 ); 
        sId2 = sId1;
        sId1 = "Deg" + sVarName ;
    }
    else
    {
        sVarName = sId1;
    }
    v = variables[ sVarName ];
    
    GetFormData();

    aSolutionOrder = new Array();
    // uncalculate everything except for the current variable
    for ( i = 0; i < equationSets.length; i++ )
    {
        equationSets[ i ].Unsolve();
    }

    var aVariablesSolved = new Array();
    aVariablesSolved[ sVarName ] = true;
    // solve eqs
    for ( i = 0; i < equationSets.length; i++ )
    {      
        var bDone = false;
        if ( !equationSets[ i ].Solved() )        
        {
            for ( var sDone in aVariablesSolved )
            {
                // check if the current variable has alredy been solved                
                if ( sDone == equationSets[ i ].aEquations[0].GetResult().GetName() ) 
                    bDone = true;
            }
            if ( !bDone )
            {
                //try to solve the equation
                equationSets[ i ].Solve();
                // if so, save the results       
                if ( equationSets[ i ].Solved() )
                {
                    aSolutionOrder[ aSolutionOrder.length ] = i;
                    sVarName = equationSets[ i ].GetCurrentSolution().GetResult().GetName();
                    v = variables[ sVarName ];    
                    aVariablesSolved[ sVarName ] = true;
                    DisableFormSingleData( sVarName, true );
                    for( var vSol in equationSets[ i ].GetCurrentSolution().GetExpression().avVariables )
                        aVariablesSolved[ vSol ] = true;
                    v.Copy( equationSets[ i ].GetCurrentSolution().GetResult() );
                    // if a variable has been solve we have to start over with the equation sets
                    i = -1;
                }
            }
        }
    }

    // display results
    var sResult = "";
    for ( i = 0; i < aSolutionOrder.length; i++ )
    {      
        sResult = sResult + equationSets[ aSolutionOrder[ i ] ].GetCurrentSolution() + "<br>" ;
    }
    if ( sResult == "" )
        sResult = "&nbsp;";
    document.getElementById("ResultEquations").innerHTML = sResult;
    
}