function dataServer() {};
dataServer.prototype.initiate = function() {
	try {
		this._xh = new XMLHttpRequest();
	} catch (e) {
		var _ieModel = new Array('MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','Microsoft.XMLHTTP');
		var success = false;
		for (var i=0;i < _ieModel.length && !success; i++) {
			try {
				this._xh = new ActiveXObject(_ieModel[i]);
				success = true;
			} catch (e) {
			}
		}
		if (!success) {
			return false;
		}
		return true;
	}
}

dataServer.prototype.occupy = function() {
	stateActual = this._xh.readyState;
	return (stateActual && (stateActual < 4));
}

dataServer.prototype.process = function() {
	if (this._xh.readyState == 4 && this._xh.status == 200) {
		this.process = true;
	}
}

dataServer.prototype.enviro = function(urlget,data) {
	if (!this._xh) {
		this.initiate();
	}
	if (!this.occupy()) {
		this._xh.open("GET",urlget,false);
		this._xh.send(data);
		if (this._xh.readyState == 4 && this._xh.status == 200) {
			return this._xh.responseText;
		}
	}
	return false;
}

window.location.querystring = (function() {
	var collection = {};
	var querystring = window.location.search;
 
	if (!querystring) {
		return { toString: function() { return ""; } };
	}
 
	querystring = decodeURI(querystring.substring(1));
	var pairs = querystring.split("&");
 
	for (var i = 0; i < pairs.length; i++) {
		if (!pairs[i]) { continue; }

		var seperatorPosition = pairs[i].indexOf("=");
 
		if (seperatorPosition == -1) {
			collection[pairs[i]] = "";
		}
		else {
			collection[pairs[i].substring(0, seperatorPosition)] 
			= pairs[i].substr(seperatorPosition + 1);
		}
	}
 
	collection.toString = function() { return "?" + querystring; };
	return collection;
})();

function postComment() {
	var querystring = window.location.querystring;
	var video = querystring["videoid"];
	
	elem = document.getElementById("vB_Editor_QR_textarea");
	remote = new dataServer;
	nt = remote.enviro("videoajax.php?do=postcomment&videoid=" + video + "&message=" + encodeURI(elem.value),"");

	if (nt == '1') {
		elem.value = "";

		elem = document.getElementById("vidSubmit");
		elem.value = "Your comment has been posted...";
		elem.disabled = true;

		getComments(1);
	} else {
		nt = nt.replace(/\n/i, '');
		while (nt.match(/<.+?>/i)) {
			nt = nt.replace(/<.+?>/i, '');
		}

		alert(nt);
		// alert("The entered comment is too short.");
	}
}

function delComment(cmnt) {
	var answer = confirm("Delete this comment?");
	if (answer) {
		remote = new dataServer;
		remote.enviro("videoajax.php?do=deletecomment&cmntid=" + cmnt,"");
		getComments(1);
	}
}

function getComments(page) {
	var querystring = window.location.querystring;
	var video = querystring["videoid"];

	elem = document.getElementById("vidComments");
	remote = new dataServer;
	nt = remote.enviro("videoajax.php?do=getcomments&videoid=" + video + "&page=" + page,"");
	elem.innerHTML = nt;

	elem = document.getElementById("vidPagesel");
	remote = new dataServer;
	nt = remote.enviro("videoajax.php?do=getpagesel&videoid=" + video + "&page=" + page,"");
	elem.innerHTML = nt;
}

function addEvent(elm, evType, fn, useCapture) {
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	} else if (elm.attachEvent) {
		var r = elm.attachEvent("on"+evType, fn);
		return r;
	} else {
		alert("Please upgrade your browser to use full functionality on this page");
	}
}

addEvent(window, "load", getComments);
