//--------------------------------------------------------------------------------------------------------
// FUNCTION: Set error messages
//--------------------------------------------------------------------------------------------------------
function setError(field, div, color) { 
	errorMsg = true; 
	field.style.backgroundColor = color;
	field.style.color = '#ffffff';
	div.style.visibility="visible";
	div.style.display="block";
	field.focus();
}

//--------------------------------------------------------------------------------------------------------
// Validates a form and returns a value indicating if validation passed or failed
//--------------------------------------------------------------------------------------------------------
function validateForm() {
	
	errorMsg = false;
	var colorOK = "white";
	var colorMissing = "#bc2d33";
	var colorError = "#bc2d33";
	
	//  HIDE ERROR MESSAGES
	document.getElementById("divNoError").style.display="none";
	document.getElementById("divMustEnter").style.display="none";
	document.getElementById("divEmailInv").style.display="none";


    var i, req, field;
	var args=validateForm.arguments;
	
	//  loop thru all the passed arguments: 1st variable is FIELD NAME, 2nd varible is "R" if required
  	for (i=0;  i<(args.length-1); i+=2) { 
		req=args[i+1];
		field = MM_findObj(args[i]);
    	if (field) { 
			field.style.backgroundColor = colorOK;
			field.style.color = '#000000';
			if ( (field.value=="") || (field.value==null) )  {
				if (req=='R') {
					setError(field, document.getElementById("divMustEnter"), colorMissing);
				}
			}
			else {   // FIELD WAS ENTERED - DO MORE CHECKS...
				
 
				// VALIDATE EMAIL ADDRESS	
				if (field.name=="E-mail")  {  
					//var reEmailPattern = /^(\w+[\-\.])*\w+@(\w+\.)+[A-Za-z]+$/;
					var reEmailPattern  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
					//var reEmailPattern = new RegExp("^(\\w(?:\\w|\\.\\w)*)@(\\w+\\.\\w(?:\\w|\\.\\w)*)$"); 
					if (!reEmailPattern.test(field.value)) 
						setError(field, document.getElementById("divEmailInv"), colorError);
				}
				
			
			}  //  end ELSE
		}  // end IF
	}  // end FOR

	if (errorMsg==false)
		return true;
	else 
		return false;
}

//--------------------------------------------------------------------------------------------------------
//  Finds the field for the passed object
//--------------------------------------------------------------------------------------------------------
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
