/**
 * @author Brett Dworaczyk 2010
 */
$(document).ready(function()
{
	//regexp used to test the email address against
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	//regexp used to test for digits inside the name field
	var nameDigitTest = /\d/;
	//regexp used to test for characters inside the color fields
	var colorCharTest = /\D/;
	//regexp used to test zip code pattern
	var zipCodePattern = /^\d{5}$|^\d{5}-\d{4}$/;
	
	$("#quote_submit").click(function(e)
	{
		var hasError = false;
		var ff_num = $("#full_front").val();
		var flc_num = $("#front_left_chest").val();
		var fb_num = $("#full_back").val();
		var tb_num = $("#tag_back").val();
		var ls_num = $("#left_sleeve").val();
		var rs_num = $("#right_sleeve").val();
						
		//check the color number values for ranges above 8 and for values that are not digits		
		$(".check_digit input").each(function(i)
		{
			if($(this).val() > 8)
			{
				if(!$(this).next().is(".form_error_msg"))
				{
					$(this).after("<span class='form_error_msg'>Please enter a valid quantity</span>");
				}
				hasError = true;
			}
			
			if(isNaN($(this).val()))
			{
				if(!$(this).next().is(".form_error_msg"))
				{
					$(this).after("<span class='form_error_msg'>Can only contain numbers</span>");
				}
				hasError = true;
			}
		});
		
		//check if email address is valid or blank
		var emailString = $("#customer_email").val();
		if(emailString == "")
		{
			if(!($("#customer_email").next().is(".form_error_msg")))
			{
				$("#customer_email").after("<span class='form_error_msg'>Please enter an email address</span>");
			}			
			hasError = true;
		}
		else if(!emailReg.test(emailString))
		{
			if(!($("#customer_email").next().is(".form_error_msg")))
			{
				$("#customer_email").after("<span class='form_error_msg'>Please enter a valid email address</span>");
			}			
			hasError = true;
		}
						
		//check if name field is invalid or blank
		var nameString = $("#customer_name").val();
		if(nameString == "")
		{
			if(!($("#customer_name").next().is(".form_error_msg")))
			{
				$("#customer_name").after("<span class='form_error_msg'>Please enter a name</span>");
			}			
			hasError = true;
		}
		else if(nameString.length < 2)
		{
			if(!($("#customer_name").next().is(".form_error_msg")))
			{
				$("#customer_name").after("<span class='form_error_msg'>Name must be at least 2 characters</span>");
			}
			hasError = true;
		}
		
		if($("#selected_item_to_customize").length != 0)
		{
			if($("#selected_item_to_customize").val() == "")
			{
				if(!($("#selected_item_to_customize").next().is(".form_error_msg")))
				{
					$("#selected_item_to_customize").after("<span class='form_error_msg'>Please select a type from the list</span>");
				}
				hasError = true;
			}
			/*else
			{
				$("#item_name").val() = $("#selected_item_to_customize").val();
			}*/
		}
						
		//check the selection in the customization type select box
		var custType = ($("#type_select").val()).toLowerCase();
		if(custType == "(select one)")
		{
			if(!($("#type_select").next().is(".form_error_msg")))
			{
				$("#type_select").after("<span class='form_error_msg'>Please select a type from the list</span>");
			}
			hasError = true;
		}
		
		//check the selection in the garment colors select box
		var garmColors = ($("#garment_color_choice").val()).toLowerCase();
		if(garmColors == "(select one)")
		{
			if(!($("#garment_color_choice").next().is(".form_error_msg")))
			{
				$("#garment_color_choice").after("<span class='form_error_msg'>Please select a choice from the list</span>");
			}
			hasError = true;
		}
				
		//check if the "quantity" field has been set
		var getQuantity = $("#quantity_select").val();
		if(getQuantity == "")
		{
			if(!($("#quantity_select").next().is(".form_error_msg")))
			{
				$("#quantity_select").after("<span class='form_error_msg'>Please enter a valid quantity</span>");
			}
			hasError = true;
		}
		if(getQuantity < 6)
		{
			if(!($("#quantity_select").next().is(".form_error_msg")))
			{
				$("#quantity_select").after("<span class='form_error_msg'>The minimum quantity is 6</span>");
			}
			hasError = true;
		}
		
		
		//check if the shipping zip code has been set
		var shipZip = $("#shipping_zip").val();
		if(shipZip == "")
		{
			if(!($("#shipping_zip").next().is(".form_error_msg")))
			{
				$("#shipping_zip").after("<span class='form_error_msg'>Please enter your shipping zip code</span>");
			}
			hasError = true;
		}		
		
		//for testing
		//hasError = false;
		
		//see if there are any errors by checking the value of hasError
		if(hasError == false)
		{
			$(this).hide();
			$("#quote_form").after("<img src='catalog/view/theme/new_theme/image/loading_1.gif' border='0' alt='loading' title='loading' id='loading' />");
			
			//submit form (with attachments if necessary) via the JQuery Form Plugin			
			var options =
			{
				target: '#message_text',
				success: displayResponse,
				url: './process_quick_quote.php',
				type: 'post'
				//dataType: 'html'
			};
			
			//$("#quote_form").ajaxForm(options);
			
			$("#quote_form").submit(function()
			{
				$("#quote_form").ajaxSubmit(options);
				return false;
			});
		}
		else
		{
			e.preventDefault();
		}		
	});
	
	/*----Begin the dynamic checking of form fields via event listeners----*/
	
	//change the background color of each form field when focused
	$("#quote_form input").each(function(i)
	{
		$(this).focus(function()
		{
			$(this).removeClass("idle_field").addClass("focused_field");
		});
		$(this).blur(function()
		{
			$(this).removeClass("focused_field").addClass("idle_field");
		});	
	});
	
	$("#selected_item_to_customize").blur(function()
	{
		if($("#selected_item_to_customize").val() == "")
		{
			if($("#selected_item_to_customize").next().is(".form_error_msg"))
			{
				if(!$("#selected_item_to_customize").next().is(":visible"))
				{
					$("#selected_item_to_customize").next().show("slide");
					$("#selected_item_to_customize").addClass("error_field");
				}
			}
			else
			{
				$("#selected_item_to_customize").after("<span class='form_error_msg'>Please enter an item for customization</span>");
				$("#selected_item_to_customize").next().hide();
				$("#selected_item_to_customize").next().show("slide");
				$("#selected_item_to_customize").addClass("error_field");
			}
		}
		else
		{
			if($("#selected_item_to_customize").next().is(".form_error_msg"))
			{
				$("#selected_item_to_customize").next().hide("slide");
				$("#selected_item_to_customize").removeClass("error_field");
			}			
		}
	});
	
	//check quantity select field for a blank value when focus is lost
	$("#quantity_select").blur(function()
	{
		if($("#quantity_select").val() == "" || $("#quantity_select").val() < 6 || colorCharTest.test($("#quantity_select").val()))
		{
			if($("#quantity_select").next().is(".form_error_msg"))
			{
				if(!$("#quantity_select").next().is(":visible"))
				{
					$("#quantity_select").next().show("slide");
					$("#quantity_select").addClass("error_field");
				}
			}
			else
			{
				$("#quantity_select").after("<span class='form_error_msg'>Please enter a valid quantity (min. 6)</span>");
				$("#quantity_select").next().hide();
				$("#quantity_select").next().show("slide");
				$("#quantity_select").addClass("error_field");
			}
		}
		else
		{
			if($("#quantity_select").next().is(".form_error_msg") && $("#quantity_select").next().is(":visible"))
			{
				$("#quantity_select").next().hide("slide");
				$("#quantity_select").removeClass("error_field");
			}			
		}
	});
	
	//check if the user has selected a proper type
	$("#type_select").change(function()
	{
		if($("#type_select").val().toLowerCase() != "(select one)")
		{
			if($("#type_select").next().is(".form_error_msg"))
			{
				if($("#type_select").next().is(":visible"))
				{
					$("#type_select").next().hide("slide");
					$("#type_select").removeClass("error_field");
				}				
			}
		}
		else
		{
			if($("#type_select").next().is(".form_error_msg"))
			{
				if(!$("#type_select").next().is(":visible"))
				{
					$("#type_select").next().show("slide");
					$("#type_select").addClass("error_field");
				}				
			}
			else
			{
				$("#type_select").after("<span class='form_error_msg'>Please select a type from the list</span>");
				$("#type_select").next().hide();
				$("#type_select").next().show("slide");
				$("#type_select").addClass("error_field");
			}
		}
	});
	
	$("#garment_color_choice").change(function()
	{
		if($("#garment_color_choice").val().toLowerCase() != "(select one)")
		{
			if($("#garment_color_choice").next().is(".form_error_msg"))
			{
				if($("#garment_color_choice").next().is(":visible"))
				{
					$("#garment_color_choice").next().hide("slide");
					$("#garment_color_choice").removeClass("error_field");
				}				
			}
		}
		else
		{
			if($("#garment_color_choice").next().is(".form_error_msg"))
			{
				if(!$("#garment_color_choice").next().is(":visible"))
				{
					$("#garment_color_choice").next().show("slide");
					$("#garment_color_choice").addClass("error_field");
				}				
			}
			else
			{
				$("#garment_color_choice").after("<span class='form_error_msg'>Please select a choice from the list</span>");
				$("#garment_color_choice").next().hide();
				$("#garment_color_choice").next().show("slide");
				$("#garment_color_choice").addClass("error_field");
			}
		}
	});
	
	$("#shipping_zip").blur(function()
	{
		var shipping_zip_entered = $("#shipping_zip").val();
		if($("#shipping_zip").val() != "" && zipCodePattern.test(shipping_zip_entered))
		{
			if($("#shipping_zip").next().is(".form_error_msg"))
			{
				if($("#shipping_zip").next().is(":visible"))
				{
					$("#shipping_zip").next().hide("slide");
					$("#shipping_zip").removeClass("error_field");
				}				
			}
		}
		else
		{
			if($("#shipping_zip").next().is(".form_error_msg"))
			{
				if(!$("#shipping_zip").next().is(":visible"))
				{
					$("#shipping_zip").next().show("slide");
					$("#shipping_zip").addClass("error_field");
				}				
			}
			else
			{
				$("#shipping_zip").after("<span class='form_error_msg'>Please enter your shipping zip code</span>");
				$("#shipping_zip").next().hide();
				$("#shipping_zip").next().show("slide");
				$("#shipping_zip").addClass("error_field");
			}
		}
	});
	
	
	$("#customer_name").blur(function()
	{
		var nameString = $("#customer_name").val();
		if($("#customer_name").val() == "" || $("#customer_name").val().length < 3 || nameDigitTest.test(nameString))
		{
			if($("#customer_name").next().is(".form_error_msg"))
			{
				if(!$("#customer_name").next().is(":visible"))
				{
					$("#customer_name").next().show("slide");
					$("#customer_name").addClass("error_field");
				}
			}
			else
			{
				$("#customer_name").after("<span class='form_error_msg'>Min. 3 characters, cannot contain numbers</span>");
				$("#customer_name").next().hide();
				$("#customer_name").next().show("slide");
				$("#customer_name").addClass("error_field");
			}
		}		
		else
		{
			if($("#customer_name").next().is(".form_error_msg"))
			{
				if($("#customer_name").next().is(":visible"))
				{
					$("#customer_name").next().hide("slide");
					$("#customer_name").removeClass("error_field");
				}							
			}
			else
			{
				$("#customer_name").removeClass("error_field");
			}
		}
	});
	
	$("#customer_email").blur(function()
	{
		var emailText = $("#customer_email").val();
		if($("#customer_email").val() == "" || !emailReg.test(emailText))
		{
			if($("#customer_email").next().is(".form_error_msg"))
			{
				if(!$("#customer_email").next().is(":visible"))
				{
					$("#customer_email").next().show("slide");
					$("#customer_email").addClass("error_field");
				}				
			}
			else
			{
				$("#customer_email").after("<span class='form_error_msg'>Please enter a valid email address</span>");
				$("#customer_email").next().hide();
				$("#customer_email").next().show("slide");
				$("#customer_email").addClass("error_field");
			}
		}
		else
		{
			if($("#customer_email").next().is(".form_error_msg"))
			{
				if($("#customer_email").next().is(":visible"))
				{
					$("#customer_email").next().hide("slide");
					$("#customer_email").removeClass("error_field");
				}				
			}
			else
			{
				$("#customer_email").removeClass("error_field");
			}
		}
	});
	
	$("#type_select").change(function()
	{
		if($("#type_select").val() == "Other")
		{
			$(".indent_line").css("display", "block");
		}
		else
		{
			if($(".indent_line").next().is(".form_error_msg"))
			{
				$(".indent_line").next().hide();
			}
			$(".indent_line").css("display", "none");
		}
	});
	
	$("#other_cust_type").keyup(function()
	{
		if($("#other_cust_type").is(":visible") && $("#other_cust_type").val() != "" && !nameDigitTest.test($(this).val()) && $("#other_cust_type").val().length > 2)
		{
			if($("#other_cust_type").next().is(".form_error_msg"))
			{
				$("#other_cust_type").next().hide();
				$("#other_cust_type").removeClass("error_field");
			}
		}
		else
		{
			if($("#other_cust_type").next().is(".form_error_msg"))
			{
				$("#other_cust_type").next().show();
				$("#other_cust_type").addClass("error_field");
			}
			else
			{
				$("#other_cust_type").after("<span class='form_error_msg'>Please enter a valid type</span>");
				$("#other_cust_type").addClass("error_field");
			}
		}
	});
	
	$("#other_cust_type").blur(function()
	{
		if($("#other_cust_type").val() == "")
		{
			if($("#other_cust_type").next().is(".form_error_msg"))
			{
				if(!$("#other_cust_type").next().is(":visible"))
				{
					$("#other_cust_type").next().show("slide");
					$("#other_cust_type").addClass("error_field");
				}
			}
			else
			{
				$("#other_cust_type").after("<span class='form_error_msg'>Please enter a valid type</span>");
				$("#other_cust_type").next().hide();
				$("#other_cust_type").next().show("slide");
				$("#other_cust_type").addClass("error_field");
			}
		}
		else
		{
			$("#other_cust_type").removeClass("error_field");
		}
	});
	
	$(".check_digit input").each(function(i)
	{
		$(this).blur(function()
		{
			if($(this).val() > 8 || colorCharTest.test($(this).val()))
			{
				if($(this).next().is(".form_error_msg"))
				{
					if(!$(this).next().is(":visible"))
					{
						$(this).next().show("slide");
						$(this).addClass("error_field");
					}
				}
				else
				{
					$(this).after("<span class='form_error_msg'>Please enter a valid quantity (0 - 8)</span>");
					$(this).next().hide();
					$(this).next().show("slide");
					$(this).addClass("error_field");
				}
			}
			else if($(this).val() == "")
			{
				$(this).val("0");
				if($(this).next().is(".form_error_msg"))
				{
					if($(this).next().is(":visible"))
					{
						$(this).next().hide("slide");
						$(this).removeClass("error_field");
					}					
				}
				else
				{
					$(this).removeClass("error_field");
				}
			}
			else
			{
				if($(this).next().is(".form_error_msg"))
				{
					if($(this).next().is(":visible"))
					{
						$(this).next().hide("slide");
						$(this).removeClass("error_field");
					}
				}
				else
				{
					$(this).removeClass("error_field");
				}
			}
		});
	});
	
	/*----End of dynamic checking of form fields----*/
	
	function displayResponse(responseText, statusText, xhr, $form)
	{
		$("#quote_form_outer_div").slideUp("normal", function()
		{
			$("#quote_form_outer_div").before("<h1>Success</h1><br /><p>Your quote request has been sent.</p>");
			$("#tab_quote img").hide();
			//$("#quote_form").hide();
		});
	}	
});

