﻿function InitComplaints(category) {
    var MessageForm = $("#divComplaintsBook");
    if (MessageForm.length == 0) {
        $("body").append("<div id=\"divComplaintsBook\" class=\"popup popupComplainBook\"></div>")
        MessageForm = $("#divComplaintsBook");
    }
    if (category)
        ShowComplaintsForm(MessageForm, '?type='+category);
    else
        ShowComplaintsForm(MessageForm, '');
}
function ShowComplaintsForm(msgForm, query) {
    if (query && query.indexOf("http://") == 0) {
        if (query.indexOf("?") > 0)
            query = query.substring(query.indexOf("?"));
        else
            query = "";
    }
    if (query)
        query += '&q=' + (new Date()).valueOf();
    else
        query = '?q=' + (new Date()).valueOf();
    msgForm.load('/page_modules/ComplaintBook.aspx' + query, function () {
        $(this).show();
        BindComplaintsEvents($(this));
    });
}

function BindComplaintsEvents(msgForm) {
    msgForm.find("div.close").click(function () {
        msgForm.hide();
    });
    msgForm.find("a.load").click(function () {
        msgForm.find("#spanLoading").show();
        ShowComplaintsForm(msgForm, $(this).attr("href"));
        return false;
    });
    var ids = ['txtName', 'txtEmail', 'ddlCategory', 'txtMessage'];
    for (var i = 0; i < ids.length; i++) {
        SetValidateOnField(msgForm, ids[i]);
    }
    msgForm.find("#btnSend").click(function () {
        var valid = true;
        for (var i = 0; i < ids.length; i++) {
            valid &= ValidateField(null, msgForm, ids[i]);
        }
        if (!valid)
            return;
        var params = { txtName: msgForm.find("#txtName").val(),
            txtEmail: msgForm.find("#txtEmail").val(),
            ddlCategory: msgForm.find("#ddlCategory").val(),
            txtMessage: msgForm.find("#txtMessage").val(),
            txtPhone: msgForm.find("#txtPhone").val(),
            ref: document.location.pathname + document.location.search
        };
        msgForm.find("#spanLoading").show();
        //сохраняем сообщение
        $.post("/page_modules/ComplaintBook.aspx", params, function (data) {
            msgForm.html(data);
            msgForm.find("div.close").click(function () {
                msgForm.hide();
            });
        });
    });
    //ловим ctrl+enter
    msgForm.keydown(function (evnt) {
        if (!evnt) evnt = window.event;
        var key = evnt.keyCode;
        if (evnt.ctrlKey && key == 13) {
            msgForm.find("#btnSend").click();
            return false;
        }
    });
}
function SetValidateOnField(msgForm, id) {
    msgForm.find("#" + id).blur(function () {
        ValidateField(this, msgForm, id)
    });
}
function ValidateField(sender, msgForm, id) {
    var txt = null;
    if (!sender)
        txt = msgForm.find("#" + id);
    else
        txt = $(sender);
    if (txt.length == 0) return true;
    if ($.trim(txt.val()) == "") {
        msgForm.find("#" + id + "Star").show();
        return false;
    }
    else {
        if (id == 'txtEmail') {
            var rg = /([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})/;
            if (!rg.test($.trim(txt.val()))) {
                msgForm.find("#" + id + "Star").show();
                return false;
            }
        }
        msgForm.find("#" + id + "Star").hide();
        return true;
    }
}

// смена иконки у ссылки на книгу при перезагрузки 
var nameAnim;
if (getCookie("startComplainBook")) {
    if(getCookie("startComplainBook") == "offerAnimBook") {
        nameAnim = "complainAnimBook";
        SetCookie("startComplainBook", nameAnim, 0.01);
    }
    else {
        nameAnim = "offerAnimBook";
        SetCookie("startComplainBook", nameAnim, 0.01);
    }
}
else {
    nameAnim = "offerAnimBook";
    SetCookie("startComplainBook", nameAnim, 0.01); 
}
$(document).ready(function() {
    $("span.startComplainBook").addClass(nameAnim);
})
