		function updateCharsLeft() {
			var charsLeft = 255-$("#problem_description").attr("value").length;
			if(charsLeft > 25) {
				$("#description h1").html("Characters Left [" + charsLeft + "]");
			} else {
				$("#description h1").html("Characters Left [<span style='color: red;'>" + charsLeft + "</span>]");
			}
		}
		
		function monthReturn(y,m) {
			document.getElementById("appliance_repair").install_date.value = m + "/" + y;
		}
		
		
		/* 
		* 	initiate calendar popup settings
		*/

		var dateRequested = new CalendarPopup();
		
		var dateInstalled = new CalendarPopup();
		
		dateInstalled.setDisplayType("month");
		dateInstalled.setReturnMonthFunction("monthReturn");
		dateInstalled.showYearNavigation();	
		
		$(document).ready(function() {		
			updateCharsLeft();
			
			$("#date_am").addClass("date_picked");
			
			$("#date_am").click(function() {
				$("#am_pm").attr("value", "8AM - 12PM");
				$(this).addClass("date_picked");
				$("#date_pm").removeClass("date_picked");
				return false;
			});

			$("#date_pm").click(function() {
				$("#am_pm").attr("value", "1PM - 5PM");
				$("#date_am").removeClass("date_picked");
				$(this).addClass("date_picked");
				return false;
			});
			
			$("#list_request a").hover(function() {
				$(this).addClass("date_highlight");
			}, function() {
				$(this).removeClass("date_highlight");
			});

			$("#install_date, #date_requested").focus(function() {
				$(this).select();
				return false;
			}).keypress(function(e) {
				if(e.which != 0) return false;
			});
						
			$("#problem_description").keypress(function(event) {
				var description = document.forms[0].problem_description.value;
				if(description.length >= 255 && (event.charCode != 0 || event.which == 13)) {
					return false;
				}
			}).keyup(function() {
				updateCharsLeft();

				var description = document.forms[0].problem_description.value;
				if(description.length > 255) document.forms[0].problem_description.value = description.substring(0, 255);
			});
			
		});

		function validateForm() {
			var error_msg = "";
			var zip_code_re = new RegExp("^[0-9]{5}$");
			var install_date_re = new RegExp("^(1[0-2]|0*[1-9])/[1,2][0-9]{3}$");
			var date_requested_re = new RegExp("^(1[0-2]|0*[1-9])/(0*[1-9]|[1,2][0-9]|3[0,1])/[1,2][0-9]{3}$");

			if($("#first_name").attr("value") == "") {
				error_msg = "Please enter a first name.";
			} else if($("#last_name").attr("value") == "") {
				error_msg = "Please enter a last name.";
			} else if($("#address").attr("value") == "") {
				error_msg = "Please enter an address.";
			} else if(!$("#zip_code").attr("value").match(zip_code_re)) {
				error_msg = "Please enter a valid zip code.";
			} else if(!$("#install_date").attr("value").match(install_date_re)) {
				error_msg = "Please enter a valid install date.";
			} else if($("#brand").attr("selectedIndex") == 0) {
				error_msg = "Please select a brand.";
			} else if($("#appliance").attr("selectedIndex") == 0) {
				error_msg = "Please select an appliance.";
			} else if($("#problem_description").attr("value") == "") {
				error_msg = "Please enter a description of the problem.";
			} else if($("#contact_num_preferred").attr("value") == "") {
				error_msg = "Please enter a valid contact number at which to reach you.";
			} else if(!$("#date_requested").attr("value").match(date_requested_re)) {
				error_msg = "Please enter a valid appointment request";
			} else if(validateRequestDate()) {
				error_msg = "Your request must be scheduled no earlier than 1 day after today's date.";
			}
			
			if(error_msg != "") {
				alert(error_msg);
				return false;
			}
		}
		
		function validateRequestDate() {
			var today = new Date();
			var tooEarly = false;
			var scheduledDate = new Date($("#date_requested").attr("value"));				
				
			scheduledDate.setDate(scheduledDate.getDate() - 1);
			if(scheduledDate.getYear() >= today.getYear()) {
				if(scheduledDate.getMonth() >= today.getMonth()) {
					if(scheduledDate.getDate() < today.getDate()) {
						tooEarly = true;
					}
				} else {
					tooEarly = true;
				}
			} else {
				tooEarly = true;
			}
			return tooEarly;
		}