var http;



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



//we wrote this function even we have other function doPost, which is for comments only, but this function below will work for common ajax tranactions.
function submitPOST(url,params,func) {


	 
   http = new Ajax_call();
  
   if (http) {
	   

      http.onreadystatechange = func;
	  http.open('POST', url, true);
	  http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	
      http.send(params);
	 
	  
   }else
    {
	alert("http undefined");
	}
}



function submitAnswer(uid, qid, answer)
{
		
  var url= "submitAnswer.php";
  var postArray = "uid=" + escape(uid) +"&qid="+escape(qid)+"&answer="+escape(answer)+"&seid="+Math.random();
  
   http = new Ajax_call();
  
   if (http) {
	   

      http.onreadystatechange = function(){ answersUpdate(qid); };
	  http.open('POST', url, true);
	  http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	
      http.send(postArray);
	 
	  
   }else
    {
	alert("http undefined");
	}
 
	
}





function answersUpdate(qid)
{
	
  var output = document.getElementById('answerForm');

if(http.readyState == 0)
 {
   output.innerHTML = "Error";
 }
 
 if(http.readyState ==3)
{
output.innerHTML = '<img src="includes/loading.gif" alt="Loading.." />';

 }
 
  if(http.readyState==4)
  {
    show_comments(qid);
  output.innerHTML=http.responseText;

     
   }
   
   
	}
	

function show_comments(qid)
 {
	
 httpreq = new Ajax_call();
  if (httpreq==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  }
  

  
 var url = "comments.php?qid="+qid+"&seid="+Math.random();

  httpreq.onreadystatechange = comments_status;
  httpreq.open("GET",url,true);
 
  httpreq.send(null);

  
 }
 
 
 
 function comments_status(){

var output = document.getElementById('answers');

if(httpreq.readyState == 0)
 {
   output.innerHTML = "Error";
 }
 
 if(httpreq.readyState ==3)
{
output.innerHTML = '<img src="loading.gif" alt="Loading.." />';

 }
 
  if(httpreq.readyState==4)
  {
    
  output.innerHTML=httpreq.responseText;
     
   }
   }
   
   
function updateSubCats()
{
	
  	http = new Ajax_call();
	var cid = document.getElementById('categorySelect').options[document.getElementById('categorySelect').selectedIndex].value;
	var url = 'subCatsOpts.php?cid='+cid;
  http.onreadystatechange = function(){subCatsOptsUpdate(); };
  http.open("GET",url,true);
 
  http.send(null);
}
   
   
   
function subCatsOptsUpdate()
{
	var output = document.getElementById('subCatsSelect');

    if(http.readyState == 0)
    {
       output.innerHTML = "Error";
    }
 
    if(http.readyState ==3)
   {
       output.innerHTML = '<img src="loading.gif" alt="Loading.." />';
   }
 
  if(http.readyState==4)
  {
    
      output.innerHTML=http.responseText;
  }
}
   
   var newwindow;
function pop_comments(url,filename)
{
	newwindow=window.open(url,'All Comments for '+filename,'height=400,width=600,scrollbars=yes,resizable=yes');
	if (window.focus) {newwindow.focus()}
}
	
	
	
