
    UrlAction = '/FRONT/ERD/include/webcoupure/ajax.webCoupure.php';
    var cpnumber = 0;

    /**
     * Fait les requetes ajax
     */
    function ajaxRequest(url, callbackString, paramsArray)
    {
        var xhr_object = null;
    	xhr_object = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");

    	xhr_object.open("GET", url, true);
    	xhr_object.onreadystatechange = function() {
    		if(xhr_object.readyState == 4)
    		{
    		    doCallBack(xhr_object, callbackString, paramsArray);
    		}
    	};
    	xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    	xhr_object.send(null);
    }


    function doCallBack(xhr_object, callbackString, paramsArray)
    {
        switch (callbackString)
        {
            case 'CALLBACK_VERIFLOGIN'                  : callback_verifLogin(xhr_object, paramsArray); break;
            case 'CALLBACK_VERIFREGISTER'               : callback_verifRegister(xhr_object, paramsArray); break;

            case 'CALLBACK_COMMUNES'                    : callback_communes(xhr_object, paramsArray); break;
            default                                     : break;
        }
    }



    /**
     * Verifications login
     */
    function verifLogin(login, mdp, lang)
    {
    	url = UrlAction + '?type=1&login=' + login + '&mdp=' + mdp + '&lang=' + lang;

    	var paramsArray = { };
    	ajaxRequest(url, 'CALLBACK_VERIFLOGIN', paramsArray);
    }

    function callback_verifLogin(xhr_object, paramsArray)
    {
       valRetour = xhr_object.responseText;

       if (document.getElementById('errorIdInvalide') != null)
            fromPopin = true;
       else
            fromPopin = false;

       if (fromPopin)
       {
           if (valRetour == 1)
           {
                document.getElementById('errorIdInvalide').style.display = 'block';
                document.getElementById('errorMdpInvalide').style.display = 'none';
           }
           if (valRetour == 2)
           {
                document.getElementById('errorIdInvalide').style.display = 'none';
                document.getElementById('errorMdpInvalide').style.display = 'block';
           }
           if (valRetour == 3)
           {
                document.getElementById('errorIdInvalide').style.display = 'block';
                document.getElementById('errorMdpInvalide').style.display = 'block';
           }
           if (valRetour == 0)
           {
                document.getElementById('errorIdInvalide').style.display = 'none';
                document.getElementById('errorMdpInvalide').style.display = 'none';
                document.getElementById('formLogin').submit();
           }
       }
       else
       {
           if (valRetour == 0)
           {
                document.getElementById('ref_wc_errorlogin').style.display = 'none';
                document.getElementById('formVosContacts').submit();
           }
           else
           {
               document.getElementById('ref_wc_errorlogin').style.display = 'block';
           }
       }
    }

    /**
     * Verifications register
     */
    function verifRegister(nom, prenom, email, fournisseur, lang, txtFermer)
    {
    	url = UrlAction + '?type=2&nom=' + nom + '&prenom=' + prenom + '&email=' + email + '&fournisseur=' + fournisseur + '&lang=' + lang;

    	var paramsArray = { 'lang' : lang, 'txtFermer' : txtFermer};
    	ajaxRequest(url, 'CALLBACK_VERIFREGISTER', paramsArray);
    }

    function callback_verifRegister(xhr_object, paramsArray)
    {
       valRetour = xhr_object.responseText;
       valRetour = valRetour.split(',');

       document.getElementById('errorRegisterNom').style.display = 'none';
       document.getElementById('errorRegisterPrenom').style.display = 'none';
       document.getElementById('errorRegisterEmail').style.display = 'none';
       document.getElementById('errorRegisterEmail2').style.display = 'none';
       document.getElementById('errorRegisterFournisseur').style.display = 'none';

       noError = 1;
       for (i = 0; i < valRetour.length; i++)
       {
           if (i == 0 && valRetour[i] == '1')
           {
                document.getElementById('errorRegisterNom').style.display = 'block';
                noError = 0;
           }
           if (i == 1 && valRetour[i] == '1')
           {
                document.getElementById('errorRegisterPrenom').style.display = 'block';
                noError = 0;
           }
           if (i == 2 && valRetour[i] == '1')
           {
                document.getElementById('errorRegisterEmail').style.display = 'block';
                noError = 0;
           }
           if (i == 2 && valRetour[i] == '2')
           {
                document.getElementById('errorRegisterEmail2').style.display = 'block';
                noError = 0;
           }
           if (i == 3 && valRetour[i] == '1')
           {
                document.getElementById('errorRegisterFournisseur').style.display = 'block';
                noError = 0;
           }

       }

       if (noError == 1)
       {
           document.getElementById('errorRegisterFournisseur').style.display = 'none';
           loadPopin('/FRONT/ERD/template/dynamique/popin-validation.php?lang=' + paramsArray['lang'], 'format2', paramsArray['txtFermer'], '/FRONT/ERD/template/dynamique/popin-validation.php?lang=fr');
       }
       else
       {
           document.getElementById('errorGeneral').style.display = 'block';
       }
    }


    /**
     * Communes
     */
    function verifCommunes()
    {
        document.getElementById('labelCp').style.color = '#616161';
        document.getElementById('labelCommune').style.color = '#616161';
        document.getElementById('labelCpElectrique').style.color = '#616161';
        document.getElementById('wcspanError').style.display = 'none';
        document.getElementById('wcspanError2').style.display = 'none';
        document.getElementById('wcspanError3').style.display = 'none';
        document.getElementById('wcspanError4').style.display = 'none';

        chooseErrorToShow = 0;
        if (!document.getElementById('radio1').checked && !document.getElementById('radio2').checked)
        {
            document.getElementById('labelCp').style.color = 'red';
            document.getElementById('labelCpElectrique').style.color = 'red';
            res = false;
        }
        else
        {
            if (document.getElementById('radio1').checked)
            {
                if (document.getElementById('cp').value != '' && document.getElementById('commune').value != ''  && document.getElementById('commune').style.display != 'none')
                    res = true;
                else
                {
                    chooseErrorToShow = 1;
                    if (document.getElementById('cp').value == '')
                        document.getElementById('labelCp').style.color = 'red';
                    else
                    {
                        document.getElementById('labelCommune').style.color = 'red';
                        chooseErrorToShow = 3;
                    }
                    res = false;
                }
            }
            else
                if (document.getElementById('radio2').checked && document.getElementById('cpElectrique').value != '' && document.getElementById('cpElectrique').value.length == 10)
                    res = true;
                else
                {
                    res = false;
                    document.getElementById('labelCpElectrique').style.color = 'red';
                    if (document.getElementById('cpElectrique').value.length != 10 && document.getElementById('cpElectrique').value != '')
                        chooseErrorToShow = 4;
                    else
                        chooseErrorToShow = 2;
                }
        }

        if (res == false)
        {
            if (chooseErrorToShow == 1)
                document.getElementById('wcspanError').style.display = 'block';
            if (chooseErrorToShow == 2)
                document.getElementById('wcspanError2').style.display = 'block';
            if (chooseErrorToShow == 3)
                document.getElementById('wcspanError3').style.display = 'block';
            if (chooseErrorToShow == 4)
                document.getElementById('wcspanError4').style.display = 'block';
        }
        else
        {
            document.getElementById('wcspanError').style.display = 'none';
            document.getElementById('wcspanError2').style.display = 'none';
            document.getElementById('wcspanError3').style.display = 'none';
            document.getElementById('wcspanError4').style.display = 'none';
        }
        return res;
    }

    function hideCommunes()
    {
       document.getElementById('cp').value = '';
       document.getElementById('labelCp').style.color = '#616161';
       document.getElementById('labelCommune').style.color = '#616161';
       document.getElementById('commune').style.display = 'none';
       document.getElementById('labelCommune').style.display = 'none';
    }

    function communes(cp)
    {
    	url = UrlAction + '?type=3&cp=' + cp;

    	var paramsArray = { };
    	ajaxRequest(url, 'CALLBACK_COMMUNES', paramsArray);
    }

    function callback_communes(xhr_object, paramsArray)
    {
       valRetour = xhr_object.responseText;

       document.getElementById('idlistCommunes').innerHTML = valRetour;
       document.getElementById('commune').style.display = 'inline';
       document.getElementById('labelCommune').style.display = 'inline';
    }

    function updateFieldCommunes(str)
    {
        if (str.length != 5)
        {
            document.getElementById('labelCommune').style.color = '#616161';
            document.getElementById('commune').style.display = 'none';
            document.getElementById('labelCommune').style.display = 'none';
        }
        else
        {
            communes(str);
            document.getElementById('commune').style.display = 'inline';
            document.getElementById('labelCommune').style.display = 'inline';
        }
    }

    function colorChangeInput(zone)
    {
        if (zone == 1)
        {
            document.getElementById('radio1').checked = true;
            document.getElementById('cp').style.backgroundColor = 'white';
            document.getElementById('cpElectrique').style.backgroundColor = '#DEDCE4';
        }
        if (zone == 2)
        {
            document.getElementById('radio2').checked = true;
            document.getElementById('cpElectrique').style.backgroundColor = 'white';
            document.getElementById('cp').style.backgroundColor = '#DEDCE4';
        }
    }