﻿/*
hexun.com javascript cookies commom
created @200-09-24
created by Yangsy
*/

// defualt Department
var defaultDept="hxck_webdev1_general";

//default Expires
var date = new Date();
var curTime = new Date().getTime();
date.setTime(curTime + (1000 * 60 * 60 * 24 * 30 * 12 * 10));	
var defaultExpires = date.toGMTString();

//end Expires
var expdate = new Date();
expdate.setTime(expdate.getTime() - 10);
var endExpires = expdate.toGMTString(); 

// default Demain
var defaultDomain = ".hexun.com";

// default Path
var defaultPath = "/";

var rootCookies = document.cookie;     

// check the existence of Cookie
function chkRootCookie()
{
    if(rootCookies)
        return true;
    else
        return null;      
}

// check the existence of Cookie that managed by one department
function chkDeptCookie(deptName)
{
   if(deptName)
   {
        if(chkRootCookie())
       {
            var deptCookie = rootCookies.match(new RegExp(deptName + "([^=;]*)"));
            if(deptCookie)
                return true;
            else
                return null;
       } 
   } 
   else
        return null;
}

// check the existence of Cookie that managed by one coder who are in one of department
function chkSubCookie(deptName, subName)
{
    if(chkRootCookie())
    {
        if(chkDeptCookie(deptName))
        {
            var deptCookie = rootCookies.match(new RegExp(subName + "([^&;]*)"));
            if(deptCookie)
                return true;
            else 
                return null
        }
        else
            return null;
    }
   else
        return null; 
}


// add Dept Cookies 
function addDeptCookie(deptName)
{
   if(chkRootCookie())
   {
        if(chkDeptCookie(deptName))
            return deptName+" does exist!";
        else
          {
                //insert a dept cookie, there may be some other depts' cookies
               //save the old cookies
                document.cookie = rootCookies + ";expires="+ defaultExpires +";domain= " + defaultDomain + ";path=" + defaultPath; 
                //add the  new cookie
                document.cookie = deptName + "=Hexun="+escape("Hexun")+";expires="+ defaultExpires +";domain= " + defaultDomain + ";path=" + defaultPath; 
               
                return deptName + " added successfully!"; 
          }
   }
   else
   {
        // add a cookie file, there is not any one cookie
        document.cookie = deptName + "=Hexun="+escape("Hexun")+";expires="+ defaultExpires +";domain= " + defaultDomain + ";path=" + defaultPath; 
        return deptName + " added successfully!"; 
   } 
}

// add the Dept Cookies with named sub
function addDeptCookieWithSubName(deptName, subName, subValue)
{
   if(chkRootCookie())
   {
        if(chkSubCookie(deptName, subName))
        {
            return  deptName + " " + subName + " does exist!";
        }
        else
          {
                //insert a dept cookie, there may be some other depts' cookies
               //save the old cookies
                document.cookie = rootCookies + ";expires="+ defaultExpires +";domain= " + defaultDomain + ";path=" + defaultPath; 
                //add the  new cookie
                document.cookie = deptName + "="+subName+"="+subValue+";expires="+ defaultExpires +";domain= " + defaultDomain + ";path=" + defaultPath; 
               
                return deptName + " added successfully!"; 
          }
   }
   else
   {
        // add a cookie file, there is not any one cookie
        document.cookie = deptName + "="+subName+"="+subValue+";expires="+ defaultExpires +";domain= " + defaultDomain + ";path=" + defaultPath; 
        return deptName + " added successfully!"; 
   } 
}


// add Sub Cookies
function addSubCookie(deptName, subName, subValue)
{
    if(chkSubCookie(deptName, subName))
        return  deptName + " " + subName + " does exist!";
    else
     {
        var arrayCookie = rootCookies.split("; ");
        for (loop=0; loop<arrayCookie.length; loop++)
        {  
           // find the deptCookie
           var deptCookie = arrayCookie[loop].match(new RegExp(deptName + "([^=;]*)"));
           if(deptCookie)                 
           {
              //find the subCookie
              var subCookie = arrayCookie[loop].match(new RegExp(subName + "([^&;]*)"));
              if(subCookie) 
                   return deptCookie[0] +" does exist!";
              else
               {
                    // insert the new sub cookie
                    document.cookie = arrayCookie[loop] + "&" + subName + "=" + subValue +  ";expires="+ defaultExpires +";domain= " + defaultDomain + ";path=" + defaultPath; 
                    return ("add new sub cookie successfully");
               } 
           }
           // if the dept doesn't exist, then call the function add a Dept with the named subcookie
           else
            {
                addDeptCookieWithSubName(deptName, subName, subValue);
                return("add dept and sub cookie syccessfully"); 
            }
        }
     }         
}        
       
  // get the dept Cookies
  function getDeptCookies(deptName) 
  {
        if(chkDeptCookie(deptName))
        {
            var arrayCookie = rootCookies.split("; ");
            for (loop=0; loop<arrayCookie.length; loop++)
            {
                 var deptCookie = arrayCookie[loop].match(new RegExp(deptName + "([^=;]*)"));
                 if(deptCookie)
                   return arrayCookie[loop];
            }
        }
        else
            return ""; 
  }     


  function getSubCookie(deptName, subName)
  {
        if(chkSubCookie(deptName, subName))
        {
            var arrayCookie = rootCookies.split("; ");
            for (loop=0; loop<arrayCookie.length; loop++)
            {
                  //find the subCookie
                  var subCookie = arrayCookie[loop].match(new RegExp(subName + "([^&;]*)"));
                  if(subCookie)                      
                       return subCookie[1].substring(1);                 
             }
        }
        else
            return "";          
  }
  
  
  
  function updateSubCookie(deptName, subName, subValue)
 {
    if(chkSubCookie(deptName, subName))
        {
            var arrayCookie = rootCookies.split("; ");            
            for (loop=0; loop<arrayCookie.length; loop++)
            {
                  //find the subCookie
                  var subCookie = arrayCookie[loop].match(new RegExp(subName + "([^&;]*)"));  
                  
                  if(!subCookie)                     
                  {                                      
                     // save the other dept cookie which have not been updated 
                     document.cookie = arrayCookie[loop] + ";expires="+ defaultExpires +";domain= " + defaultDomain + ";path=" + defaultPath;   
                     //return "the cookies unchanged are saved successfully"; 
                  }
                  else                                          
                  {
                       var stringUpdate = arrayCookie[loop].substring(arrayCookie[loop].indexOf("=")+1).split("&");
                       var ItemUpdate="";
                       for(j=0; j<stringUpdate.length; j++)
                       {
                            if(stringUpdate[j].substring(0,stringUpdate[j].indexOf("=")) == subName)
                                ItemUpdate += subName + "=" + subValue + "&";
                            else
                                ItemUpdate +=  stringUpdate[j] + "&";                         
                       }
                       // save the updated cookie                       
                      document.cookie = deptName + "=" + ItemUpdate.substring(0,ItemUpdate.length-1)  + ";expires="+ defaultExpires +";domain= " + defaultDomain + ";path=" + defaultPath; 
                      //return "the cookies changed are saved successfully";
                  }           
             }              
            return "update ok";           
        }
    else
 	    return "the subCookie does not exist!";   
 }  
  
  

   
   function lastSubCookie(deptName, subName)
   {
        if(chkSubCookie(deptName, subName))
        {
            // 是否为deptName的最后一个sunName
            var arrayCookie = rootCookies.split("; ");
            for(i=0; i<arrayCookie.length; i++)
            {
               // get the dept's cookie
               var deptCookie = arrayCookie[i].substring(arrayCookie[i].indexOf("=")+1);
               var subCookie = deptCookie.split("&");
              // the last subCookie
               if(subCookie.length==1)
                    return true;
               else
                    return null;
            }            
        }
       else
            return null; 
   }
   
   
   function lastDeptCookie()
   {
        var arrayCookie = rootCookies.split("; ");
        if(arrayCookie.length ==1)
            return true;
        else
            return null;    
   }
   
   
   function deleteSubCookie(deptName, subName)
   {
     var subValue = "hexun";
   
    if(chkSubCookie(deptName, subName))
        {
            // 是否为dept的最后一个subName
            if(lastSubCookie(deptName, subName))
            {
               // 是否为最后一个部门cookie
                if(lastDeptCookie())
               {
                    // delete rootCookie
                   document.cookie = deptName + "=" + subName +"="  + subValue + ";expires="+ endExpires +";domain= " + defaultDomain + ";path=" + defaultPath; 
                   return "delete rootCookie ok" ;
               } 
               else
               {
                    // delete deptCookie
                    var arrayCookie = rootCookies.split("; ");
                    for(i=0; i<arrayCookie.length; i++)
                    {
                        var depCookie = arrayCookie[i].match(new RegExp(deptName + "([^=;]*)"));
                        // delete the deptCookie
                       document.cookie = deptName + "=" + subName +"="  + subValue + ";expires="+ endExpires +";domain= " + defaultDomain + ";path=" + defaultPath; 
                       return "delete deptCookie ok";                      
                    }
               }
            }
            else
            {
                // delete the subName
               //---------------------------------------------------------------------------- 
                var arrayCookie = rootCookies.split("; ");            
                for (loop=0; loop<arrayCookie.length; loop++)
                {
                      //find the subCookie
                      var subCookie = arrayCookie[loop].match(new RegExp(subName + "([^&;]*)"));
                      var stringUpdate = arrayCookie[loop].substring(arrayCookie[loop].indexOf("=")+1).split("&");
                      var ItemUpdate="";
                      for(j=0; j<stringUpdate.length; j++)
                      {
                          // delete sub cookie 
                           if(stringUpdate[j].substring(0,stringUpdate[j].indexOf("=")) != subName)
                               ItemUpdate +=  stringUpdate[j] + "&";                         
                       }                                           
                        
                      document.cookie = deptName + "=" + ItemUpdate.substring(0,ItemUpdate.length-1)  + ";expires="+ defaultExpires +";domain= " + defaultDomain + ";path=" + defaultPath; 
                 } 
                 return ("del subCookie ok");
                 //--------------------------------------------             
            }
        }
        else
        {
            return ""; 
         }
   }
