var gettysburgHighSchoolsComboBox = function() {

	var comboDS = new Array();

	function initComboDataSource(comboId) {
		var recordType = Ext.data.Record.create([
		   {name: "id", type: "string"},
		   {name: "name", type: "string"},
		  ]);
	
		var ajaxfunc = AdmissionsInstitutionsAjax.getHighSchoolsByNameLike;
			
		  comboDS[comboId] = new Ext.data.Store({
		    proxy: new Ext.data.DWRProxy(ajaxfunc),
		    reader: new Ext.data.ListRangeReader( 
					{id:'id', totalProperty:'total'}, recordType),
		    remoteSort: true
		  });
		
	};
	
	 var resultTpl = new Ext.Template(
	     '<div class="search-item">',
	         '&nbsp;&nbsp;{name}',
	     '</div>'
	 );		

	 var comboObjs = new Array();
	  
	 function initCombo (comboId, selectedId, selectedValue, comboConfig) {
	 		
	 		comboConfig = comboConfig == null?new Array():comboConfig;
	 		
	 		comboConfig['store'] = comboDS[comboId];
	 		comboConfig['displayField'] = 'name';
	 		comboConfig['valueField'] = 'id';
	 		if(comboConfig['triggerAction'] == null)
	 			comboConfig['triggerAction'] = 'all';
	 		if(comboConfig['tpl'] == null)
	 			comboConfig['tpl'] = resultTpl;
	 		if(comboConfig['forceSelection'] == null)
	 			comboConfig['forceSelection'] = true;
	 		
		    comboObjs[comboId] = new Ext.form.ComboBox(comboConfig);
		    
		    if(selectedValue != null)
		    	comboObjs[comboId].setValue(selectedValue);
		    	
			// apply it to the exsting input element
			comboObjs[comboId].applyTo(comboId + 'CB');		
		    
		    var delegate = changeComboValue.createDelegate(this, [comboId], 0);
			comboObjs[comboId].on('select', delegate);
			
			if(selectedId != null)
		   		$(comboId).value = selectedId;
		   		
		   	return comboObjs[comboId];
	 }
	 
	 function changeComboValue (comboid, combo, record, index) {
		 	$(comboid).value = record.get('id');
	 }
	 
	 function init (config) {
			if(config["comboId"] == null) throw "comboId is required";
			var parentElement = config["parentElement"];
			var comboId = config["comboId"];
			var selectedId = config["selectedId"] == null?config["selectedValue"]:config["selectedId"];
			var selectedValue = config["selectedValue"] == null?selectedId:config["selectedValue"];
			var comboConfig = config["comboConfig"] == null?null:config["comboConfig"];
			new Insertion.Bottom(parentElement, "<input type='text' id='" + comboId + "CB' name='" + comboId + "CB'/>");
			new Insertion.Bottom(parentElement, "<input type='hidden' id='" + comboId + "' name='" + comboId + "'/>");
			initComboDataSource(comboId);
			initCombo(comboId, selectedId, selectedValue, comboConfig);
	 }
	 
	return {
		 
		buildComboBox : function  (config) {
		    var delegate = init.createDelegate(this, [config], 0);
			Ext.onReady(delegate);
		},
		
		getComboBox : function (id) {
			return comboObjs[id];
		}
	}
}();