<!--<HTML>
//
//    New version to operate Inglenook Sidings, 02Jan2002
//
//    XYZ
//     |
//     +-- TrackFree()
//     |
//     +-- MoveTrain2()
//     |   |
//     |   +-- MoveStock()
//     |   |   |
//     |   |   +-- DisplayStock()
//     |   |       |
//     |   |       +-- ScreenPosX()
//     |   |       |
//     |   |       +-- ScreenPosY()
//     |   |
//     |   +-- SplitTrain2()
//     |
//     +-- CheckCoupling()
//         |
//         +-- AddToTrain()
//             |
//             +-- DisplayStock()
//
//    TrainDetails()
//
//    Uncouple()
//
//    ChangePoint()
//    |
//    +-- CheckPointFree()
//
//    SetUp()
//

var TrackL1   = new Array(255);
var TrackR1   = new Array(255);
var TrackP    = new Array(255);
var TrackL2   = new Array(255);
var TrackR2   = new Array(255);
var TrackX    = new Array(255);
var TrackY    = new Array(255);
var StockL    = new Array(10);
var StockR    = new Array(10);
var StockTL   = new Array(10);
var StockTR   = new Array(10);
var StockCL   = new Array(10);
var StockCR   = new Array(10);
var CoupImglc = new Array(10);
var CoupImglu = new Array(10);
var CoupImgrc = new Array(10);
var CoupImgru = new Array(10);
var StockImg  = new Array(200);
var Point     = new Array(10);
var PointAt   = new Array(10);
var PointOffY = new Array(10);
var PointImg0 = new Array(10);
var PointImg1 = new Array(10);
var PointClrA = new Array(10);
var PointClrB = new Array(10);
var PointClrC = new Array(10);
var PointClrD = new Array(10);
var Unc       = new Array(10);
var UncImg    = new Array(10);
var TrainPos  = new Array(10);
var MarkImg   = new Array(10);

var HighTrack, MaxTrack = 250;
var HighPoint, MaxPoint = 10;
var HighStock, MaxStock = 10;
var HighUnc,   MaxUnc   = 10;

var WaitTime = 175;
var myint;

var ThisDir = -1;
var LastDir = 0;
var MoveCount = 0;

//
//
isNS4 = (document.layers) ? true : false;
isIE4 = (document.all && !document.getElementById) ? true : false;
isIE5 = (document.all && document.getElementById) ? true : false;
isNS6 = (!document.all && document.getElementById) ? true : false;

//if( isNS4 )
//    alert("NS4");
//if( isIE4 )
//    alert("IE4");
//if( isIE5 )
//    alert("IE5");
//if( isNS6 )
//    alert("NS6");

//
//

function Go()
   {SetUp();
   }



function XYZ(Dir)
   {var intFirst;

   if( isNS4 ) {
       document.myform.txtWarn.value = "";
   } else {
        document.getElementById("txtWarn").value = "";
   }
    intFirst = TrainDetails("END", "", 1, Dir);
    if (TrackFree(intFirst, Dir) == "Y")
       {if (LastDir != 0 && LastDir != Dir)
           {MoveCount = MoveCount + 1;
           if( isNS4 ) {
               document.myform.txtMoveCount.value = MoveCount;
           } else {
                document.getElementById("txtMoveCount").value = MoveCount;
           }
            LastDir = 0;
           }
        ThisDir = Dir;
        MoveTrain2(Dir);
        CheckCoupling(intFirst, Dir, "Y");
       }
    else {
        if( isNS4 ) {
            document.myform.txtWarn.value = "No free track";
        } else {
             document.getElementById("txtWarn").value = "No free track";
        }
       }
   }



function MoveTrain2(inDir)
//
//    Move whole train in selected direction. Move from front;
//      Find head of train
//      Move lead stock until loco
//      Move loco
//      Move stock after loco if coupled
//      Any uncoupled stock behind loco to be split from train
//
   {var i;
    var intLead, intThis;
    var intCoupledLeading, intStockTrailing;
    var strLoco;
 
    strLoco = "N";
    intLead = 1;

    intLead = TrainDetails("END", "BEFORE", 1, inDir);
    
    intThis = intLead;
    while (intThis != 0)
       {intCoupledLeading = TrainDetails("COUPLED", "BEFORE", intThis, inDir);
        intStockTrailing = TrainDetails("TRAIN", "AFTER", intThis, inDir);
        
        if (strLoco == "N")
           {MoveStock2(intThis, inDir);
            if (intThis == 1) strLoco = "Y";
           }
        else
           {if (intCoupledLeading != 0)
               {MoveStock2(intThis, inDir);
               }
            else
               {SplitTrain2(intThis, inDir);
                intStockTrailing = 0; 
               }
           }

        intThis = intStockTrailing;
       }
   }



function TrainDetails(inType, inMove, inStockNo, inDir)
//
//   Get details about the current train
//
   {var intReturn = -1;
    var strParm;

    strParm = inType + "." + inMove + "." + inDir;

    if (inType == "END")
       {intReturn = 1;    
        if (inDir == 1) while (StockTR[intReturn] != 0) intReturn = StockTR[intReturn];
        else            while (StockTL[intReturn] != 0) intReturn = StockTL[intReturn];
       }

    if (strParm == "TRAIN.BEFORE.1")    intReturn = StockTR[inStockNo];
    if (strParm == "TRAIN.BEFORE.-1")   intReturn = StockTL[inStockNo];
    if (strParm == "TRAIN.AFTER.1")     intReturn = StockTL[inStockNo];
    if (strParm == "TRAIN.AFTER.-1")    intReturn = StockTR[inStockNo];

    if (strParm == "COUPLED.BEFORE.1")  intReturn = StockCR[inStockNo];
    if (strParm == "COUPLED.BEFORE.-1") intReturn = StockCL[inStockNo];
    if (strParm == "COUPLED.AFTER.1")   intReturn = StockCL[inStockNo];
    if (strParm == "COUPLED.AFTER.-1")  intReturn = StockCR[inStockNo];

    if (intReturn == -1)
       {document.write("Invalid function call: TrainDetails(" + inType + ", " + inMove + ", " + inStockNo + ", " + inDir + ")");
       }

    return intReturn
   }



function TrackFree(inStockNo, inDir)
//
//   Check if track exists for selected stock to move in 
//   selected direction.
//   Extra processing included to detect an incorrectly set trailing point, and
//   if found set it for the direction of travel.
//
   {var strReturn;

    strReturn = "N";
    var intNext;
    var intDiff1, intDiff2; 

    if( isNS4 ) {
        document.myform.txtWarn.value = "";
    } else {
         document.getElementById("txtWarn").value = "";
    }
    if (inDir==1) intNext = TrackR1[StockR[inStockNo]]
    else          intNext = TrackL1[StockL[inStockNo]]

    if (intNext > 0)
       {strReturn = "Y"
        if (TrackP[intNext] != 0)
           {intDiff1 = 0;
            intDiff2 = 0;
            if (inDir == 1  && TrackL1[intNext] != TrackL2[intNext])
               {intDiff1 = TrackL1[intNext] - StockL[inStockNo];
                intDiff2 = TrackL2[intNext] - StockL[inStockNo];
               }
            if (inDir == -1 && TrackR1[intNext] != TrackR2[intNext])
               {intDiff1 = StockR[inStockNo] - TrackR1[intNext];
                intDiff2 = StockR[inStockNo] - TrackR2[intNext];
               }
            if (intDiff1==6 && Point[TrackP[intNext]]==1) ChangePoint(TrackP[intNext], "Y");
            if (intDiff2==6 && Point[TrackP[intNext]]==0) ChangePoint(TrackP[intNext], "Y");
           }
       }

    return strReturn;
   } 



function CheckCoupling(inStockNo, inDir, inAutoCouple)
//
//   Check front of train in selected direction to find if 
//   further stock to be added.
//   If any then call AddToTrain, then recall self to check if more.
//
   {var i;
    var intNextLocation;

    strReturn = "N";

    if (inDir==1)
       {intNextLocation = TrackR1[StockR[inStockNo]]
        if (intNextLocation > 0)
           {for (i=0 ; i <= 10 ; i++)
               {if (i!=inStockNo && intNextLocation==StockL[i])
                   {AddToTrain(i, inDir, inAutoCouple);
                    CheckCoupling(i, inDir, "N");
                    LastDir = ThisDir;
                   }
               }
           }
       }
    else
       {intNextLocation = TrackL1[StockL[inStockNo]]
        if (intNextLocation > 0)
           {for (i=0 ; i <= 10 ; i++)
               {if (i!=inStockNo && intNextLocation==StockR[i])
                   {AddToTrain(i, inDir, inAutoCouple);
                    CheckCoupling(i, inDir, "N");
                    LastDir = ThisDir;
                   }
               }
           }
       }

    return strReturn;
   } 



function MoveStock2(inStockNo, inDir)
//
//   Move selected piece of stock in selected direction.
//   Left and right ends move seperately
//
   {var i;
    var intRight, intLeft;

    intLeft  = StockL[inStockNo];
    intRight = StockR[inStockNo];

    if (inDir==1)
       {if (TrackP[intRight] == 0)
           {intRight = TrackR1[intRight];
           }
        else
           {if (Point[TrackP[intRight]] == 0) intRight = TrackR1[intRight];
            else                              intRight = TrackR2[intRight];
           }

        if (TrackP[intLeft] == 0)
           {intLeft = TrackR1[intLeft];
           }
        else
           {if (Point[TrackP[intLeft]] == 0) intLeft = TrackR1[intLeft];
            else                             intLeft = TrackR2[intLeft];
           }
       }
    else // (inDir == -1)
       {if (TrackP[intRight] == 0)
           {intRight = TrackL1[intRight];
           }
        else
           {if (Point[TrackP[intRight]] == 0) intRight = TrackL1[intRight];
            else                              intRight = TrackL2[intRight];
           }

        if (TrackP[intLeft] == 0)
           {intLeft = TrackL1[intLeft];
           }
        else
           {if (Point[TrackP[intLeft]] == 0) intLeft = TrackL1[intLeft];
            else                             intLeft = TrackL2[intLeft];
           }
       }

    StockL[inStockNo] = intLeft;
    StockR[inStockNo] = intRight;
    DisplayStock(inStockNo);
   }



function DisplayStock(inStockNo)
//
//   Display selected piece of stock.
//   NB also updates test box for testing.
//
{
    var i;
    var strTrain;
    var intSpanX, inSpanY;
    var intStartX, intStartY;
    var intThisX, intThisY;
    var intScreenX, intScreenY;
    var intStockImg;

    if (inStockNo < 1)  document.write("Invalid function call: DisplayStock(" + inStockNo + ")");
    if (inStockNo > 10) document.write("Invalid function call: DisplayStock(" + inStockNo + ")");

    intSpanX = TrackX[StockR[inStockNo]] - TrackX[StockL[inStockNo]];
    intSpanX = (intSpanX + 1) / 7;
    intSpanY = TrackY[StockR[inStockNo]] - TrackY[StockL[inStockNo]];
    if (intSpanY != 0)
       {intSpanY = (intSpanY + 0) / 7;
       }

    intStartX = TrackX[StockL[inStockNo]];
    intStartY = TrackY[StockL[inStockNo]];
    intStockImg = 10 * (inStockNo - 1);

    for (i=1 ; i <= 7 ; i++)
       {intThisX = intStartX + (intSpanX * (i-1));
        intThisY = intStartY + (intSpanY * (i-1));

        intScreenX = ScreenPosX(intThisX);
        intScreenY = ScreenPosY(intThisY);

        if( isNS4 ) {
            StockImg[intStockImg + i].moveTo(intScreenX,intScreenY);
        } else {
            StockImg[intStockImg + i].style.left   = intScreenX;
            StockImg[intStockImg + i].style.top    = intScreenY;
        }
//        StockImg[intStockImg + i].style.pixelLeft   = intScreenX;
//        StockImg[intStockImg + i].style.pixelTop    = intScreenY;
       }
    if (StockCR[inStockNo] == 0 ) {
        if( isNS4 ) {
            CoupImgrc[inStockNo].moveTo(50,600);//400

            CoupImgru[inStockNo].moveTo(
                8 + (ScreenPosX(TrackX[StockR[inStockNo]]))
                ,ScreenPosY(TrackY[StockR[inStockNo]]) );
        } else {
            CoupImgrc[inStockNo].style.left = 50;
            CoupImgrc[inStockNo].style.top = 600;//400

            CoupImgru[inStockNo].style.left = 8 + (ScreenPosX(TrackX[StockR[inStockNo]]));
            CoupImgru[inStockNo].style.top = ScreenPosY(TrackY[StockR[inStockNo]]);
        }
    } else {
        if( isNS4 ) {
            CoupImgru[inStockNo].moveTo(50,600);//400
            CoupImgrc[inStockNo].moveTo(
                8 + (ScreenPosX(TrackX[StockR[inStockNo]])),
                ScreenPosY(TrackY[StockR[inStockNo]]) );
        } else {
            CoupImgru[inStockNo].style.left = 50;
            CoupImgru[inStockNo].style.top = 600;

            CoupImgrc[inStockNo].style.left = 8 + (ScreenPosX(TrackX[StockR[inStockNo]]));
            CoupImgrc[inStockNo].style.top = ScreenPosY(TrackY[StockR[inStockNo]]);
        }
       }

    if (StockCL[inStockNo] == 0) {
        if( isNS4 ) {
            CoupImglc[inStockNo].moveTo(50,600);
            CoupImglu[inStockNo].moveTo(ScreenPosX(TrackX[StockL[inStockNo]]),ScreenPosY(TrackY[StockL[inStockNo]]));
        } else {
            CoupImglc[inStockNo].style.left = 50;
            CoupImglc[inStockNo].style.top = 600;

            CoupImglu[inStockNo].style.left = ScreenPosX(TrackX[StockL[inStockNo]]);
            CoupImglu[inStockNo].style.top = ScreenPosY(TrackY[StockL[inStockNo]]);
        }
    } else {
        if( isNS4 ) {
            CoupImglu[inStockNo].moveTo(50,600);
            CoupImglc[inStockNo].moveTo(ScreenPosX(TrackX[StockL[inStockNo]]),ScreenPosY(TrackY[StockL[inStockNo]]));
        } else {
            CoupImglu[inStockNo].style.left = 50;
            CoupImglu[inStockNo].style.top = 600;

            CoupImglc[inStockNo].style.left = ScreenPosX(TrackX[StockL[inStockNo]]);
            CoupImglc[inStockNo].style.top = ScreenPosY(TrackY[StockL[inStockNo]]);
        }
    }
}



function AddToTrain(inStockNo, inDir, inAutoCouple)
//
//  Add piece of stock to beginning of train in current direction.
//  Automatic coupling depened on input parameter(3), otherwise
//  current coupling status is retained.
//
   {var i;
    var intLeadStock;

    if (inDir == 1)
       {intCurrentStock = 1;
        while (StockTR[intCurrentStock] != 0)
           {intCurrentStock = StockTR[intCurrentStock];
           }
        StockTR[intCurrentStock] = inStockNo;
        StockTL[inStockNo]       = intCurrentStock;
        if (inAutoCouple == "Y" )
           {StockCR[intCurrentStock] = inStockNo;
            StockCL[inStockNo]       = intCurrentStock;
            DisplayStock(intCurrentStock);
            DisplayStock(inStockNo);
           }
       }
    else
       {intCurrentStock = 1;
        while (StockTL[intCurrentStock] != 0)
           {intCurrentStock = StockTL[intCurrentStock];
           }
        StockTL[intCurrentStock] = inStockNo;
        StockTR[inStockNo]       = intCurrentStock;
        if (inAutoCouple == "Y" )
           {StockCL[intCurrentStock] = inStockNo;
            StockCR[inStockNo]       = intCurrentStock;
            DisplayStock(intCurrentStock);
            DisplayStock(inStockNo);
           }
       }
   }



function SplitTrain2(inStockNo, inDir)
//
//   Seperate train from this point.
//
   {var intThisStock;

    if (inDir == 1)
       {intThisStock = inStockNo;
        while (intThisStock != 0)
           {StockTL[StockTR[intThisStock]] = 0;
            StockTR[intThisStock]          = 0;
            intThisStock = StockTL[intThisStock];
           }
       }
    else
       {intThisStock = inStockNo;
        while (intThisStock != 0)
           {StockTR[StockTL[intThisStock]] = 0;
            StockTL[intThisStock]          = 0;
            intThisStock = StockTR[intThisStock];
           }
       }
   }



function ChangePoint(inPointNum, inOverRide)
//
//   Change selected point.
//   A test will be made to check that no stock is over the point, if so the
//   change will not be allowed.
//   The above check will not be performed if the second parameter = "Y".

   {
    if( isNS4 ) {
        document.myform.txtWarn.value = "";
    } else {
        document.getElementById("txtWarn").value = "";
    }
    if (inOverRide != "Y" && CheckPointFree(inPointNum, 0) == "N") {
        if( isNS4 ) {
            document.myform.txtWarn.value = "Point is not free";
        } else {
             document.getElementById("txtWarn").value = "Point is not free";
        }
       }
    else
       {
    if (Point[inPointNum] == 0) {
        Point[inPointNum] = 1;
        if( isNS4 ) {
            PointImg1[inPointNum].moveTo(ScreenPosX(TrackX[PointAt[inPointNum]]),ScreenPosY(TrackY[PointAt[inPointNum]]) + PointOffY[inPointNum]);
            PointImg0[inPointNum].moveTo(50,600);
        } else {
            PointImg1[inPointNum].style.left = ScreenPosX(TrackX[PointAt[inPointNum]]);
            PointImg1[inPointNum].style.top  = ScreenPosY(TrackY[PointAt[inPointNum]]) + PointOffY[inPointNum];
            PointImg0[inPointNum].style.left = 50;
            PointImg0[inPointNum].style.top  = 600;
        }
       }
    else {
        Point[inPointNum] = 0;
        if( isNS4 ) {
            PointImg0[inPointNum].moveTo(ScreenPosX(TrackX[PointAt[inPointNum]]),ScreenPosY(TrackY[PointAt[inPointNum]]) + PointOffY[inPointNum]);
            PointImg1[inPointNum].moveTo(50,600);
        } else {
            PointImg0[inPointNum].style.left = ScreenPosX(TrackX[PointAt[inPointNum]]);
            PointImg0[inPointNum].style.top  = ScreenPosY(TrackY[PointAt[inPointNum]]) + PointOffY[inPointNum];
            PointImg1[inPointNum].style.left = 50;
            PointImg1[inPointNum].style.top  = 600;
        }
       }
       }
    LastDir = ThisDir;
   }



function CheckPointFree(inPointNo, inStockNo)
//   Check the selected point is free from stock. If second parameter
//   is passed then excluded that item of stock from the test.
   {var i;
    var strReturn;

    strReturn = 'Y';
    for (i=1 ; i <= MaxStock ; i++)
       {if (i != inStockNo)
          {if (StockL[i] >= PointClrA[inPointNo] && StockL[i] <= PointClrB[inPointNo]) strReturn = 'N';
           if (StockR[i] >= PointClrA[inPointNo] && StockR[i] <= PointClrB[inPointNo]) strReturn = 'N';
           if (StockL[i] >= PointClrC[inPointNo] && StockL[i] <= PointClrD[inPointNo]) strReturn = 'N';
           if (StockR[i] >= PointClrC[inPointNo] && StockR[i] <= PointClrD[inPointNo]) strReturn = 'N';
          }
       }

    return strReturn;
   }



function Uncouple(inUncNo)
//
//  Uncouple any stock coupled over selected uncoupler
//  coding will allow one space either side for flexibility
//
   {var i;
    var intLeft, intRight;
    var intUncLeft, intUncRight;

    intLeft  = Unc[inUncNo];
    intRight = TrackR1[intLeft];
    intUncLeft  = 0;
    intUncRight = 0;

    for (i=1 ; i <= HighStock ; ++i)
       {if (StockL[i] == intLeft || StockL[i] == intRight)
           {if (StockCL[i] != 0)
               {intUncLeft  = i;
                intUncRight = StockCL[i];
                i = HighStock + 1;
               }
           }
        else
           {if (StockR[i] == intLeft || StockR[i] == intRight)
               {if (StockCR[i] != 0)
                   {intUncLeft  = StockCR[i];
                    intUncRight = i;
                    i = HighStock + 1;
                   }
               }
           }
       }

    if (intUncLeft != 0)
       {StockCL[intUncLeft]  = 0;
        StockCR[intUncRight] = 0; 
        DisplayStock(intUncLeft);
        DisplayStock(intUncRight);
        MoveCount = MoveCount + 1;
        if( isNS4 ) {
            document.myform.txtMoveCount.value = MoveCount;
        } else {
             document.getElementById("txtMoveCount").value = MoveCount;
        }
        LastDir = 0;
       }
   }


function ScreenPosX(inX)
   {var intReturn;

//    intReturn = 150 + (inX * 10);
    intReturn = (inX * 10) - 10;
    return intReturn;
   }



function ScreenPosY(inY)
   {var intReturn;

//    intReturn = 20 + (inY * 40);
    intReturn = 180 + (inY * 40);//110
    return intReturn;
   }



function NewTrain()
//
//  Select a new train and display using markers
//
{
    var Loop;
    var ILoop;
    var Rnd;

    for(Loop=1 ; Loop<=8 ; ++Loop)
       {TrainPos[Loop]=0}

    for(Loop=1 ; Loop<=5 ; ++Loop)
        {for (ILoop=1 ; ILoop<=999 ; ++ILoop)
            {Rnd = 1+Math.round(Math.random()*7);
             if (TrainPos[Rnd] == 0)
                {TrainPos[Rnd] = Loop; 
                 ILoop = 1000;
                }
            }
        }

    for(Loop=1 ; Loop<=8 ; ++Loop) {
        if(TrainPos[Loop] ==0) {
            if( isNS4 ) {
                MarkImg[Loop].moveTo(50,600);
//                MarkImg[Loop].style.pixelLeft = 50;
//                MarkImg[Loop].style.pixelTop  = 600;
            } else {
                MarkImg[Loop].style.left = 50;
                MarkImg[Loop].style.top = 600;
//                MarkImg[Loop].style.pixelLeft = 50;
//                MarkImg[Loop].style.pixelTop  = 600;
            }
        } else {
            if( isNS4 ) {
                MarkImg[Loop].moveTo(410 + ((TrainPos[Loop]-1) * 70),220);
//                MarkImg[Loop].style.pixelLeft = 410 + ((TrainPos[Loop]-1) * 70);
//                MarkImg[Loop].style.pixelTop  = 220;
            } else {
                MarkImg[Loop].style.left = 410 + ((TrainPos[Loop]-1) * 70);
                MarkImg[Loop].style.top = 220;
//                MarkImg[Loop].style.pixelLeft = 410 + ((TrainPos[Loop]-1) * 70);
//                MarkImg[Loop].style.pixelTop  = 220;
            }
        }
    }
}

function SetUp()
   {var i;
//
//  Set array high VALUEs
//
HighTrack = 255;
HighPoint = 2;
HighStock = 9;
HighUnc = 3;

//
//  Initialise all arrays
//
    for (i=0 ; i <= HighTrack ; i++)
       {TrackL1[i] = 0;
        TrackR1[i] = 0;
        TrackP[i]  = 0;
        TrackL2[i] = 0;
        TrackR2[i] = 0;
        TrackX[i]  = 0;
        TrackY[i]  = 0;
       }
    for (i=0 ; i <= HighUnc ; i++)
       {Unc[i]   = 0;
       }
    for (i=0 ; i <= HighPoint ; i++)
       {Point[i]     = 0;
        PointAt[i]   = 0;
        PointOffY[i] = 0;
        PointClrA[i] = 0;
        PointClrB[i] = 0;
        PointClrC[i] = 0;
        PointClrD[i] = 0;

       }
    for (i=0 ; i <= HighStock ; i++)
       {StockL[i]  = 0;
        StockR[i]  = 0;
        StockTL[i] = 0;
        StockTR[i] = 0;
        StockCL[i] = 0;
        StockCR[i] = 0;
       }

//  Build track
    for (i=1 ; i <= HighTrack ; i++)
       {if (i >= 1 && i <= 81)    {TrackL1[i] = i-1; TrackR1[i] = i+1; TrackX[i] = i-5;   TrackY[i] = 1};
        if (i >= 83 && i <= 120)  {TrackL1[i] = i-1; TrackR1[i] = i+1; TrackX[i] = i-55;  TrackY[i] = 2};
        if (i >= 121 && i <= 156) {TrackL1[i] = i-1; TrackR1[i] = i+1; TrackX[i] = i-80;  TrackY[i] = 0};
        if (i >= 160 && i <= 170) {TrackL1[i] = i-1; TrackR1[i] = i+1; TrackX[i] = i-94;  TrackY[i] = 2};
       }

    TrackL1[1]   = 0;
    TrackL1[160] = 120;
    

    TrackR1[81]  = 0;
    TrackR1[120] = 160;
    TrackR1[156] = 0;
    TrackR1[170] = 0;
 
    TrackY[83] = 1;
    TrackY[84] = 1.07;
    for (i=85 ; i<=100 ; i++) TrackY[i] = TrackY[i-1] + .05;
    TrackY[101] = 1.95;
    TrackY[102] = 2;

    TrackL2[32] = 31;
    TrackR2[32] = 83;
    TrackL1[83] = 32;

    TrackY[121] = 1;
    TrackY[122] = 0.95;
    for (i=123 ; i<=138 ; i++) TrackY[i] = TrackY[i-1] - .05;
    TrackY[139] = 0.07;
    TrackY[140] = 0;

    TrackL2[45]  = 44;
    TrackR2[45]  = 121;
    TrackL1[121] = 45;

//
//  Set up array locations
//

//  Points
    TrackP[32]   = 1;  PointAt[1]  = 32;
    TrackP[45]  = 2;  PointAt[2]  = 45; PointOffY[2] = -20;

    PointClrA[1] = 32;   PointClrB[1] = 44;  PointClrC[1] = 83; PointClrD[1] = 92;
    PointClrA[2] = 45;  PointClrB[2] = 57;  PointClrC[2] = 121; PointClrD[2] = 130;


// Uncouplers
    Unc[1] = 133;
    Unc[2] = 59;
    Unc[3] = 95;

// Stock
    StockL[1] =  20; StockR[1] =  26;
    StockL[2] =  97; StockR[2] = 103;                   StockCR[2] = 3;
    StockL[3] = 104; StockR[3] = 110;  StockCL[3] = 2;  StockCR[3] = 4; 
    StockL[4] = 111; StockR[4] = 117;  StockCL[4] = 3;  StockCR[4] = 5;
    StockL[5] = 118; StockR[5] = 163;  StockCL[5] = 4;  StockCR[5] = 6;
    StockL[6] = 164; StockR[6] = 170;  StockCL[6] = 5;
    StockL[7] =  61; StockR[7] =  67;                   StockCR[7] = 8;
    StockL[8] =  68; StockR[8] =  74;  StockCL[8] = 7;  StockCR[8] = 9;
    StockL[9] =  75; StockR[9] =  81;  StockCL[9] = 8;

//
//  Build image arrays
//

//  Uncoupler
    if (isNS4){
        UncImg[1] = eval('document.layers["UC1"]');
        UncImg[2] = eval('document.layers["UC2"]');
        UncImg[3] = eval('document.layers["UC3"]');
    } else {
        UncImg[1] = eval('document.getElementById("UC1")');
        UncImg[2] = eval('document.getElementById("UC2")');
        UncImg[3] = eval('document.getElementById("UC3")');
    }

//  Stock
    for (i=1 ; i <= 7 ; i++) {
        if (isNS4){
            StockImg[i]    = eval('document.layers["Stock0' + i + '"]');
            StockImg[i+10]    = eval('document.layers["Stock1' + i + '"]');
            StockImg[i+20]    = eval('document.layers["Stock2' + i + '"]');
            StockImg[i+30]    = eval('document.layers["Stock3' + i + '"]');
            StockImg[i+40]    = eval('document.layers["Stock4' + i + '"]');
            StockImg[i+50]    = eval('document.layers["Stock5' + i + '"]');
            StockImg[i+60]    = eval('document.layers["Stock6' + i + '"]');
            StockImg[i+70]    = eval('document.layers["Stock7' + i + '"]');
            StockImg[i+80]    = eval('document.layers["Stock8' + i + '"]');
        } else {
            StockImg[i]    = eval('document.getElementById("Stock0' + i + '")');
            StockImg[i+10] = eval('document.getElementById("Stock1' + i + '")');
            StockImg[i+20] = eval('document.getElementById("Stock2' + i + '")');
            StockImg[i+30] = eval('document.getElementById("Stock3' + i + '")');
            StockImg[i+40] = eval('document.getElementById("Stock4' + i + '")');
            StockImg[i+50] = eval('document.getElementById("Stock5' + i + '")');
            StockImg[i+60] = eval('document.getElementById("Stock6' + i + '")');
            StockImg[i+70] = eval('document.getElementById("Stock7' + i + '")');
            StockImg[i+80] = eval('document.getElementById("Stock8' + i + '")');
        }
    }

//  Couplings
    for (i=1 ; i <= HighStock ; i++) {
        if (isNS4){
            CoupImglc[i] = eval('document.layers["Couplerlc' + i + '"]');
            CoupImglu[i] = eval('document.layers["Couplerlu' + i + '"]');
            CoupImgrc[i] = eval('document.layers["Couplerrc' + i + '"]');
            CoupImgru[i] = eval('document.layers["Couplerru' + i + '"]');
        } else {
            CoupImglc[i] = eval('document.getElementById("Couplerlc' + i + '")');
            CoupImglu[i] = eval('document.getElementById("Couplerlu' + i + '")');
            CoupImgrc[i] = eval('document.getElementById("Couplerrc' + i + '")');
            CoupImgru[i] = eval('document.getElementById("Couplerru' + i + '")');
        }

    }

//  Points
    for (i=1 ; i <= MaxPoint ; i++) {
        if (isNS4){
            PointImg0[i] = eval('document.layers["Point' + i + '0"]');
            PointImg1[i] = eval('document.layers["Point' + i + '1"]');
        } else {
            PointImg0[i] = eval('document.getElementById("Point' + i + '0")');
            PointImg1[i] = eval('document.getElementById("Point' + i + '1")');
        }
    }

//  Markers
    for (i=1 ; i <= 8 ; i++) {
        if (isNS4){
            MarkImg[i] = eval('document.layers["Mark' + i + '"]');
        } else {
            MarkImg[i] = eval('document.getElementById("Mark' + i + '")');
        }
    }

//
//  Move to screen locations
//

//  Track
    if (isNS4){
        elm = document.layers["Track1"];
        elm.moveTo(ScreenPosX(TrackX[1]),ScreenPosY(TrackY[1]));
        elm = document.layers["Track2"];
        elm.moveTo(ScreenPosX(TrackX[104]),ScreenPosY(TrackY[104]));

        elm = document.layers["Track3"];
        elm.moveTo(ScreenPosX(TrackX[142]),ScreenPosY(TrackY[142]));
        elm = document.layers["TrackP1"];
        elm.moveTo(370,172+70);//172
        elm = document.layers["TrackP2"];
        elm.moveTo(500,180);//110
    } else {
        elm = document.getElementById("Track1");
        elm.style.left = ScreenPosX(TrackX[1]);
        elm.style.top = ScreenPosY(TrackY[1]);

        elm = document.getElementById("Track2");
        elm.style.left = ScreenPosX(TrackX[104]);
        elm.style.top = ScreenPosY(TrackY[104]);

        elm = document.getElementById("Track3");
        elm.style.left = ScreenPosX(TrackX[142]);
        elm.style.top = ScreenPosY(TrackY[142]);

        elm = document.getElementById("TrackP1");
        elm.style.left = 370;
        elm.style.top = 172+70;    //172

        elm = document.getElementById("TrackP2");
        elm.style.left = 500;
        elm.style.top = 180;//110
    }

//  Points
    for (i=1 ; i <= HighPoint ; i++) {
        if (isNS4){
            PointImg0[i].moveTo(ScreenPosX(TrackX[PointAt[i]]),ScreenPosY(TrackY[PointAt[i]]) + PointOffY[i]);
            PointImg1[i].moveTo(50,600);     //400
        } else {
            PointImg0[i].style.left = ScreenPosX(TrackX[PointAt[i]]);
            PointImg0[i].style.top  = ScreenPosY(TrackY[PointAt[i]]) + PointOffY[i];
            PointImg1[i].style.left = 50;
            PointImg1[i].style.top = 600;//400
        }
    }

//  Uncouplers
    for (i=1 ; i <=HighUnc ; i++) {
        if (isNS4){
            UncImg[i].moveTo(ScreenPosX(TrackX[Unc[i]])+5,ScreenPosY(TrackY[Unc[i]])-5);
        } else {
            UncImg[i].style.left = ScreenPosX(TrackX[Unc[i]])+5;
            UncImg[i].style.top  = ScreenPosY(TrackY[Unc[i]])-5;
        }
    }

//  Display stock
    for (i=1 ; i <= HighStock ; i++) {
        DisplayStock(i);
    }

//  Display first train sequence
    NewTrain();
   }

// -->

