////////////////////////////////////////////////////////////////////////////////
// windows.js --- Answer Sheet and Resource Window Script File
// Author: Brad Rudnik - bar2a@mtsu.edu - May 1999 
//         Written for the Online Version of the MTSU CSCI 217
//         Computer Science II LABORATORY MANUAL 1998-1999 Edition
//                             BY: Thomas Cheatham
//                                 Judith Hankins
//                                 Brenda Parker
//                                 Nancy Wahl 
// 
// Purpose: A JavaScript source file containing the functions used to open 
//          additional (smaller) browser windows to display an Answer Sheet 
//          and various resource materials to the student.
//
// Requirements: This file must be loaded by the HTML document and calls to 
//               these functions must be contained in that document. This file
//               uses two Perl CGI files: 
//                    AnswerSheet.pl - to produce the Answer Sheet 
//                    viewcode.pl  - to produce C++ source code files in html
//               The location of these files must be included in this file.
//               For more information on any of these requirements please see 
//               the SiteDesign file in the 217man directory.
//////

// AnswerSheet.pl directory 
//master = "http://www.mtsu.edu/cgi-bin/users/csdept/217man/AnswerSheet.pl/";
master = "http://www.mtsu.edu/cgi-bin/users/csci217/AnswerSheet.pl/";

// viewcode.pl directory
//code = "http://www.mtsu.edu/cgi-bin/users/csdept/217man/viewcode.pl";
code = "http://www.mtsu.edu/cgi-bin/users/csci217/viewcode.pl";

// window status flags
Answer_Window = null;
DisplayWindow = null; 


// open a browser window containing the lab answer sheet 
// IN: the lab calling for an answer sheet ex. AnswerWindow('lab1/lab1')
function AnswerWindow(lab) {

     // check to see if window is open --- bring it to the front if it is open 
     if (Answer_Window) {
          if (Answer_Window.open) { Answer_Window.focus(); 
          } else { Answer_Window = null ; } // if window has been closed
                                            // set flag to force reopen
     }
     
     // if window has not been opened or has been close --- open it
     if ( !Answer_Window) {

     // add lab number passed to the function to master url
     masterURL = master + lab ;  
    
     // open the window
     Answer_Window = window.open(masterURL,"Answer_Window",
                     "width=550,height=510,scrollbars,resizable,toolbar"); }

}// END AnswerWindow()

// open a browser window containing lab resource material
// IN: the page to open ex. Display('$CLA/inlab1.cc') or 
//  Display('http://www.mtsu.edu')
function Display(url){

     // check to see if window is open --- bring it to the front if it is open 
     if (DisplayWindow) {
          if (DisplayWindow.open) { DisplayWindow.focus(); } }

     // check for the CLA directory - if found at the first position
     // assume it is a source code file,  remove $CLA/ and call viewcode.pl
     if ( url.indexOf( "$CLA/" ) == 0 ) {
        arrayOfUrl = url.split ( "/" );
        url = code + "?" + arrayOfUrl[1];
     }

     // open the new window or change the contents of an open window
     DisplayWindow = window.open(url,"DisplayWindow",
                                 "width=640,height=410,scrollbars,resizable");

}// END Display()

// bring the AnswerWindow to the top and move to the desired question
// IN: the question ex. Answer('Exercise1')
function Answer(question) {
     // check to see if window is open 
     // bring it to the front & move to question if it is open
     if (Answer_Window) {
          if (Answer_Window.open) { 
             Answer_Window.focus(); 
             Answer_Window.location.hash = question ;}
          else { Answer_Window = null ; } // if window has been closed set the
                                          // flag to reopen the window
     }

     // if window has not been opened or has been close --- alert user
     if (!Answer_Window) { 
        alert('You must open the Answer Window before Exercise Links will work.'); 
     }

}// END of Answers()

// end of windows.js file
