var netm = netm == null ? {} : netm;
netm.Locations = function(formName, submitOnChange, strings, initialStates) {
	this.form = document.forms[formName];
	if (this.form == null) 
		this.form = document.getElementById(formName);
	
	if (this.form == null) return;
	
	this.submitOnChange = submitOnChange;
	if (typeof this.submitOnChange != 'boolean') this.submitOnChange = false;
	
	this.strings = netm.Locations.Strings;
	if (strings != null && typeof strings == 'object') {
		for (var key in strings) {
			this.strings[key] = strings[key];
		}
	}
	
	this.initialStates = initialStates;
	
	this.prepareSelects(0);	
	this.selectLocation('country', 0, true);
}

//default strings
netm.Locations.Strings = {
	'locations.select.country': 'Select Country',
	'locations.select.region': 'Select Region',
	'locations.select.prefecture': 'Select Prefecture',
	'locations.select.city': 'Select City',
	'locations.loading': 'Loading...'	
}

netm.Locations.Types = ['country', 'region', 'prefecture', 'city'];

netm.Locations.indexOfType = function(type) {
	var types = netm.Locations.Types;
	for (var i=0,len=types.length; i<len; i++) {
		if (types[i] == type) return i;
	}	
	
	return -1;
}

netm.Locations.prototype.prepareSelects = function(level) {
	var types = netm.Locations.Types;
	
	if (level == 0) this.registeredTypes = -1;
		
	for (var i=level,len=types.length; i<len; i++) {
		var select = this.form.elements[types[i]];
		if (select == null) continue;
		
		if (level == 0) this.registeredTypes++;
		
		select.options.length = 0;
		select.options[0] = new Option(this.strings['locations.select.' + types[i]], '0');
		select.disabled = true;
	}
}

netm.Locations.prototype.selectLocation = function(type, locid, doAjax) {
	var index = netm.Locations.indexOfType(type);
	if (type == -1) return;
	
	var strs = this.strings;
	var types = netm.Locations.Types;
	
	var select = this.form.elements[type];
	select.options[0].text = strs['locations.loading'];
	select.disabled = true;
	
	this.prepareSelects(index);	
	
	if (!doAjax) return;
	
	var performChange = !(index == this.registeredTypes);
	var _self = this;
	Ajax.request('locations_' + type + '.action', {
		parameters: {'locid': locid },
		callback: function(response) {
			if (response.responseText == null || response.responseText == '') return;
			var options = eval('(' + response.responseText + ')');
			if (options == null) return;
			
			var states = _self.initialStates;
			var len = options.length;
			select.options[0].text = strs['locations.select.' + type];
			if (len != 0) {
				var state = -1;
				var stateIndex = -1;
				if (states != null && states[index] != null) {
					state = states[index];
				}
				
				for(var i=0, j=1; i<len; i++, j++) {
					select.options[j] = new Option(options[i]['label'], options[i]['value']);
					
					if (state == options[i]['value']) {
						stateIndex = j;
					}
				}
				
				select.onchange = function(e) {
					if (_self.submitOnChange && e !== 'pbar' && select.value != 0) {
						for(var i=(index+1); i<types.length; i++) {
							var elem = _self.form.elements[types[i]];
							if (elem != null) elem.disabled = true;
						}
						
						select.form.submit();
					} else if (performChange) {
						_self.selectLocation(types[index+1], select.value, select.value != 0);
					} 
				}
				
				select.disabled = false;
				
				if (stateIndex != -1) {
					select.selectedIndex = stateIndex;
					select.onchange('pbar');
				}
			}
			
		}
	});
}

netm.Locations.create = function(formName, submitOnChange, strings, initialStates) {
	var handler = function() {
		new netm.Locations(formName, submitOnChange, strings, initialStates);
	}
	
	if (typeof window.addEventListener != "undefined")
		window.addEventListener("load", handler, false);
	else if (typeof window.attachEvent != "undefined") {
		window.attachEvent("onload", handler);
	} else {
		if (window.onload != null) {
			var oldOnload = window.onload;
			window.onload = function (e) {
				oldOnload(e);
				handler();
			};
		}
		else window.onload = handler;
	}
}
