/*
 * jquery.fillCombo - Filling linked combo made so easy
 * 
 * Copyright (c) 2007 Daniel Rosiles (http://www.daniel.rosiles.com.mx/blog)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * $LastChangedDate: 2009-09-18 16:11:00 +0100 (fri, 18 sept 2009) $
 *
 * Version: 1.0.0
 * Requires: jQuery v1.2.1+
 * 
 * Share or Remix it but please Attribute the authors.
 */

(function($){
	$.fn.linking = function(){
		var linked = $(this).attr("linked");
		var hijo = $(this).attr("hijo");
		var firstvalue = $(this).attr("firstvalue");

		if(linked == "true"){
			$.getJSON("./ajax.returnJson.php",{id: $(this).val(), hijo: hijo}, function(j){
				var options = '';
				/* options += '<option value="0">' + firstvalue + '</option>';
				
				for (var i = 0; i < j.length; i++) {
					options += '<option value="' + j[i].index + '">' + j[i].value + '</option>';
				}
				 */
				var child = document.getElementById('models');
				
				for (var i = 0; i < j.length; i++) {
					child.options[i]=new Option(j[i].value, j[i].index); 
				}
				//$('#' + hijo).html(options);
				$('#' + hijo + ' option:first').attr('selected', 'selected');
				$('#' + hijo).change();
			})
		}
	}
})(jQuery);

$(function(){
	$("select").change(function(){
		$(this).linking();
	})
});
