$(document).ready( function() {
	$("#refresh").click( updateCode )

	$("form.contact").submit( function() {
		var button = $("#submitbutton");
		
		// Saving button text for further restoration.		
		var buttonText = button.attr( "value" );

		// Temporary disabling button to prevent multiple submissions.
		button.attr( "value", "Отправка..." );		
		button.attr( "disabled", "disabled" );
		button.addClass( "disabled" );

		// Performing a post request using form data.
		$.post( $(this).attr( "action" ), 
			{
				'ajax': '',
				'name': $("#name").attr( "value" ),
				'email': $("#email").attr( "value" ),
				'subject': $("#subject").attr( "value" ),
				'message': $("#message").val(),
				'code': $("#code").attr( "value" )
			},
			function( data ) {
				// Checking result.
				var result = $(data).find( "Message" ).attr( "type" );
				var msgTitle = $(data).find( "Message" ).find( "Title" ).text();
				var msgText = $(data).find( "Message" ).find( "Text" ).text();
				
				// Setting dialog options.
				var dialogOptions = {
					bgiframe: true,
					modal: true,
					draggable: false,
					resizable: false,
					buttons: {
						Ok: function() {
							$(this).dialog( 'destroy' );
						}
					},
					dialogClass: 'dialogWindow',
					title: msgTitle
				}
				
				// Checking the request result.
				if ( "ok" == result ) {
					// Clearing the fields' decorations and error messages.
					$("form.contact input, textarea").removeClass( "valid" ).removeClass( "invalid" );
					$("form.contact label.error").remove();
				
					// Showing dialog.
					$("form.contact").after( "<div id=\"window\">" + msgText + "</div>" );
					$("#window").dialog( dialogOptions );
				}
				else if ( "error" == result ) {
					// Looping through the list of invalid fields in the result XML report.
					$(data).find( "ErrorFields" ).find( "Field" ).each( function() {
						var field = $("#" + $(this).text());
						
						// Adding CSS decoration.
						field.removeClass( "valid" ).addClass( "invalid" );
						
						// Adding or replacing error message.
						if ( !field.next().is( "label.error" ) ) {
							field.after( "<label class=\"error\">" + $(this).attr( "error" ) + "</label>" );
						} else {
							field.next().text( $(this).attr( "error" ) );
						}
					});
					
					// Looping through the list of valid fields.
					$(data).find( "CorrectFields" ).find( "Field" ).each( function() {
						var field = $("#" + $(this).text());
						
						// Setting right CSS decoration.
						field.removeClass( "invalid" ).addClass( "valid" );
						
						// Removing an error message if there is any.
						if ( field.next().is( "label.error" ) ) {
							field.next().remove();
						}
					});
					
					// Showing dialog.
					$("form.contact").after( "<div id=\"window\">" + msgText + "</div>" );
					$("#window").dialog( dialogOptions );
				}
				
				// Enabling button.
				button.attr( "value", buttonText );
				button.removeAttr( "disabled" );
				button.removeClass( "disabled" );
				
				// Updating control code.
				updateCode();
				
				// Emptying control code field and disabling its decorations.
				$("#code").attr( "value", "" );
				$("#code").removeClass( "valid" );
			});
		
		// Disabling page reloading.
		return false;
	});
});

// Updating control code function.
function updateCode() {
	var scriptUrl = "/captcha/securimage_show.php?";
	
	$("img.code").attr( "src", scriptUrl + Math.random() );
	
	return false;
};
