
function jsvalidate( _formName )
{	valerror = false;
	var imgextArray = new Array("jpg", "jpeg", "gif", "png", "bmp");
	var docextArray = new Array ('txt', 'rtf', 'doc', 'docx', 'pdf', 'xls', 'xlsx', 'pps', 'ppt', 'pptx');
	var medextArray = new Array ('avi','mpg','mpeg','wmv','mp4','mov','flv','mp3','m4v','xls','xlsx','ppt');
	
	//var emailreg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var emailreg = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
	var hasFiles = 0;
	
	// Remove old error boxes!
	$$('#'+_formName+' .formError').each(function(s){	s.remove(); });
	$$('#'+_formName+' input.error').each(function(s){	s.removeClassName('error'); });
	$$('#'+_formName+' .formError').each(function(s){ Event.stopObserving(s, 'mouseover'); });

	$$('#'+_formName+' .required').each(function(s)
	{	if( s.readonly != true && !s.value )
		{	//alert('Please enter ' + s.title);
			alertBawx(s,_formName,'Please enter '+s.title);
			throw $break;
		}
	});
	if (valerror != true)
	{	$$('#'+_formName+' .state').each(function(s)
		{	if( $$('#'+_formName+' select#country').length == 0 || ($$('#'+_formName+' select#country').length > 0 && $$('#'+_formName+' select#country')[0].value == 'United States') )
			{	if( !s.value) 
				{	alertBawx(s,_formName,'Please select a state if you are residing within the United States.');
					throw $break;
				}
			}
		});
	}
	if (valerror != true)
	{	$$('#'+_formName+' .email').each(function(s)
		{	if(s.value) 
			{	if( s.value.search(emailreg) != 0 )
				{	alertBawx(s,_formName,'Sorry but the e-mail you have entered does not appear to be a real e-mail address. Please verify that you have entered it correctly. (Example: youremail@address.com)');
					throw $break;
				}
			}
		});
	}
	if (valerror != true)
	{	$$('#'+_formName+' .repassword').each(function(s, x)
		{	if(s.value) 
			{	if( s.value != $$('#'+_formName+' .password')[x].value )
				{	//alert('The password you have re-entered doesn\'t match the original password.');
					alertBawx(s,_formName,'The password you have re-entered doesn\'t match the original password.');
					throw $break;
				}
			}
		});
	}
	
	if (valerror != true)
	{	$$('#'+_formName+' .filetypeimg').each(function(s, x)
		{	if(s.value) 
			{	_file = s.value;
				
				allowSubmit = false;
				
				while (_file.indexOf("\\") != -1)
					_file = _file.slice(_file.indexOf("\\") + 1);
					
				ext = _file.slice(_file.lastIndexOf(".")).toLowerCase();
				ext = ext.replace('.','');
				
				for (var i = 0; i < imgextArray.length; i++) {
					if (imgextArray[i] == ext) { allowSubmit = true; break; }
				}
				
				if ( !allowSubmit)
				{	alertBawx(s,_formName,"Please only upload files that end in the following extensions: "
						+ (imgextArray.join("  ")) + "\n\n Select a new "
						+ "file to upload and submit again.");
					throw $break;
				}
				
				hasFiles++;
			}
		});
	}
	if (valerror != true)
	{	$$('#'+_formName+' .filetypedoc').each(function(s, x)
		{	if(s.value) 
			{	_file = s.value;
				
				allowSubmit = false;
				
				while (_file.indexOf("\\") != -1)
					_file = _file.slice(_file.indexOf("\\") + 1);
					
				
				ext = _file.slice(_file.lastIndexOf(".")).toLowerCase();
				ext = ext.replace('.','');
				
				for (var i = 0; i < docextArray.length; i++) {
					if (docextArray[i] == ext) { allowSubmit = true; break; }
				}
				
				if ( !allowSubmit)
				{	alertBawx(s,_formName,"Please only upload files that end in the following extensions: "
						+ (docextArray.join("  ")) + "\n\n Select a new "
						+ "file to upload and submit again.");
					throw $break;
				}
				
				hasFiles++;
			}
		});
	}
	if (valerror != true)
	{	$$('#'+_formName+' .filetypemedia').each(function(s, x)
		{	if(s.value) 
			{	_file = s.value;
				
				allowSubmit = false;
				
				while (_file.indexOf("\\") != -1)
					_file = _file.slice(_file.indexOf("\\") + 1);
					
				ext = _file.slice(_file.lastIndexOf(".")).toLowerCase();
				ext = ext.replace('.','');
				
				for (var i = 0; i < medextArray.length; i++) {
					if (medextArray[i] == ext) { allowSubmit = true; break; }
				}
				
				if ( !allowSubmit)
				{	alertBawx(s,_formName,"Please only upload files that end in the following extensions: "
						+ (medextArray.join("  ")) + "\n\n Select a new "
						+ "file to upload and submit again.");
					throw $break;
				}
				
				hasFiles++;
			}
		});
	}
	
	if (valerror == true)
		return false;
	else
	{	
		if( hasFiles > 0 )
		{	overheight = $$('body')[0].getHeight();
			if(overheight < document.viewport.getHeight())
				overheight = document.viewport.getHeight();
			
			$(document.body).insert(new Element('div', { 'id': 'uploading' }).setStyle({ height: overheight + 'px' }) );
			
			$('uploading').innerHTML = '<p>Uploading files. Please wait.</p>';
			//$$('body')[0].scrollTo();
		}
		return true;
	}
}

function alertBawx(parent,_formName,msg)
{	newAlert = new Element('div');
	newAlert.update(msg);
	newAlert.addClassName('formError');
	
	parent.identify();
	parent.addClassName('error');
	parent.insert({'after':newAlert});
	
	//newAlert.setStyle({ bottom: '-'+(newAlert.style.bottom-10)+'px'});
	//$$('#'+_formName+' .formError')[0].style.bottom = '-'+($$('#'+_formName+' .formError')[0].clientHeight-8)+'px';
	
	Event.observe( $$('#'+_formName+' .formError')[0], 'mouseover', function(event){ $$('#'+_formName+' .formError')[0].remove() });
	//$$('#'+_formName+' .formError')[0].onMouseOver = 'hide(this);';
	//$$('#'+_formName+' .formError')[0].onMouseOut = 'show(this);';
	
	
	valerror = true;
	parent.focus();
}

