$().ready(
    function() {
        addAttributes();
        setErrorMsg();
        //$('#regPhone').autotab({ target: 'regPhone', format: 'phone' });
        $('#regPhone2').autotab({ target: 'regPhone', format: 'phone' });

        var phoneMask = $("input:#phoneMask").val();
        $.mask.definitions['m']='[237]';
        $.mask.definitions['o']='[0]';
        $("#regPhone").mask(phoneMask,{placeholder:"_"});

        builderRegistrationFormValidator();

       $("#buBuilderAddressCityHQ").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,				
                cacheLength:1,
				max:50
            }
        ).flushCache();

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

        });

        $("#buBuilderAddressZipHQ").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+' '+item.buCityName);
                },
                mustMatch: false,
				max:50
            }
        );

        $('#buBuilderAddressZipHQ').result(function(event, data, formatted) {
            if (data) {
                $('#buBuilderAddressCityHQ').val(data.buCityName);
            }
        });

        $('#regName').blur(function(){
            $.ajax({
                url: '/buFront/ajax/check-name/format/json/',
                type: 'POST',
                dataType: 'json',
                data: 'name='+$('#regName').val(),
                success: function(data) {
                    var list = '';
                    var i = 0;

                    while (data['addressList'][i]) {
                        list += '<p>- '+data['addressList'][i]['zip']+' '+data['addressList'][i]['city']+' '+data['addressList'][i]['street']+' '+data['addressList'][i]['house']+'</p>';
                        i++;
                    }

                    if (list != '') {
                        $('#builderRegistrationErrorText').html(list);
                        $('#errorBox').show();
			$('#regName').change( function(){
				$('#errorBox').hide();
			});
			
			
			
                    }
                }
            });
        });

        $('#errorBoxClose').click(function(){
            $('#errorBox').hide();
        });

        address1 = new addressModule({lat: config.lat, lng: config.lng, countryCode: config.countryCode, position: '#buBuilderAddressZipHQ', zip: '#buBuilderAddressZipHQ', city: '#buBuilderAddressCityHQ', street: '#buBuilderAddressStreetHQ', house: '#buBuilderAddressHouseHQ', geo : '#buBuilderAddressGeoHQ', pointAcceptText: config.pointAccept, mapCloseText: config.mapClose, addressNotFoundText: config.addressNotFound, geoCodeNotFoundText: config.geoCodeNotFound});

        $('#errorBoxClose').click(function(){
            $('#errorBox').hide();
        });

	
	
		$("#smsInfo").mouseover( function(){
		$("#reklam").hide("fast");
		$("#helperBox1").css("margin-top","500px");
		$("#helperBox1").show("fast");
		
	});
	$("#smsInfo").mouseout( function(){
		$("#helperBox1").hide("fast");
		$("#reklam").show("fast");
	});

	$("#infoInfo").mouseover( function(){
		$("#reklam").hide("fast");
		$("#helperBox2").css("margin-top","400px");
		$("#helperBox2").show("fast");
	});
	$("#infoInfo").mouseout( function(){
		$("#helperBox2").hide("fast");
		$("#reklam").show("fast");
	});

    }
);

function builderRegistrationFormValidator() {
    var validator = $("#builderRegistrationForm").validate( {
        rules: {
            regName: {
                required: true
            },
            regPhone: {
                required: true,
                minlength: 8,
                remote: {
                    url: "/buFront/ajax/check-phone-aviable/format/json/",
                    type: "post",
                    dataType: "json"
                }
            },
            regPhone2: {
                minlength: 8
            },
            regEmial: {
                required: true,
                email: true,
                remote: {
                    url: "/buFront/ajax/check-email-aviable/format/json/",
                    type: "post",
                    dataType: "json"
                }
            },
            regPassword: {
                required: true,
                minlength: 5
            },
            regPassword2: {
                required: true,
                equalTo: "#regPassword"
            },
            buBuilderEmail2: {
                required: false,
                email: true
            },

            buBuilderAddressZipHQ: "required",
            buBuilderAddressCityHQ: {
                required: true,
                remote: {
                    url: "/buFront/ajax/check-builder-city/format/json/",
                    type: "post",
                    dataType: "json"
                }
            }
        },
        messages: {
            regName: {
                required: $("#regName").attr('errorText')
            },
            regPhone: {
                required: $("#regPhone").attr('errorText'),
                minlength: jQuery.format($("#regPhone").attr('errorText3')),
                remote: $("#regPhone").attr('errorText2')
            },
            regPhone2: {
                minlength: jQuery.format($("#regPhone").attr('errorText3'))
            },
            regEmial: {
                required: $("#regEmial").attr('errorText'),
                remote: $("#regEmial").attr('errorText2')
            },
            regPassword: {
                required: $("#regPassword").attr('errorText'),
                minlength: jQuery.format($("#regPassword").attr('errorText2'))
            },
            regPassword2: {
                required: $("#regPassword2").attr('errorText'),
                equalTo: $("#regPassword2").attr('errorText2')
            },

            buBuilderAddressZipHQ: $("#buBuilderAddressZipHQ").attr('errorText'),
            buBuilderAddressCityHQ: $("#buBuilderAddressCityHQ").attr('errorText')
        },
        /*errorPlacement: function(error,element) {},
        errorContainer: '#errorBox',
        showErrors: function(errorMap, errorList) {
            var i = 0;
            if ((validator.numberOfInvalids() > 1) && (errorList[1])) {
                $("#builderRegistrationErrorText").html('');
            }
            while (errorList[i]) {
                if (errorList[1]) {
                    $("#builderRegistrationErrorText").append("<p>" + errorList[i]['message'] + "</p>");
                } else {
                    if (validator.numberOfInvalids() == 1) {
                        $("#builderRegistrationErrorText").html('');
                        $("#builderRegistrationErrorText").append("<p>" + errorList[i]['message'] + "</p>");
                    }
                }
                i++;
            }
            this.defaultShowErrors();
        },*/
        submitHandler: function(form) {
        	pageTracker._trackPageview('/szaki-regisztracio-track'); 
        	form.submit();
        },
        errorPlacement: function(error, element) {
            error.appendTo( element.parent().next() );
        },
        errorClass: "regInputError",
        validClass: "regInput"
    });
}

function addAttributes()
{
    setLongdesc();
    $('.registrationInfoBox input.regInput').blur(function(e){
        $('#helperBox').hide();
    });
   /* $('.registrationInfoBox input.regInput').focus(function(e){
       $('#helperBox').css(       {
           'position': 'absolute',
           'top': $(this).elementLocation().y
       });
       $('#helperBox').show();
       $('#helperContent').html(($(this).attr('longdesc'))?($(this).attr('longdesc')):(''));
    });*/
}
