/*=============================================================================
                    AUTOMATED LOGIC CORPORATION
            Copyright (c) 1999 - 2003 All Rights Reserved
     This document contains confidential/proprietary information.
===============================================================================

   @(#)pointsbuilder.js

   Author(s)  champton, jknuth, jgrant
=============================================================================*/


/**********************************************************************
 Method: setTargetPage
 Params: newValue
 Return:
 Description:  Sets the value of the targetPage for the browser to load.
 author: champton
**********************************************************************/
function setTargetPage( newValue )
{
   top.frames.topFrame.document.forms[0].targetPage.value = newValue;
}

/**********************************************************************
 Method: loadLogoLeft
 Params:
 Return:
 Description:  Loads the logo page in the left navigation panel
 author: champton
**********************************************************************/
function loadLogoLeft()
{
   restoreEquipmentFrame();
   top.frames.navigationFrame.location = "/web/jsp2/logoLeft.jsp";
   top.frames.equipmentFrame.location = "/web/jsp2/blankLeft.jsp";
}

/**********************************************************************
 Method: loadNavigation
 Params:
 Return:
 Description:  Loads the navigation page in the left frame.
 author: champton
**********************************************************************/
function loadNavigation()
{
   restoreEquipmentFrame();
   top.frames.navigationFrame.location = "/web/jsp2/navigation.jsp";
   loadEquipmentList();
}

/**********************************************************************
 Method: loadequipmentList
 Params:
 Return:
 Description:  Loads the equipment list in the lower left frame.
 author: champton
**********************************************************************/
function loadEquipmentList()
{
   top.frames.equipmentFrame.location = "/web/jsp2/equipmentList.jsp";
}

/**********************************************************************
 Method: loadTableOfContentsNav
 Params:
 Return:
 Description:  Loads the preview table of contents in the left frame.
 author: jknuth
**********************************************************************/
function loadTableOfContentsNav()
{
   minimizeEquipmentFrame();
   top.frames.navigationFrame.location = "/web/jsp2/tableOfContents.jsp"; //navigationFrame
}

/**********************************************************************
 Method: minimizeEquipmentFrame
 Params:
 Return:
 Description:  Collapses the bottom left frame used to show the equipment list.
 author: jknuth
**********************************************************************/
function minimizeEquipmentFrame()
{
   frameset = top.document.getElementById("leftSet");
   frameset.rows = "*, 0";
}

/**********************************************************************
 Method: restoreEquipmentFrame
 Params:
 Return:
 Description:  Expands the bottom left frame used to show the equipment list.
 author: jknuth
**********************************************************************/
function restoreEquipmentFrame()
{
   frameset = top.document.getElementById("leftSet");
   frameset.rows = "216,*";
}

function minimizeControlFrame()
{
   frameset = top.document.getElementById( "rightSide" );
   frameset.cols = "0, *";
}

function restoreControlFrame()
{
   frameset = top.document.getElementById( "rightSide" );
   frameset.cols = "30,*";
}

/**********************************************************************
 Method: nextEquipment
 Params: equipment - The reference name of the selected equipment.
 Return:
 Description:  This is a rewrite of 'selectEquipment' that forces loading
               the equipment summary page to get rid of the sticky tab
               effect for the next button.
 author: champton
**********************************************************************/
function getEquipment( equipment, sticky, pageToLoad )
{
   var target = pageToLoad;
   if ( top.frames.workFrame.document.forms[0] != null )
   {
      top.frames.workFrame.document.forms[0].selected.value = equipment;

      sourcePage = top.frames.workFrame.document.forms[0].sourcePage.value;
      if (sticky && sourcePage != "addEquipment.jsp" )
         target = sourcePage;
      else if ( sticky != true )
         top.frames.topFrame.location = "/web/jsp2/top_equipment_definition.jsp";

      top.frames.workFrame.document.forms[0].targetPage.value = target;   // target is the source.  This gives us "sticky" tabs

      if ( top.frames.workFrame.document.forms[0].sourcePage.value == "customSequence.jsp" )
         saveCustomSequence( "equipmentSequence.jsp" );
      else
         top.frames.workFrame.document.forms[0].submit();
   }
}

/**********************************************************************
 Method: nextEquipment
 Params: equipment - The reference name of the selected equipment.
 Return:
 Description:  This is a rewrite of 'selectEquipment' that forces loading
               the equipment summary page to get rid of the sticky tab
               effect for the next button.
 author: champton
**********************************************************************/
function nextEquipment( equipment, sticky )
{
   getEquipment( equipment, sticky, "equipmentSummary.jsp" );
}

/**********************************************************************
 Method: nextEquipment
 Params: equipment - The reference name of the selected equipment.
 Return:
 Description:  This is a rewrite of 'selectEquipment' that forces loading
               the equipment summary page to get rid of the sticky tab
               effect for the next button.
 author: champton
**********************************************************************/
function prevEquipment( equipment, sticky )
{
   getEquipment( equipment, sticky, "equipmentSchematic.jsp" );
}


/**********************************************************************
 Method: selectEquipment
 Params: equipment - The reference name of the selected equipment.
 Return:
 Description:  Sets the selected equipment value.
 author: champton
**********************************************************************/
function selectEquipment( equipment )
{
   nextEquipment( equipment, true );
}


/**********************************************************************
 Method: openPage
 Params: pageDisplay
 Return: None.
 Description: Sets the target page to the passed in value and submits
              the workFrame form.
 author: champton
**********************************************************************/
function openPage( pageDisplay )
{
   if( top.frames.workFrame.document != null && top.frames.workFrame.document.forms[0] != null )
   {
      // jgrant 17sep03 - added to properly handle saving a custom sequence.  You cannot
      // just submit the custom sequence form.  You have to update the hidden input field
      // sequenceHtml before doing the submit.
      if ( top.frames.workFrame.document.forms[0].sourcePage.value == "customSequence.jsp" )
         saveCustomSequence( pageDisplay );   // Called to update the html before submittal
      else
      {
         if( top.frames.workFrame.document.forms[0].sourcePage.value == "addEquipment.jsp" )
            top.frames.topFrame.location.href = "top_equipment_definition.jsp";
         top.frames.workFrame.document.forms[0].targetPage.value = pageDisplay;
         top.frames.workFrame.document.forms[0].submit();
      }
   }
};

/**********************************************************************
 Method: openTab
 Params: tab - the top jsp page to display
         page - the subpage to display in the tab
 Return: None.
 Description: Moves to the requested tab and page
 author: jgrant
**********************************************************************/
function openTab( tab, page )
{
   if( top.frames.workFrame.document != null && top.frames.workFrame.document.forms[0] != null )
   {
      top.frames.topFrame.location.href = tab;

      if ( top.frames.workFrame.document.forms[0].sourcePage.value == "customSequence.jsp" )
         saveCustomSequence( page );   // Called to update the html before submittal
      else
      {
         top.frames.workFrame.document.forms[0].targetPage.value = page;
         top.frames.workFrame.document.forms[0].submit();
      }
   }
}

/**********************************************************************
 Method: saveCustomSequence
 Params: pageDisplay - target page to display after saving the sequence.
 Return: None
 Description: Causes the custom sequence to be saved.  In IE, a custom
              sequence is edited by a rich text editor.  Before submitting
              we must update a hidden input located in customSequence.jsp
              called sequenceHtml.  This input field is used to return the
              sequence back to the servlet.  In order to update this field
              we must cause the editor to unload.  This will force it to
              update the field and a submit will be performed.  The editor
              is located in an <object> tag (This allows us to embed frames
              inside frames).  We can't get to this object using javascript
              DOM, instead we force the page to unload by clearing the object.
 author: jgrant
**********************************************************************/
function saveCustomSequence( pageDisplay )
{
   top.frames.workFrame.document.forms[0].targetPage.value = pageDisplay;

   if ( document.all )
   {
      // IE only
      top.frames.workFrame.saveContent();
   }
   else
   {
      top.frames.workFrame.document.forms[0].submit();
   }
}

/**********************************************************************
 Method: setTabVisibility
 Params:
 Return:
 Description:  Hides the equipment tabs when equipment is being added
               because the points and sequences do not yet exist.
 author: champton
**********************************************************************/
function setTabVisibility()
{
   if ( top.frames.workFrame.document.forms[0] != null )
   {
      sourcePage = top.frames.workFrame.document.forms[0].sourcePage.value;
      if ( (sourcePage == "addEquipment.jsp") || (sourcePage == "copyToProject.jsp") ||
           (sourcePage == "pointSummary.jsp" ) || (sourcePage == "sendCustomEquipment.jsp" ) )
      {
         showTabs(false);
      }
      else
      {
         showTabs(true);
      }
   }
}

/**********************************************************************
 Method: showTabs
 Params:  show - boolean flag
 Return:
 Description:  Hides the equipment tabs when explicitly told to do so
               this method is less cumbersome than the original.
 author: champton
**********************************************************************/
function showTabs(show)
{
   if ( top.frames.workFrame.document.forms[0] != null )
   {
      if(show)
      {
         top.frames.topFrame.document.getElementById("tab1").style.visibility = "visible";
         top.frames.topFrame.document.getElementById("tab2").style.visibility = "visible";
         top.frames.topFrame.document.getElementById("tab3").style.visibility = "visible";
         top.frames.topFrame.document.getElementById("tab4").style.visibility = "visible";
      }
      else
      {
         top.frames.topFrame.document.getElementById("tab1").style.visibility = "hidden";
         top.frames.topFrame.document.getElementById("tab2").style.visibility = "hidden";
         top.frames.topFrame.document.getElementById("tab3").style.visibility = "hidden";
         top.frames.topFrame.document.getElementById("tab4").style.visibility = "hidden";
      }
   }
}

/**********************************************************************
 Method: openGlossary
 Params: url - url location of the glossary.
 Return:
 Description:  Opens the glossary window at the specified URL.
 author: bgreen
**********************************************************************/
function openGlossary(url)
{
	 popupWin = window.open(url,'csbglossary','width=600,height=400,left=0,top=0,location=no,menubar=no,resizable=no,scrollbars=yes,status=no,toolbar=no');
	 popupWin.focus();
}

/**********************************************************************
 Method: closeProjectWindow
 Params:
 Return:
 Description:  Saves the current page and closes the window.
 author: champton
**********************************************************************/
function closeProjectWindow( hardClose )
{
   if ( top.frames.workFrame.document != null && top.frames.workFrame.document.forms[0] != null )
   {
      if ( hardClose == true )  // this means the user closed the browser.
         top.frames.workFrame.document.forms[0].targetPage.value = 'hardClose';
      else  // this means the user clicked the close link.
         top.frames.workFrame.document.forms[0].targetPage.value = 'close.jsp';
      top.frames.workFrame.document.forms[0].target = "_top";  // We want to display this page in the top most frame
      top.frames.workFrame.document.forms[0].submit();
   }
}

/**********************************************************************
 Method: openDownload
 Params:
 Return:
 Description:  Opens the download pop up window.
 author: bgreen, jknuth
**********************************************************************/
function openDownload()
{
   document.commandForm.action = "/web/jsp2/downloadFrames.jsp";
   document.commandForm.target = "downloadPopup";
   popupWin = window.open('', 'downloadPopup', 'width=400,height=300,top=5,left=10,scrollbars=no,resizable=yes');
   popupWin.focus();
   document.commandForm.submit();
   document.commandForm.action = "/web/CtrlSpecDispatcher";
   document.commandForm.target = "_self";
}

/**********************************************************************
 Method: deleteEquipment
 Params:
 Return:
 Description:  Deletes the currently selected piece of equipment.
 author: jgrant
**********************************************************************/
function deleteEquipment( ref, displayName )
{
    check = confirm( "Permanently delete " + displayName + "?" );
    if ( check == true )
    {
       top.frames.navigationFrame.document.forms[0].action.value  = 'DELETE_EQUIPMENT';
       top.frames.navigationFrame.document.forms[0].param.value = ref;
       top.frames.navigationFrame.document.forms[0].submit();

       currentPage = top.frames.workFrame.document.forms[0].sourcePage.value;
       openPage(  currentPage );
    }
}

function copyEquipment( ref )
{
    window.status = 'Copying equipment to the clipboard ...';
    doAction( 'COPY_EQUIPMENT', ref );
    window.status = '';
}

function duplicateEquipment( ref )
{
   doAction( 'DUPLICATE_EQUIPMENT', ref );
}

function pasteEquipment()
{
    window.status = 'Pasting equipment from the clipboard ...';
    doAction( 'PASTE_EQUIPMENT', '' );
    window.status = '';
}

function moveUp( ref )
{
   doAction( 'MOVE_UP', ref );
}

function moveDown( ref )
{
   doAction( 'MOVE_DOWN', ref );
}

function doAction( action, ref )
{
   top.frames.navigationFrame.document.forms[0].action.value = action;
   top.frames.navigationFrame.document.forms[0].param.value = ref;
   top.frames.navigationFrame.document.forms[0].submit();
}

//////////////////////////////
var equipmentNameChanged = false;


function populateEquipmentName() {
    if ( equipmentNameChanged != true ) {
        document.forms[0].EquipmentName.value = document.forms[0].EquipmentType.options[document.forms[0].EquipmentType.selectedIndex].text;
    }
};

function setEquipmentNameFlag() {
    equipmentNameChanged = true;
};

function checkNewEquipmentInfo() {
    if (document.forms[0].EquipmentName.value == '') {
        document.forms[0].EquipmentName.focus();
        alert( 'Please enter a name.' );
        return false;
    }
    if (document.forms[0].EquipmentType.value == '') {
        document.forms[0].EquipmentType.focus();
        alert( 'Please select type.' );
        return false;
    }
    window.status = 'Adding new equipment ...';
    return true;
};

function scaleSchematic( select )
{
   index = select.selectedIndex;
   selectedOption = select.options[ index ];
   scaleFactor = selectedOption.value;

   defaultHeight = document.getElementById( "imageHeight" ).value;
   defaultWidth = document.getElementById( "imageWidth" ).value;

   image = document.getElementById( "schematic" );
   image.height = defaultHeight * scaleFactor;
   image.width = defaultWidth * scaleFactor;

}

function viewRevisionHistory()
{
   url = "https://www.ctrlspecbuilder.com/printreleasenotes";
 	popupWin = window.open( url, 'revisionHistory','width=800,height=600,left=0,top=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no');
 	popupWin.focus();
}

function viewRevisionDocument( documentID )
{
   url= "https://www.ctrlspecbuilder.com/sb/devlog.nsf/0/" + documentID + "?opendocument&print=1";
 	popupWin = window.open( url, 'revisionHistory','width=800,height=600,left=0,top=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no');
 	popupWin.focus();
}

function viewProjectHistory()
{
   url = "/web/jsp2/history.jsp";
 	popupWin = window.open( url, 'projectHistory','width=800,height=600,left=0,top=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no');
 	popupWin.focus();
}

function viewHelp( pageName )
{
   url = "/help/help_launcher.jsp?page=" + pageName;
 	popupWin = window.open( url, 'helpWindow','width=800,height=600');
 	popupWin.focus();
}

function viewIceCategoryHelp( equipmentReference, iceCatReference )
{
   url = "/web/jsp2/iceCategoryHelp.jsp?equipment=" + equipmentReference + "&" + "icecategory=" + iceCatReference;
 	popupWin = window.open( url, 'optionList','height=600,width=400,status=no,toolbar=no,scrollbars=yes,resizable=yes,menubar=no,location=no');
 	popupWin.focus();
}

/**********************************************************************
 Method: setHelpLink
 Params: pageName   The value to pass to the viewHelp function to view context sensitive help.
 Return:
 Description:  Sets the href for the help link in the top frame based on a value passed in from
                     the work frame.
 author: jknuth
**********************************************************************/
function setHelpLink( pageName )
{
   anchor = top.topFrame.document.getElementById( "helpLink" );
   anchor.href = "javascript:viewHelp('" +  pageName + "')";
}

function emailFeedback()
{
   location.href = "mailto:feedback@ctrlspecbuilder.com";
}

/**********************************************************************
 Method: evaluateRadioGroup
 Params: groupName  The common name attribute of the radio group.
              spanId    The id attribute of the span tag that should be shown or hidden.
 Return:
 Description:  Hides and shows the contents of a span tag based on a radio button choice.
 author: jknuth
**********************************************************************/
function evaluateRadioGroup( groupName, spanId )
{
   if ( groupName != null )
   {
      radioButtons = document.getElementsByName( groupName );
      spanTag = document.getElementById( spanId );
      if ( radioButtons[0].checked == true )
         spanTag.style.display = "none";
      else
         spanTag.style.display = "inline";
   }
}

/**********************************************************************
 Method: editBidders
 Params:
 Return:
 Description:  Automatically selects "yes" if the user begins typing in the bidder edit fields.
 author: jknuth
**********************************************************************/
function editBidders()
{
   yesRadio = document.getElementById( "EditLimitBidders|true" );
   yesRadio.checked = true;
}

function viewUpdateChanges()
{
   openPage( "equipmentSummary.jsp" );  // Force a submit in case the user has made changes
   url= "updatePreview.jsp";
 	popupWin = window.open( url, 'updatePreview','width=800,height=600,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no');
 	popupWin.focus();
}

function promptForUpdate( equipmentReferenceName )
{
   url= "updatePromptFrame.jsp?equipment=" + equipmentReferenceName;
   popupWin = window.open( url, "updatePrompt",'width=400,height=150,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no');
   popupWin.focus();
}

function updateAllEquipment( currentPage )
{
   openPage( currentPage );  // Force a submit in case the user has made changes
   url= "updatePromptFrame.jsp?equipment=all";
   popupWin = window.open( url, "updatePrompt",'width=400,height=150,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no');
   popupWin.focus();
}

function openDocument( docId )
{
   url= "https://www.ctrlspecbuilder.com/sb/welcome.nsf/0/" + docId + "?opendocument&print=1";
   popupWin = window.open( url, "csiNumbering",'width=800,height=600,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no');
   popupWin.focus();
}

function openExternalDocument( url, windowName )
{
   popupWin = window.open( url, windowName,'width=800,height=600,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no');
   popupWin.focus();
}

function showAboutBox()
{
   url= "about_box.jsp";
   popupWin = window.open( url, "about_ctrlspecbuilder",'width=450,height=350,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no');
   popupWin.focus();
}

function updateEquipmentVersion( selectBox )
{
   equipmentVersion = selectBox.options[selectBox.selectedIndex].getAttribute( "version" );
   document.forms[0].EquipmentVersion.value = equipmentVersion;    
}