/*
#---------------------------------------------------------------------------
# Copyright   : rexx systems GmbH, 20097 Hamburg, Heidenkampsweg 101
#               Veraenderung oder Nutzung der Scripte/Anwendung ist nur mit
#               schriftlicher Genehmigung der rexx systems GmbH gestattet.
#---------------------------------------------------------------------------
*/


$.fn.serializeObject = function()
{
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name]) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};


var recommendPage = (function () {

    var stringsLoaded = false,
        fields     = [],
        fieldHTMId = [],
        nameDivIds = [],
        stringids  = [],
        strings    = [];

    fieldHTMId[1]  = 'fromNameField';
    nameDivIds[1]  = 'fromName';

    fieldHTMId[2]  = 'toMailField';
    nameDivIds[2]  = 'toMail';

    fieldHTMId[3]  = 'mailMsgField';
    nameDivIds[3]  = 'mailMsg';

    fieldHTMId[4]  = 'recommendPageUrl';
    nameDivIds[4]  = null;

    fieldHTMId[5]  = 'hncaptcha_private_key';
    nameDivIds[5]  = 'capchaRequest';

    fieldHTMId[6]  = 'hncaptcha';
    nameDivIds[6]  = null;

    fieldHTMId[7]  = 'hncaptcha_public_key';
    nameDivIds[7]  = null;

    return {

        /*
         * initialisiert die seite-empfehlen funktion
         */
        init: function () {

            recommendPage.viewButton();
            $('a.recommendPageTrigger').click(recommendPage.loadForm);

        },

        showWindow: function (phpData) {

            $('.jqmWindow').css('width', '600px');
            shopBasicJs.showContentLayer(phpData.html, phpData.title);

            $('.defaultClose').hide();

            $('#recommendSubmit').click(recommendPage.submit);
            $('#hncaptcha_refresh').click(recommendPage.refreshCaptcha);

            $('#recommendExit').click(function(e) {
                e.preventDefault();

                $('.defaultClose').show();
                shopBasicJs.hideContentLayer();
            });
        },

        loadForm: function (e) {
            e.preventDefault();

            var href = $(this).attr('url');

            $.ajax({
                type: 'GET',
                url : '../inc/functions/xml_http_requests.php',
                data: {
                    f  : 'get_recommend_page_form',
                    url: href,
                    sid: mySession
                },
                dataType: 'json',
                success: recommendPage.showWindow
            });
        },

        /*
         * reaktionen auf das absenden des formulars
         */
        submit: function () {

            // fehleranzeige zurueck setzen
            $('#recommendError').hide();
            recommendPage.sendPost();

        },

        sendPost: function () {

            var data = {
                    f : 'get_recommend_page_result',
                    sid: mySession
            };
            var dataArray = $('#recommendFormBox').serializeArray();
            $.each(dataArray, function() {
                if (data[this.name]) {
                    if (!datao[this.name].push) {
                        data[this.name] = [data[this.name]];
                    }
                    data[this.name].push(this.value || '');
                } else {
                    data[this.name] = this.value || '';
                }
            });

            $.ajax({
                type: 'POST',
                url : '../inc/functions/xml_http_requests.php?url=' + encodeURI($('.recommendPageTrigger').attr('url')),
                data: data,
                dataType: "json",
                success: recommendPage.handleResult
            });
        },

        handleResult: function (phpData, status) {

            if(typeof phpData.success != 'undefined' && $(phpData.success).length > 0) {
                $('.jqmWindowContent').html(phpData.success);
                
            } else if(typeof phpData.msg != 'undefined' && $(phpData.msg).length > 0) {
                $('#recommendError').html('').show();
                $.each(phpData.msg, function(i, msg) {
                    $('#recommendError').append(
                        $('<div />').attr('id', i).html(msg)
                    );
                });
            }
        },

        viewButton: function () {

            if ($.browser.version >= 7 || !$.browser.msie) {
                $('.recommendLinkBox').show();
            }
        },

        refreshCaptcha: function () {

            $.ajax({
                type: 'POST',
                url : '../inc/functions/xml_http_requests.php',
                data: {
                    f : 'get_recommend_captcha_request',
                    sid: mySession
                },
                dataType: "json",
                success: function (json) {

                    $('img#recommendCaptcha').attr('src', 'inc/functions/captchaImage.php?public=' + json.key + '&sid=' + mySession);
                    $('input#hncaptcha_public_key').val(json.key);
                    $('input#hncaptcha').val(json.currentTry);
                    $('input#hncaptcha_private_key').val('');
                }
            });
        }

    };
}());
