// 
// Cookie Handling Routines
//
function createCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/; domain=.community.vitamix.com";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) === ' ') {
            c = c.substring(1, c.length);
        }
        if (c.indexOf(nameEQ) === 0) {
            return c.substring(nameEQ.length, c.length);
        }
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

// 
// Handle additional registration fields save
//
function cleanupCookies() {
    eraseCookie('contest');
}

// Set the cookie for the site
function setUploadContestCookies(contestToSet) {
 
    // Write to the cookies
    createCookie('contest', contestToSet);
 
}

// The custom page is being used to set the contest cookie and redirect to the upload page
if (Ka.Info.PAGE == 'pages/customPage.jsp') {
	/* Set the cookie */
	setUploadContestCookies('contest');
	/* Redirect to the upload page */
	window.location = "http://community.vitamix.com/service/displayVideoUpload.kickAction?as=22442";
}

// If the URL contains the contst param the create the contest cookie
if (window.location.href.indexOf("&param=contest") != -1) {
	/* Set the cookie */
	setUploadContestCookies('contest');
}

// After registration redirect to the Contest Search page if in contest mode
// NOTE: This has to happen as early as possible in the page handling to deal with refresh issues
var contestCookie = readCookie('contest');
var contestMode = (contestCookie == 'contest');

if (contestMode) {
	if (document.referrer.indexOf("displayUserRegisterPage")) {
		if (Ka.Info.PAGE == "pages/myPlace.jsp") {
			window.location = "http://community.vitamix.com/service/searchEverything.kickAction?as=22442&mediaType=video&sortType=recent&tab=yes&includeVideo=on&d-7095067-p=1&param=contest&adminTags=contest";
		}
	}
}

