/*
 * ajax
 */

var http_request = false;

function ajaxGetHandle() {
	var handle = false;

	if (window.XMLHttpRequest) {
		// Mozilla, Safari, Opera ...
		handle = new XMLHttpRequest();

		// for mozilla
		// if (handle.overrideMimeType) {
		// 	handle.overrideMimeType('text/xml');
		// }
	} else if (window.ActiveXObject) {
		// IE &lt; 6.0
		try {
			handle = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				handle = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				// FALLTHROUGHT
			}
		}
	}

	return handle;
}

function ajaxRequest(url, callback, param) {
	http_request = ajaxGetHandle();

	if (!http_request) {
		// alert('Error: No http_request!');

		return false;
	}

	if (param == null) {
		http_request.onreadystatechange = callback;
	} else {
		http_request.onreadystatechange = function(){callback(param);};
	}

	http_request.open('GET', url, true);
	http_request.send(null);

	return true;
}

function ajaxMarkResponse(id) {
	try {
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
				document.getElementById("mark_" + id).innerHTML = http_request.responseText;
			} else {
				// alert('Error: incorrect HTTP code');
				// FALLTHROUGHT
			}
		}
	} catch (e) {
		// alert('Error: ' + e.description);
		// FALLTHROUGHT
	}
}

/*
 * marks
 */
function mark_up(id) {
	document.getElementById("mark_up_" + id).innerHTML = '<span class="icon-mark icon-mark-up-off"></span>';
	document.getElementById("mark_down_" + id).innerHTML = '<span class="icon-mark icon-mark-down-off"></span>';

	ajaxRequest("/dowcipy/" + id + "/plus/", ajaxMarkResponse, id);
}

function mark_down(id) {
	document.getElementById("mark_up_" + id).innerHTML = '<span class="icon-mark icon-mark-up-off"></span>'
	document.getElementById("mark_down_" + id).innerHTML = '<span class="icon-mark icon-mark-down-off"></span>'

	ajaxRequest("/dowcipy/" + id + "/minus/", ajaxMarkResponse, id);
}

