$(document).ready(function(){
	$("#wine_list_table #add_more_wine").click(function(){
		var rowCount = $('#wine_list_table tr').length;
		var newID = rowCount - 1;

		if(newID <= 8)
		{
			$("#wine_list_table #add_wine_row").before('\n<tr>\n<td><input name="Qty ' + newID + '" type="text" id="Qty ' + newID + '" style="width:55px;" /></td>\n<td><input name="Year ' + newID + '" type="text" id="Year ' + newID + '" style="width:55px;" /></td>\n<td><input name="Wine ' + newID + '" type="text" id="Wine ' + newID + '" style="width:195px;" /></td>\n</tr>');
			$("#wine_list_table #wine_count").val(newID);
		}
		
		if(newID == 8)
		{
			$("#wine_list_table #add_wine_row").hide();
		}
	});
	
	$("#merch_list_table #add_more_merch").click(function(){
		var rowCount = $('#merch_list_table tr').length;
		var newID = rowCount - 2;

		if(newID <= 8)
		{
			$("#merch_list_table #add_merch_row").before('\n<tr>\n<td><input style="width:40px;" name="Merch Qty ' + newID + '" id="Merch Qty ' + newID + '" type="text" /></td>\n<td><input style="width:270px;" name="Merch Desc ' + newID + '" id="Merch Desc ' + newID + '" type="text" /></td>\n</tr>');
			$("#merch_list_table #merch_count").val(newID);
		}
		
		if(newID >= 8)
		{
			$("#merch_list_table #add_merch_row").hide();
		}
	});
	
	$("#wine_list_table2 #add_more_wine").click(function(){
		var rowCount = $('#wine_list_table2 tr').length;
		var newID = rowCount - 2;
    var tabIndex = (newID * 10)

		if(newID <= 8)
		{
			$("#wine_list_table2 #add_wine_row").before('\n<tr>\n<td><input tabIndex=' + tabIndex + ' class="sField" name="Qty ' + newID + '" type="text" id="Qty ' + newID + '" style="width:40px;" /></td>\n<td><input tabindex=' + (tabIndex + 1) + ' class="sField" name="Wine ' + newID + '" type="text" id="Wine ' + newID + '" style="width:250px;" /></td>\n</tr>');
			$("#wine_list_table2 #wine_count").val(newID);
		}
		
		if(newID == 8)
		{
			$("#wine_list_table2 #add_wine_row").hide();
		}


    for (var i=1; i<=newID; i++) {
    	var qty = document.getElementById("Qty" + i)
			if (qty == null)
					qty = document.getElementById("Qty " + i);
			if (qty != null && qty.value == '') {
				qty.focus();
				break;
			}
		}
	});
	
	$("#form_send #button_ship_wine_submit").click(function(){
		$('#form_send').submit();
		return false;
	});
	
	$("#merch_list_table2 #add_more_merch").click(function(){
		var rowCount = $('#merch_list_table2 tr').length;
		var newID = rowCount - 2;

		if(newID <= 8)
		{
			$("#merch_list_table2 #add_merch_row").before('\n<tr>\n<td><input class="sField" style="width:40px;" name="Merch Qty ' + newID + '" id="Merch Qty ' + newID + '" type="text" /></td>\n<td><input class="sField" style="width:270px;" name="Merch Desc ' + newID + '" id="Merch Desc ' + newID + '" type="text" /></td>\n</tr>');
			$("#merch_list_table2 #merch_count").val(newID);
		}
		
		if(newID >= 8)
		{
			$("#merch_list_table2 #add_merch_row").hide();
		}
	});
	
	$("form.ups_address_fill #zip").blur(function(){
		var i = 0;
		var getData = {};

		getData["zip"]	= $(this).val();
		
		$.ajax({
			type:		"GET",
			url:		"/dashboard/upsaddress/",
			data:		(getData),
			async:		false,
			success:	function(xml){
							$("form.ups_address_fill #city")
								.find('option')
								.remove()
								.end()
							;
							var selCity	= "";
							$(xml).find("element").each(function(){
								var dataCity	= $(this).find("city").text();
								var dataState	= $(this).find("state").text();
								
								if(selCity == "") selCity = dataCity;
								
								$("form.ups_address_fill #city").val(dataCity);
								$("form.ups_address_fill #state").val(dataState);
								
								$("form.ups_address_fill #city").append('<option value="' + dataCity + '">' + dataCity + '</option>');
							});

							$("form.ups_address_fill #city").val(selCity);
						}
		});
		return false;
	});
});

