function toggleDiv(divName) {
	thisDiv = document.getElementById(divName);
	if (thisDiv) {
		if (thisDiv.offsetHeight > 0 || thisDiv.style.display == "block") {
			thisDiv.style.display = "none";
		}
		else {
			thisDiv.style.display = "block";
		}
	}
	else {
		errorString = "Error: Could not locate div with id: " + divName;
		alert(errorString);
	}
}

function toggleCheckbox(thisCheckbox) {
	thisCheckbox.checked = !(thisCheckbox.checked);
}

function toggleCheckboxById(thisCheckbox) {
	document.getElementById(thisCheckbox).checked = !(document.getElementById(thisCheckbox).checked);
}

function toggleCheckboxValue(thisCheckbox, thisValue) {
	for (i = 0; i < thisCheckbox.length; $i++) {
		if (thisCheckbox[i].value == thisValue) {
			thisCheckbox.checked = !(thisCheckbox.checked);
		}
	}
}

function setSelectedRadioButton(radioArray, radioValue) {
	for (i = 0; radioArray[i]; i++) {
		if (radioArray[i].value == radioValue) {
			radioArray[i].checked = true;
		}
	}
}

function getSelectedRadioButton(radioArray) {
	for (i = 0; radioArray[i]; i++) {
		if (radioArray[i].checked == true) {
			return radioArray[i].value;
		}
	}
	return false;
}

function setSelectToValue(thisSelect, thisValue) {
	for (i = 0; thisSelect[i]; i++) {
		if (thisSelect[i].value == thisValue) {
			thisSelect[i].selected = true;
		}
	}
}

// form validation functions

var nbsp = 160;
var node_text = 3;
var emptyString = /^\s*$/
var glb_vfld;
function trim(str) {
  return str.replace(/^\s+|\s+$/g, '')
};

function setFocusDelayed() {
  glb_vfld.focus()
}

function setfocus(vfld) {
  glb_vfld = vfld;
  setTimeout( 'setFocusDelayed()', 100 );
}

function msg(fld, msgtype, message) {
  var dispmessage;
  if (emptyString.test(message)) 
    dispmessage = String.fromCharCode(nbsp);    
  else  
    dispmessage = message;

  var elem = document.getElementById(fld);
  elem.firstChild.nodeValue = dispmessage;  
  
  elem.className = msgtype;
};

var proceed = 2;  

function commonCheck(vfld, ifld, reqd) {
  if (!document.getElementById) 
    return true;
  var elem = document.getElementById(ifld);
  if (!elem.firstChild)
    return true;
  if (elem.firstChild.nodeType != node_text)
    return true;

  if (emptyString.test(vfld.value)) {
    if (reqd) {
      msg (ifld, "error", "*");  
      setfocus(vfld);
      return false;
    }
    else {
      msg (ifld, "warn", "");
      return true;  
    }
  }
  return proceed;
}

function validatePresent(vfld, ifld) {
  var stat = commonCheck (vfld, ifld, true);
  if (stat != proceed) return stat;

  msg (ifld, "warn", "");  
  return true;
};

function validateEmail  (vfld, ifld, reqd) {
  var stat = commonCheck (vfld, ifld, reqd);
  if (stat != proceed) return stat;

  var tfld = trim(vfld.value);
  var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/
  if (!email.test(tfld)) {
    msg (ifld, "error", "not a valid e-mail address");
    setfocus(vfld);
    return false;
  }

  var email2 = /^[A-Za-z][\w.-]+@\w[\w.-]+\.[\w.-]*[A-Za-z][A-Za-z]$/
  if (!email2.test(tfld)) 
    msg (ifld, "warn", "Unusual e-mail address - check if correct");
  else
    msg (ifld, "warn", "");
  return true;
};

