function nvm() {
	
}

function formatNumber($num) {
	$num = ""+$num; $cnt = 0; $len = $num.length;
	for ( $i = $len; $i > 0; $i-- )
		if ( $cnt++ == 2 && $i - 1 > 0 )
		{
			$cnt = 0;
			$num = $num.substr(0, $i - 1) +","+ $num.substr($i - 1);
		}
			
	return $num;	
}

function submitform(form, handler) {
	form = $(form); optionstring = "";	
	form.find("input, select, button, textarea").each(function() {optionstring += this.name +"="+ $(this).val() +"&";});
	optionstring = optionstring.substr(0,optionstring.length - 1);
	function responsehandler(response) {
		response = eval("("+response+")");
		if ( typeof(handler) == "function" ) handler(response);
	}
	$.ajax({type: $(form).attr('method').toUpperCase(), url: $(form).attr('action'), data: optionstring, success: responsehandler});
	return false;
}

function addOutput(title, message, success) {
	clsName = success == true ? "success" : "fail";
	output = $("<div style=\"margin-top: 10px; display: none;\" class=\"" + clsName + "\"><em>" + title + "</em>" + message + "</div>");
	$("#outputcontainer").append(output);
	
	/* Make sure item links are highlighted. */
	Item.bindDomTriggers($("#outputcontainer"));
	
	output.slideDown(300);
	setTimeout(function() {$("#outputcontainer").children(":first").slideUp(300, function() {$("#outputcontainer").children(":first").remove();});}, 10000);
}

$(function() {
	$("form.ajax").bind("submit", function(e) {
		e.preventDefault();
		/* Check if form has an onsubmit handler */
		handler = undefined;
		if ( $(this).attr("onsubmit") != "" ) {
			r = /[a-zA-Z0-9_]+/i
			if ( r.test($(this).attr("onsubmit")) ) handler = eval($(this).attr("onsubmit"));
		}
		$("#loader").fadeIn(400);
		$(".fadeOutOnAjax").fadeOut(400);
		$("div#fail, div#success").fadeOut(400, function() {$(this).remove();});
		submitform(this, handler);		
	}).find("a.submit, input.submit, input[type=submit], button.submit").bind("click", function(e) {
		e.preventDefault();
		/* Find form parent */
		form = $(this).parents('form');
		if ( form.length > 0 ) form.trigger("submit");
	});
	$("a.checkbox").bind("click", function(e) {
		/* Find the element to which to add/remove hidden inputs. */
		var parent = $(this).parent();
		while ( !parent.is("form") && !parent.is("body") ) parent = parent.parent();
		
		/* If the checkbox is checked, we need to remove a hidden input field. */
		if ( $(this).hasClass("checked") && !$(this).hasClass("radio") ) {
			parent.find("input[type=hidden][name="+$(this).attr("name")+"][value="+$(this).attr("rel")+"]").remove();
			$(this).removeClass("checked");
		}
		else {
			if ( $(this).hasClass("radio") ) {
				parent.find("input[type=hidden][name="+$(this).attr("name")+"]").remove();
				parent.find("a.checkbox.radio[name="+$(this).attr("name")+"]").removeClass("checked");
			}
			parent.append("<input type=\"hidden\" name=\""+$(this).attr("name")+"\" value=\""+$(this).attr("rel")+"\" />");
			$(this).addClass("checked");
		}
		
		/* In the case of a 'check all' checkbox */
		if ( $(this).hasClass("checkall") ) {
			var checked = $(this).hasClass("checked");
			/* The rel attribute holds the selector of the dom element in which to search for checkboxes. */
			$($(this).attr("rel")).find("a.checkbox").each(function() {
				if ( checked != $(this).hasClass("checked") ) $(this).trigger("click");
			});
		}
		
		return false;
	}).each(function(){
		if ( $(this).attr("class") == "checkbox checked" ) {
			var parent = $(this).parent();
			parent.append("<input type=\"hidden\" name=\""+$(this).attr("name")+"\" value=\""+$(this).attr("rel")+"\" />");
		}
	});
	
	/* Do the standard link popups. */
	$("a.showpopup").bind("click", function(e) {
		e.preventDefault();
		/* check for href attribute. */
		if ( $(this).attr("href") != "" && $(this).attr("href") != "#" ) {
			popup = $($(this).attr("href"));
			popup.css({"top": e.pageY+"px", "left": e.pageX+"px"}).show().bind("mouseleave", function() {
				$(this).fadeOut("fast").unbind("mouseleave");
			});
		}
	});
});

if (!Array.prototype.filter) {
  Array.prototype.filter = function(fun /*, thisp*/)
  {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();

    var res = new Array();
    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
      {
        var val = this[i]; // in case fun mutates this
        if (fun.call(thisp, val, i, this))
          res.push(val);
      }
    }

    return res;
  };
}

