/*
 * Generic javascript functions for common procedures
 * requires prototype for things like string manipulation
 */
function getCookieValue(name, unescapeValue)
{
    var cookieArray = document.cookie.split(";");
    var cookieValue = "";

    var foundValue = false;
    var valueForName;
    for (var i=0; i < cookieArray.length; i++)
    {
        cookieValue = (unescapeValue ? decodeURI(cookieArray[i].strip()) : cookieArray[i].strip());

        var idx = cookieValue.indexOf(name);
        if (idx >= 0)
        {
            valueForName = cookieValue.strip().gsub(name + '=', '');
            foundValue = true;
        }
        if (foundValue)
            break;
    }

    if (valueForName)
        valueForName = (unescapeValue ? decodeURI(valueForName) : valueForName);

    return valueForName;
}

function writeCookieValue(name, value, escapeValue)
{
    /* by default, we writing the cookies to expire in 1 year - we can
     * update this later to change the expiration date, maybe with an
     * additional parameter 
     */

    var cookieValue = (escapeValue ? encodeURI(value) : value);
    document.cookie = name +'=' + cookieValue + ";expires=" + new Date(new Date().getTime() + 365*24*60*60*1000).toGMTString();    
}

function getValueFromHashString(key, hashString, unescapeHashStringFirst, escapeReturnValue)
{
    /*
     * hashString should be a string formatted as follows:
     * key1=value1|key2=value2|key3=value3...
     * unescapeHashStringFirst - whether the hashString value passed in needs to be decoded prior to processing
     * returnEscapedValue - whether the return value should be encoded prior to passing it back
     */
    var escapedHashString = (unescapeHashStringFirst ? decodeURI(hashString) : hashString);
    var valueForKey;

    var keyValuePairs = new Array();
    if (escapedHashString.indexOf('|') > 0)
    {
        keyValuePairs = escapedHashString.split('|');
    }
    else
    {
        keyValuePairs[0] = escapedHashString;
    }
    
    var foundKey = false;
    for (var i=0; i < keyValuePairs.length; i++)
    {
        var thisKey = keyValuePairs[i].split('=')[0];
        var thisValue = keyValuePairs[i].split('=')[1];
        if (thisKey == key)
        {
            valueForKey = thisValue;
            foundKey = true;
        }
        if (foundKey)
            break;
    }

    if (foundKey)
    {
        valueForKey = (escapeReturnValue ? encodeURI(valueForKey) : valueForKey)
    }
    else
    {
        valueForKey = "";
    }
    return valueForKey;
}

function updateHashStringWithKeyValuePair(key, value, hashString, unescapeHashStringFirst, escapeReturnedHashString)
{
    /*
     * hashString should be a string formatted as follows:
     * key1=value1|key2=value2|key3=value3...
     * unescapeHashStringFirst - whether the hashString value passed in needs to be decoded prior to processing
     * returnEscapedHashString - whether the return value should be encoded prior to passing it back
     */

    var foundKey = false;
    var newHashString = "";
    if (hashString)
    {
      var escapedHashString = (unescapeHashStringFirst ? decodeURI(hashString) : hashString);
      var keyValuePairs = new Array();

      keyValuePairs = escapedHashString.split('|');
      for (var i=0; i < keyValuePairs.length; i++)
      {
          var thisKey = keyValuePairs[i].split('=')[0];
          var thisValue = keyValuePairs[i].split('=')[1];
          if (thisKey == key)
          {
              newHashString += ((i == 0) ? thisKey + '=' + value : '|' + thisKey + '=' + value);
              foundKey = true;
          }
          else
          {
              newHashString += ((i == 0) ? thisKey + '=' + thisValue : '|' + thisKey + '=' + thisValue);
          }
          if (foundKey)
              break;
      }
  
      // if we didn't find the key, just append it to the end
      if (!foundKey)
      {
          newHashString = (keyValuePairs.length > 0) ? newHashString + '|' + key + '=' + value : key + '|' + value;
      }
    }
    else
    {
      // if we don't already have a hashString, just output the key-value pair as the hash
      // this is probably due to no hash existing in the first place, in which case, we're
      // creating it now
      newHashString = key + '=' + value;
    }

    return (escapeReturnedHashString ? encodeURI(newHashString) : newHashString);
}

