var _n = function(value) {
		return +value.replace(/[^\d\\.]/ig, "");
	},
	_f = function(value) {
		var split = (+value).toFixed(2).split("."),
			value = "." + split[1],
			n = 0;
			
		split = split[0].split("");
		for (var i = split.length - 1; i >= 0; i--) {
			if (n++ % 4 == 3) {
				value = "," + value;
				n++;
			}
			value = split[i] + value;
		}
		
		return "$" + value;	
	},
	validate = function(form) {
		var form = $(form),
			valid = true;

		var temp = null;
		$(".required", form).removeClass("invalid").each(function(i, el) {
			el = $(el);
			if (!el.val() || !el.val().length) {
				el.addClass("invalid");
				valid = false;

				if (temp === null) {
					temp = el;
				}
			}
		});
		
		if (!valid) {
			alert("Please fill in the required fields");
			try {
				temp.parents(".ui-tabs").each(function(i, el) {
					el = $(el);
					el.tabs("select", temp.parents("#" + el.attr("id") + " > .ui-tabs-panel").attr("id"));
				});
				temp.focus();
			} catch (e) {
				// ignore it
			}
		}
		
		return valid;
	};
	
(function($){
	$.fn.extend({
		placeholder : function(options) {
			if (!options) {
				options = {};
			}
			
			return this.each(function() {
				var input = $(this);
				var wrapper = input.wrap("<span style='position: relative;line-height: 100%;display: inline-block; zoom: 1' />").parent();
				var label = $("<label for='" + input.attr("id") + "'>" + input.attr("placeholder") + "</label>").css({
					opacity : 0.65,
					position : "absolute",
					left : parseInt(input.css("padding-left")) + 2,
					top : options.top ? options.top : (isNaN(parseInt(input.css("top"))) ? 0 : parseInt(input.css("top"))) + parseInt(input.css("margin-top")) + parseInt(input.css("padding-top"))+ parseInt(input.css("border-top-width")),
					"font-size" : input.css("font-size")
				});
				wrapper.append(label);

				if (input.val().length) {
					label.hide();
				}
				
				input.focus(function() {
					$(this).parent().find("label").animate({ opacity: .3 }, "fast");
				}).keydown(function() {
					var t = this;
					setTimeout(function() {
						$(t).parent().find("label").css("display", (t.value.length) ? "none" : "");
					}, 0);
				}).blur(function() {
					$(this).parent().find("label").animate({ opacity: .65 }, "fast");
				}).attr("placeholder", "");
			});
		}
	});
})(jQuery);

jQuery.fn.sortElements = (function(){	 
    var sort = [].sort;
    return function(comparator, getSortable) {
        getSortable = getSortable || function(){return this;};
        var placements = this.map(function(){
            var sortElement = getSortable.call(this),
                parentNode = sortElement.parentNode,
                nextSibling = parentNode.insertBefore(
                    document.createTextNode(''),
                    sortElement.nextSibling
                );
            return function() {
                if (parentNode === this) {
                    throw new Error(
                        "You can't sort elements if any one is a descendant of another."
                    );
                }
                parentNode.insertBefore(this, nextSibling);
                parentNode.removeChild(nextSibling);
            };
        });
        return sort.call(this, comparator).each(function(i){
            placements[i].call(getSortable.call(this));
        });
    };
})();

$(function() {
	$("input.numeric").live('keypress', function(event) {
		var controlKeys = [8, 9, 13];
		var isControlKey = controlKeys.join(",").match(new RegExp(event.which));
  		if (!event.which || (48 <= event.which && event.which <= 57) || isControlKey) {
    		return true;
  		} else {
    		event.preventDefault();
  		}
	}).live('focus', function() {
		var _t = this;
		setTimeout(function() { _t.select(); }, 0);
	});
	
	$("input.postal").live('focus', function() {
		this.value = this.value.replace(" ", "");
	}).live('blur', function() {
		var value = this.value.replace(" ", "").substring(0, 6).toUpperCase();
		this.value = value.substring(0, 3) + " " + value.substring(3);
	});
	
	$("input.phone").live('focus', function() {
		this.value = this.value.replace(/[^\d]/ig, "");
	}).live('blur', function() {
		var value = this.value.replace(/[^\d]/ig, "").substring(0, 10);

		if (!value.length) {
			this.value = "";
			return;
		}
		
		var areaCode = value.substring(0, 3),
			first = value.substring(3, 6),
			second = value.substring(6);
		
		this.value = "(" + areaCode + ") " + first + "-" + second;
	});
	
	$("input.money").live('keypress', function(event) {
		var controlKeys = [8, 9, 13];
		var isControlKey = controlKeys.join(",").match(new RegExp(event.which));
  		if (!event.which || (48 <= event.which && event.which <= 57) || event.which == 46 || isControlKey) {
    		return true;
  		} else {
    		event.preventDefault();
  		}
	}).live('focus', function() {
		var _t = this;
		this.value = this.value.replace(/[^\d\\.]/ig, "");
		setTimeout(function() { _t.select(); }, 0);
	}).live('blur', function() {
		this.value = _f(this.value);
	});
});
