/*this function ensures that the user has typed in all of the 
mandatory fields.  if any are not filled out, the function returns false
and the user will not be allowed to submit until each mandatory textbox
is entered*/
function validateReport() {
	var msg = "The form you are trying to submit has the following errors:\n\n";
	var error = false;
	
	if (!(validateState(document.first_report.state)))
	{return false;}

	if (!(validateState(document.first_report.jobsite_state)))
	{return false;}
	
	if (!(validateState(document.first_report.business_state)))
	{return false;}

	if (!(checkZip(document.first_report.zip)))
	{return false;}

	if (!(checkZip(document.first_report.business_zip)))
	{return false;}
	
	if (!(mydateCheck(document.first_report.dob, document.first_report.today.value)))
	{return false;}

	if (!(mydateCheck(document.first_report.date_emp_began, document.first_report.today.value)))
	{return false;}

	if (!(mydateCheck(document.first_report.date_injury, document.first_report.today.value)))
	{return false;}

	if (!(mydateCheck(document.first_report.date_disability_began, document.first_report.today.value)))
	{return false;}

	if (!(mydateCheck(document.first_report.date_emp_notified, document.first_report.today.value)))
	{return false;}

	if (!(mydateCheck(document.first_report.date_returned, document.first_report.today.value)))
	{return false;}

	if (!(mydateCheck(document.first_report.date_died, document.first_report.today.value)))
	{return false;}
	
	if (!(myphoneCheck(document.first_report.phone)))
	{return false;}

	if (!(myphoneCheck(document.first_report.business_phone)))
	{return false;}

	if (!(checkSSN(document.first_report.ssn)))
	{return false;}
	
	if (!(checkMoney(document.first_report.wages_hour)))
	{return false;}

	if (!(checkMoney(document.first_report.average)))
	{return false;}

	if (!(checkDesc(document.first_report.description)))
	{return false;}
	
	if (!(checkEmp(document.first_report.full_time)))
	{return false;}
	
	if (!(checkEmp(document.first_report.part_time)))
	{return false;}
	
	if (!(checkFed(document.first_report.fed_id)))
	{return false;}
	
	if (!(checkDays(document.first_report.days_week)))
	{return false;}
	
	if (!(checkHours(document.first_report.hrs_day)))
	{return false;}
	
	if (!(checkAge(document.first_report.age)))
	{return false;}
	
	if (!(checkTime(document.first_report.time_injury)))
	{return false;}

	if (document.first_report.fname.value=="") {
		msg = msg + "\tA first name must be entered.\n";
		error = true;
	}
	
	if (document.first_report.lname.value=="") {
		msg = msg + "\tA last name must be entered.\n";
		error = true;
	}

	if (document.first_report.address.value=="") {
		msg = msg + "\tAn address must be entered.\n";
		error = true;
	}

	if (document.first_report.city.value=="") {
		msg = msg + "\tA city must be entered.\n";
		error = true;
	}

	if (document.first_report.state.value=="") {
		msg = msg + "\tA state must be entered.\n";
		error = true;
	}

	if (document.first_report.zip.value=="") {
		msg = msg + "\tA zip code must be entered.\n";
		error = true;
	}

	if (document.first_report.date_emp_notified.value=="") {
		msg = msg + "\tThe date the employer was notified must be entered.\n";
		error = true;
	}
	
	trim(document.first_report.description.value);
	if (document.first_report.description.value=="") {
		msg = msg + "\tA description of the accident must be entered.\n";
		error = true;
	}

	if (document.first_report.injury_code.value=="none") {
		msg = msg + "\tAn injury code must be entered.\n";
		error = true;
	}

	if (document.first_report.injury_extent_code.value=="none") {
		msg = msg + "\tAn injury extent code must be entered.\n";
		error = true;
	}
	
	if (document.first_report.injury_cause_code.value=="none") {
		msg = msg + "\tAn injury cause code must be entered.\n";
		error = true;
	}
	
	if (document.first_report.returned[0].checked && document.first_report.date_returned.value=="") {
		msg = msg + "\tIf injured employee returned to work, a date returned must be entered.\n";
		error = true;
	}
	
	if (document.first_report.died[0].checked && document.first_report.date_died.value=="") {
		msg = msg + "\tThe date the employee died must be entered.\n";
		error = true;
	}
	
	if (document.first_report.managed[0].checked && document.first_report.provider.value=="non") {
		msg = msg + "\tA managed care provider must be entered.\n";
		error = true;
	}
	
	if (document.first_report.legal.value=="") {
		msg = msg + "\tA legal business name must be entered.\n";
		error = true;
	}
	
	if (document.first_report.fed_id.value=="") {
		msg = msg + "\tA federal ID must be entered.\n";
		error = true;
	}
	
	if (document.first_report.business_address.value=="") {
		msg = msg + "\tA business address must be entered.\n";
		error = true;
	}
	
	if (document.first_report.business_city.value=="") {
		msg = msg + "\tA business city must be entered.\n";
		error = true;
	}
	
	if (document.first_report.business_state.value=="") {
		msg = msg + "\tA business state must be entered.\n";
		error = true;
	}
	
	if (document.first_report.business_zip.value=="") {
		msg = msg + "\tA business zip code must be entered.\n";
		error = true;
	}
	
	if (document.first_report.name_submit.value=="") {
		msg = msg + "\tThe submitter's name must be entered.\n";
		error = true;
	}
	
	if (document.first_report.title.value=="") {
		msg = msg + "\tThe submitter's title must be entered.\n";
		error = true;
	}
	
	
/*	 if ((document.first_report.date_emp_notified.value < document.first_report.date_injury.value )) {
	    msg = msg  + "\tThe injury date can not be after the date you notified your employer.\n";
		  error = true;
     }		  
*/    
    		
	msg = msg + "\nPlease correct these error(s) and submit the form again.";

	if (error) {
		alert(msg);
		return false;
	} else {
		alert("The information you input will now be formatted and\ndisplayed on your screen using Adobe's Acrobat Reader.\n\nYou must then PRINT THIS FORM and mail a\ncopy to the New Hampshire Department of Labor\nand your insurance carrier.");
	}
}



/*this function calls on the dateCheck() function in library.js  
it ensures that the format of the date has been entered correctly.
the format is mm/dd/yyyy or m/d/yyyy
*/
function mydateCheck (dateStr, today) {
  	if (dateStr.value !="") {
    	//today = today.substring(0, 10);
		if (!(compareDates(dateStr.value, today))) {
			alert("Please fill in a valid date using the format mm/dd/yyyy.\nYou may not enter a future date.");
			dateStr.focus();
			dateStr.select();
			return false;
		}
		
		if (!(dateCheck(dateStr.value, today))) {	
			alert("Please fill in a valid date using the format mm/dd/yyyy.");
			dateStr.focus();
			dateStr.select();
			return false;
		}
		else {
			return true;
		}
		
		if (document.occupational_disease) {
			if (dateStr==document.occupational_disease.doctor_date1 || dateStr==document.occupational_disease.doctor_date2) {
				var a = document.occupational_disease.doctor_date1;
				var b = document.occupational_disease.doctor_date2;
				checkService(a, b, dateStr);
			}
			else if (dateStr==document.occupational_disease.hospital_date1 || dateStr==document.occupational_disease.hospital_date2) {
				var a = document.occupational_disease.hospital_date1;
				var b = document.occupational_disease.hospital_date2;
				checkService(a, b, dateStr);
			}	
			else if (dateStr==document.occupational_disease.other_date1 || dateStr==document.occupational_disease.other_date2) {
				var a = document.occupational_disease.other_date1;
				var b = document.occupational_disease.other_date2;
				checkService(a, b, dateStr);
			}
		}
  	}
	else {
		return true;
	}	 
}
//this function is used exclusively for comparing dates of service on form 8AWC
function checkService(a, b, dateStr) {
	if (!(compareDates(a.value, b.value))) {
		alert("The date service began must be before the date service ended.");	
		dateStr.focus();
		dateStr.select();
		//return;
	}
}

/*this function ensures the proper formatting of the phone number*/
function myphoneCheck (phoneStr) {
  if (phoneStr.value !="") {
    if (!(phoneCheck(phoneStr.value))){	
		alert("Please fill in a valid phone number using the format ###-###-####.");
		phoneStr.focus();
		phoneStr.select();
		return false;
	}
	else {
		return true;
	} 
  }
  else {
  	return true;
  } 
}

/*this function ensures that only a valid state is entered.
it even changes the text to upper case if the user has entered it in lower case*/
function validateState(State) {
	if (State.value!="") {
		var sState = trim(State.value.toUpperCase());
		
		if (!((sState == "AK") || (sState == "AL") || (sState == "AR") || (sState == "AS") || (sState == "AZ") ||
			(sState == "CA") || (sState == "CO") || (sState == "CT") || (sState == "DC") || (sState == "DE") ||
			(sState == "FL") || (sState == "GA") || (sState == "HI") || (sState == "IA") || (sState == "ID") ||
			(sState == "IL") || (sState == "IN") || (sState == "KS") || (sState == "KY") || (sState == "LA") ||
			(sState == "MA") || (sState == "MD") || (sState == "ME") || (sState == "MI") || (sState == "MN") ||
			(sState == "MO") || (sState == "MS") || (sState == "MT") || (sState == "NC") || (sState == "ND") ||
			(sState == "NE") || (sState == "NH") || (sState == "NJ") || (sState == "NM") || (sState == "NV") ||
			(sState == "NY") || (sState == "OH") || (sState == "OK") || (sState == "OR") || (sState == "PA") ||
			(sState == "PR") || (sState == "RI") || (sState == "SC") || (sState == "SD") || (sState == "TN") ||
			(sState == "TX") || (sState == "UT") || (sState == "VA") || (sState == "VT") || (sState == "WA") ||
			(sState == "WI") || (sState == "WV") || (sState == "WY"))) {
			alert("State is not a valid state code.");
			State.focus();
			State.select();
			return false;
		}
		else {
			return true;
		}
	}
	else {
	 	return true;
	}
}

function checkZip(Zipstr) {
	if (Zipstr.value!="") {
		var pattern = /[0-9]{5}/;

		// See if number is valid 
		if ((Zipstr.value.match(pattern)==null) || (Zipstr.value=="00000")) {
			alert("Please fill in a valid zip code using the format #####.");
			Zipstr.focus();
			Zipstr.select();
			return false;
		}
		else {
			return true;
		}
	}
	else {
		return true;
	}
}

function checkAge(age) {
	if (age.value!="") {
		var pattern = /[0-9]{2}/;
		if (age.value.match(pattern)==null) {
			alert("Please fill in a valid numeric age.");
			age.focus();
			age.select();
			return false;
		}
		else {
			return true;
		}
	}
	else {
		return true;
	}
}

function checkSSN(ssn) {
	if (ssn.value!="") {
		var pattern = /[0-9]{3}\-[0-9]{2}\-[0-9]{4}/;
		if (ssn.value.match(pattern)==null) {
			alert("Please fill in a valid SSN using the format ###-##-####.");
			ssn.focus();
			ssn.select();
			return false;
		}
		else {
			return true;
		}
	}
	else {
		return true;
	}
}

function checkLicense(license) {
	if (license.value!="") {
		var pattern = /^[0-9]{2}[a-zA-Z]{3}[0-9]{5}$/;
		if (license.value.match(pattern)==null) {
			alert("Please fill in a valid NH Driver's License using the format ##XXX#####.");
			license.focus();
			license.select();
			return false;
		}
		else {
			return true;
		}
	}
	else {
		return true;
	}
}

function checkHours(hours) {
	if (hours.value!="") {
		var pattern = /\d{1,2}(.\d{1,2})?$/;
		//var pattern = /\$\d{1,3}(,\d{3})*\.\d{2}$/;
		if ((0 + hours.value) > 24 || hours.value=="0" || hours.value=="00" || hours.value.match(pattern)==null) {
			alert("Please fill in a valid numeric number of hours/day.");
			hours.focus();
			hours.select();
			return false;
		}
		else {
			return true;
		}
	}
	else {
		return true;
	}
}

function checkDays(days) {
	if (days.value!="") {
		var pattern = /[1-7]{1}/;
		if (days.value.match(pattern)==null) {
			alert("Please fill in a number between 1 & 7.");
			days.focus();
			days.select();
			return false;
		}
		else {
			return true;
		}
	}
	else {
		return true;
	}
}

function checkFed(id) {
	if (id.value!="") {
		var pattern = /[0-9]{9}/;
		if (id.value.match(pattern)==null || (id.value=="000000000")) {
			alert("Please fill in a valid 9 digit number.");
			id.focus();
			id.select();
			return false;
		}
		else {
			return true;
		}
	}
	else {
		return true;
	}
}

function checkEmp(emp) {
	if (emp.value!="") {
		var pattern = /\D/g;
		
		if ((emp.value.match(pattern)!=null) || (emp.value=="0") || (emp.value=="00") || (emp.value=="000") ||
		(emp.value=="0000") || (emp.value=="00000")) {
			if (document.request_form) {
				if (emp==document.request_form.ext) {
					alert("Please fill in a valid numeric extension.")
				}
				else {
					alert("Please fill in a valid numeric number of employees.")
				}
			}
			else {
				alert("Please fill in a valid numeric number of employees.")
			}
			
			emp.focus();
			emp.select();
			return false;
		}
		else {
			return true;
		}
	}
	else {
		return true;
	}
}

function checkMoney(moneyStr) {
	if (moneyStr) {
		if (moneyStr.value!="") {
			var pattern = /\$\d{1,3}(,\d{3})*\.\d{2}$/;
			if (moneyStr.value.match(pattern)==null) {
				alert("Please fill in a monetary value using the format $##,###.00.");
				moneyStr.focus();
				moneyStr.select();
				return false;
			}
			else {
				return true;
			}
		}
		else {
			return true;
		}
	}
}

function validateMoney(moneyStr) {
	if (moneyStr) {
		var strMoney = moneyStr.value
		strMoney = trim(strMoney)
		
		if (strMoney!="") {
			//remove the dollar sign and the commas
			strMoney = strMoney.replace(/,/g, "")
			strMoney = strMoney.replace(/\$/g, "")
			
			var pattern = /^\d{1,9}\.\d{2}$/;
			var pattern2 = /^[0]{1,9}\.[0]{2}$/;
			if ((strMoney.match(pattern)==null) || (strMoney.match(pattern2))) {
				alert("Please fill in a monetary value using the format ######.00.");
				moneyStr.focus();
				moneyStr.select();
				return false;
			}
			else {
				moneyStr.value = strMoney
				return true;
			}
		}
		else {
			moneyStr.value = strMoney
			return true;
		}
	}
}

function checkNo(textbox) {
	if (textbox==document.first_report.date_returned) {
		if (document.first_report.returned[1].checked) {
			alert("Cannot enter date if injured has not returned to work.");
			document.first_report.equipment.focus();
		}
	}
	
	if (textbox==document.first_report.return_job) {
		if (document.first_report.returned[1].checked) {
			alert("Cannot enter return job if injured has not returned to work.");
			document.first_report.equipment.focus();
		}
	}
	
	if (textbox==document.first_report.full_duty) {
		if (document.first_report.returned[1].checked) {
			alert("Cannot check full duty if injured has not returned to work.");
			document.first_report.equipment.focus();
		}
	}
	
	if (textbox==document.first_report.light_duty) {
		if (document.first_report.returned[1].checked) {
			alert("Cannot check light duty if injured has not returned to work.");
			document.first_report.equipment.focus();
		}
	}
	
	if (textbox==document.first_report.date_died) {
		if (document.first_report.died[1].checked) {
			alert("Cannot enter date if injured has not died.");
			document.first_report.legal.focus();
		}
	}
	
	if (textbox==document.first_report.provider) {
		if (document.first_report.managed[1].checked) {
			alert("Cannot enter provider if not enrolled in Managed Care Program.");
			document.first_report.full_time.focus();
		}
	}
}

function checkRadio() {
	if (document.first_report.returned[1].checked) {
		document.first_report.date_returned.value="";
		document.first_report.return_job.value="";
		document.first_report.full_duty.checked=false;
		document.first_report.light_duty.checked=false;
	}
	
	if (document.first_report.died[1].checked) {
		document.first_report.date_died.value="";
	}
	
	if (document.first_report.managed[1].checked) {
		document.first_report.provider.value="non";
	}
}

function checkTime(timeStr) {
	// Checks if time is in HH:MM:SS AM/PM format.
	// The seconds and AM/PM are optional.
	if (timeStr.value!="") {
		var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;
		var matchArray = timeStr.value.match(timePat);
		
		if (matchArray == null) {
			alert("Time is not in a valid format.");
			timeStr.focus();
			timeStr.select();
			return false;
		}
		
		hour = matchArray[1];
		minute = matchArray[2];
		second = matchArray[4];
		ampm = matchArray[6];
		
		if (second=="") { second = null; }
		if (ampm=="") { ampm = null }
		
		if (hour < 0  || hour > 12) {
			alert("Hour must be between 1 and 12.");
			timeStr.focus();
			timeStr.select();
			return false;
		}
		else if (ampm == null) {
			alert("You must specify AM or PM.");
			timeStr.focus();
			timeStr.select();
			return false;
		}
		else if (minute<0 || minute > 59) {
			alert ("Minute must be between 0 and 59.");
			timeStr.focus();
			timeStr.select();
			return false;
		}
		else if (second != null && (second < 0 || second > 59)) {
			alert ("Second must be between 0 and 59.");
			timeStr.focus();
			timeStr.select();
			return false;
		}
		else {
			return true;
		}
		return false;
	}
	else {
		return true;
	}
}

function checkDesc(desc) {
	var pattern = /^\s{1,300}$/;
	if (desc.value!="") {
		if (desc.value.length>300) {
			alert("Please limit your description to 300 characters.");
			desc.focus();
			return false;
		}
		else if (desc.value.match(pattern)) {
			alert("Please fill in a description of your injury.");
			desc.focus();
			desc.select();
			return false;
		}
		else {
			return true;
		}
	}
	else {
		return true;
	}
}

/*this function splits the description large textarea into 5 separate
substrings.  the substrings are then assigned to the values of the hidden fields above*/ 
function splitDescription_1(str) {
	var index_1, index_2, index_3, index_4, index_5, desc_1, desc_2, desc_3, desc_4, desc_5;

	if (str.length<100) {
		index_1 = str.length;
		desc_1 = str.slice(0, index_1);
		desc_2 = "";
		desc_3 = "";
		desc_4 = "";
		desc_5 = "";
	}
		
/*the .length property returns the number of characters in the string.
the .lastIndexOf property searches for a space starting from the character 
i specify.  in this way I can ensure that the substrings do not 
cut off any words in the middle.  the .slice property returns a substring 
based on the index */
		
	if (str.length>100 && str.length<200) {
		index_1 = str.lastIndexOf(" ", 100);
		index_2 = str.length;
		desc_1 = str.slice(0, index_1);
		desc_2 = str.slice(index_1, index_2);
		desc_3 = "";
		desc_4 = "";
		desc_5 = "";
	} 
		
	if (str.length>200 && str.length<300) {
		index_1 = str.lastIndexOf(" ", 100);
		index_2 = str.lastIndexOf(" ", 200);
		index_3 = str.length;
		desc_1 = str.slice(0, index_1);
		desc_2 = str.slice(index_1, index_2);
		desc_3 = str.slice(index_2, index_3);
		desc_4 = "";
		desc_5 = "";
	}
		
	if (str.length>300 && str.length<400) {
		index_1 = str.lastIndexOf(" ", 100);
		index_2 = str.lastIndexOf(" ", 200);
		index_3 = str.lastIndexOf(" ", 300);
		index_4 = str.length;
		desc_1 = str.slice(0, index_1);
		desc_2 = str.slice(index_1, index_2);
		desc_3 = str.slice(index_2, index_3);
		desc_4 = str.slice(index_3, index_4);
		desc_5 = "";
	}
		
	if (str.length>400 && str.length<500) {
		index_1 = str.lastIndexOf(" ", 100);
		index_2 = str.lastIndexOf(" ", 200);
		index_3 = str.lastIndexOf(" ", 300);
		index_4 = str.lastIndexOf(" ", 400);
		index_5 = str.length;
		desc_1 = str.slice(0, index_1);
		desc_2 = str.slice(index_1, index_2);
		desc_3 = str.slice(index_2, index_3);
		desc_4 = str.slice(index_3, index_4);
		desc_5 = str.slice(index_4, index_5);
	}
		
	if (str.length>225) {
		alert("Please limit your description to 225 characters.")
		document.occupational_disease.description.focus();
		return false;
	}
//this code assigns the value of the "desc" variables to the hidden text boxes
	document.occupational_disease.desc_1.value = desc_1
	document.occupational_disease.desc_2.value = desc_2
	document.occupational_disease.desc_3.value = desc_3
	document.occupational_disease.desc_4.value = desc_4
	document.occupational_disease.desc_5.value = desc_5
}

