function _isIE() {
	return (navigator.userAgent.toLowerCase().indexOf("msie") != -1);
}

function _isOPERA() {
	return (navigator.userAgent.toLowerCase().indexOf("opera") != -1);
}

if(!_isIE()){
	HTMLElement.prototype.removeNode = function(removeChildren) {
		if (Boolean(removeChildren) && this.parentNode!=null)
		return this.parentNode.removeChild(this);
		else {
			var r=document.createRange();
			r.selectNodeContents(this);
			if(this.parentNode!=null)
			return this.parentNode.replaceChild(r.extractContents(),this);
			//else return true
		}
	}
}

function makeAjaxRequest(url){
	$.ajax({
            type: "GET",
            url: url,
            dataType: "json",
            success : onAfterAjax
     });
}


function onAfterAjax(json){
	$('#' + json.container).html(json.content);
}

function pasteMessages(jsonMessages){

	$('#messages').remove();

	if(jsonMessages){

		var errorString = '';
		var class_name = '';
		for(i=0; i<jsonMessages.length; i++){
			if(!jsonMessages[i].type){
				class_name = 'green';				
			}else{
				class_name = 'alert';
			}
		
			errorString = errorString + '<div style="width:100%" class="'+class_name+'">'+jsonMessages[i].message+'</div>';	
		}
		
		$('#main').prepend('<div id="messages" class="errorDiv">'+errorString+'</div>');
	}		
}

function removeMessages(){
	$('#messages').remove();
}
/**
 * marks all rows and selects its first checkbox inside the given element
 * the given element is usaly a table or a div containing the table or tables
 *
 * @param    container    DOM element
 */
var marked_row = new Array();
function markAllRows( container_id ) {
    var rows = document.getElementById(container_id).getElementsByTagName('tr');
    var unique_id;
    var checkbox;

    for ( var i = 0; i < rows.length; i++ ) {

        checkbox = rows[i].getElementsByTagName( 'input' )[0];

        if ( checkbox && checkbox.type == 'checkbox' ) {
            unique_id = checkbox.name + checkbox.value;
            if ( checkbox.disabled == false ) {
                checkbox.checked = true;
                if ( typeof(marked_row[unique_id]) == 'undefined' || !marked_row[unique_id] ) {
                    rows[i].className += ' marked';
                    marked_row[unique_id] = true;
                }
            }
        }
    }

    return true;
}

/**
 * marks all rows and selects its first checkbox inside the given element
 * the given element is usaly a table or a div containing the table or tables
 *
 * @param    container    DOM element
 */
function unMarkAllRows( container_id ) {
    var rows = document.getElementById(container_id).getElementsByTagName('tr');
    var unique_id;
    var checkbox;

    for ( var i = 0; i < rows.length; i++ ) {

        checkbox = rows[i].getElementsByTagName( 'input' )[0];

        if ( checkbox && checkbox.type == 'checkbox' ) {
            unique_id = checkbox.name + checkbox.value;
            checkbox.checked = false;
            rows[i].className = rows[i].className.replace(' marked', '');
            marked_row[unique_id] = false;
        }
    }

    return true;
}
