
	function findPos(obj) {
		var curleft = curtop = 0;
		var curscrolltop = 0;
		obj2 = obj;
		while(obj2 != document.body)
		{
			curscrolltop += obj2.scrollTop;
			obj2 = obj2.parentNode;
		}
		while (obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
		return {x:curleft, y:curtop-curscrolltop};
	}

    function offsetDate(dateStr, offsetDay, dividerStr, showYesterDayTodayTomorrow, dateFormatMDY)
    {
        var currentTime = new Date(dateStr);
        currentTime.setDate(currentTime.getDate()+offsetDay);
        if(showYesterDayTodayTomorrow)
        {
            var today = new Date();
            today.setHours(0);
            today.setMinutes(0);
            today.setSeconds(0);
            today.setMilliseconds(0);
            var diff = ((currentTime - today)/1000/24/60/60);
            if(diff == -1)
            {
                return "Yesterday";
            }
            else if(diff == 0)
            {
                return "Today"; 
            }
            else if(diff == 1)
            {
                return "Tomorrow";
            }
        }
        var month = currentTime.getMonth() + 1;
        var day = currentTime.getDate();
        var year = currentTime.getFullYear();
		if (dateFormatMDY == 'mdy') {
        	return(month + dividerStr + day + dividerStr + year);
		} else {
	        return(year + dividerStr + month + dividerStr + day);
    	}
	}
    function addDay(offsetDay)
    {
        var hiddenDate = document.getElementById("hiddenDate");
        hiddenDate.value = offsetDate(hiddenDate.value, offsetDay, "/", false, 'mdy');
        for(i=-1;i<=1;i++)
        {
            var btn = document.getElementById("dateButton"+(i+2));
            btn.innerHTML = offsetDate(hiddenDate.value, i, "/", true, 'mdy');
        }
    }
    function gotoDate(offsetDay)
    {
        var hiddenDate = document.getElementById("hiddenDate");
        var sportId = document.getElementById("sportId");
        location = "/pickem.php?sport_id="+sportId.value+"&date="+ offsetDate(hiddenDate.value, offsetDay, "-", false, 'ymd');
        
    }
    function calendarSelect(type, args, obj)
    {
  		var dateArray = args[0];
        var sportId = document.getElementById("sportId");
        location = "/pickem.php?sport_id="+sportId.value+"&date="+dateArray[0][0]+"-"+dateArray[0][1]+"-"+dateArray[0][2];
    }
    function showCalendar(e, calendarObj)
    {
        var sender = e.srcElement?e.srcElement:e.target;
        var pos = findPos(sender);
        var calendarContainer = document.getElementById(calendarObj.id).parentNode;
        calendarContainer.style.left = pos.x + "px";
        calendarContainer.style.top = pos.y + "px";
        calendarObj.show();
    }
	
	// display the form to enter an expert's picks - for each open game, hide the picker and slider columns, and show the expert column; then display the form at the bottom to enter or select the expert
	function displayExpertPicksForm() {
		for (var i = 0; i < gameList.length; i++) {
			var gameId = gameList[i];
			var iqPointColumn = document.getElementById('iqPointColumn_'+gameId);
			if (iqPointColumn == null) {
				document.getElementById('pickWinningTeamColumn_'+gameId).style.display = 'none';
				document.getElementById('sliderColumn_'+gameId).style.display = 'none';
			} else {
				document.getElementById('iqPointColumn_'+gameId).style.display = 'none';
				document.getElementById('finalScoreColumn_'+gameId).style.display = 'none';
			}
			document.getElementById('expertPickColumn_'+gameId).style.display = 'block';
		}
		document.getElementById('expertEntryBlock').style.display = 'block';
	}
	
	// check the expert entry form to make sure they completed it
	function checkExpertEntryForm(theForm) {
		var errMsg = '';
		// an expert must be selected
		var selectedExpert = theForm.expert_id.options[theForm.expert_id.selectedIndex].value;
		if (selectedExpert == '') {
			errMsg = 'You must select an expert';
		
		// if "add" was selected, make sure they filled in a name and an affiliation
		} else if (selectedExpert == 'add') {
			if (theForm.expert_name.value == '') {
				errMsg = 'You must fill in the expert\'s name';
			} else {
				var selectedAffil = theForm.expert_affil_id.options[theForm.expert_affil_id.selectedIndex].value;
				// if "add" was selected as the affiliation, make sure they filled in an affiliation name
				if (selectedAffil == '') {
					errMsg = 'You must select an affiliation for this expert';
				} else if (selectedAffil == 'add' && theForm.expert_affil.value == '') {
					errMsg = 'You must fill in the name of the new affiliation';
				}
			}
		}
		if (errMsg != '') {
			alert(errMsg);
		} else {
			theForm.submit();
		}
	}