// ///////////////////////////////////////////////////////////////////////////////////////////
//      SCRIPT NAME:	formValidation.js
//    SCRIPT AUTHOR:	Mark Vega
//      SCRIPT DATE:	June 2007
//  SCRIPT LOCATION:	pitcairn://target1s3/Web2/htdocs/www/libraries/grunigen/scripts/
//  SCRIPT FUNCTION:	
//     SCRIPT NOTES:		
// OTHER FILES USED:
//    LAST MODIFIED:	Changed all paths for new home on pitcairn.  06/13/2007 MFV
//    					Revised Core Search validation function.  03/19/2008 MFV
//                  	Revised Ebooks Search validation function.  03/19/2008 MFV
//                  	Added Toolkit Search validation funciton.  03/31/2008 MFV
//
// ///////////////////////////////////////////////////////////////////////////////////////////
//
// DEBUG WRITE STATEMENT
// alert("loading formValidation.js");

//
// FUNCTION TO VALIDATE FORM INPUT BEFORE SUBMISSION
// validates forms where all fields are required when
//        passed form ID and messages array
//
function anyFormValidate(frm,messages) {
// initialize local variables
var checkBoxGroup = '';
var checkRadioGroup = '';
var checkedBoxGroup = '';
var checkedRadioGroup = '';
var elemType = '';
var elemName = '';
var frmId = document.getElementById(frm);
var formLength = frmId.elements.length - 1; // subtract 1 for submit button
var savedCheckbox = '';
var savedRadio = '';
var validate = true;

// loop through form elements array
for (i = 0; i < formLength; i++) {
// save element type, name as elemtype, elemname
elemType = frmId.elements[i].type;
elemName = frmId.elements[i].name;

// if element type is radio button and name not equal to saved name
if ((elemType == 'radio') && (savedRadio != elemName)) {
// save elemname
savedRadio = elemName;
// get element object
checkGroup = document.getElementsByName(elemName);
// send element object to radiovalidate function and save return value
checkedGroup = radioValidate(checkGroup);
}

// validate textboxes
if ((elemType == 'text') && (textboxValidate(frmId.elements[i].value) == false)) {
frmId.elements[i].focus();
message = messages[i];
validate = false;
alert(message);
break;
// validate email address format
} else if ((elemName == 'email') && (emailValidate(frmId.elements[i].value) == false)) {
frmId.elements[i].focus();
message = messages[i];
validate = false;
alert(message);
break;
// validate text areas
} else if ((elemType == 'textarea') && (textareaValidate(frmId.elements[i].value) == false)) {
frmId.elements[i].focus();
message = messages[i];
validate = false;
alert(message);
break;
// validate radio buttons
} else if ((elemType == 'radio') && (checkedGroup == '')) {
frmId.elements[i].focus();
message = messages[i];
validate = false;
alert(message);
break;
}
// reset temporary variables
elemType = '';
elemName = '';
checkBoxGroup = '';
checkRadioGroup = '';
}
return validate;
}
//
// FUNCTION TO VALIDATE TEXTBOX INPUT
//
function textboxValidate(textBox) {
var validTextbox = true;
if (textBox == '') {
validTextbox = false;
}
return validTextbox;
}
//
// FUNCTION TO VALIDATE TEXTAREA INPUT
//
function textareaValidate(textArea) {
var validTextarea = true;
if (textArea == '') {
validTextarea = false;
}
return validTextarea;
}
//
// FUNCTION TO VALIDATE EMAIL ADDRESS FORMAT
//
function emailValidate(emailAddress) {
var validEmail = true;
var pattern = /^[-._&0-9a-zA-Z]+[@][-._&0-9a-zA-Z]+[.][._0-9a-zA-Z]+[a-zA-Z]$/;
if (pattern.exec(emailAddress) == null) {
validEmail = false;
}
return validEmail;
}
//
// FUNCTION TO VALIDATE RADIO BUTTON SELECTION
//
function radioValidate(radioButtons) {
radioOption = false;
for (j = 0; j < radioButtons.length; j++) {
if (radioButtons[j].checked) {
radioOption = true;
}
}
if (radioOption == false) {
return "";
} else {
return radioOption;
}
}
//
// FUNCTION TO VALIDATE CHECKBOX SELECTION
//
function checkboxValidate(checkBoxes) {
checkboxOption = false;
for (j = 0; j < checkBoxes.length; j++) {
if (checkBoxes[j].checked) {
checkboxOption = true;
}
}
if (checkboxOption == false) {
return "";
} else {
return checkboxOption;
}
}

//
// FUNCTION TO VALIDATE CORE SEARCH FORM
//
// this function validates input from Core Resources search form before submitting,  
// returns message and cancels submission if input invalid.
//
function coreSearchValidate() {
// assign form element values to variables
var srchform = document.getElementById('f_coresearch');
var srchfor = document.getElementById('f_coresearchfor').value;
var srchwhere = document.getElementById('f_coresearchwhere').value;
var srchindex = document.getElementById('f_coresearchwhere').selectedIndex;
var srch = "scripts/search-coreresources.php?searchfor=" + srchfor + "&searchwhere=" + srchwhere;

// check each field for valid input, send message in alert window if invalid and cancel submission
if (((srchfor == '') || (srchfor == 'Enter search term here')) && (srchindex == 0)) {
alert("Please enter a search term, select a site to search from the drop-down list, and click the Search button.");
} else if ((srchfor == '') || (srchfor == 'Enter search term here')) {
alert("Please enter a search term and click the Search button.");
} else if ((srchindex == 0) || (srchindex == 1) || (srchindex == 22) || (srchindex == 24) || (srchindex == 28)) {
alert("Please select a site from the drop-down list and click the Search button.");
// } else if (srchindex == 15) {
// alert("Direct searching of this site has been temporarily disabled.  Please select another site from the drop-down list and click the Search button.");
// if input valid
} else {
// submit form
if (srchwhere == 'ebooks') {
document.getElementById('f_coresearchfor').value = "Enter search term here";
document.getElementById('f_coresearchwhere').selectedIndex = 0;
window.location.href = srch;
} else {
document.getElementById('f_coresearchfor').value = "Enter search term here";
document.getElementById('f_coresearchwhere').selectedIndex = 0;
newWin(srch,'search');
}
}
return false;
}




//
// FUNCTION TO VALIDATE TOOLKIT SEARCH FORM
//
// this function validates input from Clinician's Toolkit search forms before submitting,  
// returns message and cancels submission if input invalid.
//
function toolkitSearchValidate(frm) {
// initialize variables
var message = "Please enter search term(s) and resubmit form.";
var scopeObj = '';
var srchfor = '';
var srchscope = '';
var srchstring = '';
var srchtype = '';
var srchwhere = '';
var typeObj = '';
var validate = true;

// get form textbox values
srchwhere = document.getElementById(frm).searchwhere.value;
srchfor = document.getElementById(frm).searchfor.value;

// if no search term
if (!srchfor) {
// set validate flag to false
validate = false;
// if search term, process form values
} else {
// get form radio button values
if (srchwhere == 'clinicalqueries') {
typeObj = document.getElementById(frm).searchtype;
scopeObj = document.getElementById(frm).searchscope;
for (i = 0; i < typeObj.length; i++) {
if (typeObj[i].checked) {
srchtype = typeObj[i].value;
}
}
for (i = 0; i < scopeObj.length; i++) {
if (scopeObj[i].checked) {
srchscope = scopeObj[i].value;
}
}
}
// construct search string
srchstring = "scripts/search-clinicianstoolkit.php?searchwhere=" + srchwhere + "&searchfor=" + srchfor;
if (srchwhere == 'clinicalqueries') {
srchstring += "&searchtype=" + srchtype + "&searchscope=" + srchscope;
}
}

// check validate flag
if (validate == false) {
alert(message);
document.getElementById(frm).searchfor.focus();
return false;
} else {
// open new window and submit search
newWin(srchstring,'search');
// reset all form values
document.getElementById('f_searchuptodate').searchfor.value = '';
document.getElementById('f_searchpubmed').searchfor.value = '';
document.getElementById('f_searchclinicalqueries').searchfor.value = '';
document.getElementById('f_searchgooglescholar').searchfor.value = '';
document.getElementById('f_searchclinicalqueries').searchtype[2].checked = true;
document.getElementById('f_searchclinicalqueries').searchscope[0].checked = true;
return false;
}
}







/* MISCELLANEOUS FORM VALIDATION - NEEDS REVIEW */

//
// FUNCTION TO VALIDATE EBOOKS SEARCH FORM
//
// this function validates input from ebooks search box before submitting,  
// returns message and cancels submission if input invalid.
//
function ebookSearchValidate() {
// assign form element values to variables
var ebooksrchform = document.getElementById('f_searchonlinebooks');
var ebooksrchfor = document.getElementById('f_searchfor').value;

// check each field for valid input
if (ebooksrchfor == '') {
// send message in alert window if invalid
alert("Please enter an author, title, keyword, or ISBN and click the Go button.");
// cancel submission
return false;
// if input valid
} else {
// submit form
// ebooksrchform.submit();
return true;
}
}
//
// FUNCTION TO VALIDATE IM SEARCH FORM
//
// this function validates input from im dictionary search box before submitting,  
// returns message and cancels submission if input invalid.
//
function imSearchValidate() {
// assign form element values to variables
var imsrchform = document.getElementById('f_imsearch');
var imsrchfor = document.getElementById('f_imsearchfor').value;

// check each field for valid input
if (imsrchfor == '') {
// send message in alert window if invalid
alert("Please enter an abbreviation and click the Go button.");
// cancel submission
return false;
// if input valid
} else {
// submit form
imsrchform.submit();
}
}
//
// FUNCTION TO VALIDATE MED SEARCH FORM
//
// this function validates input from core abbreviations dictionary search box  
// before submitting, returns message and cancels submission if input invalid.
//
function medSearchValidate() {
// assign form element values to variables
var medsrchform = document.getElementById('f_medsearch');
var medsrchfor = document.getElementById('f_medsearchfor').value;

// check each field for valid input
if (medsrchfor == '') {
// send message in alert window if invalid
alert("Please enter an abbreviation and click the Go button.");
// cancel submission
return false;
// if input valid
} else {
// submit form
medsrchform.submit();
}
}


//
// MISCELLANEOUS FORM VALIDATION (NEEDS REVIEW)
//


//
// FUNCTION TO VALIDATE ROOM REQUEST FORM
//
// this function validates input from room request form before submitting,  
// returns message and cancels submission if input invalid.
//
function reserveRoomValidate() {
// assign form element values to variables
var room = document.reserveroom.elements[0].selectedIndex;
var name = document.reserveroom.elements[1].value;
var ucinetid = document.reserveroom.elements[2].value;
var dept = document.reserveroom.elements[3].value;
var email = document.reserveroom.elements[4].value;
var phone = document.reserveroom.elements[5].value;
var eventname = document.reserveroom.elements[6].value;
var date = document.reserveroom.elements[7].value;
var start = document.reserveroom.elements[8].value;
var end = document.reserveroom.elements[9].value;
var type = document.reserveroom.elements[10].selectedIndex;
var equip1 = document.reserveroom.elements[11].selectedIndex;
var equip2 = document.reserveroom.elements[12].selectedIndex;
var equip3 = document.reserveroom.elements[13].selectedIndex;
var additional = document.reserveroom.elements[14].value;
var message = '';
var validate = true;

// check each field for valid input, set validate to false if invalid
if (room == 0) {
message = "Please select a room from the drop-down list and resubmit request.";
validate = false;
} else if (name == '') {
message = "Please enter your name and resubmit request.";
validate = false;
} else if (ucinetid == '') {
message = "Please enter your UCInetID and resubmit request.";
validate = false;
} else if (dept == '') {
message = "Please enter your department name and resubmit request.";
validate = false;
} else if (email == '') {
message = "Please enter your email address and resubmit request.";
validate = false;
} else if (phone == '') {
message = "Please enter a daytime phone number and resubmit request.";
validate = false;
} else if (eventname == '') {
message = "Please enter the event name and resubmit request.";
validate = false;
} else if (date == '') {
message = "Please enter the event date and resubmit request.";
validate = false;
} else if (start == '') {
message = "Please enter start time of the event and resubmit request.";
validate = false;
} else if (end == '') {
message = "Please enter end time of the event and resubmit request.";
validate = false;
} else if (type == 0) {
message = "Please select an event type from the drop-down list and resubmit request.";
validate = false;
}
// check validation flag
if (validate == false) {
// send message in alert window if flag is false
alert(message);
// cancel form submission
return false;
// if flag is true
} else {
// submit form
document.reserveroom.submit();
}
}
//
// FUNCTION TO VALIDATE MULTIMEDIA REQUEST FORM FUNCTION
//
// this function validates input from reserve multimedia workstation form before submitting, 
// returns message and cancels submission if input invalid.
//
function reserveMediaValidate() {
// assign form element values to variables
var name = document.reservemultimedia.elements[0].value;
var ucinetid = document.reservemultimedia.elements[1].value;
var email = document.reservemultimedia.elements[2].value;
var phone = document.reservemultimedia.elements[3].value;
var date = document.reservemultimedia.elements[4].value;
var start = document.reservemultimedia.elements[5].value;
var end = document.reservemultimedia.elements[6].value;
var message = '';
var validate = true;

// check each field for valid input, set validate to false if invalid
if (name == '') {
message = 'Please enter your name and resubmit request.';
validate = false;
} else if (ucinetid == '') {
message = 'Please enter your UCInetID and resubmit request.';
validate = false;
} else if (email == '') {
message = 'Please enter your email address and resubmit request.';
validate = false;
} else if (phone == '') {
message = 'Please enter a daytime phone number and resubmit request.';
validate = false;
} else if (date == '') {
message = 'Please enter the event date and resubmit request.';
validate = false;
} else if (start == '') {
message = 'Please enter start time of the event and resubmit request.';
validate = false;
} else if (end == '') {
message = 'Please enter end time of the event and resubmit request.';
validate = false;
}
// check validation flag
if (validate == false) {
// send message in alert window if flag is false
alert(message);
// cancel form submission
return false;
// if flag is true
} else {
// submit form
document.reservemultimedia.submit();
}
}
//
// FUNCTION TO VALIDATE TOOLKIT FEEDBACK FORM
//
// this function validates input from toolkit feedback form before submitting,  
// returns message and cancels submission if input invalid.
//
// initialize variables
var feedback = '';


function feedbackValidate() {
// assign form element values to variables
var feedback = document.feedback.elements[2].value;
// check each field for valid input
if (feedback == '') {
// send message in alert window if invalid
alert("Please enter your feedback in the text box and click the Submit Feedback button.");
// cancel form submission
return false;
// if input valid
} else {
// submit form
document.feedback.submit();
}
}
//
// FUNCTION TO VALIDATE NEWS ITEM FORM
//
// this function validates input from grunigen news item form before submitting,  
// returns message and cancels submission if input invalid.
//
function newsitemValidate() {
// assign form element values to variables
var headline = document.newsitemform.elements[0].value;
var headlinelink = document.newsitemform.elements[1].value;
var newsitem = document.newsitemform.elements[3].value;
var message = '';
var validate = true;

// check each field for valid input, set validate to false if invalid
if (headline == '') {
message = "Please enter a headline for this news item.";
validate = false;
} else if (headlinelink == '' && newsitem == '') {
message = "Please enter either news item text or a URL for the headline to link to the news item.";
validate = false;
}
// check validation flag
if (validate == false) {
// send message in alert window if flag is false
alert(message);
// cancel submission
return false;
// if flag is true
} else {
// submit form
document.newsitemform.submit();
}
}
//
// FUNCTION TO VALIDATE IM EMAIL FORM
//
// this function validates input from im email form before submitting,  
// returns message and cancels submission if input invalid.
//
function imEmailValidate() {
// initialaize variables
var name = document.imemail.elements[0].value;
var email = document.imemail.elements[1].value;
var affiliation = document.imemail.elements[2].selectedIndex;
var question = document.imemail.elements[3].value;
var message = '';
var teststring = '';
var illegaltest = '';
var validate = true;

// check each field for valid input and set message, set validate to false if invalid
if (name == '') {
message = "Please enter your name and resend email.";
validate = false;
} else if (email == '') {
message = "Please enter your email address and resend email.";
validate = false;
} else if ((affiliation == 0) || (affiliation == 1)) {
message = "Please select your UCI affiliation and resend email.";
validate = false;
} else if (question == '') {
message = "Please enter your question and resend email.";
validate = false;
}
// check input for illegal characters
for (i = 0; i < document.imemail.elements.length; i++) {
teststring = document.imemail.elements[i].value;
illegaltest = /[\~$\%\^\&\*\<\>{\}\[\]\|\\+]+/;
if (teststring.match(illegaltest)) {
message = "You have entered a prohibited text character  ( ~, $, %, ^, &, *, <, >, {, }, [, ], |, \\, or + )  in one or more of this form\'s text fields. Please remove the character(s) and resubmit request.";
validate = false;
}
}
// check validation flag
if (validate == false) {
// send message in alert window if flag is false
alert(message);
// cancel submission
return false;
// if flag is true
} else {
// submit form
return true;
}
}