function resetSelect(name) {
    document.getElementById('select_' + name).value = 'tout';
}
function createRequestObject() {

    var objAjax;

    if (navigator.appName == 'Microsoft Internet Explorer') {
        objAjax = new ActiveXObject('Microsoft.XMLHTTP');
    }
    else {
        objAjax = new XMLHttpRequest();
    }

    return objAjax;

}
function sendRequest(field, condition, table) {

    switch (field) {
        case 'acquereur':
            url = 'list_cibles.php?condition=' + condition + '&table=' + table
                + '&acquereur=' + document.getElementById('select_acquereur').value
                + '&cible=' + document.getElementById('select_cible').value
                + '&operation=' + document.getElementById('select_operation').value;
            http.open('get', url);
            http.onreadystatechange = handleResponseForCible;
            break;
        case 'cible':
            url = 'list_operations.php?condition=' + condition + '&table=' + table
                + '&acquereur=' + document.getElementById('select_acquereur').value
                + '&cible=' + document.getElementById('select_cible').value
                + '&operation=' + document.getElementById('select_operation').value;
            http.open('get', url);
            http.onreadystatechange = handleResponseForOperation;
            break;
        case 'operation':
            url = 'list_acquereurs.php?condition=' + condition + '&table=' + table
                + '&acquereur=' + document.getElementById('select_acquereur').value
                + '&cible=' + document.getElementById('select_cible').value
                + '&operation=' + document.getElementById('select_operation').value;
            http.open('get', url);
            http.onreadystatechange = handleResponseForAcquereur;
            break;
        case 'industrie':
            url = 'list_clients.php?condition=' + condition + '&table=' + table
                + '&industrie=' + document.getElementById('select_industrie').value
                + '&client=' + document.getElementById('select_client').value
                + '&date=' + document.getElementById('select_date').value;
            http.open('get', url);
            http.onreadystatechange = handleResponseForClient;
            break;
        case 'client':
            url = 'list_dates.php?condition=' + condition + '&table=' + table
                + '&industrie=' + document.getElementById('select_industrie').value
                + '&client=' + document.getElementById('select_client').value
                + '&date=' + document.getElementById('select_date').value;
            http.open('get', url);
            http.onreadystatechange = handleResponseForDate;
            break;
        case 'date':
            url = 'list_industries.php?condition=' + condition + '&table=' + table
                + '&industrie=' + document.getElementById('select_industrie').value
                + '&client=' + document.getElementById('select_client').value
                + '&date=' + document.getElementById('select_date').value;
            http.open('get', url);
            http.onreadystatechange = handleResponseForIndustrie;
            break;
        case 'theme':
            url = 'list_normes.php?condition=' + condition + '&table=' + table
                + '&norme=' + document.getElementById('select_norme').value
                + '&theme=' + document.getElementById('select_theme').value;

            if (document.getElementById('select_publication'))
                url += '&publication=' + document.getElementById('select_publication').value;

            http.open('get', url);
            http.onreadystatechange = handleResponseForNorme;
            break;
        case 'norme':
            url = 'list_publications.php?condition=' + condition + '&table=' + table
                + '&theme=' + document.getElementById('select_theme').value
                + '&norme=' + document.getElementById('select_norme').value;

            if (document.getElementById('select_publication'))
                url += '&publication=' + document.getElementById('select_publication').value;

            http.open('get', url);
            http.onreadystatechange = handleResponseForPublication;
            break;
        case 'publication':
            url = 'list_themes.php?condition=' + condition + '&table=' + table
                + '&theme=' + document.getElementById('select_theme').value
                + '&norme=' + document.getElementById('select_norme').value;

            if (document.getElementById('select_publication'))
                url += '&publication=' + document.getElementById('select_publication').value;

            http.open('get', url);
            http.onreadystatechange = handleResponseForTheme;
            break;

    }
    http.send(null);

}
function ieUpdateSelect(objSelect, content) {

    var option;
    var ieSelect = document.createElement('ie_select');

    objSelect.innerHTML = '';
    ieSelect.id = 'ie_select';

    document.body.appendChild(ieSelect);
    ieSelect = document.getElementById('ie_select');
    ieSelect.style.display = 'none';

    content = 
    content.replace(/<option/g, '<span').replace(/<\/option/g, '</span');
    ieSelect.innerHTML = content;

    for (var i = 0; i < ieSelect.childNodes.length; i++) {
        if (ieSelect.childNodes[i].tagName) {
            option = document.createElement('OPTION');
            option.value = ieSelect.childNodes[i].getAttribute('value');
            option.text = ieSelect.childNodes[i].innerHTML;
            isSelected = false;
            if (ieSelect.childNodes[i].selected) {isSelected = true;}
            if (document.all) {
                objSelect.add(option);
            }
            else {
                objSelect.appendChild(option);
            }
            if (isSelected) objSelect.options[i].selected = true;
        }
    }

    // Destroy element and object
    document.body.removeChild(ieSelect)
    ieSelect = null;

}
function handleResponse(objHttp, field, next) {

    var objSelect = document.getElementById('select_' + field);
    if (objSelect && navigator.appName == 'Microsoft Internet Explorer') {
        ieUpdateSelect(objSelect, objHttp.responseText);
    }
    else if (objSelect) {
        objSelect.innerHTML = objHttp.responseText;
    }

    if (typeof next == 'undefined') return;
   

    switch (next) {
        case 'theme':
    			table = 'Articles';
            url = 'list_themes.php?table=' + table
                + '&theme=' + document.getElementById('select_theme').value
                + '&norme=' + document.getElementById('select_norme').value;

            if (document.getElementById('select_publication'))
                url += '&publication=' + document.getElementById('select_publication').value;
    
            http2.open('get', url);
            http2.onreadystatechange = handleResponseForThemeLast;
            break;
        case 'norme':
    			table = 'Articles';
            url = 'list_normes.php?table=' + table
                + '&theme=' + document.getElementById('select_theme').value
                + '&norme=' + document.getElementById('select_norme').value;

            if (document.getElementById('select_publication'))
                url += '&publication=' + document.getElementById('select_publication').value;
            
            http2.open('get', url);
            http2.onreadystatechange = handleResponseForNormeLast;
            break;
        case 'publication':
    			table = 'Articles';
            url = 'list_publications.php?table=' + table
                + '&theme=' + document.getElementById('select_theme').value
                + '&norme=' + document.getElementById('select_norme').value;

            if (document.getElementById('select_publication'))
                url += '&publication=' + document.getElementById('select_publication').value;

            http2.open('get', url);
            http2.onreadystatechange = handleResponseForPublicationLast;
            break;

        case 'acquereur':
    			table = 'Expertises';
            url = 'list_acquereurs.php?table=' + table
                + '&acquereur=' + document.getElementById('select_acquereur').value
                + '&cible=' + document.getElementById('select_cible').value
                + '&operation=' + document.getElementById('select_operation').value;
            http2.open('get', url);
            http2.onreadystatechange = handleResponseForAcquereurLast;
            break;
        case 'cible':
    			table = 'Expertises';
            url = 'list_cibles.php?table=' + table
                + '&acquereur=' + document.getElementById('select_acquereur').value
                + '&cible=' + document.getElementById('select_cible').value
                + '&operation=' + document.getElementById('select_operation').value;
            http2.open('get', url);
            http2.onreadystatechange = handleResponseForCibleLast;
            break;
        case 'operation':
    			table = 'Expertises';
            url = 'list_operations.php?table=' + table
                + '&acquereur=' + document.getElementById('select_acquereur').value
                + '&cible=' + document.getElementById('select_cible').value
                + '&operation=' + document.getElementById('select_operation').value;
            http2.open('get', url);
            http2.onreadystatechange = handleResponseForOperationLast;
            break;

        case 'industrie':
    			table = 'Rap';
            url = 'list_industries.php?table=' + table
                + '&industrie=' + document.getElementById('select_industrie').value
                + '&client=' + document.getElementById('select_client').value
                + '&date=' + document.getElementById('select_date').value;
            http2.open('get', url);
            http2.onreadystatechange = handleResponseForIndustrieLast;
            break;
        case 'client':
    			table = 'Rap';
            url = 'list_clients.php?table=' + table
                + '&industrie=' + document.getElementById('select_industrie').value
                + '&client=' + document.getElementById('select_client').value
                + '&date=' + document.getElementById('select_date').value;
            http2.open('get', url);
            http2.onreadystatechange = handleResponseForClientLast;
            break;
        case 'date':
    			table = 'Rap';
            url = 'list_dates.php?table=' + table
                + '&industrie=' + document.getElementById('select_industrie').value
                + '&client=' + document.getElementById('select_client').value
                + '&date=' + document.getElementById('select_date').value;
            http2.open('get', url);
            http2.onreadystatechange = handleResponseForDateLast;
            break;
    }
    http2.send(null);

}
function handleResponseForAcquereur() {
    if (http.readyState == 4) {
        handleResponse(http, 'acquereur', 'cible');
    }
}
function handleResponseForCible() {
    if (http.readyState == 4) {
        handleResponse(http, 'cible', 'operation');
    }
}
function handleResponseForOperation() {
    if (http.readyState == 4) {
        handleResponse(http, 'operation', 'acquereur');
    }
}
function handleResponseForAcquereurLast() {
    if (http2.readyState == 4) {
        handleResponse(http2, 'acquereur');
    }
}
function handleResponseForCibleLast() {
    if (http2.readyState == 4) {
        handleResponse(http2, 'cible');
    }
}
function handleResponseForOperationLast() {
    if (http2.readyState == 4) {
        handleResponse(http2, 'operation');
    }
}
function handleResponseForIndustrie() {
    if (http.readyState == 4) {
        handleResponse(http, 'industrie', 'client');
    }
}
function handleResponseForClient() {
    if (http.readyState == 4) {
        handleResponse(http, 'client', 'date');
    }
}
function handleResponseForDate() {
    if (http.readyState == 4) {
        handleResponse(http, 'date', 'industrie');
    }
}
function handleResponseForIndustrieLast() {
    if (http2.readyState == 4) {
        handleResponse(http2, 'industrie');
    }
}
function handleResponseForClientLast() {
    if (http2.readyState == 4) {
        handleResponse(http2, 'client');
    }
}
function handleResponseForDateLast() {
    if (http2.readyState == 4) {
        handleResponse(http2, 'date');
    }
}

function handleResponseForTheme() {
    if (http.readyState == 4) {
        handleResponse(http, 'theme', 'norme');
    }
}
function handleResponseForNorme() {
    if (http.readyState == 4) {
        handleResponse(http, 'norme', 'publication');
    }
}
function handleResponseForPublication() {
    if (http.readyState == 4) {
        handleResponse(http, 'publication', 'theme');
    }
}
function handleResponseForThemeLast() {
    if (http2.readyState == 4) {
        handleResponse(http2, 'theme');
    }
}
function handleResponseForNormeLast() {
    if (http2.readyState == 4) {
        handleResponse(http2, 'norme');
    }
}
function handleResponseForPublicationLast() {
    if (http2.readyState == 4) {
        handleResponse(http2, 'publication');
    }
}


var http = createRequestObject();
var http2 = createRequestObject();
