$(function(){
	// FAST QUOTE FORM 
	var formButtons = $(".addButtons div");
	formButtons.hover(
		function(){
			$(this).addClass("hover");
		},
		function(){
			$(this).removeClass("hover");
		}
	);
	
	setupClick();
	
	// create copy of template
		$(".addButtons .add").trigger('click');
	// hide the remove button on first visible quote form
		$(".quote:eq(1) .addButtons .remove").hide();
	
	// hide quote template
		$(".quote:first").hide();
	
	rfqType();
	
	$('textarea.expanding').autogrow({
		minHeight: 100,
		lineHeight: 16
	});

});

	
function setupClick() {
	var numberQuotes = $("#numQuotes").val()
	$(".addButtons div").click(function(){
		if ($(this).attr("class")=="add hover") {
			$(".quote:last").after($(".quote:first").clone(true).show());
			$(".quote:last .grayBox").empty();
			$(".quote:last .grayBox").html($(".quote:first .washerTable").clone(true));
		
			var getNum = $("#numQuotes").val();
			$("#numQuotes").val(parseInt(getNum)+1);
		}
		else if ($(this).attr("class")=="add") {
			$(".quote:last").after($(".quote:first").clone(true).show());
			$(".quote:last .grayBox").empty();
			$(".quote:last .grayBox").html($(".quote:first .washerTable").clone(true));
 
			if ($("#numQuotes").val() == ""){
				$("#numQuotes").val("1");
			}
		}
		else {
			$(this).parent().parent().remove();
			
			var getNum = $("#numQuotes").val();
			$("#numQuotes").val(parseInt(getNum)-1);
		}
	});
};


// SUBMIT
$(function(){
	// REMOVE "," AND "'" FROM FIELDS BEFORE SUBMISSION
	$("form:not(#fastQuote)").submit(function(){
		$("textarea, input").each(function(e){
			str = $(this).val();
			finalStr = str.replace(/,/g, "&#44;");
			finalStr = finalStr.replace(/'/g, "&rsquo;");
			$(this).val(finalStr);
		});
		//return false;
	});
	
	
	$("form:not(#fastQuote)").submit(function(){
		$(".required").each(function() {
			checkValidation($(this));
		});
		
		$("input:disabled").each(function() {
			$(this).parent().removeClass("error");
		});
		
		var errorFound = false;
		
		$("form .quote:first div").removeClass("error");
		
		$(".error").each(function() {
			errorFound = true;
		});
		
		if ( errorFound  ){
			alert("Fields with an * are required");
			return false;
		} else {
			$("input:disabled").each(function() {
				$(this).removeAttr("disabled").val(" ");
			});
			return true;
		};
	});
	
});





// FORM TYPE
function rfqType(){
	$("select[name='typOfRFQ']").change(function(){
		if ($(this).val() == "Washer"){
			$(this).parent().siblings(".grayBox").empty();
			$(this).parent().siblings(".grayBox").html($(".quote:first .washerTable").clone(true));
		} else {
			$(this).parent().siblings(".grayBox").empty();
			$(this).parent().siblings(".grayBox").html($(".quote:first .stampingTable").clone(true));
		};
	});
}	
