function safari_sucks(location) {
	if(navigator.userAgent.indexOf('Safari') != -1) 
	{	
		show_comments();
		//s = new String(window.location.href);
		//s.replace("/\?_z=33\#comments_start/g","");
		//alert(s);
		//go = s + '?_z=33#comments_start';
		//window.location.href = go;
	}
	else 
	{
		window.location.hash = 'comments_start';
		window.location.reload();
	}
}

function reload_form(location) {
	if(navigator.userAgent.indexOf('Safari') == -1) {	
//		setTimeout("window.location.reload()", 5);
		f=document.createElement("form");
		h=document.createElement("input");
		h.type="hidden";
		h.name="_z";
		h.value=34;
		f.action=location;
		f.method="get";
		f.appendChild(h);
		document.getElementById("comments").appendChild(f);
//		f.submit();
	}
}

function show_form()
{
	document.getElementById('myform').className = (document.getElementById('myform').className == 'Show') ? 'Hide' : 'Show';
	//document.getElementById('add_a_comment').className = 'Hide';
	document.getElementById("comment_subject").value = '';
	document.getElementById("comment_comment").value = '';
	document.getElementById('comment_name').focus();
}


function submit_comment()
{
	//add captcha values to form serialization via cookie
	if(document.getElementById("comment_captcha_text").value == "") {
		document.getElementById("comment_captcha_text").value = get_captcha_value();
		document.getElementById("comment_captcha_id").value = get_captcha_id();
	}
	else {
		set_captcha_value(document.getElementById("comment_captcha_text").value);
		set_captcha_id(document.getElementById("comment_captcha_id").value);
        }

	document.getElementById('myform').className = 'Hide';

	//	var params = Form.serialize($("myform"));

	//replacement for the above (explicit)
	var params = "name=" + encodeURIComponent($("comment_name").value) + 
			"&title=" + encodeURIComponent($("comment_subject").value) + 
			"&comment=" + encodeURIComponent($("comment_comment").value) + 
			"&nid=" + encodeURIComponent($("comment_nid").value) + 
			"&site=" + encodeURIComponent($("comment_site").value) + 
			"&captcha_id=" + encodeURIComponent($("comment_captcha_id").value) + 
			"&captcha_value=" + encodeURIComponent($("comment_captcha_text").value) + 
			"&action=" + encodeURIComponent($("comment_action").value);

	$.post(afns_base_url + 'comments/comments.php', params,  refresh_submit );
}

function refresh_submit(xmlDoc)
{
	var method = xmlDoc.documentElement.getElementsByTagName("method")[0].firstChild.nodeValue;
	var error = xmlDoc.documentElement.getElementsByTagName("error")[0].firstChild.nodeValue;
	if(error > 0)
	{
		display_captcha(originalRequest);
	}
	else
	{
		//removing page refresh on onion
		//safari_sucks();

		process_response(originalRequest);	
		//window.location.href = window.location.href + '#' + 'comments_start';
		//window.location.hash = new String('comments_start');
		//window.location.reload();
	}
}

function hide_comments()
{
	document.getElementById("comment_boxen").className = 'Hide';
}

function show_comments()
{
	m = '<ul id="comment_list">';
	m +=	'<li class="comment">Loading Comments...</li>'
	m += '</ul>';
	document.getElementById("comment_boxen").innerHTML = m

	var params = 'action=read&nid=' + comments_nid;
	$.get(afns_base_url + 'comments/comments.php', params, process_response);
}

var afns_dayNames = new Array("Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat");
var afns_monthNames = new Array ( "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" );

function process_response(xmlDoc)
{
	var method = xmlDoc.documentElement.getElementsByTagName("method")[0].firstChild.nodeValue;
	var id = xmlDoc.documentElement.getElementsByTagName("result")[0].firstChild.nodeValue;
	var error = xmlDoc.documentElement.getElementsByTagName("error")[0].firstChild.nodeValue;

	if(method == "solved_captcha")
 	{
		set_captcha_id(xmlDoc.documentElement.getElementsByTagName("result")[0].firstChild.nodeValue);
		set_captcha_value(xmlDoc.documentElement.getElementsByTagName("value")[0].firstChild.nodeValue);
	}

	if(method == "comments" || method == "moderate")
	{
   		document.getElementById("comment_count").innerHTML = id;

		//if we get a response back from the server, enable the comments div
		document.getElementById("comments").className = 'ShowBlock';

  	        var i = 0;

		m = '<ul id="comment_list">';
		total =  xmlDoc.documentElement.getElementsByTagName("title").length;

	        if(id > 0 && total == 0) {
		   //if there are comments to be read and we're not currently looking at comments, show the read comments span
		   document.getElementById("read_comments").className = 'Show';
	        }
		else {
		   document.getElementById("read_comments").className = 'Hide';
		}

		for(i = 0; i < total; i ++) {
		  var title = xmlDoc.documentElement.getElementsByTagName("title")[i].firstChild.nodeValue;

			var nickname = xmlDoc.documentElement.getElementsByTagName("nickname")[i].firstChild.nodeValue;
			var message = xmlDoc.documentElement.getElementsByTagName("message")[i].firstChild.nodeValue;
			var stamp = (new Date()).setTime(xmlDoc.documentElement.getElementsByTagName("time")[i].firstChild.nodeValue);
			var time = new Date();
			time.setTime( stamp * 1000 );
			var dayOfWeek = afns_dayNames[time.getDay()];
			var monthName = afns_monthNames[time.getMonth()];
			var year = 1900+time.getYear();
			var pm = "AM";
			var hour = time.getHours();
			if( hour > 11 ){
				pm = "PM";
				hour = hour-12;
			}
			if( hour == 0){
				hour = 12;
			}
			var minutes = time.getMinutes();
			if( minutes < 10 ){
				minutes = "0"+minutes;
			}
			var dateString = hour+":"+minutes+" "+pm+" "+dayOfWeek+" "+monthName+" "+time.getDate()+", "+year;
		  
			m +=	'<li class="comment">'+
					'	<h4 class="title">'+title+'</h4>'+
					'	<p class="byline">by <span class="nickname">'+nickname+'</span></p>'+
					'	<p class="message">'+message+'</p>'+
					'	<p class="timestamp">'+dateString+'</p>'+
					'</li>';
		}
		m += "</ul>";
		document.getElementById("comment_boxen").innerHTML = m;
		document.getElementById("comment_boxen").className = "ShowBlock";
		if(method == "moderate")
		{
			document.getElementById("comment_message").innerHTML = "Your comment has been submitted for moderation";
		}
		else
		{
			document.getElementById("message").innerHTML = "";
			document.getElementById("comment_message").innerHTML = "";
		}
	}
	else
	{
		display_captcha(xmlDoc);
	}
}

function display_captcha(xmlDoc)
{
	var method = xmlDoc.documentElement.getElementsByTagName("method")[0].firstChild.nodeValue;
	var id = xmlDoc.documentElement.getElementsByTagName("result")[0].firstChild.nodeValue;
	var error = xmlDoc.documentElement.getElementsByTagName("error")[0].firstChild.nodeValue;
	
	//var captcha = '<img src="/drupal/captcha.img.php?id='+id+'" border="0" /><input type="hidden" name="captcha_id" value="'+id+'" />';
	var captcha = '<img src="' + afns_base_url + 'comments/images/'+id+'.png" border="0" />';
	document.getElementById("captcha_id").innerHTML = captcha;
	document.getElementById("comment_captcha_id").value = id;

	if(error != 0) 
	{
		clear_stored_captcha();
		document.getElementById("comment_message").innerHTML = error;
		document.getElementById('myform').className = 'Show';
		if(id != 0) {
			document.getElementById('comment_captcha').className = 'Show';
		}
	}
	else {
		document.getElementById("comment_message").innerHTML = "";
	}

	if(get_captcha_value() == undefined) {
		document.getElementById('comment_captcha').className = 'Show';
		//document.getElementById("comment_captcha_text").value = "";
	}
	else {
		document.getElementById('comment_captcha').className = 'Hide';
	}
}
