//AHAJAX Functions - Copyright 2008 All rights reserved
//Proceses GET Request with server
var xmlhttp
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	  try {
	  xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
	 } catch (e) {
	  try {
	    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
	  } catch (E) {
	   xmlhttp=false
	  }
	 }
	@else
	 xmlhttp=false
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	 try {
	  xmlhttp = new XMLHttpRequest();
	 } catch (e) {
	  xmlhttp=false
	 }
	}
	function myXMLHttpRequest() {
	  var xmlhttplocal;
	  try {
	    xmlhttplocal= new ActiveXObject("Msxml2.XMLHTTP")
	 } catch (e) {
	  try {
	    xmlhttplocal= new ActiveXObject("Microsoft.XMLHTTP")
	  } catch (E) {
	    xmlhttplocal=false;
	  }
	 }

	if (!xmlhttplocal && typeof XMLHttpRequest!='undefined') {
	 try {
	  var xmlhttplocal = new XMLHttpRequest();
	 } catch (e) {
	  var xmlhttplocal=false;
	  alert('couldn\'t create xmlhttp object');
	 }
	}
	return(xmlhttplocal);
}

function addslashes(str) {
    return str.replace('/(["\'\])/g', "\\$1").replace('/\0/g', "\\0");
}

function getComments(id, page) {	
	document.getElementById('comment_loader').className = 'comment_loader';
	var url = "/include/get_comments.php?VID=" + id + "&page="+ page;
	
	xmlhttp.open("GET", url);
	xmlhttp.onreadystatechange = showComments;
	xmlhttp.send(null);
}


function showComments() {
    if(xmlhttp.readyState == 4) {
			//update div
			document.getElementById('relatedVideos').className = 'hidden';
			document.getElementById('commentpost').value = '';
			document.getElementById('add_comment').className = '';
			document.getElementById('comment_sent_loader').className = 'hidden';
            document.getElementById('comment_loader').className = 'hidden';
            document.getElementById('divComments').className = '';
         	document.getElementById('divComments').innerHTML = xmlhttp.responseText;
         	document.getElementById('comCount').innerHTML = document.getElementById('comTotal').innerHTML;
    }
}

function sndComment(comment,uid,vid) {
	document.getElementById('comment_sent_loader').className = '';
	document.getElementById('comment_sent_loader').innerHTML = '<br /><br /><div style="text-align: center;"><img src="/images/spinna.gif" alt="Loading..." /></div>';	
	document.getElementById('add_comment').className = 'hidden';
	
    xmlhttp.open('get', '/include/add_comment.php?c='+addslashes(comment)+'&uid='+uid+'&id='+vid);
    xmlhttp.onreadystatechange = showComments;
    xmlhttp.send(null);	
}

var commentAction = {
		'input#submit' : function(element){
			element.onclick = function(){
			
			var theComment = document.getElementById('commentpost').value;
				if(theComment != ""){
					var uid = document.getElementById('number').value;
					var vid = document.getElementById('id').value;

					sndComment(theComment, uid, vid); return false;		
				}
			}
		}
		
	};
Behaviour.register(commentAction);