﻿jQuery.fn.generatedate = function (options) {
    var options = jQuery.extend({
        year_input: '#Book_a_meeting_Date_year',
        day_input: '#Book_a_meeting_Date_day',
        date_input: '#SendDate'
    },
  options);

    function buildCalendar(mydays) {
        var ret = '';
        for (var i = 1; i <= mydays; i++) {
            ret += "<option value='" + i + "'>" + i + "</option>";
        };
        return ret;
    };

    var month_input = $(this);

    $(this).change(function () {
        var theDayIs = parseInt($(options.day_input + " option:selected").text()) - 1;
        if ($("option:selected", this).val() == "4" || $("option:selected", this).val() == "6" || $("option:selected", this).val() == "9" || $("option:selected", this).val() == "11") {
            days = 30;
            $(options.day_input).html(buildCalendar(days));
        };
        if ($("option:selected", this).val() == "1" || $("option:selected", this).val() == "3" || $("option:selected", this).val() == "5" ||
    $("option:selected", this).val() == "7" ||
    $("option:selected", this).val() == "8" ||
    $("option:selected", this).val() == "10" ||
    $("option:selected", this).val() == "12") {
            days = 31;
            $(options.day_input).html(buildCalendar(days));
        }
        if ($("option:selected", this).val() == "2") {
            days = 28;
            if ((parseInt($(options.year_input + " option:selected").val()) - 1996) % 4 == 0)
                days = 29;
            $(options.day_input).html(buildCalendar(days));
        }
        if ($(options.day_input + " option").length > theDayIs + 1) {
            $(options.day_input + " option:eq(" + theDayIs + ")").attr("selected", "selected");
        }
        else {
            $(options.day_input + " option:eq(0)").attr("selected", "selected");
        };

        var selected_month = $(this).val();
        if (selected_month < 10) {
            selected_month = '0' + selected_month;
        };
        var selected_day = $(options.day_input).val();
        if (selected_day < 10) {
            selected_day = '0' + selected_day;
        };
        $(options.date_input).val($(options.year_input).val() + '-' + selected_month + '-' + selected_day);
    });

    $(this).change();

    $(options.day_input + ',' + options.year_input).live('change', function () {
        month_input.change();
    });

    now_date = new Date();
    var nowFullYear = now_date.getFullYear();
    var nowMonth = now_date.getMonth() + 1;
    var nowDay = now_date.getDate()


    $(options.year_input).children('option').each(function () {
        if ($(this).val() == nowFullYear) {
            $(this).attr('selected', 'selected')
        }
    });

    $(this).children('option').each(function () {
        if ($(this).val() == nowMonth) {
            $(this).attr('selected', 'selected')
        }
    });

    $(options.day_input).children('option').each(function () {
        if ($(this).val() == nowDay) {
            $(this).attr('selected', 'selected')
        }
    });

    return this;
};

book_modal_form = function () {
    $('#Book_a_meeting_Date_month').generatedate({
        year_input: '#Book_a_meeting_Date_year',
        day_input: '#Book_a_meeting_Date_day'
    });
    $('#book-form form').validate({
        rules: {
            Book_a_meeting_Company: 'required',
            Book_a_meeting_Name: 'required',
            Book_a_meeting_Email: {
                required: true,
                email: true
            },
            Book_a_meeting_Email_Confirm: {
                required: true,
                equalTo: "#Book_a_meeting_Email"
            },
            Book_a_meeting_Adress: 'required',
            Book_a_meeting_Phone: 'required',
            Book_a_meeting_Zipcode: 'required',
            Book_a_meeting_City: 'required',
            Book_a_meeting_Time: 'required'
        },
        messages: {
            Book_a_meeting_Company: 'required',
            Book_a_meeting_Email: {
                required: "Please enter your e-mail",
                email: "This is not an email"
            },
            Book_a_meeting_Email_Confirm: {
                equalTo: "Emails are not identical",
                required: "Please confirm your e-mail"
            },
            Book_a_meeting_Adress: 'required',
            Book_a_meeting_Phone: 'required',
            Book_a_meeting_Zipcode: 'required',
            Book_a_meeting_City: 'required',
            Book_a_meeting_Time: 'required'
        },
        errorPlacement: function(error, element){
            if (element.attr("name") == "Book_a_meeting_Email" || element.attr("name") == "Book_a_meeting_Email_Confirm") 
                element.after(error)
        },
        submitHandler: function (form) {
            $(form).ajaxSubmit({
                success: function () {
                    load_page(book_modal_form_url_submit, '', '');
                }
            });
        }
    });
};

call_modal_form = function () {
    $('#Please_call_me_Date_month').generatedate({
        year_input: '#Please_call_me_Date_year',
        day_input: '#Please_call_me_Date_day'
    });
    $('#call-form form').validate({
        rules: {
            Please_call_me_Company: 'required',
            Please_call_me_Name: 'required',
            Please_call_me_Email: {
                required: true,
                email: true
            },
            Please_call_me_Email_Confirm: {
                required: true,
                equalTo: "#Please_call_me_Email"
            },
            Please_call_me_Adress: 'required',
            Please_call_me_Phone: 'required',
            Please_call_me_Zipcode: 'required',
            Please_call_me_City: 'required',
            Please_call_me_Time: 'required'
        },
        messages: {
            Please_call_me_Company: 'required',
            Please_call_me_Name: 'required',
            Please_call_me_Email: 'required',
            Please_call_me_Email: {
                required: "Please enter your e-mail",
                email: "This is not an email"
            },
            Please_call_me_Email_Confirm: {
                equalTo: "Emails are not identical",
                required: "Please confirm your e-mail"
            },
            Please_call_me_Adress: 'required',
            Please_call_me_Phone: 'required',
            Please_call_me_Zipcode: 'required',
            Please_call_me_City: 'required',
            Please_call_me_Time: 'required'
        },
        errorPlacement: function(error, element){
            if (element.attr("name") == "Please_call_me_Email" || element.attr("name") == "Please_call_me_Email_Confirm") 
                element.after(error)
        },
        submitHandler: function (form) {
            $(form).ajaxSubmit({
                success: function () {
                    load_page(call_modal_form_url_submit, '', '');
                }
            });
        }
    });
};

$(document).ready(function () {
    $('#book-button').bind('click', function () {
        load_page(book_modal_form_url, '', book_modal_form);
        return false;
    });

    $('#call-button').bind('click', function () {
        load_page(call_modal_form_url, '', call_modal_form);
        return false;
    });
});
