function dynamicSelect(id1, id2) {
    selectItems(id2, id1);
}

function refreshDynamicSelectOptions(sel1, sel2, clonedOptions, value) {
    refreshItems(sel2, sel1, null, clonedOptions, value);    
}

function selectItems(id1, id2, id3) {
    if (document.getElementById && document.getElementsByTagName) {
        var child = document.getElementById(id1);
        var parent = document.getElementById(id2);
        var parent2 = (id3 != null) ? document.getElementById(id3) : null;
        var clone = child.cloneNode(true);
        var options = clone.getElementsByTagName("option");
        var value = child.options[child.selectedIndex].value;
        refreshItems(child, parent, parent2, options, value);
        parent.onchange = function(){refreshItems(child, parent, parent2, options, value);}
        if (id3 != null) {
            parent2.onchange = function(){refreshItems(child, parent, parent2, options, value);};
        }    
    }
}

function refreshItems(child, parent, parent2, options, value) {    
    while (child.options.length) {child.remove(0);}
    var count = 0;
    for (var i = 0; i < options.length; i++) {
        if (parent2 != null) {
            if ((options[i].className == parent.options[parent.selectedIndex].value &&
                options[i].id == parent2.options[parent2.selectedIndex].value) ||
                options[i].value == "") {
                child.appendChild(options[i].cloneNode(true));
                if (options[i].value == value) {
                   child.selectedIndex = count;
                }
                count++;
            }
        } else {            
            if (options[i].className == parent.options[parent.selectedIndex].value ||
                options[i].value == "") {
                child.appendChild(options[i].cloneNode(true));
                if (options[i].value == value) {
                    child.selectedIndex = count;
                }
                count++;                
            }
        }
    }
}

var is_first_load = true;

function getItems(parent_id, child_id, param) {
    
    if (!document.getElementById) {
        return false;
    }
    
    brand_id = document.getElementById(parent_id).value;
    
    if (!brand_id) {
        return false;
    }    
    
    child = document.getElementById(child_id);    
    
    if (typeof window.ActiveXObject != 'undefined') {
        xml = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        xml = new XMLHttpRequest();
    }
    
    xml.onreadystatechange = function() {

        if (xml.readyState != 4) {
            return false;
        }                
        
        child.options.length = 0;
        child.options[0] = new Option('', '', false, false);
        
        array = xml.responseText.split('\n');
        
        for(var i in array) {
            value = array[i].split('\t');
            child.options[child.options.length] = new Option(value[1], value[0], false, false);
        }       
//        child.disabled = false;
        setValue(child_id, param);
    }
           
    xml.open("GET", "/common/js/select.php?brand_id=" + brand_id, true);
    xml.send(null);    
}

function setValue(field_id, param) {
    
    if (!document.getElementById || !is_first_load) {
        return false;
    }
    
    var field = document.getElementById(field_id);
    var re = new RegExp('^.*?.*' + param + '=(\\d*).*$');
    var matches = re.exec(window.location);
        
    if (matches && field.options.length > 0) {        
        for (var i = 0; i < field.options.length; i++) {
            if (matches[1] == field.options[i].value) {
                field.options[i].selected = true;
                is_first_load = false;                
                break;    
            }
        }    
    }
}
