// JavaScript Document

function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}

function ajaxFunction()
{
var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }

var url = "comments.php";
var params = "cname="+encodeURI(document.getElementById("cName").value);
params = params+"csite="+encodeURI(document.getElementById("cSite").value);
params = params+"cemail="+encodeURI(document.getElementById("cEmail").value);
params = params+"ccomment="+encodeURI(document.getElementById("cComment").value);
xmlHttp.open("POST", url, true);

//Send the proper header information along with the request
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", params.length);
xmlHttp.setRequestHeader("Connection", "close");
xmlHttp.send(params);
xmlHttp.onreadystatechange = function() {//Call a function when the state changes.
	if(xmlHttp.readyState == 4) {
		document.getElementById("commentsContainer").innerHTML=xmlHttp.responseText;
		
		
	}
}


}


/*function loadcomments(page_request, containerid){
if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
document.getElementById(containerid).innerHTML=page_request.responseText
}*/

function chkFields(){
	//alert("click");
	if(document.getElementById("cName").value && document.getElementById("cComment").value){
		
	}else{
		alert("Please fill out the required fields");
		return;
	}
}

function showComments(){
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 
var url="comments.php";
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
} 
function stateChanged() 
{ 
if (xmlHttp.readyState==4)
{ 
document.getElementById("commentsContainer").innerHTML=xmlHttp.responseText;
}
}


   var http_request = false;
   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;
      }
      
	  	if(document.getElementById("cName").value && document.getElementById("cComment").value){
		
	}else{
		alert("Please fill out the required fields");
		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);
   }

   function alertContents() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            //alert(http_request.responseText);
            result = http_request.responseText;
            document.getElementById("commentsContainer").innerHTML=result;          
         } else {
            alert('There was a problem with the request.');
         }
      }
   }
   
   function get(obj) {

	var params = "cname="+encodeURI(document.getElementById("cName").value)
	+"&csite="+encodeURI(document.getElementById("cSite").value)
	+"&cemail="+encodeURI(document.getElementById("cEmail").value)
	+"&ccomment="+encodeURI(document.getElementById("cComment").value);

      makePOSTRequest('comments.php', params);
   }
