﻿function forgotPassword()
{
window.open("/account/forgotpassword", "Window1",
"menubar=no,scrollbars=no,width=450,height=280,toolbar=no");
}
function openHeatSheet1()
{
window.open(heatSheetURL, "Window1",
"menubar=no,scrollbars=yes,width=890,height=550,toolbar=no");
}
function editPost(id)
{
window.open("/forum/edit/" + id,"Window1",
"menubar=no,width=600,height=350,toolbar=no");
}
function openTerms()
{
    window.open("/termsconditions.html","Window1",
"menubar=no,scrollbars=yes,width=400,height=600,toolbar=no");
}
function openPrivacy()
{
    window.open("/privacypolicy.html","Window1",
"menubar=no,scrollbars=yes,width=400,height=600,toolbar=no");
}

function openSurferBio(id)
{
window.open("/contest/surfer/" + id,"Window1",
"menubar=no,width=750,height=500,toolbar=no");
}
function leaderboard_SeasonChange()
{
    var season = document.getElementById("season");
    var pathName = document.location.pathname;
    var i = pathName.lastIndexOf("/");
    pathName = pathName.substring(0, i);
    document.location = "http://" + document.location.host + "/season/leaderboard/" + season.value; 
}

function delteThread(id)
{
    var objHttp = GetHttpRequestObject();
    if (objHttp) 
    {
        try 
        {
            var url = "";
            url = "/forum/deleteThread/" + id;
            objHttp.open("GET", url, true); // "true" creates an async. request
            objHttp.onreadystatechange = function() { onCallback_Delete(objHttp); };
            objHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
            objHttp.send(null);
        } 
        catch (e) 
        {
          alert('Error: ' + e.description); 
        }
    }
}
function deltePost(id)
{
    var objHttp = GetHttpRequestObject();
    if (objHttp) 
    {
        try 
        {
            var url = "";
            url = "/forum/deletePost/" + id;
            objHttp.open("GET", url, true); // "true" creates an async. request
            objHttp.onreadystatechange = function() { onCallback_Delete(objHttp); };
            objHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
            objHttp.send(null);
        } 
        catch (e) 
        {
          alert('Error: ' + e.description); 
        }
    }
}
function onCallback_Delete(xmlHttp)
{
    if (xmlHttp.readyState == 4) // "4" indicates the end of the request
    { 
        if (xmlHttp.status == 200) // "200" indicates a successful request
        {
            var sResult = xmlHttp.responseText;
            if(sResult == "")
            {
                window.location = window.location;
            }
            else
            {
                alert(sResult);
            }
        }
        else //Something went wrong.
        { 
            alert('Error: ' + xmlHttp.status);
        }
    }
}
function saveFriendTeams()
{
    var lstFriends = document.getElementById("selectedTeams");
    var frndIDs = "";
    for(var i = 0; i < lstFriends.options.length;i++)
    {
        //if(parseInt(lstFriends.options[i].value) > 0)
        frndIDs += lstFriends.options[i].value + ",";
    }
    var objHttp = GetHttpRequestObject();
    if (objHttp) 
    {
        try 
        {
            var url = "";
            url = "/contest/saveteam?selectedFriends=" + frndIDs + "&contestID=" + contestID + "&dummy=" + Math.floor(Math.random()*11) + "" + Math.floor(Math.random()*11) + "" + Math.floor(Math.random()*11) + "" + Math.floor(Math.random()*11);
            objHttp.open("GET", url, true); // "true" creates an async. request
            objHttp.onreadystatechange = function() { onCallback_UpdateSaveTeam(objHttp); };
            objHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
            objHttp.send(null);
        } 
        catch (e) 
        {
          alert('Error: ' + e.description); 
        }
    }
}
function onCallback_UpdateSaveTeam(xmlHttp)
{
    // This function will run over and over as the request  goes though processing
    if (xmlHttp.readyState == 4) // "4" indicates the end of the request
    { 
        if (xmlHttp.status == 200) // "200" indicates a successful request
        {
            var sResult = xmlHttp.responseText;
            if(sResult == "")
            {
                window.opener.location = window.opener.location;
                window.close();
            }
            else
            {
                alert(sResult);
            }
        }
        else //Something went wrong.
        { 
            alert('Error: ' + xmlHttp.status);
        }
    }
}
function openTeam()
{
    var cid = document.getElementById("contestID");
    window.open("/contest/team?contestID=" + cid.value,"Window1",
"menubar=no,width=676,height=451,toolbar=no");
//window.open("/contest/team", "Customize Scoreboard", "menubar=no,width=500,height=280,toolbar=no");
}

function updateHeatSheet1()
{
    var txtHeatSheet = document.getElementById("heatSheetImageURL");
    var contestID = document.getElementById("drpContest");
    if(parseInt(contestID.value) <= 0) 
        alert("Please first save the contest before updating Heat Sheet Image URL");
    else
    {   
        var objHttp = GetHttpRequestObject();
        if (objHttp) 
        {
            try 
            {
                var url = "";
                url = "/admin/updateHeatSheet?contestID=" + contestID.value + "&url=" + txtHeatSheet.value + "&dummy=" + Math.floor(Math.random()*11) + "" + Math.floor(Math.random()*11) + "" + Math.floor(Math.random()*11) + "" + Math.floor(Math.random()*11);
                objHttp.open("GET", url, true); // "true" creates an async. request
                objHttp.onreadystatechange = function() { onCallback_UpdateHeatSheet(objHttp); };
                objHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
                objHttp.send(null);
            } 
            catch (e) 
            {
              alert('Error: ' + e.description); 
            }
        }
    }
}
function onCallback_UpdateHeatSheet(xmlHttp)
{
    // This function will run over and over as the request  goes though processing
    if (xmlHttp.readyState == 4) // "4" indicates the end of the request
    { 
        if (xmlHttp.status == 200) // "200" indicates a successful request
        {
            var span = document.getElementById("heatSheetUpdate");
            span.style.display = "block";
        }
        else //Something went wrong.
        { 
            alert('Error: ' + xmlHttp.status);
        }
    }
}
function addSurfers(surferData, seedRound)
{
    surferData = surferData.substring(1, surferData.length-2);
    var surfers = surferData.split('||"');
    var color = document.getElementById("seedRoundColor" + seedRound);
    var noOfSurfers = document.getElementById("noOfSurfers" + seedRound);
    var n = parseInt(noOfSurfers.value); 
    for(var i = 0; i < surfers.length; i++)
    {
        var name = "", country = "";
        var lastComma = surfers[i].lastIndexOf(",");
        name = surfers[i].substring(0, lastComma);
        name = name.substring(0, name.length - 1);
        country = surfers[i].substring(lastComma + 1);
                
        addSurferToGrid(name, country, color.value, seedRound, n);
        n = n + 1;
    } 
    noOfSurfers.value = n;        
}


function drpSurfer_change(source)
{
    document.location = "http://" + document.location.host + document.location.pathname + "?id=" + source.value; 
}

function drpSeason_Changed(source)
{
    document.location = "http://" + document.location.host + document.location.pathname + "?id=" + source.value; 
}

function drpContest_Change(source)
{
    document.location = "http://" + document.location.host + document.location.pathname + "?id=" + source.value; 
}
function GetHttpRequestObject() 
{
    var objHttp = null;
    if (window.XMLHttpRequest) 
    {
    	objHttp = new XMLHttpRequest();    //If IE7, Mozilla, Safari, etc: Use native object
    } 
    else if (window.ActiveXObject) 
    {
        try 
        {
            objHttp = new ActiveXObject("MSXML2.XMLHTTP");    // IE5.x and IE6
        } 
        catch (e) 
        {
            try 
            {
                objHttp = new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch (e) 
            {
            }
        }
    }
    if (!objHttp) 
    {
        //[raise an error]
        //alert('Could not create XMLHttpRequest object');
    }
    return objHttp;
}



function itemsPerPage_Change(source)
{
    var itemsPerPage = source.value;
    var top = document.getElementById("itemsPerPageTop");
    var bottom = document.getElementById("itemsPerPageBottom");
    top.value = itemsPerPage;
    bottom.value = itemsPerPage;
    var location = document.location;
    var url = "";
    url = location.protocol + "//" + location.host + location.pathname + "?noOfItems=" + itemsPerPage;
    window.location = url;
}

function onCallback_AddSurfer(xmlHttp, seedRound) 
{
    // This function will run over and over as the request  goes though processing
    if (xmlHttp.readyState == 4) // "4" indicates the end of the request
    { 
        if (xmlHttp.status == 200) // "200" indicates a successful request
        {
            var sResult = xmlHttp.responseText;
            //alert("onCallback_UseServerData = " + strElement + "  ID=" + ID + "  sResult = " + sResult);
            //alert(sResult);
            var tbl = document.getElementById("tblSeedBody" + seedRound);
            var child = tbl.getFirstChild()
            tbl.innerHTML += sResult;
        }
        else //Something went wrong.
        { 
            alert('Error: ' + xmlHttp.status);
        }
    }
}
function addAltSurfer(source)
{
    var noOfAltSurfers = document.getElementById("noOfAltSurfers" + source.seedRound);
    var i = parseInt(noOfAltSurfers.value);
    var surfer = document.getElementById("altSurferName" + source.seedRound);
    var country = document.getElementById("altCountryCode" + source.seedRound);
    var tBody = document.getElementById("tblSeedAltBody" + source.seedRound);
    var drpColor = document.getElementById("seedRoundColor" + source.seedRound);
    
    var trSurfer = document.createElement("tr");
    var tdName = document.createElement("td");
    var tdCode = document.createElement("td");
    var tdColor = document.createElement("td");
    var tdActions = document.createElement("td");
    var tdNoShow = document.createElement("td");
    var tdScore = document.createElement("td");
    var tdFinal = document.createElement("td");
    
    var inName = document.createElement("input");
    inName.type = "text";
    inName.name = "altSurfer-" + source.seedRound + "-" + i;
    inName.id = "altSurfer-" + source.seedRound + "-" + i;
    inName.value = surfer.value;
    tdName.appendChild(inName);
    trSurfer.appendChild(tdName);
    
    var inCode = document.createElement("input");
    inCode.type = "text";
    inCode.name = "altCode-" + source.seedRound + "-" + i;
    inCode.id = "altCode-" + source.seedRound + "-" + i;
    inCode.value = country.value;
    tdCode.appendChild(inCode);
    trSurfer.appendChild(tdCode);
    
    var inColor = document.createElement("input");
    inColor.type = "text";
    inColor.name = "altColor-" + source.seedRound + "-" + i;
    inColor.id = "altColor-" + source.seedRound + "-" + i;
    inColor.value = drpColor.value;
    tdColor.appendChild(inColor);
    trSurfer.appendChild(tdColor);
    
    var tmpActions = document.getElementById("tempActions" + source.seedRound);
    
    var inAction = document.createElement("select");
    inAction.name = "altActions-" + source.seedRound + "-" + i;
    inAction.id = "altActions-" + source.seedRound + "-" + i;
    for(var j = 0;j < tmpActions.length; j++)
    {
        var inOpt = document.createElement("option");
        inOpt.text = tmpActions[j].text;
        inOpt.value =  tmpActions[j].value;
        inAction.options.add(inOpt);
    }
    tdActions.appendChild(inAction);
    trSurfer.appendChild(tdActions);
    
    var inNoShow = document.createElement("input");
    inNoShow.type = "checkbox";
    inNoShow.name = "altChkNoShow-" + source.seedRound + "-" + i;
    inNoShow.id = "altChkNoShow-" + source.seedRound + "-" + i;
    tdNoShow.appendChild(inNoShow);
    trSurfer.appendChild(tdNoShow);
    
    var inScore = document.createElement("input");
    inScore.type = "text";
    inScore.name = "altScore-" + source.seedRound + "-" + i;
    inScore.id = "altScore-" + source.seedRound + "-" + i;
    inScore.value = "";
    tdScore.appendChild(inScore);
    trSurfer.appendChild(tdScore);
    
    var inFinal = document.createElement("input");
    inFinal.type = "checkbox";
    inFinal.name = "altChkFinal-" + source.seedRound + "-" + i;
    inFinal.id = "altChkFinal-" + source.seedRound + "-" + i;
    tdFinal.appendChild(inFinal);
    trSurfer.appendChild(tdFinal);
    
    tBody.appendChild(trSurfer);
    
    noOfAltSurfers.value = i + 1;
    surfer.value = "";
    country.value = "";
}
function addSurfer(source)
{
    var noOfSurfers = document.getElementById("noOfSurfers" + source.seedRound);
    var i = parseInt(noOfSurfers.value);
    var surfer = document.getElementById("surferName" + source.seedRound);
    var country = document.getElementById("countryCode" + source.seedRound);
    var drpColor = document.getElementById("seedRoundColor" + source.seedRound);
    
    if(surfer.value == "")
    {
        alert("Please enter a surfer name");
        surfer.focus();
    }
    else if(country.value == "")
    {
        alert("Please enter country code");
        country.focus();
    }
    else
    {
        addSurferToGrid(surfer.value, country.value, drpColor.value, source.seedRound, i);
        surfer.focus();
        noOfSurfers.value = i + 1;
        surfer.value = "";
        country.value = "";
    }
    //<%=Html.CheckBox("chkNoShow-" + ViewData.Model.RoundNumber + "-" +i, surfer.NoShow)%>
    //<%= Html.TextBox("score-" + ViewData.Model.RoundNumber +"-" + i, surfer.Score) %>
    //<%=Html.CheckBox("chkFinal-" + ViewData.Model.RoundNumber + "-" +i, surfer.IsFinalScore)%>
}

function addSurferToGrid(name, country, color, seedRound, i)
{
    var tBody = document.getElementById("tblSeedBody" + seedRound);
    var trSurfer = document.createElement("tr");
    var tdName = document.createElement("td");
    var tdCode = document.createElement("td");
    var tdColor = document.createElement("td");
    var tdActions = document.createElement("td");
    var tdNoShow = document.createElement("td");
    var tdScore = document.createElement("td");
    var tdFinal = document.createElement("td");
    
    var inName = document.createElement("input");
    inName.type = "text";
    inName.name = "surfer-" + seedRound + "-" + i;
    inName.id = "surfer-" + seedRound + "-" + i;
    inName.value = name;
    tdName.appendChild(inName);
    trSurfer.appendChild(tdName);
    
    var inCode = document.createElement("input");
    inCode.type = "text";
    inCode.name = "code-" + seedRound + "-" + i;
    inCode.id = "code-" + seedRound + "-" + i;
    inCode.value = country;
    tdCode.appendChild(inCode);
    trSurfer.appendChild(tdCode);
    
    var inColor = document.createElement("input");
    inColor.type = "text";
    inColor.name = "color-" + seedRound + "-" + i;
    inColor.id = "color-" + seedRound + "-" + i;
    inColor.value = color;
    tdColor.appendChild(inColor);
    trSurfer.appendChild(tdColor);
    
    var tmpActions = document.getElementById("tempActions" + seedRound);
    
    var inAction = document.createElement("select");
    inAction.name = "actions-" + seedRound + "-" + i;
    inAction.id = "actions-" + seedRound + "-" + i;
    for(var j = 0;j < tmpActions.length; j++)
    {
        var inOpt = document.createElement("option");
        inOpt.text = tmpActions[j].text;
        inOpt.value =  tmpActions[j].value;
        inAction.options.add(inOpt);
    }
    tdActions.appendChild(inAction);
    trSurfer.appendChild(tdActions);
    
    var inNoShow = document.createElement("input");
    inNoShow.type = "checkbox";
    inNoShow.name = "chkNoShow-" + seedRound + "-" + i;
    inNoShow.id = "chkNoShow-" + seedRound + "-" + i;
    tdNoShow.appendChild(inNoShow);
    trSurfer.appendChild(tdNoShow);
    
    var inScore = document.createElement("input");
    inScore.type = "text";
    inScore.name = "score-" + seedRound + "-" + i;
    inScore.id = "score-" + seedRound + "-" + i;
    inScore.value = "";
    tdScore.appendChild(inScore);
    trSurfer.appendChild(tdScore);
    
    var inFinal = document.createElement("input");
    inFinal.type = "checkbox";
    inFinal.name = "chkFinal-" + seedRound + "-" + i;
    inFinal.id = "chkFinal-" + seedRound + "-" + i;
    tdFinal.appendChild(inFinal);
    trSurfer.appendChild(tdFinal);
    
    tBody.appendChild(trSurfer);
}

function openHeatSheet(url)
{
    alert(url);
    //window.open(url, "Heat Sheet", "", "");
}

function saveTeamPicks(source)
{
    var contestID = document.getElementById("contestID").value;
    var spanSuccess = document.getElementById("spanSuccess");
    var surfers = "";
    var iRedSurfers = 0, iYellowSurfers = 0, iBlueSurfers = 0;
    var bRed= false, bBlue = false;
    var err = false;
    var chkArray = document.getElementsByName("chkSurfer");
    for(var i = 0; i < chkArray.length; i++)
    {
        if(iRedSurfers + iYellowSurfers + iBlueSurfers <= 12)
        {
            var obj = chkArray[i];
            if(obj.type == "checkbox" && obj.checked)
            {
                var s = obj.value.split("|");
                var id = s[0];
                var color = s[1];
                if(color == "red" && iRedSurfers < 4)
                    iRedSurfers++;
                else if(color == "red")
                {
                    bRed = true;
                    break;
                }
                if(color == "yellow")
                    iYellowSurfers++;
                if(color == "blue")
                    iBlueSurfers++;
                surfers += id + ",";   
            }
        }
        else
        {
            err = true;
            alert("Teams must include 12 surfers. No more than 4 red surfers, and no less than 4 blue surfers. Please review your team selection to be sure it meets these criteria.");
            break;
        }
    }
    if(iBlueSurfers < 4)
    {
        err = true;
        bBlue = true;
    }
    
    var msg = "";
    if(bRed || bBlue || (iRedSurfers + iBlueSurfers + iYellowSurfers != 12))
        alert("Teams must include 12 surfers. No more than 4 red surfers, and no less than 4 blue surfers. Please review your team selection to be sure it meets these criteria.");
    else //if(!err)
    {
        var objHttp = GetHttpRequestObject();
        if (objHttp) 
        {
            try 
            {
                var saveTeamText = document.getElementById("saveTeamLink");
                saveTeamText.innerHTML = "please wait...";
                saveTeamText.disabled = true;
                var url = "";
                url = "/contest/addteam?contestID=" + contestID + "&surfers=" + surfers + "&dummy=" + Math.floor(Math.random()*11) + "" + Math.floor(Math.random()*11) + "" + Math.floor(Math.random()*11) + "" + Math.floor(Math.random()*11);
                objHttp.open("GET", url, true); // "true" creates an async. request
                objHttp.onreadystatechange = function() { onCallback_AddTeam(objHttp, source, spanSuccess, surfers, contestID); };
                objHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
                source.innerHTML = "saving, please wait...";
                source.disabled = true;
                objHttp.send(null);
            } 
            catch (e) 
            {
              alert('Error: ' + e.description); 
            }
        }
    }
}

function onCallback_AddTeam(xmlHttp, source, spanSuccess, surfers, contestID)
{
 // This function will run over and over as the request  goes though processing
    if (xmlHttp.readyState == 4) // "4" indicates the end of the request
    { 
        if (xmlHttp.status == 200) // "200" indicates a successful request
        {
            var sResult = xmlHttp.responseText;
            //alert("onCallback_UseServerData = " + strElement + "  ID=" + ID + "  sResult = " + sResult);
            //alert(sResult);
            source.innerHTML = "save team picks";
            if(sResult != "")
            {
                alert(sResult);
            }
            else
            {
                alert("Team has saved successfully.");// Testing:" + sResult + " -Testing 2: " + surfers + " contest: " + contestID);
                document.location = document.location;
                spanSuccess.style.display = "block";
            }
        }
        else //Something went wrong.
        { 
            alert('Error: ' + xmlHttp.status);
        }
    }
}