(function($) {
	$.fn.check = function( type, option) {
		var re;
		if ($(this).val() == $(this).attr('placeholder')) return false;
		switch( type ) {
			case 'email':
				re = new RegExp(/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i);
				if ($(this).val().match(re) == null) return false;
				break;
			case 'phone':
				re = new RegExp(/\D/gi);
				var phone = $(this).val().replace(re,"");
				if (phone.length < 10) return false;
				break;
			case 'state':
				re = new RegExp(/[a-z]{2,}/i);
				if ($(this).val().match(re) == null) return false;
				/*
if (typeof(option) == "object" && option.exclude != null) {
					if ($(this).val().toLowerCase().indexOf(option.exclude) > -1) return false;
				}
*/
				break;
			case 'zip':
			case 'zipcode':
				re = new RegExp(/^[0-9]{5}$/);
				if ($(this).val().match(re) == null) return false;
				break;
			case 'domain':
				re = new RegExp(/(http:\/\/)*[a-z]*[\.]?[a-z0-9\-]{4,}(\.[a-z]{2,3})+$/i);
				if ($(this).val().match(re) == null) return false;
				break;
			case 'name':
				re = new RegExp(/^[a-z]{3,}\s[a-z]+$/i);
				if ($(this).val().match(re) == null) return false;
				break;
			case 'length':
				if (typeof(option) == "object") {
					if ($(this).val() != null) {
						if ($(this).val().length < parseInt(option.min)) return false;
					} else {
						return false;
					}
				}
				break;
			case 'address':
				re = new RegExp(/[a-z0-9#\.]{1,}\s[a-z0-9#\.\s]{4,}[a-z0-9#\.]*/i);
				if ($(this).val().match(re) == null) return false;
				break;
			case 'city':
				re = new RegExp(/[a-z]{4,}[\sa-z]*/i);
				if ($(this).val().match(re) == null) return false;
				break;
			case 'currency':
				re = new RegExp(/[0-9]+\.[0-9]{2,}/i);
				if ($(this).val().match(re) == null) return false;
				break;
			case 'mdy':
				re = new RegExp(/[0-9]+(-|\/)[0-9]+(-|\/)[0-9]{4}/i);
				if ($(this).val().match(re) == null) return false;
				break;
			default:
				return false;
				break;
		}
		return true;
	}
	
	$.fn.checkFile = function( $types ) {
		var $this = $(this);
		if ($this.val() == $this.attr('placeholder')) return false;
		var dot = $this.val().lastIndexOf(".");
 		if( dot == -1 ) return false;
 		var ext = $this.val().substr(dot+1, $this.val().length);
 		return ($types.indexOf(ext) == -1 ? false:true);
	}
	
	$.fn.count = function( s, config ) {
		var d = {pref: '', suff: '', dir: 'up', max: 0};
		if (typeof(config) == "object") d = $.extend(d, config);
		var shell = $('#'+s);
		switch(shell.attr('tagName')) {
			case 'DIV':
				$(this).bind('keydown keyup', function() {
					if (d.dir == 'down') {
						var cnt = d.max - $(this).val().length;
					} else {
						var cnt = $(this).val().length;
					}
					shell.html(d.pref+" "+cnt+" "+d.suff);
				});
				break;
			case 'INPUT':
				$(this).bind('keydown keyup', function() {
					if (d.dir == 'down') {
						var cnt = d.max - $(this).val().length;
					} else {
						var cnt = $(this).val().length;
					}
					shell.vall(d.pref+" "+cnt+" "+d.suff);
				});
				break;
		}
	}
	
	$.fn.placeholder = function( $subject ) {
		return $(this).each(function() {
			var $this = $(this), text = $subject;
			var clr = $this.css('color');
			$this.closest('form').submit( function() {
				$(this).clearholders();
			});
			//when focused ont the input
			$this.attr('placeholder', text);
			$this.focus(function(){
				if (!$this.attr('cols')) {
					if($this.val() == text) {
						$this.val("");
						$this.css( {color: clr} );
					}
				} else {
					if ($this.html() == text) {
						$this.html("");
						$this.css( {color: clr} );
					}
				}
			});
	                       //looses focus
			$this.blur(function() {
				if ($this.attr('cols')) {
					if($this.val() == "") {
						$this.css( {color: '#C7C7C7'} );
						$this.html(text);
					}
				} else {
					if($this.val() == "") {
						$this.css( {color: '#C7C7C7'} );
						$this.val(text);
					}
				}
			});
			if (!$this.attr('cols')) {
				if ($this.val() == '') {
					$this.val($subject);
					$this.css( {color: '#C7C7C7'} );
				}
			} else {
				if ($this.html() == '') {
					$this.html($subject);
					$this.css( {color: '#C7C7C7'} );
				}
			}
		});
	}
	
	$.fn.clearholders = function() {
		$('input[type=text]', $(this)).each(function() {
			if ($(this).val() == $(this).attr('placeholder')) {
				$(this).val('');
			}
		});
	}
})(jQuery);
