function emailList(autocompleter, token) {
    var userId = $("idSociete").value;
    // "idSociete", c'est nul pour le input hidden, mais c'était déjà comme ça et c'est délicat à modifier
    var isAgencyUser = $("isAgencyUser").value;
    if (isAgencyUser=="true") {
        UserServiceFacade.getCompleteEmailByAgencyUserId(token, userId, function(data) {
            autocompleter.setChoices(data)
        });
    } else {
        UserServiceFacade.getCompleteEmailByCompanyUserId(token, userId, function(data) {
            autocompleter.setChoices(data)
        });
    }
}

function hideElement(elementName) {
    var element = document.getElementById(elementName);
    if (element != null) {
        element.style.visibility = "hidden";
        element.style.display = "none";
    }
}

function showElement(elementName) {
    var element = document.getElementById(elementName);
    if (element != null) {
        element.style.visibility = "visible";
        element.style.display = "block";
    }
}

function showHideElement(elementName) {

    if (elementName != "mailBlock") 
        hideElement("mailBlock");
    
    var element = document.getElementById(elementName);
    if (element != null) {
        if (element.style.visibility == "visible" || element.style.display == "block") {
            hideElement(elementName);
        } else {
            showElement(elementName);
            
            if (elementName == "mailBlock") {
                var newUrl = "#" + "mailBlockEncre";
                document.location.href = newUrl;
            }
        }
    }
    if (document.getElementById('downloadEmail') != null) 
        document.getElementById('downloadEmail').focus();
    
}

function showHideElementAndRenameButton(elementName, buttonName, buttonValue0, buttonValue1) {
    showHideElement(elementName);
    if (elementName == 'detailedSearchBlock') {
        showHideElement('detailedSearchBlock_budget');
    }
    var button = document.getElementById(buttonName);
    if (button != null) {
        if (button.value == buttonValue0) {
            button.value = buttonValue1;
        } else {
            button.value = buttonValue0;
        }
    }
}

function showHideElementAndRenameButtonByName(elementName, buttonName, buttonValue0, buttonValue1) {
    showHideElement(elementName);
    if (elementName == 'detailedSearchBlock') {
        showHideElement('detailedSearchBlock_budget');
    }
    var button = document.getElementsByName(buttonName);
    
    for (var i = 0; i < button.length; i++) {
        if (button[i] != null) {
            if (button[i].value == buttonValue0) {
                button[i].value = buttonValue1;
            } else {
                button[i].value = buttonValue0;
            }
        }
    }
}

function switchCheckbox(checkboxId) {
    var chk = document.getElementById(checkboxId);
    if (chk != null) {
        if (chk.checked || chk.checked == "checked") {
            chk.checked = false;
        } else {
            chk.checked = "checked";
        }
    }
}

function emailIsValid(email) {
    //var regexp = "^([_a-z0-9-]+)(.[_a-z0-9-]+)*@([a-z0-9-]+)(.[_a-z0-9-]+)*(.[a-z]{2,4}\t*)$";
    var regexp = "^([_a-z0-9-]+)(.[_a-z0-9-]+)*@([a-z0-9-]+)(.[_a-z0-9-]+)*(.[a-z]{2,4})$";
    if (email.match(regexp)) {
        return true;
    }
}

function dateIsValid(mydate) {
    var regexp = "^((0?[0-9])|([12][0-9])|(3[01]))\/((0?[0-9])|(1[012]))\/([0-9]{4})$";
    if (mydate.match(regexp)) {
        return true;
    }
    return false;
}

function inputIsEmpty(inputObject) {

    if (inputObject != null) {
        var textValue = inputObject.value;
        if (textValue == "") {
            return true;
        }
    }
    return false;
}

function goSubmit(formName) {
    var formToSubmit = document.getElementById(formName);
    if (formToSubmit != null) {
        document.location = '#top';
        formToSubmit.submit();
    }
}

window.onload = cachetout;

function montre(id) {
    cachetout();
    var d = document.getElementById(id);
    if (d) {
        d.style.display = 'block';
    }
}

function cachetout() {
    for (var i = 1; i <= 10; i++) {
        if (document.getElementById('smenu' + i)) {
            document.getElementById('smenu' + i).style.display = 'none';
        }
    }
}

function changeElementCssName(elementName, newCssName) {
    if (elementName != null) {
        var elementObject = document.getElementById(elementName);
        if (elementObject != null) {
            elementObject.className = newCssName;
        }
    }
}

function changeElementCssNameByObject(elementObject, newCssName) {
    if (elementObject != null) {
        elementObject.className = newCssName;
    }
}

function setValue(elementName, elementValue) {
    if (elementValue != null && elementValue.length > 0) {
        var elementObject = document.getElementById(elementName);
        if (elementObject != null) {
            elementObject.value = elementValue;
        }
    }
}

function setValues(elementName, elementValue) {
    if (elementValue != null && elementValue.length > 0) {
        var elements = document.getElementsByName(elementName);
        
        for (i in elements) {
            if (elements[i] != null) {
                elements[i].value = elementValue;
            }
        }
    }
}

function goToInitialForm(url) {
    document.location.href = url;
}

function checkInt(value) {
	if (value=="") return false;
    var pattern = "0123456789";
    for (var i = 0; i < value.length; i++) {
        if (pattern.indexOf(value.charAt(i)) < 0) {
            return false;
        }
    }
    return true;
}

function checkDouble(value) {
	// You should do { value.replace(",","."); } before calling this method 
	if (value=="") return false;
    var pattern = "0123456789.";
    var c ; 
    for (var i = 0; i < value.length; i++) {
    	c = value.charAt(i);
        if (pattern.indexOf(c) < 0) {
            return false;
        }
    	if (c=='.') {
    		pattern = "0123456789"
    	}
    }
    return true;
}

function showRMRules() {
    alert("\nPour indiquer le format, vous devez entrer une expression régulière.\n\n" +
    "Entre crochets les valeurs possibles : [a-z] pour une minuscule ou [0-9] pour un chiffre.\n" +
    "Plusieurs intervalles sont possibles : [a-z0-9] pour une minuscule ou un chiffre.\n" +
    "Entre accolades leur nombre d'occurences : {2} pour 2 fois ou {3,6} pour de 3 à 6 fois.\n" +
    "Vous pouvez également indiquer un caractère fixe : B ou 9 par exemple.\n\n" +
    "[0-9]{7}B[0-9]{2,3} --> 7 chiffres, puis la lettre B, puis 2 ou 3 chiffres.\n" +
    "[a-z]{4.6}9[A-Z]{3,5} --> 4 à 6 minuscules, puis le chiffre 9, puis 3 à 5 majuscules.\n" +
    "[0-9]{3}[a-zA-Z]{3} --> 3 chiffres, puis 3 lettres (minuscules ou majuscules).\n\n" +
    "Vous pourrez tester directement le format en ajoutant un nouveau code à lister.\n");
}
