$.widget('ui.validation', {
	options: {
		validate: '',
		valtype: 'outline' //outline, inline
	},
	_create: function() {
		var obj = this;
		var parent = this.element;
		var items = parent.find('.validate');
		var validated = 0;
		var validate = this.options.validate;
		var valtype = this.options.valtype;
		
		items.blur(function(){
			validated = 0;
			validated = obj.validate($(this));
		});
		parent.submit(function(){
			items = parent.find('.validate');
			validated = 0;
			items.each(function(){
				if($(this).is(':visible')){
					validated += obj.validate($(this));
				}
			});
			if(validated != 0){
				parent.removeClass('validated');
				return false;
			}else{
				parent.addClass('validated');
				return true;
			}
		});
		
	},
	
	_init: function(){
		var obj = this;
		var validate = this.options.validate;
		var items = this.element.find('.validate');
		var validated = 0;
		var valtype = this.options.valtype;
		
		if(validate == 'validate'){
			validated = 0;
			items.each(function(){
				validated += obj.validate($(this));
			});
			if(validated != 0){
				this.element.removeClass('validated');
			}else{
				this.element.addClass('validated');
			}
		}
	},
	
	validate: function(el){
		var content = el.val();
		var emailregex = /^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$/;
		var creditregex = /^[0-9\-\s]+$/;
		var cvvregex = /^([0-9]{3,4})$/;
		var weightregex = /^([0-9]{2,3})$/;
		var passregex = /^([A-Za-z0-9]+)$/;
		var passlength = /^([A-Za-z0-9]{6,25})$/;
		var errors = 0;
		var error_msg = '';
		var valtype = this.options.valtype;
		
		if(el.hasClass('vldemail')){
			if(el.hasClass('vldrequired') || el.val().length > 0){
				if(el.val() != el.attr('title') || el.attr('title') == ''){
					if(content.search(emailregex) == -1){
						errors+=1;
						error_msg += '<li class="entry">A valid email is required</li>';
					}
				}
			}
		}
		if(el.hasClass('vldcredit')){
			if(el.hasClass('vldrequired') || el.val().length > 0){
				if(content.search(creditregex) == -1){
					errors+=1;
					error_msg += '<li class="entry">A valid credit card number is required</li>';
				}
			}
		}
		if(el.hasClass('vldcvv')){
			if(el.hasClass('vldrequired') || el.val().length > 0){
				if(content.search(cvvregex) == -1){
					errors+=1;
					error_msg += '<li class="entry">A valid CSV code is required</li>';
				}
			}
		}
		if(el.hasClass('vldweight')){
			if(el.hasClass('vldrequired') || el.val().length > 0){
				if(content.search(weightregex) == -1){
					errors+=1;
					error_msg += '<li class="entry">A valid weight is required</li>';
				}
			}
		}
		if(el.hasClass('vldpass')){
			if(el.hasClass('vldrequired') || el.val().length > 0){
				if(content.search(passregex) == -1){
					errors+=1;
					error_msg += '<li class="entry">A valid Password is required</li>';
				}
			}
		}
		if(el.hasClass('vldpasslength')){
			if(el.hasClass('vldrequired') && el.val().length > 0){
				if(content.search(passlength) == -1){
					errors+=1;
					error_msg += '<li class="entry">A minimum of 6 characters long</li>';
				}
			}
		}
		if(el.hasClass('vldrequired')){
			if(content.length == 0 || el.val() == el.attr('title')){
				if(el.val() == el.attr('title') || el.attr('title') == ''){
					errors+=1;
					error_msg += '<li class="entry">This field is required</li>';
				}
			}
			
			// Checkbox
			if( el.attr('type') == "checkbox" ) {
				if( !el.is(":checked") ) {
					errors+=1;
					error_msg += '<li class="entry">This field is required</li>';
				}
			}
		}
		el.next('.validation-msg').remove();
		
		var outerBorder = el.closest('.user-input-border');
		
		if((errors > 0 && !el.hasClass('hotel-input')) || (errors > 0 && el.hasClass('hotel-input') && $('input[name="cbox-hotel-transfer"]').is(':checked'))){
			el.addClass('error');
			
			if(valtype == 'outline'){
				if( $('.co-stages-pd').length > 0 ) {
					el.addClass('user-input-invalid');
					outerBorder.addClass('user-input-border-invalid');
				} else {
					el.parents('.bill-addr li').css({'position':'relative', 'z-index':'3'});
					el.parents('.deliv-addr li').css({'position':'relative', 'z-index':'2'});
					el.after('<div class="validation-msg"><ul class="list">'+ error_msg +'</ul><div class="validation-bottom"></div></div>');
					el.next('.validation-msg').show();
					el.next('.validation-msg').bind('mouseleave click',function(){
						if(!$.browser.msie){
							$(this).fadeOut(function(){
								$(this).remove();
							});
						}else{
							$(this).remove();
						}
					});
				}
			}
			if(valtype == 'inline'){
				el.parent().addClass('user-input-invalid');
			}
		
			return 1;
		}else{
			el.removeClass('error');
			el.removeClass('user-input-invalid');
			el.parent().removeClass('user-input-invalid');
			outerBorder.removeClass('user-input-border-invalid');
			return 0;
		}
		
	}
	
});

$().ready(function() {
	$('.validate-form').validation({valtype: 'inline'});
});

