function pollForChallenges(){
    //change this to ajax.update and use the insertion flag maybe.
    var url = 'function/currentChallenges.php';
    var ajax = new Ajax.Request(url, {
    method:'get',
    onSuccess: searchChallengesList});

}

function searchChallengesList(response){
    var challengeList = eval('(' + response.responseText + ')');
    var keyList = Object.keys(challengeList[character_id]);
    var newString = "";
    
    keyList.each(function(s) {
        newString += makeChallengeBox(challengeList[character_id][s]['challenger_name'],challengeList[character_id][s]['challenger_id'],challengeList[character_id][s]['status'],challengeList[character_id][s]['room']);
    });   
    //For lack of a better name all the messages are going in a div
    //called fruits... kind of like fruits growing on a tree.  :( sorry.
    $('fruits').update(newString);
    $('fruits').innerHTML;
    $('fruits').show();
}

function makeChallengeBox(name, id, status, room){
    var divID = name.gsub(' ', '') + id;
    var fightStatus = "fight";

    if(status == "issue"){
        return "<div id='"+ divID +"' align='center'>" 
        +"<div class=\"duels_avatar\"> <a href=\"\">"
          +"<img src=\"images/avatar/thumb/"+id+".png\" width=\"100\" height=\"100\" class=\"duelsborder\" />"
          +"</a> </div>"
        +"<div class=\"duels_challengebox\">"
          +"<div class=\"duels_challengetext\"> "+name+" has challenged you to a duel! </div>"
          +"<button class='duel_sort' onclick='challengeResponse(\""+divID+"\","+id+",\"accept\","+room+")'>Accept</button>"
          +"<button class='duel_sort' onclick='declineChallenge(\""+divID+"\","+id+")'>Decline</button>"
        +"</div>"
        +"<div class=\"duels_clear\"></div>"
        +"</div>";
    }else if(status == "accept"){
        return "<div id='"+ divID +"' align='center'>" 
        +"<div class=\"duels_avatar\"> <a href=\"\">"
          +"<img src=\"images/avatar/thumb/"+id+".png\" width=\"100\" height=\"100\" class=\"duelsborder\" />"
          +"</a> </div>"
        +"<div class=\"duels_challengebox\">"
          +"<div class=\"duels_challengetext\"> "+name+" has accepted your challenge! </div>"
          +"<button class='duel_sort' onclick='challengeResponse(\""+divID+"\","+id+",\"fight\","+room+")'>Fight!</button>"
          +"<button class='duel_sort' onclick='declineChallenge(\""+divID+"\","+id+")'>Retreat!</button>"
        +"</div>"
        +"<div class=\"duels_clear\"></div>"
        +"</div>";

        /**    
        return "<div id='"+ divID +"'  align='center' class='challengeBox'>" 
        +name + " has accepted your challenge. <br />"+ 
        "<a href='duels_duelspage.html?room="+room+"&type=first'><button class='duel_sort' onclick='challengeResponse(\""+divID
        +"\","+id+",\"fight\","+room+")'>Fight!</button></a>"+
        "<button class='duel_sort' onclick='declineChallenge(\""+divID+"\","+id
        +")'>Run Away!</button></div>";
        **/   
    }else{
        return "<div id='"+ divID +"' align='center'>" 
        +"<div class=\"duels_avatar\"> <a href=\"\">"
          +"<img src=\"images/avatar/thumb/"+id+".png\" width=\"100\" height=\"100\" class=\"duelsborder\" />"
          +"</a> </div>"
        +"<div class=\"duels_challengebox\">"
          +"<div class=\"duels_challengetext\"> "+name+" has declined your challenge! </div>"
          +"<button class='duel_sort' onclick='challengeResponse(\""+divID+"\","+id+",\"onion\")'>Ok</button>"
        +"</div>"
        +"<div class=\"duels_clear\"></div>"
        +"</div>";
    
    /**
        return "<div id='"+ divID +"' align='center' class='challengeBox'>" 
        +name +" has declined your challenge.<br /><button class='duel_sort' onclick='challengeResponse(\""
        +divID+"\", "+id+",\"onion\")'>Ok</button></div>";
        **/
    }
}
//I was just using this to test and make sure paremeters were
//correct when calls were being made.
function challengeResponseTest(divID, victim, status, room){
alert(divID+" "+victim+" "+status+" room:"+room);
}

function issueChallenge(victim){
var url = 'function/sendChallengeRequest.php';
    var ajax = new Ajax.Request(
    url,
    {method: 'get', 
    parameters: {victim: victim, status: "issue"}});
    alert("challenge issued");
}

function challengeResponse(divID, victim, status, room){
//alert("Starting to send: "+divID+" "+victim+" "+status+" room:"+room);
//alert("Hiding Div: "+divID+" "+victim+" "+status+" room:"+room);
var url = 'function/sendChallengeRequest.php';
    var ajax = new Ajax.Request(
    url,
    {method: 'get', 
    parameters: {victim: victim, status: status, room: room}});
//alert("Ajax sent : "+divID+" "+victim+" "+status+" room:"+room);
$(divID).hide();
//Forward the player to the dueling screen:
    if(status == "accept"){
        location.href="duels_duelspage.html?room="+room+"&type=second";
    } else if (status == "fight"){
        location.href="duels_duelspage.html?room="+room+"&type=first";
    }
}

function declineChallenge(divID, victim){
//alert("Starting to decline: "+divID+" "+victim);
//alert("Hiding Div: "+divID+" "+victim);
var url = 'function/sendChallengeRequest.php';
    var ajax = new Ajax.Request(
    url,
    {method: 'get', 
    parameters: {victim: victim, status: "decline"}});
//alert("Ajax sent : "+divID+" "+victim);
$(divID).hide();
}

