﻿(function($) {
	$(function() {

		var url = $('#url').val();
		var closingJson = {};

		if (url) {
			$("#csz").autocsz(url).result(function(event, item) {
				$.extend(closingJson, item);
			});
		}

		$("#closingForm").submit(function() {
			if ($("#closingForm").valid()) {
			    if($('#company').val() == 'Company Name') {
				    $('#company').val('');
				}
				if($('#address').val() == 'Address') {
				    $('#address').val('');
				}
			}
		});

		$("#closingForm").validate({
			errorLabelContainer: "#error",
            errorClass: "error",
            errorPlacement: function(error, element) {
                var thisOne = element.nextAll(".fielderror:first");
                error.appendTo(thisOne);
            }
		});

		$.validator.addMethod("csz", function(value, element) {
			$("#error").text("");
			if (value == 'City/Town, State or ZIP' || value == '') {
				return 0;
			}
			return 1;

		}, 'Please enter City/Town, State or ZIP');

		$.validator.addMethod("requiredCoverageArea", function(value, element) {
			return value != '-1';
		}, 'Please select a Coverage Area');

		$.validator.addMethod("requiredAssociationType", function(value, element) {
			return value > 0;
		}, 'Please select an Association Type');

		$.validator.addMethod("requiredService", function(value, element) {
			return value > 0;

		}, 'Please select a service');

		$.validator.addMethod("closingServiceType", function(value, element) {
			return value > 0;

		}, 'Please select a service');

		$.validator.addMethod("serviceCategoryId", function(value, element) {
			return value > 0;

		}, 'Please select a service');

		//Set field actions
		$('#company').focus(function() {
			if ($(this).val() == 'Company Name') {
				$(this).val('');
			}
		})
		.blur(function() {
			if ($(this).val() == '') {
				$(this).val('Company Name');
			}
		});

		$('#address').focus(function() {
			if ($(this).val() == 'Address') {
				$(this).val('');
			}
		})
		.blur(function() {
			if ($(this).val() == '') {
				$(this).val('Address');
			}
		});

		$('#csz').focus(function() {
			if ($(this).val() == 'City/Town, State or ZIP') {
				$(this).val('');
			}
		})
		.blur(function() {
			if ($(this).val() == '') {
				$(this).val('City/Town, State or ZIP');
			}
		});

		if($('#csz').val() == ''){
			$('#csz').val('City/Town, State or ZIP')
		}

		$("#re-csz").autocsz(url);

	});
})(jQuery);