// Declare our request variable.
var request = false;

var Url = {

	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},

	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},

	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}

}


// Define a function that will make our request for us:
function edit_comments(categ_id,table_id) {
    // Clear the curent request
    request = false;
	var categ_id = categ_id;
	var table_id = table_id;
	//alert(table_id);
	//alert(categ_id);
	//alert(getSelectedCheckbox(checkbox_fields));
    //alert(getSelectedCheckboxValue(checkbox_fields));
	
	// Generate the request object and handle different browsers:
    if (window.XMLHttpRequest) { // Mozilla & other compliant browsers
        request = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // Internet Explorer
        request = new ActiveXObject("Microsoft.XMLHTTP");
    }

    // If we don't have a request object, then error out.
    if (!request) {
        alert('Browser does not support AJAX!');
        return false;
    }

    // Ok, now we are ready.  Make the request, and tell it to run the
    // function 'updateDate' when it gets data back.
    request.onreadystatechange = update_comments;

    // Open the connection, sending the current value of the form element:
    request.open('GET',
        'http://www.sibiul.ro/ajax-comments.php?id=' + categ_id + '&table_id=' + table_id + '&action=display_form');
    request.send(null);
	//alert(request.responseText);
	//alert('Error performing request!' + request.status);
}

// The function that will accept the data, and update the page:
function update_comments() {
	//alert('aaaaaa');
	//var id = id;
	//alert('111' + request.readyState);
	//alert(id);
    // Make sure that the state is '4', which means finished:
    if (request.readyState == 4) {
		//alert('222' + request.readyState);
        // Make sure that the status is 200, or 'ok'
        if (request.status == 200) {
			//alert('3333' + request.readyState);
            // And now, update the text on the page:
			//alert(id);
			//alert(request.responseText);
			var pos = request.responseText.indexOf('<');
			//alert(pos);
			var id = request.responseText.substring(0,pos);
			//alert(id);
            var text = document.getElementById('comment_' + id);
            //text.innerHTML = result.firstChild.data;
			//alert(request.responseText);
			text.innerHTML = request.responseText.substring(pos,request.responseText.length);
        } else {
            alert('Error performing request!' + request.status);
        }
    }
	
}

function update_edit_comments(comment_id, table_id) {
    // Clear the curent request
    request = false;
	var comment_id = comment_id;
	var table_id = table_id;
	var comment = Url.encode(document.getElementById('comment_text_hidden').value);
	//var comment_orig = document.getElementById('comment_upd').value;
	
	//alert(comment_id);
	//alert(comment_orig);
	//alert(comment);
	//alert(getSelectedCheckbox(checkbox_fields));
    //alert(getSelectedCheckboxValue(checkbox_fields));
	
	// Generate the request object and handle different browsers:
    if (window.XMLHttpRequest) { // Mozilla & other compliant browsers
        request = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // Internet Explorer
        request = new ActiveXObject("Microsoft.XMLHTTP");
    }

    // If we don't have a request object, then error out.
    if (!request) {
        alert('Browser does not support AJAX!');
        return false;
    }

    // Ok, now we are ready.  Make the request, and tell it to run the
    // function 'updateDate' when it gets data back.
    request.onreadystatechange = update_comments2;
	//alert(table_id);
    // Open the connection, sending the current value of the form element:
    request.open('GET',
        'http://www.sibiul.ro/ajax-comments.php?id=' + comment_id + '&comment=' + comment + '&table_id='+ table_id +'&action=edit_comment');
    request.send(null);
	//alert(request.responseText);
	//alert('Error performing request!' + request.status);
}

// The function that will accept the data, and update the page:
function update_comments2() {
	//alert('aaaaaa');
	//var id = id;
	//alert('111' + request.readyState);
	//alert(id);
    // Make sure that the state is '4', which means finished:
    if (request.readyState == 4) {
		//alert('222' + request.readyState);
        // Make sure that the status is 200, or 'ok'
        if (request.status == 200) {
			//alert('3333' + request.readyState);
            // And now, update the text on the page:
			//alert(id);
			//alert(request.responseText);
			//var pos = request.responseText.indexOf('<');
			//alert(pos);
			//var id = request.responseText.substring(0,pos);
			//alert(id);
			
			
			
			var pos = request.responseText.indexOf('#');
			//alert(pos);
			var id = request.responseText.substring(0,pos);
			//alert(id);
            var text = document.getElementById('comment_' + id);
            //text.innerHTML = result.firstChild.data;
			//alert('ddddddd' + request.responseText);
			text.innerHTML = request.responseText.substring(pos+1,request.responseText.length);
			
			/*if (text) alert('ok');
			else
				alert('not ok');
		   text.innerHTML = 'blsdsf';*/
			
			
/*			
            var text = document.getElementById('comment_' + id);
            //text.innerHTML = result.firstChild.data;
			text.innerHTML = request.responseText;*/
        } else {
            alert('Error performing request!' + request.status);
        }
    }
	
}


function delete_comments(comment_id, table_id, user_id) {
    // Clear the curent request
    request = false;
	var comment_id = comment_id;
	var table_id = table_id;
	var user_id = user_id;
	//alert(comment_id);
	//alert(comment);
	
	// Generate the request object and handle different browsers:
    if (window.XMLHttpRequest) { // Mozilla & other compliant browsers
        request = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // Internet Explorer
        request = new ActiveXObject("Microsoft.XMLHTTP");
    }

    // If we don't have a request object, then error out.
    if (!request) {
        alert('Browser does not support AJAX!');
        return false;
    }

    // Ok, now we are ready.  Make the request, and tell it to run the
    // function 'updateDate' when it gets data back.
    request.onreadystatechange = remove_deleted_comments;
	//alert(table_id);
    // Open the connection, sending the current value of the form element:
    request.open('GET',
        'http://www.sibiul.ro/ajax-comments.php?id=' + comment_id + '&table_id='+ table_id +'&action=delete_comment&user_id='+user_id);
    request.send(null);
	alert(request.responseText);
	//alert('Error performing request!' + request.status);
}

// The function that will accept the data, and update the page:
function remove_deleted_comments() {
    // Make sure that the state is '4', which means finished:
    if (request.readyState == 4) {
		//alert('222' + request.readyState);
        // Make sure that the status is 200, or 'ok'
        if (request.status == 200) {
			
			var response = request.responseText;
	
			if(response.indexOf('^') != -1) 
			{
				//alert("in if" + response);
				var update = new Array();
				update = response.split('^');
				
				if(document.getElementById('comment_' + update[0] + '_container'))
				{
					var text = document.getElementById('comment_' + update[0] + '_container');
					text.innerHTML = update[1];
				}
				else
				{
					//alert(document.getElementById('comment_' + update[0] + '_container')+" <= nu am gasit divu asta (array)");	
				}
			}
			else
			{
				if(document.getElementById('comment_' + response + '_container'))
				{
					var text = document.getElementById('comment_' + response + '_container');
					text.innerHTML = '';
				}
				else
				{
					//alert(document.getElementById('comment_' + response + '_container')+" <= nu am gasit divu asta (text) ");	
				}
			}
			
			
			
        } else {
            alert('Error performing request!' + request.status);
        }
    }
	
}

