var multiSingle = false;

function initCreateWager(){
    // initialize calculator
	if (multiSingle) {
		initCreateWagerMultiSingle();
		return;
	}
	
    if (document.createWager) { 
	    if (currentRiskAmount != null){
			document.createWager.riskAmount.value = currentRiskAmount; // set selected risk if true
		} else if (wagerMin < lastRiskAmount ) {
            document.createWager.riskAmount.value = lastRiskAmount; // set min bet
        } else {
            document.createWager.riskAmount.value = wagerMin; // set min bet
        }
        document.createWager.riskAmount.focus();
        wagerWin = document.createWager.riskAmount.value * wAmount / rAmount;
        wagerWin = Math.round( wagerWin * 100 ) / 100;
        document.createWager.winAmount.value = dollarize(wagerWin);
        defineEvents();
    }
}

function initCreateWagerMultiSingle(setManually) {
	var wagerCount = arrCurrentRiskAmount.length;
	
	if (document.createWager) { 
		for (var w=0;w<wagerCount;w++) {
			if (!setManually) {
				if (arrCurrentRiskAmount[w] != null) {
					document.createWager['riskAmount' + w].value = arrCurrentRiskAmount[w];
				} else if (arrWagerMin[w] < arrLastRiskAmount[w] ) {
		            document.createWager['riskAmount' + w].value = arrLastRiskAmount[w]; // set min bet
		        } else {
		            document.createWager['riskAmount' + w].value = arrWagerMin[w]; // set min bet
		        }
			}
			wagerWin = document.createWager['riskAmount' + w].value * arrWAmount[w] / arrRAmount[w];
	        wagerWin = Math.round( wagerWin * 100 ) / 100;
	        document.createWager['winAmount' + w].value = dollarize(wagerWin);
		}
		defineMultiSingleEvents();
		setMultiSingleRiskTotals();
	}
}

function setMultiSingleRiskTotals() {

	var wagerCount = arrCurrentRiskAmount.length;
	var objRiskTotal = document.getElementById('total-risk');
	var objWinTotal = document.getElementById('total-towin');
	var riskAmount = 0;
	var winAmount = 0;
	var objRisk;
	var objWin;
	var r, w;
	
	if (document.createWager) {
		for (var w=0;w<wagerCount;w++) {
			objRisk = document.getElementById('riskAmount'+w);
			riskAmount += parseFloat(objRisk.value);
			objWin = document.getElementById('winAmount'+w);
			winAmount += parseFloat(objWin.value);
		}
		
		r = new Number(riskAmount).toString();
		w = new Number(winAmount).toString();

		if (riskAmount < 0 || r == 'NaN')	{
			objRiskTotal.innerHTML = '0';
		} else {
			objRiskTotal.innerHTML = dollarize(riskAmount);
		}
		
		if (winAmount < 0 || w == 'NaN')	{
			objWinTotal.innerHTML = '0';
		} else {
			objWinTotal.innerHTML = dollarize(winAmount);	
		}
	}
	
}


/********** Used by updateWager.jsp **********************/
function initUpdateWager(){
    // focus the place bet button
    document.placeBetForm.placeBet.focus();
}


//*************** calculator *******************//

function defineEvents() {
	document.createWager.riskAmount.onkeyup=setWinAmount;
	document.createWager.winAmount.onkeyup=setRiskAmount;
}

function defineMultiSingleEvents() {
	var wagerCount = arrCurrentRiskAmount.length;
	var objRisk;
	var objWin;
	
	if (document.createWager) { 
		for (var w=0;w<wagerCount;w++) {
			objRisk = document.getElementById('riskAmount'+w);
			objWin = document.getElementById('winAmount'+w);
			objRisk.onkeyup=setMultiWinAmount;
			objWin.onkeyup=setMultiRiskAmount;
		}
	}
}


function setMultiWinAmount() {
	var riskAmount = this.value;
	var n = new Number(riskAmount).toString();
	var index = this.id.split('riskAmount')[1];
	var objWin = document.getElementById('winAmount'+index);
	
	stripAlpha(this);
	
	if (riskAmount < 0 || n == 'NaN')	{
		objWin.value = 0;
	} else {
		wagerWin = riskAmount * arrWAmount[index] / arrRAmount[index];
	    wagerWin = Math.round( wagerWin * 100 ) / 100;
		objWin.value = dollarize(wagerWin);
	}
	setMultiSingleRiskTotals();
}

function stripAlpha(field) {
	var val = field.value;
	//only allow numbers and decimal
	field.value = val.replace(/[^0-9.]/i,'');
}

function setMultiRiskAmount() {
	var winAmount = this.value;
	var n = new Number(winAmount).toString();
	var index = this.id.split('winAmount')[1];
	var objRisk = document.getElementById('riskAmount'+index);

	stripAlpha(this);
	
	if (winAmount < 0 || n == 'NaN')	{
		objRisk.value = 0;
	} else {
		wagerRisk = winAmount * arrRAmount[index] / arrWAmount[index];
	    wagerRisk = Math.round( wagerRisk * 100 ) / 100;
		objRisk.value = dollarize(wagerRisk);
	}
	setMultiSingleRiskTotals();
}


// turn incoming expression into a dollar value
function dollarize (expr) {
	return format(expr,2);
}

function setWinAmount() {
	var riskAmount = document.createWager.riskAmount.value;
	var n = new Number(riskAmount).toString();
	
	stripAlpha(this);
	
	if (riskAmount < 0 || n == 'NaN')	{
		document.createWager.winAmount.value = 0;
	} else {
	    wagerWin = document.createWager.riskAmount.value * wAmount / rAmount;
	    wagerWin = Math.round( wagerWin * 100 ) / 100;
	    document.createWager.winAmount.value = dollarize(wagerWin);
	}
}

function setRiskAmount() {
	var winAmount = document.createWager.winAmount.value;
	var n = new Number(winAmount).toString();
	
	stripAlpha(this);
	
	if (winAmount < 0 || n == 'NaN')	{
		document.createWager.riskAmount.value = 0;
	} else {
	    wagerRisk = document.createWager.winAmount.value * rAmount / wAmount;
	    wagerRisk = Math.round( wagerRisk * 100 ) / 100;
	    document.createWager.riskAmount.value = dollarize(wagerRisk);
	}
}

// This is set globally in checkWC so that I can see it's value to verify against the maximum number of multi-singles
var globalTotalNum = 0;

function verifyMultiSingles() {
	var MAX_SINGLES = 20;
	
	if (globalTotalNum > MAX_SINGLES) {
		alert('You may only place a maximum of ' + MAX_SINGLES + ' single bets at one time.\nYou have selected ' + globalTotalNum + ' single bets.\n\nPlease uncheck ' + (globalTotalNum-MAX_SINGLES) + ' of them and try again.');
		return false;
	} else {
		return true;
	}
}

// this function takes care of the decimal place formating
function format (expr, decplaces) {
	var str = "" + Math.round (eval(expr) * Math.pow(10,decplaces));
	while (str.length <= decplaces) {
    	str = "0" + str;
	}
	var decpoint = str.length - decplaces;
	return str.substring(0,decpoint) + "." + str.substring(decpoint,str.length);
}
// ******************* end calculator *************************//

/********** Disable Bet Button **********************/

function disableBetButton(){ /*unused*/
document.placeBetForm.placeBet.disabled = true;
}

/***************** Bet functions *********************/
function cancel(targetURL) {
	document.location.href = targetURL;
}

function placeBet() {
    document.createWager.action = "UpdateWager";
    document.createWager.submit();
}

function switchPicks() {
    document.createWager.action = "SwitchPicks";
    document.createWager.submit();
}

function addPicks() {
    document.createWager.action = "AddPick";
    document.createWager.submit();
}

function addPicksOnly() {
    document.wagerLocation.action = "/sportsbook/app/AddPick";
    document.wagerLocation.submit();
}

function changeBuyPoints() {
    document.createWager.action = "CreateWager";
    document.createWager.submit();
}

function repostMultiSingle() {
    document.placeBetForm.action = "CreateWager";
    document.placeBetForm.submit();
}



function setTeaserOption( optionID ) {
    document.createWager.action = "CreateWager";
    document.createWager.teaserOption.value = optionID;
    document.createWager.submit();
}

// disable form buttons

function updateWagerButAction(){
document.placeBetForm.submit();
document.placeBetForm.placeBet.disabled=true; 
document.placeBetForm.Cancel.disabled=true; 
document.placeBetForm.reenter.disabled=true; 
}


// *******sports menu dropdown handler ************/
function formHandler(PICK){ /*unused*/
		if (document.wagerLocation.selectSport.value == "#") {
		return;
		} else {
		document.location = PICK.options[PICK.selectedIndex].value;
	}
}

/*********Schedule Form Buttons Handler *******/
function doSomething(){ /*unused*/
    alert('action');
}

var isProp = false; // used to check for futures/props
var isTeasable = false; // used to check for teasable events

function checkWC() {

	var checkBoxes = document.getElementsByName("lineID")
    var len = checkBoxes.length
    var checkedNum = 0;
     
    for( var i=0; i<len; i++ ) {
        if( checkBoxes[i].checked ) {
            checkedNum ++;
        }
    }
    
    var pickBoxes = document.getElementsByName("pickIndex")
    var picklen = pickBoxes.length
    var pickNum = 0;
     
    for( var i=0; i<picklen; i++ ) {
        if( pickBoxes[i].checked ){
            pickNum ++;
        }
    }

    var totalNum = checkedNum + pickNum;
	globalTotalNum = totalNum;

    // Disable all Buttons
    if( totalNum == 0 ) {
		deactivateButton( "toolbar-single" );
		deactivateButton( "toolbar-if-bet" );		
		deactivateButton( "toolbar-reverse" );
		deactivateButton( "toolbar-parlay" );			
		deactivateButton( "toolbar-teaser" );
		deactivateButton( "toolbar-round-robin" );

		/*		
        document.wagerLocation.single.disabled = true;
		//document.wagerLocation.single.value = 'Single'		
        document.wagerLocation.single.className = "toolbarButtonDis";
        document.wagerLocation.ifbet.disabled = true;
        document.wagerLocation.ifbet.className = "toolbarButtonDis";
        document.wagerLocation.reverse.disabled = true;
        document.wagerLocation.reverse.className = "toolbarButtonDis";
        document.wagerLocation.parlay.disabled = true;
        document.wagerLocation.parlay.className = "toolbarButtonDis";
        document.wagerLocation.teaser.disabled = true;
        document.wagerLocation.teaser.className = "toolbarButtonDis";
        document.wagerLocation.roundrobin.disabled = true;
        document.wagerLocation.roundrobin.className = "toolbarButtonDis";
		*/
    // Disable parlay/teaser wager buttons
    } else if( totalNum == 1 ) {
		activateButton( "toolbar-single" );
		deactivateButton( "toolbar-if-bet" );		
		deactivateButton( "toolbar-reverse" );
		deactivateButton( "toolbar-parlay" );			
		deactivateButton( "toolbar-teaser" );
		deactivateButton( "toolbar-round-robin" );
	
		/*
        document.wagerLocation.single.disabled = false;
		//document.wagerLocation.single.value = 'Single';
        document.wagerLocation.single.className = "toolbarButton";
        document.wagerLocation.ifbet.disabled = true;
        document.wagerLocation.ifbet.className = "toolbarButtonDis";
        document.wagerLocation.reverse.disabled = true;
        document.wagerLocation.reverse.className = "toolbarButtonDis";
        document.wagerLocation.parlay.disabled = true;
        document.wagerLocation.parlay.className = "toolbarButtonDis";
        document.wagerLocation.teaser.disabled = true;
        document.wagerLocation.teaser.className = "toolbarButtonDis";
        document.wagerLocation.roundrobin.disabled = true;
        document.wagerLocation.roundrobin.className = "toolbarButtonDis";
		*/
		
    // Disable Single Wager Button
    } else if( totalNum == 2 ) {
		activateButton( "toolbar-single" );
		activateButton( "toolbar-if-bet" );		
		activateButton( "toolbar-reverse" );		
		activateButton( "toolbar-parlay" );				
		deactivateButton( "toolbar-round-robin" );	
	
		/*
        document.wagerLocation.single.disabled = false;
		//document.wagerLocation.single.value = 'Multi';
        document.wagerLocation.single.className = "toolbarButton";
        document.wagerLocation.ifbet.disabled = false;
        document.wagerLocation.ifbet.className = "toolbarButton";
        document.wagerLocation.reverse.disabled = false;
        document.wagerLocation.reverse.className = "toolbarButton";
        document.wagerLocation.parlay.disabled = false;
        document.wagerLocation.parlay.className = "toolbarButton";
        document.wagerLocation.teaser.disabled = false;
        document.wagerLocation.teaser.className = "toolbarButton";
        document.wagerLocation.roundrobin.disabled = true;
        document.wagerLocation.roundrobin.className = "toolbarButton";
		*/
    } else {
		activateButton( "toolbar-single" );
		deactivateButton( "toolbar-if-bet" );		
		deactivateButton( "toolbar-reverse" );
		activateButton( "toolbar-parlay" );			
		activateButton( "toolbar-round-robin" );	
	
		/*
        document.wagerLocation.single.disabled = false;
		//document.wagerLocation.single.value = 'Multi';
        document.wagerLocation.single.className = "toolbarButtonDis";
        document.wagerLocation.ifbet.disabled = true;
        document.wagerLocation.ifbet.className = "toolbarButtonDis";
        document.wagerLocation.reverse.disabled = true;
        document.wagerLocation.reverse.className = "toolbarButtonDis";
        document.wagerLocation.parlay.disabled = false;
        document.wagerLocation.parlay.className = "toolbarButton";
        document.wagerLocation.teaser.disabled = false;
        document.wagerLocation.teaser.className = "toolbarButton";
        document.wagerLocation.roundrobin.disabled = false;
        document.wagerLocation.roundrobin.className = "toolbarButton";
		*/
    }

	if (isTeasable && totalNum >= 2 ){
		activateButton( "toolbar-teaser" );
	} else {
		deactivateButton( "toolbar-teaser" );		
	}	
	   
    if (checkedNum >= 1) {
	
//        document.wagerLocation.addtopicks.disabled = false;
//        document.wagerLocation.addtopicks.className = "toolbarButton";
    } else {
//        document.wagerLocation.addtopicks.disabled = true;
//        document.wagerLocation.addtopicks.className = "toolbarButtonDis";
    }
	
      
   // disable parlay and teaser buttons if only props
   if (isProp == true && checkedNum >= 1) { 
		activateButton( "toolbar-single" );
		deactivateButton( "toolbar-if-bet" );		
		deactivateButton( "toolbar-reverse" );
		deactivateButton( "toolbar-parlay" );			
		deactivateButton( "toolbar-teaser" );
		deactivateButton( "toolbar-round-robin" );   

		/*   
       document.wagerLocation.parlay.disabled = true;
       document.wagerLocation.parlay.className = "toolbarButtonDis";
       document.wagerLocation.teaser.disabled = true;
       document.wagerLocation.teaser.className = "toolbarButtonDis";
       document.wagerLocation.ifbet.disabled = true;
       document.wagerLocation.ifbet.className = "toolbarButtonDis";
       document.wagerLocation.reverse.disabled = true;
       document.wagerLocation.reverse.className = "toolbarButtonDis";
       document.wagerLocation.roundrobin.disabled = true;
       document.wagerLocation.roundrobin.className = "toolbarButtonDis";
	   */
   }
}

function activateButton( buttonName ) {
	var disabledTopButton = document.getElementById(buttonName + '-top-disabled');
	if (disabledTopButton) { disabledTopButton.style.display = 'none'; }	
	var activeTopButton = document.getElementById(buttonName + '-top-active');
	if (activeTopButton) { activeTopButton.style.display = 'block';	}

	var disabledBottomButton = document.getElementById(buttonName + '-bottom-disabled');
	if (disabledBottomButton) { disabledBottomButton.style.display = 'none'; }	
	var activeBottomButton = document.getElementById(buttonName + '-bottom-active');
	if (activeBottomButton) { activeBottomButton.style.display = 'block';	}
	
}

function deactivateButton (buttonName ) {
	var activeTopButton = document.getElementById(buttonName + '-top-active');
	if (activeTopButton) { activeTopButton.style.display = 'none';	}
	var disabledTopButton = document.getElementById(buttonName + '-top-disabled');
	if (disabledTopButton) { disabledTopButton.style.display = 'block'; }	

	var activeBottomButton = document.getElementById(buttonName + '-bottom-active');
	if (activeBottomButton) { activeBottomButton.style.display = 'none'; }
	var disabledBottomButton = document.getElementById(buttonName + '-bottom-disabled');
	if (disabledBottomButton) { disabledBottomButton.style.display = 'block';}
}

/************** used by Bet History ************/
function betHistoryHandler(x){
		var period = x;
		if (document.betHistoryForm.timePeriod.value == "#") {
		return;
		} else {
		document.location = '/sportsbook/app/ViewSettledWagers?period='+period;
	}
}

// *************** used by the reload lines button ************//

function reloadLines() {
var event = document.location;
	document.location.href = event;
}

// ************** used by createTeaser.jsp to show sweetheart teaser info //
function showTeaserInfo(){
if (!document.getElementById){
openHelp('sportsbook/wager-teaser.jsp');
} else {
document.getElementById('sweetheartInfo').style.display = 'block';
}
}

function hideTeaserInfo(){
document.getElementById('sweetheartInfo').style.display = 'none';
}

var timeoutID = null;

function markToolbarLocation() { /*unused*/
    var scheduleToolbar = document.getElementById('scheduleToolbar');

    if ( scheduleToolbar ) {
        if( timeoutID != null ) {
            clearTimeout( timeoutID );
        }
        scheduleToolbar.style.visibility = "hidden";
        timeoutID = setTimeout( "positionToolbar()", 300 );
    }
}

function positionToolbar() { /*unused*/ /*except for above*/
    var ie = (navigator.userAgent.indexOf("MSIE 6.") != -1);

    var scheduleToolbar = document.getElementById('scheduleToolbar');
    var masterBlock = document.getElementById('master-content-block');
    
    if( document.documentElement.scrollTop ){
        var viewTop = document.documentElement.scrollTop;
    } else {
        var viewTop = document.body.scrollTop;
    }
    var scheduleTop = masterBlock.offsetTop + 64;
    
    if( viewTop  > scheduleTop ) {
        var offsety = ( viewTop - scheduleTop ) + 64;

        if( ie ) {
            scheduleToolbar.style.visibility = "hidden";
        }
        
        scheduleToolbar.style.top = offsety + 'px';
        scheduleToolbar.style.left = '190px';
        
        if( ie ) {
            scheduleToolbar.filters[0].Apply();
        }
        
        scheduleToolbar.style.visibility = "visible";
        
        if( ie ) {
            scheduleToolbar.filters[0].Play();
        }
    } else {
        scheduleToolbar.style.top  = '64px';
        scheduleToolbar.style.left = '190px';
        scheduleToolbar.style.visibility = "visible";
    }
}


function ViewPreferences( strFilter, strSort, strPicksViewState, strOddsFormat ){
    this.eventFilter = strFilter;
    this.eventSort = strSort;
    this.picksViewState = strPicksViewState;
    this.oddsFormat = strOddsFormat;
}

ViewPreferences.prototype.setFilter = function( strFilter ) {
    this.eventFilter = strFilter;
    this.setCookie();
    document.location.replace( document.location.href );
}

ViewPreferences.prototype.setSort = function( strSort ) {
    this.eventSort = strSort;
    this.setCookie();
    document.location.replace( document.location.href );
}

ViewPreferences.prototype.setPicksViewState = function( strPicksViewState ) {
    this.picksViewState = strPicksViewState;
    this.setCookie();
    //document.location.replace( document.location.href );
}


ViewPreferences.prototype.setOddsFormat = function( strOddsFormat ) {
    this.oddsFormat = strOddsFormat;
    this.setCookie();
    document.location.replace( document.location.href );
}

ViewPreferences.prototype.setCookie = function() {
    if ( this.oddsFormat == null ) {
        document.cookie = "ScheduleViewPrefs=filter:"+ this.eventFilter + "&sort:" + this.eventSort + "&picks:" + this.picksViewState + "; path=/";
    } else {
        document.cookie = "ScheduleViewPrefs=filter:"+ this.eventFilter + "&sort:" + this.eventSort + "&picks:" + this.picksViewState + "&oddsFormat:" + this.oddsFormat + "; path=/";
    }
}

function ScheduleMenu(){
    this.openBranches = new Array();
}

ScheduleMenu.prototype.toggleBranch = function( elmNode ){  
    var branch = elmNode.parentNode;
    if( branch.className == "branch-closed" ) {
        //closeOpenBranches();
        this.addOpenBranch( branch.id )
        branch.className = "branch-open";
    } else {
        this.removeOpenBranch( branch.id )
        branch.className = "branch-closed";
    }
    
	if (branch.id == "Basketball" && branch.className == "branch-open"){		
		this.setCookie( "basketball", "open" );
	} else	if (branch.id == "Basketball" && branch.className == "branch-closed"){		
		this.setCookie( "basketball", "closed" );
	} else {
	    this.setCookie();
	}
}

ScheduleMenu.prototype.addOpenBranch = function( strId) {
    this.openBranches[ this.openBranches.length ] = strId;
}

ScheduleMenu.prototype.removeOpenBranch = function( strId) {
    var newOpenBranches = new Array();
    
    var len = this.openBranches.length; 
    for( i=0; i<len; i++ ) {
        var currId = this.openBranches[i];
        if( strId != currId ){
            newOpenBranches[ newOpenBranches.length ] = currId;
        }
    }
    
    this.openBranches = newOpenBranches;
}

ScheduleMenu.prototype.closeOpenBranches = function() {
    var len = this.openBranches.length; 
    for( i=0; i<len; i++ ) {
        var branch = document.getElementById( openBranches[i] );
        branch.className = "branch-closed";
    }
    this.openBranches = new Array()
}

ScheduleMenu.prototype.setCookie = function( branchId , state ) {
    var cookieValue = "";
    var len = this.openBranches.length; 
    for( i=0; i<len; i++ ) {
        cookieValue += this.openBranches[i];
        if( i < ( len -1 ) ) {
            cookieValue += '&';
        }
    }
	
	if (branchId == "basketball"){
	    document.cookie = "ScheduleMenuFootballConfig="+ state +"; path=/";	
	} 

    document.cookie = "ScheduleMenuConfig="+cookieValue+"; path=/";


}

// used for date and time stamp in sportsbook toolbar
    function writeTime() { /*unused*/ /* okay, not really.. but it should be */
    var timeStamp = document.getElementById( "timestamp" );
    
    if( timeStamp == null || timeStamp.innerHTML == null ){
        return;
    }
    
    
    var d = new Array();
    var weekday = new Array( "Sun","Mon","Tue","Wed","Thu","Fri","Sat" );
    var monthname = new Array( "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" );
    
    var today = new Date( msStartTime );
    
    var dayname = weekday[ today.getUTCDay() ];
    var month = monthname[ today.getUTCMonth() ];
    var numdate = today.getUTCDate();
    var hours = today.getUTCHours();
    var minutes = today.getUTCMinutes();
    var seconds = today.getUTCSeconds();
    
    var ampm = ( hours >= 12 )?'p':'a';
    
    if ( hours == 0 ) {
        hours = 12;
    }

    if ( hours > 12 ){
        hours -= 12;
    }
        
    minutes = fixTime( minutes );
    seconds = fixTime( seconds );

    var time = dayname + " " + month + ". " + numdate + ", " + hours + ":" + 
    minutes +  ":" + seconds + ampm + " " + serverTimeZone;
    
    var currentStamp = timeStamp.innerHTML;

    if (currentStamp != time ){
        timeStamp.innerHTML = time;
    }
    
    msStartTime += 500;
    timeout= setTimeout( 'writeTime();',500 );
}


function writeLocalTime() { /*unused*/
    var timeStamp = document.getElementById( "timestamp" );
    
    if( timeStamp == null || timeStamp.innerHTML == null ){
        return;
    }
    
    var d = new Array();
    var weekday = new Array( "Sun","Mon","Tue","Wed","Thu","Fri","Sat" );
    var monthname = new Array( "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" );
    
    var today = new Date( msStartTime );
    
    var dayname = weekday[ today.getDay() ];
    var month = monthname[ today.getMonth() ];
    var numdate = today.getDate();
    var hours = today.getHours();
    var minutes = today.getMinutes();
    var seconds = today.getSeconds();
    
    var ampm = ( hours >= 12 )?'p':'a';
   
    if ( hours == 0 ) {
        hours = 12;
    }

    if ( hours > 12 ){
        hours -= 12;
    }
        
    minutes = fixTime( minutes );
    seconds = fixTime( seconds );

    var time = dayname + " " + month + ". " + numdate + ", " + hours + ":" + 
    minutes +  ":" + seconds + ampm + " " + serverTimeZone;
    
    var currentStamp = timeStamp.innerHTML;

    if (currentStamp != time ){
        timeStamp.innerHTML = time;
    }
    
    msStartTime += 500;
    timeout= setTimeout( 'writeTime();',500 );
}

function fixTime( time ) { /*unused*/
    if (time < 10){
        time = "0" + time;
    }
    return time;
}

function submitWager(type){

	var wagerVar = document.getElementById("wagerType");
	wagerVar.value = type;
	if (type == 'Single' && verifyMultiSingles()){
		document.wagerLocation.submit();
	}
	
	if (type == 'Parlay'){
		document.wagerLocation.submit();		
	}
	
	if (type == 'Round Robin'){
		roundRobinParlay();
		document.wagerLocation.submit();			
	}
	
	if (type == 'If Bet'){
		document.wagerLocation.submit();			
	}	

	if (type == 'Reverse'){
		document.wagerLocation.submit();			
	}			

	if (type == 'Teaser'){
		document.wagerLocation.submit();			
	}	
	if (type == 'Accumulator'){
		document.wagerLocation.submit();			
	}
}

function pressButton(name,image)
{
	document[name].src='/images/sportsbook/' + image;
}	


function selectbillboard(id){/* billboards/spotlights */ /*unused*/ /* i think */

	if (selectedspotlight != id){
	
		if (selectedspotlight != ""){
			var oldspotlight = document.getElementById('spotlight-' + selectedspotlight);
			oldspotlight.style.background = 'url(/images/sportsbook/spotlight-off.gif)';

			var oldspotlightarrow = document.getElementById('spotlight-arrow-' + selectedspotlight);
			oldspotlightarrow.style.background = '#000';
			
			var oldpromoblock =  document.getElementById('promoblock-' + selectedspotlight);				

			if (oldpromoblock){
				oldpromoblock.style.display = 'none';			
			} else {
				oldpromoblock =  document.getElementById('promoblock-default-' + selectedspotlight);				
				oldpromoblock.style.display = 'none';								
			}
		}
				

				
		if (  ! document.getElementById('spotlight-' + id) ){
			return false;
		}
		
		
		var newspotlight = document.getElementById('spotlight-' + id);		
		newspotlight.style.background = 'url(/images/sportsbook/spotlight-selected.gif)';	

		var newspotlightarrow = document.getElementById('spotlight-arrow-' + id);
		newspotlightarrow.style.background = 'url(/images/sportsbook/spotlight-arrow.gif)';	

		var newpromoblock =  document.getElementById('promoblock-' + id);	
		
		if (newpromoblock){
			newpromoblock.style.display = 'block';				
		} else {
			newpromoblock =  document.getElementById('promoblock-default-' + id);				
			newpromoblock.style.display = 'block';										
		}

		selectedspotlight = id;		

		return true;
	}
}


function activatespotlight(id){/*unused*//*i think*/
	// swap background image
	if (id != selectedspotlight){
		var spotlight = document.getElementById('spotlight-' + id);		
		spotlight.style.background = "url(/images/sportsbook/spotlight-on.gif)";
		spotlight.style.cursor = "pointer";
	}
}

function deactivatespotlight(id){/*unused*//*i think*/
	// swap background image
	if (id != selectedspotlight){
		var spotlight = document.getElementById('spotlight-' + id);		
		spotlight.style.background = "url(/images/sportsbook/spotlight-off.gif)";
		spotlight.style.cursor = "default";		
	}
	
}

/* used in racebook-offline.jsp, but does nothing */
function onloadBillboard(  promoParameter, promoFirst ){

	var promofound = false;
	
	if (promoParameter != ''){
			promofound = selectbillboard( promoParameter );	
	}

	if (!promofound ){
		promofound = selectbillboard( promoFirst );		
	}

	if (!promofound){

		// hide the billboards
//		var billboards = document.getElementById('billboard');
//		billboards.style.display = 'none';
		
		// show default
	
	}				

}

function popUp(name, url){	
	newWin=window.open(url,name,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,width=1024,height=800');
	if(!newWin){
		popupDisabledMsg();
	}
	newWin.focus();
}


function  popupDisabledMsg() {	
		var errorHTML = "";
		errorHTML += "<div class=\"no-script\">";
		errorHTML += "	<div id=\"error-icon\"><img src=\"/images/template/warnings/icon-error-large.gif\" align=\"left\" alt=\"Error\"/></div>";
		errorHTML += "	<div id=\"error-copy\">";
		errorHTML += "		<span id=\"error-copy-warning\" >It seems your browser's popup is blocked. You have to click the \"Live\" button again.<br/></span>";
		errorHTML += "	</div>";
		errorHTML += "</div>";	
		document.getElementById("popup-disabled-alert").innerHTML = errorHTML;	
} 

 function selectAllPicks(){
 	var picks = document.getElementsByName('pickIndex');

	if (picks){
		for (var i=0; i < picks.length; i++){
			if (picks[i].type == 'checkbox'){
				picks[i].checked  = true;
			}
		}
	}
 }
 
 function clearAllPicks(){
 	var picks = document.getElementsByName('pickIndex');

	if (picks){
		for (var i=0; i < picks.length; i++){
			if (picks[i].type == 'checkbox'){

				picks[i].checked  = false;
			}
		}
	}
 }


 /***  jQuery functions ***/
 
 // this will be taken care of in the namespace method in BODOG.js
if (!BODOG) { var BODOG = {}; }
if (!BODOG.components) { BODOG.components = {}; }

BODOG.components.sportsbook = function() {
		
	return {
		
		settings: {

		},

		init: function( options ) {
            
            // Sportsbook Menu -            
            // On page load, get the 'ScheduleMenuConfig' 
            // if it doesn't exist, collapse everything
            // else only close items which are not in the cookie
            var openbranches = $.cookie('ScheduleMenuConfig');
            
            if (!openbranches) {
                $("#menu-sportsbook ul li").css("display", "none");       
                $("#menu-sportsbook h4").attr("class", "closed");
            } else {
                $("#menu-sportsbook h4").each ( function (i) {
                
                    var branch = this.id.replace("header-","");
                    if ( openbranches.indexOf( branch ) < 0 ){
                        $("#menu-sportsbook ul#category-" + branch + " li ").css("display", "none");
                        this.className = "closed";
                    }
                })            
            }
			
            // Sportsbook menu - 
            // Assign toggle event to sports category headers
            $("#menu-sportsbook h4").click( function () {

                if ( $(this).attr( "class") == "open"  ){
                    $(this).attr("class", "closed");
                    $(this).next("ul").children().css("display", "none");
                } else {
                    $(this).attr("class", "open");
                    $(this).next("ul").children().css("display", "block");                    
                }
    
                // Get all the headers that don't have 'closed' class
                // if none of them are 'closed' then remove 'ScheduleMenuConfig' cookie
                // else iterate through id to build string of category ID seperated by '&'
                // then write 'ScheduleMenuConfig' cookie
                var closedbranches = $("#menu-sportsbook h4.open");
                if ( closedbranches.length == 0 ) {                
                    $.cookie('ScheduleMenuConfig', null  , { path: '/' });
                } else {
                    var branchid = "";
            
                    for (var i = 0; i < closedbranches.length; i++){
                        var closedbranch = closedbranches[i].id.replace("header-", "");
                        
                        if (branchid == "") {
                            branchid = closedbranch;
                        } else {
                            branchid = branchid + '&' + closedbranch;
                        }
                    }
            
                    $.cookie('ScheduleMenuConfig', branchid , { path: '/' });
                }
            });  // end $("#menu-sportsbook h4").click          
            
            
		} // end init: function

	}// end return object
}();

$(function(){
    BODOG.components.sportsbook.init( {} ) ;
});

 
 
