function checkAjax(sessionID) {
  var ajaxObj=null;
  if(window.XMLHttpRequest) {
    ajaxObj=new XMLHttpRequest();
    ajaxObj.onreadystatechange=function() {}
    ajaxObj.open("GET","/ajax.php?check&SID="+sessionID,true);
    ajaxObj.send(null);}
  else if(window.ActiveXObject) {
    try {ajaxObj=new ActiveXObject("Msxml2.XMLHTTP");}
    catch (e) {ajaxObj=new ActiveXObject("Microsoft.XMLHTTP");}
    ajaxObj.onreadystatechange=function() {}
    ajaxObj.open("GET","/ajax.php?check="+key+"&SID="+sessionID,true);
    ajaxObj.send(null);}}

function initAjax() {
  var ajaxObj=null;
  if(window.XMLHttpRequest) ajaxObj=new XMLHttpRequest();
  else if(window.ActiveXObject) {
    try {ajaxObj=new ActiveXObject("Msxml2.XMLHTTP");}
    catch (e) {ajaxObj=new ActiveXObject("Microsoft.XMLHTTP");}}
  else ajaxObj=false;
  return(ajaxObj);}

function importFile(sessionID) {
  var xhr=initAjax();
  // On défini ce qu'on va faire quand on aura la réponse
  xhr.onreadystatechange=function() {importLoop(sessionID,xhr);}
  xhr.open("GET","/import.php?SID="+sessionID,true);
  xhr.send(null);}

function ajaxChange(sentValue,returnField,waitID,sessionID) {
  document.getElementById(waitID).style.display='inline-block';
  var xhr=initAjax();
  xhr.onreadystatechange= function() { ajaxChangeDo(xhr,returnField,waitID); }
  xhr.open("GET","/ajax.php?"+sentValue+"&SID="+sessionID,true);
  xhr.send(null);}
function ajaxChangeDo(xhr,returnField,waitID){
  if (xhr.readyState==4 && xhr.status==200){
    var docXML= xhr.responseXML;
    var done=docXML.getElementsByTagName("done").item(0).firstChild.data;
    document.getElementById(waitID).style.display='none';
    document.getElementById(returnField).innerHTML=done;}}
    
function ajaxMailTest(sessionID) {
  document.getElementById('ajaxWait1').style.display='inline-block';
  var xhr=initAjax();
  xhr.onreadystatechange= function() { ajaxChangeDo(xhr,'done','ajaxWait1'); }
  xhr.open("GET","/user.php?testMail&ajax&SID="+sessionID,true);
  xhr.send(null);}
  
function ajaxRedir(sessionID,linkId) {
  var xhr=initAjax();
  xhr.open("GET","/redir.php?link="+linkId+"&SID="+sessionID,true);
  xhr.send(null);}

function ajaxLinkCheck(sessionID,urlToCheck) {
  document.getElementById('ajaxWait0').style.display='inline-block';
  if (urlToCheck!='') {
    var xhr=initAjax();
    xhr.onreadystatechange= function() { ajaxLinkCheckDo(xhr); }
    xhr.open("GET","/ajax.php?testLink="+urlToCheck+"&SID="+sessionID,true);
    xhr.send(null);}
  else {
    document.getElementById('ajaxWait0').style.display='none';}}
function ajaxLinkCheckDo(xhr) {
  if (xhr.readyState==4 && xhr.status==200){
  var docXML= xhr.responseXML;
  var done=docXML.getElementsByTagName("done").item(0).firstChild.data;
  var result=docXML.getElementsByTagName("result").item(0).firstChild.data;
  document.getElementById('ajaxWait0').style.display='none';
  document.getElementById('done').innerHTML=done;
  if (result==1) {
    var urlTitle=docXML.getElementsByTagName("title").item(0).firstChild.data;
    var icon=docXML.getElementsByTagName("icon").item(0).firstChild.data;
    document.getElementById('linkName').value=urlTitle;
    document.getElementById('currentIcon').src=icon;}}}

function ajaxTitleCheck(sessionID,urlToCheck) {
  document.getElementById('ajaxWait1').style.display='inline-block';
  if (urlToCheck!='') {
    var xhr=initAjax();
    xhr.onreadystatechange= function() { ajaxTileCheckDo(xhr); }
    xhr.open("GET","/ajax.php?testLink="+urlToCheck+"&SID="+sessionID,true);
    xhr.send(null);}
  else {
    document.getElementById('ajaxWait1').style.display='none';}}
function ajaxTileCheckDo(xhr) {
  if (xhr.readyState==4 && xhr.status==200){
  var docXML= xhr.responseXML;
  var done=docXML.getElementsByTagName("done").item(0).firstChild.data;
  var result=docXML.getElementsByTagName("result").item(0).firstChild.data;
  document.getElementById('ajaxWait1').style.display='none';
  if (result==1) {
    var urlTitle=docXML.getElementsByTagName("title").item(0).firstChild.data;
    if (urlTitle!='') document.getElementById('linkName').value=urlTitle;}}}

function ajaxIconCheck(sessionID,urlToCheck) {
  document.getElementById('ajaxWait2').style.display='inline-block';
  if (urlToCheck!='') {
    var xhr=initAjax();
    xhr.onreadystatechange= function() { ajaxIconCheckDo(xhr); }
    xhr.open("GET","/ajax.php?testIcon="+urlToCheck+"&SID="+sessionID,true);
    xhr.send(null);}
  else {
    document.getElementById('ajaxWait2').style.display='none';}}
function ajaxIconCheckDo(xhr) {
  if (xhr.readyState==4 && xhr.status==200){
  var docXML= xhr.responseXML;
  var icon=docXML.getElementsByTagName("icon").item(0).firstChild.data;
  document.getElementById('ajaxWait2').style.display='none';
  document.getElementById('currentIcon').src=icon;}}

function iconDelete(sessionID,iconToDelete) {
  document.getElementById('ajaxWait2').style.display='inline-block';
    var xhr=initAjax();
    xhr.onreadystatechange= function() { iconDeleteDo(xhr); }
    xhr.open("GET","/ajax.php?iconDelete="+iconToDelete+"&SID="+sessionID,true);
    xhr.send(null);}
function iconDeleteDo(xhr) {
  if (xhr.readyState==4 && xhr.status==200){
  var docXML= xhr.responseXML;
  var result=docXML.getElementsByTagName("result").item(0).firstChild.data;
  document.getElementById('ajaxWait2').style.display='none';
  if (result==1) document.getElementById('currentIcon').src='/skins/pics/globe.png';}}

function importLoop(sessionID,xhr){
  // On ne fait quelque chose que si on a tout reçu et que le serveur est ok
  if(xhr.readyState==4 && xhr.status==200){
    var docXML= xhr.responseXML;
    var moreToCome=docXML.getElementsByTagName("more").item(0).firstChild.data;
    if (moreToCome=="y") {
      var divWidth=docXML.getElementsByTagName("width").item(0).firstChild.data;
      var perCent=docXML.getElementsByTagName("percent").item(0).firstChild.data;
      var checkLink=docXML.getElementsByTagName("checkLink").item(0).firstChild.data;
      document.getElementById('countMeter').style.width=divWidth;
      document.getElementById('countMeterLeft').innerHTML=perCent+'%';
      document.getElementById('checkLink').innerHTML=unescape(checkLink);
      importFile(sessionID);}
else  window.location.href='/import.php?SID='+sessionID;}}

