﻿<!-- ============================================================ -->
//Generic AJAX function		
var http = createRequestObject();
var arr_val = new Array();
var arr_text = new Array();
var mainList, targetList, listSelected

function createRequestObject() {
    if (window.XMLHttpRequest)
      {
      http=new XMLHttpRequest()
      }
    // code for IE
    else if (window.ActiveXObject)
      {
      http=new ActiveXObject("Microsoft.XMLHTTP")
      }
}

function ajax_getOptions(prmThis, prmTarget, prmType){
    createRequestObject();
	
    mainList =prmThis;
    targetList = prmTarget;

    str_param = "?target=" + targetList + "&parent=" + mainList.value + "&type=" + prmType;

    tmp_target = document.getElementById(targetList)
    var elOptNew = document.createElement('option');
    tmp_target.length = 0;
	   
	elOptNew = new Option("Loading...",0);
	tmp_target.options.add(elOptNew, 0)
	        
    http.open('GET', '/library/script/inc_buildList.aspx' + str_param, true);		
    http.onreadystatechange = buildList;
    http.send(null);

}

function buildList() {
    if(!http){return;}            
    if(http.readyState == 4){	
        //Read values from xml	
        var temp1, temp2;	
        arr_val.length = 0;
        arr_text.length = 0;

        //alert(http.responseText)
	    var str_xml = http.responseXML;
		
	    var arrOptions1 = str_xml.getElementsByTagName("va");
	    for (x=0;x<arrOptions1.length;x++){
	        arr_val[x]=arrOptions1[x].firstChild.data 
	    }
	    var arrOptions2 = str_xml.getElementsByTagName("te");
	    for (x=0;x<arrOptions2.length;x++){
	        arr_text[x]=arrOptions2[x].firstChild.data 	       
	    }
	    str_xml = "";
	    
	    
	    tmp_target = document.getElementById(targetList)
	    var elOptNew = document.createElement('option');
	    tmp_target.options.length = 0;

        //Read XML into select
		
	    for(x=0; x<arr_val.length; x++){
	        elOptNew = new Option(arr_text[x],arr_val[x]);
	        tmp_target.options.add(elOptNew, x)			
	    }
		if(arr_val.length==1){
			tmp_sub_locs = document.getElementById(tmp_sub_div);
			tmp_sub_locs.removeChild(tmp_target);					
		}

    }		
}

//===========================================================
//Deal with adding new dd lists

var arr_lists = new Array();
var tmp_str = ""
var tmp_sub_div = ""

function updateChild(prmField, prm_str, prm_sub_div){
	tmp_field = document.getElementById(prmField);
	tmp_value = tmp_field.value;
	tmp_id = parseInt(tmp_field.id.replace(prm_str, ""))
	tmp_str = prm_str;
	tmp_sub_div = prm_sub_div;
	
	//Rebuild child lists
	if(tmp_value>0){
		tmp_sub_locs = document.getElementById(tmp_sub_div);
		
		tmp_nextID = tmp_id+1;
		tmp_next = tmp_str+tmp_nextID;
		
		if(!document.getElementById(tmp_next)){
			//Add a new child drop down list
			script = document.createElement('select');
			script.setAttribute('id',tmp_next);
			script.setAttribute('name',tmp_next);
			script.setAttribute('class','search_fields');
			
			tmp_sub_locs.appendChild(script)
			
			tmp1 = document.getElementById(tmp_next)
			tmp1.style.width = "165px";
			tmp1.onchange = new Function ("evt", "updateChild('" + tmp_next + "', '" + tmp_str + "', '" + tmp_sub_div + "')");
		}
		ajax_getOptions(tmp_field, tmp_next, "location");
		//alert(document.getElementById(tmp_next).options.length)

	}
	
	
	
	//if(!tmp_nextID){tmp_nextID=0}
	//alert(tmp_nextID)
	tmp_start = tmp_nextID+1;
	tmp_end = tmp_nextID +6;
	for(y=tmp_start; y<tmp_end; y++){
		var olddiv = document.getElementById(tmp_str+y);
		if(olddiv){tmp_sub_locs.removeChild(olddiv);}
		
	}		
}
