var DELIMITER = "*";
var whitespace = " \t\n\r";

function logOut()
{
	document.memLinkForm.action = 'memberLogOutScript.php';
	document.memLinkForm.submit();
}

function editProfile()
{
	top.location = "index.php?pg=editProfile";
}

function gam()
{
	top.location = "index.php?pg=memberGAM";
}

function seasonPlanner()
{
	top.location = "index.php?pg=planner";
}

function players()
{
	top.location = "index.php?pg=players";
}

function tournaments()
{
	top.location = "index.php?pg=tournaments";
}

function tournamentscores()
{
	top.location = "index.php?pg=tournamentScores";
}

function tournamentroster()
{
	top.location = "index.php?pg=roster";
}

function tournamentplaces()
{
	top.location = "index.php?pg=tournamentPlacement";
}

function field()
{
	top.location = "index.php?pg=tournamentField";
}

function teetimeMaint()
{
	top.location = "index.php?pg=teetimeMaint";
}

function pointMaint()
{
	top.location = "index.php?pg=pointMaint";
}

function winnerMaint()
{
	top.location = "index.php?pg=winnerMaint";
}

function teesets()
{
	top.location = "index.php?pg=teeSets";
}

function mailingLists()
{
	top.location = "index.php?pg=mailingListMaint";
}
function courses()
{
	top.location = "index.php?pg=courses";
}

function seasonMaint()
{
	top.location = "index.php?pg=seasonMaint";
}
function mainBlog()
{
	top.location = "index.php?pg=mainBlog";
}

function postBlog()
{
	top.location = "index.php?pg=postBlog";
}
function playerBlog()
{
	top.location = "index.php?pg=playerBlog";
}

function scheduleOnChangeSeason()
{
	var selSeason = document.selectSeason.seasonID.options[document.selectSeason.seasonID.selectedIndex].value;
	top.location='index.php?pg=schedule&season=' + selSeason;
}
function plannerOnChangeSeason()
{
	var selSeason = document.selectSeason.seasonID.options[document.selectSeason.seasonID.selectedIndex].value;
	top.location='index.php?pg=planner&season=' + selSeason;
}

function standingsOnChangeSeason()
{
	var selSeason = document.selectSeason.seasonID.options[document.selectSeason.seasonID.selectedIndex].value;
	top.location='index.php?pg=pointStandings&season=' + selSeason;
}

function winnersOnChangeSeason()
{
	var selSeason = document.selectSeason.seasonID.options[document.selectSeason.seasonID.selectedIndex].value;
	top.location='index.php?pg=winners&season=' + selSeason;
}

function teeTimesOnChangeSeason()
{
	var selSeason = document.selectSeason.seasonID.options[document.selectSeason.seasonID.selectedIndex].value;
	top.location='index.php?pg=times&season=' + selSeason;
}

function tournamentResultsOnChangeSeason()
{
	var selSeason = document.selectSeason.seasonID.options[document.selectSeason.seasonID.selectedIndex].value;
	top.location='index.php?pg=tourneyStats&season=' + selSeason;
}

function teeTimesOnChangeTournament()
{
	var selTournament = document.selectTournament.tournamentID.options[document.selectTournament.tournamentID.selectedIndex].value;
	top.location='index.php?pg=times&tournament=' + selTournament;
}

function tournamentResultsOnChangeTournament()
{
	var selTournament = document.selectTournament.tournamentID.options[document.selectTournament.tournamentID.selectedIndex].value;
	top.location='index.php?pg=tourneyStats&tournament=' + selTournament;
}

function tournamentScoresOnChangeTournament()
{
	var selTournament = document.selectTournament.tournamentID.options[document.selectTournament.tournamentID.selectedIndex].value;
	top.location='index.php?pg=tournamentScores&tournament=' + selTournament;
}

function tournamentPlacementOnChangeTournament()
{
	var selTournament = document.selectTournament.tournamentID.options[document.selectTournament.tournamentID.selectedIndex].value;
	top.location='index.php?pg=tournamentPlacement&tournament=' + selTournament;
}

function tournamentPointsOnChangeTournament()
{
	var selTournament = document.selectTournament.tournamentID.options[document.selectTournament.tournamentID.selectedIndex].value;
	top.location='index.php?pg=pointMaint&tournament=' + selTournament;
}

function tournamentFieldOnChangeTournament()
{
	var selTournament = document.selectTournament.tournamentID.options[document.selectTournament.tournamentID.selectedIndex].value;
	top.location='index.php?pg=tournamentField&tournament=' + selTournament;
}

function tournamentTeeTimeMaintOnChangeTournament()
{
	var selTournament = document.selectTournament.tournamentID.options[document.selectTournament.tournamentID.selectedIndex].value;
	top.location='index.php?pg=teetimeMaint&tournament=' + selTournament;
}

function tournamentTeeSetOnChangeTournament()
{
	var selTournament = document.selectTournament.tournamentID.options[document.selectTournament.tournamentID.selectedIndex].value;
	top.location='index.php?pg=teeSets&tournament=' + selTournament;
}
function tournamentTeeSetOnChangeRound()
{
	var selTournament = document.selectTournament.tournamentID.options[document.selectTournament.tournamentID.selectedIndex].value;
	top.location='index.php?pg=teeSets&tournament&=' + selTournament;
	
}
function tournamentWinnersOnChangeTournament()
{
	var selTournament = document.selectTournament.tournamentID.options[document.selectTournament.tournamentID.selectedIndex].value;
	top.location='index.php?pg=winners&tournament=' + selTournament;
}

function tournamentWinnerMaintOnChangeTournament()
{
	var selTournament = document.selectTournament.tournamentID.options[document.selectTournament.tournamentID.selectedIndex].value;
	top.location='index.php?pg=winnerMaint&tournament=' + selTournament;
}

function playerMaintOnChangePlayer()
{
	var selPlayer = document.player.playerID.options[document.player.playerID.selectedIndex].value;
	if (selPlayer == 'select')
	{
		top.location = 'index.php?pg=players';
	}
	else
	{
		top.location = 'index.php?pg=players&player=' + selPlayer;
	}
}

function tournamentMaintOnChangeTournament()
{
	var selTournament = document.tournament.tournamentID.options[document.tournament.tournamentID.selectedIndex].value;
	if (selTournament != 'new')
	{
		top.location='index.php?pg=tournaments&tournament=' + selTournament;
	}
	else
	{
		document.tournament.tournament.value = "new";
		document.tournament.tournamentName.value = "";
		document.tournament.tournamentDate.value = "";
		document.tournament.tournamentStatus.selectedIndex = 0;
		document.tournament.firstTeeTime.value = "";
		document.tournament.courseID.selectedIndex = 0;
		document.tournament.entryFee.value = "";
		document.tournament.payPalButton.value = "";
		document.tournament.payOutText.value = "";
		document.tournament.skins.value = "";
		document.tournament.description.value = "";
	}
}
function courseMaintOnChangeCourse()
{
	var selCourse = document.course.courseID.options[document.course.courseID.selectedIndex].value;
	if (selCourse != 'new')
	{
		top.location='index.php?pg=courses&course=' + selCourse;
		
	}
	else
	{
		document.course.course.value = "new";
		document.course.CourseID.value = "";
		document.course.CourseName.value = "";
		document.course.Address.value = "";
		document.course.City.value = "";
		document.course.State.value = "MI";
		document.course.Phone.value = "";
		document.course.Zip.value = "";
		document.course.Website.value = "";
		document.course.Picture.value = "/images/";
		
		
	}
}
function tournamentScoreForPlayer(t,p)
{
	//t is the tournament
	//p is the player
	//open up the screen to edit scores.
	top.location='index.php?pg=editRound&tournament=' + t + '&player=' + p;
}

function editRoundReturnToList(t)
{
	top.location = "index.php?pg=tournamentScores&tournament=" + t;	
}

function moveToList(fromList, toList, fromArray, toArray, allFlag)
{
    var val;
    var name;
    
    if (fromList.length == 0)
    {
        return 0;
    }
    if (!allFlag && fromList.selectedIndex == -1)
    {
        return -1;
    }
    var origLen = toList.length;          
    for (var i = 0; i <= fromList.length - 1; i++) 
    {
        if (allFlag || fromList.options[i].selected)
        {          
            val = fromList.options[i].value;
            name = fromList.options[i].text;
            toList.options[toList.length] = new Option(name, val, true, false);        
                    
            if (fromArray[fromList.options[i].value] == 1)
            {
                fromArray[fromList.options[i].value] = null;
            }
            else
            {
                toArray[fromList.options[i].value] = 1;
            }
        }
    }
 
    for (var i = toList.length - 1; i >= origLen; i--) 
    {
        toList.options[i].selected = true;
    }

    for (var i = fromList.length - 1; i >= 0; i--) 
    { 
        if (allFlag || fromList.options[i].selected)
        {
            fromList.options[i].value = "JUNK";
            fromList.options[i].selected = false;
        }
    }
    
    for (var i = fromList.length - 1; i >= 0; i--) 
    { 
        if (fromList.options[i].value == "JUNK")
        {
            fromList.options[i] = null;
        }
    }
    return 1;
}

function createDelimetedList(anArray)
{
    var str = "";
    for (var x in anArray)
    {
        if (anArray[x] == 1)
           str += x + DELIMITER;
    }
    if (str.length >= DELIMITER.length)
    {
        str = str.substring(0, str.length - DELIMITER.length);
    }
    return str;
}

function isArrayDirty(hArray)
{
    for (var x in hArray)
    {
        if (hArray[x] == 1)
        {
            return true;
        }
    }
    
    return false;
}

function charInString(c, s)
{
    for (var i = 0; i < s.length; i++)
    {   
        if (s.charAt(i) == c) return true;
    }
    
    return false
}

function trim(s)
{   
    var i = 0;
    var len = s.length;

    while ((i < len) && charInString (s.charAt(i), whitespace))
       i++;

    if (i == len)
    {
        return s.substring(i, len);
    }
    else
    {
        var j = len - 1;
        while ((j > 0) && charInString (s.charAt(j), whitespace))
            j--;

        return s.substring (i, j + 1);
    }
}
function submit_comment(myfo) {
	//init facebook
            FB_RequireFeatures(["Connect"], function() {

                FB.ensureInit(function() {
	                
	                var thisid = myfo.elements["thisid"];
	                var title =myfo.elements["title"];
	                var comment_text = myfo.elements["entry"];
	                var message = myfo.elements["entry"];
					var pic = myfo.elements["pict"];
					
	                var attachment = {
		                'name': message.value,
	                	'href': 'http://miagt.com/index.php?pg=editBlog&thisid='+ thisid.value,
	                	'description': 'Posted at Michigan eGolf Tour www.miagt.com',
	                	"media": [{ 'type': 'image', 'src': "http://miagt.com/images/BLOG/"+pic.value, "href": 'http://miagt.com/index.php?pg=editBlog&thisid='+ thisid.value}]
	                	
	                 };
	                var action_links = [{ 'text': 'Michigane eGolf Tour!', 'href': 'http://miagt.com/index.php?pg=editBlog&thisid='+ thisid.value}];
                    FB.Connect.streamPublish(title.value, attachment, action_links, null, "Share your Golf comments at miagt.com!", callback, false, null);
				});
			});
			
			function callback(post_id, exception) {
                alert('Wall Post Complete');
            }
}
function initFB() {
	FB_RequireFeatures(["XFBML"], function(){
		FB.init("e30bcfa58d7025ffe68c71b4d045ccf1", "xd_receiver.htm");
	});}

function onConnected(user_id) {
    //alert("User is logged into FB and also this app");
} 
function onNotConnected() { 
     FB.ensureInit(function() {
        FB.Connect.get_status().waitUntilReady(function(status) {
            if (status == FB.ConnectState.userNotLoggedIn )    {
                //alert("User is not logged into FB");
            }
            if (status == FB.ConnectState.appNotAuthorized)    {
                //alert("User is logged into FB but this app is not authorized");
            }
        });
    });
}


var loginReq;
function fblogIn()
{
	alert("submit"+document.fbmemberLogIn.memberID.value);
	if (loginReq != null)
	{
		location.reload(true);
		
	}
	else
	{
	
	
	loginReq = null;
	if (document.fbmemberLogIn.memberID.value == "")
	{
		alert('Please enter your member name.');
		document.fbmemberLogIn.memberID.focus();
		return false;
	}	
	if (document.fbmemberLogIn.memPass.value == "")
	{
		alert('Please enter your password.');
		document.fbmemberLogIn.memPass.focus();
		return false;
	}	
	try 
	{ 
		loginReq = new XMLHttpRequest(); 
	} 
	catch (error) 
	{ 
		try 
		{ 
			loginReq = new ActiveXObject("Microsoft.XMLHTTP"); 
		} 
		catch (error) 
		{ 
			return false; 
		} 
	}
	loginReq.open('post',  'memberLogInScript.php?memberID=' + document.fbmemberLogIn.memberID.value + "&memPass=" + document.fbmemberLogIn.memPass.value);
	loginReq.setRequestHeader('Content-Type', 'text/plain');
	loginReq.onreadystatechange = logInCallback;
	loginReq.send(null);
	}
	
	return true;
	
}

function confirmSubmit()
{
	var agree=confirm("Do you want to report this as offensive?");
	if (agree)
		return true ;
	else
		return false ;
}
function confirmDelete()
{
	var agree=confirm("Do you want to Delete This Entry?");
	if (agree)
		return true ;
	else
		return false ;
}

function logInCallback()
{
	
	if (loginReq.readyState == 4)
	{
		
			if (loginReq.status == 200)
		{
            parseLogInMessage();
            location.reload(true);
        }
    }
}

function parseLogInMessage()
{
	var myXML, xmlObj;
	var text, val;
	var td = document.getElementById("logInArea");
	td.innerHTML = loginReq.responseText;
}

function gotocomment(thisid)
{
	var myurl = 'index.php?pg=editBlog&thisid='+thisid;
	window.location='index.php?pg=commentBlog&thisid='+thisid
	//location.replace('index.php?pg=editBlog&amp;thisid='+thisid);
}