﻿function ajaxVille(Txt1, Txt2, 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="+Txt1.value+"-"+Txt2.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=Ville.offsetLeft+"px";
                Liste.style.top=Ville.offsetTop+Ville.offsetHeight+"px";
                Liste.style.width=Ville.offsetWidth-2*1+"px";
                Liste.style.height=i*15+"px";
                Liste.style.display='';
                }
            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);}    
}

// ProcAsync Début
var procAsync = new ProcAsync();
//Constructeur Protocole
function ProcAsync()
{
  this.timer = null;
  this.div = null;
  this.url = null;
}

function initProcAsync(Div,Url,Interval)
{
    procAsync.div = Div;
    procAsync.url = Url;
    procAsync.timer = setInterval("ajaxProcAsync()", Interval);
}

function ajaxProcAsync()
{
    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", procAsync.url+"?suivi="+suiviNumero(), true);
    xhr.onreadystatechange = function ()
        {
        if (xhr.readyState == 4)
            {
            if (xhr.responseText.substring(0,5) != '<END>' && xhr.responseText !='')
                {
                procAsync.div.innerHTML = xhr.responseText;
                }
            else if (xhr.responseText != '')
                {
                clearInterval(procAsync.timer);
                procAsync.div.innerHTML = xhr.responseText.substring(5,xhr.responseText.length-1);
                }
            }
        }
    xhr.send(null);
 }

function suiviNumero()
{
    var d = new Date();
    var n = '' + d.getFullYear();
    if (d.getMonth()+1<10) {n = n + '0' + (d.getMonth()+1);} else {n = n + (d.getMonth+1);}
    if (d.getDate()<10) {n = n + '0' + d.getDate();} else {n = n + d.getDate();}
    if (d.getHours()<10) {n = n + '0' + d.getHours();} else {n = n + d.getHours();}
    if (d.getMinutes()<10) {n = n + '0' + d.getMinutes();} else {n = n + d.getMinutes();}
    if (d.getSeconds()<10) {n = n + '0' + d.getSeconds();} else {n = n + d.getSeconds();}
    return n;
}
// ProcAsync Fin


