Tactica.Forms = new Object();

// formats a phone number
Tactica.Forms.FormatPhone = function(field)
{
	if (field != null && field.value != null)
	{
		var phone = field.value.replace(/[^0-9]/gi, "");

		switch (phone.length)
		{
			case 0:
				field.value = "";
				break;

			case 7:
				field.value = phone.substr(0, 3) + "-" + phone.substr(3);
				break;

			default:
				field.value = "(" + phone.substr(0, 3) + ") " + phone.substr(3, 3) + "-" + phone.substr(6);
				break;
		}

		$(field).change();
	}
}

// formats a postal code
Tactica.Forms.FormatPostalCode = function(field)
{
	if (field != null && field.value != null)
	{
		var code = field.value.toUpperCase();
		var codeExpr = /^([A-Z][0-9][A-Z])\s?([0-9][A-Z][0-9])$/gi;
		var data;

		if ((data = codeExpr.exec(code)) != null)
		{
			code = data[1] + " " + data[2];
		}

		field.value = code;

		$(field).change();
	}
}

Tactica.Forms.LookupBox = function(textID, valueID, callbackUrl)
{
	this.callbackUrl = callbackUrl;
	this.text = $(textID);
	this.value = $(valueID);
	
	if (this.text.length > 0)
	{
		this.selectedIndex = -1;
		this.selector = $(this.text.get(0).offsetParent.appendChild(document.createElement("UL")));
		this.selector.fadeTo(0, 0);
		this.timer = null;
		this.init();
	}
}

Tactica.Forms.LookupBox.prototype.hide = function()
{
	var me = this;

	if (this.timer == null)
	{
		this.timer = window.setTimeout(function() { me.selector.fadeTo(500, 0, function() { me.selector.css("visibility", "hidden") }); me.timer = null }, 1000);
	}
}

Tactica.Forms.LookupBox.prototype.init = function()
{
	var me = this;

	with (this.text)
	{
		attr("autoComplete", "off");
		attr("defaultValue", attr("value"));
		css("position", "relative");
		blur(function(e) { me.hide() });
		focus(function(e) { if (this.value == this.defaultValue) this.value = ""});
		keydown(function(e) { if (e.keyCode == 13) e.preventDefault() });
		keyup(function(e)
		{
			switch (e.keyCode)
			{
				case 9: break;
				case 13: me.selectIndex(e, me.selectedIndex, true); break;
				case 38: me.selectIndex(e, me.selectedIndex - 1, false); break;
				case 40: me.selectIndex(e, me.selectedIndex + 1, false); break;
				default: me.lookup(); break;
			}

			return true;
		});
	}

	this.selector.addClass("selector").css({ position: "absolute", visiblity: "hidden", zIndex: "10" }).width(this.text.width());
}

Tactica.Forms.LookupBox.prototype.lookup = function()
{
	var me = this;
	var tu = this.callbackUrl.replace(/:text/gi, this.text.val());
	
	if (me.request && me.request.abort)
	{
		me.request.abort();
		me.request = null;
	}
	
	me.request = $.getJSON(tu, function(data){me.lookupProcess(data)});
}

Tactica.Forms.LookupBox.prototype.lookupProcess = function(data)
{
	var me = this;
	
	me.selector.empty();

	$.each(data, function(i,item)
	{
		var link = $("<li/>").html(item.text).click(function(e){me.selectIndex(e,this.index,true)}).appendTo(me.selector).get();
		
		for (var i = 0; i < link.length; i++)
		{
			link[i].index = i;
			link[i].valueText = item.value;
		}
	});
	
	me.show();
	me.request = null;
}

Tactica.Forms.LookupBox.prototype.selectIndex = function(e, i, submit)
{
	var me = this;
	var li = me.selector.children("li:eq(" + i + ")");

	if (i < 0)
	{
		i = 0;
	}

	if (i > me.selector.children().length - 1)
	{
		i = me.selector.children().length - 1;
	}

	me.selectedIndex = i;
	me.selector.children().removeClass("active");

	li.addClass("active");

	if (submit)
	{
		me.hide();
		me.text.val(li.html());
		me.value.val(li.get(0).valueText);
	}
}

Tactica.Forms.LookupBox.prototype.position = function()
{
	var p = this.text.get(0);
	var x = p.offsetLeft + "px";
	var y = (p.offsetTop + p.offsetHeight - 1) + "px";
	var w = (p.offsetWidth - 2) + "px";

	this.selector.css({left:x, top:y, width:w, visibility:"visible"});
}

Tactica.Forms.LookupBox.prototype.show = function()
{
	if (this.timer != null)
	{
		window.clearTimeout(this.timer);
	}

	this.position();
	this.selector.css("visibility", "visible");
	this.selector.fadeTo(500, 1);
	this.timer = null;
}

