// JavaScript Document

function validateFieldset(id) {
	var fail = false;
	$('#set-' + id + ' .required input, #set-' + id + ' .required select').each(function () {
		if ($(this).val() != null && $(this).val != '' && $(this).val() != '0' && ($(this).hasClass('error') && $(this).attr('type') != 'email')) {
			$(this).removeClass('error').addClass('success');
		}

		if ($(this).val() == null || $(this).val() == '' || $(this).val() == '0') {
			$(this).addClass('error');
			fail = true;
		} else {
			if ($(this).attr('type') == 'email') {
				if ($(this).hasClass('error')) $(this).removeClass('error');
				var x = $(this).val();
				var atpos = x.indexOf('@');
				var dotpos = x.lastIndexOf('.');
				if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= x.length) {
					$(this).addClass('notice');
					fail = true;
				} else {
					$(this).addClass('success');
				}
			}
		}
	});
	if (!fail) {
		if (id == 1) {
			$('#set-1').fadeOut(500, function () {
				$('#set-2').fadeIn(500);
			});
		} else {
			$('#set-2').fadeOut(500, function () {
				$('#set-1').fadeIn(500);
			});
		}
	} else {
		$('h2#notification').text('Please fill in the required fields');
	}
}

function formSubmit(event) {
   var fail = false
    $('.required input, .required select').each(function () {
        if ($(this).val() != null && $(this).val() != '' && $(this).val() != '0' && ($(this).hasClass('error') && $(this).attr('type') != 'email')) {
            $(this).removeClass('error').addClass('success');
        }

        if ($('#InfoIsCorrect input').prop('checked') == false) {
            $('#InfoIsCorrect input').addClass('error');
            fail = true;
        }

        if ($(this).val() == null || $(this).val() == '' || $(this).val() == '0') {
            $(this).addClass('error');
            fail = true;
        } else {
            if ($(this).attr('type') == 'email') {
                if ($(this).hasClass('error')) {
                    $(this).removeClass('error');
                }
                var x = $(this).val();
                var atpos = x.indexOf("@");
                var dotpos = x.lastIndexOf(".");
                if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= x.length) {
                    $(this).addClass('notice');
                    fail = true;
                } else {
                    $(this).addClass('success');
                }
            }
        }
    });
    if (fail) {
        $('#set-2').fadeOut(500, function () {
            $('#set-1').fadeIn(500);
            $('h2#notification').text('Please fill in the required fields');
        });
        event.preventDefault();
    }
    /* Depreciated due to AJAX not handling file uploads.
    if (!fail) {
        $.post('../../../wp-content/themes/recruiting/test.php', ($('form').serialize()), function (data) {
            $('#page-content').fadeOut(500, function () {
                $('#page-content').empty().append('<h1>Processing</h1><p>We are processing your request. Thank you for your patience.</p>' + data).fadeIn(500);
            });
            $.post('http://leaddata.naaleads.com/post.php', ($('form').serialize()), function () {
                $('#page-content').fadeOut(500, function () {
                    $('#page-content').empty().append('<h1>Success</h1><p>Thank you for your submission. Someone will contact you in the next 24 hours.</p>').fadeIn(500);
                });
            });
        });
    }
    */
} // formSubmit
