/*---------------------------------------------------------------*/
/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure){
  document.cookie= name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

/*---------------------------------------------------------------*/
/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
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));
}

/*---------------------------------------------------------------*/
/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain){
  if(getCookie(name)){
    document.cookie = name + "=" + 
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

/*Attention! first is executed jsrsExecute(...) and only then processCookie(...)*/
/*---------------------------------------------------------------*/
/**
 *take the string result from calling jsrsExecute.
 *if session cookie is not present on client side => request it from the server, or
 *if session cookie is present on client side but is not functional(is expired or broken)
 * => request a new one from the server
 */
function processSessionCookie(returnstring){
  var new_sess_id = returnstring;

  /*try to get current cookie*/
  var curr_sess_id = getCookie("SessionID");
  
  /*if there is no cookie at all, set a new one*/
  if(!curr_sess_id){
    setCookie("SessionID", new_sess_id, null, "/", document.domain, null);
  /*if a broken cookie is present => delete it and set a new one*/
  }else if(curr_sess_id!=new_sess_id){
    deleteCookie("SessionID", "/", document.domain);
    setCookie("SessionID", new_sess_id, null, "/", document.domain, null);
  }
}

function processVisitorCookie(returnstring){
  document.cookie= "VisitorID=" + escape(returnstring) +"; expires=Thu, 31 Dec 2099 00:00:00 UTC; path=/; domain=" + document.domain;
}
