var ajax_example = {
    
    init: function(bookid) 
    {
       // Grab the elements we'll need.                                                                                                                                      
       ajax_example.form = document.getElementById('cform');
       ajax_example.results_div = document.getElementById('test_div'); 

        YAHOO.util.Connect.setForm(ajax_example.form);
          
        //Temporarily disable the form.
        for(var i=0; i<ajax_example.form.elements.length; i++) 
        {
    	       ajax_example.form.elements[i].disabled = true;
        }
        var cObj = YAHOO.util.Connect.asyncRequest('POST', 'http://publisher.webfactional.com/detail/?bookid=' + bookid, ajax_example.ajax_callback);
   },  
   
   ajax_callback: 
   {
      success: function(o) 
      {
             // This turns the JSON string into a JavaScript object.
        	 var response_obj = eval('(' + o.responseText + ')');
        
            var input_list = document.createElement('div');
            input_list.style.paddingTop = '10px';
            input_list.style.paddingBottom = '35px';
            input_list.style.borderBottom = '1px black solid';
       	    input_list.innerHTML = "<b>" + response_obj.cname + " Says:</b>";
            
            var ictext = document.createElement("p")
            ictext.innerHTML = response_obj.ctext;
            input_list.appendChild(ictext);
            var iaform = document.createElement("form")
            iaform.id = 'agree_form';
            iaform.action = 'http://publisher.webfactional.com/detail/?bookid=' + response_obj.bookid; 
            iaform.method = "POST";
            
            var icommentid = document.createElement("input")
            icommentid.id = 'comment_id';
            icommentid.name = 'comment_id';
            icommentid.type = 'hidden'; 
            icommentid.value = response_obj.id;
            iaform.appendChild(icommentid);
            input_list.appendChild(iaform);
            
            var idbutton = document.createElement("div")
            idbutton.innerHTML = 'Agree';
            idbutton.className = 'comment_button';
            idbutton.style.width = "45px";
            idbutton.style.marginRight = "10px";
            idbutton.style.cssFloat = "left";
            idbutton.onclick = Function("cagree(" + response_obj.id +");");
            input_list.appendChild(idbutton);
            
            var iagree = document.createElement("div")
            iagree.style.cssFloat = "left";
            iagree.style.height = "20px";
            iagree.style.lineHeight = "24px";
            iagree.style.width = "170px";
            iagree.id = "numa" + response_obj.id;
            iagree.innerHTML = response_obj.agree + " readers agree"; 
            input_list.appendChild(iagree);
                
   	        ajax_example.results_div.innerHTML = '';
            ajax_example.results_div.appendChild(input_list);

            for(var i=0; i<ajax_example.form.elements.length; i++) 
            {
                ajax_example.form.elements[i].disabled = false;
            }
      },
      
      failure: function(o) 
      { // In this example, we shouldn't ever go down this path.
	       alert('An error has occurred');
      }
   },
};

var ajax_agree = {
    init: function(comment_id, agree) 
    {
       // Grab the elements we'll need.                                                                                                                                      
       ajax_example.form = document.getElementById('agree_form' + comment_id);
       
       document.getElementById('abut' + comment_id).onclick = "";
       if(agree)
       {     ajax_example.results_div = document.getElementById('numa' + comment_id); }
       /*else
       {     ajax_example.results_div = document.getElementById('numd' + comment_id); }*/

        YAHOO.util.Connect.setForm(ajax_example.form);
          
        if(agree)
        {   var cObj = YAHOO.util.Connect.asyncRequest('POST', 'http://publisher.webfactional.com/agree/', ajax_agree.ajax_callback);}
        /*else
        {   var cObj = YAHOO.util.Connect.asyncRequest('POST', 'http://publisher.webfactional.com/disagree/', ajax_agree.ajax_callback);}*/
   },  
   
   ajax_callback: 
   {
      success: function(o) 
      {
             // This turns the JSON string into a JavaScript object.
        	 var response_obj = eval('(' + o.responseText + ')');
                
   	        if(response_obj.isagree)
            {   ajax_example.results_div.innerHTML = response_obj.agree + ' readers agree';}
            /*else
            {   ajax_example.results_div.innerHTML = "&nbsp;&nbsp;" + response_obj.disagree + ' readers disagree';}*/
      },
      
      failure: function(o) 
      { // In this example, we shouldn't ever go down this path.
	       alert('An error has occurred');
      }
   },
    
};
      
function cagree(comment_id)
{
    ajax_agree.init(comment_id, 1);
}

/*function dagree(comment_id)
{
    ajax_agree.init(comment_id, 0);
}*/

function show(eID)
{
    if(document.getElementById(eID).style.display == "block")
		document.getElementById(eID).style.display = "none";
	else if(document.getElementById(eID).style.display == "none")
		document.getElementById(eID).style.display = "block";
		
}

function cvalidate(bookid)
{
    if(document.getElementById('cname').value == "" || document.getElementById('cname').value == "Your Name")
		document.getElementById('cerror').innerHTML = "Please enter your name.";
	else if(document.getElementById('creview').value == "")
		document.getElementById('cerror').innerHTML = "Markus Wiener does not accept blank reviews.";
    else if(document.getElementById('creview').value == "Type your review here.")
        document.getElementById('cerror').innerHTML = "Use the box below write a short review of this book.";
    else
    {
        document.getElementById('cerror').innerHTML = "";
        ajax_example.init(bookid);
        //document.forms["cform"].submit(); 
    }    		
}

