// JavaScript Document
//Jobj = new JSON();
AjaxObject = GetXmlHttpObject();
CID = 00000000000000000001;
php = null;
commno = 0;

function submitcomment() {
	
	//alert("submitting comment");
	var params = '';
	var url = '';
	var CommentedUser = $('Tuser').innerHTML;
	var CommentedTitle = $('Ttitle').innerHTML;
	var CommentedVersion = $('Tversion').innerHTML;
	var Comment = $("commentbox").value; //removed:  .replace("'",'"')
	SubObject = GetXmlHttpObject();
	if (SubObject!=null){	
		//alert("xmlhttp object is"+xmlhttp);
		commenturl = "http://aether3d.com/_php/subcomment.php";
		params = "CommentedUser="+CommentedUser;
		if (CommentedTitle != ''){params += "&CommentedTitle="+CommentedTitle;}
		if (CommentedVersion != ''){params += "&CommentedVersion="+CommentedVersion;}
		params += "&Comment="+Comment;
		SubObject.open("POST", commenturl, true);
		/*Send the proper header information along with the request*/
		SubObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		SubObject.setRequestHeader("Content-length", params.length);
		SubObject.setRequestHeader("Connection", "close");
		SubObject.onreadystatechange=submitstate;
		SubObject.send(params);
		//alert("sent xmlhttp request!");
		$("commentbox").value = '';
		$('subcomment').style.display='none';
	} else {alert("SubObject==null!!!");}
}

function submitstate() {
	if (SubObject.readyState==4){
		
		if (SubObject.responseText.search('okrefresh') == 0){
			//alert("All good! "+SubObject.responseText);
			commentrefresh();
			//refreshcomments
		} else{
			alert("something went horibly wrong! "+SubObject.responseText);
		}
		$('subcomment').style.display='inline-block';
	}
	
}

function commentrefresh() {

	if (AjaxObject!=null){
		commenturl = "http://aether3d.com/_php/getcomment.php";
		CommentedUser = $('Tuser').innerHTML;
		var CommentedTitle = $('Ttitle').innerHTML;
		var CommentedVersion = $('Tversion').innerHTML;
		params = "CommentedUser="+CommentedUser;
		if (CommentedTitle != ''){params += "&CommentedTitle="+CommentedTitle;}
		if (CommentedVersion != ''){params += "&CommentedVersion="+CommentedVersion;}
		params += "&CID="+CID;
		//alert('loading comments');
		AjaxObject.open("POST", commenturl, true);
		AjaxObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		//AjaxObject.setRequestHeader("Content-length", params.length);
		//AjaxObject.setRequestHeader("Connection", "close");
		AjaxObject.onreadystatechange=refreshstate;
		AjaxObject.send(params);
	}
}

function refreshstate() {
	if (AjaxObject.readyState==4){
		if (AjaxObject.responseText != 'noref'){
			//alert( $type(AjaxObject.responseText) );
			var Aresult = JSON.decode(AjaxObject.responseText, true);
			//$('comments').innerHTML = AjaxObject.responseText+Aresult;
			//var rarr = reverse(php.unserialize(AjaxObject.responseText)); // should give a 2dimensional array
			// walkthrough array to fill  div id ='comments'
			var commentdiv = $('comments').innerHTML;
			CID = Aresult.id;
			//alert(CID);
			var rarr = Aresult.comments;
			var ammount = Aresult.ammount;
			//alert(ammount);
			for (i=0 ; i<ammount ; i++){
				commno++;
				if (rarr[i].CID > CID) {
					CID = rarr[i].CID;
					alert(CID);
				}
				if (rarr[i].CVE == 0){ rarr[i].CVE = 'Overview'; }
				
				if (rarr[i].CTI != '' && rarr[i].CVE == 'Overview') {
					commentdiv = '<div id="comment'+commno+'" class="comment" onmouseover="gethot(this,\'#F1F1FF\')" onmouseout="getcold(this,\'#FEFEFF\')"><div id="commentT" class="commentT"><a href="http://aether3d.com/index.php?u='+rarr[i].CUS+'"><strong>'+rarr[i].CUS+'</strong></a> on <a href="http://aether3d.com/index.php?u='+CommentedUser+'&p='+rarr[i].CTI+'"><strong>'+rarr[i].CTI+'</strong></a> version'+' '+rarr[i].CVE+' at '+rarr[i].CDA+'</div><div id="commentC" class="commentC">'+rarr[i].CCO+'</div></div>'+commentdiv;  
				} else if (rarr[i].CTI != '') {
					commentdiv = '<div id="comment'+commno+'" class="comment" onmouseover="gethot(this,\'#F1F1FF\')" onmouseout="getcold(this,\'#FEFEFF\')"><div id="commentT" class="commentT"><a href="http://aether3d.com/index.php?u='+rarr[i].CUS+'"><strong>'+rarr[i].CUS+'</strong></a> on <a href="http://aether3d.com/index.php?u='+CommentedUser+'&p='+rarr[i].CTI+'"><strong>'+rarr[i].CTI+'</strong></a> version'+' <a href="http://aether3d.com/index.php?u='+CommentedUser+'&p='+rarr[i].CTI+'&v='+rarr[i].CVE+'"><strong>'+rarr[i].CVE+'</strong></a> at '+rarr[i].CDA+'</div><div id="commentC" class="commentC">'+rarr[i].CCO+'</div></div>'+commentdiv; 
				} else {
					//title is '' wich means the comment was posted on the project overview..
					commentdiv = '<div id="comment'+commno+'" class="comment" onmouseover="gethot(this,\'#F1F1FF\')" onmouseout="getcold(this,\'#FEFEFF\')"><div id="commentT" class="commentT"><a href="http://aether3d.com/index.php?u='+rarr[i].CUS+'"><strong>'+rarr[i].CUS+'</strong></a> on <a href="http://aether3d.com/index.php?u='+CommentedUser+'"><strong>'+CommentedUser+'\'s projects'+rarr[i].CVE+'</strong></a> at '+rarr[i].CDA+'</div><div id="commentC" class="commentC">'+rarr[i].CCO+'</div></div>'+commentdiv;
				}
				$('comments').innerHTML = commentdiv;
				
			}
			//alert(Aresult.ammount);
		} else {
			//this means the response is noref..
			//alert(AjaxObject.responseText);
		}
		
		setTimeout('commentrefresh()',10000);
	}
}