/*
    Copyright � 2006, 2007 Positive Friends Inc.
    All Rights Reserved.
*/

// If we have jQuery loaded
if (typeof(jQuery) != 'undefined'){

    // This is the last username we checked
    var userChecked = '';

    // This is the last email address we checked
    var emailChecked = '';

    // Set defaults for the validator
    $.validator.setDefaults({

        // Make sure we check elements when we lose focus
        event: 'blur',

        // Make sure we do not focus elements after they are proven erroneous
        focusInvalid: false,

        // This is the error class we will be using for the form fields
        errorClass: 'fieldError',

        // This is what we do when a field has been filled successfully
        success: function(label) {

            // Hide the eroneous label
            label.hide();
        }
    });

    // When the DOM is ready for us
    $().ready(function(){

        // This is a custom validation rule to make sure the username requested is available
        $.validator.addMethod('available', function(value){

            // If the username we want to check was NOT the last checked
            if (userChecked != value){

                // Build the content object we will be sending
                var contentObj = {

                    // The query type is to check available login names
                    queryType: 'login',

                    // The query string is the username we want to check
                    queryString: value
                };

                // Make an AJAX request
                $.getJSON('/ajax/join/checkAvailable.ajax', contentObj, function(json){
										
                    // If this username is not available
                    if (json.result.toUpperCase() != 'Y'){

                        // Add the field error class to the field
                        $('#login').addClass('fieldError');

                        // Force the validation stuff to highlight the field and send the error
                        validator.showErrors({'login': 'The username you chose is taken'});
                    }
                });

                // Build the content object we will be sending
                var contentObj = {

                    // The query type is to check available display names
                    queryType: 'displayName',

                    // The query string is the username we want to check
                    queryString: value
                };

                // Make an AJAX request
                $.getJSON('/ajax/join/checkAvailable.ajax', contentObj, function(json){

                    // If this username is not available
                    if (json.result.toUpperCase() != 'Y'){

                        // Add the field error class to the field
                        $('#login').addClass('fieldError');

                        // Force the validation stuff to highlight the field and send the error
                        validator.showErrors({'login': 'The username you chose is taken'});
                    }
                });

                // Set the last checked user
                userChecked = value;
            }

            // Because of the async structure, and inability to hitch, we always return success
            return true;

        });

        // This is a custom validation rule to make sure the email address hasn't been used
        $.validator.addMethod('emailused', function(value){

            // If the email address we want to check was NOT the last checked
            if (emailChecked != value){

                // Build the content object we will be sending
                var contentObj = {

                    // The query type is to check available email address
                    queryType: 'email',

                    // The query string is the username we want to check
                    queryString: value
                };

                // Make an AJAX request
                $.getJSON('/ajax/join/checkAvailable.ajax', contentObj, function(json){

                    // If this username is not available
                    if (json.result.toUpperCase() != 'Y'){

                        // Add the field error class to the field
                        $('#email').addClass('fieldError');

                        // Force the validation stuff to highlight the field and send the error
                        validator.showErrors({'email': 'This email address is already taken'});
                    }
                });


                // Set the last checked email address
                emailChecked = value;
            }

            // Because of the async structure, and inability to hitch, we always return success
            return true;

        });

        // This is a custom rule to make sure a password does not contain invalid characters
        $.validator.addMethod('password', function(value){

            // Create the regular expression object
            var regExp = /^[0-9a-zA-Z]+$/;

            // If we get a match to the expression
            if (value.match(regExp)){

                // Success
                return true;

            } else {

                // Failure
                return false;
            }
        });

        // Make sure the entire registration from gets validated on submit
        var validator = $("#regSubmit").validate({

            /*// This is what we do once we submit the form
            submitHandler: function(form){

                // If we have no condition checked off
                if ($('#hpv').attr('checked') != true && $('#herpes').attr('checked') != true && $('#hepatitisa').attr('checked') != true && $('#hepatitisb').attr('checked') != true && $('#hepatitisc').attr('checked') != true && $('#hiv').attr('checked') != true && $('#other').attr('checked') != true){

                    // Fire and error through the validator
                    validator.showErrors({'herpes': 'Please check the conditions that apply to you'});

                    // Highlight the appropriate fields
                    $('/html/body/div/div[2]/form/fieldset[2] input[@type="checkbox"]').addClass('fieldError');

                    // Make sure that when the elements are clicked we remove the error class
                    $('/html/body/div/div[2]/form/fieldset[2] input[@type="checkbox"]').click(function(){

                        // If the user just checked us
                        if (this.checked == true){

                            // Then remove all the references to the fieldError
                            $('/html/body/div/div[2]/form/fieldset[2] input[@type="checkbox"]').removeClass('fieldError');

                            // Hide the error label
                            this.blur();
                        }
                    });

                } else {

                    // Submit the form
                    form.submit();
                }
            },*/

            // These are the rules we will be applying to the form
            rules: {

                // Username
                login: {
                    required: true,
                    minLength: 3,
                    maxLength: 20,
                    available: true
                },

                // Zip code
                zipcode: {
                    required: true,
                    minLength: 5,
                    maxLength: 5,
                    number: true
                },

                // The over 18 check box
                tos: {
                    required: true
                },

                // Password field
                passwd: {
                    minLength: 6,
                    maxLength: 20,
                    required: true,
                    password: true
                },

                // Password Confirmation
                confirmPasswd: {
                    minLength: 6,
                    maxLength: 20,
                    required: true,
                    password: true,
                    equalTo: '#passwd'
                },

                // Email Address
                email: {
                    required: true,
                    email: true,
                    emailused: true
                },

                // This is the security code the user needs to type in
                securityInputCode: {
                    required: true,
                    minLength: 3
                },

                // This is the text input the user enters what they are looking for into
                profileBasicInfo: {
                    required: true
                },

                // This field is only validated here when the tagline suggestions are not in use
                tagline: {
                    required: true
                }
            },

            // These are messages we will show for the various errors
            messages: {

                // Username
                login: {
                    required: 'You need to enter in your login name',
                    minLength: 'The login must be at least 6 characters',
                    maxLength: 'The login must be at most 20 characters',
                    available: 'The username you chose is taken'
                },

                // Zip code
                zipcode: {
                    required: 'You need to enter in your zip code',
                    minLength: 'Your zip code is not long enough',
                    maxLength: 'Your zip code is too long',
                    number: 'Your zip code contains invalid characters'
                },

                // Over 18 checkbox
                tos: {
                    required: 'You must confirm you are 18 and agree to PositiveFriends.com\'s privacy policy and terms of use to continue'
                },

                // Password field
                passwd: {
                    required: 'Please enter a password between 6 and 20 characters',
                    minLength: 'Your password must be at least 6 characters',
                    maxLength: 'Your password must be at most 20 characters',
                    password: 'Your password can only contain alpha-numeric characters'
                },

                // Password Confirmation
                confirmPasswd: {
                    required: 'You must confirm your password to continue',
                    minLength: 'Your confirmation password must be at least 6 characters',
                    maxLength: 'Your confirmation password must be at most 20 characters',
                    password: 'Your confirmation password can only contain alpha-numeric characters',
                    equalTo: 'Your password and password confirmation don\'t match'
                },

                // This is the email address
                email: {
                    required: 'Please enter an email address',
                    email: 'That is not a valid email address'
                },

                // This is the security code the user needs to type in
                securityInputCode: {
                    required: 'Please type in the phrase in the image box',
                    minLength: 'The phrase you typed is too short'
                },

                // This is the text input the user enters what they are looking for into
                profileBasicInfo: {
                    required: 'Please tell us more about you and what you are looking for'
                },

                // This field is only validated here when the tagline suggestions are not in use
                tagline: {
                    required: 'Please give yourself a tagline'
                }
            }
        });
    });
}
