function GetCookie(name) {
   var start = document.cookie.indexOf(name+"=");
   var len = start+name.length+1;
   if ((!start) && (name != document.cookie.substring(0,name.length)))
      return null;
   if (start == -1) return null;
   var end = document.cookie.indexOf(";",len);
   if (end == -1) end = document.cookie.length;
   return unescape(document.cookie.substring(len,end));
}

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" : "");
}

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";
}

function CheckMain() {
   CookieName = "SessionID";
   Path = "/";
   Expires = new Date();
   Addto = (20 * 60 * 60 * 1000);
   Expires.setTime(Expires.getTime()+Addto);
   Secure="";
   RetrievedCookie = GetCookie(CookieName);
   RedirectCount = GetCookie("RedirectCount");
   if (RetrievedCookie && SessionID != RetrievedCookie) {
      RedirectCount = GetCookie("RedirectCount");
      if (!RedirectCount) {
         RedirectCount = 1;
      } else {
         RedirectCount = parseInt(RedirectCount) + 1;
      }
      if (RedirectCount < 9 ) {
         SetCookie("RedirectCount", RedirectCount, null, Path, Domain, Secure);
         window.location.href = "http://" + Domain + "/hamiltonbook.storefront/" + RetrievedCookie + "/Home/0/2";
      } else {
         SetCookie("RedirectCount", "0", null, Path, Domain, Secure);
         SetCookie(CookieName, SessionID, Expires, Path, Domain, Secure);
      }
   } else {
      if (!RetrievedCookie) {
         SetCookie("RedirectCount", "0", null, Path, Domain, Secure);
         SetCookie(CookieName, SessionID, Expires, Path, Domain, Secure);
      }
   }
}

function CheckSub() {
   CookieName = "SessionID";
   Path = "/";
   Expires = new Date();
   Addto = (20 * 60 * 60 * 1000);
   Expires.setTime(Expires.getTime()+Addto);
   Secure="";
   RetrievedCookie = GetCookie(CookieName);
   FileReader = document.location.href.indexOf("filereader");
   DSE = document.location.href.indexOf("/EN/");
//   if (FileReader >= 0 && !RetrievedCookie) {
//      window.location.href = "http://" + Domain + "/";
//   }
   if (FileReader < 0 && !RetrievedCookie) {
      SetCookie(CookieName, SessionID, Expires, Path, Domain, Secure);
   }
   if (DSE >= 0 && RetrievedCookie) {
      if (window.location.href.indexOf("/searchmask") >= 0) {
         window.location.href = "http://" + Domain + "/hamiltonbook.filereader?" + RetrievedCookie + "+EN/storefront/SearchMask";
      }
      if (window.location.href.indexOf("/hotdeals") >= 0) {
         window.location.href = "http://" + Domain + "/hamiltonbook.filereader?" + RetrievedCookie + "+EN/storefront/HotDeals";
      }
      if (window.location.href.indexOf("/catalog") >= 0) {
         start = window.location.href.indexOf("/catalog");
         start = start + 9;
         window.location.href = "http://" + Domain + "/hamiltonbook.filereader?" + RetrievedCookie + "+EN/catalogs/" + window.location.href.substring(start) ;
      }
      if (window.location.href.indexOf("/product/") >= 0) {
         start = window.location.href.indexOf("/product/");
         start = start + 9;
         window.location.href = "http://" + Domain + "/hamiltonbook.filereader?" + RetrievedCookie + "+EN/products/" + window.location.href.substring(start) ;
      }
   }
}
