function init_rf(){
    init_ajax("fncid=0", generate_ops);
}

function ajax_loader(css){
    document.getElementById("rfloader").style.display = css;

}

function generate_ops (){
    if (XHR.readyState==4 || XHR.readyState=="complete"){
        r = eval('(' + XHR.responseText + ')');
        cnr = document.getElementById("renofindercontainer");
        
        //If there are options, then build a list; otherwise assume
        //we've exhausted the drilldown and display a submit button
        if (r.ops != null){
            //Building/retrieving the list
            e = (document.getElementById(r.sel) == null) ?
                document.createElement("select") : document.getElementById(r.sel);
            
            //Setting parameters
            e.id = r.sel;
            e.fnid = r.fnid;
            e.param = r.param;
            e.style.display = '';
            
            //Populating the options
            for (i in r.ops){
                op = document.createElement("option");
                op.innerHTML = r.ops[i].name;
                op.value = r.ops[i].id;
                e.appendChild(op, null);
            }
            
            //Registering the onchange event
            e.onchange = updatelist;
        } else {
            //Building/retrieving the submit button
            e = (document.getElementById("searchbutton") == null) ?
                document.createElement("input") : document.getElementById("searchbutton");
            
            //Setting specific properties
            e.id = "searchbutton";
            e.value = "Search!";
            e.type = "submit";
            e.className = "submitbutton";
            e.style.display = '';
        }
        
        //Adding the new element to the container
        cnr.appendChild(e, null);
        
        //Hiding loader
        ajax_loader("none");
    }
}

function updatelist(){
    //Show loader
    ajax_loader("");
    
    s_coll = gen_coll(this);
    //Only continue if the default selection wasn't chosen
    if (this.selectedIndex > 0){
        //Clear all subsequent lists before appending new ones
        clear_ops(s_coll.currentindex, s_coll);
        
        //Pulling the current state of the renofinder
        state = get_state(s_coll);
        
        //Submitting to AJAX backend to evaluate and return results
        init_ajax("fncid=" + this.fnid + "&" + state, generate_ops);
    }

}

function gen_coll(sel_obj){
    tmp_coll = document.getElementById("renofindercontainer").childNodes;
    s_coll = new Array();
    
    for (i = 0; i < tmp_coll.length; i++){
        if (tmp_coll[i].nodeType == 1 && tmp_coll[i].nodeName.toLowerCase() == "select"){
            if (tmp_coll[i].options.length > 0){
                s_coll.push(tmp_coll[i]);
                
                //Determining where the calling element is
                if (sel_obj != null && tmp_coll[i] == sel_obj)
                    s_coll.currentindex = s_coll.length;
            }
        }
    }
    
    return s_coll;
}

function clear_ops(ind, s_coll){
    for (i = ind; i < s_coll.length; i++ ){
        s_coll[i].length = 0;
        s_coll[i].style.display = 'none';
    }
    
    sub_elem = document.getElementById("searchbutton");
    
    if (sub_elem != null){
        sub_elem.style.display = 'none';
    }
}

function rf_submit(frm){
    s_coll = gen_coll(null);
    flag = true;
    url = frm.action + "?";
    
    for (i = 0; i < s_coll.length; i++){
        s_coll[i];
        
        url += s_coll[i].param + "=" + s_coll[i].options[s_coll[i].selectedIndex].value + "&";
        
        //Ensuring something other than the first option is selected
        //Starting out with true, and negating on any infraction
        if (s_coll[i].selectedIndex <= 0){
            flag = false;
            break;
        }
    }
    
    if (flag){
        //Removing trailing ampersand
        url = url.substring(0, url.lastIndexOf('&'));
        window.location.href = url;
    }
    
    //Never cause a round trip to the server
    return false;
}

function get_state(col){
    s = "";
    
    //Can't use for...in with this type of collection; enumerates length and other properties as well
    for (i = 0; i < col.length; i++){
        
        //Must change to >= 1 once we have a default selection in the list
        if (col[i].selectedIndex >= 1){
            s += col[i].id + "=";
            s += col[i].options[col[i].selectedIndex].value + "&";
        }
    }
    
    end = s.lastIndexOf('&');    
    s = s.substring(0, end);
    
    return s;
}

function init_ajax(data, readyHandle){
    XHR = GetXmlHttpObject();
	
  	if (readyHandle != null){
  		 XHR.onreadystatechange = readyHandle;
  		 async = true;
  	} else {
  		 async = false;
  	}
  	
  	XHR.open("POST","renofinder.php", async);
  	XHR.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  	XHR.send(data);
}
