$(document).ready ( function(){

	// required on load so greyed out class is removed if input error occurs.
	if ($('#existingCustomer').is(':checked')) {

		$('#existingRetailer').removeAttr('disabled');
		$('#existingRetailer').removeClass('greyout');
	} else {

		$('#existingRetailer').attr('disabled', 'disabled');
		$('#existingRetailer').attr('value', '');
		$('#existingRetailer').addClass('greyout');
	}

    $('a[rel*=facebox]').facebox();

    if ($('#existingCustomer').is(':checked')) {

            $('#associatedRetailerName').removeAttr('disabled');
			$('#associatedRetailerName').removeClass('greyout');
        } else {

            $('#associatedRetailerName').attr('disabled', 'disabled');
            $('#associatedRetailerName').attr('value', '');
			$('#associatedRetailerName').addClass('greyout');
        }

    if(!$('#existingCustomer').is(':checked')){
        $("#associatedRetailerName").attr('disabled', 'disabled');
    }

    $("a#findAddress").click(function (e) {

        e.preventDefault();
        findAddress();
        return false;
    });

    $("#existingCustomer").click(function(){

        if ($('#existingCustomer').is(':checked')) {

            $('#associatedRetailerName').removeAttr('disabled');
			$('#associatedRetailerName').removeClass('greyout');
        } else {

            $('#associatedRetailerName').attr('disabled', 'disabled');
            $('#associatedRetailerName').attr('value', '');
			$('#associatedRetailerName').addClass('greyout');
        }
    });

    $("#associatedRetailerName").autocomplete("/retailer/find",{
        dataType:"json",
        autoFill:true,
        cacheLength:10,
        matchContains:true,
        parse: function(data) {
            var rows = new Array();
            for(var i=0; i<data.length; i++){
                rows[i] = {
                    data:data[i],
                    value:data[i].dealerName,
                    result:data[i].dealerName
                };
            }
            return rows;
        },
        formatItem: function(row, i, n) {
            return row.dealerName;
        }
    });


    $(':submit[name=_eventId_confirmPrizeDrawEntry]').live('click', function(event){
        
        $(':hidden#soldOutDealerNumber').attr('value', $(this).attr('id'))
    });

    $("select#currentCarManufacturer").change(function(){
        $.getJSON("/carManufacturerModels/returnModelsForManufacturer",{
            id: $(this).attr('value'),
            ajax: 'true'
        }, function(j){
            var options = '<option value="">Please Choose</option>';
            for (var i = 0; i < j.length; i++) {
                options += '<option value="' + j[i].name + '">' + j[i].name + '</option>';
            }
            $("select#currentCarModel").html(options);
            $("select#currentCarModel").removeAttr('disabled');

        })
    })


});


function findAddress() {

    var postcode = $("input#postcode").val().replace(/ /g, '');

    $.get("/postcode/find", {
        postcode: postcode
    }, function(xml) {

        var street = $(xml).find('street').text();

        if (street.length > 0) {

            // Clear error message area
            $("#findAddressError").html("")
            $("input#street1").val(street);
            var street2 = $(xml).find('street2').text();
            $("input#street2").val(street2);
            var city = $(xml).find('city').text();
            $("input#city").val(city);
            //var county = $(xml).find('county').text();
            //$("input#county").val(county);
            var postcode = $(xml).find('postcode').text();
            $("input#postcode").val(postcode);

        } else {

            var postcode = $("input#postcode").val()

            if (postcode) {
                $("#findAddressError").html("Postcode not found");
            } else {
                $("#findAddressError").html("Please enter a postcode");
            }
        }
    });

}