/* support.js
 *
 * Javascript code used by the support form.
 */

$(document).ready(function(){
				
	$("#fms-support-form").ajaxForm({            
		target: "",
		beforeSubmit: validateSupportForm,
		success     : onSupportSuccess
	});
});

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// This function will simply do the graphical logic, all validations are done on the server side.
//
// 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 validateSupportForm(formData, jqForm, options) {

	//get the form object
	var form = jqForm[0];
	var valid = true;
	
	//name required
	if(!form.support_name.value){
		$("#fms-support-form-name").addClass("error");
		valid = false;
	}
	
	//email required
	if(!form.support_email.value){
		$("#fms-support-form-email").addClass("error");
		valid = false;
	}
	
	//message required
	if(!form.support_msg.value){
		$("#fms-support-form-msg").addClass("error");
		valid = false;
	}

	if(valid){
		
		$("#fms-support-wrapper").block({ 
			message: '<div class="loading" style="width:100%; height:100%; background-color:#296060;"></div>', 
			css    : {
				width : '374px', 
				height: '480px',
				cursor: 'default',
				'background-color': '#296060'
			} 
		});	
	}

	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 onSupportSuccess(result){
	
	//parse the result so that we know if there was an error or everything went fine
	var str = result.substr(0, 5);	
	
	if(str == "false"){
		
		$(".blockMsg").html(result.substr(6, result.length));
	}
	else{
		$(".blockMsg").html(result);
		$(".blockMsg").slideDown('fast');
	}
}

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// This function will unblock the register form.
function unblockSupportForm(){
	$("#fms-support-wrapper").unblock();
}