(function($) {
	$.fn.rform = function(settings) {
	
		var form = this;
		
		$(form).bind("submit",function(){
			var d = $(form).serialize();
			
			var error = false;
			$('input[name="firstname"]',form).each(function(e){
				if(this.value == this.defaultValue){
					error = true;
				}
			});
			$('input[name="lastname"]',form).each(function(e){
				if(this.value == this.defaultValue){
					error = true;
				}
			});
			$('input[name="boxcutter"]',form).each(function(e){
				if(this.value == this.defaultValue){
					error = true;
				}
			});
			
			if(!error){			
				$('#submit-btn',form).attr({'disabled':'disabled'}).fadeTo('fast',0.5);
				$.ajax({
					url: $(form).attr('action'),
					type: "POST",
					data: d,
					success: function(info){
						if($.trim(info)==""){
							alert("Thank you for filling out Perkasie Container's Request A Quote form!");
							document.requestform.reset();
							$('input,textarea',form).each(function(){
								$(this).addClass('idleField').removeClass('focusField');
							});
							$('#requesttable ul li',form).each(function(){
								_deleteItem($(this));
							});
						} else {
							alert(info);
						}
						$('#submit-btn',form).removeAttr('disabled').fadeTo('fast',1.0);
					}
				});
			} else {
				alert("Please fill out all required fields!");
			}
			
			return false;
		});
		
		$('.add-btn',form).bind('click',function(){
			_addToQueue();
			return false;
		});
		
		function _addToQueue(){
			var quantity = $("#form-right input[name='quantity']",form).val();
			if(!isNaN(parseFloat(quantity))){
			
				var style = $("#form-right select[name='style']",form).val();
				
				if(style==0){
				
					alert('Please choose a style!');
					
				} else {
			
					if($('#requesttable ul',form).hasClass('empty')){
						$('#requesttable ul',form).removeClass('empty');
						$('#requesttable ul',form).empty();
					}					
					
					var boxlength = $("#form-right input[name='boxlength']",form).val();
					var boxwidth = $("#form-right input[name='boxwidth']",form).val();
					var boxdepth = $("#form-right input[name='boxdepth']",form).val();
					var joint = $("#form-right select[name='joint']",form).val();
					var test = $("#form-right select[name='test']",form).val();
					var flute = $("#form-right select[name='flute']",form).val();
					var specialstock = $("#form-right select[name='specialstock']",form).val();
					var colors = $("#form-right select[name='colors']",form).val();
					var panels = $("#form-right select[name='panels']",form).val();
					
					var li = $('<li>').prependTo('#requesttable ul',form);
					var item = $('<div>').prependTo(li).addClass('item').append('<span>'+quantity+'</span>'+style);
					var actions = $('<div>').appendTo(li).addClass('actions');
						var deletebtn = $('<a>').appendTo(actions).addClass('delete-btn').attr({'href':'#'}).text("Delete").bind('click',{li:li},function(event){
							_deleteItem(event.data.li);
							return false;
						});
					var clear = $(li).append('<div class="clear">&nbsp;</div>');
					
					var id = _getID();
					
					var quantity_h = $('<input type="hidden" />').appendTo(li).attr({'name':'quantity['+id+']','value':quantity});
					var length_h = $('<input type="hidden" />').appendTo(li).attr({'name':'boxlength['+id+']','value':boxlength});
					var width_h = $('<input type="hidden" />').appendTo(li).attr({'name':'boxwidth['+id+']','value':boxwidth});
					var depth_h = $('<input type="hidden" />').appendTo(li).attr({'name':'boxdepth['+id+']','value':boxdepth});
					var style_h = $('<input type="hidden" />').appendTo(li).attr({'name':'style['+id+']','value':style});
					var joint_h = $('<input type="hidden" />').appendTo(li).attr({'name':'joint['+id+']','value':joint});
					var test_h = $('<input type="hidden" />').appendTo(li).attr({'name':'test['+id+']','value':test});
					var flute_h = $('<input type="hidden" />').appendTo(li).attr({'name':'flute['+id+']','value':flute});
					var specialstock_h = $('<input type="hidden" />').appendTo(li).attr({'name':'specialstock['+id+']','value':specialstock});
					var colors_h = $('<input type="hidden" />').appendTo(li).attr({'name':'colors['+id+']','value':colors});
					var panels_h = $('<input type="hidden" />').appendTo(li).attr({'name':'panels['+id+']','value':panels});
				
				}
				
			} else {
				alert('Quantity must be a number!');
			}			
		}
		
		function _getID(){
			var lis = $('#requesttable ul li',form);
			var id = $(lis).length;
			return id-1;
		}
		
		function _deleteItem(obj){
			$(obj).remove();
			if(_getID()<=-1){
				$('#requesttable ul',form).addClass('empty');
				var li = $('<li>').prependTo('#requesttable ul',form).text("No Items Added");
			}
			var id = _getID();
			$('#requesttable ul li',form).each(function(i){
				$('input[name^="quantity"]',this).attr({'name':'quantity['+(id-i)+']'});
				$('input[name^="style"]',this).attr({'name':'style['+(id-i)+']'});
				$('input[name^="joint"]',this).attr({'name':'joint['+(id-i)+']'});
				$('input[name^="test"]',this).attr({'name':'test['+(id-i)+']'});
				$('input[name^="flute"]',this).attr({'name':'flute['+(id-i)+']'});
				$('input[name^="specialstock"]',this).attr({'name':'specialstock['+(id-i)+']'});
				$('input[name^="colors"]',this).attr({'name':'colors['+(id-i)+']'});
				$('input[name^="panels"]',this).attr({'name':'panels['+(id-i)+']'});
			});
		}

	};
})(jQuery);