var _cookieJSLoaded = false;

var isApi="No";  //default value

function setIsApi()
{
  isApi="Yes";
}

function GetCookie(name) 
{
  var result = null;
  var cookieCopy = " " + document.cookie + ";";
  var searchKey = " " + name + "=";
  var stOfst = cookieCopy.indexOf(searchKey);
  var enOfst = 0;

  if (stOfst >= 0) 
  {
    stOfst += searchKey.length;
    enOfst = cookieCopy.indexOf(";", stOfst);
    result = unescape(cookieCopy.substring(stOfst, enOfst));
  }
  return result;
}

function SetCookie(name, value) 
{
  var argv = SetCookie.arguments;
  var argc = SetCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : null;
  var path = (argc > 3) ? argv[3] : null;
  var domain = (argc > 4) ? argv[4] : null;
  var secure = (argc > 5) ? argv[5] : false;

  if (value == null || value == "" || name == null || name == "")
    return;

  var base = '/' + self.location.href.split('/')[3];

  document.cookie = name + "=" + escape(value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    "; path=" + ((path == null) ? base : path) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
}
 

function DeleteCookie(name) 
{
  var argv = DeleteCookie.arguments;
  var argc = DeleteCookie.arguments.length;
  var path = (argc > 1) ? argv[1] : null;
  var domain = (argc > 2) ? argv[2] : null;
  if (GetCookie(name) != null) 
  {
    var ADay = 24 * 60 * 60 * 1000;
    var expireDate = new Date();

    expireDate.setTime(expireDate.getTime() - ADay);
    SetCookie(name, "0", expireDate,path,domain);
  }
}



function GetPseudoCookie(name) 
{
  var result = null;

  /*
     Since MSIE only allows 4k in all cookies for a site (instead
     of 4k for each cookie from a site, we must try to limit the
     size of data we store in our cookies.  Certain cookies will be
     stored as XML islands instead of being stored in the actual 
     cookie.  

     NOTE THAT ANYTHING STORED WITH THIS FUNCTION SHOULD NOT BE
     CONSIDERED AVAILABLE ON THE SERVER SIDE.
  */
/*
  if(navigator.appName.indexOf("Microsoft") != -1 && 
    (navigator.appVersion.indexOf("MSIE 5.") != -1 ||
     navigator.appVersion.indexOf("MSIE 6.") != -1))
*/
  if(0)
  {
    //use ie data persist
    pcookiediv.load(name);
    result=pcookiediv.getAttribute(name);
    //alert("getting " +  name + " = " + result);
  }
  else
  {
    //use netscape cookies
    result=GetCookie(name); 
  }

  return result;
}

function SetPseudoCookie(name, value) 
{
  if (value == null || value == "" || name == null || name == "")
    return;

  /*
     Since MSIE only allows 4k in all cookies for a site (instead
     of 4k for each cookie from a site, we must try to limit the
     size of data we store in our cookies.  Certain cookies will be
     stored as XML islands instead of being stored in the actual 
     cookie.  

     NOTE THAT ANYTHING STORED WITH THIS FUNCTION SHOULD NOT BE
     CONSIDERED AVAILABLE ON THE SERVER SIDE.
  */

/*
  if(navigator.appName.indexOf("Microsoft") != -1 && 
    (navigator.appVersion.indexOf("MSIE 5.") != -1 ||
     navigator.appVersion.indexOf("MSIE 6.") != -1))
*/
  if(0)
  {
    //use data persist
    //alert("setting " +  name + " = " + value);
    pcookiediv.setAttribute(name,value);
    pcookiediv.save(name);
  }
  else
  {
    //use netscape cookies
    SetCookie(name,value); 
  }
}
 

function DeletePseudoCookie(name) 
{
  /*
     Since MSIE only allows 4k in all cookies for a site (instead
     of 4k for each cookie from a site, we must try to limit the
     size of data we store in our cookies.  Certain cookies will be
     stored as XML islands instead of being stored in the actual 
     cookie.  

     NOTE THAT ANYTHING STORED WITH THIS FUNCTION SHOULD NOT BE
     CONSIDERED AVAILABLE ON THE SERVER SIDE.
  */

  /*
  if(navigator.appName.indexOf("Microsoft") != -1 && 
    (navigator.appVersion.indexOf("MSIE 5.") != -1 ||
     navigator.appVersion.indexOf("MSIE 6.") != -1))
  */
  if(0)
  {
    //use data persist
    //alert("deleting " +  name);
    pcookiediv.removeAttribute(name);
  }
  else
  {
    //use netscape cookies
    DeleteCookie(name); 
  }
 
}

//used to make sure lastSearch cookie has a current version of webfields
//this is done by updating the _m field in lastSearch to a newer one.
function updateLastSrch()
{
   var newSrch;
   var lastSrch=GetCookie("lastSearch");
   var newMtag=document.location.search;
   var oldMtag=lastSrch;
   var stPt;
   var endPt;

   if (lastSrch != null && lastSrch != "")
   {
     //extract the value of _m= from newMtag
     stPt=newMtag.search(/_m=/);
     //plus 3 to loose the _m=
     newMtag=newMtag.slice(stPt+3,newMtag.length);
     //strip off everything after the value
     endPt=newMtag.search(/&/);
     newMtag=newMtag.slice(0,endPt);
     
     //replace the old value of _m in lastSrch with the new one
     //But only if there was an old value, other wise insert the new _m
     //at the end
     endPt=oldMtag.search(/_m=/);
  
     if(endPt>-1)
     {
        //copy the first part of lastSrch to newSrch
        newSrch=oldMtag.substr(0,endPt+3);
  
        //copy the newMtag to newSrch
        newSrch+=newMtag;
  
        //copy the end of lastSrch to newSrch   
        oldMtag=oldMtag.slice(endPt+4,oldMtag.length);
        endPt=oldMtag.search(/&/);
        newSrch+=oldMtag.slice(endPt,oldMtag.length);
     }
     else
     {
        newSrch=lastSrch + "&_m=" + newMtag;
     }
  
     SetCookie("lastSearch", newSrch);
  }
}
_cookieJSLoaded = true;






