$.fn.extend({ reset: function() {
    return this.each(function() {
        $(this).is('form') && this.reset();
    });
} });

function pp(val) {
	if(typeof(val)=="object" || typeof(val)=="array") {
		for (var i in val) {
			console.log(i+" : "+val[i]);
		}
	} else {
		console.log(val);
	}
}


var ajax = {
	
	form : {
		
		// supply form id, controller/action, callback
		// gets data from the controller action and
		// appends it to the form.
		
		open : function(form_id,cont_action,callback,options) {
			
			// remove notices
			$('.notice').remove();
			
			//$('form').not('#'+form_id).hide();
			
			var url = absPath+cont_action;
			ajax.form.open_url(form_id,url,callback,options);
			
		}, // end open
		
		open_url : function(form_id,url,callback,options) {
			
			options = (options) ? options : {}
			
			if ($('#'+form_id+' input[type=submit]').length < 1) {
			
				$.get(url,function(data){
					
					$(data).appendTo("#"+form_id);
					
					$('input[type=password]:eq(0)',$('#'+form_id)).pstrength();
					
					if (typeof(callback)=="function") { callback(data); }
					
					$('#'+form_id).show("slow")
					
				});
			
			} else {
				
				if (options.getdata) {
				
					var form = $("#"+form_id);
					url += "?dataonly=true";
					
					$.getJSON(url,function(json){
	
						for (var key in json) {
							
							$("*[name="+key+"]").each(function(){
								
								if ($(this).is("span")) {
									
									$(this).text(json[key]);
									
								} else if ($(this).is("input[type=password]")) {
									
									$(this).val('');
								
								} else {
									
									$(this).val(json[key]);
									
								}
								
							});
							
						}
						
						if (typeof(callback)=="function") { callback(data); }
						$('#'+form_id).show("slow");
						
					});
				
				} else {
					
					$('.pstrength-bar').add('.pstrength-info').hide();
					$('#'+form_id).reset().show("slow");
					
				}
								
			}
						
		}, // end open_url
		
		check : function(who) {
						
			var err = '';
			$('input.required, select.required, textarea.required',who).each(function(){
								
				var val = $.trim($(this).val());
				if (val.length<1) {
					err += "-- "+$(this).attr('rel')+"\n";
				}
				
			});
			
			return (err) ? err : false;
			
		}, // end check
	
		
		cancel : function(form) {
			
			var form = ($(form).is('form')) ? form : $(form).parent('div').parent("form");	
			
			$('input[type=hidden].tempvalue').remove();
			
			$("input,select,textarea",form)
				.not(":hidden")
				.not(":submit")
				.val('');
				
			$('.pstrength-bar').add('.pstrength-info').hide();
			$(form).hide('slow');
		
		}, // end cancel
		
		
		tempvalue : function(form,name,value) {
			
			$("<input type='hidden' name='"+name+"' value='"+value+"' class='tempvalue'>")
				.appendTo(form);
			
		},		
		
		submit : function(form,callback,type) {
			
			// hide the flash
			var form_id = $(form).attr('id');
			var msg_id = form_id + '_msg';
			$("#"+msg_id).add('.notice').hide();
			
			// check the form
			var err = ajax.form.check(form);
			if (err) {
				alert("Please enter data into the required fields:\n\n"+err);
				return false;
			}
			
			var type   = (type) ? type : 'json';
			var action = $(form).attr("action");
			var data   = $(form).serialize();
			
			$.post(action,data,function(data){
				
				if(typeof(callback)=="function") {
					
					return callback(data);
					
				} else {
					
					return data;
					
				}
				
				$('input[type=hidden].tempvalue').remove();
				
			},type);
			
		}, // end submit
		
		
		flash : function(form,msg,classes) {
			
			var form_id = $(form).attr('id');
			var msg_id = form_id + '_msg';
			
			if ($(msg_id).length<1) {
				
				$("<div id='"+msg_id+"' class='notice "+classes+"'>"+msg+"</div>").insertAfter(form).show();
				
			} else {
				
				$("#"+msg_id).text(msg).show();
				
			}
			
			
		},
		
		
		
		flash2 : function(form,flash) {
						
			$(".notice").remove();
			$(flash).insertAfter(form).show();
			//window.location.hash = "notice";
			
		}
		
		
	
	} // end form
	
} // end ajax

