var elementfocus = -1;

function changeentextfield(id){

  idhidden = "hidden"+id;
  if(document.getElementById(idhidden)){
    inputName = document.getElementById(idhidden).name;
    inputValue = document.getElementById(idhidden).value;
    taille = inputValue.length;
    idinput = "text"+id;
    if(taille == 0){
      taille = 1;
    }
    document.getElementById(id).innerHTML="<INPUT id='"+idinput
    +"' name='"+inputName
    +"' size=\""+taille+"\" onkeydown=\"changefocus('"+id+"', event); elargie('"+idinput+"', event);\" type='text'  value='"+inputValue+"'/>";

  }
  Nom_Browser = navigator.appName;
    if ( Nom_Browser == "Microsoft Internet Explorer" ){
    document.getElementById("text"+id).select();
    	document.getElementById("text"+id).focus();
    }
  if(elementfocus != -1 && id != elementfocus){
      remet(elementfocus);
   }
  elementfocus = id;
}

function elargie(idinput, e){
  if(document.getElementById(idinput)){
    if(document.getElementById(idinput).value.length != 0){
      document.getElementById(idinput).size = document.getElementById(idinput).value.length;
    }else{
      document.getElementById(idinput).size = 1;
    }
  }
}



function remet(id){
  idinput = "text"+id;
  if(document.getElementById(idinput)){
    elementfocus = -1;
    valeur = document.getElementById(idinput).value ;
    name = document.getElementById(idinput).name;
    document.getElementById(id).innerHTML = valeur + "<input id=\"hidden"+id+"\" type=\"hidden\" name=\""+name+"\" value=\""+valeur+"\"></input>";
    document.getElementById(id).style.background="#FFFFFF";
  }
}


function remet(){
  idinput = "text"+elementfocus;
  if(document.getElementById(idinput)){

    valeur = document.getElementById(idinput).value ;
    name = document.getElementById(idinput).name;
    document.getElementById(elementfocus).innerHTML = valeur + "<input id=\"hidden"+elementfocus+"\" type=\"hidden\" name=\""+name+"\" value=\""+valeur+"\"></input>";
    document.getElementById(elementfocus).style.background="#FFFFFF";
  }
  elementfocus = -1;
}

function setfocus(id){
  Nom_Browser = navigator.appName;
  if(Nom_Browser != "Microsoft Internet Explorer"){
    document.getElementById("text"+id).select();
  }
  document.getElementById("text"+id).focus();
}


function setfocusseul(id){
  if(document.getElementById("text"+id)){
    document.getElementById("text"+id).focus();
  }else if(document.getElementById("autre"+id)){
    document.getElementById("autre"+id).focus();
  }
}

function putColor(id){
  document.getElementById(id).style.background="#DDDDDD";
}

function delcolor(id){
  document.getElementById(id).style.background="#FFFFFF";
}

function changefocus(id, e){
  key = getkey(e);
  Nom_Browser = navigator.appName;

  if(Nom_Browser == "Microsoft Internet Explorer"){
    if(key == 9){
      idplus = parseInt(id)+1;
      idhidden = "hidden" + idplus
      if(document.getElementById(idhidden)){
        changeentextfield(parseInt(id)+1);
        setfocus(parseInt(id)+1);
      }else{
        remet(id);
        if(document.getElementById(parseInt(id)+1)){
          setfocusseul(parseInt(id)+1);
        }
      }
    }
  }else{
    if(key == 9){
      if(document.getElementById(parseInt(id)+1)){
        changeentextfield(parseInt(id)+1);
      }else{
        remet(id);
      }
    }
  }
}


function resetElementfocus(){
if(elementfocus != -1){
  remet(elementfocus);
  elementfocus = -1;
}
}


function getkey(e){
  if (window.event)
    return window.event.keyCode;
  else if (e)
    return e.which;
  else
    return null;
}

