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

/**
 * Returns the value of a form field interpreted as an integer.
 * 
 * @param {String} id
 */
function $Fi(id)
{
  var value = $F(id);
  return safeParseInt(value); 
}

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';
}

/**
 * Creates a date from an ISO formatted string.
 * 
 * @param {String} date
 * @return {Date}
 */
Date.fromISO = function(date)
{
  var a = date.split('-');
  return new Date(a[0], a[1]-1, a[2]);
}

// 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","");});

/**
* Returns the value of the selected radio button in the radio group, null if
* none are selected, and false if the button group doesn't exist
*
* Based on Prototype's $F() function, which because it takes an element
* ID as the argument, does not work for radio buttons.
*
* Requires Prototype. 
*
* @param {radio Object} or {radio id} el
* OR
* @param {form Object} or {form id} el
* @param {radio group name} radioGroup
*/
function $RF(el, radioGroup) {
    if($(el).type && $(el).type.toLowerCase() == 'radio') {
        var radioGroup = $(el).name;
        var el = $(el).form;
    } else if ($(el).tagName.toLowerCase() != 'form') {
        return false;
    }
 
    var checked = $(el).getInputs('radio', radioGroup).find(
        function(re) {return re.checked;}
    );
    return (checked) ? $F(checked) : null;
}
