if (document.getElementById) {
  document.write('<style type="text/css">.answer {display:none; margin-left:10px;}</style>');
}

function showhide(id){
  if (document.getElementById) {
    obj = document.getElementById(id);
    obj.style.display = (obj.style.display == "block") ? "none" : "block";
  }
}

var submitcount=0;
function checkSubmit() {     
      
 if (submitcount == 0)  {
   submitcount++;
   return true;
 }else{
   alert("This form has already been submitted.\nWe are preparing the next page for you." );
   return false;
 } 

}

// Check required fields
function formCheck(){

  first_submit = checkSubmit();
  if (first_submit == false) { return false; }

  formobj = document.forms[0];

	// dialog message
	var alertMsg = "Please complete the following fields(s):\n";
	
	var l_Msg = alertMsg.length;
	
	for (var i = 0; i < fieldRequired.length; i++){
		var obj = formobj.elements[fieldRequired[i]];
		if (obj){
			switch(obj.type){
			case "select-one":
				if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "select-multiple":
				if (obj.selectedIndex == -1){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "text":
			case "textarea":
				if (obj.value == "" || obj.value == null){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			default:
			}
			if (obj.type == undefined){
				var blnchecked = false;
				for (var j = 0; j < obj.length; j++){
					if (obj[j].checked){
						blnchecked = true;
					}
				}
				if (!blnchecked){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
			}
		}
	}

	if (alertMsg.length == l_Msg){
		if (EmailCheck(formobj.email.value)) {
		   return true;
		}
		else {
		   submitcount=0;
		   return false;		  	
		}	
	}else{
	  submitcount=0;
		alert(alertMsg);
		return false;
	}
}

function EmailCheck(emailaddr) {
	
	var emailstring = emailaddr;
	var ampIndex = emailstring.indexOf("@");
	var afterAmp = emailstring.substring((ampIndex + 1), emailstring.length);
	var dotIndex = afterAmp.indexOf(".");

	dotIndex = dotIndex + ampIndex + 1;
	afterAmp = emailstring.substring((ampIndex + 1), dotIndex);

	var afterDot = emailstring.substring((dotIndex + 1), emailstring.length);
	var beforeAmp = emailstring.substring(0,(ampIndex));
	var email_regex = /^\w(?:\w|-|\.(?!\.|@))*@\w(?:\w|-|\.(?!\.))*\.\w{2,3}/ 

	if ((emailstring.indexOf("@") != "-1") &&
		(emailstring.length > 5) &&
		(afterAmp.length > 0) &&
		(beforeAmp.length > 1) &&
		(afterDot.length > 1) &&
		(email_regex.test(emailstring)) ) {
		  return true;
	} else {
	      alert('The email address you have entered is invalid. \n\n ' + emailaddr + ' \n\nPlease correct before resubmitting the form.');
		 		return false;
	}
}

