﻿ 
function VoteUp(sStoryID, sControlID)
{
    DebugOut("  Voting for Story " + sStoryID );
    MetaSites_Voting.ThumbsUp(
        sStoryID,        
        VoteUp_Succeeded, VoteUp_FailedCallback,
        sStoryID);

}

 
function TipStoryUp(sStoryID, sTipAmount)
{

    DebugOut("  Voting for Story " + sStoryID );
    MetaSites_Voting.TipStory(
        sStoryID, sTipAmount,        
        TipStory_Succeeded, TipStory_FailedCallback,
        sStoryID);

}

  
function TipUserUp(UserId, sTipAmount)
{
    
    MetaSites_Voting.TipUser(
        UserId, sTipAmount,        
        TipUser_Succeeded, TipUser_FailedCallback,
        UserId);

}

 
function VoteComment(sCommentID, nValue)
{

    DebugOut("  Voting for Comment " + sCommentID );
    MetaSites_Voting.CommentUp(
        sCommentID, nValue,       
        CommentUp_Succeeded, CommentUp_FailedCallback,
        sCommentID);

}

 
function CommentOnStory(sStoryID, nReplyCommentID, sComment, sControlID)
{
    DebugOut("  Commenting on Story " + sStoryID );
        
    MetaSites_Voting.CommentOnStory(
        sStoryID, nReplyCommentID, sComment,       
        Comment_Succeeded, Comment_FailedCallback,
        sControlID);
        
}


function VoteUp_Succeeded(result, userContext, methodName)
{

    var sErrCode = "";

        if(result.selectSingleNode("//vote/@errcode").text == null)
        {
            sErrCode = result.selectSingleNode("//vote/@errcode").textContent;        
         }
         else
         {
            sErrCode = result.selectSingleNode("//vote/@errcode").text;
         }
    
    DebugOut("  VoteUp_Succeeded.  ErrCode was  " + sErrCode);

    if(sErrCode == "0")
    {    
        var sNewCount = ""
        
          if(result.selectSingleNode("//vote/@errcode").text == null)
        {
            sNewCount = result.selectSingleNode("//vote/@newcount").textContent;        
         }
         else
         {
            sNewCount = result.selectSingleNode("//vote/@newcount").text;
         }
                
        DebugOut("  NewCount = " + sNewCount);  
        var nCur = 0;        
        //var sDivName = userContext + "_" + nCur;                
        
        var sDivName = "thumbsup-" + userContext 
        
        
        var elOutput = document.getElementById(sDivName);
        
        var sDivVote = "vote-" + userContext;
        var elVote = document.getElementById(sDivVote);
        
        elOutput.innerHTML = sNewCount + " <br />Votes";
        elVote.innerHTML = "<a href='javascript://'>Thanks!</a>";
        //elVote.innerHTML = "<a href=\"javascript://\" onclick='TipStory(" +userContext + ");'>Tip</a>";                
    }           
    else
    {
    
     if(sErrCode == "2")
        alert("You must be logged in to vote");
     else
        DebugOut("  ERROR HIT: " + sErrCode);
    }   
}

// This is the callback function invoked if the Web service
// failed.
// It accepts the error object as a parameterd.
function VoteUp_FailedCallback(error)
{
    if(error == 2)
        MessageBox("You must be logged in to vote");
     else
        DebugOut("  ERROR HIT: " + error);
}


function TipStory_Succeeded(result, userContext, methodName)
{
 
    var sErrCode = "";

    if(result.selectSingleNode("//vote/@errcode").text == null)
    {
        sErrCode = result.selectSingleNode("//vote/@errcode").textContent;        
     }
     else
     {
        sErrCode = result.selectSingleNode("//vote/@errcode").text;
     }
    
    DebugOut("  VoteUp_Succeeded.  ErrCode was  " + sErrCode);

    if(sErrCode == "0")
    {    
        var sNewCount = ""
        
          if(result.selectSingleNode("//vote/@errcode").text == null)
        {
            sNewCount = result.selectSingleNode("//vote/@newcount").textContent;        
         }
         else
         {
            sNewCount = result.selectSingleNode("//vote/@newcount").text;
         }
                
        DebugOut("  NewCount = " + sNewCount);  
        var nCur = 0;        
        
        var sDivName = "thumbsup-" + userContext 
        
        
        var elOutput = document.getElementById(sDivName);
        
        var sDivVote = "vote-" + userContext;
        var elVote = document.getElementById(sDivVote);
        
        elOutput.innerHTML = sNewCount + " <br />Votes";
        elVote.innerHTML = "<a href='javascript://'>Tip'd!</a>";
    }           
    else
    {
    
     if(sErrCode == "2")
        alert("You must be logged in to vote");
     else
        DebugOut("  ERROR HIT: " + sErrCode);
    }   
}

// This is the callback function invoked if the Web service
// failed.
// It accepts the error object as a parameterd.
function TipStory_FailedCallback(error)
{
    if(error == 2)
        MessageBox("You must be logged in to tip");
     else
        DebugOut("  ERROR HIT: " + error);
}


// This is the callback function invoked if the Web service
// failed.
// It accepts the error object as a parameterd.
function VoteUp_FailedCallback(error)
{
    if(error == 2)
        MessageBox("You must be logged in to vote");
     else
        DebugOut("  ERROR HIT: " + error);
}


// ===================


function TipUser_Succeeded(result, userContext, methodName)
{
 
    var sErrCode = "";

    if(result.selectSingleNode("//vote/@errcode").text == null)
    {
        sErrCode = result.selectSingleNode("//vote/@errcode").textContent;        
     }
     else
     {
        sErrCode = result.selectSingleNode("//vote/@errcode").text;
     }
    
    DebugOut("  UserUp_Succeeded.  ErrCode was  " + sErrCode);

    if(sErrCode == "0")
    {    
        var sNewCount = ""
        
          if(result.selectSingleNode("//vote/@errcode").text == null)
        {
            sNewCount = result.selectSingleNode("//vote/@newcount").textContent;        
         }
         else
         {
            sNewCount = result.selectSingleNode("//vote/@newcount").text;
         }
                
        DebugOut("  NewCount = " + sNewCount);  
        var nCur = 0;        
        
        var sDivName = "ctl00_ContentPlaceHolder1_txtNozzlCash" 
        
        
        var elOutput = document.getElementById(sDivName);
        
        
        elOutput.innerHTML = "$" + sNewCount ;
        
    }           
    else
    {
    
     if(sErrCode == "2")
        alert("You must be logged in to vote");        
     else
     {
         if(sErrCode == "3")
         {
         alert("Unable to tip that");
         }
         else     
            DebugOut("  ERROR HIT: " + sErrCode);      
     }   
    }
}

// This is the callback function invoked if the Web service
// failed.
// It accepts the error object as a parameterd.
function TipUser_FailedCallback(error)
{
    if(error == 2)
        MessageBox("You must be logged in to tip");
     else
        DebugOut("  ERROR HIT: " + error);
}


// This is the callback function invoked if the Web service
// failed.
// It accepts the error object as a parameterd.
function VoteUp_FailedCallback(error)
{
    if(error == 2)
        MessageBox("You must be logged in to vote");
     else
        DebugOut("  ERROR HIT: " + error);
}


// ===================

function CommentUp_Succeeded(result, userContext, methodName)
{

    var sErrCode = "";

        if(result.selectSingleNode("//vote/@errcode").text == null)
        {
            sErrCode = result.selectSingleNode("//vote/@errcode").textContent;        
         }
         else
         {
            sErrCode = result.selectSingleNode("//vote/@errcode").text;
         }
    
    DebugOut("  VoteUp_Succeeded.  ErrCode was  " + sErrCode);

    if(sErrCode == "0")
    {    
        var sNewCount = ""
        
          if(result.selectSingleNode("//vote/@errcode").text == null)
        {
            sNewCount = result.selectSingleNode("//vote/@newcount").textContent;        
         }
         else
         {
            sNewCount = result.selectSingleNode("//vote/@newcount").text;
         }
                
        DebugOut("  NewCount = " + sNewCount);  
        var nCur = 0;        
        //var sDivName = userContext + "_" + nCur;                
        
        var sDivName = "thumbsup-" + userContext 
        
        
        var elOutput = document.getElementById(sDivName);
        
        var sDivVote = "vote-" + userContext;
        var elVote = document.getElementById(sDivVote);
        
        elOutput.innerHTML = sNewCount + " Votes";
        elVote.innerHTML = "&nbsp;&nbsp;<a href='javascript://' title=''>Thanks!</a>&nbsp;&nbsp;";
    }           
    else
    {
    
     if(sErrCode == "2")
        alert("You must be logged in to vote");
     else
        DebugOut("  ERROR HIT: " + sErrCode);
    }   
}

// This is the callback function invoked if the Web service
// failed.
// It accepts the error object as a parameterd.
function CommentUp_FailedCallback(error)
{
    alert(error);
    if(error == 2)
        MessageBox("You must be logged in to vote");
     else
        DebugOut("  ERROR HIT: " + error);
}

function Comment_Succeeded(result, userContext, methodName)
{
    
    var sErrCode = "";

    if(result.selectSingleNode("//comment/@errcode").text == null)
    {
    
        sErrCode = result.selectSingleNode("//comment/@errcode").textContent;        
     }
     else
     {
     
        sErrCode = result.selectSingleNode("//comment/@errcode").text;
     }


    DebugOut(" Comment_Succeeded.  ErrCode was  " + sErrCode);
    
    
     //var elFin = document.getElementById(userContext);
     
//    if(elFin == null)
        elFin = userContext;
 
    if(elFin != null)
        elFin.style.display = "block";

    if(sErrCode == "0")
    {    
        var sComment = ""
        
        if(result.selectSingleNode("//comment/@commenttext").text == null)
        {
            sComment = result.selectSingleNode("//comment/@commenttext").textContent;        
         }
         else
         {
            sComment = result.selectSingleNode("//comment/@commenttext").text;
         }
         
         var sStoryID = ""
        
        if(result.selectSingleNode("//comment/@storyid").text == null)
        {
            sStoryID = result.selectSingleNode("//comment/@storyid").textContent;        
         }
         else
         {
            sStoryID = result.selectSingleNode("//comment/@storyid").text;
         }
         elFin.innerHTML = "<br/><b>Comment Added</b>: <i>" +  sComment + "</i>.";//  View <a target='_top' href='story.aspx?id=" + sStoryID + "'>other comments</a>";        

    }           
    else
    {
        
         if(sErrCode == "2")
            alert("You must be logged in to vote");
         else
         {
            elFin.innerText = "Error saving comment: " + sErrCode;
            DebugOut("  ERROR HIT: " + sErrCode);
         }
    }   
}

// This is the callback function invoked if the Web service
// failed.
// It accepts the error object as a parameterd.
function  Comment_FailedCallback(error)
{
    alert(error.get_message());    
    if(error == 2)
        MessageBox("You must be logged in to vote");
     else
        DebugOut("  ERROR HIT: " + error);
}

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
