/*
This function can be used to add unlimited window.onload events.
*/
function addLoadEvent(func) {
    var oldonload = window.onload;
        if (typeof window.onload != 'function') {
            window.onload = func;
        } else {
            window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

/*
This function is used to disable the form to help prevent 
double submits.

Usage: 
<form onsubmit="submitAndDisableForm(this);return void;">
*/
function submitAndDisableForm(form)
{
    form.submit();
    var elements = form.elements();
    for(var i=0;i<elements.length;i++) {
        elements[i].disabled=true;
    }
}

/*
This function can be used to disable the "Enter" key for 
stricter control over forms.
Usage:
document.onkeypress = disableEnterKey;
*/
function disableEnterKey(e)
{
     var key;
     if(window.event)
          key = window.event.keyCode;     //IE
     else
          key = e.which;     //firefox

     if(key == 13)
          return false;
     else
          return true;
}

function popupImage(image, width, height, title, alt)
{
	lPos = (screen.width) ? (screen.width-width)/2 : 0;
	tPos = (screen.height) ? (screen.height-height)/2 : 0;
	title = title ? title:'Image Popup';
	alt = alt ? alt:'';

	newWin = this.open('', '_blank', "toolbar=no,width="+width+",height="+height+",left="+lPos+",top="+tPos+",directories=no,status=no,scrollbars=no,resize=no,menubar=no");
	var tmp = newWin.document;

	tmp.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1-transitional.dtd">');
	tmp.write('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>'+title+'</title>');
	tmp.write('<style type="text/css">* {padding:0px; margin:0px;}</style></head>');
	tmp.write('<body><img src="'+image+'" width="'+width+'" height="'+height+'" alt="'+alt+'" /></body></html>');

	tmp.close();
	newWin.focus();
}

function popupWindow(url, width, height)
{
	lPos = (screen.width) ? (screen.width-width)/2 : 0;
	tPos = (screen.height) ? (screen.height-height)/2 : 0;

	newWin = this.open('', '_blank', "toolbar=no,width="+width+",height="+height+",left="+lPos+",top="+tPos+",directories=no,status=no,scrollbars=yes,resize=no,menubar=no");
	newWin.location = url;
	newWin.focus();
}
function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;

	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function joinList() {
	window.open( 'http://oi.vresp.com?fid=8d742e28a8', 'vr_optin_popup', 'scrollbars=yes,width=600,height=450' ); 
	document.getElementById('mailinglist').submit;
}

function getCheckedValue(radioObj) {
    if(!radioObj)
        return "";
    var radioLength = radioObj.length;
    if(radioLength == undefined)
        if(radioObj.checked)
            return radioObj.value;
        else
            return "";
    for(var i = 0; i < radioLength; i++) {
        if(radioObj[i].checked) {
            return radioObj[i].value;
        }
    }
    return "";
}

function setCheckedValue(radioObj, newValue) {
    if(!radioObj)
        return;
    var radioLength = radioObj.length;
    if(radioLength == undefined) {
        radioObj.checked = (radioObj.value == newValue.toString());
        return;
    }
    for(var i = 0; i < radioLength; i++) {
        radioObj[i].checked = false;
        if(radioObj[i].value == newValue.toString()) {
            radioObj[i].checked = true;
        }
    }
}

/**
 * usage:
 *
 * var limiter = new CharacterLimit(150, document.getElementById('some-input-field'), document.getElementById('some-element'));
 * limiter.update();
 */
CharacterLimit = function(limit, field, track)
{
	if(typeof field != 'object') {
		field = document.getElementById(field);
	}
	if(typeof track != 'object') {
		track = document.getElementById(track);
	}

    this.limit = limit;
    this.field = field;
    this.track = track || false;

    var tmp = this;
    this.field.onkeyup = function() { tmp.update(); }

    this.update = function() {
        if(this.field.value.length > this.limit) {
            alert('Character limit has been reached.');
            this.field.value = this.field.value.substring(0, this.limit);
        }

        if(this.track) {
            this.track.innerHTML = '' + this.limit - this.field.value.length;
        }
    }
}

/**
 * usage:
 * new SupressNewline(element);
 * @param element - Either the document element to supress newlines on (ie the body element or a form field) or the 
 *		id of an element to supress new line characters on.
 */
SupressNewline = function(el)
{
	if(typeof el != 'object') {
		el = document.getElementById(el);
	}

	el.onkeydown = function(ev)
	{
		var key;
		if(window.event)
			key = window.event.keyCode;     //IE
		else
			key = ev.which;     //firefox

		if(key == 13) {
			return false;
		}
		else {
			return true;
		}
	}
}
