﻿/// <reference path="jquery/jquery-1.2.6.min.js" />
/// <reference path="ajax/MicrosoftAjax.js" />
/// <reference path="ajax/MicrosoftAjaxTimer.js" />
/// <reference path="ajax/MicrosoftAjaxWebForms.js" />

Type.registerNamespace('PetroPrint');

PetroPrint.LoginControl = function(element) {
    PetroPrint.LoginControl.initializeBase(this, [element]);
    PetroPrint.LoginControl.createProperty("redirectedUrl");
    PetroPrint.LoginControl.createProperty("loginWebMethod");
    PetroPrint.LoginControl.createProperty("loginTextBoxId");
    PetroPrint.LoginControl.createProperty("passwordTextBoxId");
    PetroPrint.LoginControl.createProperty("rememberMeCheckBoxId");
    PetroPrint.LoginControl.createProperty("loginButtonId");
    PetroPrint.LoginControl.createProperty("errorPanelId");
    PetroPrint.LoginControl.createProperty("errorCssClass");

    this._loginTextBox = null;
    this._passwordTextBox = null;
    this._loginButton = null;
    this._rememberCheckBox = null;
    this._errorPanel = null;
    this._loginHandler = null;
}

PetroPrint.LoginControl.prototype =
{
    initialize: function() {
        PetroPrint.LoginControl.callBaseMethod(this, 'initialize');
        this._loginTextBox = $get(this.get_loginTextBoxId());
        this._passwordTextBox = $get(this.get_passwordTextBoxId());
        this._rememberCheckBox = $get(this.get_rememberMeCheckBoxId());
        this._loginButton = $get(this.get_loginButtonId());
        this._errorPanel = $get(this.get_errorPanelId());
        this._loginHandler = Function.createDelegate(this, this._login);
        $addHandler(this._loginButton, 'click', this._loginHandler);
        this._loginTextBox.focus();

        $(".defaultbutton").removeClass("defaultbutton");
        $(this._loginButton).addClass("defaultbutton");
    },
    dispose: function() {
        $removeHandler(this._loginButton, 'click', this._loginHandler);
        PetroPrint.LoginControl.callBaseMethod(this, 'dispose');
    },
    _login: function() {

        var elem = $("#" + this.get_element().id);
        elem.find("td.loginErrorIcon").hide();
        elem.find("td.loginErrorMessage").hide();
        elem.find("td.passwordErrorIcon").hide();
        elem.find("td.passwordErrorMessage").hide();
        $(this._passwordTextBox).removeClass(this.get_errorCssClass());
        $(this._loginTextBox).removeClass(this.get_errorCssClass());
        if (isEmpty(this._loginTextBox.value)) {
            $(this._loginTextBox).addClass(this.get_errorCssClass());
            elem.find("td.loginErrorIcon").show();
            elem.find("td.loginErrorMessage").show();
            $(this._loginTextBox).focus();
            return;
        }
        if (isEmpty(this._passwordTextBox.value)) {
            $(this._passwordTextBox).addClass(this.get_errorCssClass());
            elem.find("td.passwordErrorIcon").show();
            elem.find("td.passwordErrorMessage").show();
            $(this._passwordTextBox).focus();
            return;
        }

        this._startRequest();
        var params = "{'userName':'" + this._loginTextBox.value
            + "','password':'" + this._passwordTextBox.value
            + "','createPersistentCookie':'" + this._rememberCheckBox.checked + "'}";

        $.ajax({
            type: "POST",
            url: this.get_loginWebMethod(),
            data: params,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: Function.createDelegate(this, this._getResults),
            error: Function.createDelegate(this, this._webServiceError)
        });
    },
    _getResults: function(msg) {
        if (msg.d == true) {
            window.location = this.get_redirectedUrl();
        }
        else {
            this._errorPanel.style.display = 'inline';
            this._passwordTextBox.value = '';
            this._endRequest();
        }
    },
    _webServiceError: function(msg) {
        this._endRequest();
    },
    _startRequest: function() {
        this._errorPanel.style.display = 'none';
        this._loginButton.disabled = true;
    },
    _endRequest: function() {
        this._loginButton.disabled = false;
    }
}

PetroPrint.LoginControl.registerClass('PetroPrint.LoginControl', AjaxControlToolkit.ControlBase);

PetroPrint.LoginControl.descriptor =
    {
        properties: [{ redirectedUrl: String },
                     { loginWebMethod: String },
                     { loginTextBoxId: String },
                     { passwordTextBoxId: String },
                     { rememberMeCheckBoxId: String },
                     { loginButtonId: String },
                     { errorPanelId: String },
                     { errorCssClass: String }
                     ]
    }

PetroPrint.SubscribeControl = function(element) {
    PetroPrint.SubscribeControl.initializeBase(this, [element]);
    PetroPrint.SubscribeControl.createProperty("emailTextBoxId");
    PetroPrint.SubscribeControl.createProperty("subscribeButtonId");
    PetroPrint.SubscribeControl.createProperty("infoMessageId");
    PetroPrint.SubscribeControl.createProperty("errorMessageId");
    PetroPrint.SubscribeControl.createProperty("subscribeWebMethod");


    this._emailTextBox = null;
    this._subscribeButton = null;
    this._infoMessage = null;
    this._errorMessage = null;
    this._subscribeHandler = null;
    this._keyDownHandler = null;
    this._blurHandler = null;
    this._focusHandler = null;
}


PetroPrint.SubscribeControl.prototype =
{
    initialize: function() {

        this._emailTextBox = $get(this.get_emailTextBoxId());
        this._subscribeButton = $get(this.get_subscribeButtonId());
        this._infoMessage = $get(this.get_infoMessageId());
        this._errorMessage = $get(this.get_errorMessageId());

        this._subscribeHandler = Function.createDelegate(this, this._subscribe);

        $addHandler(this._subscribeButton, 'click', this._subscribeHandler);

        this._keyDownHandler = Function.createDelegate(this, this._defaultButtonSelect);
        $addHandler($("form")[0], 'keydown', this._keyDownHandler);

        this._blurHandler = Function.createDelegate(this, this._blur);
        this._focusHandler = Function.createDelegate(this, this._focus);

        $addHandler(this._emailTextBox, 'blur', this._blurHandler);
        $addHandler(this._emailTextBox, 'focus', this._focusHandler);

        PetroPrint.SubscribeControl.callBaseMethod(this, 'initialize');

    },
    dispose: function() {
        $removeHandler($("form")[0], 'keydown', this._keyDownHandler);
        $removeHandler(this._subscribeButton, 'click', this._subscribeHandler);

        $removeHandler(this._emailTextBox, 'blur', this._blurHandler);
        $removeHandler(this._emailTextBox, 'focus', this._focusHandler);

        PetroPrint.SubscribeControl.callBaseMethod(this, 'dispose');
    },
    _blur: function() {
        $(this._subscribeButton).removeClass("defaultbutton");
    },
    _focus: function() {
        $(this._subscribeButton).addClass("defaultbutton");
    },
    _subscribe: function() {

        if (!isValidEmail(this._emailTextBox.value)) {
            $(this._errorMessage).text('Введите правильный email');
            $(this._errorMessage).show();
            return;
        }
        $(this._errorMessage).hide();
        $(this._infoMessage).hide();

        this._subscribeButton.disabled = true;
        var params = "{'email':'" + this._emailTextBox.value + "'}";

        $.ajax({
            type: "POST",
            url: this.get_subscribeWebMethod(),
            data: params,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: Function.createDelegate(this, this._getResults),
            error: Function.createDelegate(this, this._webServiceError)
        });
    },
    _getResults: function(msd) {
        this._subscribeButton.disabled = false;
        if (msd.d) {
            $(this._infoMessage).text('Вы успешно подписаны');
            $(this._infoMessage).show();
        }
        else {
            $(this._errorMessage).text('Ваш E-mail уже зарегистрирован');
            $(this._errorMessage).show();
        }
    },
    _webServiceError: function(msd) {
        this._subscribeButton.disabled = false;
        $(this._errorMessage).text('Неопознанная ошибка. Попробуйте еще раз.');
        $(this._errorMessage).show();
    },
    _defaultButtonSelect: function(event) {
        if (event.keyCode && event.keyCode == 13) {
            if ($('.defaultbutton').size() > 0) {
                $('.defaultbutton').trigger('click');
                event.stopPropagation();
                event.preventDefault();
                return true;
            }
            var tgr = $(event.target);
            if (event.target.tagName && event.target.tagName.toLowerCase() == 'input' && tgr.attr('type') == 'text') {
                event.stopPropagation();
                event.preventDefault();
                return false;
            }
            return true;
        }
        else {
            return true;
        }

    }
}

PetroPrint.SubscribeControl.registerClass('PetroPrint.SubscribeControl', AjaxControlToolkit.ControlBase);

PetroPrint.SubscribeControl.descriptor =
    {
        properties: [{ emailTextBoxId: String },
        { subscribeWebMethod: String },
        { subscribeButtonId: String },
        { infoMessageId: String },
        { errorMessageId: String}]
    }


PetroPrint.RequestForm = function(element) {
    PetroPrint.RequestForm.initializeBase(this, [element]);
    PetroPrint.RequestForm.createProperty("emailTextBoxId");
    PetroPrint.RequestForm.createProperty("commentTextBoxId");
    PetroPrint.RequestForm.createProperty("submitButtonId");
    PetroPrint.RequestForm.createProperty("infoMessageId");
    PetroPrint.RequestForm.createProperty("errorMessageId");
    PetroPrint.RequestForm.createProperty("requestWebMethod");

    this._emailTextBox = null;
    this._commentTextBox = null;
    this._submitButton = null;
    this._infoMessage = null;
    this._errorMessage = null;
    this._submitHandler = null;
    this._keyDownHandler = null;
    this._blurHandler = null;
    this._focusHandler = null;


}


PetroPrint.RequestForm.prototype =
{
    initialize: function() {
        PetroPrint.RequestForm.callBaseMethod(this, 'initialize');

        this._emailTextBox = $get(this.get_emailTextBoxId());
        this._commentTextBox = $get(this.get_commentTextBoxId());
        this._submitButton = $get(this.get_submitButtonId());
        this._infoMessage = $get(this.get_infoMessageId());
        this._errorMessage = $get(this.get_errorMessageId());

        this._submitHandler = Function.createDelegate(this, this._submit);

        $addHandler(this._submitButton, 'click', this._submitHandler);



        this._blurHandler = Function.createDelegate(this, this._blur);
        this._focusHandler = Function.createDelegate(this, this._focus);

        $addHandler(this._emailTextBox, 'blur', this._blurHandler);
        $addHandler(this._emailTextBox, 'focus', this._focusHandler);

    },
    dispose: function() {
        $removeHandler(this._submitButton, 'click', this._submitHandler);

        $removeHandler(this._emailTextBox, 'blur', this._blurHandler);
        $removeHandler(this._emailTextBox, 'focus', this._focusHandler);
        PetroPrint.RequestForm.callBaseMethod(this, 'dispose');
    },
    _blur: function() {
        $(this._submitButton).removeClass("defaultbutton");
    },
    _focus: function() {
        $(this._submitButton).addClass("defaultbutton");
    },
    _submit: function() {

        $(this._errorMessage).hide();
        $(this._infoMessage).hide();

        this._submitButton.disabled = true;

        var params = "{'contact':'" + clearText(this._emailTextBox.value)
           + "','comment':'" + clearText(this._commentTextBox.value)
            + "','refferer':'" + window.location.href + "'}";

        $.ajax({
            type: "POST",
            url: this.get_requestWebMethod(),
            data: params,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: Function.createDelegate(this, this._getResults),
            error: Function.createDelegate(this, this._webServiceError)
        });
    },
    _getResults: function(msd) {
        this._submitButton.disabled = false;
        $(this._infoMessage).text('Запрос отправлен');
        $(this._infoMessage).show();
    },
    _webServiceError: function(msd) {
        this._submitButton.disabled = false;
        $(this._errorMessage).text('Неопознанная ошибка. Попробуйте еще раз.');
        $(this._errorMessage).show();
    }
}

PetroPrint.RequestForm.registerClass('PetroPrint.RequestForm', AjaxControlToolkit.ControlBase);

PetroPrint.RequestForm.descriptor =
    {
        properties: [{ emailTextBoxId: String },
        { commentTextBoxId: String },
        { infoMessageId: String },
        { errorMessageId: String },
        { requestWebMethod: String },
        { submitButtonId: String}]
    }


 
