$().ready(
    function() {
    	setErrorMsg();
    	
    	$('#shShopPhone').autotab({ target: 'shShopPhone', format: 'phone' });    	
    	
    	editShopFormValidator();
    	
    	$("#shShopAddressCity").autocomplete('/buFront/ajax/list-city/format/json/',
    	        {
    	            dataType: "json",
    	            parse: function(data) {
    	                return $.map(data.cityList, function(row) {
    	                    return {
    	                        data: row,
    	                        value: row.buCityName,
    	                        result: row.buCityName
    	                    } ;
    	                });
    	            },
    	            formatItem: function(item) {
    	                return (item.buCityZip+' '+item.buCityName);
    	            },
    	            mustMatch: true
    	        }
    	    );

    	    $('#shShopAddressCity').result(function(event, data, formatted) {
    	        if (data) {
    	            if ($('#shShopAddressZip').val() == '') {
    	                $('#shShopAddressZip').val(data.buCityZip);
    	            }
    	        }
    	    });

    	    $("#shShopAddressZip").autocomplete('/buFront/ajax/list-zip/format/json/',
    	        {
    	            dataType: "json",
    	            parse: function(data) {
    	                return $.map(data.zipList, function(row) {
    	                    return {
    	                        data: row,
    	                        value: row.buCityZip,
    	                        result: row.buCityZip
    	                    } ;
    	                });
    	            },
    	            formatItem: function(item) {
    	                return (item.buCityZip);
    	            },
    	            mustMatch: false
    	        }
    	    );

    	    $('#shShopAddressZip').result(function(event, data, formatted) {
    	        if (data) {
    	            $('#shShopAddressCity').val(data.buCityName);
    	        }
    	    });
    	    
    	    address1 = new addressModule({lat: config.lat, lng: config.lng, countryCode: config.countryCode, position: '#shShopAddressCity', zip: '#shShopAddressZip', city: '#shShopAddressCity', street: '#shShopAddressStreet', house: '#shShopAddressHouse', geo : '#shShopAddressGeocode', pointAcceptText: config.pointAccept, mapCloseText: config.mapClose, addressNotFoundText: config.addressNotFound, geoCodeNotFoundText: config.geoCodeNotFound});
    }
);

function editShopFormValidator(){
    var validator = $("#shopRegistrationForm").validate({
        rules: {
    		shShopName: "required",                    
            shShopEmail: {
                required: false,
                email: true
            },

            shShopPhone: "required",
            description: "required",
            uploadFile1: "required",
            
            shShopAddressZip: "required",
            shShopAddressCity: {
                required: true,
                remote: {
                    url: "/buFront/ajax/check-builder-city/format/json/",
                    type: "post",
                    dataType: "json"
                }
            },
            shShopAddressStreet: "required",
            shShopAddressHouse: "required"
        },
        messages: {
        	shShopName: {
                required: $("#shShopName").attr('errorText')
            },
            shShopEmail:{
            	email: $("#shShopEmail").attr('errorText')                
            },          
            
            shShopPhone: $("#shShopPhone").attr('errorText'), 
            description: $("#description").attr('errorText'), 
            uploadFile1: $("#uploadFile1").attr('errorText'), 
            
            shShopAddressZip: $("#shShopAddressZip").attr('errorText'),            
            shShopAddressCity:{
	            required: $("#shShopAddressCity").attr('errorText'),
	            remote: jQuery.format($("#shShopAddressCity").attr('errorText2'))
            },
            shShopAddressStreet: $("#shShopAddressStreet").attr('errorText'),
            shShopAddressHouse: $("#shShopAddressHouse").attr('errorText')
        },
        submitHandler: function(form) {
        	pageTracker._trackPageview('/shop-registration-track'); 
        	form.submit();
        },
        errorPlacement: function(error, element) {
            error.appendTo( element.parent().next() );
        },
        errorClass: "regInputError",
        validClass: "regInput"
    });
}
