initPage(handleLinks);
//initPage(checkDivs);
initPage(fixHeight);
initPage(adjustHeight);


function restoreDims() { 
	var mySidebar = document.getElementById("sidebar");
	var pageContent = document.getElementById("pageContent");
	var pageBackground = document.getElementById("lowerBodyTop");
	var whiteBackground = document.getElementById("bottomBodyWhite");
	var newHeightString = '0px';
	if ( pageContent )
		changeStyle('pageContent', 'minHeight', newHeightString);
	if ( mySidebar )
		changeStyle('sidebar', 'minHeight', newHeightString);
	if ( pageBackground )
		changeStyle('lowerBodyTop', 'minHeight', newHeightString);
	if ( whiteBackground )
		changeStyle('bottomBodyWhite', 'minHeight', newHeightString);
}

// 2.8.2010 Hopefully, a modular universal updater 
function silentUpdate(dbName, tableName, fieldToMatch, matchValue, fieldToChange,  sendingID, isCheckbox) {
	if ( document.getElementById('dirtyDocument') ) { 
		document.getElementById('dirtyDocument').value = 0;
	}
	if ( isCheckbox == '1' ) {
		if (document.getElementById(sendingID).checked) {
			var newValue = '1';
		}
		else {
			var newValue = '0';
		}
	} else {
	var newValue = escape(document.getElementById(sendingID).value);
	}
	var QS = 'D=' + dbName + '&T=' + tableName + '&MF=' + fieldToMatch;
	QS += '&MV=' + matchValue + '&CF=' + fieldToChange + '&NV=' + newValue;
	doAjax('/lib/ajaxDBMaster.php', QS, 'silentCallback', 'post', 0);
}

function silentCallback(text) {
	
}


// a plus sign doesn't make it through the Javascript escape function ...

function escapePlus(expr) {
	var escapedExpr = expr; 
	escapedExpr = escapedExpr.replace(/\+/g, '&#43;'); 
	return escape(escapedExpr);
}

// 11.23.2010 -- For Horizontal Menus in IE for new site
function fixMenuLink(num, id) {
if (document.all) { 
		if ( num == 1 ) {
			changeStyle(id, 'font-weight', 'bold');
			changeStyle(id, 'text-align', 'left');
		} else {
			changeStyle(id, 'font-weight', 'normal');
			changeStyle(id, 'text-align', 'left');			
		}
		
	}
}

function adjustHeight() { 
	var mySidebar = document.getElementById("sidebar");
	var pageContent = document.getElementById("pageContent");
	var pageBackground = document.getElementById("lowerBodyTop");
	var whiteBackground = document.getElementById("bottomBodyWhite");
	var infoBar = document.getElementById("infoBar");
	restoreDims();
	if ( mySidebar  && pageContent && pageBackground && whiteBackground)
	{ 
		if ( mySidebar.offsetHeight )
		{ 
			var sideHeight = mySidebar.offsetHeight; 
			sideHeight = ( sideHeight > 900 ? sideHeight + 200 : sideHeight); 
			var contentHeight = pageContent.offsetHeight;
			var newHeight = Math.max(sideHeight, contentHeight);
			newHeight = newHeight - 12;
			newHeight = Math.max(newHeight, 440); 

			newHeightString = newHeight + 'px';
			newHeightForBackground = newHeight + 197;
			newHeightForBackgroundString = newHeightForBackground + 'px';
			newHeightForWhite = newHeight + 13;
			newHeightForWhiteString = newHeightForWhite + 'px';
			changeStyle('pageContent', 'minHeight', newHeightString);
			changeStyle('sidebar', 'minHeight', newHeightString);
			changeStyle('lowerBodyTop', 'minHeight', newHeightForBackgroundString);
			changeStyle('bottomBodyWhite', 'minHeight', newHeightForWhiteString);
		
			if ( navigator.userAgent.indexOf('MSIE 6') > 0 )
			{
				changeStyle('pageContent', 'height', newHeightString);
				changeStyle('sidebar', 'height', newHeightString);
				changeStyle('lowerBodyTop', 'height', newHeightForBackgroundString);
				
			}
		}
	}
	if (infoBar) { 
		var infoHeight = infoBar.offsetTop; 
		var heightDiff = newHeight - infoHeight; 
		var newInfoHeight =  heightDiff;
		var newInfoHeightString = newInfoHeight + 'px';
		changeStyle('infoBarContent', 'minHeight', newInfoHeightString);
		if ( navigator.userAgent.indexOf('MSIE 6') > 0 ) {
			changeStyle('infoBarContent', 'height', newInfoHeightString);
		}
		
	}	
}

function changeClass( objectID, newClass)
{
	var myObject = document.getElementById(objectID);	
	myObject.className = newClass;
}

function changeStyle( objectID, cssProperty, newValue )
{ 
	//alert(objectID + ' ' + cssProperty + ' ' + newValue);
	var myObj = document.getElementById(objectID);
	myObj.style[cssProperty] = newValue;
}

function getStyle ( objectID, cssProperty )
{
	var myObj = document.getElementById(objectID);
	var myStyle = myObj.style[cssProperty];
	return myStyle;
}

function toggleDiv ( objectID )
{
	var myObj = document.getElementById(objectID);
	var myStyle = getStyle (objectID, 'display');
	if ( myStyle == 'none' )
		showDiv ( objectID );
	else
		hideDiv ( objectID );
	adjustHeight();
}




/* needs to be invoked with onerror event handler */
function errorHandler(msg, url, line )
{
	var errMsg = "Error: " + msg + "\n";
	errMsg += "URL: " + url + "\n";
	errMsg += "Line: " + line + "\n";
	alert(errMsg);
	return true;
}

function doThisSearch()
{
	var K = document.getElementById("keywords").value;
	url = '/mbs/searchSite.php?k=' + K;
	window.location.href=url;

}




//initPage(beAnnoying);

/* for fun / future use */
function findMouse(evt) {
	var evt = (evt) ? evt : ( (window.event) ? window.event : null );
	alert ( 'Browser horizontal = ' + evt.clientX + ' Browser vertical = ' + evt.clientY );
	alert ( 'Screen horizontal = ' + evt.screenX + ' Screen vertical = ' + evt.screenY );
}

function beAnnoying() {
	window.document.onclick = findMouse;
}



/* for menus */
function toggleDiv2( objectID )
{
	var myObj = document.getElementById(objectID);
	var myStyle = getStyle (objectID, 'display');
	if ( myStyle == 'none' )
		showDiv ( objectID );
	else
		hideDiv ( objectID );
	fixHeight();
	var style="";
	var divListText = "";
	for ( var i = 1; i < 200; i++ )
	{
		if ( document.getElementById('div'+i) )
		{
			style=getStyle('div'+i, 'display');
			if ( style != 'none' )
				divListText += ( i + '|');
		}
	}
	var expireDate = new Date();
	expireDate.setMonth(expireDate.getMonth()+1);
	document.cookie="menuDivs=" + divListText + "; path=/" +";expires=" + expireDate.toGMTString();
}

function sidebarCallback(text) 
{
	var linkText = text.split('||');
	var link = linkText[0];
	divText = linkText[1];
	window.location.href=link;
}

/*  Safari is only browser that can't grab values if set in stylesheet */
/* can read inline styles or dynamically set styles */
function findStyle( id, styleProp, IEStyleProp)
{
	//changeStyle( id, styleProp, '300px');
	changeClass('dragMessages', 'hideMe');
	var myObject = document.getElementById(id);
	if ( myObject.currentStyle)
		return myObject.currentStyle[IEStyleProp];
	else
		if ( window.getComputedStyle)
		{
			compStyle = window.getComputedStyle(myObject,' ');
			return compStyle.getPropertyValue(styleProp);
		}
	else
		return myObject.style[styleProp];
}

function moveObject (objectID, x, y ) 
{
	var winHt = window.outerHeight;
	var winWd = window.outerWidth;
	var myObj = document.getElementById(objectID);
	var myX = myObj.style.left;
	var baseLenX = myX.length -2;
	var myY = myObj.style.top;
	var baseLenY = myY.length -2;
	newX =parseInt(myX.substring(0, baseLenX)) + x;
	newY = parseInt(myY.substring(0, baseLenY))+ y;
	if ( newX > winWd )
		newX = 0;
	if ( newY > winHt )
		newY = 0;	
	myObj.style.left = newX +  'px';
	myObj.style.top = newY + 'px';
}

function initPage(myFunction) {
	var oldHandler = window.onload;
	
	if ( typeof oldHandler == "function" )
	{
		window.onload = function()
		{
			if ( oldHandler )
			{
				oldHandler();
			}
			myFunction();
		}
	}
	else
	{
		window.onload = myFunction;
	}
}

function openNewWindow(newPage, myW, myH, mySB)
{
	var myScroll = (mySB == 1 ? "yes" : "no" );
	var ranNum = Math.floor((Math.random() * document.links.length));
	var winName = "Win" + ranNum;
    
	var myWin = window.open(newPage,winName,'width=' + myW + ',height=' + myH + ',location=no,toolbar=no,resizable=yes,scrollbars=' + myScroll + ',left=100,top=100');

	myWin.focus(); 
	return false;
}

function doMapQuest( locationID ) {
	doAjax('/mbs/ajaxScripts/doMap.php','locationID=' + locationID ,'mapCallback','post',0);
}

function mapCallback(text) {
	var locInfo = text.split('|');
	var n2 = locInfo[0];
	var a2 = locInfo[1];
	var c2 = locInfo[2];
	var s2 = locInfo[3];
	var z2 = locInfo[4];
	//window.location.href="/map2.php?X=1&A=" + a2 + "&C=" + c2 + "&S=" + s2 + "&z=" + z2;
	var myLoc = "/map2.php?X=1&N=" + n2 + "&A=" + a2 + "&C=" + c2 + "&S=" + s2 + "&z=" + z2;
	var myNewWindow = window.open(myLoc, "mapWin",'scrollbars=yes,width=900,height=900');
	myNewWindow.focus();
	myNewWindow.moveTo(50,50);
	
}


function handleLinks() 
{
	for (var i=0; i<document.links.length; i++)
	{
		var ranNum = Math.floor((Math.random() * document.links.length));
		var tarName = "Win" + i + ranNum;
		if (document.links[i].className == "doTarget")
			document.links[i].target = tarName;
		if (document.links[i].className == "doWindow" )
			document.links[i].onclick = doWindow;
	}
	
	function doWindow()
	{
		var ranNum = Math.floor((Math.random() * document.links.length));
		var winName = "Win" + ranNum;
		var myNewWindow = window.open(this.href, winName,'width=900,height=600');
		myNewWindow.focus();
		return false;
	
	}
}

function showDiv( divName) { 
	var myDiv = document.getElementById(divName);
	myDiv.setAttribute("style", "display:block");
	myDiv.style.cssText="display:block";	
	fixHeight();
	adjustHeight();
}

function hideDiv( divName) {
	var myDiv = document.getElementById(divName);
	myDiv.setAttribute("style", "display:none");
	myDiv.style.cssText="display:none";	
}

function checkMatch(div1, div2) {
	 var myObj1 = document.getElementById(div1);
	 var myObj2 = document.getElementById(div2);
	 var myVal1 = myObj1.value;
	 var myVal2 =  myObj2.value;
	 if ( !( myVal1 == myVal2) )
	{
		alert ( "Values do not match.");
		myObj1.value = "";
		myObj2.value = "";
		myObj1.focus();
		return false;
	}
	else
		return true;
}

function validPhoneNumber(myElement)
{
   if (myElement.value.length == 0 )
        return true;
    var myRegExp = /^\(?(\d{3})\)?[\.\- ]?(\d{3})[\.\- ]?(\d{4})$/;
	validNumber = myRegExp.exec(myElement.value);
	if (validNumber)
	{
		myElement.value="(" + validNumber[1] + ") " + validNumber[2] + "-" + validNumber[3];
		return true;
	}
	else 
	{
		alert("Please enter as (xxx)xxx-xxxx or xxxxxxxxxx");
		myElement.value="1111111111";
		myElement.focus();
		return false;
	}
} 

function validPhoneNumber2(myElement)  // No non-numerics
{
	if (myElement.value.length == 0 )
        return true;
    var myRegExp = /^\(?(\d{3})\)?[\.\- ]?(\d{3})[\.\- ]?(\d{4})$/;
	validNumber = myRegExp.exec(myElement.value);
	if (validNumber)
	{
		myElement.value= validNumber[1] + validNumber[2] +  validNumber[3];
		return true;
	}
	else 
	{
		alert("Please enter as xxxxxxxxxx");
		myElement.focus();
		return false;
	}

} 

function validSSN(myElement)
{
	if (myElement.value.length == 0 )
        return true;
    var myRegExp = /^(\d{3})[\.\- ]?(\d{2})[\.\- ]?(\d{4})$/;
	validNumber = myRegExp.exec(myElement.value);
	if (validNumber)
	{
		myElement.value= validNumber[1] + "-" + validNumber[2] + "-" + validNumber[3];
		return true;
	}
	else 
	{
		alert("Please enter as xxx-xx-xxxx or xxxxxxxxx");
		myElement.focus();
		return false;
	}

} 

function validEMail(myElement)
{
	var myRegExp = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
	var myString = myElement.value;
	if ( !myString.match(myRegExp) && myString.length > 0)
	{
		alert("The e-mail address does not appear to be valid.");
	//	myElement.focus();
		return false;
	}
	else
	{
		return true;
	}

} //end function validEMail

function isValidZip(myElement) 
{
	var myString = myElement.value;
	var myRegExp = /^(\d{5})(\-?)(\d{4})?$/;
	
	if ( !myString.match(myRegExp) && myString.length > 0)
	{
		alert("Please enter a valid 5-digit or zip+4 code.");
		myElement.focus();
		return false;
	}
	else
	{
		return true;
	}
} 

function doNotSubmit()
{
	return !(window.event && window.event.keyCode == 13);
}

function searchNews() {
	var keywds = document.getElementById("keywds").value;
	doAjax('/mbs/ajaxScripts/doNewsArchive.php','K=' + keywds ,'newsCallback','post',0);
}

function newsCallback(text) {
	document.getElementById("pageContentText").innerHTML = text;
	document.getElementById("archiveDiv").innerHTML = '<a href="searchNews.php">News Archive</a>';
	fixHeight();
	adjustHeight();
}

function getGallery(elt) {
	var myID = document.getElementById(elt).value;
	doAjax('/mbs/ajaxScripts/doShow.php','newsID=' + myID ,'galleryCallback','post',0);
}

function galleryCallback(text) {
	//alert( text );
	var textArray = text.split('||');
	height = textArray[0];
	shots = textArray[1].split('|');
	bigShots = shots;
	myPicName = textArray[2].split('|');
	myCaption = textArray[3].split('|');
	myPhotoID = textArray[4].split('|');
	var picDivText = '<a href="/mbs/news/picPopup.php?id=' + myPhotoID[0] + '" onclick="openNewWindow(this.href, 600, 600, 0); return false;">';
	picDivText += '<img id="showPic" name="showPic" src="' + shots[0] + '" alt="" style="height:' + height + 'px" />';
	picDivText += '</a><br />&nbsp;<br /><b>' + myCaption[0] + '</b><br /><i>' + myPicName[0] + '</i><br /><br />';
	document.getElementById("picDiv").innerHTML = picDivText;
	if ( document.getElementById("slideshow2") )
		showDiv("slideshow2");
	else 
		showDiv("slideshow");
}

function getFaculty(FID) {
	var myID = ( FID == 0 ? document.getElementById("departmentID").value : FID);
	doAjax('ajaxScripts/doFaculty.php','J=DDS&deptID=' + myID ,'facultyCallback','post',0);
}

function doFacultyNameSearch(value) {
	var scrap = escape(value);
	doAjax('ajaxScripts/doFaculty.php','J=DNS&V=' + scrap ,'facultyCallback','post',0);
}

function facultyCallback(text) {
	document.getElementById("faculty").innerHTML = text;
	handleLinks();
	fixHeight();
	adjustHeight();
}

function getTeams() {
	var mySeason = document.getElementById("seasonID").value;
	var SY = document.getElementById("schoolYear").value;
	if ( mySeason == 0 )
		alert( "Please select a season.");
	else
		doAjax('/mbs/ajaxScripts/doGames.php','key=1&S='+ mySeason + '&sYear=' + SY ,'teamCallback','post',0);	
}

function teamCallback(text) {
	
	hideDiv("setSeason");
	myCurrentText = document.getElementById("setTeam").innerHTML;
	document.getElementById("setTeam").innerHTML =  text + ' ' + myCurrentText;
	showDiv("setTeam");
	handleLinks();
	fixHeight();
	adjustHeight();

}

function getGames() {
	var mySchoolYear = document.getElementById("schoolYear").value;
	var myTeam = document.getElementById("teamID").value;
	doAjax('/mbs/ajaxScripts/doGames.php','key=2&sYear=' + mySchoolYear + '&T=' + myTeam,'gamesCallback','post',0);	
}

function gamesCallback(text) {
	var textArray = text.split('|');
	var schoolYear = textArray[0];
	var teamName = textArray[1];
	var records = textArray[2];
	var farRightMenu = '<div id="subMenu"><a href="/mbs/Athletics/games.php">New Request</a></div>';
	hideDiv("setYear");
	hideDiv("setSeason");
	hideDiv("setTeam");
	document.getElementById("titleBar").innerHTML =  schoolYear +  ' ' + teamName + farRightMenu;
	document.getElementById("games").innerHTML =  records;
	handleLinks();
	fixHeight();
	adjustHeight();
	
}

/* Slideshow */
function keyHit(evt)  {
	var ltArrow = 37;
	var rtArrow = 39;
	var thisKey = ( evt) ? evt.which : window.event.keyCode;
	if ( thisKey == ltArrow )
	{
		//changeShot(-1);
		changeDiv(-1);
	}
	else if ( thisKey == rtArrow )
	{
		//changeShot(1);
		changeDiv(1);
	}
	return false;
}

function changeDiv(direction) {
	var max= shots.length -1;
	thisPic = thisPic + direction;
	if ( thisPic > max )
		thisPic = 0;
	if ( thisPic < 0 )
		thisPic  = max; 
	var divText = '<a href="/mbs/news/picPopup.php?id=' + myPhotoID[thisPic] + '" onclick="openNewWindow(this.href, 600, 600, 0); return false;">';
	divText += '<img id="showPic" name="showPic" src="' + shots[thisPic] + '" style="height:' + height + 'px" /></a>';
	divText += '<br />&nbsp;<br />';
	divText += '<b>' +  myCaption[thisPic] + '</b><br /><i>' + myPicName[thisPic] + '</i><br />&nbsp;<br />';
	document.getElementById( "picDiv").innerHTML = divText;
}

function animate() {
	if (myMove == 0 )
	{
		changeDiv(1);
		setTimeout('animate()', 3000);
	}
}

function noAnimate() {
	myMove = 1;
}

function reAnimate() {
	changeDiv(1);
	myMove = 0;
	setTimeout('animate()', 3000);
}

function hitPlay() {
	document.playButton.src='/mbs/images/PlayDark.gif';
	document.pauseButton.src='/mbs/images/Pause.gif';
	
}

function hitPause() {
	document.playButton.src='/mbs/images/Play.gif';
	document.pauseButton.src='/mbs/images/PauseDark.gif';
	
}

function noPush() {
	noAnimate();
	document.playButton.src='/mbs/images/Play.gif';
	document.pauseButton.src='/mbs/images/Pause.gif';
}

// deprecated ... 
function fixHeight() {
	handleLinks();
	var mySidebar = document.getElementById("sidebar");
	var myMainContent = document.getElementById("mainContent");
	if ( mySidebar  && myMainContent )
	{
		if ( mySidebar.offsetHeight )
		{
			var sideHeight = mySidebar.offsetHeight;
			var mainHeight = myMainContent.offsetHeight;
			var newHeight = Math.max(sideHeight, mainHeight);
			newHeight = newHeight - 12;
			newHeight = Math.max(newHeight, 440);    
			newHeightString = newHeight + 'px';
			//myMainContent.setAttribute("style", "min-height" + ":" + newHeight + "px;");
			//myMainContent.style.cssText="min-height:" + newHeight + "px;";
			// Example of how to change additional style elements ... 
			// curStyle = myMainContent.getAttribute("style");
			// myMainContent.setAttribute("style", curStyle + "font" + ":" + "18px Arial");
			//mySidebar.setAttribute("style", "min-height" + ":" + newHeight + "px;");
			//mySidebar.style.cssText="min-height:" + newHeight + "px;";
			//changeStyle('sidebar', 'minHeight', newHeight);
			
			changeStyle('mainContent', 'minHeight', newHeightString);
			changeStyle('sidebar', 'minHeight', newHeightString);
			if ( navigator.userAgent.indexOf('MSIE 6') > 0 )
			{
				//alert( newHeightString );
				changeStyle('mainContent', 'height', newHeightString);
				changeStyle('sidebar', 'height', newHeightString);
				
			}
			if ( document.sidebarInner )
			changeStyle('sidebarInner', 'minHeight', newHeightString);
		}
	}
}

function subForm(x) {
   
     document.forms[x].submit();			
}


function changeShot(direction) {
	var max= shots.length -1;
	thisPic = thisPic + direction;
	if ( thisPic > max )
		thisPic = 0;
	if ( thisPic < 0 )
		thisPic = max ; 
	document.showPic.src=shots[thisPic];
}

function checkVenue( homeVenueID, gameYear, gameMonth, gameDay ) {
    var myID = homeVenueID.value;
    var myDate = gameYear.value + "-" + gameMonth.value + "-" + gameDay.value;
    var myQS = "SELECT DATE_FORMAT(startTime, '%M %d, %Y') AS startDate, DATE_FORMAT(startTime, '%r') AS startTime, homeVenueName FROM game, homeVenue WHERE game.homeVenueID = homeVenue.homeVenueID AND startTime LIKE '" + myDate + "%' AND game.homeVenueID = " + myID;
    testWindow = window.open("", "testWin", "toolbar=no,location=no,scrollbars=no,resizable=yes,width=300,height=300");
    testWindow.document.write('<html><head><title>Check Venue</title><style>a {text-decoration:none; color:#790000;} #links {text-align:center; padding:50px;}.formLogo {float:left;}</style></head><body><div id="links" ><img src = "/mbs/images/MB.jpg" alt="" class="formLogo" /><br /><a href="testVenue.php?qs='+myQS+'">Check Venue</a>')
  // alert( myQS );
 //  return myQS ;

}

