if (!JEJ)
	var JEJ = {};

if (!JEJ.doubledelegate) {
	JEJ.doubledelegate = function(f1, f2) {
		return function() {
			if (f1)
				f1();
			if (f2)
				f2();
		}
	}
}

if (!QP)
	var QP = {};

QP.submitForm = function() {
	if (QP.checkIt()) {
		var form = document.getElementById('entryform1');
		form.submit();
	}
}

//variables that store contents of e-mail address and question fields for validation
//Create a variable like these for each text or textarea field that you want to require patron to complete
//Then add a check for the contents of the variable where indicated (see "Make sure the E-mail address
// contains some data" for an example)

QP.checkIt = function() {
	var form = document.getElementById('entryform1');
	var email = form.email.value;
	var email2 = form.emailconf.value; //variable for value of e-mail confirmation field
	var question = form.question.value;
	var lib;
	//variable that contains contents of message to display to patron if required field(s) are blank
	var msg = '';

	// Make sure the E-mail address field contains some data
	if(email.length < 1) {
		if(msg.length > 0)
			msg+=', ';
		msg += 'E-mail address';
	}

	// Make sure the E-mail address field contains some data
	if(email2.length < 1) {
		if(msg.length > 0)
			msg+=', ';
		msg += 'Confirm E-mail address';
	}

	//Make sure the Question field contains some data
	if(question.length < 1) {
		if(msg.length > 0)
			msg+=', ';
		msg += 'Question';
	}

	//Display alert box for patron; he/she left at least one required field blank
	if (msg != '') {
		alert("One or more required fields are blank:\r\n" + msg);
		return false;
	}

	if (QP.emailCheck(email, true)) { 
		//the next four lines only apply to forms with a confirmation e-mail field. Remove or comment
		//them out if you do not need them or the script will not run properly.
		if 	(email != email2) {
			alert("Check the e-mail address and confirmation e-mail address. They do not match.")
			return false;	  
		}
		//end e-mail confirmation check
		return true;
	} else {
		return false;
	}
}

QP.emailCheck = function(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("The address must end in a three or four-letter domain, two-letter country, 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;
}

QP.start = function() {
	var ref = document.getElementById('field5');
	ref.value = document.referrer;
	var ent = document.getElementById('entryform1');
	ent.onsubmit = QP.checkIt;
	var sub = document.getElementById('Submit');
	sub.onclick = QP.submitForm;
}
window.onload = JEJ.doubledelegate(window.onload, QP.start);

//document.form.field4.value = document.referrer;

