function checkform(of) {
	var reqfields,em,i,f,ty;
	if(getObject('required')) {
		reqfields=getObject('required').value.split(',');
		if(getObject('alertMessage')) {
			em=getObject('alertMessage');
			em.parentNode.removeChild(em);
		}
		for(i=0;i<reqfields.length;i++) {
			f=getObject(reqfields[i]);
			if(f) {
				f.parentNode.id = "";
				ty=f.type.toLowerCase();
				switch(ty) {
					case 'text':
						if(f.value==''){adderr(f.id,of,'We were unable to process your form.','Please review the highlighted fields and resubmit.')}
						else if(f.id=='email' && !isEmailAddr(f.value)){adderr(f.id,of,'We were unable to process your form.','Please review the highlighted fields and resubmit.')}
						else if(f.id=='phone'){
							if(!isPhone(f.value)){adderr(f.id,of,'We were unable to process your form.','Please review the highlighted fields and resubmit.');}
							else{f.value=isPhone(f.value);}
						}
					break;
					case 'textarea':
						if(f.value==''){adderr(f.id,of,'We were unable to process your form.','Please review the highlighted fields and resubmit.')}			
					break;
					case 'password':
						if(f.value==''){adderr(f.id,of,'We were unable to process your form.','Please review the highlighted fields and resubmit.')}	
					break;
					case 'checkbox':
						if(!f.checked){adderr(f.id,of,'We were unable to process your form.','Please review the highlighted fields and resubmit.')}							
					break;
					case 'select-one':
						if(f.options[f.selectedIndex].value==''){adderr(f.id,of,'We were unable to process your form.','Please review the highlighted fields and resubmit.')}
					break;	
				}
			}
		}
	}
	if(getObject('alertMessage')) {
		return false;
	}
}
function adderr(id,of,msg,notes) {
	var se,i,nli,na;
	se=getObject(id);
	se.parentNode.id = "alerted";
	if(!getObject('alertMessage')){
		var alertDiv = document.createElement('div');
		alertDiv.id = "alertMessage";
		alertDiv.className = "container";
		var alertIcon = document.createElement('div');
		alertIcon.id = "alertIcon";
		var alertImg = document.createElement('img');
		alertImg.src = "http://www.cns-software.com/img/alertIcon.gif";
		alertImg.alt = "We were unable to process your form. Please review the highlighted fields and resubmit.";
		var em=document.createElement('p');
		var noteSpan = document.createElement('span');
		noteSpan.className = "notes";
		alertIcon.appendChild(alertImg);
		em.appendChild(document.createTextNode(msg));
		noteSpan.appendChild(document.createTextNode(notes));
		em.appendChild(noteSpan);
		alertDiv.appendChild(alertIcon);
		alertDiv.appendChild(em);
		of.parentNode.insertBefore(alertDiv,of);
	}
}

function isEmailAddr(str) {
    return str.match(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/);
}
function isPhone(str) {
	str = str.replace( /[^0-9]/g, "" ); // Strip out non-numerics 
	var reg = /^([0-9]{3})([0-9]{3})([0-9]{4})$/;
	if (vals = reg.exec(str))                                  
		return "(" + vals[1] + ") " + vals[2] + "-" + vals[3];
	else
		return false;
}
function leaveDigits(master) {
		master.value=strip(master.value);
}
function strip(_str) {
	var STR="";
	var _exp =/\d+|\./g;
	var _array;
	while(_array=_exp.exec(_str)) {
		STR+=_array.join();
	}
	return(STR);
}
