$.widget("ui.helperValue", {
	_create: function() {
		var obj = this;
		var parent = this.element;
		var form = parent.closest('form');

		if(parent.val() == ''){
			parent.val(parent.attr('title'));
		}

		parent.focus(function(){
			if($(this).val() == $(this).attr('title')){
				$(this).val('');
			}
		});

		parent.blur(function(){
			if($(this).val() == ''){
				$(this).val($(this).attr('title'));
			}
		});
	}
});

$.widget('ui.ajaxform', {
	options: {
		reqclass: '',
		type: 'POST',
		address: ''
	},
	
	_create: function() {
		var obj = this;
		var parent = this.element;
		var reqclass = this.options.reqclass;
		var before = this.options.before;
		
		parent.submit(function(){
			if ($.isFunction(before)) before();
			if(reqclass.length > 0){
				if(parent.hasClass(reqclass)){
					obj.sendout();
				}
			}else{
				obj.sendout();
			}
			return false;
		});
	},
	
	sendout: function(){
		var formdata = this.element.serialize();
		var callback = this.options.change;
		var type = this.options.type;
		var address = this.options.address;
		if( address.length == 0 ){
			address = parent.attr('action');
		}

		$.ajax({
			type: type,
			url: address,
			data: formdata,
			success: function(data) {
				if ($.isFunction(callback)) callback(data); 
			}
		});
	}
	
});

$.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(){
			validated = 0;
			items.each(function(){
				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\-\ ]+$/;
		var cvvregex = /^([0-9]{3,4})$/;
		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('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')){
				errors+=1;
				error_msg += '<li class="entry">This field is required</li>';
			}
		}
		
		el.next('.validation-msg').remove();
		if(errors > 0){
			el.addClass('error');
			
			if(valtype == 'outline'){
				el.parent().css({ position: 'relative' });
				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').mouseleave(function(){
					if(!$.browser.msie){
						$(this).fadeOut(function(){
							$(this).remove();
						});
					}else{
						$(this).remove();
					}
				});
			}
		
			return 1;
		}else{
			el.removeClass('error');
			return 0;
		}
		
	}
	
});


$(document).ready(function(){
	$('.helpervalue').helperValue();
	$('form.validate').validation();
	$('form.ajaxform').ajaxform();
});
