// JavaScript Document
var loadingBoxState = false;
var loadingBoxInst = 0;

/**
 * affiche et masque la boite de chargement
 * @param boolean state
 */
function loadingBox(state) {
	
	if (state) {
		loadingBoxInst += 1;
		if (!loadingBoxState) {
			loadingBoxState = true;
			$( "#loading" ).css("visibility", "visible");
			$( "#loading" ).animate({
				  "top": "0", "opacity": 0.8, "visibility": "visible"
				}, 500 );
		}
	}
	else {
		loadingBoxInst -= 1;
		if (loadingBoxInst < 1) {
			loadingBoxState = false;
			$( "#loading" ).animate({
				  "top": "-40", "opacity": 0.0, "visibility": "hidden"
				}, 500 );
		}
	}
	// protégé la variable à 0
	if (loadingBoxInst<0){
		loadingBoxInst = 0;
	}
}

/**
 * défile à l'objet indiqué par le sélecteur jquery
 * @param string selector
 */
function scrollTo(selector) {
	$.scrollTo(selector, 60 );
}

function get_form()
{
	var vargroup = "";
    var formInputs = document.getElementsByTagName('input');
    for (var i = 0; i < formInputs.length; i++) {
        var theInput = formInputs[i];
		if (theInput.type == "checkbox") {
			var checkboxValue;
			if (theInput.checked) {
				checkboxValue = 1;
			}
			else {
				checkboxValue = 0;
			}
			vargroup += "&"+theInput.name+"="+checkboxValue;
		}
		else {
			vargroup += "&"+theInput.name+"="+prepare_text(htmlentities(theInput.value));
		}
	}
    var formTexts = document.getElementsByTagName('textarea');
    for (var i = 0; i < formTexts.length; i++) {
        var theInput = formTexts[i];
		if (theInput.name == "txtEdit") {
			vargroup += "&"+'content'+"="+prepare_text(editor_getText(theInput.id));
		}
		else {
			vargroup += "&"+theInput.name+"="+prepare_text(htmlentities(theInput.value));
		}
		
	}
    var formSelects = document.getElementsByTagName('select');
    for (var i = 0; i < formSelects.length; i++) {
        var theInput = formSelects[i];
			vargroup += "&"+theInput.name+"="+prepare_text(theInput.value);
	}

	return vargroup;
}

function clear_id(div)
{
	$('#'+div).html('');
}

function setVisibility(div, state)
{
	if (state) {
		document.getElementById(div).style.display = "block";
	}
	else {
		document.getElementById(div).style.display = "none"; 
	}
}
 
function runScripts(e) {
	if (e.nodeType != 1) return; //if it's not an element node, return
 
	if (e.tagName.toLowerCase() == 'script') {
		eval(e.text); //run the script
	}
	else {
		var n = e.firstChild;
		while ( n ) {
			if ( n.nodeType == 1 ) runScripts( n ); //if it's an element node, recurse
			n = n.nextSibling;
		}
	}
}

function enableButtonIfnotEmpty(input) {
	if (input.value) {
		$('#editeur_boutons #btn_ajout').attr('disabled', false);
		$('#editeur_boutons #btn_edit').attr('disabled', false);
	}
	else {
		$('#editeur_boutons #btn_ajout').attr('disabled', true);
		$('#editeur_boutons #btn_edit').attr('disabled', true);
	}
	
	if (input.value == $('#nom_defaux').val()) {
		$('#editeur_boutons #btn_ajout').attr('disabled', true);
	}
}

function displayIfTrue(selector, state)
{
	if (state) {
		$(selector).css('display', 'inline-block');
	}
	else {
		$(selector).css('display', 'none');
	}
}
