/*

ctrlEnter plugin v0.1
Prevents submitting mistakenly a form by blocking enter,
adds submit hotkey Ctrl+Enter.


 http://www.therror.com/html/ctrlenter


*/
(function($){
	$.fn.ctrlenter = function() {
		var keyList = new Array();

		$(this).find("input, select").keypress(function(k){
			if(keyList.findInArray(17) && keyList.findInArray(13)){
				$(this).parents("form:eq(0)").submit();
			}
			if(k.keyCode==13){
				if($(this).parents("form").find(".ctrlenter-blocked").length)
					$(this).parents("form").find(".ctrlenter-blocked").stop().slideDown(500, function(){
						$(this).animate({opacity: 1}, 7500, function(){ $(this).slideUp(400); });
					});
				return false;
			}
		});
		
		// tratamiento especial a <textarea>
		$(this).find("textarea").keypress(function(k){
			if(keyList.findInArray(17) && keyList.findInArray(13)){
				$(this).parents("form:eq(0)").submit();
			}
		});
		
		$(this).find("input, select, textarea").keyup(function(k){
			keyList.splice(keyList.posInArray(k.keyCode), 1);
		});
		
		$(this).find("input, select, textarea").keydown(function(k){
			if(!keyList.findInArray(k.keyCode)){
				keyList.push(k.keyCode);
			}
		});
		
		$(this).find("input[type=submit]").unbind('keypress');
	};
	$(document).ready(function(){
		$("form.ctrlenter").ctrlenter();
	});
	Array.prototype.posInArray = function(a){
		var arreglo = this;
		try{ return this.indexOf(a); }
		catch(err){
			for(var i in arreglo){
				if(arreglo[i]==a){
					return i;
				}
			}
			return -1;
		}
	};
	Array.prototype.findInArray = function(a){
		return this.posInArray(a)==-1 ? false : true;
	};
})(jQuery);
