Tactica.Forms = new Object();
Tactica.Forms.ConvertTitleToUrl = function(source, targetID)
{
	var target = document.getElementById(targetID);
	
	if (target)
	{
		var title = source.value != null ? source.value : "";
		
		if (target.value == null || target.value == "")
		{
			title = title.replace(/[^A-Z0-9]/gi, "_");
			title = title.replace(/^_+|_+$/gi, "");
			title = title.replace(/_+/gi, "_");
			
			target.value = title.toLowerCase();
		}
	}
}

Tactica.Forms.FormatPhone = function(field)
{
	var phone = null;
	var regex = /[^0-9]/gi;

	if (field != null && field.value != null)
	{
		phone = field.value;
		phone = phone.replace(regex, "");
			
		if (phone.length > 6) field.value = "(" + phone.substring(0, 3) + ") " + phone.substring(3, 6) + "-" + phone.substring(6, 10);
		else if (phone.length > 3) field.value = "(" + phone.substring(0, 3) + ") " + phone.substring(3, 6);
		else if (phone.length > 0) field.value = "(" + phone;
		else field.value = "";
							
		window.status = phone;
	}
}

Tactica.Forms.FormatPostalCode = function(field)
{
	var code = null;
	var regex = /^([A-Z][0-9][A-Z]).*([0-9][A-Z][0-9])$/gi;

	if (field != null && field.value != null)
	{
		code = field.value;
		code = code.toUpperCase();

		if (regex.test(code))
		{
			code = code.replace(regex, "$1 $2");
		}

		field.value = code;
	}
}

LookupBox = function(inputID, valueID, url, submit)
{
	this.input = document.getElementById(inputID);
	this.inputSubmit = submit == true;
	this.selector = this.input.offsetParent.appendChild(document.createElement("SELECT"));
	this.url = url;
	this.value = document.getElementById(valueID);
	this.init();
}

LookupBox.prototype.cancelEvent = function(event)
{
	if (event)
	{
		if (event.preventDefault)
		{
			event.preventDefault();
		}
		
		if (event.stopPropagation)
		{
			event.stopPropagation();
		}
		
		event.cancelBubble = true;
		event.returnValue = false;
	}
}

LookupBox.prototype.clear = function()
{
	this.selector.innerHTML = "";
}

LookupBox.prototype.hide = function(timeout)
{
	if (timeout && timeout > 0)
	{
		var instance = this;
	
		window.setTimeout(function(){instance.hide()}, timeout);
	}
	else
	{
		this.selector.style.visibility = "hidden";
	}
}

LookupBox.prototype.init = function()
{
	var input = this.input;
	var instance = this;
	var selector = this.selector;

	input.style.position = "relative";
	input.setAttribute("autoComplete", "off");
	input.onblur = function(){instance.hide(100)};
	
	if (this.inputSubmit == false)
	{
		input.onkeydown = input.onkeypress = function(event)
		{
			if (typeof(event) == "undefined")
			{
				event = window.event;
			}

			if (event.keyCode == 13)
			{
				instance.cancelEvent(event);
			}
		}
	}

	input.onkeyup = function(event)
	{
		if (typeof(event) == "undefined")
		{
			event = window.event;
		}
		
		switch (event.keyCode)
		{
			case 9:		break;
			case 13:	instance.selectItem(event); break;
			case 38:	selector.selectedIndex = Math.max(selector.selectedIndex - 1, -1); break;
			case 40:	selector.selectedIndex = Math.min(selector.selectedIndex + 1, selector.options.length - 1); break;
			default:	instance.lookup(); break;
		}
		
		return true;
	}

	selector.onchange = function(){instance.selectItem()};
	selector.size = 10;
	selector.style.position = "absolute";
	selector.style.visibility = "hidden";
	selector.style.width = input.style.width;
	selector.style.zIndex = 10;
}

LookupBox.prototype.lookup = function()
{
	var instance = this;
	
	if (instance.request && instance.request.abort)
	{
		instance.request.abort();
		instance.request = null;
	}
	
	if (window.XMLHttpRequest)
	{
    	try
		{
			instance.request = new XMLHttpRequest();
        }
		catch(e)
		{
			instance.request = null;
        }
    }
	else if (window.ActiveXObject)
	{
		try
		{
			instance.request = new ActiveXObject("Msxml2.XMLHTTP");
      	}
		catch(e)
		{
			try
			{
				instance.request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e)
			{
				instance.request = null;
			}
		}
    }
	
	if (instance.request)
	{
		var url = instance.url + "?name=" + escape(instance.input.value);
		
		instance.request.onreadystatechange = function(){instance.lookupProcess()};
		instance.request.open("GET", url, true);
		instance.request.send("");
	}
}

LookupBox.prototype.lookupProcess = function()
{
	var instance = this;
	
	if (instance.request != null && instance.request.readyState == 4 && instance.request.status == 200)
	{
		var data = eval("(" + instance.request.responseText + ")");
		
		instance.clear();

		for (var i = 0; i < data.length; i++)
		{
			var option = new Option();
			
			option.text = data[i].text;
			option.value = data[i].value;
			
			instance.selector.options[instance.selector.options.length] = option;
		}

		instance.show();
		instance.request = null;
	}
}

LookupBox.prototype.selectItem = function(event)
{
	if (this.selector.selectedIndex >= 0 && this.selector.selectedIndex < this.selector.options.length)
	{
		if (this.value != null)
		{
			this.value.value = this.selector.options[this.selector.selectedIndex].value;
			this.input.value = this.selector.options[this.selector.selectedIndex].text;
		}
		else
		{
			this.input.value = this.selector.options[this.selector.selectedIndex].value;
		}
	}
	
	this.cancelEvent(event);
	this.hide();
}

LookupBox.prototype.show = function()
{
	var x = this.input.offsetLeft;
	var y = this.input.offsetTop + this.input.offsetHeight;

	this.selector.size = Math.min(this.selector.options.length, 10);
	this.selector.style.left = x + "px";
	this.selector.style.top = y + "px";
	this.selector.style.visibility = "visible";
}

function OpenPopup(url, w, h)
{
	window.open(url, null, "menubar=0,resizable=1,scrollbars=0,width=" + w + ",height=" + h);
	
	return false;
}

function SubmitForm(e, id)
{
	if (e == null)
	{
		e = window.event;
	}
	
	if (e && (e.keyCode == 13 || e.type == "submit"))
	{
		var btn = document.getElementById ? document.getElementById(id) : null;
		
		if (btn)
		{
			var prn = btn.parentNode;
			
			switch (btn.nodeName)
			{
				case "A":
					eval(btn.href.replace(/^javascript:/gi, ""));
					return false;
				
				case "INPUT":
					btn.click();
					return false;
			}
		}
	}
	
	return true;
}

function ConvertTitleToUrl(source, targetID)
{
	Tactica.Forms.ConvertTitleToUrl(source, targetID);
}

function FormatPhone(field)
{
	Tactica.Forms.FormatPhone(field);
}

function FormatPostalCode(field)
{
	Tactica.Forms.FormatPostalCode(field);
}
