function checkComments()
{
	var parameters = location.search.substring(1).split("&");

	var temp = parameters[0].split("=");
	var reloaded = unescape(temp[0]);
	return true;
	//return (reloaded == "submit");	// page loaded by the submit button of a form
}
function validate_form ( )
{
    valid = true;

    if ( document.ajax.ilogin.value == "" )
    {
        alert ( "Please fill in the 'Name' box." );
        valid = false;
    }
	 if ( document.ajax.icomment.value == "" )
    {
        alert ( "Please fill in the 'Comment' box." );
        valid = false;
    }
    return valid;
}


   function makePOSTRequest(url, parameters) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      
      http_request.onreadystatechange = alertContents;
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
	 // alert("test inside post req "+http_request.readyState+parameters);
   }

   function alertContents() {
    if (http_request.readyState == 4) {
         if (http_request.status == 200) {

   submitForm('comment_new/demo-comment.xml');
   }}
   }
   
   function get(obj) {
      var poststr = "ilogin=" + encodeURI( document.getElementById("ilogin").value ) +
                    "&icomment=" + encodeURI( document.getElementById("icomment").value );
      makePOSTRequest('comment_new/build-xml-comment.php', poststr);
	 // alert("test inside get");
   }


// processData writes the retrieved values into the page

function processData(doc)
{
	var element = doc.getElementsByTagName('login');
	tabBody=document.getElementById("ct");
	tabBody.innerHTML="";

	for(i=0; i<element.length;i++)
			
	{		tbody=document.createElement("TBODY");

			table1=document.createElement("TABLE");
			 row1=document.createElement("TR");
			 row2=document.createElement("TR");
             cell1_n = document.createElement("TH");
			 cell1_t = document.createElement("TH");
             cell2 = document.createElement("TD");
             textnode1=document.createTextNode(doc.getElementsByTagName('login').item(i).firstChild.data);
             textnode2=document.createTextNode(doc.getElementsByTagName('comment').item(i).firstChild.data);
		textnode3=document.createTextNode(doc.getElementsByTagName('time').item(i).firstChild.data);
		            cell1_n.appendChild(textnode1);
		cell1_t.appendChild(textnode3);
            cell2.appendChild(textnode2);

		cell2.setAttribute('style','border-top-width: 1px; border-top-style: dashed; border-top-color: #999999;padding-bottom:12px;padding-top:7px');
		cell2.setAttribute('colspan','2');
		cell1_t.setAttribute('style','padding-top:15px;color:#999999; text-align:right; font-weight:normal;font-size:10px');
		cell1_n.setAttribute('style','background-image: url(p1.png); padding-left:26px;padding-top:15px; color:#3C6291');
		
		cell1_n.style['background']='url(p1.png)';
		cell1_n.style['padding']='15px 0px 0px 26px';
		cell1_t.style['color']='#999999';
		cell1_t.style['padding']='15px 0px 0px 0px';
		cell1_t.style['textAlign']='right';
		cell1_t.style['fontWeight']='normal';
		cell2.style['padding']='7px 0px 12px 0px';
		cell2.style['borderTop']='1px dashed #999999';
		cell2.colSpan='2';
		
		row1.appendChild(cell1_n);
		row1.appendChild(cell1_t);
            row2.appendChild(cell2);
		
            tbody.appendChild(row1);
		tbody.appendChild(row2);
		table1.appendChild(tbody);
		table1.setAttribute('width','720px'  );
		table1.setAttribute('align','center'  );
		table1.setAttribute('cellspacing','0'  );

		tabBody.appendChild(table1);


	}

	//document.getElementById("bt").innerHTML="<a href=\"#comment\" onclick=\"hidecomment()\">hide</a>";
	var mstring="";
	mstring+="<div style=\"height:190px\"><form name=\"ajax\" method=\"POST\" action=\"javascript:get(document.getElementById(\'ajax\'));\" target=\"_parent\" style=\"width:700px; \" onsubmit=\"return validate_form ( );\">";
	mstring+="<p align=\"left\"> Name: <br>";
	mstring+="<input type=\"text\" id=\"ilogin\" name=\"ilogin\" maxlength=\"40\" value=\"\" />";
	mstring+="</p>";
	mstring+="<p align=\"left\">Type comment:<br>";
	mstring+="<textarea id=\"icomment\" name=\"icomment\" rows=\"4\" cols=\"64\"></textarea>";
	mstring+="</p>";
	mstring+="<p align=\"left\">";
	mstring+="<input type=\"submit\" value=\"Add comment\" name=\"submit\" />";
	mstring+="</p>";
	mstring+="</form></div>";
	document.getElementById("formholder").innerHTML=mstring;
	window.location.href='#bottom';


	
		
}
function hidecomment()
{
	tabBody=document.getElementById("ct");
	tabBody.innerHTML="";
	document.getElementById("formholder").innerHTML="";	
	document.getElementById("bt").innerHTML="<a href=\"#comment\" onclick=\"submitForm(\'comment_new/demo-comment.xml\')\">Comments</a>";
}


// submitForm is an Ajax script to load the XML file 

function submitForm(doc)
{ 

	var xhr=createXHR();

	xhr.open("GET", doc + "?nocache=" + Math.random(),true);

	xhr.onreadystatechange = function()
	{ 

		if(xhr.readyState == 4)
		{

			if(xhr.status == 200)
			{
				processData(xhr.responseXML);
			}	
			else	
			{
				alert("Error: returned status code " + xhr.status + " " + xhr.statusText);
			}	

		} 
	}; 

	xhr.send(null); 
} 

// assigning a processing to the onload event

window.onload = function()
{
	/*var n=comment_num()+"Comments";
	document.getElementById("bt").innerHTML="<a href=\"#comment\" onclick=\"submitForm(\'comment1/demo-comment.xml\')\">"+n+"</a>";*/
	submitForm('comment_new/demo-comment.xml')
}