// javascript statements, functions, and methods for use by all pages on tenmercer.com


// break out of frames
if (top != self) {
	top.location.href = document.location.href;
}


// scroll address bar out of view on iOS (and other mobile?) devices
function hideAddressBar() {
	if(!window.location.hash) {
		setTimeout( function(){ window.scrollTo(0, 1); }, 50 );
		}
	}

window.addEventListener("load", function(){ if(!window.pageYOffset){ hideAddressBar(); } } );
window.addEventListener("orientationchange", function(){ if(!window.pageYOffset){ hideAddressBar(); } } );


// this function sets onLoad if it isn't already set;
// if it is set, it adds to the onLoad without erasing what is already there
// see http://simon.incutio.com/archive/2004/05/26/addLoadEvent


function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}



// if DOM 0 is supported, add reset check to onLoad event
if (document.forms && document.forms.length>0) {
	 addLoadEvent(addResetConfirm);
}

function addResetConfirm() {
	for (var i=0; document.forms[i]; i++) {
		document.forms[i].onreset = function() {
			return confirm('Do you want to erase all fields in this form?');
		} // end onreset function
	} // end for
} // end function





// hasClassName
//
// Description : returns boolean indicating whether the object has the class name
//							 with the understanding that there may be multiple classes
//
// Arguments:
//		classString		 - class name to check
//
function hasClass(classString) {

	// if there is a class
	if (this.className) {

		// the classes are just a space separated list, so first get the list
		var classList = this.className.split(/\s/g);

		// find any instance
		for (var i = 0; classList[i]; i++) {

			// if class found
			if ( classList[i] == classString ) {

				// we found it
				return true;

				} // if class found

			}	// for loop

		} // if there is a class

	// if we got here then the class name is not there
	return false;

} // function hasClass




// removeClassName
//
// Description : removes a class from the class attribute of a DOM element
//							 with the understanding that there may be multiple classes
//
// Arguments:
//		classString		 - class name to remove
//
function removeClass(classString) {

	// if there is a class (if not, there's nothing to remove)
	if (this.className) {

		// the classes are just a space separated list, so first get the list
		var classList = this.className.split(/\s/g);

		// find all instances and remove them
		// for ( var i = 0; i < classList.length; i++ ) {

		for (var i = 0; classList[i]; i++) {

			// if class found
			if ( classList[i] == classString ) {

				// remove array item
				classList.splice(i, 1);

				// decrement loop counter as we have adjusted the array's contents
				i--;

			} // if class found

		} // for loop

		// assign modified class name attribute
		this.className = classList.join(' ');
	}	// if there is a class

} // function removeClassName



// addClassName
//
// Description : adds a class to the class attribute of a DOM element
//							 with the understanding that there may be multiple classes
//
// Arguments:
//		classString		 - class name to add
//
function addClass(classString) {

	// if there is a class
	if (this.className) {

		// if new class name is not already in class attribute
		if (!this.hasClass(classString)) {
			// append class string to attribute
			this.className += ' ' + classString;
		}

	} // if there is a class

	// if there was no class, assign class name attribute
	else {		
		this.className = classString;
	
	}

}	// function addClassName



// hasElement
function hasElement(element, className) {
	// search for child element with class className
	var childElements = this.getElementsByTagName(element);

	for (var j = 0; childElements[j]; j++) {
		if (childElements[j].hasClass(className) ) {
			return formChildElements[j];
		}
	}
	return false;
}




Element.prototype.hasClass = hasClass;
Element.prototype.removeClass = removeClass;
Element.prototype.addClass = addClass;
Element.prototype.hasElement = hasElement;
