﻿function ajaxVille(Txt, Ville, Liste)
{
    var xhr = null;
    if (window.XMLHttpRequest)
        {xhr = new XMLHttpRequest();}
    else if (window.ActiveXObject)
        {try 
            {xhr = new ActiveXObject("Msxml2.XMLHTTP");}
         catch(e)
            {xhr = new ActiveXObject("Microsoft.XMLHTTP");}   
         }
    else 
        {return;} 
    xhr.open("GET", "Ajax.aspx?ville="+Txt.value, true);
    xhr.onreadystatechange = function ()
        {
        if (xhr.readyState == 4)
            {
            var reponse = xhr.responseText;
            if (reponse!='')
                { 
                var reg = new RegExp(";", "g");
                var tmp = reponse.split(reg);
                //Construction de la table
                var chaine="";
                for (var i = 0; i<tmp.length; i++)
                  {
                  chaine+="<div style='cursor: pointer; padding-left: 2px' onmouseover='this.style.fontWeight=\"bold\"; this.style.backgroundColor=\"#eeeeee\"' onmouseout='this.style.fontWeight=\"normal\"; this.style.backgroundColor=\"#ffffff\"' onmousedown='document.getElementById(\""+Ville.id+"\").value=\""+tmp[i]+"\";'>"+tmp[i]+"</div>";
                  }  
                Liste.innerHTML=chaine;
                Liste.style.left='0px';//Ville.offsetLeft+"px";
                Liste.style.top=Ville.offsetHeight+'px';
                Liste.style.width=Ville.offsetWidth-2*1+'px';
                Liste.style.height=i*15+"px";
                Liste.style.display='';
                Liste.style.backgroundColor='#ffffff';
                Liste.style.zIndex=999;
                }
            else 
                {
                Liste.style.display='none';
                }
            }
        }    
    xhr.send(null);
}

function ajaxProfession(Txt, Cbx, Img, Ibn)
{
    var xhr = null;
    if (window.XMLHttpRequest)
        {xhr = new XMLHttpRequest();}
    else if (window.ActiveXObject)
        {try 
            {xhr = new ActiveXObject("Msxml2.XMLHTTP");}
         catch(e)
            {xhr = new ActiveXObject("Microsoft.XMLHTTP");}   
         }
    else 
        {return;} 
    if (Cbx.getAttribute("requete") == true) 
        {return;}
    Cbx.setAttribute("requete",true);    
    xhr.open("GET", "Ajax.aspx?profession="+Txt.value, true);
    xhr.onreadystatechange = function ()
        {
        if (xhr.readyState == 4)
            {
            var reponse = xhr.responseText;
            var reg = new RegExp(";", "g");
            var tmp = reponse.split(reg);
            //On remplace le Textbox par le Combobox
            Txt.style.display="none";
            Ibn.style.display="none";
            Cbx.style.display="";
            Cbx.focus();
            for (var i = 0; i<tmp.length; i++)
                {Cbx.options[i]=new Option( tmp[i], i);} 
            Cbx.options[Cbx.options.length-2].style.fontWeight='bold';
            Cbx.options[Cbx.options.length-1].style.fontWeight='bold';
            Cbx.options[Cbx.options.length-1].style.color='#9bcc01';
            Txt.value = Cbx.options[Cbx.selectedIndex].text;
            Img.src="Images/Validation/SaisieOk.gif";
            Cbx.removeAttribute("requete");
            }
        }
    xhr.send(null);
}

function ChangeProfession(Txt, Cbx, Img, Ibn, Obligatoire)
{
    if (Cbx.selectedIndex == Cbx.options.length-1)
        {
        Txt.style.display = "";
        Txt.value = "";
        Txt.focus();
        Ibn.style.display="";
        //Suppression des elements du combo
        for (var i = Cbx.options.length-1; i>=0; i--)
            {Cbx.options[i]=null;}        
        Cbx.style.display = "none"; 
        if (Obligatoire == true)
            {Img.src="Images/Validation/SaisieObligatoire.gif";}
        else
            {Img.src="Images/Validation/SaisieVide.gif";}
        }
     else
        {
        Txt.value = Cbx.options[Cbx.selectedIndex].text;
        }
}

function LanceAjaxProfession(Txt, Cbx, Img, Ibn)
{
    if (Txt.value == "")
        {ajaxProfession(Txt, Cbx, Img, Ibn);}
}

function InitAjaxProfession(Txt, Cbx, Img, Ibn)
{
    if (Txt.value != "")
        {ajaxProfession(Txt, Cbx, Img, Ibn);}    
}

function ajaxProposition(Wait, Prop, Refe)
{
    var xhr = null;
    if (window.XMLHttpRequest)
        {xhr = new XMLHttpRequest();}
    else if (window.ActiveXObject)
        {try 
            {xhr = new ActiveXObject("Msxml2.XMLHTTP");}
         catch(e)
            {xhr = new ActiveXObject("Microsoft.XMLHTTP");}   
         }
    else 
        {return;} 
    xhr.open("GET", "Ajax.aspx?proposition=go", true);
    xhr.onreadystatechange = function ()
        {
        if (xhr.readyState == 4)
            {
            var reponse = xhr.responseText;
            Wait.style.display='none';
            Prop.style.display='';
            Refe.innerHTML=reponse;
            }
        }
    xhr.send(null);
}

function ajaxTopMessage(Page,Top,Width)
{
    var xhr = null;
    if (window.XMLHttpRequest)
        {xhr = new XMLHttpRequest();}
    else if (window.ActiveXObject)
        {try 
            {xhr = new ActiveXObject("Msxml2.XMLHTTP");}
         catch(e)
            {xhr = new ActiveXObject("Microsoft.XMLHTTP");}   
         }
    else 
        {return;} 
    xhr.open("GET", Page, true);
    xhr.onreadystatechange = function ()
        {
        if (xhr.readyState == 4)
            {
            var reponse = xhr.responseText;
            topMessage(reponse, Top, Width);
            }
        }
    xhr.send(null);
}

function ajaxCalculPalier(EtapeId)
{
    var lance = true;
    var Drp = $(EtapeId+'_DrpNbPaliers');
    for (var i = 1; i<Drp.options[Drp.selectedIndex].value; i++)
    {
        if (RegTest($(EtapeId+'_MontantPalier'+i+'_TxtNombre').value,'^\\s*$')) lance = false;
        if (RegTest($(EtapeId+'_DureePalier'+i+'_TxtNombre').value,'^\\s*$')) lance = false;
    }
    if (RegTest($(EtapeId+'_Taux_TxtNombre').value,'^\\s*$')) lance = false;
    if (RegTest($(EtapeId+'_Montant_TxtNombre').value,'^\\s*$')) lance = false;
    if (RegTest($(EtapeId+'_Duree_TxtNombre').value,'^\\s*$')) lance = false;
    if ($(EtapeId+'_Periodicite_DrpListe').selectedIndex==0) lance = false;
    if (!lance) return;
    var xhr = null;
    if (window.XMLHttpRequest)
        {xhr = new XMLHttpRequest();}
    else if (window.ActiveXObject)
        {try 
            {xhr = new ActiveXObject("Msxml2.XMLHTTP");}
         catch(e)
            {xhr = new ActiveXObject("Microsoft.XMLHTTP");}   
         }
    else 
        {return;} 
    var chaine = '';
    chaine += '&duree='+$(EtapeId+'_Duree_TxtNombre').value;
    chaine += '&montant='+$(EtapeId+'_Montant_TxtNombre').value;
    chaine += '&taux='+$(EtapeId+'_Taux_TxtNombre').value;
    chaine += '&periodicite='+$(EtapeId+'_Periodicite_DrpListe').selectedIndex;    
    for (var i = 1; i<Drp.options[Drp.selectedIndex].value; i++)
    {
        chaine += '&paliermontant'+i+'='+$(EtapeId+'_MontantPalier'+i+'_TxtNombre').value;
        chaine += '&palierduree'+i+'='+$(EtapeId+'_DureePalier'+i+'_TxtNombre').value;
    }
    xhr.open("GET", "Ajax.aspx?palier=calcul"+chaine, true);
    xhr.onreadystatechange = function ()
        {
        if (xhr.readyState == 4)
            {
            var reponse = xhr.responseText;
            var tmp = reponse.split(new RegExp(";", "g"));
            $(EtapeId+'_MontantPalier'+Drp.options[Drp.selectedIndex].value+'_TxtNombre').value=tmp[0];
            $(EtapeId+'_DureePalier'+Drp.options[Drp.selectedIndex].value+'_TxtNombre').value=tmp[1];
            $(EtapeId+'_MontantPalier'+Drp.options[Drp.selectedIndex].value+'_ImgValidation').src='Images/Validation/SaisieOk.gif';
            $(EtapeId+'_DureePalier'+Drp.options[Drp.selectedIndex].value+'_ImgValidation').src='Images/Validation/SaisieOk.gif';
            }
        }
    xhr.send(null);
}

