/**
  * Common JS From Old IRE Site
  */
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

var preloadFlag = false;
function preloadImages() {
	if (document.images) {
	/*
		ire_nav_about = newImage("images/nav_about-over.gif");
		ire_nav_contact = newImage("images/nav_contact-over.gif");
		ire_nav_home = newImage("images/nav_home-over.gif");
		ire_nav_mailinglist = newImage("images/nav_mailinglist-over.gif");
		ire_nav_programs = newImage("images/nav_programs-over.gif");
		ire_nav_quote = newImage("images/nav_quote-over.gif");
		ire_nav_transactions = newImage("images/nav_transactions-over.gif");
	*/
		preloadFlag = true;
	}
}


/**
 * Basic Utility Functons
 */
if (!Array.prototype.forEach) {
  Array.prototype.forEach = function(func, scope) {
    scope = scope || this;
    for (var i = 0, l = this.length; i < l; i++)
      func.call(scope, this[i], i, this); 
  }
}

if (!Array.prototype.map) {
  Array.prototype.map = function(func, scope) {
    scope = scope || this;
    var list = [];
    for (var i = 0, l = this.length; i < l; i++)
        list.push(func.call(scope, this[i], i, this)); 
    return list;
  }
}

if (!Array.prototype.filter) {
  Array.prototype.filter = function(func, scope) {
    scope = scope || this;
    var list = [];
    for (var i = 0, l = this.length; i < l; i++)
        if (func.call(scope, this[i], i, this)) list.push(this[i]); 
    return list;
  }
}

['forEach', 'map', 'filter', 'slice', 'concat'].forEach(function(func) {
    if (!Array[func]) Array[func] = function(object) {
      return this.prototype[func].apply(object, Array.prototype.slice.call(arguments, 1));
    }
});

if (typeof window.Prototype == 'undefined') {
function DOLLAR() {
  var results = [], element;
  Array.map(arguments, function(element) {
    if (typeof element == 'string')
      element = document.getElementById(element);
    results.push(element);
  });
  return results.length == 1 ? results[0] : results;
}

try {
	window.$ = DOLLAR;
} catch (e) {}

}

function $$(className, context) {
  context = context || document;
  var nodeList;
  
  if (context == document || context.nodeType == 1) {
    if (typeof document.evaluate == 'function') {
      var xpath = document.evaluate(".//*[contains(concat(' ', @class, ' '), ' " + className + " ')]", 
                                    context, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
      var els = [];
      for (var i = 0, l = xpath.snapshotLength; i < l; i++)
       els.push(xpath.snapshotItem(i));
      return els;
    } else nodeList = context.getElementsByTagName('*');
  } else nodeList = context;
  
  var re = new RegExp('(^|\\s)' + className + '(\\s|$)');
  return Array.filter(nodeList, function(node) {  return node.className.match(re) });
}
/* End Basic Utilities */


/**
 *	Visibility Toggles
 */
var sgToggleVis = {
	
	showHide: function(ref) {
		el = (typeof ref == 'string') ? $(ref) : ref;
		if (el.style.display == 'none')
			sgToggleVis.show(el);
		else 
			sgToggleVis.hide(el);
	},
	
	show: function(ref) {
		el = (typeof ref == 'string') ? $(ref) : ref;
		el.style.display = 'block';
	},
	
	showAll: function(cn,context) {
		var args = $$(cn,context);
		for (i=0;i<args.length;i++) {
			sgToggleVis.show(args[i]);
		}
	},
	
	showOnly: function(ref,cn,context) {
		el = (typeof ref == 'string') ? $(ref) : ref;
		sgToggleVis.hideAll(cn,context);
		sgToggleVis.show(el);
	}, 	
	
	hide: function(ref) {
		el = (typeof ref == 'string') ? $(ref) : ref;
		el.style.display = 'none';
	},
	
	hideAll: function(cn,context) {
		var args = $$(cn,context);
		for(i=0;i<args.length;i++) {
			sgToggleVis.hide(args[i]);
		}		
	},

	hideOnly: function(ref,cn,context) {
		el = (typeof ref == 'string') ? $(ref) : ref;
		sgToggleVis.showAll(cn,context);
		sgToggleVis.hide(el);
	}

}
/* End Visibility Toggles */

/* Clean Urls */
function sg_urlSafeStr(str) {

	// Future addition... Map non-ascii chars to their english equivalents.
	// ie.. &uuml; -> u (Ÿ -> u).
	
	reg = /\s/ig
	str = str.replace(reg,'-');
	
	reg = /[^a-zA-Z0-9\-]/ig
	str = str.replace(reg,'');
	
	reg = /[\-]{2,}/ig
	str = str.replace(reg, '-');
	
	reg = /^\-+|\-+$/ig
	str = str.replace(reg, '');
	
	return str;
	
}

function sg_setCleanValue(s,ta,frm) {
	var formObj = document.forms[frm]
	var el = formObj.elements[ta]
	
	el.value = sg_urlSafeStr(s.value);
	el.disabled = true

}

function sg_enableField(ta,frm) {
	var formObj = document.forms[frm]
	var el = formObj.elements[ta]
	
	el.disabled = false
	
	return true;
}

function sg_disableField(ta,frm) {
	var formObj = document.forms[frm]
	var el = formObj.elements[ta]
	
	el.disabled = true
	
	return true;
}

/* Basic Form Related */
function sg_submitForm(name) {
	if (!document.forms[name])
		return false;
	
	var formObj = document.forms[name];
	formObj.submit();
	
	return false;	
	
}

/* Pop up window */
function sgWindow(url,w,h,name) {
	url = SG_URL_BASE + url;
	
	top.mWindow = window.open(url,name,'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width=' + w + ',height=' + h + ',screenX=' + w +',screenY=' + h +',top=50,left=50')

	return;

}  


// written by Dean Edwards, 2005
// with input from Tino Zijdel, Matthias Miller, Diego Perini

// http://dean.edwards.name/weblog/2005/10/add-event/

function addEvent(element, type, handler) {
	if (element.addEventListener) {
		element.addEventListener(type, handler, false);
	} else {
		// assign each event handler a unique ID
		if (!handler.$$guid) handler.$$guid = addEvent.guid++;
		// create a hash table of event types for the element
		if (!element.events) element.events = {};
		// create a hash table of event handlers for each element/event pair
		var handlers = element.events[type];
		if (!handlers) {
			handlers = element.events[type] = {};
			// store the existing event handler (if there is one)
			if (element["on" + type]) {
				handlers[0] = element["on" + type];
			}
		}
		// store the event handler in the hash table
		handlers[handler.$$guid] = handler;
		// assign a global event handler to do all the work
		element["on" + type] = handleEvent;
	}
};
// a counter used to create unique IDs
addEvent.guid = 1;

function removeEvent(element, type, handler) {
	if (element.removeEventListener) {
		element.removeEventListener(type, handler, false);
	} else {
		// delete the event handler from the hash table
		if (element.events && element.events[type]) {
			delete element.events[type][handler.$$guid];
		}
	}
};

function handleEvent(event) {
	var returnValue = true;
	// grab the event object (IE uses a global event object)
	event = event || fixEvent(((this.ownerDocument || this.document || this).parentWindow || window).event);
	// get a reference to the hash table of event handlers
	var handlers = this.events[event.type];
	// execute each event handler
	for (var i in handlers) {
		this.$$handleEvent = handlers[i];
		if (this.$$handleEvent(event) === false) {
			returnValue = false;
		}
	}
	return returnValue;
};

function fixEvent(event) {
	// add W3C standard event methods
	event.preventDefault = fixEvent.preventDefault;
	event.stopPropagation = fixEvent.stopPropagation;
	return event;
};

fixEvent.preventDefault = function() {
	this.returnValue = false;
};

fixEvent.stopPropagation = function() {
	this.cancelBubble = true;
};


function safeEmail(name,domain) {
	document.write('<a href=\"mailto:' + name + '@' + domain + '\">');
	document.write(name + '@' + domain + '</a>');
}
