/**
 * jquery.formPopulate.js v0.1
 * 
 * This plugin will take an array as data source and
 * will try to find the matching fields in the html form then
 * will populate the fields with values from the data source
 * 
 * @author Yasar Bayar <yasar@navigatormm.com>
 */

(function($) {
	$.fn.formPopulate=function(_post){
		var _form = this;
		$.each(_post,function(key,val){
			if (val instanceof Array){
				$(val).each(function(){
					$('[value='+this+']', _form).attr('checked','checked');
				});
				return;
			}
			
			el = $('[name='+key+']', _form);
			if (! el) return;
			
			switch($(el).get(0).nodeName.toLowerCase()){
				case 'input':
					$(el).val(val);
				break;
				case 'select':
					$(el).children('option').each(function(){
						if($(this).val() == val) 
						$(this).attr('selected','selected');
					});
				break;
				case 'textarea':
					$(el).text(val);
				break;
			}
		});
	};
})(jQuery);