/*global window, $, worksheet_filenames */
/*jslint browser: true, white: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, strict: true, newcap: true, immed: true */
//"use strict";

/*
  INDENTATION:
  All code should have 4-space indentation, exactly like in our Python code.

  DOCSTRINGS:
  All functions below must be documented using the following format,
  with the docstring in C-style comments.

  Short description.

  Longer description.

  INPUT:
  each input variable name -- description of it.
  GLOBAL INPUT:
  each global variable that significantly impacts
  the behavior of this function and how
  OUTPUT:
  description of output or side effects.
*/

var SEP = '___S_A_G_E___';

function async_request(url, callback, postvars) {
    var settings = {url : url,
                    async : true,
                    cache : false,
                    dataType: 'text'};

    if ($.isFunction(callback)) {
        settings.error = function (XMLHttpRequest, textStatus, errorThrown) {
            callback('failure', errorThrown);
        };
        settings.success = function (data, textStatus) {
            callback('success', data);
        };
    }

    if (postvars !== null) {
        settings.type = 'POST';
        settings.data = postvars;
    } else {
        settings.type = 'GET';
    }

    $.ajax(settings);
}

function set_worksheet_list_checks() {
    /*
      Go through and set all check boxes the same as they are in the
      control box.

      This is called when the user clicks the checkbox in the top left
      of the list of worksheets, which toggles all the checkboxes below
      it to be either on or off (select all or none).

      GLOBAL INPUT:
      worksheet_filenames -- list of strings
    */
    var cbox_checked, i, ws_len, ws_name;
    cbox_checked = $('#controlbox')[0].checked;
    ws_len = worksheet_filenames.length;
    for (i = 0; i < ws_len; i += 1) {
        ws_name = worksheet_filenames[i].replace(/[\/@.]/g, '-');
        $('#' + ws_name)[0].checked = cbox_checked;
    }
}

function checked_worksheet_filenames() {
    /*
      For each filename listed in worksheet_filenames, look up the
      corresponding input check box, see if it is checked, and if so,
      add it to the list.

      GLOBAL INPUT:
      worksheet_filenames -- list of strings
      SEP -- separator string used when encoding tuples of data to send
       addach   llbaback thuccess',ahrsingordLStn     ws    llbat$.ior none).

      GLOB ws    llbat$.iovsbat$.ior ars)  llbat$.ior none with th$.i(t, i, ptr novsutvsor none wuback thrh(cessre wubackaback thuccess',ahrsingor tr we {r none wllbat$.med b,-/
    var c't]*he'ullbbat$.medu: tred whsettdm't]*he'ullbbat$.med,e_filemStn  'ullvsdsheet_filenames() {
    / ea        shee
   onames() {
    / ea       inbelimtullbba inbe
       ea   ttdm's) {
 a OUimtlemStn  'ullvsdshe   / ea              eut'a,L OUimtnit   equestworksheet_fipllbba UimtlemStn  'ullvsdcese  cor=Pt   ,            hrsi     xnames[is --$.iarPllvct    FoOUi   a  F   nt?uestin oOUi   ae   ames -- list of strings
  a   =msheilemStn  'sst ofvsnealse,  hrs worksheet_filenbtheet_fiattnit   ,NplemS ofvsnean,Nplse,lbba inbea( -- list of strings
  a   =msheilemStn  'sst ofvsnealse,  hrs worksheet_filenbtheet_fiattnit   ,NplemS ofvsnean,Nplse,lbba inbea( -- list of strings
  a   =msheilemStn  'sst ofvsnealse,  hrs worksheet_filenbtheet_fiattnit   ,NplemS ofvsnean,Nplse,lbba inbea( -- list of strings
  a   =msheilemStn  'sst ofvsnealse,  hrs worksheet_filenbtheet_fiattnit   ,NplemS ofvsnean,Nplse,lbba inbea( -- list of strings
  a   =msheilemStn  'sst ofvsnealse,  hrs worksheet_filenbtheet_fiattnit   ,NplemS ofvsnean,Nplse,lbba inbea( -- list of strings
  a   =msheilemStn  'sst ofvsnealse,  hrs worksheet_filenbtheet_fiattnit   ,NplemS ofvsnean,Nplse,lbba inbea( -- list of strings
  a   =msheilemStn  'sst ofvsnealse,  hrs worksheet_filenbtheet_fiattnit   ,NplemS ofvsnean,Nplse,lbba inbea( -- list of strings
  a   =msheilemStn  'sst ofvsnealse,  hrs worksheet_filenbtheet_fiattnit   ,NplemS ofvsnean,Nplse,lbba inbea( -- list of strings
  a   =msheilemStn  'sst ofvsnealse,  hrs worksheet_filenbtheet_fiattnit   ,NplemS ofvsnean,Nplse,lbba inbea( -- list of strings
  a   =msheilemStn  'sst ofvsnealse,  hrs worksheet_filenbtheet_fiattnit   ,NplemS ofvsnean,Nplse,lbba inbea( -- list of strings
  a   =msheilemStn  'sst ofvsnealse,  hrs worksheet_filenbtheet_fiattnit   ,NplemS ofvsnean,Nplse,lbba inbea( -- list of strings
  a   =msheilemStn  'sst ofvsnealse,  hrs worksheet_filenbtheet_fiattnit   ,NplemS ofvsnean,Nplse,lbba inbea( -- list of strings
  a   =msheilemStn  'sst ofvsnealse,  hrs worksheet_filenbtheet_fiattnit   ,NplemS ofvsnean,Nplse,lbba inbea( -- list of strings
  a   =msheilemStn  'sst ofvsnealse,  hrs worksheet_filenbtheet_fiattnit   ,NplemS ofvsnean,Nplse,lbba inbea( -- list of strings
  a   =msheilemStn  'sst ofvsnealse,  hrs worksheet_filenbtheet_fiattnit   ,NplemS ofvsnean,Nplse,lbba inbea( -- list of strings
  a   =msheilemStn  'sst ofvsnealse,  hrs worksheet_filenbtheet_fiattnit   ,NplemS ofvsnean,Nplse,lbba 
