function createCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days*24*60*60*1000));
		createCookieByDate(name, value, date);
	}
    else {
        createCookieByDate(name, value, null);
    }
}

function createCookieByDate(name, value, cDate) {
	if (cDate) {
		var expires = "; expires=" + cDate.toGMTString();
	}
    else {
        var expires = "";
    }
	document.cookie = name + "=" + value + expires + "; path=/";
}

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);
}

var abTesting = true; // Set to false to stop AB Testing.

// Override AB Testing if "abTesting=false" is passed in the query string
if (window.location.search.toLowerCase().indexOf("abtesting=") != -1) {
    // Stop AB Testing
    abTesting = false;

    // Set AB Testing cookies
    if (window.location.search.toLowerCase().indexOf("abtesting=a") != -1) {
        createCookie('pfl2ab', 'a', 90);
    }
    else if (window.location.search.toLowerCase().indexOf("abtesting=b") != -1) {
        createCookie('pfl2ab', 'b', 90);
    }
}

// If cookie does not exist, place it and redirect.
if(abTesting) {
    if(!readCookie('pfl2ab')) {
	    var rand_no = Math.floor(2*Math.random());
	    if(rand_no==0) { //set cookie and leave them here
		    createCookie('pfl2ab','a',90);
        }
        else { //set cookie and redirect them
		    createCookie('pfl2ab','b',90);
		    //redirect them to B analog (if it exists)...
		    //...first,check for otherPage variable
		    //then redirect them to B analog
		    if(typeof otherPage != "undefined") {
			    document.location = "/2" + otherPage;
		    }
            else {
                document.location = "/2" + document.location.pathname + document.location.search;
            }
	    }
    }
    else if (readCookie('pfl2ab')=="b") {
	    //redirect them to B analog (if it exists)...
	    //...first,check for otherPage variable
	    //then redirect them to B analog
	    if(typeof otherPage != "undefined") {
		    document.location = "/2" + otherPage;
	    }
	    else {
            document.location = "/2" + document.location.pathname + document.location.search;
        }
    }
}

