var percInBeta = 30;


function getRand(x){
var randomnumber=Math.floor(Math.random()*(x+1));
//alert(randomnumber);
return randomnumber;
}

function appendQSnGo(mode){
if(mode==1){
url = 'http://www.cars.com/go/search/fs_search_results.jsp' + self.location.search;
} else {
url = 'http://beta.cars.com/go/search/search_results.jsp' + self.location.search;
}
window.location = url;
}

function checkTestGroup(){

var isBeta = false;
alert("GC:"+getcookie("betagrp"));
if(getcookie("betagrp").length < 1){
var rd = getRand(100);
if(rd < percInBeta) {writeABCookie("betagrp", "true", 30, false);isBeta=true;} else {writeABCookie("betagrp", "false", 30, false);isBeta=false;}
} else if(getcookie("betagrp") == "true") {
isBeta = true;
} else {isBeta = false;}
//alert(isBeta);
alert(getcookie("betagrp"));
return isBeta;

}

function quitTestGroup(){
removeTestGroup();
appendQSnGo(1);
}

function removeTestGroup(){
writeABCookie("betagrp", "false", 30, true);
}

/* was in compare.jsp */
function getcookie(cookiename)
{
    var cookiestring = ""+document.cookie;
    var index1 = cookiestring.indexOf(cookiename);
    if (index1==-1 || cookiename=="")
        return ""; 

    var index2=cookiestring.indexOf(';',index1);
    if (index2==-1) 
        index2=cookiestring.length; 
    return unescape(cookiestring.substring(index1+cookiename.length+1,index2));
}


/* AB cookie setter */
function writeABCookie(name, value, expires, remove)
{
	var now = new Date();
	if(remove){
	var expiration_date = new Date((new Date()).getTime() - 3600*1000);
	} else {
	var expiration_date = new Date((new Date()).getTime() + ((expires*24)*3600000));
	}
	var expiration_date = expiration_date.toGMTString();
	document.cookie = name + "=" + value + "; path=/; domain=cars.com;  expires=" + expiration_date;
}

