imgLoading     = new Image();
imgLoading.src = '/images/loading.gif';

ind = '';
count = '';
req = new Array();
trk = new Array();

function getNewXMLHTTP() {
    try {
        return new XMLHttpRequest();
    } catch(e) {	
        try {
            var aObj = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                var aObj = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(e) {
                return false;
            }
        }
    }
    return aObj;
}

function indexer() {
    if (document.getElementById('validation_site').host.value != 'http://') {
        url  = document.getElementById('validation_site').host.value;
        host = url.replace('http://','');

        document.getElementById('wait').innerHTML   = '<img src="/images/loading.gif" alt="Traitement en cours..." width="15" height="15" align="absmiddle" />&nbsp; Traitement en cours...';
        document.getElementById('button').innerHTML = '<input type="button" value="Arrêter" onclick="window.location=\'validation-site.html?host='+host+'\'" class="btn_left_100" />';
        try {
            ind = getNewXMLHTTP();
            ind.open("GET", "/scripts/ajax/ajax.index.php?url="+document.getElementById('validation_site').host.value+"&hash="+Math.random(), true);
            ind.onreadystatechange = reload_page;
            ind.send(null);
        } catch(e) {
            alert("Une erreur s'est produite, veuillez réessayer de nouveau!        ");
        }
    } else {
        alert('Veuillez entrer l\'url du site à valider        ');
    }
    setTimeout('check_count()', 1000);
}

function check_count() {
    try {
        count = getNewXMLHTTP();
        count.open("GET", "/scripts/ajax/ajax.index.count.php?url="+document.getElementById('validation_site').host.value+"&hash="+Math.random(), true);
        count.onreadystatechange = update_count;
        count.send(null);
    } catch(e) {
        alert("Une erreur s'est produite, veuillez réessayer de nouveau!        ");
    }
}

function update_count() {
    if (count.readyState == 4) {
        if (count.status == 200) {
            url  = document.getElementById('validation_site').host.value;
            host = url.replace('http://','');

            x = count.responseXML.getElementsByTagName('host');
            a = x[0].getAttribute('count');
            if (a == 1) {
                document.getElementById('wait').innerHTML = '<img src="/images/loading.gif" alt="Traitement en cours..." width="15" height="15" align="absmiddle" />&nbsp; Traitement en cours... '+a+' page indexée';
            } else if (a > 1) {
                document.getElementById('wait').innerHTML = '<img src="/images/loading.gif" alt="Traitement en cours..." width="15" height="15" align="absmiddle" />&nbsp; Traitement en cours... '+a+' pages indexées';
            }
            setTimeout('check_count()', 1000);
        }
    }
}

function reload_page() {
    if (ind.readyState == 4) {
        if (ind.status == 200) {
            x = ind.responseXML.getElementsByTagName('host');
            a = x[0].getAttribute('url');
            if (a == '') {
                document.getElementById('wait').innerHTML = '<span class="error">Le nom de domaine semble invalide</span>';
            } else {
                window.location = '/outils/validation-site.html?host='+a;
            }
        }
    }
}

function valider(id) {
    document.getElementById('url-'+id).innerHTML = '<div style="padding:0;font-size:1px;"><img src="/images/loading.gif" alt="Traitement en cours..." width="15" height="15" /></div>';
    try {
        trk[id] = true;
        req[id] = getNewXMLHTTP();
        req[id].open("GET", "/scripts/ajax/ajax.validator.w3c.php?id="+id+"&hash="+Math.random(), true);
        req[id].onreadystatechange = update_error;
        req[id].send(null);
    } catch(e) {
        alert("Une erreur s'est produite, veuillez réessayer de nouveau!        ");
    }
}

function valider_tout() {
    for(var i in ids) {
        valider(ids[i]);
    }
}

function update_error() {
    for(var i in req) {
        if (trk[i] == true && req[i].readyState == 4 && req[i].status == 200) {
            x = req[i].responseXML.getElementsByTagName('url');
            a = x[0].getAttribute('id');
            b = x[0].getAttribute('count');
            c = document.getElementById('href-'+a).href;
            document.getElementById('url-'+a).innerHTML = '<div style="padding:1px 0 2px 0;"><a href="javascript:void(0);" onclick="w3c(\''+c+'\');return false;" title="Voir les erreurs">'+b+'</a></div>';
            if (b > 0 || b == 'x' || b == 'X' || b == 'nf' || b == 'NF') {
                clr[a] = '#ffcccc';
                document.getElementById('bg-'+a).style.backgroundColor = '#ffcccc';
            } else {
                clr[a] = '#fafafa';
                document.getElementById('bg-'+a).style.backgroundColor = '#fafafa';
            }
            trk[i] = false;
        }
    }
}

function w3c(url) {
    window.open('http://validator.w3.org/check?uri='+url);
}