// BEGIN: QuestionPoint Script

function submitFormTest(form) {
 if (checkIt(form)) {
   form.submit();
 }
}

function checkIt(form) {
	var email = form.email.value;
	var question = form.question.value;
	var lib;
	var msg = '';
	
	if (form.refid.value != '') {
	      alert("Cannot submit the question");
	      return false;
	   }
	
	if (form.library.selectedIndex >= 0) {
	          lib = form.library.options[form.library.selectedIndex].value;
	  }
	if (lib == null) {
	          lib=form.library.value;
	  }
	
	if(email.length < 1) {
	if(msg.length > 0)
	msg+=', ';
	msg += 'Email Address';
	}
	
	if(question.length < 1) {
	if(msg.length > 0)
	msg+=', ';
	msg += 'Question';
	}
	
	//Make sure Need assistance within has been selected
	//Use this as a prototype for validating other drop-down list fields
	if (form.field4.selectedIndex == 0) {
	if (msg.length >0)
	msg+= ', ';
	msg += 'Status';
	}
	
	if (msg != '')
	{
	alert("One or more required fields are blank\r\n" + msg);
	return false;
	}
	
	if(lib.length < 1) {
	alert("The library supplying this form has an error in the form. Please contact them and alert them to the problem\r\n");
	return false;
	}
	if (emailCheck(email, true)) {
	return true;
	}
	else {
	return false;
	}
}

function emailCheck (emailStr, alertflag) {
	
	var emailPat=/^(.+)@(.+)$/
	
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	
	var word="(" + atom + "|" + quotedUser + ")"
	
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
	        if (alertflag)
		alert("Email address seems incorrect (check @ and .'s)")
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]
	
	// See if "user" is valid
	if (user.match(userPat)==null) {
	    // user is not valid
	     if (alertflag)
	    alert("The username doesn't seem to be valid.")
	    return false
	}
	
	/* if the e-mail address is at an IP address (as opposed to a symbolic
	   host name) make sure the IP address is valid. */
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
	    // this is an IP address
		  for (var i=1;i<=4;i++) {
		    if (IPArray[i]>255) {
	      if (alertflag)
		        alert("Destination IP address is invalid!")
			return false
		    }
	    }
	    return true
	}
	
	// Domain is symbolic name
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
	      if (alertflag)
		alert("The domain name doesn't seem to be valid.")
	    return false
	}
	
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	
			musExp = /.museum/
			isMus = domain.search(musExp)
	
			if ( ( (domArr[domArr.length-1].length < 2) || (domArr[domArr.length-1].length > 6) ) ||
				(domArr[domArr.length-1].length == 5) ||
				( (domArr[domArr.length-1].length == 6) && (isMus == -1) ) ) {
			   // the address must end in a two, three, or four letter word, or .museum.
				  if (alertflag)
			   		alert("Your e-mail address must end in a three or four-letter domain,\ntwo-letter 		country code, or .museum.")
			   return false
			}
	
	// Make sure there's a host name preceding the domain.
	if (len<2) {
	   var errStr="This address is missing a hostname!"
	      if (alertflag)
	   alert(errStr)
	   return false
	}
	
	// If we've gotten this far, everything's valid!
	return true;
}

// END: QuestionPoint Script