
// check for blank fields
function isNonBlank(theField,alertText) {
	if (theField.value == "")
	 {
		alert("The \""+alertText+":\" field requires a value.");
	    theField.focus();
		return (false);
	 }
return true;
}

// validate email address
function isValidEmail(theEmail,alertText) {
	// check for @ and .
	if ((theEmail.value.indexOf ('@',0) == -1) || (theEmail.value.indexOf ('.',0) == -1)) {
		alert("The  \""+alertText+":\" field is not in a valid format.\n\nIt must include the \"@\" and \".\" characters.");
		theEmail.select();
		theEmail.focus();
		return false;
      }
return true;
}

function isValidDate(theDate,alertText) {
	// Checks for the following valid date formats:
	// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
	// Also separates date into month, day, and year variables
	//var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;

	// To require a 4 digit year entry, use this line instead:
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

	var matchArray = theDate.value.match(datePat);
	if (matchArray == null) {
	  alert("The date in the \""+alertText+":\" field is not in a valid format.\n\nPlease use the following format:\nmm/dd/yyyy")
	  theDate.select();
	  theDate.focus();
	  return false;	
	}
	month = matchArray[1]; // parse date into variables
	day = matchArray[3];
	year = matchArray[4];
	if (month < 1 || month > 12) { // check month range
	  alert("Month in the \""+alertText+":\" field must be between 1 and 12 in the date.");
	  theDate.select();
	  theDate.focus();
	  return false;
	}

	if (day < 1 || day > 31) {
	  alert("Day in the \""+alertText+"\": field must be between 1 and 31 in the date.");
	  theDate.select();
	  theDate.focus();
	  return false;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
	  alert("Month "+month+" in the \""+alertText+":\" field doesn't have 31 days in the date.")
	  theDate.select();
	  theDate.focus();
	  return false
	}
	if (month == 2) { // check for february 29th
	  var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
	  if (day>29 || (day==29 && !isleap)) {
	    alert("February " + year + " doesn't have " + day + " days in the date.  Please check the \""+alertText+":\" field.");
	    theDate.select();
	    theDate.focus();
	    return false;
   	  }
	}
return true;
}

function isValidTime(theTime,alertText) {
	// Checks if time is in HH:MM:SS AM/PM format.
	// The seconds and AM/PM are optional.
	//var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;

	// To eliminate need for am and pm and seconds, use this line instead
	var timePat = /^(\d{1,2}):(\d{2})?$/;

	var matchArray = theTime.value.match(timePat);
	if (matchArray == null) {
	  alert("The time in the \""+alertText+":\" field is not in a valid format.");
	  theTime.select();
	  theTime.focus();
	  return false;
	}
	hour = matchArray[1];
	minute = matchArray[2];

	if (hour < 0  || hour > 12) {
	  alert("The hour in the \""+alertText+":\" field must be between 1 and 12.");
	  theTime.select();
	  theTime.focus();
	  return false;
	}
	if (minute<0 || minute > 59) {
	  alert ("The minute in the \""+alertText+":\" field must be between 0 and 59.");
	  theTime.select();
	  theTime.focus();
	  return false;
	}
	return true;
}





// main validation routine
function validate()
	{

	// Your Information Validation
	if (isNonBlank(document.forms[0].setby,'Set by (your name)') == false) {
	  return (false);
	}

	if (isNonBlank(document.forms[0].emailadd,'Email Address') == false) {
	  return (false);
	}

	if (isValidEmail(document.forms[0].emailadd,'Email Address') == false) {
	  return (false);
	}

	if (isNonBlank(document.forms[0].firm,'Firm') == false) {
	  return (false);
	}

	if (isNonBlank(document.forms[0].attorney,'Attorney') == false) {
	  return (false);
	}

	if (isNonBlank(document.forms[0].phone,'Phone Number') == false) {
	  return (false);
	}

	if (isNonBlank(document.forms[0].newdepdate,'Date') == false) {
	  return (false);
	}

	if (isValidDate(document.forms[0].newdepdate,'Date') == false) {
	  return (false);
	}

	if (isNonBlank(document.forms[0].newdeptime1,'first Time') == false) {
	  return (false);
	}

	if (isValidTime(document.forms[0].newdeptime1,'first Time') == false) {
	  return (false);
	}

	if (document.forms[0].newdeptime2.value != '') {
		if (isValidTime(document.forms[0].newdeptime2,'second Time') == false) {
		  return (false);
		}
	}


	if (document.forms[0].newdeptime3.value != '') {
		if (isValidTime(document.forms[0].newdeptime3,'third Time') == false) {
		  return (false);
		}
	}


	if (document.forms[0].rescheddate.value != '') {
		if (isValidDate(document.forms[0].rescheddate,'reschedule Date') == false) {
		  return (false);
		}
	}

	if (document.forms[0].reschedtime.value != '') {
		if (isValidTime(document.forms[0].reschedtime,'reschedule Time') == false) {
		  return (false);
		}
	}

	if (isNonBlank(document.forms[0].casename1,'first Case Name') == false) {
	  return (false);
	}

	if (isNonBlank(document.forms[0].depstreet,'Deposition Street Address') == false) {
	  return (false);
	}

	if (isNonBlank(document.forms[0].depcity,'Deposition City') == false) {
	  return (false);
	}

return (true);
}

