// Cookies.lib.js
// Loaded by the echoHTMLHead php function.
var createCookie = function (name, value, days) {
  "use strict";
  var
    date,
    expires;
  if (days) {
    date = new Date();
    date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
    expires = "; expires=" + date.toGMTString();
  } else {
    expires = "";
  }
  document.cookie = name + "=" + value + expires + "; path=/";
};

// readCookie
// Returns the data part of the requested named cookie from the document cookie string.
var readCookie = function (name) {
  "use strict";
  var
    nameEQ = name + "=",
    i,
    cookieArray,
    candidate;
  // Make an array of all the cookies that are set for this domain and path
  // by splitting the document cookie on semi-colons.
  cookieArray = document.cookie.split(';');
  // Go through all the cookies
  for (i = 0; i < cookieArray.length; i = i + 1) {
    candidate = cookieArray[i];
    // Strip leading space of the current candidate
    while (candidate.charAt(0) === ' ') { candidate = candidate.substring(1, candidate.length); }
    // If it's the one we are looking for, return it without its name.
    if (candidate.indexOf(nameEQ) === 0) { return candidate.substring(nameEQ.length, candidate.length); }
  }
  return null;
};

// cookieToForm
// Used (2010-06-11) to read cookie values into calculators including Manning Pipe Flow
var cookieToForm = function (cookieName, form) {
  "use strict";
  var
    i,
    cookie = readCookie(cookieName),
    cookieVars,
    cookieVarsLength,
    inputCounter = -1,
    selectCounter = -1,
    cookieVarSplit;
  if (cookie) {
    cookieVars = cookie.split(",");
    cookieVarsLength = cookieVars.length;
    for (i = 0; i < cookieVarsLength; i = i + 1) {
      cookieVarSplit = cookieVars[i].split(":");
      switch (cookieVarSplit[0]) {
      case 'i':
        inputCounter = inputCounter + 1;
        form.getElementsByTagName("INPUT")[inputCounter].value = cookieVarSplit[1];
        break;
      case 's':
        selectCounter = selectCounter + 1;
        form.getElementsByTagName("SELECT")[selectCounter].value = cookieVarSplit[1];
        break;
      }
    }
  }
  return cookie;
};

var formToCookie = function (form, cookieName) {
  "use strict";
  var
    i,
    cookie = "",
    formElementsLength = form.elements.length,
    element;
  for (i = 0; i < formElementsLength; i =  i + 1) {
    element = form.elements[i];
    switch (element.tagName) {
    case 'INPUT':
      if (element.type === "text") {
        cookie += ',' + 'i:' + element.value;
      }
      break;
    case 'SELECT':
      cookie += ',' + 's:' + element.value;
      break;
    }
  }
  createCookie(cookieName, cookie.substring(1), 36000);
};

