// cookies.js

function TestStringsEqual(Str1, Str2) {
  var TSE1 = Str1.toLowerCase();
  var TSE2 = Str2.toLowerCase();
  var TSE3 = false;
  while(TSE1.length > 0 && TSE1.substring(0,1) == " ") TSE1 = TSE1.substring(1,TSE1.length);
  while(TSE1.length > 0 && TSE1.substring(TSE1.length,1) == " ") TSE1 = TSE1.substring(0,TSE1.length-1);
  while(TSE2.length > 0 && TSE2.substring(0,1) == " ") TSE2 = TSE2.substring(1,TSE2.length);
  while(TSE2.length > 0 && TSE2.substring(TSE2.length,1) == " ") TSE2 = TSE2.substring(0,TSE2.length-1);
  TSE3 = (TSE1 == TSE2);
  return TSE3;
}

function GetStrCookie(CookieName) {
  var CookieValue = "";
  var CookieTxt = "";
  var ValBeg = 0;
  var ValEnd = 0;
  if(document.cookie) {
    CookieTxt = document.cookie;
    while(CookieTxt.length > 0) {
      while(CookieTxt.length > 0 && CookieTxt.substring(0,1) == " ") CookieTxt = CookieTxt.substring(1,CookieTxt.length);
      ValBeg = 0;
      ValEnd = CookieTxt.indexOf("=");
      if (ValEnd == -1) ValEnd = CookieTxt.length;
      if(TestStringsEqual(CookieTxt.substring(ValBeg,ValEnd),CookieName) == true) {
        ValBeg = ValEnd + 1;
        ValEnd = CookieTxt.indexOf(";");
        if (ValEnd == -1) ValEnd = CookieTxt.length;
        CookieValue = CookieTxt.substring(ValBeg,ValEnd);
        CookieTxt = "";
      }
      ValBeg = CookieTxt.indexOf(";");
      if (ValBeg == -1) ValBeg = CookieTxt.length;
      ValBeg = ValBeg +1 ;
      ValEnd = CookieTxt.length;
      if (ValEnd > ValBeg) CookieTxt = CookieTxt.substring(ValBeg,ValEnd);
      else CookieTxt = "";
    }
  }
  return CookieValue;
}

function SetStrCookie(CookieName, CookieValue, ExpiresTimeSec) {
  var TimeNow = new Date();
  var ExpiresTime = new Date(TimeNow.getTime() + ExpiresTimeSec*1000);
  document.cookie = CookieName + "=" + CookieValue + "; expires="+ExpiresTime.toGMTString()+";";
}

function GoToURL(URLName) {
  if (URLName != "") window.open(URLName,"_self")
  else alert("invalid URL : '" + URLName + "'");
}


