// <![CDATA[
/**
 * dependency of this plugin:
 *
 * //@var global string http_root
 * //@var global string rp_global_image
 * //@var global string img_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_request_reset_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){
				
				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 form_data = object.find('form').serialize();
				
				var top = o.top;
				var width = o.width;

				// Keep the lines below for checking.
				//alert(form_data);
				//alert(target_postpath);
				//alert($cm.selector);
				//var margin_left = "-"+ ((scroll_left + width)/2);
				//alert(o.textEditor);
				
				// Processing. 
				$("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn(1000);
				
				// 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', 'disabled').css({opacity:0.4});
				
				// Post the form.
				// You must serialise the form after tinyMCE.triggerSave() if tiny mce is in use. 
				$.post(target_postpath, object.find('form').serialize(),function(xml){
					
					// Remove any previous error and the popup processing
					$('fieldset',$cm.selector).removeClass();
					
					// Procees the form output with the callback function.
					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');
					
					// Append the message to the .respond-message class
					$("#msgbox").fadeTo(200,0.1,function(){ 
						$(this).html(message).addClass('messageboxerror').fadeTo(900,1); 
					});	
					
				});
				
				$("securityError", xml).each(function(){

					// Set the local variable.
					var target_security = $('#popup-reset-security');
					var elementid = $(this).attr('elementid');
					var message = $(this).attr('message');
					//alert(message);
					
					// Add an error class to each error element and change the html text in each span.
					$("#"+elementid+"_label",target_security).addClass('error');
					$("#"+elementid+"_label",target_security).parentsUntil('form').addClass('error');
					$("#"+elementid+"_label span",target_security).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").parentsUntil('form').removeClass('error');
					$("#"+elementid+"_label span").html(message);
					
				});
				
				// If error found for email.
				if($(xml).find("error").length > 0)
				{
					// Attach other functions and plugins.
					$('form *[title]').inputHint();
					
					// Enable 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', '').css({opacity:1.0});
				}
				
				// If email has passed and now on the security checking stage
				else if($(xml).find("security").length > 0)
				{
					//alert('security stage');
					
					// Append a popup for displaying the security checking form.
					$(document.body).append("<div id='popup-reset-security' 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 id='form-reset-security' class='form-reset form'></div></div></div></div>");
					
					// Set the target's value and set the target's width.
					var target_security = $('#popup-reset-security').width(400);
					
					// Set the popup css and hide it first.
					target_security.css({
						display: 'none'
					});
					
					// Set the local variable.
					var message = $(xml).find("security").attr('message');
					var path = $(xml).find("security").attr('path');
					//alert(path);
					
					// Append the message to the .respond-message class.
					$("#msgbox").fadeOut('fast',function(){ 
						
						// Append the message to the .respond-message class.
						$(".respond-message").append(message);

						// Set the popup to the center of the page.
						set_center(target_security);
						
					});
					
					// Load the confirmation form and attach the postForm_confirm() function.
					$("#form-reset-security",target_security).load(http_root+path, {}, function(){
						
						// Put the input hint in the form.
						$('form *[title]').inputHint();
						
						// Post the security form.
						post_security(target_security);
						
					});
				}
				
				// If error found for security.
				else if($(xml).find("securityError").length > 0)
				{
					var target_security = $('#popup-reset-security');
					//alert('error'); 
					
					// Enable the submit button again after processing the xml output.	
					$('input[type=submit]',target_security).attr('disabled', '').css({opacity:1});
					
					// Put the input hint back in.
					$('form *[title]',target_security).inputHint();
				}
				
				// If security stage has passed and now on the confirmation stage.
				else if($(xml).find("confirmation").length > 0)
				{
					//alert('confirmation stage');
					
					// Remove the security popup and its form.
					$('#popup-reset-security').remove();
					
					// Append a popup for displaying the security checking form.
					$(document.body).append("<div id='popup-reset-confirmation' 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 id='form-reset-confirmation' class='form-reset form'></div></div></div></div>");
					
					// Set the target's value and set the target's width.
					var target_confirmation = $('#popup-reset-confirmation').width(400);
					
					// Set the popup css and hide it first.
					target_confirmation.css({
						display: 'none'
					});
					
					
					// Set the local variable.
					var message = $(xml).find("confirmation").attr('message');
					var path = $(xml).find("confirmation").attr('path');
					//alert(path);
					
					// Append the message to the .respond-message class.
					$(".respond-message").append(message);
					
					// Load the confirmation form and attach the postForm_confirm() function.
					$("#form-reset-confirmation",target_confirmation).load(http_root+path, {}, function(){
						
						// Set the popup to the center of the page.
						set_center(target_confirmation);
						
						target_confirmation.fadeIn('fast', function(){
							
							// Post the confirmation.
							post_conifrmation(target_confirmation);
						});
					});
				}
				
				// 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);
					
					// Fade in the result.
					set_center(target_result);
					
					// Set the popup to the center of the page.
					target_result.fadeIn('fast',function(){
						
						// Fade out the request form then remove it.
						$($cm.selector).fadeOut('fast', function(){ 
							$(this).remove();
						});
					
					});

				}
							
				
					
			}
			
			
			function post_security(target_security)
			{
	
				$("input[name=submit]",target_security).click(function(){
				
					$("form",target_security).submit(function(){
						
						// Remove the error classes and remove popup-respond.
						$('label').removeClass('error');
						
						// Unbind the submit otherwise you will attach the submit each time when you click.
						$("form",target_security).unbind('submit');
						//$("form",target_security).unbind('click');
						
						var path_return = $(this).attr('action');
						var processing = $('form .processing',target_security);
						//alert(path_return);
						
						// Set the css for processing.
						processing.css({
							margin:"5px 0px 0px 10px"
						});
						
						// add text to the processing div and animate it.
						processing.html('<img src="'+http_root+rp_global_image+img_loader+'"/> processing').show();
						
						// Serialise the email input from the request form.
						// Serialise the security input from the security form
						var str_1 = $('form',$cm.selector).serialize();
						var str_2 = $("form",target_security).serialize();
						var str_total = str_1+'&'+str_2;
						
						$.post(path_return, str_total,function(xml){
							
							process_posted_reset_account(xml);
							processing.hide();
						});
						
						
						// Disable the submit button so that you won't click it twice while the ajax is processing the form.
						$('input[type=submit]',target_security).attr('disabled', 'disabled').css({opacity:0.4});
						
						return false;
					});
				});	
			
			}
			
			
			function post_conifrmation(target_confirmation)
			{
				$("input[name=yes]",target_confirmation).click(function(){
				
					$("form",target_confirmation).submit(function(){
						
						// Unbind the submit otherwise you will attach the submit each time when you click.
						$("form",target_confirmation).unbind('submit');
						
						var path_return = $(this).attr('action');
						var processing = $('form .processing',target_confirmation);
						//alert(path_return);
						
						// Set the css for processing.
						processing.css({
							margin:"5px 0px 0px 10px"
						});
						
						// Add text to the processing div and animate it.
						processing.html('<img src="'+http_root+rp_global_image+img_loader+'"/> processing');
								
						$.post(path_return, $('form',$cm.selector).serialize(),function(xml){
							
							target_confirmation.remove();
							process_posted_reset_account(xml);
							clear_form($('form',$cm.selector));
							
						});
						
						// Disable the submit button so that you won't click it twice while the ajax is processing the form.s
						$('input[type=submit]',target_confirmation).attr('disabled', 'disabled').css({opacity:0.4});
						
						return false;
					});
				});	
				
				$("input[name=no]",target_confirmation).click(function(){
				
					target_confirmation.fadeOut('fast', function(){
							
						// Remove confirmation popup.
						// Clear the request form.
						target_confirmation.remove();
						clear_form($('form',$cm.selector));
						
						// Enable the submit button again after processing the xml output.
						$('input[type=submit]',$('form',$cm.selector)).attr('disabled', '').css({opacity:1});
						
					});
					
					$("form",target_confirmation).submit(function(){
						return false;
					})	
				});
			
			}
			
			
			
        }
    });
     
//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);

// ]]>
