
/* enter and tab patch */

var tIndex = new Array();

function enterAndTabPatch() {
    var MonForm = document.getElementsByTagName("form")[0];
    var NbElements = MonForm.elements.length;
    var formElem = new Array();
    var haveTabIndex = false;
    for (var r = 0; r < NbElements && MonForm.elements[r]; r++)
    {
        var Champ = MonForm.elements[r];
        if (Champ && Champ.type != "hidden")
        {
            var Nom_Element = Champ.id;
            if (Nom_Element && Nom_Element != "" && Nom_Element.indexOf("_MainContent_") != -1)
            {
                formElem[formElem.length] = Champ;
                if (Champ.getAttribute('tabindex') != 0)
                {
                    haveTabIndex = true;
                }
            }
        }
    }
    if (haveTabIndex == false)
    {
        for (var r = 0; r < formElem.length && formElem[r]; r++)
        {
            formElem[r].setAttribute('tabIndex', String(r+1));
        }
    }


    for (var i=0;i < formElem.length;i++) {
        var elem = formElem[i];
        var tId = parseInt(elem.getAttribute('tabIndex'));
        if (tId) tIndex[tId] = elem;
        
        elem.onkeydown = function(e) {
            e = e || window.event;
            var key = e.keyCode;
            if (key == 13 || key == 9) {
                var elem = e.srcElement;
                var submitLink = elem.getAttribute("submitLink");
                if (submitLink && submitLink.length != 0 && key == 13)
                {
                    var sbmt = document.getElementById(submitLink);
                    window.location = sbmt.getAttribute("href");
                }
                else
                {
                    var tId = parseInt(elem.getAttribute('tabIndex'))+1;
                    if (tId == 0)
                    {
                        if (tIndex[1]) tId = 1;
                        else return true;
                    }
                    var focusIsSet = false;
                    if (!tIndex[tId]) tId = 1;
                    while (tIndex[tId])
                    {
                        var nextelem = tIndex[tId];
                        
                        if (nextelem.getAttribute('disabled')) 
                        {
                            tId++;
                            if (!tIndex[tId]) tId = 1;
                        }
                        else {
                            try {
                                nextelem.focus();
                                focusIsSet = true;
                                break;
                            }
                            catch (e)
                            {
                                tId++;
                                if (!tIndex[tId]) tId = 1;
                            }
                        }
                    }
                    if (focusIsSet) return false;
                }
            }
        };
    }
};


/* asp patch */

var setAvr = false;
var globalValidatorGroups = null;

function myvalidator()
{   
    if (typeof(Page_Validators) == "undefined")
        return;
    var i, val;
    for (i = 0; i < Page_Validators.length; i++) {
    
        val = Page_Validators[i];
        ValidatorValidate(val, val.validationGroup, null);
    }
    ValidatorOnLoad();
    ValidatorUpdateIsValid();
}

var globalTimer = null;

function verifPostAsp()
{
    clientTimer = function(val)
    {
        if (globalTimer) clearTimeout(globalTimer);
        verifPostAsp(1);
        if (val)
            val++;
        else
        var val = 1;
        if (val < 30)
            globalTimer = setTimeout("clientTimer("+val+")",100);
    }
    if (typeof ValidatorOnLoad != "undefined" && setAvr == false)
    {
        clientTimer = function() { if (globalTimer) clearTimeout(globalTimer); }
        setAvr = true;
        var erdv = document.getElementById("errorDiv");
        for (var i = 0; Page_Validators[i]; i++)
        {
            var val = Page_Validators[i];
            if (val.style.visibility == "hidden")
            {
                val.style.visibility = "";
                val.style["display"] = "none";
                if (erdv && val.parentNode.name == "errorDiv") erdv.appendChild(val);
            }
        }
        
        
        
        // patch begin here 
        //Surcharge de l'événement "une date est sélectionnée" du composant calendrier
        //permet de lancer la validation du formulaire lorsqu'une date est sélectionnée
        //et ainsi faire disparaître le message d'avertissement correspondant
        if (typeof CalendarPopup_Up_SelectDate != "undefined")
        {
            var _old_CalendarPopup_Up_SelectDate = CalendarPopup_Up_SelectDate;
            CalendarPopup_Up_SelectDate = function(tbName, lblName, divName, myName, theDate, formatNum, enableHide, pad, postbackFunc, customFunc, visibleKey) {
                _old_CalendarPopup_Up_SelectDate(tbName, lblName, divName, myName, theDate, formatNum, enableHide, pad, postbackFunc, customFunc, visibleKey);
                myvalidator();
            }
        }
        ValidatorValidate = function(val, validationGroup, event) {
            globalValidatorGroups = validationGroup;
            val.isvalid = true;
            if ((typeof(val.enabled) == "undefined" || val.enabled != false) && IsValidationGroupMatch(val, validationGroup)) {
                if (typeof(val.evaluationfunction) == "function") {
                    if (val.evaluationfunction == CustomValidatorEvaluateIsValid)
                    {
                        val.evaluationfunction = function(val) {
                            var value = "";
                            var args = { Value:value, isvalid:true };
                            if (typeof(val.clientvalidationfunction) == "string") {
                                eval(val.clientvalidationfunction + "(val, args) ;");
                            }
                            if (typeof(val.controltovalidate) == "string") {
                                value = ValidatorGetValue(val.controltovalidate);
                                if ((ValidatorTrim(value).length == 0) && val.validateemptytext == "true") {
                                    args.isvalid = false;
                                }
                            }
                            return args.isvalid;
                        }
                    }
                    val.isvalid = val.evaluationfunction(val);
                    if (!val.isvalid && Page_InvalidControlToBeFocused == null &&
                        typeof(val.focusOnError) == "string" && val.focusOnError == "t") {
                        ValidatorSetFocus(val, event);
                    }
                }
            }
            ValidatorUpdateDisplay(val);
        }
        ValidatorSelect = function(source, args, valid, select, empty)
        {
            if (typeof source.validateemptytextisset == 'undefined' && source.validateemptytext)
                source.validateemptytextisset = source.validateemptytext;
            var arrValid = valid.split(",");
            var TB_Valid = document.getElementById(source.controltovalidate);
            var selectOpt = select.options[select.selectedIndex].value;
            
            if (source.validateemptytextisset) source.validateemptytext = "false";
            args.isvalid = true;
            //TB_Valid.disabled = true;
            if (!TB_Valid.oldClass && TB_Valid.className)
                TB_Valid.oldClass = TB_Valid.className.replace(" disableInput","");
            else if (!TB_Valid.oldClass)       
                TB_Valid.oldClass = "";
            TB_Valid.className = TB_Valid.oldClass+" disableInput";
            var isFalse = false;
            if (select.disabled == false)
            {
                for (var i = 0; i < arrValid.length; i++)
                {
                    if (String(selectOpt) == String(arrValid[i]))
                    {
                        if (source.validateemptytextisset) source.validateemptytext = "true";
                        TB_Valid.className = TB_Valid.oldClass;
                        isFalse = true;
                    }
                }
            }
            if (isFalse == false)
            {
                /*if (TB_Valid.tagName == 'INPUT' && !empty) TB_Valid.value = "";*/
                TB_Valid.disabled = true;
            }
            else 
            {
                TB_Valid.disabled = false;
            }
        }
        
        AllValidatorsValid = function(validators) {
            if ((typeof(validators) != "undefined") && (validators != null)) {
                var i;
                for (i = 0; i < validators.length; i++) {
                    if (!validators[i].isvalid) {
                        return false;
                    }
                }
            }
            return true;            
        }

           
        ValidatorOnLoad = function() {
            if (typeof(Page_Validators) == "undefined")
                return;
            var i, val;
            var erdv = document.getElementById("errorDiv");
            if (erdv.isvisible == true) erdv.style["display"] = "none";
            for (i = 0; i < Page_Validators.length; i++) {
            
                val = Page_Validators[i];
                if (typeof(val.evaluationfunction) == "string") {
                    eval("val.evaluationfunction = " + val.evaluationfunction + ";");
                }
                val.isvalid = eval(val.isvalid);
                if (val.isvalid == false)
                {
                    Page_IsValid = false;
                    if (val.parentNode.id == "errorDiv")
                    {
                        val.style["display"] = "block";
                    }
                    else
                        val.style["display"] = "";
                    if (globalValidatorGroups && erdv.isvisible == true) 
                    {
                        erdv.style["display"] = "inline-block";
                    }
                }
                else
                {
                    val.style["display"] = "none";
                }
                    
                if (typeof(val.enabled) == "string") {
                    val.enabled = (val.enabled != "False");
                }
                if (typeof(val.controltovalidate) == "string") {
                    ValidatorHookupControlID(val.controltovalidate, val);
                }
                if (typeof(val.controlhookup) == "string") {
                    ValidatorHookupControlID(val.controlhookup, val);
                }
            }
            
            Page_ValidationActive = true;
        }
        ValidatorUpdateIsValid = function() {
            var erdv = document.getElementById("errorDiv");
            Page_IsValid = AllValidatorsValid(Page_Validators);
            if (!globalValidatorGroups && document.getElementById("ctl00_CPH_MainContent_valSum") && document.getElementById("ctl00_CPH_MainContent_valSum").validationGroup)
            {
                globalValidatorGroups = document.getElementById("ctl00_CPH_MainContent_valSum").validationGroup;
            }
            
            if (globalValidatorGroups && document.getElementById("ctl00_CPH_MainContent_valSum"))
            {
                if (erdv.isvisible == true) erdv.style["display"] = "none";
                ValidationSummaryOnSubmit(globalValidatorGroups);
            }
            if (!Page_IsValid) {
                if (erdv && globalValidatorGroups && erdv.isvisible == true) erdv.style["display"] = "inline-block";
            }
            else {
                if (erdv && erdv.isvisible == true) erdv.style["display"] = "none";
                for (var i = 0; Page_Validators[i]; i++)
                {
                    var val = Page_Validators[i];
                    val.style["display"] = "none";
                }
            }
        }
        
        ValidationSummaryOnSubmit = function(validationGroup) {
            if (typeof(Page_ValidationSummaries) == "undefined")
                return;
            var summary, sums, s;
            for (sums = 0; sums < Page_ValidationSummaries.length; sums++) {
                summary = Page_ValidationSummaries[sums];
                summary.style["display"] = "none";
                if (!Page_IsValid && IsValidationGroupMatch(summary, validationGroup)) {
                    var i;
                    if (summary.showsummary != "False") {
                        summary.style["display"] = "";
                        if (typeof(summary.displaymode) != "string") {
                            summary.displaymode = "BulletList";
                        }
                        switch (summary.displaymode) {
                            case "List":
                                headerSep = "<br>";
                                first = "";
                                pre = "";
                                post = "<br>";
                                end = "";
                                break;
                            case "BulletList":
                            default:
                                headerSep = "";
                                first = "<ul>";
                                pre = "<li>";
                                post = "</li>";
                                end = "</ul>";
                                break;
                            case "SingleParagraph":
                                headerSep = " ";
                                first = "";
                                pre = "";
                                post = " ";
                                end = "<br>";
                                break;
                        }
                        s = "";
                        if (typeof(summary.headertext) == "string") {
                            s += summary.headertext + headerSep;
                        }
                        s += first;
                        for (i=0; i<Page_Validators.length; i++) {
                            if (!Page_Validators[i].isvalid && typeof(Page_Validators[i].errormessage) == "string") {
                                s += pre + Page_Validators[i].errormessage + post;
                            }
                        }
                        s += end;
                        /*var elem = document.createElement("div");
                        while (summary.childNodes.length)
                            elem.appendChild(summary.childNodes[0]);
                        elem.innerHTML = s;
                        summary.appendChild(elem);*/
                        summary.innerHTML = s;
                    }
                    if (summary.showmessagebox == "True") {
                        s = "";
                        if (typeof(summary.headertext) == "string") {
                            s += summary.headertext + "\r\n";
                        }
                        var lastValIndex = Page_Validators.length - 1;
                        for (i=0; i<=lastValIndex; i++) {
                            if (!Page_Validators[i].isvalid && typeof(Page_Validators[i].errormessage) == "string") {
                                switch (summary.displaymode) {
                                    case "List":
                                        s += Page_Validators[i].errormessage;
                                        if (i < lastValIndex) {
                                            s += "\r\n";
                                        }
                                        break;
                                    case "BulletList":
                                    default:
                                        s += "- " + Page_Validators[i].errormessage;
                                        if (i < lastValIndex) {
                                            s += "\r\n";
                                        }
                                        break;
                                    case "SingleParagraph":
                                        s += Page_Validators[i].errormessage + " ";
                                        break;
                                }
                            }
                        }
                        alert(s);
                    }
                }
            }
        }        
        
        ValidatorUpdateDisplay = function(val) {
            
            val.style.visibility = "";
            if (val.parentNode.id != "errorDiv")
                val.style["display"] = val.isvalid ? "none" : "";
            else
            {
                val.style["display"] = val.isvalid ? "none" : "block";
                if (!val.isvalid && globalValidatorGroups)
                {
                    var erdv = document.getElementById("errorDiv");
                    //alert(erdv.isvisible+" "+erdv.style.display);
                    if (erdv.isvisible == true)
                    {
                        erdv.style["display"] = "inline-block";
                        if (erdv.style["display"] == "")
                        {
                            erdv.style["display"] = "block";
                        }
                    }
                }
            }
        }
        myvalidator();
    }
    
    if (typeof WebForm_DoPostBackWithOptions != "undefined" && typeof _WebForm_DoPostBackWithOptions == "undefined")
    {
        var _WebForm_DoPostBackWithOptions = WebForm_DoPostBackWithOptions;
        WebForm_DoPostBackWithOptions = function(val) {
            var erdv = document.getElementById("errorDiv");
            erdv.isvisible = true;
            _WebForm_DoPostBackWithOptions(val);
        }
    }
}
