// re-assigning some methods to change behaviour
jQuery.validator.setDefaults(
	{
		errorClass: "jsError",
		
		invalidHandler: function(e, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				var message = 'Sorry, we are unable to subimit the form. Please complete the following information:';
				jQuery(validator.currentForm).find("div.form-errors div").html(jQuery("<div>").append(message));
				jQuery(validator.currentForm).find("div.form-jsError-box").show();
			} else {
				jQuery(validator.currentForm).find("div.form-jsError-box").hide();
			}
		},
		showErrors: function() {
			for ( var i = 0; this.errorList[i]; i++ ) {
				var error = this.errorList[i];
				
				this.settings.highlight && this.settings.highlight.call( this, error.element, this.settings.errorClass, this.settings.validClass, error.message );
				this.showLabel( error.element, error.message );
			}
			if( this.errorList.length ) {
				this.toShow = this.toShow.add( this.containers );
			}
			if (this.settings.success) {
				for ( var i = 0; this.successList[i]; i++ ) {
					this.showLabel( this.successList[i] );
				}
			}
			if (this.settings.unhighlight) {
				for ( var i = 0, elements = this.validElements(); elements[i]; i++ ) {
					this.settings.unhighlight.call( this, elements[i], this.settings.errorClass, this.settings.validClass );
				}
			}
			this.toHide = this.toHide.not( this.toShow );
			this.hideErrors();
			
			this.addWrapper( this.toShow ).show();
			this.addWrapper( this.toShow ).parent().show();
			
		},
		highlight: function(element, errorClass, validClass, message) {
			name = jQuery(element).attr('name').replace('[','').replace(']','');
			el = jQuery("div."+name+"-box");
			if(el.length>0) {
				el.addClass(errorClass)
			}	
			else {
				el = jQuery(element).parent().children("select,input,textarea").addClass(errorClass);
			}
			
			
			el.parents('.field').find('label').addClass(errorClass+'-label');
			el.parents('.field').find('.jsErrorIcon').remove();
			
			
			fieldEl = el.parents('.field');
			iconEl = jQuery("<div>").addClass('jsErrorIcon');
			if(message!='') {
				iconEl.append(jQuery("<div>").addClass("jsErrorIcon-tooltip").html(message));
			}
			fieldEl.append(iconEl);
		},
		unhighlight: function( element, errorClass, validClass ) {
			name = jQuery(element).attr('name').replace('[','').replace(']','');
			el = jQuery("div."+name+"-box");
			if(el.length>0) {
				el.removeClass(errorClass).addClass(validClass);
			}	
			else {
				el = jQuery(element).removeClass(errorClass).addClass(validClass);
				
			}
			el.parents('.field').find('.jsErrorIcon').remove();
			el.parents('.field').find('label').removeClass(errorClass+'-label');
		},
		errorPlacement: function(error, element) {
			error.appendTo( element.parent().next() );
		}
		
	}
)

;(function($) {
	$.validator.prototype.showLabel = function(element, message) {
		var label = this.errorsFor( element );
		if ( label.length ) {
			// refresh error/success class
			label.removeClass().addClass( this.settings.errorClass+'-bottom-block' );
		
			// check if we have a generated label, replace the message then
			label.attr("generated") && label.html(message);
		} else {
			// create label
			label = $("<" + this.settings.errorElement + "/>").append(
				jQuery("<label>")
				.attr({"for":  this.idOrName(element), generated: true})
				.addClass(this.settings.errorClass+'-bottom-block')
				.html(message || ""));
			if ( this.settings.wrapper ) {
				// make sure the element is visible, even in IE
				// actually showing the wrapped element is handled elsewhere
				label = label.hide().show().wrap("<" + this.settings.wrapper + "/>").parent();
			}
			this.labelContainer.append(label);
			
		}
		if ( !message && this.settings.success ) {
			label.text("");
			typeof this.settings.success == "string"
				? label.addClass( this.settings.success )
				: this.settings.success( label );
		}
		this.toShow = this.toShow.add(label);
	}
	$.validator.prototype.errors = function() {
		return $( this.settings.errorElement + " label." + this.settings.errorClass+"-bottom-block", this.errorContext )			
	}
	$.validator.prototype.hideErrors = function() {
		this.addWrapper( this.toHide ).hide().parent().hide();
		
	}
})(jQuery)
/**
 * new validator type
 * it's for date elements which are consist of three select boxes
 * id's supposed to be similar like dob_month, dob_year, dob_day
 * validation should be laucnhed only for _month element though
 */
jQuery.validator.addMethod("dateValidator", function(value, element,param) {
	val = !this.optional(jQuery(element).parent().children("select")[0]);
	for(i=1; i<jQuery(element).parent().children("select").legth; i++ ) {
		val = val && !this.optional(jQuery(element).parent().children("select")[i]);
	}	
	return  this.optional(element) || val;
}, jQuery.validator.messages.required)

/**
 * Same stuff for phone field
 */
jQuery.validator.addMethod("phoneValidator", function(value, element,param) {
	return this.optional(element) || (!this.optional(jQuery(element).parent().children("input")[0]) && 
			!this.optional(jQuery(element).parent().children("input")[1]) &&
			!this.optional(jQuery(element).parent().children("input")[2]));
}, jQuery.validator.messages.required)



