var globalpage;
var globallastid;

function savecomment()
{	
text= (document.formcomments.text.value);
page= (document.formcomments.page.value);
name= (document.formcomments.name.value);
globalpage=page;

	// add the point and name to the database on the server
	
	//Create a boolean variable to check for a valid MS instance.
	var xmlhttp = false;
	
	//Check if we are using IE.
	try {
		//If the javascript version is greater than 5.
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		//If not, then use the older active x object.
		try {
			//If we are using IE.
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			//Else we must be using a non-IE browser.
			xmlhttp = false;
		}
	}
	
	//If we are using a non-IE browser, create a javascript instance of the object.
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	
	
    xmlhttp.onreadystatechange=function()
      {
      if(xmlhttp.readyState==4)
        {
	// do stuff
	 
	 globallastid=xmlhttp.responseText;
	//alert (globallastid);
	 
	document.formcomments.text.value="";
	document.formcomments.name.value="";
	
	// Get date, month and year
	today=new Date();
	day=today.getDate().toString();
	jsmonth=today.getMonth();
	jsyear=today.getFullYear().toString();
	// Convert Month to 12 unit measure
	month=(jsmonth + 1).toString();
	// Output month in two digits
    if (month.length != 2) {
 	month="0" + month  }
	// Output year in two digits
	year=jsyear
	// Output day in two digits
    if (day.length != 2) {
 	day="0" + day  }
	// Output date in YYMMDD format
 	todayDate=(day+"/"+ month +"/"+ year);
	

if (name!="")
{
	datename="By "+name+" on "+todayDate;
}
else
{
	datename="On "+todayDate;
}
	
livedel ="<a href='#footer' onclick='delcomment()'>Delete This Comment</a>";


		
document.getElementById("livecomment").innerHTML="<blockquote><p><a name='newcomment' id='newcomment'></a>"+ text +"<br/> "+datename+"<br/>"+livedel+"</p></blockquote>";
	
	
document.getElementById("addcommentform").innerHTML="<p><a href='#newcomment'>Thank you, your comment has been added above.</a></p>";
	
        }
      }  
	  
xmlhttp.open("GET","ajax/addnewcomment.php?text="+encodeURI(text)+"&name="+name+"&page="+page,true);
	  alert ("send");
xmlhttp.send(null);	
}


function delcomment()
{	
	document.getElementById("livecomment").innerHTML="";
	document.getElementById("addcommentform").innerHTML="<div id='addcommentform'><p>Your old comment has been deleted. You can add another one if you wish.</p><form name='formcomments' id='formcomments' method='Post'><table border='0'><tr><td align='right'><p>Comments : </p></td><td colspan='2'><textarea name='text' cols='40' rows='5'></textarea></td></tr><tr><td align='right'><p>Your Name (optional) : </p></td><td><input type='text' name='name' size='30'/></td><td align ='left'><input name='page' type='hidden' value='"+ globalpage +"'/><input type='button' name='Submit' value='Save' onclick='savecomment()'/></td></tr></table></form></div>";
	
	


	// add the point and name to the database on the server
	
	//Create a boolean variable to check for a valid MS instance.
	var xmlhttp = false;
	
	//Check if we are using IE.
	try {
		//If the javascript version is greater than 5.
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		//If not, then use the older active x object.
		try {
			//If we are using IE.
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			//Else we must be using a non-IE browser.
			xmlhttp = false;
		}
	}
	
	//If we are using a non-IE browser, create a javascript instance of the object.
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	
	
    xmlhttp.onreadystatechange=function()

      {
      if(xmlhttp.readyState==4)
        {
	document.formcomments.text.value="";
	document.formcomments.name.value="";
	
	
        }
      }  
	// alert ("asking to del"+globallastid);
	  xmlhttp.open("GET","ajax/deletenewcomment.php?id="+globallastid,true);
	  
xmlhttp.send(null);	


	
}

