/* JavScript für questionpoint */

function submitForm(form) {
	if (checkIt(form)) {
		form.submit();
	}
} // end submitForm


function checkIt(form) {
	var lib;
	var msg = '';
	var assign = '';

	if ( form.library.selectedIndex >= 0 ) {
		lib = form.library.options[form.library.selectedIndex].value;
	}
	if ( lib == null ) lib = form.library.value;
	if ( lib.length < 1 ) {
		alert("The library supplying this form has an error in the form (lib). Please contact them and alert them to the problem.");
		return false;
	}

	if ( form.assign.value.length < 1 ) {
		alert("The library supplying this form has an error in the form (assign). Please contact them and alert them to the problem.");
		return false;
	}
	assign = form.assign.value;
	if ( isNaN(assign) || assign < 1 || assign > 10 ) {
		alert("The library supplying this form has an error in the form (assign). Please contact them and alert them to the problem.");
		return false;
	}
	
	if ( form.question.value.length < 1 && msg == '' ) msg = 'Ihre Frage';
	if ( form.field1.selectedIndex == 0 && msg == '' ) msg = 'Thema Ihrer Anfrage';
	if ( form.email.value.length < 1 && msg == '' )    msg = 'Ihre E-Mail Adresse';
	if ( form.name.value.length < 1 && msg == '' )     msg = 'Ihr Name';
	if ( form.field3.value.length < 1 && msg == '' )   msg = 'Ihre Straße, Hausnummer';
	if ( form.field4.value.length < 1 && msg == '' )   msg = 'Ihre PLZ, Ort';

	for ( i=0, n=form.field7.length; i<n; i++ ) {
   		if ( form.field7[i].checked ) {
			var checkvalue = form.field7[i].value;
			break;
		}
	}
	if ( !(checkvalue) && msg == '' )  msg = 'Sind Sie damit einverstanden, dass Ihre Frage wenn nötig an Partnerbibliotheken weitergeleitet und anonym archiviert wird?';
	
	if ( msg != '' ) {
		alert("Es sind nicht alle Felder ausgefüllt: " + msg);
		return false;
	}

	if ( emailCheck(form.email.value, true) ) return true;

	return false;
} // end checkIt


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("E-Mail-Adresse fehlerhaft!")
		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("E-Mail-Name fehlerhaft!");
    		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("IP-Adresse fehlerhaft!")
				return false
	    		}
    		}
    		return true
	}

	// Domain is symbolic name
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		if (alertflag) alert("Domain-Name fehlerhaft!")
    		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("E-Mail-Adresse fehlerhaft (Domain-Name)!")
	   	return false
	}

	// Make sure there's a host name preceding the domain.
	if (len<2) {
		var errStr = "E-Mail-Adresse fehlerhaft (Host-Name)!"
		if (alertflag) alert(errStr)
   		return false
	}

	// If we've gotten this far, everything's valid!
	return true;
} // end emailCheck

