// <![CDATA[
/**
 * dependency of this plugin:
 *
 * //@var global string http_root
 * //@var global string rp_global_image
 * //@var global string image_loader
 * //@var global string rp_cms
 *
 * //@function close_popup()
 *
*/

// You need an anonymous function to wrap around your function to avoid conflict
(function($){
 
    // Attach this new method to jQuery
    $.fn.extend({ 
         
        // This is where you write your plugin's name
       post_form_account: function(options) {
		
			// Set the default values, use comma to separate the settings, example:
            var defaults = {
				top:			250, // The top of the proceesing popup and the result popup.
				width:			400 // The width of the proceesing popup and the result popup.
            }
                 
            var options =  $.extend(defaults, options);
			
			// return this.click(function(){ // original
			// "this" is already a jQuery object: 
			// When you create the click function you can assign that element to a variable and reference it within:
			var $cm = this.submit(function(e){
				
				// Remove the previous error class.
				$('label').removeClass('error');
				
				var o = options;
				var object = $(this);
				
				// Get the path from attribute of action in the form.
				var target_postpath = object.find('form').attr('action');
				var processing = $('.processing',$($cm.selector));
				var top = o.top;
				var width = o.width;
				
				// Keep the lines below for checking.
				//alert(form_data);
				//alert(form_data_2);
				//alert(target_postpath);
				//alert($cm.selector);
				//var margin_left = "-"+ ((scroll_left + width)/2);
				//alert(user_email);
				//alert(user_reset);
				//alert(path);

				// Set the css for processing.
				processing.css({
					margin:"5px 0px 0px 10px"
				});
				
				// Disable the submit button so that you won't click it twice while the ajax is processing the form.
				$('input[type=submit]',$($cm.selector)).attr('disabled', true).css({opacity:0.4});
				
				/* add text to the processing div and animate it */
				processing.html('<img src="'+http_root+rp_global_image+image_loader+'"/> processing').show();

				// Post the form .
				$.post(target_postpath,$(document).find('form').serialize(),function(xml){
					
					processing.hide();			
					process_posted_reset_account(xml);
					
				});
				
				return false;
				
			});
			
			// Callback function for proccesing the deleted item.
			function process_posted_reset_account(xml)
			{
	
				$("error", xml).each(function(){
			
					// Set the local variable.
					var elementid = $(this).attr('elementid');
					var message = $(this).attr('message');
					
					// Add an error class to each error element.
					$("#"+elementid+"_label").addClass('error');
					$("#"+elementid+"_label span").html(message);
					
				});
				
				$("securityError", xml).each(function(){
						
					// Set the local variable.
					var elementid = $(this).attr('elementid');
					var message = $(this).attr('message');
					//alert(category);
					
					// Add an error class to each error element and change the html text in each span.
					$("#"+elementid+"_label").addClass('error');
					$("#"+elementid+"_label span").html(message);
					
				});
				
				$("securityPassed", xml).each(function(){
						
					// Set the local variable.
					var elementid = $(this).attr('elementid');
					var message = $(this).attr('message');
					//alert(category);
					
					// Add an error class to each error element and change the html text in each span.
					$("#"+elementid+"_label span").html(message);
					
				});
				
				// If error found for email.
				if($(xml).find("error").length > 0)
				{
					//alert('error'); 
					/* enable the submit button again after processing the xml output */	
					$('input[type=submit]',$($cm.selector)).attr('disabled', false).css({opacity:1});
					
					/* put the input hint back in */
					$('form *[title]',$($cm.selector)).inputHint();
				}
				
				// If error found for security
				else if($(xml).find("securityError").length > 0)
				{
					//alert('error'); 
					// Enable the submit button again after processing the xml output.
					$('input[type=submit]',$($cm.selector)).attr('disabled', false).css({opacity:1});
					
					// Put the input hint back in.
					$('form *[title]',$($cm.selector)).inputHint();
				}
				
				// If security stage, and confirmation stage have passed, print the result.
				else if($(xml).find("result").length > 0)
				{	
					//alert('result');
					
					// Append a popup for displaying the security checking form.
					$(document.body).append("<div id='popup-reset-result' class='popup-outer popup-reset'><div class='popup-inner'><div class='close'><a href='#' class='button-close'>x close</a></div><div class='set-form'><div class='respond-message'></div></div></div></div>");
					
					// Set the target's value and set the target's width.
					var target_result = $('#popup-reset-result').width(400);
					
					// Set the popup css and hide it first.
					target_result.css({
						display: 'none'
					});
					
					$('.set-form',target_result).css({
						padding: '25px 30px 25px 30px'
					});
					
					// Set the local variable.
					var message = $(xml).find("result").attr('message');
					//alert(message);
					
					// Append the message to the .respond-message class.
					$(".respond-message").append("<img src='"+http_root+rp_global_image+"info.png' /> " + message);
					
					// Set the popup to the center of the page.
					//set_center(target_result);
						
					// Fade out the reset form and remove it.
					$($cm.selector).fadeOut('fast',function(){
					
						// Set the popup to the center of the page.
						set_center(target_result);
						
						// Fade in the result.
						target_result.fadeIn('fast');
						
						$(this).remove();
					});
						
	
				}
							
				
					
			}
			
			
        }
    });
     
//pass jQuery to the function, 
//So that we will able to use any valid Javascript variable name 
//to replace "$" SIGN. But, we'll stick to $ (I like dollar sign: ) )       
})(jQuery);

// ]]>
