// $Id: knime2009new.js,v 1.2 2010/05/28 03:01:15 jon Exp $
//

// getCookie function (only) from http://webreference.com/js/column8/functions.html
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}
// end getCookie




function restore_choices (cookiename, logincookie, theform, delimiter) {

//  alert("restore_choices function");
//  alert("cookiename: " + cookiename);
//  alert("logincookie: " + logincookie);
//  alert("theform: " + theform);
//  alert("delimiter: " + delimiter);

  var include_data = false;
  var data_filename = 'knime_workflow_example_data.zip';

  var cookievalue = getCookie(cookiename);
//  simple backwards-compatible way to check for
//  undefined value; define an undefined variable and check against it.
//  from http://www.webreference.com/programming/javascript/gr/column9/
  var undefined;
  if (cookievalue == undefined ) {
//    alert("Cookie " + cookiename + " not defined. Exiting.");
    return null;
  }

// If we are here, the choice cookie has been set

//  alert("cookievalue: " + cookievalue);

//  var message = "Previous script choices are available." + "\n\n" + "Would you like to restore those choices to the form?"
//  if(confirm(message)) {

    var cookievalue_array = cookievalue.split(delimiter); 
    var num_cookie_values = cookievalue_array.length;
//  alert("num_cookie_values: " + num_cookie_values);

    var num_elements = theform.length;
//  alert("num_elements: " + num_elements);

    for (i = 0; i < num_cookie_values; i++) {
//    alert("Cookie element loop: " + i);
      for (j = 0; j < num_elements; j++) {
//      alert("Form element loop: " + j);
        if (theform.elements[j].value == cookievalue_array[i]) {
//        alert("Match");
          theform.elements[j].checked=true;

          if (theform.elements[j].value == data_filename) {
//            alert("data_filename matched: " + data_filename)
            include_data = true;
          }
        }
        else {
//        alert("No Match");
        }
      }
    }

    if (include_data) {
//      alert("Include data")
    }
    else {
//      alert("Don't include data")
      for (i = 0; i < num_elements; i++) {
        if (theform.elements[i].value == data_filename) {
          theform.elements[i].checked = false
        }
      }      
    }


//  alert("Choices restored.");


    var login_cookie = getCookie(logincookie);
    if (login_cookie == undefined) {
//      alert("Cookie " + logincookie + " not defined.  User has logged in.");
    }
    else {
//      alert("Cookie " + logincookie + " defined.  User has not logged in. Exiting.");
      return null;
    }

//  If we are here, the login cookie is undefined, 
//  meaning they've logged in at least once.


//  we're done restoring from the cookie, now delete it
//    document.cookie = cookiename + "=;domain=.schrodinger.com;path=/;expires=Thu, 01-Jan-1970 00:00:01 GMT"

    if (theform.read_note) {
      theform.read_note.checked=true;
    }

    for (i = 0; i < num_elements; i++) {
      if ((theform.elements[i].name == 'eula') && (theform.elements[i].value == 'Download')) {
//        alert("Clicking element: " + theform.elements[i].value)
        theform.elements[i].click()
      }
    }

//  }

}



function SelectAll(theform) {
  var num_elements = theform.length;

  for (i = 0; i < num_elements; i++) {
    if (theform.elements[i].name != "read_note") {
      theform.elements[i].checked=true;
    }
  }
}


function UpdateAll(theform) {
  var num_elements = theform.length

  SelectAll(theform);

  if (theform.read_note) {
    theform.read_note.checked=true;
  }

  for (i = 0; i < num_elements; i++) {
    if ((theform.elements[i].name == 'eula') && (theform.elements[i].value == 'Download')) {
      theform.elements[i].click()
    }
  }

}


function checkdependencies(theform) {

  var num_elements = theform.length
  var prepwizard_checked = false
  
  for (i = 0; i < num_elements; i++) {
    if (theform.elements[i].value == "prepwizard.py" && theform.elements[i].checked == true){
      prepwizard_checked = true
      break
    }
  }

  if (prepwizard_checked) {
    for (j = 0; j < num_elements; j++) {
      if (theform.elements[j].value == "protassign.py") {
        theform.elements[j].checked = true
        break
      }
    }
  }

}


function CheckNote(fieldname) {
  if (fieldname.checked == true) {
//   checkdependencies(fieldname.form)
   return true
  }
  else {
    alert("You must check the box confirming you have read the Important Notes before downloading.")
    fieldname.focus()
    return false
  }
}

function SelectByDate(theform, num_days, debug) {
   if (theform.selectbydate.className) {
     if (debug) {alert("This function is supported under your browser.")}
   }
   else {
     alert("Sorry, this function is not supported under your browser.")
     return false
   }

   if (!num_days) {
     alert("Please enter the number of days.")
     return false
   }

  var current_date = new Date()
  var current_millisec = current_date.getTime()
  if (debug) {alert("Current milliseconds: " + current_millisec)}

  if (debug) {alert("Number of days: " + num_days)}
  var num_days_adj = Number(num_days) + 1
  if (debug) {alert("Number of days (adjusted): " + num_days_adj)}
  var num_millisec = num_days_adj * 24 * 60 * 60 * 1000
  if (debug) {alert("Number of adjusted milliseconds: " + num_millisec)}

  var num_elements = theform.length

  var match_array = new Array()
  var match_array_ind = 0

  for (i = 0; i < num_elements; i++) {
    if (theform.elements[i].className) {
      var date_elements = theform.elements[i].className.split("/")
      if (date_elements.length == 3) {
         var class_adj_month = (date_elements[0] - 1)
         var class_day = date_elements[1]
         var class_year = date_elements[2]
         if (debug) {alert("Year: " + class_year + " Adjusted Month: " + class_adj_month + " Day: " + class_day)}

         var class_date = new Date(class_year, class_adj_month, class_day)
         var class_date_string = class_date.toString()
         if (debug) {alert("Class date as string:" + class_date_string)}

         var class_millisec = class_date.getTime()
         if (debug) {alert("Class milliseconds:" + class_millisec)}

         if ((current_millisec - class_millisec) <= num_millisec) {
           // theform.elements[i].checked=true;
           // match_array[match_array_ind] = theform.elements[i].value
           match_array[match_array_ind] = i
           match_array_ind++
         }

      }
    }
  }

  var num_matches = match_array.length  
  var matches = ""
  if (num_matches >= 1) {
    for (j = 0; j < (Number(num_matches) - 1); j++) {
      matches += theform.elements[match_array[j]].value + ", "
    }
    matches += theform.elements[match_array[j]].value

    var message = "Matches found: " + num_matches + "\n" + matches + "\n\n" + "Click OK to select these workflows or Cancel if you do not wish to select them." + "\n" + "You can press the form Reset button if you wish to clear all current selections and search on a different date range." + "\n\n" + "Would you like to select these workflows?"
    if(confirm(message)) {
      for (j = 0; j < num_matches; j++) {
        theform.elements[match_array[j]].checked=true
      }
    }
    return
  }
  else {
    alert("No matches found.")
    return false
  }
}


function selectworkflow(theform, checkbox, workflowname) {

  if (checkbox.checked == true) {

    var num_elements = theform.length;

    for (i = 0; i < num_elements; i++) {
      if (theform.elements[i].value == workflowname) {
        theform.elements[i].checked=true;
        alert(workflowname + " has also been selected.")
      }
    }
  }
}

function select_data(theform,thevalue) {
  var num_elements = theform.length;

  for (i = 0; i < num_elements; i++) {
    if (theform.elements[i].value == thevalue) {
      theform.elements[i].checked=true;
      alert(theform.elements[i].value + " has also been selected.")
    }
  }
}

