
	$(init);
	
	var fileAttached = false;
	
	function init(){
		$("#rfp-form").submit(validateForm);
		$("#preferred-dates input, #alternate-dates input")
			.blur(blurDates)
			.change(blurDates);
		$("#add-agenda-button").click(addAgendaItem);
		$("#remove-agenda-button").click(removeAgendaItem);
		
		$("#preferred-roomNeeds input, #alternate-roomNeeds input")
			.focus(roomNeedsFocus)
			.blur(roomNeedsBlur);
			
		$("#rfp_file_check").click(checkRFPCheck);
		$("#rfp_file").focus(function (){
			$("#rfp-file-check").attr("checked", "checked");
			$("#rfp-input").hide();
			fileAttached = true;
		}).blur(function (){
			if ($(this).val() === ""){
				$("#rfp-input").show();
			}
		});
	}
	
	// Hide/Show Input Fields if file is attached
	function checkRFPCheck(){
		var isChecked = $(this).attr("checked");
		
		if (isChecked){
			$("#rfp-input").hide();
			fileAttached = true;
		}else{
			$("#rfp-input").show();
			fileAttached = false;
		}
	}
	
	
	// Handle Room Needs focus/blur
	function roomNeedsFocus(){
		if($(this).val() === "0") $(this).val("");	
	}
	
	function roomNeedsBlur(){
		if($(this).val() === "") $(this).val("0");
	}
	
	// Add Agenda Items
	function addAgendaItem (){
		var	agendaContainer	= $("#agenda-items"),
				lastItem					= $("ul.item:last", agendaContainer),
				newItem;
				
		newItem = $(lastItem).clone(false).insertBefore("#agenda-buttons");
	}
	
	// Remove Agenda Items
	function removeAgendaItem (){
		var	agendaContainer	= $("#agenda-items"),
				itemCount				= $("ul.item", agendaContainer).length,
				lastItem					= $("ul.item:last", agendaContainer);
				
		if (itemCount > 1 && confirm("Are you sure you want to remove the last event?")){
			$(lastItem).remove();
		}
	}
	
	
	// Handle Arrival / Departure dates and dynamic room needs fields 
	function blurDates (){
		var 	container			= $(this).parent().parent().parent(),
				type					= $(container).attr("id"),
				arrDate				= $(".arr", container).val(),
				depDate			= $(".dep", container).val();
				
		if (arrDate.length && depDate.length){

			var 	diff = findDateDiff(arrDate, depDate),
					roomContainer, original;
			
			if (diff > 0 && diff < 16){			
				if (type === "preferred-dates"){
					roomContainer = $("#preferred-roomNeeds");
				}else if (type === "alternate-dates"){
					roomContainer = $("#alternate-roomNeeds"); 
					$(roomContainer).show();
				}
				
				original = $("ul.item", roomContainer).html();
				$("ul.item", roomContainer).remove();
				
				for (var i=0; i < diff; i++){
					$("<ul></ul>")
						.appendTo(roomContainer)
						.addClass("clear-fix")
						.addClass("item")
						.html(original)
						.find("h5")
						.html("Night "+(i+1))
						.end()
						.find("input")
						.val("0")
						.focus(roomNeedsFocus)
						.blur(roomNeedsBlur);
				}
			}else{
				alert("Your arrival date must be before your departure date and you are limited to 15 days.");
			}		
		}else{
			if (type === "alternate-dates") {
				$("#alternate-roomNeeds").hide().find("input").val("");
			}
		}
	}
	
	// Find Date Difference
	function findDateDiff (from, to){
		var	fromDate			= Date.parse(from),
				toDate				= Date.parse(to),
				diff					= ((toDate-fromDate) / (24*60*60*1000));
				
				
		if (diff > 0) return diff;
		return -1;
	
	}
	
	
	
	
	
	
	// Validate and submit form
	function validateForm(){

		var 	requiredFields 	= (fileAttached) ? $("input[rel='required']:not('#rfp-input input')") : $("input[rel='required']:not('#rfp-upload input')", this),
				emailFields 		= $("input.email", this),
				zipFields 			= $("input.zipcode", this),
				formValues		= {},
				errors 				= [],
				ajaxObj				= {};
				
		
						
		for (var i=0, count=requiredFields.length; i < count; i++){
			if (!requiredFields[i].value.length) errors.push(requiredFields[i]); // If the field is blank, add the field to the errors array
		}
		
		for (var i=0, count=emailFields.length; i < count; i++){
			if (!validateEmail(emailFields[i].value) && emailFields[i].value.length) errors.push(emailFields[i]); // If the value of an email field is not a valid email, add the field to the errors array
		}	
				
		for (var i=0, count=zipFields.length; i < count; i++){
			if (!validateZipcode(zipFields[i].value) && zipFields[i].value.length) errors.push(zipFields[i]); // If the value of a zip field is not a valid zipcode, add the field to the errors array
		}	
		
		
		// If there are errors, display the error messages and highlight the fields.
		if(errors.length){
			$(".has-error", this).removeClass("has-error");
			$("#error-message").fadeIn("fast");
			self.scrollTo(0, 0);
			
			for (var i=0, count=errors.length; i < count; i++){
				$(errors[i]).parent().addClass("has-error")
			}
		}else{
			// No Errors... so allow the form to submit
			$(':text').each(function (){
				if ($(this).val() == '') $(this).val(' - ');
			});
			return true;
		}
		
		return false;
	}	
	
	
	function validateEmail (email){
		var email_regex = new RegExp(/^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/);
		if (email_regex.test(email)) return true;
		return false;
	}
	
	function validateZipcode (zip){
		var zip_regex = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);
		if (zip_regex.test(zip)) return true;
		return false;
	}
	
	function validatePhone (phone){
		var phone_regex = new RegExp(/^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/);
		if (phone_regex.test(phone)) return true;
		return false;
	}
