var http_request = false;
   
// Get the HTTP Objectfunction 
function getHTTPObject(){   
		if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");   
		else if (window.XMLHttpRequest) return new XMLHttpRequest();   
		else {      
		alert("Your browser does not support AJAX.");      
			return null;   
		}
} 
   
function makePOSTRequest(url, parameters, callBack) {

      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 = callBack;
      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 getComments(videoID){
	var poststr = "action=get&v=" + videoID;
	makePOSTRequest('http://www.viddix.com/comment-service.php', poststr, setComments);    
}

// Load comments into DIV
function setComments(){    
	if(http_request.readyState == "4"){        
	var el=document.getElementById("comments")
	el.innerHTML= http_request.responseText; 
	} 
}

// Send data
function checkComment(){    
	document.getElementById("commenterror").style.display="none";
	
	var videoID = document.getElementById("videoIDfield").value;
	
	var comment_email = document.getElementById("comment_email").value;	
	var comment_username = document.getElementById("comment_username").value;
	var comment_text = document.getElementById("comment_text").value;	
	
	var poststr = "action=check&v=" + videoID + "&comment_text=" + document.getElementById("comment_text").value  + "&comment_username=" + document.getElementById("comment_username").value  + "&comment_email=" + document.getElementById("comment_email").value;	
	makePOSTRequest('http://www.viddix.com/comment-service.php', poststr, afterCheck);
}

function afterCheck(){
	if(http_request.readyState == "4"){       
		if(http_request.responseText != ""){
		var el=document.getElementById("commenterror");
		el.innerHTML= http_request.responseText; 
		document.getElementById("commenterror").style.display="block";	
		} else {
		document.getElementById("comment_text").value = "";
		var videoID = document.getElementById("videoIDfield").value;
		getComments(videoID);
		}
	} 
}