// Timer Tool - A tool to measure time while answering multiple choice questions
//    Copyright (C) 2010  jb2010
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var menuEntries = [ {name: "Add Headers", functionName: "header"},
                      {name: "Reset Timer", functionName: "reset_timer"},
                      {name: "A", functionName: "answerA"},
                      {name: "B", functionName: "answerB"},
                      {name: "C", functionName: "answerC"},
                      {name: "D", functionName: "answerD"},
                      {name: "E", functionName: "answerE"} ];
  ss.addMenu("Timer Tool", menuEntries);
}

function header() {
  var doc = SpreadsheetApp.getActiveSpreadsheet();
  var lastRow = doc.getLastRow();
  var cell = doc.getRange('a1').offset(lastRow, 0);
  cell.setValue("No");
  cell.offset(0,1).setValue("Your Answer");
  cell.offset(0,2).setValue("Time Taken");
  reset_timer();
}

function reset_timer() {
  // Write the data in the text boxes back to the Spreadsheet
  var doc = SpreadsheetApp.getActiveSpreadsheet();
  var lastRow = doc.getLastRow();
  var cell = doc.getRange('a1').offset(lastRow, 0);

  var cellj1 = doc.getRange('j1');

  
//  cell.offset(0, 0).setValue('0');
//  cell.offset(0, 1).setValue('');
//  cell.offset(0, 2).setValue(Utilities.formatDate(new Date(), "GMT", "HH:mm:ss"));
    cellj1.setValue(Utilities.formatDate(new Date(), "GMT", "HH:mm:ss"));

}


function select_answer(param) {
  // Write the data in the text boxes back to the Spreadsheet
  var doc = SpreadsheetApp.getActiveSpreadsheet();
  var lastRow = doc.getLastRow();
  var cell = doc.getRange('a1').offset(lastRow, 0);
  
  var cellj1 = doc.getRange('j1');
  
  cellj1.offset(0,1).setValue(Utilities.formatDate(new Date(), "GMT", "HH:mm:ss"));
  cellj1.offset(0, 2).setFormula("=" + cellj1.offset(0,1).getA1Notation() + "-" + cellj1.getA1Notation());
  
  var no = 0;
  if(lastRow < 1)
    no = 0;
  else if(cell.offset(-1,0).getValue() == 'No')
   no = 0;
  else
    no = cell.offset(-1,0).getValue();
  
  cell.offset(0, 0).setValue( no + 1);
  
  
  cell.offset(0, 1).setValue(param);
  cell.offset(0, 2).setValue(cellj1.offset(0,2).getValue());
  
  cellj1.setValue(cellj1.offset(0,1).getValue());
}




function answerA() {
  select_answer('A');
}
function answerB() {
  select_answer('B');
}
function answerC() {
  select_answer('C');
}
function answerD() {
  select_answer('D');
}
function answerE() {
  select_answer('E');
}



