$(document).ready(function() {
    // We use the jQuery validator plugin to do input validation
    // http://docs.jquery.com/Plugins/Validation

    // call the validate method on our form, and pass in our explicit options
    $("#frmAdvertise").validate({
        onkeyup:false,
        rules: {
            first_name: {
                required:true,
                minlength:2,
                maxlength:100,
                validChars:true,
            },
            last_name: {
                required:true,
                minlength:2,
                maxlength:100,
                validChars:true,
            },
            email: {
                required:true,
                email:true,
                maxlength:255,
                emailCheck:false    // remote check for duplicate email address
            }
        },
        messages: {
            first_name: {
                required: "First Name is required.",
                minlength: jQuery.format("First Name must be at least {0} characters in length."),
                maxlength: jQuery.format("First Name can not exceed {0} characters in length."),
                validChars: "Please supply valid characters only.",
            },
            last_name: {
                required: "Last Name is required.",
                minlength: jQuery.format("Last Name must be at least {0} characters in length."),
                maxlength: jQuery.format("Last Name can not exceed {0} characters in length."),
                validChars: "Please supply valid characters only.",            },
            email: {
                required: "Email Address is required.",
                email: "Email Address must be valid.",
                maxlength: jQuery.format("Email Address can not exceed {0} characters in length."),
                emailCheck:"This Email Address is already in use."
            }
        }
    });

		// Limit characters for pet story to 250
		$('#message').keyup(function(){
			limitChars('message', 500, 'message_limit');
		})

});

// extend the validation plugin to do remote username and email dupe checking
jQuery.validator.addMethod('emailCheck', function(email) {
    var postURL = "/includes/json_email_check.php";
    $.ajax({
        cache:false,
        async:false,
        type: "POST",
        data: "email=" + email,
        url: postURL,
        success: function(msg) {
            result = (msg=='TRUE') ? true : false;
        }
    });
    return result;
}, '');

// check for unwanted characters
$.validator.addMethod('validChars', function (value) {
    var result = true;

    // unwanted characters
    var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";

    for (var i = 0; i < value.length; i++) {
        if (iChars.indexOf(value.charAt(i)) != -1) {
            return false;
        }
    }
    return result;
}, '');