$(document).ready(function(){
	
	var arr = ["8:00am", "8:30am", "9:00am", "9:30am", "10:00am", "10:30am", "11:00am", "11:30am", "12:00pm", "12:30pm", "1:00pm", "1:30pm", "2:00pm", "2:30pm", "3:00pm", "3:30pm", "4:00pm", "4:30pm", "5:00pm", "5:30pm"];
	
	$.each( arr, function(i, n){	
		$(".selectTime").append("<option value=" + n + ">"+ n +"</option>");
	});
	
	$("#submit").click(function(){
		/* Trac Ticket 976 */
		/***********************************************************************************/
		/* Until further notice the first and last name inputs are only checked for blanks */
		/***********************************************************************************/
		/* 
			tag[0] => First Name
			tag[1] => Email Address
			tag[2] => Last Name
			tag[3] => Move-in Date
			tag[4] => How did you hear about us
		*/
		
		msgArray = new Array(); // For displaying error messages.
		tag = new Array(); // To get the order of the missing elements.
		error = false; // To check if there are any errors.
		
		/* Check move-in date */
		if($("#MoveIn").val().length == 0){
			$("#MoveIn").css("background","#fafad2");					   
			msgArray[3] = "A move-in date is required. eg: mm/dd/yyyy\n\n";
			error = true;
			tag[3] = 4;
		}
		else {
			var filter = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/;
			if (!filter.test($("#MoveIn").val())) { 
				$("#MoveIn").val('').css("background","#fafad2");					   
				msgArray[3] = "Please enter a valid move-in date. eg: mm/dd/yyyy\n\n";
				error = true;
				tag[3] = 4;
			}
		}
		
		/* Check first name */
		if(jQuery.trim($("#FName").val()) == ""){
			$("#FName").empty().css("background","#fafad2");					   
			msgArray[0] = "First name is required.\n\n";
			error = true;
			tag[0] = 1;
		}
		
		/* Check last name */
		if(jQuery.trim($("#LName").val()) == ""){
			$("#LName").empty().css("background","#fafad2");					   
			msgArray[2] = "Last name is required.\n\n";
			error = true;
			tag[2] = 3;
		}
		
		/* Check 'how did you hear' */
		if(jQuery.trim($("#LID").val()) == ""){
			$("#LID").css("background","#fafad2");					   
			msgArray[2] = "'How did you hear about us?' is required.\n\n";
			error = true;
			tag[4] = 5;
		}
		
		/* Check email */
		if(jQuery.trim($("#Email").val()) == ""){
			$("#Email").css("background","#fafad2");					   
			msgArray[1] = "Email address is require.\n\n"
			error = true;;
			tag[1] = 2;
		}
		else {
			var filter = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/; 
			if (!filter.test($("#Email").val())) {
				$("#Email").val('').css("background","#fafad2");					   			   
				msgArray[1] = "Please enter a valid email address.\n\n";
				error = true;
				tag[1] = 2;
			}
		}
		
		if (error){ // An error was found.
			error = false; 
			
			newTag = cleanArray(tag).slice(); // Remove any blank elements in the arrray.
			
			newMsgArray = cleanArray(msgArray).join(''); // Remove the commas in the arrray for display.
			
			alert(newMsgArray); // Display the required elements that are missing.
			
			switch (newTag[0]) { // Place focus on the first missing element.
				case 1:
					$("#FName").focus();
					break;
				case 2:
					$("#Email").focus();
					break;
				case 3:
					$("#LName").focus();
					break;
				case 4:
					$("#Email").focus();
					break;
			}
			return false;
		}
	}); // End of $("#submit").click(function()
});

function cleanArray(actual){
  var newArray = new Array();
  for(var i = 0; i<actual.length; i++){
      if (actual[i]){
        newArray.push(actual[i]);
    }
  }
  return newArray;
}

