// JScript File

var strPath = '/content/upload/AssetMgmt/inDirectoryAjaxXML/';
var tag, cat;
var AjaxServerPageName = strPath + 'AppraiserStates.xml' ;
var noPref = "-- Select a city --";


function AjaxCaller(ctrl, tag, pdir) {

	var obj = document.getElementById(ctrl); 
	if (obj==null){ obj=document.getElementsByName(ctrl)[0]; }

	removeAllOptions(obj);
	cat = pdir ;
	var requestUrl = AjaxServerPageName;
	var ajax = new AjaxDelegate(requestUrl, CreateList, ctrl, tag);
	ajax.Fetch();

}


function CreateList(url, response, ctrl, tag) {

	var data = new Array();
	data =	response ;
	//tag = 'Insurance';

	var obj = document.getElementById(ctrl);
	if (obj==null){ obj=document.getElementsByName(ctrl)[0]; }
	
	//removeAllOptions(obj);
	loadXMLToDropdown(data, obj, tag) 
}


function removeAllOptions(from) { 
	//remove all options	
	var max = from.options.length
	for (var i=0; i<max; i++) {
		from.options[0] = null; 
	}
	from.selectedIndex = -1; 
} 


function loadXMLToDropdown(xml, dd, field) {
        var xmldoc, parser;
        var Elem;
        var destination="", addtext, addvalue;
        var xAttr, attrValue, j;

        j=0;
        try{		// Internet Explorer
		xmldoc = new ActiveXObject("Microsoft.XMLDOM") 
		xmldoc.async = false 
		xmldoc.loadXML(xml)         

		Elem = xmldoc.getElementsByTagName(field) ; 
		Provlength = Elem.length; 
            	
		for(i=0; i < Provlength; i++) 
		{ 
			xAttr = Elem.item(i).attributes;
			attrValue = xAttr.getNamedItem("category").nodeValue;
			if (attrValue==cat){
		    		destination = Elem.item(i).text;
		    		    
		    		addtext = destination; 
		    		
		    		if (destination == noPref) {destination=""}
		    		    
		    		addvalue = destination; 
		    
		    		dd.options[j] = new Option(addtext,addvalue);
		    		j = j+1;
		    	}
		}            
            
        }catch(e){	//Firefox, Mozilla, Opera, etc.                
            	parser = new DOMParser();
            	xmldoc = parser.parseFromString(xml,"text/xml");

		Elem = xmldoc.getElementsByTagName(field) ; 
        	Provlength = Elem.length;                        
        
		for(i=0; i < Provlength; i++) 
		{ 
		
			xAttr = Elem[i].attributes;
			attrValue = xAttr.getNamedItem("category").nodeValue;
			if (attrValue==cat){
				destination = Elem[i].childNodes[0].nodeValue; 
				
				addtext = destination; 

				if (destination == noPref) {destination=""}

				addvalue = destination; 

				dd.options[j] = new Option(addtext, addvalue);                  
				j = j+1;
			}
		}
        	
        }

                              
    } 


function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

// example of using trim, ltrim, and rtrim
//var myString = " hello my name is ";
//alert("*"+trim(myString)+"*");
//alert("*"+ltrim(myString)+"*");
//alert("*"+rtrim(myString)+"*");

