// JavaScript Document

// Fill in the date field for the user (this field can still be edited, if necessary)
//
function populateDate() {
	var dateField = document.getElementById("date");
	if (!dateField) return;
	
	var currentTime = new Date();
	var month = currentTime.getMonth() + 1;
	var day = currentTime.getDate();
	var year = currentTime.getFullYear();
	
	dateField.value = month + "/" + day + "/" + year;
}

// This function will populate the billing address fields when the user checks the
// sameascbox checkbox.
//
function populateBillingAddress() {
	var cbox = document.getElementById("sameascbox");
	if (cbox.checked) {
		document.getElementById("billingaddr1").value = document.getElementById("addr1").value;
		document.getElementById("billingaddr2").value = document.getElementById("addr2").value;
		document.getElementById("billingcity").value = document.getElementById("city").value;
		document.getElementById("billingstate").value = document.getElementById("state").value;
		document.getElementById("billingzip").value = document.getElementById("zip").value;
	}
}

// This function will do a cursory check on the required form fields to make sure they have
// data when the user clicks the Submit Request button. Required fields are:
//   date
//   companyname
//   addr1
//   city
//   state
//   zip
//   contactname
//   telephone
//   email
//   descriptionofwork
//
var missingFields = false;
var focusField = "";
function checkRequiredFields() {
	missingFields = false;
	focusField = "";
	if (document.getElementById("date").value == "") callout("date"); else clearCallout("date");
	if (document.getElementById("companyname").value == "") callout("companyname"); else clearCallout("companyname");
	if (document.getElementById("addr1").value == "") callout("addr1"); else clearCallout("addr1");
	if (document.getElementById("city").value == "") callout("city"); else clearCallout("city");
	if (document.getElementById("state").value == "") callout("state"); else clearCallout("state");
	if (document.getElementById("zip").value == "") callout("zip"); else clearCallout("zip");
	if (document.getElementById("contactname").value == "") callout("contactname"); else clearCallout("contactname");
	if (document.getElementById("telephone").value == "") callout("telephone"); else clearCallout("telephone");
	if (document.getElementById("email").value == "") callout("email"); else clearCallout("email");
	if (document.getElementsByName("descriptionofwork")[0].value == "") callout("descriptionofwork"); else clearCallout("descriptionofwork");

	if (missingFields == true)
		alert("Please complete all required fields before hitting Submit Request.\n\n"
			+ "Fields that still require information are preceded by a green asterisk (*), and have green labels.");

	return (!missingFields);
}
function callout(id) {
	missingFields = true;
	
	// set the focus to the first empty required field
	if (focusField == "") {
		focusField = id;
		
		// since "descriptionofwork" is a textarea, it is treated slightly differently than the text inputs
		if (id != "descriptionofwork")
			document.getElementById(focusField).focus();
		else
			document.getElementsByName("descriptionofwork")[0].focus();
	}
	
	var e = document.getElementById(id + "Label");
	if (e) {
		e.style.color = "#00B72C";
	}
}
function clearCallout(id) {
	var e = document.getElementById(id + "Label");
	if (e) {
		e.style.color = "#FFFFFF";
	}
}
