var DynamicCombo = Class.create();
DynamicCombo.prototype = {
	initialize: function(source, target, url, sourceId, targetId, sourceUrl) {
		this.source = $(source);
		this.target = $(target);
		this.url = url;
		var obj = this;
		this.source.onchange = function() {
			new Ajax.Request(
				'http://www.sisnav.com/index.php' + obj.url + '/' + this.options[this.selectedIndex].value, 
				{
					asynchronous:true, evalScripts:false,
					onComplete:function(request, json){
						obj.clearSelect(obj.target);
						//var data = request.responseText;
						//var json = data.evalJSON();
						for(var i = 0; i < json.length; i++) {
							obj.addOption(obj.target, json[i].text, json[i].value);
						}
					}
				}
			);
		}
		if(sourceUrl != null) {
			new Ajax.Request(
				'http://www.sisnav.com/index.php' + sourceUrl, 
				{
					asynchronous:true, evalScripts:false,
					onComplete:function(request, json){
						obj.clearSelect(obj.source);
						//var data = request.responseText;
						//var json = data.evalJSON();
						for(var i = 0; i < json.length; i++) {
							obj.addOption(obj.source, json[i].text, json[i].value);
						}
						
						if(sourceId != '' && targetId != '') {
						obj.setSelectedValue(obj.source, sourceId);
						new Ajax.Request(
							'http://www.sisnav.com/index.php' + obj.url + '/' + obj.source.options[obj.source.selectedIndex].value, 
							{
								asynchronous:true, evalScripts:false,
								onComplete:function(request, json){
									obj.clearSelect(obj.target);
									//var data = request.responseText;
									//var json = data.evalJSON();
									for(var i = 0; i < json.length; i++) {
										obj.addOption(obj.target, json[i].text, json[i].value);
									}
									obj.setSelectedValue(obj.target, targetId);
								}
							}
						);
					}
					}
				}
			);
		}
  	},
	say: function() {
		return this.source.options[this.source.selectedIndex].value;
	},
	clearSelect: function(select) {
		var i;
		for(i=select.options.length-1;i>=0;i--)
		{
			select.remove(i);
		}
	},
	addOption: function(select, text, value) {
		var optn = document.createElement("OPTION");
		optn.text = text;
		optn.value = value;
		select.options.add(optn);
	},
	setSelectedValue: function(select, value) {
		for(i=0; i<select.length; i++) {
  			if(select.options[i].value == value) {
     			select.options[i].selected = true;
			}
		}	
	}
};