//JS functions for the blog news and updates

//to find out the User Agent string; some UAs don't like the ajax comments
function testUA(){
    var client = navigator;
    var cUA = client.userAgent;
//    alert (cUA);
    iBad = 0;
    var badArray = new Array();
    badArray[iBad++] = "Nokia6133";
    badArray[iBad++] = "N770/SU-18";
//    badArray[iBad++] = "Firefox";
    for(iCount =0; iCount < badArray.length; iCount++)
	{
	if(cUA.indexOf(badArray[iCount]) > -1)
	    {
//	    alert (cUA + " is bad (" + badArray[iCount] + ")");
	    return -1;
	    }
	}
//    alert (cUA + " is GOOD");
    return 1;
}


function show_comments(div, url) {
//first, see if we should try ajax...
//doing this in the blosxom cgi now
/*    if(testUA() == -1)
	{
	var newURL = url.replace(/inline_c/, "writeback");
//	alert("newURL = " + newURL);
	//	newURL.replace(/inline_c/, "writeback");
	location.href=newURL;
	return;
	}
*/
    var html;
//    alert("Is anybody home? " + url);
    new Ajax.Request(url,
      {
	method:'get',
	onSuccess: function(transport){
	  response = transport.responseText || "no response text";
//Life is good:    
//	  alert("looking good...");
	  html = build_overlay(div, response);
	  var node = document.getElementById(div);
	  node.innerHTML = html;
	  new Effect.BlindDown(div);
	},
	      onFailure: function(){ alert('Something went wrong; please try later...') }
      });
//    alert("ending show_comments()...");
}

//this hides the comments w/scriptaculus BlindUp
//then rewrites the div
function hide_comments(div) {
    new Effect.BlindUp(div);
    node = document.getElementById(div);
    setTimeout("clear_comments(node)", 2500);
}

//this does the re-write on the div to remove the comments
function clear_comments(node){
    node.innerHTML = "";
}


//this is some basic overlay formatting
//div is the id of the enclosing div (used for close link)
//data is the guts of the overlay to display
function build_overlay(div, data) {
    var close_link = "<SPAN class=\"SmallText\"> ";
    close_link += "<INPUT type=\"button\" value=\"close comments\" onclick=\"javascript:hide_comments('" + div + "')\";> ";
    close_link += "</SPAN> </DIV>\n";

    var html = "<DIV class=\"HeaderOverlay\">\n";
    html += close_link;
    html += "<DIV class=\"BodyOverlay\">\n";
    html += data;
    html += "</DIV></DIV>\n";

//    alert("build_overlay html = " + html);
    return(html);
}

//this does the ajax comment submission
function post_comment(div, url, form){
    new Ajax.Request(url, {
	    parameters: $(form).serialize(true),
	method:'post',
	onSuccess: function(transport){
	  response = transport.responseText || "no response text";
//Life is good:    
//	  alert("looking good...");
	  html = build_overlay(div, response);
	  var node = document.getElementById(div);
	  node.innerHTML = html;
//	  new Effect.BlindDown(div);
	},
	      onFailure: function(){ alert('Something went wrong; please try later...') }
      });
//    alert("ending show_comments()...");

}
