
textIDGlobal = "com_content";


function createObject() {
var http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/xml');

// Читайте ниже об этой строке
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
return http_request;
}
/* -------------------------- */
/*          SAVE TEXT         */
/* -------------------------- */


function addComment(id){
http = createObject();

text = encodeURI(document.getElementById('textId'+id).value);
name = encodeURI(document.getElementById('nameId'+id).value);
http.open('post', '/action/public.php',true);
http.onreadystatechange = function(){
if (http.readyState == 4) {
if (http.status == 200) {
var response = http.responseText;
if(id){
	document.getElementById(id).innerHTML = response;
}else{
	comments = document.getElementById(textIDGlobal).innerHTML;
	document.getElementById(textIDGlobal).innerHTML = comments + response;
}
}else if(http.status == 401){
			 document.location.href = "/index.php?action=login";
			}else if(http.status == 403){
			 document.location.href = "/";
			}
}
}
var data = 'newText='+text+'&id='+id+'&name='+name;
http.setRequestHeader("Content-length", data.length); // Длинна отправляемых данных
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.send(data);
}


function remove(id){
http = createObject();
var answer = confirm ("Are you sure?")
if (answer)
{
	http.open('get', '/action/remove.php?id='+id,true);
	http.onreadystatechange = function(){
	if (http.readyState == 4) {
	if (http.status == 200) {
//var response = http.responseText;
	node 				= document.getElementById(id);
	parentNode 	= node.parentNode;
	parentNode.removeChild(node);
	}else if(http.status == 401){
			 document.location.href = "/index.php?action=login";
			}else if(http.status == 403){
			 document.location.href = "/";
			}
	}
	}
	http.send(null);
	}
}


function edit(id, action){
	
	http = createObject();
	http.open('get', '/action/edit.php?id='+id+'&action='+action,true);
	
	http.onreadystatechange = function(){
		if (http.readyState == 4) {
//		alert(http.status);
			if (http.status == 200) {
				document.getElementById(id).innerHTML = http.responseText;
			}else if(http.status == 401){
			 document.location.href = "/index.php?action=login";
			}else if(http.status == 403){
			 document.location.href = "/";
			}
		}
	}
	http.send(null);
}




