﻿var validemail = false;

UserTestHelper = {
    SetTest: function(testID) {
        WebpandoraHelper.Instance.currentAnswer = -1;
        WebpandoraHelper.Instance.currentNumber = 1;
        WebpandoraHelper.Instance.answers = new Array();
        WebpandoraHelper.Instance.testID = testID;
        $.getScript(hostName + '/partners/remote/form.ashx?' + $.param({ place: WebpandoraHelper.Instance.place, service: WebpandoraHelper.Instance.service, "testID": testID, "encoding": encoding, date: new Date() }));
        this.RenderQuest();
    },

    SetTestByName: function(testName) {
        $.getScript(hostName + '/partners/remote/form.ashx?' + $.param({ method: "GetTestID", "testName": testName, "encoding": encoding, date: new Date() }));
    },

    SetCategory: function(category) {
        $.getScript(hostName + '/partners/remote/form.ashx?' + $.param({ place: WebpandoraHelper.Instance.place, service: WebpandoraHelper.Instance.service, "categoryID": category, "encoding": encoding, date: new Date() }));
    },

    RenderQuest: function() {
        $.getScript(hostName + '/partners/remote/form.ashx?' + $.param({ method: "RenderQuest", testID: WebpandoraHelper.Instance.testID, questionNumber: WebpandoraHelper.Instance.currentNumber, "encoding": encoding, date: new Date() }));
    },
    CheckEmail: function(email) {
        $.getScript(hostName + '/partners/remote/form.ashx?' + $.param({ method: "CheckEmail", "email": email, place: WebpandoraHelper.Instance.place, "encoding": encoding, date: new Date() }));
    },
    AddAnswer: function() {
        $("input#answers").val($("input#answers").val() + WebpandoraHelper.Instance.currentAnswer + ".");
        WebpandoraHelper.Instance.currentNumber++;
        this.RenderQuest();
        WebpandoraHelper.Instance.currentAnswer = -1;
    },

    SetAnswer: function(answer, sender) {
        WebpandoraHelper.Instance.currentAnswer = answer;
        if (sender) {
            $("a[rel='image']").each(function() {
                $(this).removeClass("active");
            });
            $(sender).addClass("active");
        }
    },

    NextQuest: function() {
        if (!this.CheckInput()) {
            return false;
        } else if (WebpandoraHelper.Instance.currentAnswer > 0) {
            $("div#genderBirthDay").hide();
            this.AddAnswer();
        } else {
            alert("Необходимо выбрать один из вариантов ответа");
        }
        return false;
    },

    CheckInput: function() {
        var res = true;
        var gender = $('input[name*=rblGender]:checked').val();
        if (!gender) {
            alert("Необходимо выбрать пол");
            res = false;
        }
        var day = $('select[name*=ddlDate]').val();
        var month = $('select[name*=ddlMonths]').val();
        var year = $('select[name*=ddlYear]').val();

        if ('Invalid Date' == new Date(month + '/' + day + '/' + year)) {
            alert('Неверная дата');
            res = false;
        }
        return res;
    },
    ChangeGender: function() {
        WebpandoraHelper.Instance.gender = $('input[name*=rblGender]:checked').val();
    },
    ChangeDate: function() {
        WebpandoraHelper.Instance.date = $("select[id*='ddlDate']").val();
    },
    ChangeMonth: function() {
        WebpandoraHelper.Instance.months = $("select[id*='ddlMonths']").val();
    },
    ChangeYear: function() {
        WebpandoraHelper.Instance.year = $("select[id*='ddlYear']").val();
    },
    ValidateEmail: function(data) {
        var email = data.email;
        if (!email || email == '') {
            alert('Укажите почтовые данные!');
            return false;
        }
        $.getScript(hostName + '/partners/remote/form.ashx?' + $.param({ method: "CheckEmail", "email": email, place: WebpandoraHelper.Instance.place, "encoding": encoding, date: new Date() }), function() {
            if (validemail) {
                SetProgressImage();
                $.getScript(hostName + '/partners/remote/form.ashx?' + $.param(data));
            }
        });
    }

};

function MenuAlign() {

    var mWidth = $('#ddmst').width();
    var dWidth = $('#par_container').width();

    var offLeft = (dWidth - mWidth) / 2;
    $('#ddmst').css("left", offLeft + "px");
}
  