
/**
 * Returns true if the browser is IE 6
 * 
 * @return {Boolean}
 */
function browserIsIE6()
{
  return (navigator.userAgent.match(/MSIE 6\./)); 
}

function safeParseInt(x) {
  var cleanNumReg = new RegExp ('[^0-9\.]+', 'g') ;
  x=x.replace(cleanNumReg, '');
  if(x=='' || isNaN(x))
    return 0;
  return parseInt(x);
}

function safeParseFloat(x) {
  x=String(x);
  var cleanNumReg = new RegExp ('[^0-9\.]+', 'g') ;
  x=x.replace(cleanNumReg, '');
  if(x=='' || isNaN(x))
    return 0;
  return parseFloat(x);
}

/**
 * clears the value of a numeric field if it's value is equal to 0.
 * 
 * This function should be called in the onfocus event of a numeric input element.
 * 
 * If the initial value of the field is 0, you should add style="color: #999;" to the tag.
 */
function clearNumField(inputEl) {
  var amount = inputEl.value.replace(/[^0-9\.]/, '');
  if (amount == null || amount == '')
    amount = '0';
  
  amount = parseFloat(amount);  
  if (isNaN(amount))
    amount = 0;
  
  if (amount != 0)
    return;
  
  inputEl.value = '';
  inputEl.style.color = '#000';
}

/**
 * clears the value of a currency field if it's value is equal to $0.00.
 * 
 * This function should be called in the onfocus event of a currency input element.
 * 
 * If the initial value of the field is $0.00, you should add style="color: #999;" to the tag.
 */
function clearCurrencyField(inputEl) {
  clearNumField(inputEl);
}

/**
 * Apply numeric formatting to the value of an input field.
 * 
 * This function should be called in the onblur event of a numeric input element.
 * 
 * If the initial value of the field is 0, you should add style="color: #999;" to the tag.
 */
function formatNumField(inputEl) {
  var amount = inputEl.value.replace(/[^0-9\.]/, '');
  if (amount == '')
    amount = '0';
  
  amount = parseInt(amount);
  if (isNaN(amount))
    amount = 0;
  
  inputEl.value = amount;
  inputEl.style.color = (amount == 0) ? '#999' : '#000';
}

/**
 * Apply currency formatting to the value of an input field (eg: $42.00).
 * 
 * This function should be called in the onblur event of a currency input element.
 * 
 * If the initial value of the field is $0.00, you should add style="color: #999;" to the tag.
 */
function formatCurrencyField(inputEl) {
  
  var amount = inputEl.value.replace(/[^0-9\.]/, '');
  if (amount == '')
    amount = '0';
  
  amount = parseFloat(amount);  
  if (isNaN(amount))
    amount = 0;
  
  inputEl.value = '$' + amount.toFixed(2);
  inputEl.style.color = (amount == 0) ? '#999' : '#000';
}

// Target Blank Replacement

/*
JSTarget function by Roger Johansson, www.456bereastreet.com
*/
var JSTarget = {
	init: function(att,val,warning) {
		if (document.getElementById && document.createElement && document.appendChild) {
			var strAtt = ((typeof att == 'undefined') || (att == null)) ? 'class' : att;
			var strVal = ((typeof val == 'undefined') || (val == null)) ? 'non-html' : val;
			var strWarning = ((typeof warning == 'undefined') || (warning == null)) ? ' (opens in a new window)' : warning;
			var oWarning;
			var arrLinks = document.getElementsByTagName('a');
			var oLink;
			var oRegExp = new RegExp("(^|\\s)" + strVal + "(\\s|$)");
			for (var i = 0; i < arrLinks.length; i++) {
				oLink = arrLinks[i];
				if ((strAtt == 'class') && (oRegExp.test(oLink.className)) || (oRegExp.test(oLink.getAttribute(strAtt)))) {
					oWarning = document.createElement("em");
					oWarning.appendChild(document.createTextNode(strWarning));
					oLink.appendChild(oWarning);
					oLink.onclick = JSTarget.openWin;
				}
			}
			oWarning = null;
		}
	},
	openWin: function(e) {
		var event = (!e) ? window.event : e;
		if (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) return true;
		else {
		    var oWin = window.open(this.getAttribute('href'), '_blank');
			if (oWin) {
				if (oWin.focus) oWin.focus();
				return false;
			}
			oWin = null;
			return true;
		}
	},
	/*
	addEvent function from http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
	*/
	addEvent: function(obj, type, fn) {
		if (obj.addEventListener)
			obj.addEventListener(type, fn, false);
		else if (obj.attachEvent) {
			obj["e"+type+fn] = fn;
			obj[type+fn] = function() {obj["e"+type+fn]( window.event );}
			obj.attachEvent("on"+type, obj[type+fn]);
		}
	}
};
JSTarget.addEvent(window, 'load', function(){JSTarget.init("rel","external","");});

function BrowserIsIE7() { return (navigator.userAgent.match(/MSIE 7\./)); }
function BrowserIsIE6() { return (navigator.userAgent.match(/MSIE 6\./)); }

function ID(id){
  return document.getElementById(id);
}

var Flies = new Object;
Flies['nav'] = new Object;

function FlyOver(id,type) {

  for (var i in Flies[type]) {
    if (typeof Flies[type][i] == 'number') {
      FlyKill(i);
      clearTimeout(Flies[type][i]);
    }
  }
  Flies[type] = new Object;
  var n = ID(id);
  n.style.display = 'block';
  n.style.opacity = 1;
  n.style.filter = "alpha(opacity=100)";
  Flies[type][id] = true;
}

function FlyOut(id,type) {
  var n = ID(id);
  Flies[type][id] = setTimeout("FlyFade('"+id+"','"+type+"')", 500);
}

function FlyKill(id) {
  var n = ID(id);
  n.style.display = 'none';
}

function FlyFade(id,type) {
  var n = ID(id);
  var op = n.style.opacity;
  if (op < 0.1) {
    FlyKill(id);
    return;
  }
  op -= op / 10;
  n.style.filter = "alpha(opacity=" + (op * 100) + ")";
  n.style.opacity = op;
  Flies[type][id] = setTimeout("FlyFade('"+id+"','"+type+"')", 10);
}

function Show(id, hidetext) {
  var element = ById(id);
  if (!element) return false;
  var link = ById(id + '-link');
  if (element.style.display == 'block') {
    if (link) {
      link.innerHTML = link.getAttribute('showoriginaltext');
      link.style.backgroundImage = 'url(../images/g-arrow-dn.png)';
    }
    element.style.display = 'none';    
	$(id).previous('h3').down('a').removeClassName('highlight');
  }
  else {
    if (link && hidetext) {
      link.setAttribute('showoriginaltext', link.innerHTML);
      link.innerHTML = hidetext;
      link.style.backgroundImage = 'url(../images/g-arrow-up.png)';
    }
    element.style.display = 'block';
	$(id).previous('h3').down('a').addClassName('highlight');
  }
}

function ById(id) {
  if (!document.getElementById) return false;
  return document.getElementById(id);
}

fadeOutQuote = function() {
	new Effect.Opacity('header_quote', { from: 1, to: 0 });		
}
new Ajax.PeriodicalUpdater('header_quote', 'testimonials.random', {
  method: 'get', frequency: 8, decay: 2, evalJS: true,
  onSuccess: function() {
	new Effect.Opacity('header_quote', { from: 0, to: 1 });	
	setTimeout(fadeOutQuote,7000);
  }
});
