//
// javascript_global.js
// --------------------
// Common JavaScript functions used throughout the site.
//
//

var popUp;
 

function offsitePopup(url) {
    var popUp = window.open(url, 'offsiteWindow', 'width=750,height=500,scrollbars=yes,resizable=yes,menubar=yes,toolbar=yes,location=yes');
    if (typeof popUp == 'object') {
        popUp.focus();
    }
}

function enlargementPopup(url, title) {
    var popUp = window.open('/lib/popups/enlargement.php?url=' + url + '&title=' + title, 'enlargementWindow', 'width=630,height=600,scrollbars=yes,resizable=yes,menubar=no,toolbar=no,location=no');
    if (typeof popUp == 'object') {
        popUp.focus();
    }
}

function popup(url, width, height) {
    var popUp = window.open(url, 'popupWindow', 'width=' + width + ',height=' + height + ',scrollbars=yes,resizable=yes,menubar=no,toolbar=no');
    if (typeof popUp == 'object') {
        popUp.focus();
    }
    return false;
}
function CSSpopupWindow(trigger) {
    FM.toggleClass(trigger,"Popup");
    aClass = trigger.className.split(" ");
    width = 700;
    height = 550;
	winArgs = ",menubar=no,toolbar=no";
    for (var j = 0; j < aClass.length; j++) {
        if (aClass[j].search(widthReg) >= 0)  { width = aClass[j].substr(1); }
        if (aClass[j].search(heightReg) >= 0) { height = aClass[j].substr(1); }
		if (aClass[j] == "ConversionEvent") { triggerConversionEvent(trigger.href); }
		if (aClass[j].search(/FullChrome/) >= 0 ) {winArgs = ",toolbar=yes,menubar=yes"; }
    }
	
    return basePopup(trigger.href,width,height,'popup',',scrollbars=yes,resizable=yes' + winArgs);

}
function basePopup(uri,width,height,winName,winArgs) {
	/* close existing popUp window */
	if (typeof(popUp) == "object") { if (typeof(popUp.window) == "object") { popUp.close(); }}

    popUp = window.open(uri, winName, 'width=' + width + ',height=' + height + winArgs);

	/* attempt to focus the new window */
	if (typeof(popUp) == "object") {
		try {
		    popUp.focus();
		} catch(err) {
			return false;
		}
	}
	return false;
}

CurrFAQ = "";
LastQ = "";
function showFAQ(trigger) {
    FM.toggleClass(trigger,"FAQ");
	FAQnumber = trigger.className;
	if (CurrFAQ > "") {
		FM.removeClass("FAQ"+CurrFAQ,"Visible");
		if (typeof(LastQ)=="object") LastQ.href="#Answer-"+CurrFAQ;
	}
	if (FAQnumber != CurrFAQ) {
		FM.addClass("FAQ"+FAQnumber,"Visible");
		trigger.href="#Top";
		LastQ = trigger;
		CurrFAQ = FAQnumber;
	} else {
		CurrFAQ = "";
	}
    FM.toggleClass(trigger,"FAQ");
	return false;
}

function verifyEmailAddress(address) {
}


widthReg  = /^w([0-9]+)$/;			// defines regex for conveying pop-up width info via classnames
heightReg = /^h([0-9]+)$/;			// defines regex for conveying pop-up height info via classnames
foundClass = "";

function globalOnLoad() {

    // Assign link events
    for (var i = 0; i < document.links.length; i++) {
        if (FM.checkForClass(document.links[i],"Popup"))	foundClass = "Popup";
        if (FM.checkForClass(document.links[i],"FAQ"))		foundClass = "FAQ";
        if (document.links[i].className.match('PDF')) {
            document.links[i].onclick = function() { popup(this.href,'750','500'); return false; }
        }
        if (document.links[i].className.match('Offsite')) {
            document.links[i].onclick = function() { offsitePopup(this.href); return false; }
        }
        if (document.links[i].className.match('Enlargement')) {
            document.links[i].onclick = function() { enlargementPopup(this.href, this.title); return false; }
        }
        if (foundClass != "") {
            /* width & height should be specified by class names that begin with a 'w' or an 'h'
               popup class names should be, e.g.: class="Popup w400 h320" */
            if (foundClass=="ImagePop") {
                document.links[i].onclick = function () { NewImagePopup(this); return false; };
            } else if (foundClass=="FAQ") {
				document.links[i].onclick = function () { return showFAQ(this); };
			} else {
                document.links[i].onclick = function() { CSSpopupWindow(this); return false; };
            }
        }
		foundClass = "";
    }

    // Run page onLoad function, if it exists
    if (typeof onLoad != 'undefined') {
        onLoad();
    }
    	window.scrollTo(0,1);
}



// Every page should assign events
window.onload = globalOnLoad;

/* ben's hide n' show javascript */
function getRef(obj){
  if(typeof obj == "string") {obj= document.getElementById(obj);}
  else if (typeof obj == "object") {obj = obj;}

  return obj;
}

function setStyle(obj, style, value){
  getRef(obj).style[style]= value;
}

function getStyle(obj, style){
  return getRef(obj).style[style];
}

function move(elem,x,y) {
  getRef(elem).style.left = x + "px";
  getRef(elem).style.top = y + "px";
}

function hide(elem)
{
  setStyle(elem,'display','none');
  setStyle(elem,'visibility','hidden');

}

function show(elem)
{
  setStyle(elem,'display','inline');
  setStyle(elem,'visibility','visible');
}

function showhide(elem) {
  if (getStyle(elem, 'visibility') == "hidden")
    show(elem);
  else
    hide(elem);

}

// Dan's FluidMind functions //
// Make sure the "FM" namespace object exists
if (typeof FM != 'object') {
    FM = new Object();
}

/**
 * Checks a given class attribute for the presence of a given class
 *
 * @author  Dan Delaney     http://fluidmind.org/
 * @param   element         DOM Element object (or element ID) to remove the class from
 * @param   nameOfClass     The name of the CSS class to check for
 */
FM.checkForClass = function(element, nameOfClass) {
    if (typeof element == 'string') { element = document.getElementById(element); }

    if (element.className == '') {
        return false;
    } else {
        return new RegExp('\\b' + nameOfClass + '\\b').test(element.className);
    }
}


/**
 * Adds a class to an element's class attribute
 *
 * @author  Dan Delaney     http://fluidmind.org/
 * @param   element         DOM Element object (or element ID) to add the class to
 * @param   nameOfClass     Class name to add
 * @see     checkForClass
 */
FM.addClass = function(element, nameOfClass) {

    if (typeof element == 'string') { element = document.getElementById(element); }

    if (!FM.checkForClass(element, nameOfClass)) {
        element.className += (element.className ? ' ' : '') + nameOfClass;
        return true;
    } else {
        return false;
    }
}


/**
 * Removes a class from an element's class attribute
 *
 * @author  Dan Delaney     http://fluidmind.org/
 * @param   element         DOM Element object (or element ID) to remove the class from
 * @param   nameOfClass     Class name to remove
 * @see     checkForClass
 */
FM.removeClass = function(element, nameOfClass) {

    if (typeof element == 'string') { element = document.getElementById(element); }

    if (FM.checkForClass(element, nameOfClass)) {
        element.className = element.className.replace(
            (element.className.indexOf(' ' + nameOfClass) >= 0 ? ' ' + nameOfClass : (
			 element.className.indexOf(nameOfClass + ' ') >= 0? nameOfClass + ' ' : nameOfClass)
			 ),
			'');
        return true;
    } else {
        return false;
    }
}


/**
 * Replaces a class with another if the class is present
 *
 * @author  Dan Delaney     http://fluidmind.org/
 * @param   element         DOM Element object (or element ID) to remove the class from
 * @param   class1          Class name to replace
 * @param   class2          Class name to replace it with
 * @see     checkForClass
 * @see     addClass
 * @see     removeClass
 */
FM.replaceClass = function(element, class1, class2) {

    if (typeof element == 'string') { element = document.getElementById(element); }

    if (FM.checkForClass(element, class1)) {
        FM.removeClass(element, class1);
        FM.addClass(element, class2);
        return true;
    } else {
        return false;
    }
}


/**
 * Toggles the specified class on and off
 *
 * @author  Dan Delaney     http://fluidmind.org/
 * @param   element         DOM Element object (or element ID) to toggle the class of
 * @param   nameOfClass     Class name to toggle
 * @see     checkForclass
 * @see     addClass
 * @see     removeClass
 */
FM.toggleClass = function(element, nameOfClass) {

    if (typeof element == 'string') { element = document.getElementById(element); }

    if (FM.checkForClass(element, nameOfClass)) {
        FM.removeClass(element, nameOfClass);
    } else {
        FM.addClass(element, nameOfClass);
    }

    return true;
}
// Dan's FluidMind functions //
