
/*
Copyright 2008 Marek Goźliński (email: markgo@wp.pl)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

function Sevj() {
	var path = document.getElementById("sevj_plugin_path").value;
	var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
	
	this.showTextarea = function() {
		document.getElementById("sevj_div").setAttribute("class", "");
	};
	
	this.sendErrors = function() {
		var page = document.location.href;
		var textarea = document.getElementById("sevj_textarea").value;
		var user_login = document.getElementById("sevj_user_login").value;
		var check = document.getElementById("sevj_check").value;
		if(check != '2') {
			document.getElementById("sevj_status").innerHTML = "Nieprawidłowy wynik. Na pewno jesteś człowiekiem?";
		}
		else if(textarea.length < 10) {
			document.getElementById("sevj_status").innerHTML = "Raport musi być dłuższy niż 10 znaków!";
		}
		else {
			var msg = page + "|" + textarea + "|" + user_login+ "|" + check;
			msg = encodeURIComponent(msg);
	
			xhr.open('POST', path + 'sevj.ajax.php', true);
			xhr.onreadystatechange = this.handleResponse;
			xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			xhr.send('msg=' + msg);
		}
	};
	
	this.handleResponse = function() {
		document.getElementById("sevj_span").setAttribute("class", "sevj_hidden");
		document.getElementById("sevj_div").setAttribute("class", "sevj_hidden");
		if(xhr.readyState == 1 || xhr.readyState == 2 || xhr.readyState == 3) {
			document.getElementById("sevj_status").innerHTML = "<br/><img src=\"" + path + "progress.gif\" alt=\"Progress icon\" />";
		}
		else if(xhr.readyState == 4) {
			var response = xhr.responseText;
			if(response != 'ok') {
				document.getElementById("sevj_div").setAttribute("class", "");
				document.getElementById("sevj_status").innerHTML = response;
			}
			else {
				document.getElementById("sevj_div").setAttribute("class", "sevj_hidden");
				document.getElementById("sevj_status").innerHTML = "<br/>Błąd został zgłoszony. Dziękujemy!";
			}
		}
	};
}
