// CREATING THE REQUEST

function createRequestObject()
{
	try
	{
		xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch(e)
	{
		alert('Sorry, but your browser doesn\'t support XMLHttpRequest.');
	}
	return xmlhttp;
}

var http = createRequestObject();
var sess = createRequestObject();

// IMAGE REFRESHING

function refreshimg()
{
	var url = 'http://' + top.location.host + '/lib/ajaxcaptcha/image_req.php';
	dorefresh(url, displayimg);
}

function dorefresh(url, callback)
{
	sess.open('POST', 'http://' + top.location.host + '/lib/ajaxcaptcha/newsession.php', true);
	sess.send(null);
	http.open('POST', url, true);
	http.onreadystatechange = displayimg;
	http.send(null);
}

function displayimg()
{
	if(http.readyState == 4)
	{
		var showimage = http.responseText;
		$('.captchaimage').html(showimage);
	}
}

// SUBMISSION

function check(a)
{
	if (a == 0) {
		var submission = $('#captcha').val();
		var url = 'http://' + top.location.host + '/mods/request.php';
		
		var params = 'captcha=' + submission + '&mes=1&postmes=' + $("#nomes").contents().find("body").html() + '&uid=' + readCookie("user") + '&parent=' + $("input[name=submit]").attr("par");
		
		http.open("POST", url, true);
		
		http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Content-length", params.length);
		http.setRequestHeader("Connection", "close");
		http.onreadystatechange = displaycheck;
		
		http.send(params);
		
	}else{
		var submission = $('#captcha_'+a).val();
		var url = 'http://' + top.location.host + '/mods/request.php';
		
		var params = 'captcha=' + submission + '&mes=1&postmes=' + $("#nomes_"+a).contents().find("body").html() + '&uid=' + readCookie("user") + '&parent=' + a;
	
		http.open("POST", url, true);
		
		http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Content-length", params.length);
		http.setRequestHeader("Connection", "close");
		http.onreadystatechange = displaycheck;
		
		http.send(params);
	}

}

function docheck(url, callback)
{
	http.open('GET', url, true);
	http.onreadystatechange = displaycheck;
	http.send(null);
}

function displaycheck()
{
	if(http.readyState == 4)
	{
		var showcheck = http.responseText;
		if(showcheck == 1)
		{
			document.getElementById('captcha').style.border = '1px solid #49c24f';
			document.getElementById('captcha').style.background = '#bcffbf';
			
			// ! $("#forum_table").hide("slide", { direction: "up" }, 800);

			//$("#forum_table").html("<tr><td width='800px'><center><img style='margin:30px' src='http://"+top.location.host+"/images/loader.gif'><br><h2>Refreshing...</h2></center></td></tr>");
			//$("#forum_table").show("slide", { direction: "up" }, 800);

			
			$.post("http://"+top.location.host+"/mods/request.php", {
            			refresh: "1",
						url: location.href
      					  },function(data){
						  	//$("#forum_table").hide("slide", { direction: "up" }, 800);
                                                refreshimg(); 
        					// ! $("#forum_table").html(data);
							// ! $("#forum_table").show("slide", { direction: "up" }, 800);
							
							location.href=location.href;
							
                            ///QUOTe
                            $(".quot").click(function(){
                                $('#nomes_' + $(this).attr("gid")).rte({
                                    width: 450,
                                    height: 200,
                                    controls_rte: rte_toolbar
                                });
                                $("#mes_" + $(this).attr("gid")).show();
                                
                            });
                            $(".newmes_with_posts").click(function(){
                                $('#nomes').rte({
                                    width: 450,
                                    height: 200,
                                    controls_rte: rte_toolbar
                                });
                                $("#capcha_new").show();
                                
                            });
      				  });
			
		}
		if(showcheck == 0)
		{
			$("input[name=captcha]").each(function(){
				$(this).css({'border':'1px solid #c24949', 'background':'#ffbcbc'})
			});
		}
		if(showcheck == 2)
		{
			$("iframe").each(function(){
				$(this).css({ 'background':'#ffbcbc'})
			});


		}
		if(showcheck == 3)
		{
			alert('Post creation ERROR');


		}
	}
}
