// Returns a value depicting how complete this row is ie:
  
var ROWSTATUS_GOOD              = 10;
var ROWSTATUS_NOMATCH           = 20;
var ROWSTATUS_MISSINGVERSION    = 30;
var ROWSTATUS_BAD               = 40;

var ROWSTATUS_MISSINGFEATURE    = 100;
var ROWSTATUS_MISSINGTYPE       = 110;
var ROWSTATUS_MISSINGLOCATION   = 120;
var ROWSTATUS_MISSINGQUANTITY   = 130;



// --------------------------------------------------------------------

// NB - do all frames need to have their own gp_XML or can that be optimised?

/*
 * Display the object passed as an argument(example: display DIV)
 * &param obj - the object to be displayed (id of DIV) 
 * &return boolean
 */

function Toggle (obj){
  if (obj.style.display == ""){
         obj.style.display = "none";
  } else{
         obj.style.display= "";
  }
  return false;
}




function isMozilla()
{
        if (window.ActiveXObject)
        {
                return false;
        }
        else if (document.implementation && document.implementation.createDocument)
        {
                return true;
        }
        else
        {
                alert('Your browser cannot handle this script');
                return false;
        }
}



function getSrcElement(_pEvent)
// NB: Taken from http://www.w3schools.com/js/tryit.asp?filename=try_dom_event_srcelement (but cleaned up!)
{
        var pElement    = null;
        var pEvent      = _pEvent;

        if (pEvent!='undefined')   {
          if (!pEvent)
          {
                  pEvent = window.event;
          }

          if (pEvent.target)
          {
                  pElement = pEvent.target;
          }
          else if (pEvent.srcElement)
          {
                  pElement = pEvent.srcElement;
          }

          if (pElement.nodeType == 3) // defeat Safari bug
          {
                  pElement = pElement.parentNode;
          }
        }


        return pElement;
}


function buildTagTree(_pThis, _iLevel, _iNodeIndex, _sTree)
{
        var sTagTree = "";
        var li_Index = 0;
        

        if (_sTree)
        {
                sTagTree = _sTree;
        }
        
        if (!_iLevel)
        {
                _iLevel = 1;
        }

        if (!_iNodeIndex)
        {
                _iNodeIndex = 0;
        }

        sTagTree += "Tag tree ";
        
        for (li_Index = 0; li_Index < (_iLevel - 1); li_Index++)
        {
                sTagTree += "--->";
        }
        
        sTagTree += "-" + _iNodeIndex + "->";

        sTagTree += _pThis.id;
        
        if (_pThis.tagName)
        {
                sTagTree += "(" + _pThis.tagName + ")";
        }
        
        sTagTree += "=" + _pThis.innerText + "\n";

        for (li_Index = 0; li_Index < _pThis.childNodes.length; li_Index++)
        {
                sTagTree = buildTagTree(_pThis.childNodes[li_Index], _iLevel + 1, li_Index, sTagTree);
        }
        
        return sTagTree;
}


function getMessage(_sMsgID)
{
        var sMsg        = "(undefined error)";
        var pFrame      = frames["SummaryArea"];
        
        if (pFrame)
        {
                var pMessage    = pFrame.document.getElementById(_sMsgID);

                if (pMessage)
                {
                        sMsg    = pMessage.value;
                        sMsg    = sMsg.replace(/_br_/g, "\n");
                }

        }

        return sMsg;    
}
        

/*
==============================================================================================
SessionHandler Object:
----------------------
Holds user login info, also the session log (a bit like the LogFile except there are
timestamps here and much less detail).
*/

function SessionHandler(_iUserGroup, _iUserID)
{
//      alert("SessionHandler START");
// properties
        this.iUserGroup                 = _iUserGroup;
        this.iUserID                    = _iUserID;
//      this.sLoginTimestamp    = new Date();
        this.sSessionLog                = "";

// methods
        this.register                   = SessionHandler_Register;

        // make first entry in session log
        this.register("Initialised");

//      alert("SessionHandler END");
}


function SessionHandler_Register(_sAction)
{
//      alert("SessionHandler_Register START");
        this.sSessionLog += new Date().toString() + "  " + _sAction + "\n";
//      alert("SessionHandler_Register END");
}

/*
==============================================================================================
*/






/*
==============================================================================================
LogHandler Object:
----------------------
Handles all log output, from error messages to full trace.  Each function / code block counts
as a Module, and debugging / logging can be turned on and off at a global level or for each
Module separately.
Messages are given a log level and
module name in the actual code, and these are checked against an array of Modules

*/

var LOG_NONE            = 0;
var LOG_CRITICAL        = 1;
var LOG_ERROR           = 2;
var LOG_WARNING         = 3;
var LOG_TRACE           = 4;
var LOG_DETAIL          = 5;

function LogHandler()
{
// properties
        this.aModuleStatus              = new Array();
        this.aModuleOutputs             = new Array();
        this.sOutputAtStatus    = LOG_ERROR;
        this.sAlertAtStatus             = LOG_CRITICAL;
        this.sDefaultOutput             = "LOG INTERNAL";
        this.sLogFile                   = "";
        
// methods
        this.init                       = LogHandler_initialise;
        this.setLogStatus               = LogHandler_setLogStatus;
        this.log                        = LogHandler_log;
        this.logToLogFile               = LogHandler_logToLogFile;
        this.logToFrame                 = LogHandler_logToFrame;
        this.logToAlert                 = LogHandler_logToAlert;
        this.logToDB                    = LogHandler_logToDB;
        this.setDefaults                = LogHandler_setDefaults;
        
//      alert("Declared LogHandler - about to initialise");
        this.init();
}

function LogHandler_initialise()
{
//      alert("Initialising to User Group " + window.top.gpSession.iUserGroup);
        switch (window.top.gpSession.iUserGroup)
        {
                case 0:
                // Not Logged In
                        this.setDefaults(LOG_CRITICAL, LOG_CRITICAL, "LOG INTERNAL");
                        break;
                        
                case 1:
                // Field Rep
                        this.setDefaults(LOG_ERROR, LOG_CRITICAL, "LOG INTERNAL");
                        break;
                        
                case 2:
                // Casual Tester
                        this.setDefaults(LOG_WARNING, LOG_ERROR, "LOG INTERNAL");
                        break;
                        
                case 3:
                // Project Manager
                        this.setDefaults(LOG_WARNING, LOG_ERROR, "LOG INTERNAL");
                        break;
                        
                case 4:
                // Admin
                        this.setDefaults(LOG_WARNING, LOG_CRITICAL, "LOG INTERNAL");
                        break;
                        
                case 5:
                // Executive
                        this.setDefaults(LOG_WARNING, LOG_CRITICAL, "LOG INTERNAL");
                        break;
                        
                case 6:
                // Super Tester
                        this.setDefaults(LOG_TRACE, LOG_WARNING, "LOG TO WINDOW");
                        break;
                        
                case 7:
                // Programmer
                        this.setDefaults(LOG_WARNING, LOG_WARNING, "LOG TO WINDOW");
                        break;

                case 8:
                // Mystery Shop Admin
                        this.setDefaults(LOG_WARNING, LOG_CRITICAL, "LOG INTERNAL");
                        break;
                        
                default:
                        this.log(LOG_ERROR, "Unknown User Group: " + window.top.gpSession.iUserGroup, "LogHandler");
        }
}

function LogHandler_setLogStatus(_sModule, _iStatus, _sOutput)
{
        if (_sModule)
        {
                this.aModuleStatus[_sModule]    = _iStatus;
                this.aModuleOutputs[_sModule]   = _sOutput;

                alert("Set Module " + _sModule + " to Status " + _iStatus + ", Output " + _sOutput);
        }
}

function LogHandler_deleteLogStatus()
{
        this.aModuleStatus[_sModule]    = null;
        this.aModuleOutputs[_sModule]   = null;
}

function LogHandler_setDefaults(_sOutputAtStatus, _sAlertAtStatus, _sDefaultOutput)
{
        this.sOutputAtStatus    = _sOutputAtStatus;
        this.sAlertAtStatus             = _sAlertAtStatus;
        this.sDefaultOutput             = _sDefaultOutput;
}

function LogHandler_logToLogFile(_iLogType, _sMsg, _sModule)
{
        var sLogLine = _iLogType + " - " + _sModule + ": " + _sMsg;
        this.sLogFile += sLogLine;
}

function LogHandler_clearLogFile()
{
        this.sLogFile = "";
}

function LogHandler_logToFrame(_iLogType, _sMsg, _sModule)
{
//      alert("Logging  to logWindow");
        window.top.logToLogWindow(_iLogType, _sMsg, _sModule);
}

function LogHandler_logToAlert(_iLogType, _sMsg, _sModule)
{
        var sLogLine = _iLogType + " - " + _sModule + ": " + _sMsg;
        alert(sLogLine);
}

function LogHandler_logToDB(_iLogType, _sMsg, _sModule)
{
}

function LogHandler_log(_iLogType, _sMsg, _sModule)
{
//      alert("LogHandler_log START - " + _iLogType + ", " + _sMsg + ", " +_sModule);
        var iModuleOutputAtStatus       = this.sOutputAtStatus;

        if (this.aModuleStatus[_sModule])
        {
                iModuleOutputAtStatus = this.aModuleStatus[_sModule];
        }

//      alert("LogHandler_log iModuleOutputAtStatus = " + iModuleOutputAtStatus + ", _iLogType = " + _iLogType);

        if (iModuleOutputAtStatus >= _iLogType)
        {
                var sModuleOutput       = this.sDefaultOutput;

//              alert("LogHandler_log A sModuleOutput = " + sModuleOutput);

                if (this.aModuleOutputs[_sModule])
                {
                        sModuleOutput = this.aModuleOutputs[_sModule];
                }
                
//              alert("LogHandler_log B sModuleOutput = " + sModuleOutput);

                switch (sModuleOutput)
                {
                        case "LOG INTERNAL":
                                this.logToLogFile(_iLogType, _sMsg, _sModule);
                                break;
                        
                        case "LOG TO WINDOW":
                                this.logToLogFile(_iLogType, _sMsg, _sModule);
                                this.logToFrame(_iLogType, _sMsg, _sModule);
                                break;

                        case "LOG TO ALERT":
                                this.logToLogFile(_iLogType, _sMsg, _sModule);
                                this.logToAlert(_iLogType, _sMsg, _sModule);
                                break;
                                
                        case "LOG TO DB":
                                this.logToLogFile(_iLogType, _sMsg, _sModule);
                                this.logToDB(_iLogType, _sMsg, _sModule);
                                break;
                        
                        default:
                                alert("Unknown Module Output - " + sModuleOutput);
                                // do nothing
                }
                
        }
//      alert("LogHandler_log DONE - " + _iLogType + ", " + _sMsg + ", " +_sModule);
}

function log(_iLogType, _sModuleName, _sMsg)
{
//      alert("Logging: " + _iLogType + ", " + _sModuleName + ", " +_sMsg);
        window.top.gpLog.log(_iLogType, _sModuleName, _sMsg);
}

// --------------------------------------------------------------------

function RowFactory(_pDocument)
{
        this.aRows              = new Array();
        this.pDocument          = _pDocument;

        this.getRow             = RowFactory_getRow;
        this.getRowCount        = RowFactory_getRowCount;
        this.loadRows           = RowFactory_loadRows;
        this.createNewRow       = RowFactory_createNewRow;
}

function RowFactory_getRow(pi_RowIndex)
{
        if (pi_RowIndex >= this.aRows.length)
        {
                if (!this.createNewRow())
                {
                        //break;
                }
        }
        
        return this.aRows[pi_RowIndex];
}

function RowFactory_getRowCount()
{
        return this.aRows.length;
}

function RowFactory_loadRows()
{
        var pNextRow    = null;
        var li_RowIndex = 0;
        
        while (pNextRow = this.pDocument.getElementById("ScreenRow_" + li_RowIndex))
        {
                this.aRows[li_RowIndex] = pNextRow;
                li_RowIndex++;
        }

//      alert("Added " + li_RowIndex + " rows");
}

function RowFactory_createNewRow()
{
        alert("Maximum allowed rows!")
        return false;
}

// --------------------------------------------------------------------

function DataList(_iJobSegmentID, _iScreeningDetailID, _iCinemaID)
{
//      alert("Creating DataList, _iJobSegmentID = " + _iJobSegmentID + ", _iScreeningDetailID = " + _iScreeningDetailID + ", _iCinemaID = " + _iCinemaID);

        this.aDataRows                  = new Array();
        this.aScreenRows                = new Array();
        this.iJobSegmentID              = _iJobSegmentID;
        this.iScreeningDetailID = _iScreeningDetailID;
        this.iCinemaID                  = _iCinemaID;
        this.bHasChanged                = false;

        this.showInDetailsFrame         = DataList_showInDetailsFrame;
        this.addRow                     = DataList_addRow;
        this.addRowFromObject           = DataList_addRowFromObject;
        this.setRow                     = DataList_setRow;
        this.getRowCount                = DataList_getRowCount
        this.swapRows                   = DataList_swapRows;
        this.moveRowUp                  = DataList_moveRowUp;
        this.moveRowDown                = DataList_moveRowDown;
        this.addEmptyRow                = DataList_addEmptyRow;
        this.renderList                 = DataList_renderList;
        this.renderRow                  = DataList_renderRow;
        this.deleteRow                  = DataList_deleteRow;
        this.insertRow                  = DataList_insertRow;
        this.submit                             = DataList_submit;
        this.hasChanged                 = DataList_hasChanged;
        this.validateForSubmit  = DataList_validateForSubmit;
        this.explainStatus              = DataList_explainStatus;
        this.explainStatusFR              = DataList_explainStatusFR;
}

function DataList_validateForSubmit()
{
        var bReturn             = true;

        var pRow                = null;
        var iGeneralStatus      = 0;
        
        for(var iIndex = 0; iIndex < this.aDataRows.length ; iIndex++)
        {
                pRow = this.aDataRows[iIndex];
                
                switch (pRow.getStatus())
                {
                        case ROWSTATUS_NOMATCH:
                                if (iGeneralStatus < ROWSTATUS_NOMATCH)
                                {
                                        iGeneralStatus = ROWSTATUS_NOMATCH;
                                }
                                break;

                        case ROWSTATUS_MISSINGVERSION:
                                if (iGeneralStatus < ROWSTATUS_MISSINGVERSION)
                                {
                                        iGeneralStatus = ROWSTATUS_MISSINGVERSION;
                                }
                                break;

                        case ROWSTATUS_BAD:
                                if (iGeneralStatus < ROWSTATUS_BAD)
                                {
                                        iGeneralStatus = ROWSTATUS_BAD;
                                }
                                break;

                        default:
                                if (iGeneralStatus < ROWSTATUS_GOOD)
                                {
                                        iGeneralStatus = ROWSTATUS_GOOD;
                                }
                }
        }
        
        switch (iGeneralStatus)
        {
                case 0:
                        var sMsg = getMessage("hidMsg_NoEntries");
                        // var sMsg = "You have not entered any items for the Pre Show Programme - is this correct? (NB - you will be able to come back to this screen to add them later). \n\n Click OK to continue, or CANCEL to stay on this screen."
                        
                        if (!confirm(sMsg))
                        {
                                bReturn = false;
                        }
                        break;

                case ROWSTATUS_NOMATCH:
                        var sMsg = getMessage("hidMsg_TrailersUnmatched");
                        // var sMsg = "Some of the trailers you have entered have not been matched up with a title in our database (look for the amber colour in the Status column)- are you SURE they cannot be matched? \n\n Click OK to continue, or CANCEL to stay on this screen."
                        
                        if (!confirm(sMsg))
                        {
                                bReturn = false;
                        }
                        break;

                case ROWSTATUS_MISSINGVERSION:
                        var sMsg = getMessage("hidMsg_MissingData");
                        // var sMsg = "Some of the items you have entered are missing some data (look for the red colour in the Status column)."
                        alert(sMsg);
                        bReturn = false;

                        break;

                case ROWSTATUS_BAD:
                        var sMsg = getMessage("hidMsg_MissingData");
                        // var sMsg = "Some of the items you have entered are missing some data (look for the red colour in the Status column)."
                        alert(sMsg);
                        bReturn = false;

                        break;

                default:
                        bReturn = true;
        }
        
        return bReturn;
}

function DataList_hasChanged()
{
        return this.bHasChanged;
}

function DataList_submit()
{
        if ((gi_SelectedRow) && (gi_SelectedRow < this.aDataRows.length))
        {
                if (!this.aDataRows[gi_SelectedRow].isEmpty())
                {
                        updateSelectedRow();
                }
        }
        
        frames["SummaryArea"].document.getElementById("hidRowCount").value = this.getRowCount();

        if(this.validateForSubmit())
        {
                frames["SummaryArea"].document.getElementById("frmMain").submit();
        }
}

function DataList_showInDetailsFrame(pi_RowIndex)
{
        log(LOG_DETAIL, "DataList_showInDetailsFrame START", "DataList");
        var pCurrent = this.aDataRows[pi_RowIndex];

        log(LOG_DETAIL, "DataList_showInDetailsFrame 1", "DataList");
        frames["DetailsArea"].showDetails(pi_RowIndex + 1, pCurrent);

        log(LOG_DETAIL, "DataList_showInDetailsFrame DONE", "DataList");
}

function DataList_addRow(ps_Type, ps_FirstValue, pb_FirstValueMatched, pi_MatchID, ps_SecondValue, ps_StartTime, ps_Full )
{
//      alert("this.aDataRows.length = " + this.aDataRows.length + ", ps_FirstValue = " + ps_FirstValue);
        this.bHasChanged = true;

        this.aDataRows[this.aDataRows.length] = new DataRow(ps_Type, ps_FirstValue, pb_FirstValueMatched, pi_MatchID, ps_SecondValue, ps_StartTime, ps_Full );
//      alert("Added.");
}

function DataList_addRowFromObject(po_CurrentRow)
{
//      alert("this.aDataRows.length = " + this.aDataRows.length + ", ps_Title = " + ps_Title);
        this.bHasChanged = true;

        this.aDataRows[this.aDataRows.length] = po_CurrentRow;
}

function DataList_setRow(pi_RowIndex, po_CurrentRow)
{
//      alert("Set Row, " + pi_RowIndex + ", " + po_CurrentRow);
        if(pi_RowIndex < this.aDataRows.length)
        {
//              alert("Setting row " + pi_RowIndex + " to title " + po_CurrentRow.getTrailerTitle());
                this.aDataRows[pi_RowIndex] = po_CurrentRow;
//              alert("Setting row " + pi_RowIndex + " to title " + this.aDataRows[pi_RowIndex].getTrailerTitle());

                this.renderRow(pi_RowIndex);
        }
        else
        {
                if (!po_CurrentRow.isEmpty())
                {
                        this.addRowFromObject(po_CurrentRow);
                        this.renderRow(pi_RowIndex);
                        this.addEmptyRow(true);
                        
                        frames["DetailsArea"].resetFocus();
                }
        }
}

function DataList_getRowCount()
{
        return this.aDataRows.length;
}

function DataList_swapRows(pi_RowIndex1, pi_RowIndex2)
{
        this.bHasChanged = true;

        var pTemp                       = this.aDataRows[pi_RowIndex1];
        this.aDataRows[pi_RowIndex1]    = this.aDataRows[pi_RowIndex2];
        this.aDataRows[pi_RowIndex2]    = pTemp;
}

function DataList_moveRowUp(pi_RowIndex)
{
        if (pi_RowIndex > 0)
        {
                this.swapRows(pi_RowIndex, pi_RowIndex - 1);
                return true;
        }
        else
        {
                return false;
        }
}

function DataList_moveRowDown(pi_RowIndex)
{
        if (pi_RowIndex < this.aDataRows.length - 1)
        {
                this.swapRows(pi_RowIndex, pi_RowIndex + 1);
                return true;
        }
        else
        {
                return false;
        }
}

function DataList_addEmptyRow(_bSelected)
{
//      alert("Adding empty row: _bSelected = " + _bSelected);

        this.bHasChanged = true;
        
        var pRowFactory                                         = frames["SummaryArea"].gRowFactory;
        this.aScreenRows[this.aDataRows.length]                 = pRowFactory.getRow(this.aDataRows.length);
        
        if (_bSelected)
        {
                if (gi_SelectedRow != -1)
                {
                        this.aScreenRows[gi_SelectedRow].className      = "UnHighlightedRow"
                }
                
                gi_SelectedRow = this.aDataRows.length;
                this.aScreenRows[this.aDataRows.length].className       = "SelectedRow"
        }
        else
        {
                this.aScreenRows[this.aDataRows.length].className       = "UnHighlightedRow"
        }
        
        this.renderRow(this.aDataRows.length);
}

function DataList_renderList()
{
        log(LOG_DETAIL, "DataList_renderList: this.aDataRows.length = " + this.aDataRows.length, "DataList");

        var pRowFactory         = frames["SummaryArea"].gRowFactory;
        var pScreenRow          = null;
        var bSomethingSelected  = false;
        
//      alert("this.iScreeningDetailID = " + this.iScreeningDetailID);
        var pField = pRowFactory.pDocument.getElementById("hidScreeningDetailID");
        pField.value = this.iScreeningDetailID;

//      alert("this.iJobSegmentID = " + this.iJobSegmentID);
        pField = pRowFactory.pDocument.getElementById("hidJobSegmentID");
        pField.value = this.iJobSegmentID;

        for (var li_RowIndex = 0; li_RowIndex < this.aDataRows.length; li_RowIndex++)
        {
                // get next available Screen Row
                this.aScreenRows[li_RowIndex]           = pRowFactory.getRow(li_RowIndex);

                if (gi_SelectedRow == li_RowIndex)
                {
                        this.aScreenRows[li_RowIndex].className = "SelectedRow"
                        bSomethingSelected = true;
                }
                else
                {
                        this.aScreenRows[li_RowIndex].className = "UnHighlightedRow"
                }
                this.renderRow(li_RowIndex);
        }
        
        if (bSomethingSelected)
        {
                this.addEmptyRow(false);
        }
        else
        {
                this.addEmptyRow(true);
        }

//      alert("pRowFactory.pDocument.removeLoadingDiv");
        frames["SummaryArea"].removeLoadingDiv();
}

function DataList_renderRow(pi_RowIndex)
{


        var pRowFactory = frames["SummaryArea"].gRowFactory;
        var pScreenRow  = this.aScreenRows[pi_RowIndex];
        
        // fill out fields in Screen Row

        if (isMozilla())
        {
//              alert ("DataList_renderRow Mozilla 0");
                log(LOG_DETAIL, "DataList_renderRow MZ: buildTagTree = " + buildTagTree(pScreenRow), "DataList");

                pScreenRow.childNodes[1].childNodes[0].data = pi_RowIndex + 1;

//              alert ("DataList_renderRow Mozilla 0.5");

                if (this.aDataRows[pi_RowIndex])
                {
//                      alert ("DataList_renderRow Mozilla 1");
                        
                        if (this.aDataRows[pi_RowIndex].sType == "-")
                        {
                                pScreenRow.childNodes[3].className = "HeaderTypeNone";
                        }
                        else
                        {
                                pScreenRow.childNodes[3].className = "HeaderType" + this.aDataRows[pi_RowIndex].sType;
                        }
                        
                        pScreenRow.childNodes[3].childNodes[0].data = this.aDataRows[pi_RowIndex].sType;
                        pScreenRow.childNodes[5].childNodes[0].data = this.aDataRows[pi_RowIndex].getDescriptionString();
                }
                else
                {
                        pScreenRow.childNodes[3].childNodes[0].data = "";
                        pScreenRow.childNodes[5].childNodes[0].data = "";
                }
        }
        else
        {
//              alert ("DataList_renderRow IE 0");
                log(LOG_DETAIL, "DataList_renderRow IE: buildTagTree = " + buildTagTree(pScreenRow), "DataList");

                pScreenRow.childNodes[0].innerText = pi_RowIndex + 1;

                if (this.aDataRows[pi_RowIndex])
                {
                        if (this.aDataRows[pi_RowIndex].sType == "-")
                        {
                                pScreenRow.childNodes[1].className = "HeaderTypeNone";
                        }
                        else
                        {
                                pScreenRow.childNodes[1].className = "HeaderType" + this.aDataRows[pi_RowIndex].sType;
                        }

                        pScreenRow.childNodes[1].innerText = this.aDataRows[pi_RowIndex].sType;
                        pScreenRow.childNodes[2].innerText = this.aDataRows[pi_RowIndex].getDescriptionString();

//              document.getElementById("lb_DataFlag_")

                }
                else
                {
                        pScreenRow.childNodes[1].innerText = "";
                        pScreenRow.childNodes[2].innerText = "";
                }
        }
        
        // Update status icon:
        // -------------------
        var pImgStatus          = pRowFactory.pDocument.getElementById("ImgStatus" + pi_RowIndex);
        var pImgAdd             = pRowFactory.pDocument.getElementById("ImgAdd" + pi_RowIndex);
        var pImgDelete          = pRowFactory.pDocument.getElementById("ImgDelete" + pi_RowIndex);
        var pStatusCell         = pRowFactory.pDocument.getElementById("CellStatus" + pi_RowIndex);
        var sStatusClass        = "";
        
//      alert("pi_RowIndex = " + pi_RowIndex);
//      alert("this.aDataRows[pi_RowIndex] = " + this.aDataRows[pi_RowIndex]);

        if (this.aDataRows[pi_RowIndex])
        {
                switch (this.aDataRows[pi_RowIndex].getStatus())
                {
                        case ROWSTATUS_GOOD:
                                pImgStatus.src  = "Img/tick.png";
                                sStatusClass    = "CellStatus_Good";
                                break;

                        case ROWSTATUS_NOMATCH:
                                pImgStatus.src  = "Img/film_error.png";
                                sStatusClass    = "CellStatus_Unmatched";
                                break;

                        case ROWSTATUS_MISSINGVERSION:
                                pImgStatus.src  = "Img/note_add.png";
                                sStatusClass    = "CellStatus_Missing";
                                break;

                        case ROWSTATUS_BAD:
                                pImgStatus.src  = "Img/cross.png";
                                sStatusClass    = "CellStatus_Bad";
                                break;

                        default:
                }

                pImgAdd.src                             = "Img/add.png";
                pImgDelete.src                  = "Img/delete.png";
                pStatusCell.className   = sStatusClass;


                // Set form fields:
                // ----------------

/*
                this.getStatus                  = DataRow_getStatus;
                this.getTrailerTitle    = DataRow_getTrailerTitle;
                this.getTrailerVersion  = DataRow_getTrailerVersion;
                this.getAdvertiser              = DataRow_getAdvertiser;
                this.getProduct                 = DataRow_getProduct;
                this.getIdent                   = DataRow_getIdent;
*/

//              alert("pi_RowIndex = " + pi_RowIndex + ", this.aDataRows[pi_RowIndex] = " + this.aDataRows[pi_RowIndex]);

                pRowFactory.pDocument.getElementById("lb_DataFlag_" + pi_RowIndex).value = "TRUE";
                pRowFactory.pDocument.getElementById("ls_Sequence_" + pi_RowIndex).value = (pi_RowIndex + 1);
                pRowFactory.pDocument.getElementById("ls_Type_" + pi_RowIndex).value = this.aDataRows[pi_RowIndex].sType;
                pRowFactory.pDocument.getElementById("ls_Title_" + pi_RowIndex).value = this.aDataRows[pi_RowIndex].sFirstValue;
                pRowFactory.pDocument.getElementById("lb_TitleMatched_" + pi_RowIndex).value = this.aDataRows[pi_RowIndex].bFirstValueMatched;
                pRowFactory.pDocument.getElementById("li_MatchID_" + pi_RowIndex).value = this.aDataRows[pi_RowIndex].iMatchID;
                pRowFactory.pDocument.getElementById("ls_Version_" + pi_RowIndex).value = this.aDataRows[pi_RowIndex].sSecondValue;
                pRowFactory.pDocument.getElementById("ls_StartTime_" + pi_RowIndex).value = this.aDataRows[pi_RowIndex].tStartTime;
                pRowFactory.pDocument.getElementById("ls_Full_" + pi_RowIndex).value = this.aDataRows[pi_RowIndex].sFull;

        }
        else
        {
                pImgStatus.src                  = "Img/transpixel.gif";
                pImgAdd.src                             = "Img/transpixel.gif";
                pImgDelete.src                  = "Img/transpixel.gif";
                pStatusCell.className   = "CellStatus";
        }
}

function DataList_deleteRow(pi_RowIndex)
{
        this.bHasChanged = true;
        
        for (var li_RowIndex = pi_RowIndex; li_RowIndex < (this.aDataRows.length - 1); li_RowIndex++)
        {
                this.aDataRows[li_RowIndex] = this.aDataRows[li_RowIndex + 1];
        }
        
        this.aDataRows.length = this.aDataRows.length - 1;
        this.aScreenRows.length = this.aScreenRows.length - 1;
}

function DataList_insertRow(pi_RowIndex)
{
        this.bHasChanged = true;
        
        for (var li_RowIndex = (this.aDataRows.length - 1); li_RowIndex >= pi_RowIndex; li_RowIndex--)
        {
                this.aDataRows[li_RowIndex + 1] = this.aDataRows[li_RowIndex];
        }
        
        this.aDataRows[pi_RowIndex] = new DataRow("", "", false, -1, "");
        setSelectedRow(pi_RowIndex);
        this.renderList();
}

function DataList_explainStatus(pi_RowIndex)
{
        var sMsg = "";
        
        if (!pi_RowIndex)
        {
                pi_RowIndex = getSelectedRow();
        }
                
        switch (this.aDataRows[pi_RowIndex].getStatus())
        {
                case ROWSTATUS_GOOD:
                        sMsg = "This entry is fine.";
                        break;

                case ROWSTATUS_NOMATCH:
                        sMsg = "This entry does not match up with anything in our database - it may still be correct, but you should check to see if there is a match in our database (perhaps spelled slightly differently).";
                        break;

                case ROWSTATUS_MISSINGVERSION:
                        sMsg = "This entry is not complete - you must enter a value into the second text (marked Product, or Version).";
                        break;

                case ROWSTATUS_BAD:
                        sMsg = "This entry is not complete - you must enter a value into the textbox.";
                        break;
        }
        
        alert(sMsg);
}

function DataList_explainStatusFR(pi_RowIndex)
{
        var sMsg = "";
        
        if (!pi_RowIndex)
        {
                pi_RowIndex = getSelectedRow();
        }

	switch (this.aDataRows[pi_RowIndex].getStatus())
	{
                case ROWSTATUS_GOOD:
                        sMsg = "Cette entrée est valide.";
                        break;

                case ROWSTATUS_NOMATCH:
                        sMsg = "Cette entrée ne correspond avec aucun titre dans notre base de données - cela pourrait être correcte, mais veuillez vérifier dans la liste pour en être sûr (car il est possible qu’il soit écrit différemment).";
                        break;

                case ROWSTATUS_MISSINGVERSION:
                        sMsg = "Cette entrée est incomplète - vous devez indiquer une valeur dans la deuxième boîte de texte (marquée Produit, ou Version)";
                        break;

                case ROWSTATUS_BAD:
                        sMsg = "Cette entrée est incomplète - vous devez indiquer une valeur dans la boîte de texte.";
                        break;
        }

        
        alert(sMsg);
}



// --------------------------------------------------------------------

function DataRow( ps_Type, ps_FirstValue, pb_FirstValueMatched, pi_MatchID, ps_SecondValue, pt_StartTimeValue, ps_Full )
{

// Properties:
// -----------

        this.sType                              = ps_Type;
        this.sFirstValue                        = ps_FirstValue;
        this.bFirstValueMatched                 = pb_FirstValueMatched;
        this.iMatchID                           = pi_MatchID;
        this.sSecondValue                       = ps_SecondValue;
        this.tStartTime                         = pt_StartTimeValue;
        this.sFull                              = ps_Full;


// Methods:
// -----------

        this.isEmpty                            = DataRow_isEmpty;
        this.getStatus                          = DataRow_getStatus;

        this.getTrailerTitle            = DataRow_getTrailerTitle;
        this.setTrailerTitle            = DataRow_setTrailerTitle;

        this.getTrailerVersion          = DataRow_getTrailerVersion;
        this.setTrailerVersion          = DataRow_setTrailerVersion;

        this.getMatchID                         = DataRow_getMatchID;
        this.setMatchID                         = DataRow_setMatchID;

        this.getAdvertiser                      = DataRow_getAdvertiser;
        this.setAdvertiser                      = DataRow_setAdvertiser;

        this.getProduct                         = DataRow_getProduct;
        this.setProduct                         = DataRow_setProduct;

        this.getIdent                           = DataRow_getIdent;
        this.setIdent                           = DataRow_setIdent;

        this.getSpecialTitle            = DataRow_getSpecialTitle;
        this.setSpecialTitle            = DataRow_setSpecialTitle;

        this.getStartTime               = DataRow_getStartTime;
        this.setStartTime               = DataRow_setStartTime;

        this.getFull                    = DataRow_getFull;
        this.setFull                    = DataRow_setFull;

        this.getDescriptionString       = DataRow_getDescriptionString;
//      this.getCompleted                       = DataRow_getCompleted;



}

/*
function DataRow_getCompleted()
{
        var bNeedSecondValue    = false;
        var bNotStarted         = false;
        
        switch (this.sType)
        {
                case "Advert":
                        bNeedSecondValue = true;
                        break;

                case "Trailer":
                        bNeedSecondValue = false;
                        // NB - will change if Trailer Versions are needed
                        break;

                case "Logo":
                        bNeedSecondValue = false;
                        break;

                default:
                        bNeedSecondValue = false;
                        bNotStarted = true;
                        break;
        }
        
        if (bNotStarted)
        {
                return false;
        }
        else
        {
                if (sFirstValue
        }
}
*/

function DataRow_getMatchID()
{
        return this.iMatchID;
}

function DataRow_setMatchID(pi_MatchID)
{
        this.iMatchID = pi_MatchID;
}

function DataRow_isEmpty()
{
        var bReturn = true;
        
        if (this.getTrailerTitle())
        {
                bReturn = false;
        }
        
        return bReturn;
}

function DataRow_getStatus()
{
        var iReturn = ROWSTATUS_GOOD;
        
        switch (this.sType)
        {
                case "-":
                        iReturn = ROWSTATUS_BAD;
                        break;
                        
                case "Trailer":
                        if (this.sFirstValue)
                        {
                                if (!this.bFirstValueMatched)
                                {
                                        iReturn = ROWSTATUS_NOMATCH;
                                }
                                else
                                {
                                        iReturn = ROWSTATUS_GOOD;
                                }
                        }
                        else
                        {
                                iReturn = ROWSTATUS_BAD;
                        }
                        break;
                        
                case "Advert":
                        if (this.sFirstValue)
                        {
//                              if (!this.bFirstValueMatched)
//                              {
//                                      iReturn = ROWSTATUS_NOMATCH;
//                              }
//                              else
//                              {
                                        if (this.sSecondValue)
                                        {
                                                iReturn = ROWSTATUS_GOOD;
                                        }
                                        else
                                        {
                                                iReturn = ROWSTATUS_MISSINGVERSION;
                                        }
//                              }
                        }
                        else
                        {
                                iReturn = ROWSTATUS_BAD;
                        }
                        break;
                        
                case "Logo":
                        if (this.sFirstValue)
                        {
                                iReturn = ROWSTATUS_GOOD;
                        }
                        else
                        {
                                iReturn = ROWSTATUS_BAD;
                        }
                        break;
                        
                case "Special":
                        if (this.sFirstValue)
                        {
                                if (!this.bFirstValueMatched)
                                {
                                        iReturn = ROWSTATUS_NOMATCH;
                                }
                                else
                                {
                                        iReturn = ROWSTATUS_GOOD;
                                }
                        }
                        else
                        {
                                iReturn = ROWSTATUS_BAD;
                        }
                        break;
                        
                default:
                
        }
        
        return iReturn;
}

function DataRow_getDescriptionString()
{
        var sDescription = "";

        if (window.LI_CheckTypeID=='9') {
              switch (this.sType.toUpperCase())
              {
                      case "TRAILER":
                              sDescription = this.sFirstValue + " - " + this.sSecondValue+" - st:"+this.tStartTime+" - Full:"+this.sFull;
                              break;

                      case "ADVERT":
                              sDescription = this.sFirstValue + " - " + this.sSecondValue+" - st:"+this.tStartTime+" - Full:"+this.sFull ;
                              break;

                      case "SPECIAL":
                              sDescription = this.sFirstValue + " - " + this.sSecondValue+" - st:"+this.tStartTime+" - Full:"+this.sFull;
                              break;

                      default:
                              sDescription = this.sFirstValue + " - " + this.sSecondValue+" - st:"+this.tStartTime+" - Full:"+this.sFull;
              }
        } else {
              switch (this.sType.toUpperCase())
              {
                      case "TRAILER":
                              sDescription = this.sFirstValue + " - " + this.sSecondValue;
                              break;

                      case "ADVERT":
                              sDescription = this.sFirstValue + " - " + this.sSecondValue;
                              break;

                      case "SPECIAL":
                              sDescription = this.sFirstValue + " - " + this.sSecondValue;
                              break;

                      default:
                              sDescription = this.sFirstValue + " - " + this.sSecondValue;
              }

        }

        return sDescription;
}

function DataRow_getTrailerTitle()
{
        log(LOG_DETAIL, "DataRow_getTrailerTitle: " + this.sFirstValue, "DataRow");
        return this.sFirstValue;
}

function DataRow_setTrailerTitle(ps_Value)
{
        this.sFirstValue = ps_Value;
}

function DataRow_getTrailerVersion()
{
        if (this.sSecondValue)
        {
                return this.sSecondValue;
        }
        else
        {
                return "";
        }
}

function DataRow_setTrailerVersion(ps_Value)
{
        this.sSecondValue = ps_Value;
}

function DataRow_getAdvertiser()
{
        return this.sFirstValue;
}

function DataRow_setAdvertiser(ps_Value)
{
        this.sFirstValue = ps_Value;
}

function DataRow_getProduct()
{
        return this.sSecondValue;
}

function DataRow_setProduct(ps_Value)
{
        this.sSecondValue = ps_Value;
}

function DataRow_getIdent()
{
        return this.sFirstValue;
}

function DataRow_setIdent(ps_Value)
{
        this.sFirstValue = ps_Value;
}

function DataRow_getSpecialTitle()
{
        log(LOG_DETAIL, "DataRow_getSpecialTitle: " + this.sFirstValue, "DataRow");
        return this.sFirstValue;
}

function DataRow_setSpecialTitle(ps_Value)
{
        this.sFirstValue = ps_Value;
}

function DataRow_getStartTime()
{
        log(LOG_DETAIL,"DataRow_getTimeValue: " + this.tStartTime, "DataRow" );

        return this.tStartTime;
}

function DataRow_setStartTime(ps_Value)
{
        this.tStartTime = ps_Value;
}

function DataRow_getFull()
{
        log(LOG_DETAIL, "DataRow_getFullValue: "+this.sFull, "DataRow" );
        return this.sFull;
}
function DataRow_setFull( ps_Value )
{
        this.sFull = ps_Value;
}

// --------------------------------------------------------------------

function LobbyDataList(_iJobSegmentID, _iLobbyCheckDetailID, _iCinemaID)
{
        log(LOG_TRACE, "LobbyDataList CONSTRUCT: _iJobSegmentID = " + _iJobSegmentID + ", _iLobbyCheckDetailID = " + _iLobbyCheckDetailID + ", _iCinemaID = " + _iCinemaID, "LobbyDataEntry");

        this.aDataRows                          = new Array();
        this.aScreenRows                        = new Array();
        this.iJobSegmentID                      = _iJobSegmentID;
        this.iLobbyCheckDetailID        = _iLobbyCheckDetailID;
        this.iCinemaID                          = _iCinemaID;
        this.bHasChanged                        = false;

        this.showInDetailsFrame         = LobbyDataList_showInDetailsFrame;
        this.addRow                                     = LobbyDataList_addRow;
        this.addRowFromObject           = LobbyDataList_addRowFromObject;
        this.setRow                                     = LobbyDataList_setRow;
        this.getRowCount                        = LobbyDataList_getRowCount
        this.swapRows                           = LobbyDataList_swapRows;
        this.moveRowUp                          = LobbyDataList_moveRowUp;
        this.moveRowDown                        = LobbyDataList_moveRowDown;
        this.addEmptyRow                        = LobbyDataList_addEmptyRow;
        this.renderList                         = LobbyDataList_renderList;
        this.renderRow                          = LobbyDataList_renderRow;
        this.deleteRow                          = LobbyDataList_deleteRow;
        this.insertRow                          = LobbyDataList_insertRow;
        this.submit                                     = LobbyDataList_submit;
        this.hasChanged                         = LobbyDataList_hasChanged;
        this.validateForSubmit          = LobbyDataList_validateForSubmit;
        this.explainStatus                      = LobbyDataList_explainStatus;
        this.logEachRow                         = LobbyDataList_logEachRow;
}

function LobbyDataList_logEachRow()
{
        var pRow = null;
        
        for(var iIndex = 0; iIndex < this.aDataRows.length ; iIndex++)
        {
                pRow = this.aDataRows[iIndex];
                pRow.logThisRow();
        }       
}

function LobbyDataList_validateForSubmit()
{
        var bReturn             = true;

        var pRow                = null;
        var iGeneralStatus      = 0;
        
        for(var iIndex = 0; iIndex < this.aDataRows.length ; iIndex++)
        {
                pRow = this.aDataRows[iIndex];
                
                switch (pRow.getStatus())
                {
                        case ROWSTATUS_NOMATCH:
                                if (iGeneralStatus < ROWSTATUS_NOMATCH)
                                {
                                        iGeneralStatus = ROWSTATUS_NOMATCH;
                                }
                                break;

                        case ROWSTATUS_MISSINGVERSION:
                                if (iGeneralStatus < ROWSTATUS_MISSINGVERSION)
                                {
                                        iGeneralStatus = ROWSTATUS_MISSINGVERSION;
                                }
                                break;

                        case ROWSTATUS_MISSINGFEATURE:
                                if (iGeneralStatus < ROWSTATUS_MISSINGFEATURE)
                                {
                                        iGeneralStatus = ROWSTATUS_MISSINGFEATURE;
                                }
                                break;

                        case ROWSTATUS_MISSINGTYPE:
                                if (iGeneralStatus < ROWSTATUS_MISSINGTYPE)
                                {
                                        iGeneralStatus = ROWSTATUS_MISSINGTYPE;
                                }
                                break;

                        case ROWSTATUS_MISSINGLOCATION:
                                if (iGeneralStatus < ROWSTATUS_MISSINGLOCATION)
                                {
                                        iGeneralStatus = ROWSTATUS_MISSINGLOCATION;
                                }
                                break;

                        case ROWSTATUS_MISSINGQUANTITY:
                                if (iGeneralStatus < ROWSTATUS_MISSINGQUANTITY)
                                {
                                        iGeneralStatus = ROWSTATUS_MISSINGQUANTITY;
                                }
                                break;

                        default:
                                if (iGeneralStatus < ROWSTATUS_GOOD)
                                {
                                        iGeneralStatus = ROWSTATUS_GOOD;
                                }
                }
        }
        
        switch (iGeneralStatus)
        {
                case 0:
                        var sMsg = getMessage("hidMsg_NoEntries");
                        // var sMsg = "You have not entered any items for the Pre Show Programme - is this correct? (NB - you will be able to come back to this screen to add them later). \n\n Click OK to continue, or CANCEL to stay on this screen."
                        
                        if (!confirm(sMsg))
                        {
                                bReturn = false;
                        }
                        break;

                case ROWSTATUS_NOMATCH:
                        var sMsg = getMessage("hidMsg_ItemsUnmatched");
                        // var sMsg = "Some of the items you have entered have not been matched up with a title in our database (look for the amber colour in the Status column)- are you SURE they cannot be matched? \n\n Click OK to continue, or CANCEL to stay on this screen."
                        
                        if (!confirm(sMsg))
                        {
                                bReturn = false;
                        }
                        break;

                case ROWSTATUS_MISSINGVERSION:
                        var sMsg = getMessage("hidMsg_VersionsBlank");
                        // var sMsg = "The Versions field is blank for some of the items you have entered (look for the red colour in the Status column)."

                        alert(sMsg);
                        bReturn = false;

                        break;

                case ROWSTATUS_MISSINGFEATURE:
                        var sMsg = getMessage("hidMsg_FeatureBlank");
                        // var sMsg = "The Feature field is blank for some of the items you have entered (look for the red colour in the Status column)."
                        
                        alert(sMsg);
                        bReturn = false;

                        break;

                case ROWSTATUS_MISSINGTYPE:
                        var sMsg = getMessage("hidMsg_TypeBlank");
                        // var sMsg = "The Type field is blank for some of the items you have entered (look for the red colour in the Status column)."
                        
                        alert(sMsg);
                        bReturn = false;

                        break;

                case ROWSTATUS_MISSINGLOCATION:
                        var sMsg = getMessage("hidMsg_LocationBlank");
                        // var sMsg = "The Location field is blank for some of the items you have entered (look for the red colour in the Status column)."
                        
                        alert(sMsg);
                        bReturn = false;

                        break;

                case ROWSTATUS_MISSINGQUANTITY:
                        var sMsg = getMessage("hidMsg_QuantityBlank");
                        // var sMsg = "The Quantity field is blank for some of the items you have entered (look for the red colour in the Status column)."
                        
                        alert(sMsg);
                        bReturn = false;

                        break;

                default:
                        bReturn = true;
        }
        
        this.logEachRow();
        
        return bReturn;
}

function LobbyDataList_hasChanged()
{
        return this.bHasChanged;
}

function LobbyDataList_submit()
{
        if ((gi_SelectedRow) && (gi_SelectedRow < this.aDataRows.length))
        {
                if (!this.aDataRows[gi_SelectedRow].isEmpty())
                {
                        updateSelectedRow();
                }
        }
        
        frames["SummaryArea"].document.getElementById("hidRowCount").value = this.getRowCount();

        if(this.validateForSubmit())
        {
                frames["SummaryArea"].document.getElementById("frmMain").submit();
        }
}

function LobbyDataList_showInDetailsFrame(pi_RowIndex)
{
        log(LOG_DETAIL, "LobbyDataList_showInDetailsFrame START", "LobbyDataList");
        var pCurrent = this.aDataRows[pi_RowIndex];

        log(LOG_DETAIL, "LobbyDataList_showInDetailsFrame 1, pCurrent = " + pCurrent, "LobbyDataList");
        frames["DetailsArea"].showDetails(pi_RowIndex + 1, pCurrent);

        log(LOG_DETAIL, "LobbyDataList_showInDetailsFrame DONE", "LobbyDataList");
}

function LobbyDataList_addRow(ps_FeatureDescription, 
                                                                pi_FeatureID, 
                                                                pi_LobbyLocationID, 
                                                                ps_LocationText,
                                                                pi_Prominent, 
                                                                pi_LobbyTypeID, 
                                                                ps_TypeText,
                                                                pi_LobbyVersionID, 
                                                                ps_VersionText,
                                                                pi_Quantity,
                                                                pb_VersionActivated)
{
//      alert("this.aDataRows.length = " + this.aDataRows.length + ", ps_FirstValue = " + ps_FirstValue);
        this.bHasChanged = true;
        
        this.aDataRows[this.aDataRows.length] = new LobbyDataRow(ps_FeatureDescription,
                                                                                                                        pi_FeatureID, 
                                                                                                                        pi_LobbyLocationID,
                                                                                                                        ps_LocationText,
                                                                                                                        pi_Prominent, 
                                                                                                                        pi_LobbyTypeID, 
                                                                                                                        ps_TypeText,
                                                                                                                        pi_LobbyVersionID,
                                                                                                                        ps_VersionText,
                                                                                                                        pi_Quantity,
                                                                                                                        pb_VersionActivated);

//      alert("Added.");
}

function LobbyDataList_addRowFromObject(po_CurrentRow)
{
//      alert("this.aDataRows.length = " + this.aDataRows.length + ", ps_Title = " + ps_Title);
        this.bHasChanged = true;

        this.aDataRows[this.aDataRows.length] = po_CurrentRow;
}

function LobbyDataList_setRow(pi_RowIndex, po_CurrentRow)
{
        log(LOG_TRACE, "LobbyDataList_setRow START: pi_RowIndex = " + pi_RowIndex + ", po_CurrentRow = " + po_CurrentRow, "LobbyDataList");

        if(pi_RowIndex < this.aDataRows.length)
        {
                log(LOG_DETAIL, "LobbyDataList_setRow 1: this.aDataRows.length = " + this.aDataRows.length, "LobbyDataList");
//              alert("Setting row " + pi_RowIndex + " to title " + po_CurrentRow.getTrailerTitle());
                this.aDataRows[pi_RowIndex] = po_CurrentRow;
//              alert("Setting row " + pi_RowIndex + " to title " + this.aDataRows[pi_RowIndex].getTrailerTitle());

                this.renderRow(pi_RowIndex);
        }
        else
        {
                log(LOG_DETAIL, "LobbyDataList_setRow 2: this.aDataRows.length = " + this.aDataRows.length, "LobbyDataList");

                if (!po_CurrentRow.isEmpty())
                {
                        log(LOG_DETAIL, "LobbyDataList_setRow 3: po_CurrentRow.isEmpty() = false", "LobbyDataList");
                        this.addRowFromObject(po_CurrentRow);
                        this.renderRow(pi_RowIndex);
                        this.addEmptyRow(true);
                        
                        frames["DetailsArea"].resetFocus();
                }
        }

        log(LOG_TRACE, "LobbyDataList_setRow END", "LobbyDataList");
}

function LobbyDataList_getRowCount()
{
        log(LOG_DETAIL, "LobbyDataList_getRowCount START", "LobbyDataList");
        var iRowCount = this.aDataRows.length;

        log(LOG_DETAIL, "LobbyDataList_getRowCount RETURNING", "LobbyDataList");
        return iRowCount;
}

function LobbyDataList_swapRows(pi_RowIndex1, pi_RowIndex2)
{
        this.bHasChanged = true;

        var pTemp                       = this.aDataRows[pi_RowIndex1];
        this.aDataRows[pi_RowIndex1]    = this.aDataRows[pi_RowIndex2];
        this.aDataRows[pi_RowIndex2]    = pTemp;
}

function LobbyDataList_moveRowUp(pi_RowIndex)
{
        if (pi_RowIndex > 0)
        {
                this.swapRows(pi_RowIndex, pi_RowIndex - 1);
                return true;
        }
        else
        {
                return false;
        }
}

function LobbyDataList_moveRowDown(pi_RowIndex)
{
        if (pi_RowIndex < this.aDataRows.length - 1)
        {
                this.swapRows(pi_RowIndex, pi_RowIndex + 1);
                return true;
        }
        else
        {
                return false;
        }
}

function LobbyDataList_addEmptyRow(_bSelected)
{
//      alert("Adding empty row: _bSelected = " + _bSelected);

        this.bHasChanged = true;
        
        var pRowFactory                                         = frames["SummaryArea"].gRowFactory;
        this.aScreenRows[this.aDataRows.length]                 = pRowFactory.getRow(this.aDataRows.length);
        
        if (_bSelected)
        {
                if (gi_SelectedRow != -1)
                {
                        this.aScreenRows[gi_SelectedRow].className      = "UnHighlightedRow"
                }
                
                gi_SelectedRow = this.aDataRows.length;
                this.aScreenRows[this.aDataRows.length].className       = "SelectedRow"
        }
        else
        {
                this.aScreenRows[this.aDataRows.length].className       = "UnHighlightedRow"
        }
        
        this.renderRow(this.aDataRows.length);
}

function LobbyDataList_renderList()
{
        log(LOG_DETAIL, "LobbyDataList_renderList: this.aDataRows.length = " + this.aDataRows.length, "LobbyDataList");

        var pRowFactory                 = frames["SummaryArea"].gRowFactory;
        var pScreenRow                  = null;
        var bSomethingSelected  = false;

        log(LOG_DETAIL, "LobbyDataList_renderList: pRowFactory = " + pRowFactory, "LobbyDataList");
        
//      alert("this.iLobbyCheckDetailID = " + this.iLobbyCheckDetailID);
        var pField = pRowFactory.pDocument.getElementById("hidLobbyCheckDetailID");
        pField.value = this.iLobbyCheckDetailID;

        log(LOG_DETAIL, "LobbyDataList_renderList: hidLobbyCheckDetailID, pField = " + pField, "LobbyDataList");

//      alert("this.iJobSegmentID = " + this.iJobSegmentID);
        pField = pRowFactory.pDocument.getElementById("hidJobSegmentID");
        pField.value = this.iJobSegmentID;

        log(LOG_DETAIL, "LobbyDataList_renderList: Starting Loop, this.aDataRows.length = " + this.aDataRows.length, "LobbyDataList");

        for (var li_RowIndex = 0; li_RowIndex < this.aDataRows.length; li_RowIndex++)
        {
                // get next available Screen Row
                this.aScreenRows[li_RowIndex]           = pRowFactory.getRow(li_RowIndex);

                if (gi_SelectedRow == li_RowIndex)
                {
                        this.aScreenRows[li_RowIndex].className = "SelectedRow"
                        bSomethingSelected = true;
                }
                else
                {
                        this.aScreenRows[li_RowIndex].className = "UnHighlightedRow"
                }
                this.renderRow(li_RowIndex);
        }
        
        if (bSomethingSelected)
        {
                this.addEmptyRow(false);
        }
        else
        {
                this.addEmptyRow(true);
        }

//      alert("pRowFactory.pDocument.removeLoadingDiv");
        frames["SummaryArea"].removeLoadingDiv();
}

function LobbyDataList_renderRow(pi_RowIndex)
{
        log(LOG_TRACE, "LobbyDataList_renderRow START, pi_RowIndex = " + pi_RowIndex, "LobbyDataList");

        var pRowFactory = frames["SummaryArea"].gRowFactory;
        var pScreenRow  = this.aScreenRows[pi_RowIndex];
        var bSetDetails = false;
        
        // fill out fields in Screen Row

        if (isMozilla())
        {
//              alert ("LobbyDataList_renderRow Mozilla 0");
                log(LOG_DETAIL, "LobbyDataList_renderRow MZ: buildTagTree = " + buildTagTree(pScreenRow), "LobbyDataList");

                pScreenRow.childNodes[1].childNodes[0].data = pi_RowIndex + 1;

//              alert ("LobbyDataList_renderRow Mozilla 0.5");

                if (this.aDataRows[pi_RowIndex])
                {
//                      alert ("LobbyDataList_renderRow Mozilla 1");

                        this.sFeatureDescription        = ps_FeatureDescription;
                        this.iFeatureID                         = pi_FeatureID;
                        this.iLobbyLocationID           = pi_LobbyLocationID;
                        this.iProminent                         = pi_Prominent;
                        this.iLobbyTypeID                       = pi_LobbyTypeID;
                        this.iLobbyVersionID            = pi_LobbyVersionID;
                        this.iQuantity                          = pi_Quantity;

                        
                        pScreenRow.childNodes[1].childNodes[0].data = this.aDataRows[pi_RowIndex].sFeatureDescription;
                        pScreenRow.childNodes[3].childNodes[0].data = this.aDataRows[pi_RowIndex].getTypeString();
//                      pScreenRow.childNodes[5].childNodes[0].data = this.aDataRows[pi_RowIndex].sLocationText;
                        pScreenRow.childNodes[5].childNodes[0].data = this.aDataRows[pi_RowIndex].iQuantity;

                }
                else
                {
                        pScreenRow.childNodes[1].childNodes[0].data = "";
                        pScreenRow.childNodes[3].childNodes[0].data = "";
//                      pScreenRow.childNodes[5].childNodes[0].data = "";
                        pScreenRow.childNodes[5].childNodes[0].data = "";
                }
        }
        else
        {
//              alert ("LobbyDataList_renderRow IE 0");
                log(LOG_DETAIL, "LobbyDataList_renderRow IE: pi_RowIndex = " + pi_RowIndex + ", buildTagTree = " + buildTagTree(pScreenRow), "LobbyDataList");

                pScreenRow.childNodes[0].innerText = pi_RowIndex + 1;

                if (this.aDataRows[pi_RowIndex])
                {
                        log(LOG_DETAIL, "LobbyDataList_renderRow IE: this.aDataRows[pi_RowIndex] = " + this.aDataRows[pi_RowIndex], "LobbyDataList");
                        pScreenRow.childNodes[0].innerText = this.aDataRows[pi_RowIndex].sFeatureDescription;
                        pScreenRow.childNodes[1].innerText = this.aDataRows[pi_RowIndex].getTypeString();
//                      pScreenRow.childNodes[2].innerText = this.aDataRows[pi_RowIndex].sLocationText;
                        pScreenRow.childNodes[2].innerText = this.aDataRows[pi_RowIndex].iQuantity;
                }
                else
                {
                        log(LOG_DETAIL, "LobbyDataList_renderRow IE: NOT this.aDataRows[pi_RowIndex].", "LobbyDataList");
                        pScreenRow.childNodes[0].innerText = "";
                        log(LOG_DETAIL, "LobbyDataList_renderRow IE: a1", "LobbyDataList");
                        pScreenRow.childNodes[1].innerText = "";
                        log(LOG_DETAIL, "LobbyDataList_renderRow IE: a2", "LobbyDataList");
//                      pScreenRow.childNodes[2].innerText = "";
                        log(LOG_DETAIL, "LobbyDataList_renderRow IE: a3", "LobbyDataList");
                        pScreenRow.childNodes[2].innerText = "";
                        log(LOG_DETAIL, "LobbyDataList_renderRow IE: a4", "LobbyDataList");
                }
        }
        
        // Update status icon:
        // -------------------
        var pImgStatus          = pRowFactory.pDocument.getElementById("ImgStatus" + pi_RowIndex);

        log(LOG_DETAIL, "LobbyDataList_renderRow getElementById('ImgStatus' + " + pi_RowIndex + ") = " + pImgStatus, "LobbyDataList");

        var pImgDelete          = pRowFactory.pDocument.getElementById("ImgDelete" + pi_RowIndex);
        var pStatusCell         = pRowFactory.pDocument.getElementById("CellStatus" + pi_RowIndex);
        var sStatusClass        = "";
        
//      alert("pi_RowIndex = " + pi_RowIndex);
//      alert("this.aDataRows[pi_RowIndex] = " + this.aDataRows[pi_RowIndex]);

        log(LOG_DETAIL, "LobbyDataList_renderRow this.aDataRows[pi_RowIndex] = " + this.aDataRows[pi_RowIndex], "LobbyDataList");

        if (this.aDataRows[pi_RowIndex])
        {
                switch (this.aDataRows[pi_RowIndex].getStatus())
                {
                        case ROWSTATUS_GOOD:
                                pImgStatus.src  = "Img/tick.png";
                                sStatusClass    = "CellStatus_Good";
                                break;

                        case ROWSTATUS_NOMATCH:
                                pImgStatus.src = "Img/film_error.png";
                                sStatusClass    = "CellStatus_Unmatched";
                                break;

                        case ROWSTATUS_MISSINGFEATURE:
                                pImgStatus.src = "Img/cross.png";
                                sStatusClass    = "CellStatus_Bad";
                                break;

                        case ROWSTATUS_MISSINGTYPE:
                                pImgStatus.src = "Img/cross.png";
                                sStatusClass    = "CellStatus_Bad";
                                break;

                        case ROWSTATUS_MISSINGLOCATION:
                                pImgStatus.src = "Img/cross.png";
                                sStatusClass    = "CellStatus_Bad";
                                break;

                        case ROWSTATUS_MISSINGQUANTITY:
                                pImgStatus.src = "Img/cross.png";
                                sStatusClass    = "CellStatus_Bad";
                                break;

                        case ROWSTATUS_MISSINGVERSION:
                                pImgStatus.src = "Img/note_add.png";
                                sStatusClass    = "CellStatus_Missing";
                                break;

                        default:
                }

                log(LOG_DETAIL, "LobbyDataList_renderRow SET VALUES a", "LobbyDataList");

                pImgDelete.src                  = "Img/delete.png";
                pStatusCell.className   = sStatusClass;

                // Set form fields:
                // ----------------

                log(LOG_DETAIL, "LobbyDataList_renderRow SET VALUES b", "LobbyDataList");

                pRowFactory.pDocument.getElementById("lb_DataFlag_" + pi_RowIndex).value                        = "TRUE";
                pRowFactory.pDocument.getElementById("ls_FeatureDescription_" + pi_RowIndex).value      = this.aDataRows[pi_RowIndex].sFeatureDescription;
                pRowFactory.pDocument.getElementById("li_FeatureID_" + pi_RowIndex).value                       = this.aDataRows[pi_RowIndex].iFeatureID;
//              pRowFactory.pDocument.getElementById("li_LobbyLocationID_" + pi_RowIndex).value         = this.aDataRows[pi_RowIndex].iLobbyLocationID;
//              pRowFactory.pDocument.getElementById("li_Prominent_" + pi_RowIndex).value                       = this.aDataRows[pi_RowIndex].iProminent;
                pRowFactory.pDocument.getElementById("li_LobbyTypeID_" + pi_RowIndex).value                     = this.aDataRows[pi_RowIndex].iLobbyTypeID;
                pRowFactory.pDocument.getElementById("li_LobbyVersionID_" + pi_RowIndex).value          = this.aDataRows[pi_RowIndex].iLobbyVersionID;
                pRowFactory.pDocument.getElementById("li_Quantity_" + pi_RowIndex).value                        = this.aDataRows[pi_RowIndex].iQuantity;
                pRowFactory.pDocument.getElementById("lb_VersionActivated_" + pi_RowIndex).value        = (this.aDataRows[pi_RowIndex].bVersionActivated == 1) ? 1 : 0;

//              alert("Just set pRowFactory.pDocument.getElementById(lb_VersionActivated_ + pi_RowIndex).value to " + pRowFactory.pDocument.getElementById("lb_VersionActivated_" + pi_RowIndex).value);

                bSetDetails     = true;
        }
        else
        {
                pImgStatus.src                  = "Img/transpixel.gif";
                pImgDelete.src                  = "Img/transpixel.gif";
                pStatusCell.className   = "CellStatus";
        }

        log(LOG_TRACE, "LobbyDataList_renderRow END, bSetDetails = " + bSetDetails, "LobbyDataList");
}

function LobbyDataList_deleteRow(pi_RowIndex)
{
        this.bHasChanged = true;
        
        for (var li_RowIndex = pi_RowIndex; li_RowIndex < (this.aDataRows.length - 1); li_RowIndex++)
        {
                this.aDataRows[li_RowIndex] = this.aDataRows[li_RowIndex + 1];
        }
        
        this.aDataRows.length = this.aDataRows.length - 1;
        this.aScreenRows.length = this.aScreenRows.length - 1;
}

function LobbyDataList_insertRow(pi_RowIndex)
{
        this.bHasChanged = true;
        
        for (var li_RowIndex = (this.aDataRows.length - 1); li_RowIndex >= pi_RowIndex; li_RowIndex--)
        {
                this.aDataRows[li_RowIndex + 1] = this.aDataRows[li_RowIndex];
        }
        
        this.aDataRows[pi_RowIndex] = new LobbyDataRow("", -1, -1, -1, -1, -1, -1);
        setSelectedRow(pi_RowIndex);
        this.renderList();
}

function LobbyDataList_explainStatus(pi_RowIndex)
{
        var sMsg = "";
        
        if (!pi_RowIndex)
        {
                pi_RowIndex = getSelectedRow();
        }
                
        switch (this.aDataRows[pi_RowIndex].getStatus())
        {
                case ROWSTATUS_GOOD:
                        sMsg = "This entry is fine.";
                        break;

                case ROWSTATUS_MISSINGFEATURE:
                        sMsg = "This entry is not complete - you must enter a value for the Feature name.";
                        break;

                case ROWSTATUS_MISSINGTYPE:
                        sMsg = "This entry is not complete - you must choose a value for the material Type.";
                        break;

                case ROWSTATUS_MISSINGLOCATION:
                        sMsg = "This entry is not complete - you must choose a value for the material Location.";
                        break;

                case ROWSTATUS_MISSINGQUANTITY:
                        sMsg = "This entry is not complete - you must enter the number of these items seen.";
                        break;

                case ROWSTATUS_NOMATCH:
                        sMsg = "The feature name for this entry does not match up with anything in our database - it may still be correct, but you should check to see if there is a match in our databse (perhaps spelled slightly differently).";
                        break;

                case ROWSTATUS_MISSINGVERSION:
                        sMsg = "This entry is not complete - you must choose one of the specific versions for this material type.";
                        break;
        }

        alert(sMsg);
}

// --------------------------------------------------------------------

function LobbyDataRow(ps_FeatureDescription, 
                                        pi_FeatureID, 
                                        pi_LobbyLocationID, 
                                        ps_LocationText,
                                        pi_Prominent, 
                                        pi_LobbyTypeID, 
                                        ps_TypeText,
                                        pi_LobbyVersionID, 
                                        ps_VersionText,
                                        pi_Quantity,
                                        pb_VersionActivated)
{

        log(LOG_TRACE, "LobbyDataRow CONSTRUCT: ps_FeatureDescription = " + ps_FeatureDescription + ", ps_LocationText = " + ps_LocationText + ", ps_TypeText = " + ps_TypeText, "LobbyDataEntry");

// Properties:
// -----------

//      alert("pb_VersionActivated = " + pb_VersionActivated);

        this.sFeatureDescription        = ps_FeatureDescription;
        this.sLocationText                      = ps_LocationText;
        this.sTypeText                          = ps_TypeText;
        this.sVersionText                       = ps_VersionText;
        this.iFeatureID                         = pi_FeatureID;
        this.iLobbyLocationID           = pi_LobbyLocationID;
        this.iProminent                         = pi_Prominent;
        this.iLobbyTypeID                       = pi_LobbyTypeID;
        this.iLobbyVersionID            = pi_LobbyVersionID;
        this.iQuantity                          = pi_Quantity;
        this.bVersionActivated          = pb_VersionActivated;

// Methods:
// -----------

        this.isEmpty                    = LobbyDataRow_isEmpty;
        this.getStatus                  = LobbyDataRow_getStatus;
        this.getTypeString              = LobbyDataRow_getTypeString;
        this.logThisRow                 = LobbyDataRow_logThisRow;

        log(LOG_TRACE, "LobbyDataRow CONSTRUCT END", "LobbyDataEntry");
}

function LobbyDataRow_logThisRow()
{
        log(LOG_DETAIL, "LobbyDataRow_logThisRow this.sFeatureDescription = " + this.sFeatureDescription, "LobbyDataEntry");
        log(LOG_DETAIL, "LobbyDataRow_logThisRow this.sLocationText = " + this.sLocationText, "LobbyDataEntry");
        log(LOG_DETAIL, "LobbyDataRow_logThisRow this.sTypeText = " + this.sTypeText, "LobbyDataEntry");
        log(LOG_DETAIL, "LobbyDataRow_logThisRow this.sVersionText = " + this.sVersionText, "LobbyDataEntry");
        log(LOG_DETAIL, "LobbyDataRow_logThisRow this.iFeatureID = " + this.iFeatureID, "LobbyDataEntry");
        log(LOG_DETAIL, "LobbyDataRow_logThisRow this.iLobbyLocationID = " + this.iLobbyLocationID, "LobbyDataEntry");
        log(LOG_DETAIL, "LobbyDataRow_logThisRow this.iProminent = " + this.iProminent, "LobbyDataEntry");
        log(LOG_DETAIL, "LobbyDataRow_logThisRow this.iLobbyTypeID = " + this.iLobbyTypeID, "LobbyDataEntry");
        log(LOG_DETAIL, "LobbyDataRow_logThisRow this.iLobbyVersionID = " + this.iLobbyVersionID, "LobbyDataEntry");
        log(LOG_DETAIL, "LobbyDataRow_logThisRow this.iQuantity = " + this.iQuantity, "LobbyDataEntry");
        log(LOG_DETAIL, "LobbyDataRow_logThisRow this.bVersionActivated = " + this.bVersionActivated, "LobbyDataEntry");
}

function LobbyDataRow_getTypeString()
{
        var sType = "";
        
        if (this.iLobbyVersionID && this.iLobbyVersionID != "-1")
        {
                sType = this.sTypeText + " - " + this.sVersionText;
        }
        else
        {
                sType = this.sTypeText;
        }
        
        return sType;
}

function LobbyDataRow_isEmpty()
{
        var bReturn = true;
        
        if (this.sFeatureDescription)
        {
                bReturn = false;
        }
        
        return bReturn;
}

function LobbyDataRow_getStatus()
{
        log(LOG_DETAIL, "LobbyDataRow_getStatus START ", "LobbyDataRow");

        var iReturn = ROWSTATUS_GOOD;

//      alert ("this.bFirstValueMatched = " + this.bFirstValueMatched);
//      alert ("this.iFeatureID = " + this.iFeatureID);

        if (!this.sFeatureDescription)
        {
                iReturn = ROWSTATUS_MISSINGFEATURE;
        }
        else if (this.iFeatureID == -1)
        {
                iReturn = ROWSTATUS_NOMATCH;
        }
        else if (!this.iLobbyTypeID)
        {
                iReturn = ROWSTATUS_MISSINGTYPE;
        }
        else if (this.iQuantity < 1)
        {
                iReturn = ROWSTATUS_MISSINGQUANTITY;
        }
        else if (this.bVersionActivated == 1 && !this.iLobbyVersionID)
        {
                iReturn = ROWSTATUS_MISSINGVERSION;
        }
        
/*
        else if (!this.iLobbyLocationID)
        {
                iReturn = ROWSTATUS_MISSINGLOCATION;
        }
*/

        log(LOG_DETAIL, "LobbyDataRow_getStatus END, iReturn = " + iReturn, "LobbyDataRow");

        return iReturn;
}

// --------------------------------------------------------------------


var ga_ButtonStrips     = new Array();


function createButtonStrip(_sStripName, _iButtonCount, _iIndex, _bRadioButton)
{
        ga_ButtonStrips[ga_ButtonStrips.length] = new ButtonStrip(_sStripName, _iButtonCount, _iIndex, _bRadioButton);

//      alert("Created Button Strip " + _sStripName);
}

function ButtonStrip(_sStripName, _iButtonCount, _iIndex, _bRadioButton)
{
        this.sStripName                 = _sStripName;
        this.iButtonCount               = _iButtonCount;
        this.iSelectedButton    = (_iIndex) ? _iIndex : -1;
        this.bRadioButton               = _bRadioButton;
        this.aSelected                  = new Array(_iButtonCount);

        this.hitButton                  = ButtonStrip_hitButton;

//      alert("Creating ButtonStrip with selected: " + this.iSelectedButton);

        if (this.iSelectedButton != -1)
        {
                this.hitButton(this.iSelectedButton);
        }
}

function ButtonStrip_hitButton(_iButtonIndex)
{
        var pButton = null;
        var iReturn     = null;

        if (this.bRadioButton)
        {
                if (this.iSelectedButton != -1)
                {
                        pButton = getEl(this.sStripName + "_" + this.iSelectedButton);
                        pButton.className = "ButtonOff";
                }

                this.iSelectedButton    = _iButtonIndex;

                pButton = getEl(this.sStripName + "_" + this.iSelectedButton);
                pButton.className = "ButtonOn";

                iReturn = this.iSelectedButton;
        }
        else
        {
                if (this.aSelected[_iButtonIndex])
                {
                        this.aSelected[_iButtonIndex] = false;
                        pButton = getEl(this.sStripName + "_" + _iButtonIndex);
                        pButton.className = "ButtonOff";
                }
                else
                {
                        this.aSelected[_iButtonIndex] = true;
                        pButton = getEl(this.sStripName + "_" + _iButtonIndex);
                        pButton.className = "ButtonOn";
                }

                iReturn = this.aSelected[_iButtonIndex];
        }

        return iReturn;
}


// --------------------------------------------------------------------

function leftTrim(sString) 
{
        while (sString.substring(0,1) == ' ')
        {
                sString = sString.substring(1, sString.length);
        }
        return sString;
}

function rightTrim(sString) 
{
        while (sString.substring(sString.length-1, sString.length) == ' ')
        {
                sString = sString.substring(0,sString.length-1);
        }
        return sString;
}

function describe(_sEvalString)
{
        var sEvalString = _sEvalString.replace(/|/g, "-");
        alert(_sEvalString + "=" + eval(_sEvalString));
}


// --------------------------------------------------------------------
// --------------------------------------------------------------------

window.status = "Common.js loaded OK";
//	alert("Common.js loaded OK");


