//------------------------------------
//Online Email Form Validation Scripts
//------------------------------------
function MM_findObj(n, d) { //v4.0
  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 && document.getElementById) x=document.getElementById(n); return x;
}

function MM_validateEmailForm(form) { //v4.0
  //Note: first arg passed to function is the form itself; all other args are field names.

  var bRes,i,p,q,nm,test,num,min,max,errors = '',args = MM_validateEmailForm.arguments;
  var fld;
  var res = false;
  var bEmailRequired = false;

  selectErrors = checkEmailFormDropDowns(form, args); //check all select boxes in form
  //alert("args[0]: " + args[0].name);

  for (i = 1; i < (args.length - 2); i += 3) {
	//alert("args[" + i + "]: " + args[i]);

	test = args[i + 2]; obj = MM_findObj(args[i]);

	//alert("name: \'"+obj.name+"\' title: \'"+obj.title+"\' type: \'"+obj.type+"\' value: \'"+obj.value+"\'"+"\' test: \'"+test.indexOf('isEmail')+"\'");

	if (obj) {
	  nm = obj.name;
	  if ((val = obj.value) != "") {
		if (test.indexOf('isEmail') != -1) {  //check string should be: 'email','','isEmail' as opposed to 'email','','R'
		  bRes = checkEmailFormEmailAddress(form, nm, bEmailRequired);
		  p = val.indexOf('@');
		  if (p < 1 || p == (val.length - 1) || (bRes == false) && !bEmailRequired) errors += '- ' + nm + ' must contain a valid e-mail address.\n';
		  bEmailRequired = false;
		} else if (test != "R") {
		  if (isNaN(val)) errors += '- ' + nm + ' must contain a number.\n';
		  if (test.indexOf('inRange') != -1) {
			p = test.indexOf(':');
			min = test.substring(8, p);
			max = test.substring(p + 1);
			if (val < min || max < val) errors += '- ' + nm + ' must contain a number between ' + min + ' and ' + max + '.\n';
		  }
		}
	  } else if (test.charAt(0) == "R") {	//value is empty and required
      	if (obj.disabled != true) {
	  	  if (nm.indexOf('email') != -1) bEmailRequired = true;
	  	  errors += '- ' + nm + ' is required.\n';
	  	}
	  }
	}
  }
  errors += selectErrors;  //add them to the errors alert
  if (errors) alert('The following error(s) occurred:\n' + errors);
  document.MM_returnValue = (errors == '');
}

function checkEmailFormEmailAddress(form, name, bEmailRequired) {
  //alert("form: " + form.name);
  //if (name != null) alert("name: " + name);

  var fld,fieldname,fieldvalue,fieldtabindex,fieldtype;
  var res = false;

  if (name != null) {  //process given field only
	fld = form.elements[name];
	if (fld.type == "text") {
	  if (fld.name.indexOf("email") > -1) {
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,9})+$/.test(fld.value)) {
		  res = true;
		}
		//if (!res) alert("Invalid E-mail Address! Please re-enter.");
	  }
	}
  } else {            // process whole form
	for(i=0; i<form.elements.length-1; i++)
	{
	  fld = form.elements[i];
	  if (fld.type == "text") {
		if (fld.name.indexOf("email") > -1) {
		  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,9})+$/.test(fld.value)) {
			res = true;
		  }
		  //if (!res) alert("Invalid E-mail Address! Please re-enter.");
		}
	  }
	}
  }
  return (res)
}

function checkEmailFormDropDowns(form,args) {
  var returnThis = "";
  var val;
  for (i = 1; i < (args.length - 2); i += 3) {
	  //alert("args[" + i + "]: " + args[i]);
	  test = args[i + 2]; val = MM_findObj(args[i]);
	  //alert("name: \'"+val.name+"\' title: \'"+val.title+"\' type: \'"+val.type+"\' value: \'"+val.value+"\'"+"\' test: \'"+test.indexOf('isEmail')+"\'");
	  if (val) {
		  if (val.type == 'select-one') {
				//alert("field: " + val.name + ", fieldType: " + val.type + ", value: " + val.value);
				if (val.value == 'Select...' || val.value == '(select one)') returnThis += "- " + val.name + " you must select from this dropdown.\n";
		  }
	}
  }
  //alert("checkEmailFormDropDowns returned: " + returnThis);
  return returnThis;
}

function textEmailFormCounter(field, countfield, maxlimit) {
  if (field.value.length > maxlimit) {
	field.value = field.value.substring(0, maxlimit);
	alert('Textarea value can only be 2000 characters in length.');
	return false;
  } else {
	countfield.value = maxlimit - field.value.length;
  }
}
