var bValErrs;

function validateSig(sigForm) {
  var fName = document.getElementById('inpSignatory');
  var fAddress = document.getElementById('inpAddress');
  
  clearValErrs(sigForm);
  
  if (!fName.value) {
    valErr(fName, "You need to enter a name!");
  }
  if (!fAddress.value) {
    valErr(fAddress, "A valid email address is required (see the fine print for reasons)!");
  } else if (!validateEmail(fAddress.value)) {
    valErr(fAddress, "A valid email address is required. Sorry, this doesn't seem to be a valid address.");
  }
    
  return !bValErrs;
}
function validateFwd(fwdForm) {
	var fEmails = fwdForm.getElementsByTagName('input');

	clearValErrs(fwdForm);

	for(var i=0;i<fEmails.length;i++) {
		if (fEmails[i].getAttribute("type") == "text") {
			if (fEmails[i].value && !validateEmail(fEmails[i].value)) {
				valErr(fEmails[i], "The address at input "+i+" appears invalid. Please re-enter it.");
			}
		}
	}
	return !bValErrs;
}
function valErr(field, msg) {
	bValErrs = true;
  field.parentNode.className = "invalid";
  field.scrollIntoView(false);
  alert(msg);
}
function clearValErrs(sigForm) {
	bValErrs = false;
  var spans = sigForm.getElementsByTagName("span");
  for (var i=0;i<spans.length;i++) {
    spans[i].className = "";
  }
}
function validateEmail(addr) {
	var re = new RegExp("^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$");
  return re.exec(addr);
}

function toggleNames() {
  var h = document.getElementById("sigHeader");
  var d = document.getElementById("signatories");
	var o = document.getElementById("sigOrder");
  var a = document.getElementById("toggleA");
  
  if (h.style.display != 'none') {
    h.style.display = 'none';
    d.style.display = 'none';
		o.style.display = 'none';
    a.innerHTML = "show them?";
  } else {
    h.style.display = 'block';
    d.style.display = 'block';
		o.style.display = 'inline';
    a.innerHTML = "hide them?";
  }
}

function addYours() {
	var inp = document.getElementById('inpSignatory');
	inp.scrollIntoView();
	inp.focus();
	return false;
}
