jQuery(document).ready(function(){

	// multi-select for locations
	makeSublist('areacode','city', false);
	makeSublist('state','areacode', false);

});

// makeSublist - filters list by previous form field
// takes three options: parent ID, child ID, true/false to 
// select an item in the child ID, and item in the child ID  
function makeSublist(parent,child,isSubselectOptional,childVal){
	jQuery("body").append("<select style='display:none' id='"+parent+child+"'></select>");
	jQuery('#'+parent+child).html(jQuery("#"+child+" option"));
	
	var parentValue = jQuery('#'+parent).attr('value');
	jQuery('#'+child).html(jQuery("#"+parent+child+" .sub_"+parentValue).clone());
	
	childVal = (typeof childVal == "undefined")? "" : childVal ;
	jQuery("#"+child+' option[@value="'+ childVal +'"]').attr('selected','selected');
	
	jQuery('#'+parent).change( 
	function(){
		var parentValue = jQuery('#'+parent).attr('value');
		jQuery('#'+child).html(jQuery("#"+parent+child+" .sub_"+parentValue).clone());
		if(isSubselectOptional) 
		jQuery('#'+child).prepend("<option value='none'> — Select — </option>");
		jQuery('#'+child).trigger("change"); 
		jQuery('#'+child).focus();
	});
}	

