/*======================================================================*\
|| #################################################################### ||
|| # Cookie manager with javascript ( Cookie.js ) # ||
|| # ------------------------------------------------------------------------- # ||
|| # Copyright ©2008 Mohammad Habibi. All Rights Reserved. # ||
|| # Contact Email : habibi.mh@gmail.com # ||
|| #################################################################### ||
\*======================================================================*/
var globalCookieName = 'cpk_global_cookie';

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return "";
}

function getCookieAry(c_name)
{
	var cookieSTR = getCookie(c_name);
	var ary = cookieSTR.split('|');
	return ary;
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
";path=/"+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function delCookieRecord(pid){
	if(!pid) return;
	var ary = getCookieAry(globalCookieName);
	var newString='';
	for(i=0 ; i<ary.length ; i++ ) if(ary[i]!=pid) newString +=(newString)?'|'+ary[i]:ary[i];
	setCookie(globalCookieName,newString,365);
}

function addCookieRecord(pid){
	if(!pid) return;
	var lastcookie = getCookie(globalCookieName);
	var ary = getCookieAry(globalCookieName);
	for(i=0 ; i<ary.length ; i++ ) if(ary[i]==pid) return; // if duplicate pid , not continue script
	lastcookie = (lastcookie)?lastcookie+'|'+pid:pid;
	setCookie(globalCookieName,lastcookie,365);
	return 1;
}
