// JavaScript Document

/* These scripts are used to check and verify forms fields before sending them to an email generator
   (php script file on Webservices). 
*/

function zp(n){
	return n<10?("0"+n):n;
}

/* automatically inserts the date into a field. Called in HTML code with 
   onFocus="insertDate(this,'MM/DD/YYYY');this.className='inputHighlighted';" 
   in the <input> tag for the desired field.
*/
function insertDate(t,format){
		var now=new Date();
		var DD=zp(now.getDate());
		var MM=zp(now.getMonth()+1);
		var YYYY=now.getFullYear();
		var YY=zp(now.getFullYear()%100);
		
	format=format.replace(/DD/,DD);
	format=format.replace(/MM/,MM);
	format=format.replace(/YYYY/,YYYY);
	format=format.replace(/YY/,YY);
	t.value=format;
}


/* checks if the passed string is empty
*/
function isEmpty(str) {
  return (str == null) || (str.length == 0) || (str == '');
}


/* checks the format of the entered email address. 
   Called in verifyRequired().
*/
function checkEmail(formName) {
	if (!isEmpty(document.submitter.patEmail.value))
		if  (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.submitter.patEmail.value)){
			if (formName == 'LitSrch') {
				if ( (document.submitter.patEmail.value.match("@baylorhealth.edu")) ||
					 (document.submitter.patEmail.value.match("@bcd.tamhsc.edu")) )
					return (true);
			}
			else return (true);
		}
		
	alert("The Email Address entered is invalid. Please enter in the form: \n - johndoe@baylorhealth.edu or \n - jsmith@bcd.tamhsc.edu, etc.")
	
	return (false)
}


/* checks the signature name entered against the name entered in contact info fields.
   Called with onBlur="checkSignature(this);" in <input> tag of desired field. 
*/
function checkSignature(sig) {
		var matches=false;
		patName = document.submitter.patFName.value + ' ' + document.submitter.patLName.value;
		
	if (sig.value == patName) {
		sig.className = "requiredField";
		matches = true;
	}
	else {
		sig.className = "requiredHighltd";
		alert ("The signature must match the values in the Name fields above.")
	}
	return (matches);
}


/* checks the signature name entered against the name entered in contact info fields.
   Used when form splits name into separate first- and last-name fields.
   Called with onBlur="checkSignature2(this);" in <input> tag of desired field. 
*/
function checkSignature2(sig) {
		var matches=false;
		patName = document.submitter.patFName.value + ' ' + document.submitter.patLName.value;
		
	if (sig.value == patName) {
		sig.className = "requiredField";
		matches = true;
	}
	else {
		sig.className = "requiredHighltd";
		alert ("The signature must match the values in the Name fields above.")
	}
	return (matches);
}


/* this function is called by the ILL Request form to reset only the values of the Requested Document section.
   It leaves the Contact info alone. Called with onClick="ResetDocInfo(); in <input> tag of reset button.
*/
function ResetDocInfo() {
	document.getElementById("Title").value='';
	document.getElementById("Article_Chapter").value='';
	document.getElementById("Pages").value='';
	document.getElementById("Author").value='';
	document.getElementById("UI_PMID").value='';
	document.getElementById("Volume").value='';
	document.getElementById("Issue").value='';
	document.getElementById("Date").value='';
	document.getElementById("resNOTNeededAfter").value='';
}



/* this is the main verification function. 
   Called with onClick="return verifyRequired();this.disabled=true;this.value='Submitting...';" in <submit> tag of form.
   Required fields should have class="requiredField". If they are not filled out, then the class is reset to 
   requiredHighltd. Their associated headers have class="requiredHighltd" and is changed to requiredHighltd
   (or requiredBoxHighlt or highltdRadioHeader depending on the input type). Some help text can also be displayed for 
   certain fields.
   formName identifies the caller of this function so that specific checks can be identified
*/
function verifyRequired(formName) {
		var themessage = "Please provide information for all required fields.";
		var pass=true;
		var EmailField = document.getElementById("patEmail");
		var SigField = document.getElementById("Signature");
		var defaultSig = "(retype name here)";

//alert("VerifyRequired:" + formName);

	for (i=0; i<document.submitter.length; i++) {
			var Field=document.submitter.elements[i];

		//* check required text fields and set any unfilled boxes to pink *//
		if (Field.type == 'text') {
			if (isEmpty(Field.value) && (Field.className=='requiredField' || Field.className=='requiredHighltd')) {
				pass=false;
				Field.className="requiredHighltd";
			}
			//* if a value has been entered in a highlighted required field reset the box color *//
			else if (Field.className=="requiredHighltd") {
				Field.className="requiredField";
			}
		}

		//* check required radio fields *//
		if(Field.type == "radio") {
				var radiogroup = document.submitter.elements[Field.name]; // get the whole set of radio buttons.
				var itemchecked = false;

			//* NOTE: all radio button fields are required at this point (May, 2007) *//
			for(j=0; j<radiogroup.length; ++j) {
				if(radiogroup[j].checked) {
					itemchecked = true;
					if (radiogroup[j].name == "Priority")
						document.getElementById("Txt" + Field.name).className="priorityRadioHeader";
					else
						document.getElementById("Txt" + Field.name).className="requiredRadioHeader";
					break;
				}
			}

			if(!itemchecked) { 
				document.getElementById("Txt" + Field.name).className = "highltdRadioHeader";
				pass=false;
			}
		}
		//* check if copyright warning was checked *//
		if ((Field.type == 'checkbox') && (Field.className=='requiredField')) {
			if (Field.checked)
				document.getElementById("Txt" + Field.name).className = "requiredHeader";
			else {
				document.getElementById("Txt" + Field.name).className = "requiredBoxHighlt";
				pass = false;
			}
		}
	}
	
	//* if Volume, Issue or Date are blank display hidden message to use NA if no info avail.*//
	if (formName == "ILL") {
		if (isEmpty(document.getElementById("Volume").value) ||
			isEmpty(document.getElementById("Issue").value) ||
			isEmpty(document.getElementById("Date").value))
			 document.getElementById("Vol_Iss_Dt_Help").className="displayHelpText";
		else document.getElementById("Vol_Iss_Dt_Help").className="hiddenHelpText";
	}
			
	//* highlight signature if not signed
	if (SigField.value == defaultSig) {
		SigField.className="requiredHighltd";
		pass=false;
	}
	else SigField.className="requiredField";
		

	//* if all fields present then check email format *//
	if (pass) {
		if (!checkEmail(formName)) {
			pass=false;
			document.submitter.patEmail.className="requiredHighltd";
		}
		else 
			//return (pass);
			document.submitter.submit();
	}
	else {
		alert(themessage);
		return false;
   }
   return (pass);
}
