/**
 * Adds an event handler to the event handler list for a given even on a given
 * object in a portable and clean manner. This is better and cleaner than
 * using HTML attributes.
 */
function AddEvent(obj, type, fn)
{
    // Mozilla/W3C listeners?
    if (obj.addEventListener)
    {
        obj.addEventListener(type, fn, true);
        return true;
    }

    // IE-style listeners?
    if (obj.attachEvent)
        return obj.attachEvent('on' + type, fn);

    return false;
}

// Generic superduper validation! Just add water!
function CheckFields()
{
	var allClear = true;

	// Allow an array to be passed in as the first argument.
	var fields = arguments;
	if (fields.length == 1 && typeof fields[0] == 'object')
		fields = fields[0];

	// Clear all the labels, and validate the fields.
	for (var i = 0; i < fields.length; i++)
	{

		var details = fields[i].split(':');
				alert(details[0]);
		var label = document.getElementById("lbl" + details[0]);
		var field = document.getElementById(details[0]);
		var type  = details.length > 1 ? details[1] : 'text';

		if (label != null)
			label.className = label.className.replace(" bad", "");

		bad = false;
		switch (type)
		{
			case 'text':
				bad = (field.value.length == 0);
				break;

			case 'email':
				bad = (field.value.indexOf("@") == -1 || field.value.indexOf(".") == -1);
				break;

			// Add new validation types in here.

			default:
				alert('I don\'t understand how to validate "' + type + '".');
				bad = true;
				break;
		}

		if (bad)
		{
		
			if (label != null)
				label.className += " bad";
			allClear = false;
		}
	}

	return allClear;
}


function FocusFirstFormElement()
{
	if (document.forms.length > 0)
	{
		var frm = document.forms[0];
		for (var i = 0; i < frm.elements.length; i++)
		{
			var elem = frm.elements[i];
			if ((elem.tagName == 'INPUT' && elem.type != 'hidden') || elem.tagName == 'TEXTAREA')
			{
				/**
				elem.focus();
				 */
				break;
			}
		}
	}
}


/**
 * Sets up a form validator. It can cope with a variety of different places
 * where the validator might be put.
 */
 
function SetValidator(id, fn)
{
	var elem = document.getElementById(id);

	if (elem == null)
	{
		alert('Cannot set validator on ' + id + ': does not exist.');
	}
	if (elem.tagName == 'INPUT' && elem.type == 'submit')
	{
		AddEvent(elem, 'click', fn);
	}
	else if (elem.tagName == 'INPUT' && elem.type == 'button')
	{
		// For this, we need to create a wrapper closure to cause the button
		// to submit on a successful click.
		AddEvent(elem, 'click', function()
		{
			if (fn())
			{
				// Temporary hack for dumbass IE.
				if (this.form)
					this.form.submit();
				else
					document.forms[0].submit();
			}
		});
	}
	else if (elem.tagName == 'FORM')
	{
		AddEvent(elem, 'submit', fn);
	}
	else
	{
		alert('Can\'t set validator on ' + id + ': must be a FORM, INPUT, or A.');
	}
}

function FlickerFactory(id, mode)
{
	var elem = document.getElementById('BusinessName');
	return function()
	{
		elem.disabled = mode;
	};
}

AddEvent(window, 'load', FocusFirstFormElement);

function pop() {
	var width=632;
	var height=485;
    var sLeft=(screen.width-width)/2;
	var sTop=(screen.height-height)/2;

	var params=eval("'height="+height+", width="+width+", top="+sTop+", left="+sLeft+", scrollbars=no'");

	window.open("popup.cfm", '', params);
}

/*
 * Catch all the links we want opening in a new window.
 */
AddEvent(window, "load", function()
{
	var width  = 632;
	var height = 485;
    var left   = (screen.width  - width)  / 2;
	var top    = (screen.height - height) / 2;

	var params = "height=" + height + ",width=" + width + ",top=" + top;

	var handler = function()
	{
		window.open(this.href, null, params);
		return false;
	};

	var links = document.getElementsByTagName("a");
	for (var i = 0; i < links.length; i++)
		if (/\bnew-window\b/.test(links[i].className))
			links[i].onclick = handler;
});