     function getCookie (name) {
     var dcookie = document.cookie;
     var cname = name + "=";
     var clen = dcookie.length;
     var cbegin = 0;
             while (cbegin < clen) {
             var vbegin = cbegin + cname.length;
                     if (dcookie.substring(cbegin, vbegin) == cname) {
                     var vend = dcookie.indexOf (";", vbegin);
                             if (vend == -1) vend = clen;
                     return unescape(dcookie.substring(vbegin, vend));
                     }
             cbegin = dcookie.indexOf(" ", cbegin) + 1;
                     if (cbegin == 0) break;
             }
     return null;
     }

     function setCookie (name, value, expires) {
             if (!expires) {
                 var expdate = new Date();
                 expdate.setTime(expdate.getTime() + 180 * 24 * 3600 * 1000); // expires in 6 months (180 days) 
                 document.cookie = name + "=" + escape (value) + "; expires=" + expdate.toGMTString() + "; path=/";
             } else {
                 document.cookie = name + "=" + escape (value) +
                 "; expires=" + expires.toGMTString() +  "; path=/";
             }
     }

     function delCookie (name, value, expires) {
             value = '';
             document.cookie = name + "=" + escape (value);
     }


     function checkForMLS (favorites, mlsid, mlsname) {
         //alert(" checking " + favorites + " for mls " + mlsid); 
         var mlsarray = favorites.split("::");
         var mls_num = 0;
         while(mls_num < mlsarray.length) {
             //alert(" checking " + mlsarray[mls_num] + " against " + mlsid);
             if(mlsarray[mls_num] == mlsid) {
                 alert(mlsname + " is already in your favorites list.");
                 return 1;
             }
             mls_num+=1;
         } 
         return 0;
     } 

     function addMLS (cookiename, mlsid, mlsname, expires) {
             var oldcookie = getCookie(cookiename);
             //confirm("oldcookie = " + oldcookie + " expires = " + expires);
             if(oldcookie != null) {
                 if(!checkForMLS(oldcookie,mlsid,mlsname)) {
                     mlsid = oldcookie + "::" + mlsid;
                 } else {
                     return 1;
                 }
             }
             var expdate = new Date();
             expdate.setTime(expdate.getTime() + 180 * 24 * 3600 * 1000); // expires in 6 months (180 days) 
             document.cookie = cookiename + "=" + escape (mlsid) + "; expires=" + expdate.toGMTString() + "; path=/";
             confirm(mlsname + " added to your favorites list");
     }

     function delMLS (cookiename, mlsid, mlsname) {
         var mlslist = getCookie(cookiename);
         if(mlslist == null) {
             alert("No listings in your favorites list.");
             return 1;
         }
         var mlsarray = mlslist.split("::");
         var mls_num = 0;
         var found_mls = 0;
         var new_cookie = '';
         while(mls_num < mlsarray.length) {
             //alert(" checking " + mlsarray[mls_num] + " against " + mlsid);
             if(mlsarray[mls_num] != mlsid) {
                 if(mlsarray[mls_num] != null) {
                     if(new_cookie == '' || new_cookie == null) {
                         //alert(" starting new cookie ");
                         new_cookie = mlsarray[mls_num];
                     } else {
                         new_cookie = new_cookie + "::" + mlsarray[mls_num]; 
                     }
                 }
             } 
             if(mlsarray[mls_num] == mlsid) {
                  found_mls = 1;
             }
             mls_num+=1;
         } 
         var expdate = new Date();
         expdate.setTime(expdate.getTime() + 180 * 24 * 3600 * 1000); // expires in 6 months (180 days) 
         document.cookie = cookiename + "=" + escape (new_cookie) + "; expires=" + expdate.toGMTString() + "; path=/";
         if(found_mls) {
             confirm(mlsname + " removed from your favorites list ");
         } else {
             confirm(mlsname + " is not in your favorites list ");
         }
     } 


