$(function() {

	//$(".msg").hide();

	/*$(".textfield, .dropdown").bind("focus blur", function(){

		$(this).nextAll('.msg').toggle("medium"); 

    });*/

	if (typeof focus_field == 'undefined') {
		
		$("#realname").focus();
	
	} else {
		
		$("#"+focus_field).focus();
	
	}

	// validate signup form on keyup and submit 

    var validator = $("#signupform").validate({ 

        rules: { 

            realname: { 

                required: true, 

                minlength: 2 

            },  

            username: { 

                required: true, 

                minlength: 2,

				alphanumeric: true,

                remote: baseurl+"/content/ajax/ajax_check_username.php" 

            }, 

            password: { 

                required: true, 

                minlength: 6 

            }, 

            email: { 

                required: true, 

                email: true

                //remote: "emails.php" 

            }

        }, 

        messages: { 

            realname: { 

                required: "You do know your name, right?", 

                minlength: jQuery.format("We really need at least {0} characters")

            },

            username: { 

                required: "Please choose a username", 

                minlength: jQuery.format("We really need at least {0} characters"), 

                remote: jQuery.format("Sorry - {0} is already in use") 

            }, 

            password: { 

                required: "Yep, we checked, you didn't enter a password", 

                rangelength: jQuery.format("We really need at least {0} characters") 

            }, 

            email: { 

                required: "Please enter a valid email address", 

                minlength: "Please enter a valid email address", 

                remote: jQuery.format("{0} is already in use") 

            }

        }, 

        // the errorPlacement has to take the table layout into account 

        errorPlacement: function(error, element) { 

            if ( element.is(":radio") ) 

                error.appendTo( element.parent().next() ); 

            else if ( element.is(":checkbox") ) 

                error.appendTo ( element.next() ); 

            else 

				// original Wooshii code - placed to right of input fields: element.parent().after(error); 

				// this code leaves the small instruction text: error.prependTo ( element.next("small") ); 

				element.next("small").html(error);

        }, 

        errorLabelContainer: "div.formfield clearfix",

        wrapper: "div.description",



		/*

        // specifying a submitHandler prevents the default submit, good for the demo 

        submitHandler: function() { 

            alert("submitted!"); 

        }, */

        // set this class to error-labels to indicate valid fields 

        success: function(label) { 

            // set   as text for IE 

            label.html(" ").addClass("checked"); 

        } 

    }); 



    

    // propose username 

    $("#username").focus(function() { 

        var realname = $("#realname").val(); 

        if(realname && !this.value) { 

            this.value = realname.replace (/\W/g, ''); 

        } 

    }); 

	jQuery.validator.addMethod("alphanumeric", function(value, element) {

	return this.optional(element) || /^[\w-]+$/i.test(value);

	}, "Letters, numbers, hyphens and underscores only please");  

	

	// tooltips

	// select all desired input fields and attach tooltips to them

	$(".help").tooltip({



		// place tooltip on the right edge

		position: "center right",

	

		// a little tweaking of the position

		offset: [-8, 40],

	

		// use the build-in fadeIn/fadeOut effect

		effect: "fade",

		

		// custom opacity setting

		opacity: 0.7,

		

		// use this single tooltip element

		tip: '.tooltip'

		

	});

 });

 
