$(document).ready(function()
{
var game_options = {
	beforeSubmit:  preSubmit,  // pre-submit callback

	// other available options:
	url:       '/user_upload.php',         // override for form's 'action' attribute
	type:      'post',        // 'get' or 'post', override for form's 'method' attribute
	dataType:  'json',        // 'xml', 'script', or 'json' (expected server response type)
        iframe: true,

        success:       postSubmit,  // post-submit callback

	clearForm: true,        // clear all form fields after successful submit
	resetForm: true        // reset the form after successful submit
 };

 var contact_options = {
	beforeSubmit:  preContactSubmit,  // pre-submit callback

	// other available options:
	url:       '/contact.php',         // override for form's 'action' attribute
	type:      'post',        // 'get' or 'post', override for form's 'method' attribute
	dataType:  'json',        // 'xml', 'script', or 'json' (expected server response type)
        iframe: false,

        success:       postContactSubmit,  // post-submit callback

	clearForm: true,        // clear all form fields after successful submit
	resetForm: true        // reset the form after successful submit
 };


$('#game_userForm').ajaxForm(game_options);
$('#contact_userForm').ajaxForm(contact_options);
});

function preSubmit(formData, jqForm, game_options)
{
    if(CheckUserForm())
    {
        $('#game_submit, #game_reset').fadeOut('normal', function()
        {
            $(this).hide();
            $('#ajax_loader').fadeIn('normal');
        });
        return true;
    }else{
        return false;
    }
}


function postSubmit(data)
{
    if(data.insert == 'ok')
    {
        $('#ajax_loader').fadeOut('normal', function()
        {
            $(this).hide();
            $('#game_submit, #game_reset').show().fadeIn('normal');
            alert('Your game was successfully uploaded! You should receive a notification email very shortly! Thank you!');
            return true;
        });
    }
    if(data.insert == 'not ok')
    {
       $('#ajax_loader').fadeOut('normal', function()
        {
            $(this).hide();
            $('#game_submit, #game_reset').show().fadeIn('normal');
            alert('Your game was not uploaded! Please try again!');
            return false;
        });
    }
}

function preContactSubmit(formData, jqForm, contact_options)
{
    if(CheckContactForm())
    {
        $('#contact_submit, #contact_reset').fadeOut('normal', function()
        {
            $(this).hide();
            $('#ajax_loader').fadeIn('normal');
        });
        return true;
    }else{
        return false;
    }
}


function postContactSubmit(data)
{
    if(data.message == 'ok')
    {
        $('#ajax_loader').fadeOut('normal', function()
        {
            $(this).hide();
            $('#contact_submit, #contact_reset').show().fadeIn('normal');
            alert('Your message was successfully sent! We will try our best to go over your email and get back to you as fast as we can! Thanks!');
            return true;
        });
    }
    
     if(data.message == 'notOk')
    {
        $('#ajax_loader').fadeOut('normal', function()
        {
            $(this).hide();
            $('#contact_submit, #contact_reset').show().fadeIn('normal');
            alert('Your message was not sent! Please try again in a few moments! Thank you!');
            return true;
        });
    }

}


