/**
 * sites.js
 * JQuery code needed to interact with the send pwd form.
 */

// $Id$

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// define the behavior of the register form.
$(document).ready(function(){

	$("#fms-form-send-pwd").ajaxForm({            
		target: "",
		beforeSubmit: validateSendPwdForm,
		success     : onSendPwdSuccess
	});
});

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// This function will make sure that the username field is not empty and do the graphical logic.
//
// formData:
// formData is an array of objects representing the name and value of each field 
// that will be sent to the server;  it takes the following form: 
// 
// [ 
//     { name:  username, value: valueOfUsernameInput }, 
//     { name:  password, value: valueOfPasswordInput } 
// ] 
//
// JqForm:
// jqForm is a jQuery object which wraps the form DOM element 
//
// options:
// this is the Options Object passed into ajaxForm/ajaxSubmit
function validateSendPwdForm(formData, jqForm, options) {

	//get the form object
	var form = jqForm[0];
	var valid = true;
	
	//email required
	if(!form.username.value){
		$("#fms-form-send-pwd-username").addClass("error");
		valid = false;
	}
	
	//everything is good, show the throbber and proceed to the core operation
	if(valid){
		$("#fms-form-send-pwd-wrapper").fadeOut('fast', function(){
			$("#fms-send-pwd-wrapper").addClass("loading"); 
		});
	}

	return valid;
}

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// This function will process the returning value of the submitions to see if the operation
// was successfull or not.
//
// result:
// the responseText or responseXML value (depending on the value of the dataType option).
function onSendPwdSuccess(result){
	
	//parse the result so that we know if there was an error or everything went fine
	var str = result.substr(0, 5);
	
	$("#fms-send-pwd-wrapper").removeClass("loading");
	
	if(str == "false"){
	
		//get the incorrect field
		var idx = result.indexOf("=");
		
		//check if the resulting error indicates a field. [example: false field_with_error=error_message]
		if(idx != -1){
			var field = result.substr(6, idx-6);
			alert(result.substr(idx+1, result.length));
			$("[name=" + field + "]").addClass("error");
		}else{
			alert(result.substr(6, result.length));
		}

		//graphical logic
		$("#fms-form-send-pwd-wrapper").fadeIn('fast');
	}
	else{
		alert(result);
		showLoginForm();
	}
}

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// Simple function used to hide the password retrieval form and show the login form.
function showLoginForm(){
	
	$("#fms-form-send-pwd-wrapper").fadeOut('fast', function(){
		$("#fms-form-login-wrapper").fadeIn('fast');
	});
}

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// Simple function used to hide the login form and show the password retrieval form.
function showSendPwdForm(){
	
	$("#fms-form-login-wrapper").fadeOut('fast', function(){
		$("#fms-form-send-pwd-wrapper").fadeIn('fast');
	});
}