function ajaxLoad(method, url, id, wait, parametrs) { 
	if (window.XMLHttpRequest) var http_request = new XMLHttpRequest();
	else if (window.ActiveXObject) var http_request = new ActiveXObject("Microsoft.XMLHTTP");	
	if (http_request) {
		if (wait) document.getElementById(wait).style.display = '';
		http_request.onreadystatechange = function() {
			if (http_request.readyState == 4) {
				var response = http_request.responseText;
				var re = /<script.*?>(.*?)<\//igm;
				var match;
				while (match = re.exec(response)) eval(match[1]);
				document.getElementById(id).innerHTML = response; 
				if (wait) document.getElementById(wait).style.display = 'none';	} }
		if (method == "POST") {
			http_request.open("POST", url, true);
			http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			http_request.setRequestHeader("Content-length", parametrs.length);
			http_request.setRequestHeader("Connection", "close");
			http_request.send(parametrs); }
		else {
			http_request.open("GET", url, true);
			http_request.send(null); } } }

function ajaxVote(video, vote) {
	ajaxLoad('GET', 'index.php?action=vote&video=' + video + '&vote=' + vote + '&ajax=true', 'ajax_vote', 'ajax_vote_wait'); }

function ajaxVoteShow(video) {
	ajaxLoad('GET', 'index.php?action=vote_show&video=' + video + '&ajax=true', 'ajax_vote_show', 'ajax_vote_show_wait'); }

function ajaxFavorite(video) {
	ajaxLoad('GET', 'index.php?action=favorite&video=' + video + '&ajax=true', 'ajax_favorite', 'ajax_favorite_wait'); }

function ajaxReport(video) {
	ajaxLoad('GET', 'index.php?action=report&video=' + video + '&ajax=true', 'ajax_report', 'ajax_report_wait'); }

function ajaxComment(video) {
	var submitParametrs = 'action=comment&video=' + video + '&comment_message=' + encodeURI(document.getElementById("editor_area").value) + '&comment_captcha=' + encodeURI(document.getElementById("comment_captcha").value);
	ajaxLoad('POST', 'index.php?ajax=true', 'ajax_comment', 'ajax_comment_wait', submitParametrs); }

function ajaxCommentShow(video, page) {
	ajaxLoad('GET', 'index.php?action=comment_show&video=' + video + '&page=' + page + '&ajax=true', 'ajax_comment_show', 'ajax_comment_show_wait'); }

function ajaxSuggest() {
	if (window.XMLHttpRequest) var http_request = new XMLHttpRequest();
	else if (window.ActiveXObject) var http_request = new ActiveXObject("Microsoft.XMLHTTP");	
	if (http_request) {
		var str = escape(document.getElementById('suggest_title').value);
		if (str.length >= 3) {
			http_request.onreadystatechange = function() {
				if (http_request.readyState == 4) {
					document.getElementById('search_suggest').style.display = 'block';
					document.getElementById('search_suggest').innerHTML = '';
					var str = http_request.responseText.split("\n");
					if ((str.length - 1) == 0) {
						document.getElementById('search_suggest').style.display = 'none'; }
					else {
						for(i=0; i < str.length - 1; i++) {
							var suggest = "<div onmouseover=\"javascript:this.className='suggest_link_over';\" ";
							suggest += "onmouseout=\"javascript:this.className='suggest_link';\" ";
							suggest += "onclick=\"javascript:ajaxSuggestSearch(this.innerHTML);\" ";
							suggest += "class='suggest_link'>" + str[i] + "</div>";
							document.getElementById('search_suggest').innerHTML += suggest; } } } }
			http_request.open("GET", 'index.php?action=suggest&title=' + str + '&ajax=true', true);
			http_request.send(null); }
		else {
			document.getElementById('search_suggest').style.display = 'none'; }	} }

function ajaxSuggestSearch(value) {
	document.getElementById('suggest_title').value = value.substr(0, value.indexOf("<"));
	document.getElementById('search_suggest').innerHTML = '';
	document.getElementById('search_suggest').style.display = 'none'; }
