
function GetCookie (name) {  
	var arg = name + "=";  
	var alen = arg.length;  
	var clen = document.cookie.length;  
	var i = 0;  
	while (i < clen) {    
	var j = i + alen;    
	if (document.cookie.substring(i, j) == arg)      
		return getCookieVal (j);    
		i = document.cookie.indexOf(" ", i) + 1;    
		if (i == 0) break;   
	}  
	return null;
}
function SetCookie (name, value, persists, secure, cookiepath) {
	if(null == persists)
		persists=false;
	
	var expiryInfo="";
	if(	persists){
		var exp = new Date();  
	    exp.setTime (exp.getTime() +  (50 * 24 * 60 * 60 *1000)  );  
		expiryInfo= "; expires=" + exp.toGMTString()
	}
	
	var path="";
	if(!cookiepath)
		cookiepath="/";

	path= "; path="+cookiepath+" ";

	
		
	var secureInfo="";
	if(null != secure && secure)
		secureInfo= "; secure";



	document.cookie = name + "=" + escape (value) + expiryInfo + secureInfo+path;
}


function DeleteCookie (name, path) {  
	var exp = new Date();  
	exp.setTime (exp.getTime() - 1);  
	
	var pathdata = "";
	if(null != path)
		pathdata= " path=" + path ;
	// This cookie is history  
	var cval = GetCookie (name);  
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString()+ "; " + pathdata;
}


function getCookieVal(offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}


//////////////////////////
//betting page cookie functions
var betOpsCookieName = "betOptions";
var NUM_OPTIONS = 9;
var MAXCOOKIEAGE = 2073600; //2 weeks in seconds

var PRICETYPE = 0;
var  BETSIZE = 1;
var  REVERSELAY = 2;
var  DETAILS = 3;
var  INCFEES = 4;
var  SHOWLOTS = 5;
var  BIDFIRST = 6;
var  NODDYBET = 7;
var  SHOWHELP = 8;
var  LAYTOWIN = 9;


var defaultOptions = "1110000110";

function setBetOption(optionIndex, value){
	if(isNaN(value)  || value>9 || value<0 ){
		alert("invalid option: " + value)
	    return;
	}
	
	if(optionIndex > defaultOptions.length-1){
	    alert("invalid option index: " + optionIndex)
		return;
	}
		
	var options = GetCookie(betOpsCookieName);
	if(null == options)
		options = "";

	while(options.length < optionIndex+1)
		options = options+ defaultOptions.charAt(options.length);
		
	var newOptions = "";
	
	for (var i = 0; i < defaultOptions.length; i++) {
		var oldValue = options.charAt(i);
		var defaultValue = defaultOptions.charAt(i);
		
		if(i==optionIndex)
			newOptions = newOptions +value;
	    else{
			if(oldValue != "")	
				newOptions = newOptions +oldValue;
			else
				newOptions = newOptions +defaultValue;
	    }
	}	
	//alert("oldOops:\t" + options + "\r\nnewOps:\t" + newOptions + "\r\ndefOps:\t" + defaultOptions);		

	SetCookie(betOpsCookieName , newOptions, true);
}



function getBetOption(optionIndex){
	var options = GetCookie(betOpsCookieName);

	var defaultVal = defaultOptions.charAt(optionIndex);
	if(defaultVal=="")
		defaultVal="0";
	
	var val="0";
	
	if(null == options)
		val = defaultVal;
	else{
		val = 	options.charAt(optionIndex);
		if(val == "")
			val = defaultVal;
	}
	
	//alert(val)
	return val;	
}

function getBoolBetOption(optionIndex){
	return getBetOption(optionIndex)==1?true:false;
}
function setBoolBetOption(optionIndex, boolVal){
	var value = 0;
	if(boolVal)
		value=1;

    setBetOption(optionIndex, value);

}




