$(function() {
    if ($('#order_form').length) {
	
	// Ensure javascript
	$('.enable-javascript').hide();
	$('#order_form').show();
	
	/***********************************************
		* Drop Down Date select script- by JavaScriptKit.com
		* This notice MUST stay intact for use
		* Visit JavaScript Kit at http://www.javascriptkit.com/ for this script and more
		***********************************************/

	var monthtext = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec'];

	function populateDropDown(dayfield, monthfield, yearfield) {
	    var today = new Date();
	    var dayfield = document.getElementById(dayfield);
	    var monthfield = document.getElementById(monthfield);
	    var yearfield = document.getElementById(yearfield);
	    
	    for (var i = 0; i<31; i++)
		dayfield.options[i] = new Option(i+1, i+1);
	    
	    dayfield.options[today.getDate()] = new Option(today.getDate(), today.getDate(), true, true); //select today's day
	    
	    for (var m = 0; m<12; m++)
		monthfield.options[m] = new Option(monthtext[m], monthtext[m]);
	    
	    monthfield.options[today.getMonth()] = new Option(monthtext[today.getMonth()], monthtext[today.getMonth()], true, true); //select today's month
	    var thisyear = today.getFullYear();
	    
	    for (var y = 0; y < 20; y++) {
		yearfield.options[y] = new Option(thisyear, thisyear);
		thisyear += 1;
	    }
	    yearfield.options[0] = new Option(today.getFullYear(), today.getFullYear(), true, true); //select today's year
	}
	
	populateDropDown('arrival_day', 'arrival_month', 'arrival_year');
	
	$('#brand_select').change(function() {
	    var brand = $('#brand_select option:selected').text();
	    var id = '#' + brand.replace(/\W/g, '') + "_select";
	    var num = $('#num_of_wines').val();

	    $("input[name='brand" + "_" + num + "']").val(brand);
	    $('.varietal_select').hide();
	    $('.vintage_select').hide();
	    
	    $(id).show().change(function() {
		var variety = $(id + " option:selected").text();
		var vintageID = '#' + variety.replace(/\W/g, '') + "_select";
		
		$("input[name='variety" + "_" + num + "']").val(variety);
		$('.vintage_select').hide();
		
		$(vintageID).show().change(function() {
		    var vintage = $(vintageID + " option:selected").text();
		    $("input[name='vintage" + "_" + num + "']").val(vintage);
		})
		    .change();
	    })
		.change();
	})
	    .change();
	
	$('#quantity').change(function() {
	    var num = $("#num_of_wines").val();
	    $("input[name='quantity" + "_" + num + "']").val($("#quantity").val());
	});

	function fillShipToFields() {
	    var shipToFields = $("#orderform-shipto input[type='text']");
	    var requestedByFields = $("#orderform-requestedby input[type='text']");

	    requestedByFields.each(function(i) {
		if (i < shipToFields.length)
		    $(shipToFields[i]).val($(this).val());
	    });
	}
	
	function clearShipToFields() {
	    $('#orderform-shipto input').each(function() {
		$(this).val("");
	    });
	}
	function addToAttr(elem, attr, num) {
	    if (!elem.attr(attr)) alert(attr + elem + num);
	    var a = elem.attr(attr).split("_")[0];
	    elem.attr(attr, a + "_" + num);
	}

	function modify(container, counter) {
	    var num = counter.val(parseInt(counter.val()) + 1).val();
	    //alert(num);
	    addToAttr(container, 'id', num);

	    container.children().each(function() {
		addToAttr($(this), 'id', num);
		addToAttr($(this), 'name', num);
	    });
	    return container;
	}
				
	$("#add-link").click(function() {
	    var num = $("#num_of_wines").val();
	    $("#your-wines").show().append("<li>" + $("input[name='brand_" + num + "']").val() + "</li>");
	    $("#order_form").append(modify($("#wine_" + num).clone(), $('#num_of_wines')));
	    $("#brand_select").change();
	    $("#quantity").change();
	});
	
	$("#use-different").click(function() {
	    clearShipToFields();
	    $('#orderform-shipto').show();
	});
	$("#use-same").click(function() {
	    fillShipToFields();
	    $('#orderform-shipto').hide();
	});
	
	$("#use-same").click();

	$("#orderform-requestedby input[type='text']").blur(function() {
	    if ($("#orderform-shipto").is(':hidden')) {
		fillShipToFields();
	    }
	});

	$("#order_form").validate({
	    
	    rules: {
		requested_by_name: "required",
		requested_by_address: "required",
		requested_by_city: "required",
		requested_by_state: "required",
		requested_by_zip: "required",
		ship_to_name: "required",
		ship_to_address: "required",
		ship_to_city: "required",
		ship_to_state: "required",
		ship_to_zip: "required",
		quantity: "required",
		email: {
		    required: true,
		    email: true
		}
	    }
	});
    }
});