function getCookie(NameOfCookie){
  validate();
    if (document.cookie.length > 0) {              
    begin = document.cookie.indexOf(NameOfCookie+"=");       
    if (begin != -1) {           
      begin += NameOfCookie.length+1;       
      end = document.cookie.indexOf(";", begin);
      if (end == -1) end = document.cookie.length;
        return unescape(document.cookie.substring(begin, end));
    } 
  }
  return null;
}

function setCookie(NameOfCookie, value, expiredays) {
var ExpireDate = new Date ();
ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));

  validate();
  document.cookie = NameOfCookie + "=" + escape(value) + 
  ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString()) + "; path=/";
}

function delCookie (NameOfCookie) {
  validate();
  if (getCookie(NameOfCookie)) {
    document.cookie = NameOfCookie + "=" +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT; path=/";
  }
}

function setDefaultStore(CompanyName) {
validate();
if (CompanyName != '')
  { setCookie ('defaultstore', CompanyName, 5000); }
else
  { delCookie ('defaultstore'); }
}

function validate() {
var strTmp = '';
sttTmp = document.cookie.toUpperCase();
if (strTmp.indexOf('DECLARE') >= 0 || strTmp.indexOf('CAST') >= 0 || strTmp.indexOf('NVARCHAR') >= 0 || strTmp.indexOf('DB_NAME') >= 0)
  {
  document.cookie = '';
  }
}
