/**
 * 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 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;
            }
        }
    }
}

/**
 * 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;
        }
    }
}


/*
 * Masquer/Afficher simplement un div
 */
function LiveScroll(id_div) {
	if (id_div != '') action_div(id_div);
}

function action_div(id_div) {
	var item = null;
	if (document.getElementById) {
		item = document.getElementById(id_div);
	} else if (document.all){
		item = document.all[id_div];
	} else if (document.layers){
		item = document.layers[id_div];
	}
	
	if (!item) { // rien à faire
	} else if (item.style) {
		if (item.style.display == "none") {
			item.style.display = "";
		} else {
			item.style.display = "none";
		}
	} else {
		item.visibility = "show";
	}
}


function getElementsByClassName(className, tag, elm){
	var testClass = new RegExp("(^|\\\\s)" + className + "(\\\\s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++){
		current = elements[i];
		if(testClass.test(current.className)){
			returnElements.push(current);
		}
	}
	return returnElements;
}


/*
 * Ajax pour la gestion des numéros
 */
function getXhr() {
	if(window.XMLHttpRequest)
		return new XMLHttpRequest();
	else if(window.ActiveXObject) {
		try { 
			return new ActiveXObject("Msxml2.XMLHTTP"); 
		} catch (e) {
			return new ActiveXObject("Microsoft.XMLHTTP"); 
		} 
	} 
	else {
		//Le navigateur ne supporte pas les objets XMLHTTPRequest
		return false; 
	}
}


function report(id_num, comment) {
	xhr = getXhr();
	
	if (xhr == false) {
		document.location.href = 'index.php?do=report&id_num=' + id_num + '&comment=' + comment ;
	}
	else {
		xhr.onreadystatechange = function() {
			if(xhr.readyState == 4 && xhr.status == 200) {
				document.getElementById('ajax-' + id_num).style.display = 'none';
				document.getElementById('rep-' + id_num).innerHTML = xhr.responseText + '<br />';
				fadeIn('rep-' + id_num, 0);
			} 
		} 
		xhr.open("POST", 'ajaxreport.php', true); 
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); 
		xhr.send("id_num=" + id_num + "&comment=" + comment);
	}
}


/*
 * L'ajax le plus simple au monde, par reduce-your-bill !
 * Exécute la requête reçue et renvoie la réponse
 */
function sendAjaxReq(url,param) {
	xhr = getXhr();

	if (xhr == false) {
		return 'Votre navigateur ne prend pas en charge cette fonctionalité. Pensez à le mettre à jour et à activer JavaScript.';
	}
	else {
		xhr.onreadystatechange = function() {
			if(xhr.readyState == 4 && xhr.status == 200) {
				return xhr.responseText;
			} 
		} 
		xhr.open("POST", url, true); 
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); 
		xhr.send(param);
	}
}

function setOpacity(obj, opacity) {
	opacity = (opacity == 100)?99.999:opacity;

	obj.style.filter = 'alpha(opacity:' + opacity + ')';
	obj.style.KHTMLOpacity = opacity/100;
	obj.style.MozOpacity = opacity/100;
	obj.style.opacity = opacity/100;
	obj.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + opacity + ')';
}

function fadeIn(id_div,opacity) {
	var obj = null;
	if (document.getElementById) {
		obj = document.getElementById(id_div);
	} else if (document.all){
		obj = document.all[id_div];
	} else if (document.layers){
		obj = document.layers[id_div];
	}

	if (opacity <= 100) {
		setOpacity(obj, opacity);
		opacity += 8;
		window.setTimeout("fadeIn('" + id_div + "'," + opacity + ")", 100);
	}
}

function fadeOut(id_div,opacity) {
	var obj = null;
	if (document.getElementById) {
		obj = document.getElementById(id_div);
	} else if (document.all){
		obj = document.all[id_div];
	} else if (document.layers){
		obj = document.layers[id_div];
	}

	if (opacity > 0) {
		setOpacity(obj, opacity);
		opacity -= 8;
		window.setTimeout("fadeOut('" + id_div + "'," + opacity + ")", 100);
	}
}

function displayAjaxFormDiv(id_div) {
	id_div = 'ajax-' + id_div;
	var obj = null;
	if (document.getElementById)
		obj = document.getElementById(id_div);
	else if (document.all)
		obj = document.all[id_div];
	else if (document.layers)
		obj = document.layers[id_div];

	if(obj.style.display != "block") {
		obj.style.display = "block";
		fadeIn(id_div,0);
	}
	else {
		fadeOut(id_div,100);
		obj.style.display = "none";
	}
}