﻿function OpenBox()
{
    var containerMaxHeight = 800;
    var containerWidth = 420;

    var blackdiv = document.createElement('div');
    document.body.appendChild(blackdiv);
    jQuery(blackdiv).css('top', 0).css('left', 0).css('position', 'absolute').css('z-index', 99998).
            width(jQuery(document.body).width()).height(jQuery(document.body).height()).css('background-color', '#333').css('opacity', 0.6);

    var container = document.createElement('div');
    document.body.appendChild(container);
    jQuery(container).css('position', 'absolute').
            css('text-align', 'center').css('border', '1px solid #333').css('z-index', 99998).
            css('background-color', '#fff').css('overflow', 'hidden').
            css('-moz-border-radius', '8px').css('-webkit-border-radius', '8px').css('visibility', 'hidden').
            css('-moz-box-shadow', '6px 6px 6px #666666').css('-webkit-box-shadow', '6px 6px 6px #666666').css('box-shadow', '6px 6px 6px #666666');

    var close = document.createElement('a');
    document.body.appendChild(close);
    jQuery(close).attr('href', '#').css('background-image', 'url(../../Partners/PostbankGASuche/Layout/Images/List/close.png)').
            css('position', 'absolute').css('display', 'block').width(27).height(27).css('z-index', 99999).css('visibility', 'hidden');

    function SetPos()
    {
        jQuery(container).css('left', (jQuery(window).width() - jQuery(container).width()) / 2).css('top', Math.max($(window).scrollTop() + 20, $(window).scrollTop() + (jQuery(window).height() - jQuery(container).height())/2 ));
        jQuery(close).css('left', (jQuery(window).width() + jQuery(container).width()) / 2 - 11).css('top', $(container).offset().top - 12);
        jQuery(container).css('visibility', 'visible');
        jQuery(close).css('visibility', 'visible');
    }
    
    setTimeout(SetPos, 200);
    
    function closeFunc()
    {
        container.parentNode.removeChild(container);
        blackdiv.parentNode.removeChild(blackdiv);
        close.parentNode.removeChild(close);
    }

    jQuery(blackdiv).click(function() { closeFunc(); return false; });
    jQuery(close).click(function() { closeFunc(); return false; });

    container.close = function() { jQuery(close).click(); };
    
    function SetTop()
    {
        jQuery(container).css('top', jQuery(window).scrollTop() + 20);
        jQuery(close).css('top', jQuery(window).scrollTop() + 8);
    }
    SetTop();
    //jQuery(window).scroll(SetTop);

    return container;
}

function ChangeItem(Item, Blocked)
{

    if (typeof Blocked == 'undefined')
    {
        var ajaxurl = '/Common/AJAX/IsPoiBlocked.ashx?Ebinr=' + Item.Ebinr;
        if (window.location.pathname.match(/\/YMPresentation\//i))
        {
            ajaxurl = window.location.pathname.replace(/(\/YMPresentation\/.*)/i, "/YMPresentation/Common/AJAX/IsPoiBlocked.ashx?Ebinr=" + Item.Ebinr);
        }

        $.ajax({
            url: ajaxurl,
            dataType: 'json',
            data: null,
            success: function (resp)
            {
                if (resp != null && resp.IsEditable === true)
                {
                    ChangeItem(Item, true);
                }
                else
                {
                    //window.location.href = "../../YM/Login/LoginMigration.aspx?Ebinr={0}&SessionGuid={1}".replace("{0}", Item.Ebinr).replace("{1}", SessionGuid);
                    var href;
                    if (window.location.pathname.match(/\/YMPresentation\//i))
                    {
                        href = window.location.pathname.replace(/(\/YMPresentation\/.*)/i, "/YMPresentation/YM/Login/LoginMigration.aspxEbinr={0}&SessionGuid={1}".replace("{0}", Item.Ebinr).replace("{1}", SessionGuid));
                    }
                    else
                    {
                        href = "/YM/Login/Login-Migration.aspx?Ebinr={0}&SessionGuid={1}".replace("{0}", Item.Ebinr).replace("{1}", SessionGuid);
                    }

                    window.location.href = href;
                }
            }
        });

        return;
    }


    var Callee = arguments.callee;
    if (typeof jQuery !== 'function') {setTimeout(function() { Callee(Item.Ebinr); }, 100);  return; }

    var container = OpenBox();

    var productSelectUrl = '/YMap/Registration/ProductSelect.aspx?Ebinr=' + Item.Ebinr + '&SessionGuid=' + SessionGuid;
    if (window.location.pathname.match(/\/YMPresentation\//i))
    {
        productSelectUrl = window.location.pathname.replace(/(\/YMPresentation\/.*)/i, '/YMPresentation/YMap/Registration/ProductSelect.aspx?Ebinr=' + Item.Ebinr + '&SessionGuid=' + SessionGuid);
    }

    var ihtml = "";
    ihtml = '<div class="ChangeItemContainer"><h3>Korrektur melden</h3>';
    ihtml += '<div>';
    ihtml += '<span class="ChangeItem-ItemName">' + Item.Firstname + ' ' + Item.Surname + ' ' + Item.Addon1 + '</span>';
    ihtml += '<span class="ChangeItem-ItemAddress1">' + Item.Street + ' ' + Item.HouseNo + '</span>';
    ihtml += '<span class="ChangeItem-ItemAddress2">' + Item.Zip + ' ' + Item.Town + ' ' + Item.Town2 + '</span>';
    ihtml += '<span class="ChangeItem-Question">Sie haben aktuelle Informationen zu diesem Eintrag und möchten uns diese mitteilen? Dann wählen Sie bitte unter folgenden Meldungen aus:</span>';
    ihtml += '<div class="ChangeItem-RadioBox"><input type="radio" value="1" name="ChangeType" />Eintrag existiert nicht mehr<br />';
    ihtml += '<input type="radio" name="ChangeType" value="3" />Eintrag ist eine Dublette<br />';
    ihtml += '<input type="radio" name="ChangeType" value="2" />Falsche Adresse<br />';
    ihtml += '<input type="radio" name="ChangeType" value="4" />Falsche Kontaktdaten<br />';
    ihtml += '<input type="radio" name="ChangeType" value="5" />Sonstiges</div>';
    ihtml += '<textarea rows="8" cols="42"></textarea>';
    ihtml += '<div><span class="ChangeItem-Email">Ihre E-Mail-Adresse (optional): </span><input class="ChangeItem-InpEmail" type="text" /></div>';
    ihtml += '<div style="position: relative; margin: 12px 8px 0px 12px; padding-bottom: 12px;">';
    ihtml += '<div class="ChangeItem-HintText"><span>Hinweis: Ihre Meldung wird redaktionell geprüft und anschliessend freigegeben. Falls Sie Ihre E-Mail-Adresse angeben, besteht bei Fragen der Redaktion die Möglichkeit, sich direkt an Sie zu wenden.</span></div>';
    ihtml += '<button class="ChangeItem-BtnSend Button-Normal" type="submit"><span class="Submit"><em>Mitteilung abschicken</em></span></button>';
    ihtml += '</div><div style="clear: both;" class="hr"><hr/></div>';
    ihtml += '<div class="ChangeItem-OwnItem"><span class="ChangeItem-OwnItemText">Es handelt sich um Ihren Eintrag?</span>';
    ihtml += '<a class="Button-Normal" href="' + productSelectUrl + '"><span class="Submit"><em>Meinen Eintrag pflegen</em></span></a>';
    ihtml += '<div style="clear: both;"></div>';
    ihtml += '</div>';
    ihtml += '</div>';
    container.innerHTML = ihtml;

    $(container).find('.ChangeItem-BtnSend').click(function ()
    {
        var ebinr = Item.Ebinr;
        var type = "";
        $(container).find('input[type=radio]').each(function ()
        {
            var rb = $(this);
            if (rb.attr('checked'))
            {
                type = rb.val();
            }
        });

        if (type == null || type == "")
        {
            alert("Bitte wählen Sie eine von den Meldungen.");
            return;
        }

        var msg = encodeURIComponent($(container).find('textarea').val());
        var email = $(container).find('.ChangeItem-InpEmail').val();

        function checkEmail(email)
        {
            email = jQuery.trim(email);
            if (email == null || email == "") return true;

            var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
            if (filter.test(email)) return true;

            return false;
        }

        if (!checkEmail(email))
        {
            alert("Bitte geben Sie eine gültige E-Mail Adresse ein oder lassen Sie das Feld leer.");
            return;
        }

        var url = "/YMap/Registration/Registration.aspx?ItemContentEbinr={0}&ItemContentContractAction=31&Action=Dummy&ItemEditorTypeOfChange={1}&ItemEditorComment={2}&ItemEditorEmail={3}";

        if (window.location.pathname.match(/\/YMPresentation\//i))
        {
            url = window.location.pathname.replace(/(\/YMPresentation\/.*)/i, "/YMPresentation/YMap/Registration/Registration.aspx?ItemContentEbinr={0}&ItemContentContractAction=31&Action=Dummy&ItemEditorTypeOfChange={1}&ItemEditorComment={2}&ItemEditorEmail={3}");
        }

        url = url.replace('{0}', ebinr);
        url = url.replace('{1}', type);
        url = url.replace('{2}', msg);
        url = url.replace('{3}', email);

        $.ajax({ url: url, type: 'get',
            success: function ()
            {
                var div = $('.ChangeItemContainer > div');
                var height = div.height();
                var span = $('<span class="ChangeItem-Thankyou">Vielen Dank für Ihre Mitteilung!</span>');
                div.html(span).height(height);
                span.css('position', 'relative').css('top', (span.height() + div.height()) / 3);
                setTimeout(function () { container.close(); }, 2000);
            },
            error: function ()
            {
                //alert('Ein Fehler ist aufgetreten!');
            }
        });

    });

    return false;
}
