// Declare our request variable.
var request = false;

// Define a function that will make our request for us:
function retrieveNews(categ_id) {
    // Clear the curent request
    request = false;
	var categ_id = categ_id;
	var checkbox_fields = document.news_categ.stiri_categ;
	//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 = updateNews;

    // Open the connection, sending the current value of the form element:
    request.open('GET',
        'ajax-news.php?categ_id=' + getSelectedCheckboxValue(checkbox_fields))
    request.send(null);
}

// The function that will accept the data, and update the page:
function updateNews() {
    // Make sure that the state is '4', which means finished:
    if (request.readyState == 4) {
        // Make sure that the status is 200, or 'ok'
        if (request.status == 200) {
            // And now, update the text on the page:
            var text = document.getElementById('filtred_news');
            //text.innerHTML = result.firstChild.data;
			text.innerHTML = request.responseText;
        } else {
            alert('Error performing request!' + request.status);
        }
    }
}

/*-----------------------------------------------------------+
 | addLoadEvent: Add event handler to body when window loads |
 +-----------------------------------------------------------*/
function addLoadEvent(func) {
	var oldonload = window.onload;
	
	if (typeof window.onload != "function") {
		window.onload = func;
	} else {
		window.onload = function () {
			oldonload();
			func();
		}
	}
}

/*------------------------------------+
 | Functions to run when window loads |
 +------------------------------------*/
addLoadEvent(function () {
	initChecklist();
});

/*----------------------------------------------------------+
 | initChecklist: Add :hover functionality on labels for IE |
 +----------------------------------------------------------*/
function initChecklist() {
	if (document.all && document.getElementById) {
		// Get all unordered lists
		var lists = document.getElementsByTagName("ul");
		
		for (i = 0; i < lists.length; i++) {
			var theList = lists[i];
			
			// Only work with those having the class "checklist"
			if (theList.className.indexOf("checklist") > -1) {
				var labels = theList.getElementsByTagName("label");
				
				// Assign event handlers to labels within
				for (var j = 0; j < labels.length; j++) {
					var theLabel = labels[j];
					theLabel.onmouseover = function() { this.className += " hover"; };
					theLabel.onmouseout = function() { this.className = this.className.replace(" hover", ""); };
				}
			}
		}
	}
}


/*<script src="<?= $website_address;?>/ajax-news.js" type="text/javascript"></script>*/