function isblank(s)
{
	
	for(var i=0;i<s.length;i++)
	{
		var c = s.charAt(i);
		
		if((c!=' ') && (c!='\n') && (c!='\t'))
	        return true;	
	}
	return false;
}

function isemail(email)
{
	invalidChars = " /:,;"

	// Check if it's empty			
	if (email=="")
		return false;
	
	// Check if it has invalid characters
	for (i=0; i<invalidChars.length; i++)
	{
		badChar = invalidChars.charAt(i)
		if (email.indexOf(badChar,0)>-1)
			return false;
	}
	
	// Check if it contains an @
	atPos = email.indexOf("@",1)
	if (atPos==-1)
		return false;
		
	// only one @
	atPos = email.indexOf("@",atPos+1)
	if (atPos>-1)
		return false;
	
	// Check if it contains a period after @
	periodPos = email.indexOf(".", atPos)
	if (periodPos==-1)
		return false;
		
	// At least 2 characters after the period
	if (periodPos+3 > email.length)
		return false;
		
	// everything ok
	return true;
}

function MailValidator(f)
{
	target = /REQ_/;
	target2 = /_/g;
	var empty_fields=""
	var fieldfocus = null;
	var temp="";
	
	for(var i=0; i<f.length; i++)
	{
		var e=f.elements[i];
		 
		 if ((e.selectedIndex=="")||(e.selectedIndex==0))
		 	{
				temp2=e.name;
				empty_fields+="\n        " + temp2;
				
				if (fieldfocus==null)
					fieldfocus = e;
			}
		// first check if the field is text or textarea and if it is required

		if(((e.type=="text")||(e.type=="textarea")) && (e.name.search(target)==0))
		{
			// finally check if the field is empty
			if((e.value==null) || (e.value=="") || !isblank(e.value))
			{
  				temp = e.name.replace(target, "");	// Erase "REQ_" from string
				temp = temp.replace(target2, " ");	// Replace "_" with spaces
  				empty_fields+="\n        " + temp;
				
				if (fieldfocus==null)
					fieldfocus = e;
			}	
			
			if (e.email)
				{
					
					if (!isemail(e.value))
					{
						temp = e.name.replace(target, "");	// Erase "REQ_" from string
						temp = temp.replace(target2, " ");	
					    empty_fields+="\nThe  " + temp + " field must be a valid email.\n";
						fieldfocus = e;
					}
				}
			
		}
	}
	if (empty_fields)
	{
		alert ("The following required field(s) are empty:" + empty_fields + "\n");
		fieldfocus.focus()
		return false;
	}

	return true;
}