﻿/**
* @author Flavia Goncalves <flavia.goncalves@wunderman.com.br>
*/
jQuery(function() {
    WF._init();
});


var WF = {
    _init: function() {
        try {
            WF.gateway();
            WF.limpaCampo();
        }
        catch (e) {
            //console.log('Error: ' + e.description);
        }
    },

    limpaCampo: function() {
        $('.dtNascimento input').each(function() {
            $(this).focus(function() {
                jQuery.data(this, 'value', $(this).val());
                $(this).val('');
            });

            $(this).blur(function() {
                if ($(this).val() == '') {
                    $(this).val(jQuery.data(this, 'value'));
                }
            });
        });


    },
    gateway: function() {

        jQuery.validator.addMethod("isValidDate", function(value, element, params) {
            var day = $(params.day).val();
            var month = $(params.month).val();
            var year = $(params.year).val();

            if (day == '' || month == '' || year == '' || this.optional(element)) return true;

            year = parseInt(year, 10);
            month = parseInt(month, 10);
            day = parseInt(day, 10);

            var min_age = 18;
            var date = new Date(year, month - 1, day);
            var convertedDate = "" + date.getFullYear() + (date.getMonth() + 1) + date.getDate();
            var givenDate = "" + year + month + day;
            var resultDate = givenDate == convertedDate;

            return resultDate
        }, 'Por favor, forneça uma data de nascimento válida');


        jQuery.validator.addMethod("apartDate", function(value, element, params) {
            var day = $(params.day).val();
            var month = $(params.month).val();
            var year = $(params.year).val();

            if (day == '' || month == '' || year == '' || this.optional(element)) return false;

            year = parseInt(year, 10);
            month = parseInt(month, 10) - 1;
            day = parseInt(day, 10);

            var date = new Date(currentTime.getFullYear() - 18, currentTime.getMonth(), currentTime.getDate());

            var validDate = new Date(year, month, day);

            if (validDate.getFullYear() != year || validDate.getMonth() != month || validDate.getDate() != day) {
                return false;
            }

            var isValid = true;

            if (year > date.getFullYear())
                isValid = false;
            else if (year == date.getFullYear()) {
                if (month > date.getMonth())
                    isValid = false;
                else if (month == date.getMonth()) {
                    if (day > date.getDate())
                        isValid = false;
                }
            }

            return isValid;



        }, "Você precisa informar uma data válida para entrar no site.");



        $("#gateway").validate({
            submitHandler: function(form) {
                if (WF.WAIT) return;
                WF.WAIT = true;


                jQuery.post($(form).attr('action'), $(form).serialize(), function(response, textStatus) {
                    if (response.Success) {

                        if (window.location.href.split('?r=/')[1] != null && window.location.href.split('?r=/')[1].length != 0) {
                            window.location = window.location.href.split('?r=/')[1];
                        } else if (window.location.href.split('&r=/')[1] != null && window.location.href.split('&r=/')[1].length != 0) {
                            window.location = window.location.href.split('&r=/')[1];
                        } else {
                            window.location = '/home';
                        }
                    }
                    else {
                        $('div.uniqueError').text(response.Message);
                        $('div.uniqueError').show();
                        WF.WAIT = false;
                    }
                }, "json");

                return false;
            },
            highlight: function(element, errorClass) {
                $('div.uniqueError').show();
                $('div.erroMenor').hide();
                $('ul#errorscontainer').hide();

                var label = $(element).prevAll("label").get(0);
                if (label)
                    $(label).addClass(errorClass);
                else
                    $(element).addClass(errorClass);
            },
            unhighlight: function(element, errorClass) {
                var label = $(element).prevAll("label").get(0);
                if (label)
                    $(label).removeClass(errorClass);
                else
                    $(element).removeClass(errorClass);
            },
            errorLabelContainer: $("#errorscontainer"),
            errorElement: "li",

            rules: {
                country: 'required',
                day: {
                    required: true,
                    apartDate: { day: document.gateway.day, month: document.gateway.month, year: document.gateway.year }
                },
                month: {
                    required: true,
                    apartDate: { day: document.gateway.day, month: document.gateway.month, year: document.gateway.year }
                },
                year: {
                    required: true,
                    apartDate: { day: document.gateway.day, month: document.gateway.month, year: document.gateway.year }
                }
            },
            messages: {
                country: 'Por favor selecione o seu país',
                day: {
                    required: 'Preencha o dia do nascimento',
                    apartDate: 'O conteúdo deste site é restrito a maiores de 18 anos'
                },
                month: {
                    required: 'Preencha o mês do nascimento',
                    apartDate: 'O conteúdo deste site é restrito a maiores de 18 anos'
                },
                year: {
                    required: 'Preencha o ano do nascimento',
                    apartDate: 'O conteúdo deste site é restrito a maiores de 18 anos'
                }
            }
        });

        /* $("#gateway").validate({
        submitHandler: function(form) {
        if (WF.WAIT) return;
        WF.WAIT = true;


                jQuery.post($(form).attr('action'), $(form).serialize(), function(response, textStatus) {
        if (response.Success) {
        window.location = '/home';
        WF.WAIT = false;
        }
        else {
        $('div.uniqueError').text(response.Message);
        $('div.uniqueError').show();
        WF.validaIdade();
        WF.WAIT = false;
        }
        }, "json");

                return false;
        },
        highlight: function(element, errorClass) {
        $('div.uniqueError').show();
        $('div.erroMenor').hide();
        $('ul#errorscontainer').hide();

                var label = $(element).prevAll("label").get(0);
        if (label)
        $(label).addClass(errorClass);
        else
        $(element).addClass(errorClass);
        },
        unhighlight: function(element, errorClass) {
        var label = $(element).prevAll("label").get(0);
        if (label)
        $(label).removeClass(errorClass);
        else
        $(element).removeClass(errorClass);
        },
        errorLabelContainer: "#errorscontainer",
        wrapper: "li",

            rules: {
        country: 'required',
        day: {
        required: true,
        number: true
        },
        month: {
        required: true,
        number: true
        },
        year: {
        required: true,
        number: true
        }
        },
        messages: {
        country: 'Por favor selecione o seu país',
        day: 'Preencha o dia do nascimento',
        month: 'Preencha o mês do nascimento',
        year: 'Preencha o ano do nascimento'
        }

        });
        },

    validaIdade: function(step) {
        var isvalid = WF.isDate($(document.gateway.day).val(), $(document.gateway.month).val(), $(document.gateway.year).val());

        if (!step) {
        var pass = true;

            if (!isvalid) {
        $('div.uniqueError').hide();
        $('div.erroMenor').show();
        pass = false;
        }


            if (pass) {
        $("#gateway").submit();
        }
        }

        return isvalid;
        },

    isDate: function(day, month, year) {

        if (day == '' || month == '' || year == '') return false;

        year = parseInt(year, 10);
        month = parseInt(month, 10);
        day = parseInt(day, 10);
        var min_age = 18;
        var date = new Date(year, month - 1, day);
        var convertedDate = "" + date.getFullYear() + (date.getMonth() + 1) + date.getDate();
        var givenDate = "" + year + month + day;
        var resultDate = givenDate == convertedDate;
        var theirDate = new Date((year + min_age), month, day);
        var today = new Date;
        var resultAge = today.getTime() - theirDate.getTime();

        if (resultDate) {

            if (resultAge < 0) {
        return false
        } else {
        return true;
        }

        } else {
        return null;
        }*/

    }
}
