var xmlhttp = false;

function _el(id){
    return document.getElementById(id);
}

function initRequest(){

    var xmlhttptemp = false;

    if(window.XMLHttpRequest) {
        try {
            xmlhttptemp = new XMLHttpRequest();
        } catch(e) {
            xmlhttptemp = false;
            alert('Failed to create XMLHttpRequest');
        }
    } else if(window.ActiveXObject) {
        try {
            xmlhttptemp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            try {
                xmlhttptemp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(e) {
                xmlhttptemp = false;
                alert('Failed to create XMLHTTP');
            }
        }
    }
    return xmlhttptemp;
}

function show_models_combo(id_brand) {
    xmlhttp = initRequest();
    xmlhttp.onreadystatechange=set_model_combobox;
    xmlhttp.open("GET", baseurl + '/getmodelscombo?id_brand='+escape(id_brand).replace(/\+/g,'%2B'), true);
    xmlhttp.send(null);
}

function set_model_combobox()
{
    if (xmlhttp.readyState==4){
        if (xmlhttp.status == 200) {
            _el('model_combobox').innerHTML = xmlhttp.responseText;
        } else {
            alert("There was a problem retrieving the XML data:\n" + xmlhttp.statusText);
        }
        show_acc_combo('');
    }
}

function show_acc_combo(id_model) {
    xmlhttp = initRequest();
    xmlhttp.onreadystatechange=set_acc_combobox;
    xmlhttp.open("GET", baseurl + '/getacccombo?id_model='+escape(id_model).replace(/\+/g,'%2B'), true);
    xmlhttp.send(null);
}

function set_acc_combobox()
{
    if (xmlhttp.readyState==4){
        if (xmlhttp.status == 200) {
            _el('acc_combobox').innerHTML = xmlhttp.responseText;
        } else {
            alert("There was a problem retrieving the XML data:\n" + xmlhttp.statusText);
        }
    }
}



function doSearch() {
    var id_brand    = _el('id_brand').value;
    var id_model    = _el('id_model').value;
    var id_acc      = _el('id_acc').value;

    if (id_acc=='' && id_brand=='' && id_model=='') {
        document.location = baseurl+'/accessories/';
        return false;
    } 
    
    if (id_acc!='' && id_brand=='' && id_model=='') {
        document.location = baseurl+'/accessories/'+id_acc+'/';
        return false;
    } 
    
    if (id_acc=='' && id_brand!='' && id_model=='') {
        document.location = baseurl+'/brands/'+id_brand+'/';
        return false;
    } 
    
    if (id_acc=='' && id_brand!='' && id_model!='') {
        document.location = baseurl+'/brands/'+id_brand+'/'+id_model+'/';
    }
    
    if (id_acc!='' && id_brand!='' && id_model!='') {
        document.location = baseurl+'/brands/'+id_brand+'/'+id_model+'/'+id_acc+'/';
    }
}

