monName = new Array ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
now = new Date;
year = now.getFullYear();

function year_options() {
	document.write("<option value='null' selected='selected'>Year</option>");
	document.write("<option>" + (now.getFullYear() - 1) + "</option>");
	document.write("<option>" + now.getFullYear() + "</option>");
	document.write("<option>" + (now.getFullYear() + 1) + "</option>");
	document.write("<option>" + (now.getFullYear() + 2) + "</option>");
}

function month_options() {
	document.write("<option value='null' selected='selected'>Month</option>");
	for (i=0; i<12; i++) {			
		document.write("<option value=" + (i+1) + ">" + monName[i] + "</option>");
	}
}

function change_days(month, id, form_name) {
	if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
		for (i=29; i<32; i++) {
			eval("document." + form_name + "." + id + "day.options[i].value = i");
			eval("document." + form_name + "." + id + "day.options[i].text = i");
		}
	}
	else if (month == 4 || month == 6 || month == 9 || month == 11) {
		for (i=29; i<31; i++) {
			eval("document." + form_name + "." + id + "day.options[i].value = i");
			eval("document." + form_name + "." + id + "day.options[i].text = i");
		}
		eval("document." + form_name + "." + id + "day.options[31].value = 'null'");
		eval("document." + form_name + "." + id + "day.options[31].text = ''");
	}
	else if (month == 2 && (year %  4 == 0) ) {
		eval("document." + form_name + "." + id + "day.options[29].value = 29");
		eval("document." + form_name + "." + id + "day.options[29].text = 29");
		for (i=30; i<32; i++) {
			eval("document." + form_name + "." + id + "day.options[i].value = 'null'");
			eval("document." + form_name + "." + id + "day.options[i].text = i");
		}
	}
	else {
		for (i=29; i<32; i++) {
			eval("document." + form_name + "." + id + "day.options[i].value = 'null'");
			eval("document." + form_name + "." + id + "day.options[i].text = ''");
		}
	}
}

function load_images() {
		add_off_button = new Image;
		add_on_button = new Image;
		add_button = new Image;
		update_off_button = new Image;
		update_on_button = new Image;
		update_button = new Image;
		list_off_button = new Image;
		list_on_button = new Image;
		list_button = new Image;
		backup_off_button = new Image;
		backup_on_button = new Image;
		backup_button = new Image;
		restore_off_button = new Image;
		restore_on_button = new Image;
		restore_button = new Image;
		
		add_off_button.src = "images/add_off.gif";
		add_on_button.src = "images/add_on.gif";
		add_button.src = "images/add.gif";
		update_off_button.src = "images/update_off.gif";
		update_on_button.src = "images/update_on.gif";
		update_button.src = "images/update.gif";
		add_off_button.src = "images/add_off.gif";
		add_on_button.src = "images/add_on.gif";
		add_button.src = "images/add.gif";
		list_off_button.src = "images/list_off.gif";
		list_on_button.src = "images/list_on.gif";
		list_button.src = "images/list.gif";
		backup_off_button.src = "images/backup_off.gif";
		backup_on_button.src = "images/backup_on.gif";
		backup_button.src = "images/backup.gif";
		restore_off_button.src = "images/restore_off.gif";
		restore_on_button.src = "images/restore_on.gif";
		restore_button.src = "images/restore.gif";
}

function validate_add_form(passForm) {
	 
	if(passForm.first_name.value == "") {
		alert("You must enter a first name");
		passForm.first_name.focus();
		return false;
	}
	
	if(passForm.last_name.value == "") {
		alert("You must enter a last name");
		passForm.last_name.focus();
		return false;
	}
	
	// Check for Guard Card # if DD-214 is not selected
	if(passForm.g_card_number.value == "" && !(passForm.training[3].checked)) {
		alert("You must enter a guard card #");
		passForm.g_card_number.focus();
		return false;
	}
	
	// Check guard date if DD-214 is not checked
	if((passForm.g_year.value == "null" || passForm.g_month.value == "null" || passForm.g_day.value == "null") && !(passForm.training[3].checked)) {
		alert("You must enter a Guard Card Expiration Date");
		passForm.h_year.focus();
		return false;
	}
	
	// Check for a hire date
	if(passForm.h_year.value == "null" || passForm.h_month.value == "null" || passForm.h_day.value == "null") {
		alert("You must enter a hire date");
		passForm.h_year.focus();
		return false;
	}
	
	// Check DD-214 Date if a DD-214 is being used in lieu of a guard card
	if(passForm.training[3].checked == true && (passForm.d_year.value == "null" || passForm.d_month.value == "null" || passForm.d_day.value == "null")) {
		alert("You have selected DD-214 \n You must enter a DD-214 date");
		passForm.d_year.focus();
		return false;
	}
	
// Password checking and email verification code concepts taken from JavaScript for the WWW, Tom Negrino and Dori Smith
	
	re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	if(passForm.email.value != "") {
		if(!re.test(passForm.email.value)) {
			alert("Invalid Email Address");
			passForm.email.focus();
			passForm.email.select();
			return false;
		}
	}
	
	if(passForm.password1.value == "" && passForm.password2.value != "") {
		alert("You must enter the password twice");
		passForm.password1.focus();
		return false;
	}
	
	if(passForm.password2.value == "" && passForm.password1.value != "") {
		alert("You must enter the password twice");
		passForm.password2.focus();
		return false;
	}
	
	if(passForm.password1.value != passForm.password2.value) {
		alert("Entered passwords did not match");
		passForm.password1.focus();
		passForm.password1.select();
		return false;
	}
	
	if(passForm.password1.value.length < 6 && passForm.password1.value != "") {
		alert("Your password needs to be between 6 and 10 characters");
		passForm.password1.focus();
		passForm.password1.select();
		return false;
	}
	
	// If passwords are entered, check for email address
	if(passForm.password1.value != "" && passForm.email.value == "") {
		alert("You must enter in a valid Email address");
		passForm.email.focus();
		passForm.email.select();
		return false;
	} 
	
	return true;
}

function selected_DD214() {
	document.add.g_card_number.disabled=true; 
	document.add.g_card_number.value=""; 
	document.add.g_year.selectedIndex=0; 
	document.add.g_month.selectedIndex=0; 
	document.add.g_day.selectedIndex=0; 
	document.add.g_year.disabled=true; 
	document.add.g_month.disabled=true; 
	document.add.g_day.disabled=true;
	document.add.d_year.disabled=false;
}

function selected_hours() {
	document.add.g_card_number.disabled=false;
	document.add.g_year.disabled=false;
	document.add.d_year.disabled=true;
	document.add.d_month.disabled=true; 
	document.add.d_day.disabled=true;
	document.add.d_year.selectedIndex=0; 
	document.add.d_month.selectedIndex=0; 
	document.add.d_day.selectedIndex=0; 
}

function checkDate(passDate) {

 	if(confirm("This will over-write the existing data in the database.\n Please confirm you wish to replace the existing data with the contents of the backup file.")) {
 		return true;
 	}
 	else {
 		return false;
	}
 
 	if(passDate.filedate.value == 'NONE') {
 		alert("There is no backup file to restore!");
		return false;
 	} 
 	else {
 		return true;
	}
}

function reload_update(newLoc) {
	empl_ID = newLoc.options[newLoc.selectedIndex].value;
	
	if(empl_ID != "") {
		window.location = "update.php?e_ID=" + empl_ID;
	}
}

function validate_update_form(passForm) {
	// Active Employee not set, do nothing
	if(passForm.active_employee.value == "") return false;

	// Validate Guard Card Update / Creation
	
	// Check for a valid guard date
	if(passForm.g_year.value != "null" && (passForm.g_month.value == "null" || passForm.g_day.value == "null")) {
		alert("You must enter a valid Guard Card Expiration Date");
		passForm.g_year.focus();
		return false;
	}
	
	// Check for guard card # if an expiration date is set and the current record does not contain a guard card #
	if(passForm.r_g_card_number.value == " " && passForm.g_year.value != "null" && passForm.g_card_number.value == "") {
		alert("You must enter a guard card #");
		passForm.g_card_number.focus();
		return false;
	}
	
	// Check for guard card date if a card # is set and the current record does not contain a guard card #
	if(passForm.r_g_card_number.value == " " && passForm.g_year.value == "null" && passForm.g_card_number.value != "") {
		alert("You must enter a valid Guard Card Expiration Date");
		passForm.g_year.focus();
		return false;
	}

	// Validate Firearm Card Update / Creation
	
	// Check for a valid firearm date
	if(passForm.f_year.value != "null" && (passForm.f_month.value == "null" || passForm.f_day.value == "null")) {
		alert("You must enter a valid Firearm Card Expiration Date");
		passForm.f_year.focus();
		return false;
	}
	
	// Check for firearm card # if an expiration date is set and the current record does not contain a firearm #
	if(passForm.r_f_card_number.value == " " && passForm.f_year.value != "null" && passForm.f_card_number.value == "") {
		alert("You must enter a Firearm Card Number");
		passForm.f_card_number.focus();
		return false;
	}
	
	// 
	if(passForm.r_f_card_number.value == " " && passForm.f_year.value == "null" && passForm.f_card_number.value != "") {
		alert("You must enter a valid Firearm Card Expiration Date");
		passForm.f_year.focus();
		return false;
	}
	
	// Check Termination date
	if(passForm.t_year.value != "null" && (passForm.t_month.value == "null" || passForm.t_day.value == "null")) {
		alert("You must enter a valid Termination Date");
		passForm.t_year.focus();
		return false;
	}
	
	// Check DD-214 date
	if(passForm.d_year.value != "null" && (passForm.d_month.value == "null" || passForm.d_day.value == "null")) {
		alert("You must enter a valid DD-214 Date");
		passForm.d_year.focus();
		return false;
	}
	
	// Password checking and email verification code concepts taken from JavaScript for the WWW, Tom Negrino and Dori Smith
	
	re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	if(passForm.email.value != "") {
		if(!re.test(passForm.email.value)) {
			alert("Invalid Email Address");
			passForm.email.focus();
			passForm.email.select();
			return false;
		}
	}
	
	if(passForm.password1.value == "" && passForm.password2.value != "") {
		alert("You must enter the password twice");
		passForm.password1.focus();
		return false;
	}
	
	if(passForm.password2.value == "" && passForm.password1.value != "") {
		alert("You must enter the password twice");
		passForm.password2.focus();
		return false;
	}
	
	if(passForm.password1.value != passForm.password2.value) {
		alert("Entered passwords did not match");
		passForm.password1.focus();
		passForm.password1.select();
		return false;
	}
	
	if(passForm.password1.value.length < 6 && passForm.password1.value != "") {
		alert("Your password needs to be between 6 and 10 characters");
		passForm.password1.focus();
		passForm.password1.select();
		return false;
	}
	
	// If passwords are entered, check for email address
	if(passForm.password1.value != "" && passForm.email.value == "") {
		alert("You must enter in a valid Email address");
		passForm.email.focus();
		passForm.email.select();
		return false;
	}
	
return true;
}

function set_update_form() {

	// Set Training Checkboxes based on value in the Training text box
	train = parseInt(document.update.training.value);
	if(train != "") {
		if(train >= 8) {
			document.update.eight_hour.checked = true;
			document.update.eight_hour.disabled = true;
		} 
		if(train >= 16) {
			document.update.sixteen_hour.checked = true;
			document.update.sixteen_hour.disabled = true;
		}
		if(train == 40) {
			document.update.forty_hour.checked = true;
			document.update.forty_hour.disabled = true;
		}
	}
	if(document.update.calibers.value) add_caliber();
	if(document.update.d_date.value.length > 9) document.update.d_year.disabled = false;
	else document.update.d_year.disabled = true;
	
}

function add_caliber() {
	if(document.update.calibers.value) {
		list_of_calibers = new Array;
		list_of_calibers = document.update.calibers.value.split(" ");
		
		// Set all of the calibers to unchecked and enabled
		document.update.c_22.checked=false;
		document.update.c_22.disabled=false;
		document.update.c_25.checked=false;
		document.update.c_25.disabled=false;
		document.update.c_32.checked=false;
		document.update.c_32.disabled=false;
		document.update.c_357.checked=false;
		document.update.c_357.disabled=false;
		document.update.c_38.checked=false;
		document.update.c_38.disabled=false; 
		document.update.c_380.checked=false;
		document.update.c_380.disabled=false; 
		document.update.c_40.checked=false;
		document.update.c_40.disabled=false; 
		document.update.c_44.checked=false;
		document.update.c_44.disabled=false; 
		document.update.c_45.checked=false;
		document.update.c_45.disabled=false;
		document.update.c_50.checked=false;
		document.update.c_50.disabled=false; 
		document.update.c_9mm.checked=false;
		document.update.c_9mm.disabled=false; 
		document.update.c_10mm.checked=false;
		document.update.c_10mm.disabled=false;
		
		for (i=1; i<= list_of_calibers.length ; i++) {
			switch (list_of_calibers[i]) {
				case ".22": 
					document.update.c_22.checked=true;
					document.update.c_22.disabled=true;
					break;
				case ".25": 
					document.update.c_25.checked=true;
					document.update.c_25.disabled=true;
					break;
				case ".32": 
					document.update.c_32.checked=true;
					document.update.c_32.disabled=true;
					break;
				case ".357": 
					document.update.c_357.checked=true;
					document.update.c_357.disabled=true;
					break;
				case ".38":
					document.update.c_38.checked=true;
					document.update.c_38.disabled=true;
					break;
				case ".380": 
					document.update.c_380.checked=true;
					document.update.c_380.disabled=true;
					break;
				case ".40": 
					document.update.c_40.checked=true;
					document.update.c_40.disabled=true;
					break;
				case ".44": 
					document.update.c_44.checked=true;
					document.update.c_44.disabled=true;
					break;
				case ".45": 
					document.update.c_45.checked=true;
					document.update.c_45.disabled=true;
					break;
				case ".50": 
					document.update.c_50.checked=true;
					document.update.c_50.disabled=true;
					break;
				case "9mm": 
					document.update.c_9mm.checked=true;
					document.update.c_9mm.disabled=true;
					break;
				case ".10mm": 
					document.update.c_10mm.checked=true;
					document.update.c_10mm.disabled=true;
					break;
			} // End-of-Switch
		} // End-of-For
	} // End-of-If
}

function remove_caliber() {
	if(document.update.calibers.value) {
		list_of_calibers = new Array;
		list_of_calibers = document.update.calibers.value.split(" ");
		
		// Set all of the calibers to unchecked and disabled
		document.update.c_22.checked=false;
		document.update.c_22.disabled=true;
		document.update.c_25.checked=false;
		document.update.c_25.disabled=true;
		document.update.c_32.checked=false;
		document.update.c_32.disabled=true;
		document.update.c_357.checked=false;
		document.update.c_357.disabled=true;
		document.update.c_38.checked=false;
		document.update.c_38.disabled=true; 
		document.update.c_380.checked=false;
		document.update.c_380.disabled=true; 
		document.update.c_40.checked=false;
		document.update.c_40.disabled=true; 
		document.update.c_44.checked=false;
		document.update.c_44.disabled=true; 
		document.update.c_45.checked=false;
		document.update.c_45.disabled=true;
		document.update.c_50.checked=false;
		document.update.c_50.disabled=true; 
		document.update.c_9mm.checked=false;
		document.update.c_9mm.disabled=true; 
		document.update.c_10mm.checked=false;
		document.update.c_10mm.disabled=true;
		
		// enable the calibers currently in the firearm record
		for (i=1; i<= list_of_calibers.length ; i++) {
			switch (list_of_calibers[i]) {
				case ".22": 
					document.update.c_22.checked=false;
					document.update.c_22.disabled=false;
					break;
				case ".25": 
					document.update.c_25.checked=false;
					document.update.c_25.disabled=false;
					break;
				case ".32": 
					document.update.c_32.checked=false;
					document.update.c_32.disabled=false;
					break;
				case ".357": 
					document.update.c_357.checked=false;
					document.update.c_357.disabled=false;
					break;
				case ".38":
					document.update.c_38.checked=false;
					document.update.c_38.disabled=false;
					break;
				case ".380": 
					document.update.c_380.checked=false;
					document.update.c_380.disabled=false;
					break;
				case ".40": 
					document.update.c_40.checked=false;
					document.update.c_40.disabled=false;
					break;
				case ".44": 
					document.update.c_44.checked=false;
					document.update.c_44.disabled=false;
					break;
				case ".45": 
					document.update.c_45.checked=false;
					document.update.c_45.disabled=false;
					break;
				case ".50": 
					document.update.c_50.checked=false;
					document.update.c_50.disabled=false;
					break;
				case "9mm": 
					document.update.c_9mm.checked=false;
					document.update.c_9mm.disabled=false;
					break;
				case ".10mm": 
					document.update.c_10mm.checked=false;
					document.update.c_10mm.disabled=false;
					break;
			} // End-of-Switch
		} // End-of-For
	} // End-of-If
}