var email = "(type again)"
//var phone = "(if you don't mind us calling to get more info about your question)"
var tools = "(list any databases, websites, books, etc. that you have used to answer this question)"
var dept = "(If applicable to the question, what is the course number or relevant academic department?)"
var question = "(To be most helpful, please give us as many details about what you are seeking.)"

function init(){
	document.getElementById('flag_email').value = 0
	document.getElementById('emailconf').value = email
	//document.getElementById('field2').value = phone
	document.getElementById('flag_field6').value = 0
	document.getElementById('field6').value = tools
	document.getElementById('flag_field7').value = 0
	document.getElementById('field7').value = dept
	document.getElementById('flag_question').value = 0	
	document.getElementById('question').value = question	
}

function cls(el){
//el.value = ""
	//only clear fields if there is user input(ie, flags are not set)
	switch (el.name){
		case "emailconf":
			if(document.getElementById('flag_email').value == 0)
				el.value = ""
		break;
		case "question":
			if (document.getElementById('flag_question').value == 0	)
				el.value = ""
		break;
		case "field6":
			if (document.getElementById('flag_field6').value == 0)
				el.value = ""
		break;
		case "field7":
			if (document.getElementById('flag_field7').value == 0)
				el.value = ""
		break;
		default:
			if(el.value.length == 0){
				el.value = ""
			}
		break;
	}
}

function reset_form(){
	document.entryform1.reset()
	init()
}

function check(el){
	if(el.value.length == 0){
		//alert('nothing entered: '+el.name)
		val = -1
	}
	else{
		//alert('something was entered: '+el.name)
		val = el.value
	}

	switch (el.name){
		case "emailconf":
			document.getElementById('flag_email').value = val == -1 ? 0 : 1		
			document.getElementById('emailconf').value = val == -1 ? email : val
		break;

		case "question":
			document.getElementById('flag_question').value = val == -1 ? 0 : 1		
			document.getElementById('question').value = val == -1 ? question : val
		break;

		case "field6":
			document.getElementById('flag_field6').value = val == -1 ? 0 : 1		
			document.getElementById('field6').value = val == -1 ? tools : val		
		break;
		
		case "field7":
			document.getElementById('flag_field7').value = val == -1 ? 0 : 1				
			document.getElementById('field7').value = val == -1 ? dept : val		
		break;
	}
	
}

      function submitForm(form) {
       if (checkIt(form)) {
         form.submit();
       }
      }
      function checkIt(form) {
	  //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)
      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;
	  var subject = form.subject.value;
	  var success = true;
	  
	  if (subject != "")
	  {
		 success = false;
		 
	  }
	  
	  
	  
	  //variable that contains contents of message to display to patron if required field(s) are blank
      var msg = '';
	 
      if (form.library.selectedIndex >= 0) {
                lib = form.library.options[form.library.selectedIndex].value;
        }
      if (lib == null) {
                lib=form.library.value;
        }
     
	  // 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 Confirm 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';
      }

	  //field 6: resources not required
	  //clear out default value
	  if(form.flag_field6.value == 0){
		form.field6.value = ""
	  }
	  
	  //field 7: dept not required
	  //clear out default value
	  if(form.flag_field7.value == 0){
		form.field7.value = ""
	  }

	  //question is most definitely required
	  //add error message
	  if(form.flag_question.value == 0){
      if(msg.length > 0)
		msg+=', ';		  
     	msg += "Question"
	  }
	  
	  //alert("field 6: \n flag: "+form.flag_field6.value+"\nvalue: "+form.field6.value);
	  //alert("field 7: \n flag: "+form.flag_field7.value+"\nvalue: "+form.field7.value);
	  //alert("question: \n flag: "+form.flag_question.value+"\nvalue: "+form.question.value);	  
	  //return false;
	  
	  //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 (!success)
	  {
		        alert("Sorry no spam! \r\n");

		  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)) { 
	  //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;
      }
      }

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("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;
}


