var EventManager = {
	_registry: null,
	Initialize: function() {
		if (this._registry === null) {
			this._registry = [];
			// Register the cleanup handler on page unload.
			EventManager.Add(window, "unload", this.CleanUp);
		}
	},
	/**
	* Registers an event and handler with the manager.
	*
	* @param  obj         Object handler will be attached to.
	* @param  type        Name of event handler responds to.
	* @param  fn          Handler function.
	* @param  useCapture  Use event capture. False by default. If you don't understand this, ignore it.
	*
	* @return True if handler registered, else false.
	*/
	Add: function(obj, type, fn, useCapture) {
		this.Initialize();
		/* If a string was passed in, it's an id. */
		if (typeof obj == "string") { obj = document.getElementById(obj); }
		if (obj == null || fn == null) { return false; }
		/* Mozilla/W3C listeners? */
		useCapture = (useCapture===true);
		if (obj.addEventListener) {
			obj.addEventListener(type, fn, useCapture);
			this._registry.push({obj: obj, type: type, fn: fn, useCapture: useCapture});
			return true;
		}
		/* IE-style listeners? */
		if (obj.attachEvent && obj.attachEvent("on" + type, fn)) {
			this._registry.push({obj: obj, type: type, fn: fn, useCapture: false});
			return true;
		}
		return false;
	},
	/* Cleans up all the registered event handlers. */
	CleanUp: function() {
		for (var i = 0; i < EventManager._registry.length; i++) {
			with (EventManager._registry[i]) {
				// Mozilla/W3C listeners?
				if (obj.removeEventListener) {
					obj.removeEventListener(type, fn, useCapture);
				}
				// IE-style listeners?
				else if (obj.detachEvent) {
					obj.detachEvent("on" + type, fn);
				}
			}
		}
		// Kill off the registry itself to get rid of the last remaining references.
		EventManager._registry = null;
	}
};

function cancelEvent(e,ethis) {
	var e = e || window.event;
	e.cancelBubble = true; // for IE
	if (e.stopPropagation) {
		e.stopPropagation();
	}
	e.returnValue = false; // for IE
	if (e.preventDefault) {
		e.preventDefault();
	}
}

function initOverLabels () {
	if (!document.getElementById) return;      

	var labels, id, field;

	// Set focus and blur handlers to hide and show 
	// labels with 'overlabel' class names.
	labels = document.getElementsByTagName('label');
	for (var i = 0; i < labels.length; i++)
	{
		if (labels[i].className == 'overlabel')
		{
			// Skip labels that do not have a named association
			// with another field.
			id = labels[i].htmlFor || labels[i].getAttribute('for');
			if (!id || !(field = document.getElementById(id))) {
				continue;
			} 

			// Change the applied class to hover the label 
			// over the form field.
			labels[i].className = 'overlabel-apply';

			// Hide any fields having an initial value.
			if (field.value !== '') {
				hideLabel(field.getAttribute('id'), true);
			}

			// Set handlers to show and hide labels.
			field.onfocus = function () {
				hideLabel(this.getAttribute('id'), true);
			};
			field.onblur = function () {
				if (this.value === '') {
					hideLabel(this.getAttribute('id'), false);
				}
			};

			// Handle clicks to label elements (for Safari).
			labels[i].onclick = function () {
				var id, field;
				id = this.getAttribute('for');
				if (id && (field = document.getElementById(id))) {
					field.focus();
				}
			};
		}
	}
};

function hideLabel (field_id, hide) {
	var field_for;
	var labels = document.getElementsByTagName('label');
	for (var i = 0; i < labels.length; i++) {
		field_for = labels[i].htmlFor || labels[i].getAttribute('for');
		if (field_for == field_id) {
			labels[i].style.textIndent = (hide) ? '-1000px' : '0px';
			return true;
		}
	}
}

/**
* Form Validation
*/

function checkForm(form){
	var must_not_be_empty = new Array("firstname","lastname");
	var either_or_sets = new Array(new Array("phone", "email"));
	var errors = new Array();
	for(var i in must_not_be_empty) {
		if(form.elements[must_not_be_empty[i]] &&
			form.elements[must_not_be_empty[i]].value == "") {
			errors[errors.length] = must_not_be_empty[i];
		}
	}
	for(var i in either_or_sets) {
		var local_err = new Array();
		for(var j in either_or_sets[i]) {
			if (form.elements[either_or_sets[i][j]] &&
				form.elements[either_or_sets[i][j]].value == "") {
				local_err[local_err.length] = either_or_sets[i][j];
			}
		}
		if(local_err.length == either_or_sets[i].length) {
			for(var j in local_err) {
				errors[errors.length] = local_err[j];
			}
		}
	}
	if (errors.length == 0) {
		return true;
	} else {
		highlight(errors);
		return false;
	}
}

function validate_email_simple(email) {
	return (email.indexOf('@',0)==-1 ||
		email.indexOf('@',0)== 0 ||
		email.indexOf('.',0)==-1);
}

function highlight(fields) {
	if(document.getElementById) {
		for(var i in fields) {
			var field = document.getElementById(fields[i]);
			field.className = "open_textbox_error";
		}
	}
	alert("You have an error in the following fields:\n"+fields.join(", "));
}

function unhighlight(field) {
	field.className = "open_textbox";
}

function loadSWFinto(id,fid,path,w,h,ver) {
	if(document.getElementById(id)) {
		var so = new SWFObject(SITE_ROOT+path, fid, w,h, ver, "#333333");
		so.addParam("wmode", "transparent");
		success = so.write(id);
		return success;
	}
	return false;
}

var SITE_ROOT = (window.location.href.indexOf('test/') != -1) ? '/test/':'/';

window.onload = function () {
	//setTimeout(initOverLabels, 50);
	var flhome = document.getElementById('flhome');
	success = loadSWFinto('flhome','fl_home',"images/home.swf?link=/construction-process/",409,250,6); // home
	if(!success && flhome) { flhome.innerHTML = '<a href="/construction-process/" title="Come on in and see me being built!"><img src="/images/home.jpg" alt="Elegant Country Farmhouse" border="0" /></a>' }
}
