var origCValue;

function deleteC(cookieName)    {
    if( getCookieV(cookieName) )    {
		writePersistentC(cookieName,"Pending delete","years", -1);  
        return true;  
	}
}

function writeSessionC(cookieName, cookieValue) {
    if( testSessionCookie() )     {
        document.cookie = escape(cookieName) + "=" + escape(cookieValue) + ";domain="+debugDomainName+";path=/";
        deleteC("testSessionCookie");
	    return true;
    }
    else    {
  	    return false;
    }
}

function rewriteSessionCookie(cookieName, cookieValue)    {
	document.cookie = escape(cookieName) + "=" + escape(cookieValue) + ";domain="+debugDomainName+";path=/";
	//jQuery.cookie(cookieName, cookieValue, { domain: debugDomainName, path: '/' })
	//alert(jQuery.cookie(cookieName));
}

function unloadSC(cookieName)    {
	document.cookie = escape(cookieName) + "=" + escape(origCValue) + ";domain="+debugDomainName+";path=/";
}

function getCookieV(cookieName) {
    var exp = new RegExp(escape(cookieName) + "=([^;]+)");
    
	if( exp.test(document.cookie + ";") )    {
        exp.exec(document.cookie + ";");
        return unescape(RegExp.$1);
    }
  	else    {
		return false;
	}
}

function testSessionCookie() {
    document.cookie ="testSessionCookie=Enabled;domain="+debugDomainName+";path=/";
    
	if(getCookieV("testSessionCookie")=="Enabled")    {
        return true;
    }
    else    {
        return false;
    }
}

function testPersistentC() {
    writePersistentC("testPersistentC", "Enabled", "minutes", 1);
     
	if(getCookieV("testPersistentC") == "Enabled")    {
        return true;
    }
    else    { 
        return false;
    }
}       

function writePersistentC(cookieName, cookieValue, periodType, offset) {
    var expireDate = new Date();
    offset = offset / 1;
  
    var myPeriodType = periodType;
    
	switch( myPeriodType.toLowerCase() )    {
        case "years": 
            var year = expireDate.getYear();     
            
			if(year < 1000)    {
				year = year + 1900;
			}
     		
			expireDate.setYear(year + offset);
        break;
        case "months":
            expireDate.setMonth(expireDate.getMonth() + offset);
        break;
        case "days":
            expireDate.setDate(expireDate.getDate() + offset);
        break;
        case "hours":
            expireDate.setHours(expireDate.getHours() + offset);
        break;
        case "minutes":
            expireDate.setMinutes(expireDate.getMinutes() + offset);
        break;
        default:
            //alert("Bad periodType parameter for writePersistentC() method");
        break;
    } 
  
    //document.cookie = escape(cookieName) + "=" + escape(cookieValue) + "; expires=" + expireDate.toGMTString() + ";domain="+debugDomainName+";domain="+debugDomainName+";path=/";
	document.cookie = cookieName+"="+cookieValue+";expires=Fri, 21 Dec 1976 04:31:24 GMT;domain="+debugDomainName+";path=/;";
}

function DelCookie(cookie_name)	{
	if( getCookieV(cookie_name) )    {
	    document.cookie = cookie_name + "=; expires=Fri, 21 Dec 1976 04:31:24 GMT;path=/";
	}
}

function cookieApp(cookieName, cookieAppend, siteException)    {
	if(getCookieV(cookieName) == false)   {
		if(writeSessionC(cookieName, getAdvCookie(cookieName) + cookieAppend))	{
			//
		}
		else	{
			//
		}
	}
	else	{
		origCValue = getAdvCookie(cookieName);
	
		if(origCValue.indexOf(cookieAppend) != -1)    {
			//
		}
		else    {
			deleteC(cookieName);
			rewriteSessionCookie( cookieName, (origCValue + cookieAppend) );
		}
	}

	if(!siteException) { DelCookie('site') }; 
	//ONLY cbs.com/originals should assign siteException so as to not delete the 'site' cookie. This code had to be added in case a user has cbs/originals open in 1 browser and cbs.com/primetime/cold_case/video/ open for example. In this case, because i couldn't isolate the 'site' cookie to /originals only because the video player can only read cookies at the root level, in case the 'site cookie is a live for a user because of the 2 video pages open at the same time, the ;site cookie needs to be deleted on non cbs/originals/video video pages.
	//i had to add this function call due to the webisode central new 'site' cookie. For centered paged, the above line of code is applied in sitecommon/includes/video/centered/player.php. For non nentered pages it is applied here as all non centered pages use a different video player which required type=nobc to be appended to the adv cookie, so this is the easiest place to delete the 'site' cookie for non centered video pages
}