/**
 * @author GeekTantra
 * @date 20 September 2009
 */
var ValidationState = "valid";

function _validate() { if ((jQuery('#username').val().match(/^[\w\d\_\.]{3,}$/)) && (jQuery('#username').val().length > 4)) return true; else return false; }

//see applications/is_username_available.ctp for the content (div id="username_not_avail") necessary to make this function work.
function _checkUsername(username, register) {
	if (username) {
		var _validation_state = eval(_validate());
	    if (_validation_state) {
			jQuery('#loadingDiv').css('display', 'block');
			jQuery.post('/applications/isUsernameAvailable/'+username,
			  	function(data){
			  		jQuery('#checkusername').html(data);
			  		if (jQuery('#username_not_avail').html() != null) {
				  		jQuery('#username').focus();
				  		jQuery('#username').select();
				  		jQuery('#submit').attr('disabled', 'disabled');
				  		jQuery('#submit').attr('value', 'Check Username Availability');
			  		} else {
		  				jQuery('#submit').removeAttr('disabled');
		  				jQuery('#submit').attr('value', register);
			  		}
			  		jQuery('#loadingDiv').hide();
			  	}
			);
		} else {
			jQuery("#checkusername").html("");
		}
	} else {
		jQuery('#username').focus();
	}

}

(function(jQuery){
    jQuery.fn.validate = function(options){
        options = jQuery.extend({
            expression: "return true;",
            message: "",
            error_class: "ValidationErrors",
            error_field_class: "clearfix error",
            scroll: false,
            live: true
        }, options);
        var SelfID = jQuery(this).attr("id");
        if (options['live']) {
            if (jQuery(this).find('input').length > 0) {
                jQuery(this).find('input').bind('blur', function(){
                    if (validate_field("#" + SelfID, options)) {
                        if (options.callback_success) 
                            options.callback_success(this);
                    }
                    else {
                        if (options.callback_failure) 
                            options.callback_failure(this);
                    }
                });
                jQuery(this).find('input').bind('focus keypress click', function(){
                    jQuery("#" + SelfID).next('.' + options['error_class']).remove();
                    jQuery("#" + SelfID).parent().removeClass(options['error_field_class']);
                });
            }
            else {
                jQuery(this).bind('blur', function(){
                    validate_field(this);
                });
                jQuery(this).bind('focus keypress', function(){
                    jQuery(this).next('.' + options['error_class']).fadeOut("fast", function(){
                        jQuery(this).remove();
                    });
                    jQuery(this).parent().removeClass(options['error_field_class']);
                });
            }
        }
        jQuery(this).parents("form").submit(function(e){
        	validate_field('#' + SelfID);
            if (ValidationState == "valid") 
                return true;
            else {
                jQuery('html, body').animate({scrollTop: '0px'}, 0);
            	return false;
            }
        });
        function validate_field(id){
            var self = jQuery(id).attr("id");
            var expression = 'function Validate(){' + options['expression'].replace(/VAL/g, 'jQuery(\'#' + self + '\').val()') + '} Validate()';
            var validation_state = eval(expression);
            if (!validation_state) {
                if (jQuery(id).next('.' + options['error_class']).length == 0) {
                    jQuery(id).after('<span class="' + options['error_class'] + '">' + options['message'] + '</span>');
                    jQuery(id).parent().addClass(options['error_field_class']);
                   // $.scrollTo('#'+self, {duration:500, margin: true, offset: -20});
                }
                ValidationState = "invalid";
                return false;
            }
            else {
                ValidationState = "valid";
                return true;
            }
        }
    };
})(jQuery);

