function me_add_em(text) {
    text = ' ' + text + ' ';
    if (document.sc_form.message.createTextRange && document.sc_form.message.caretPos) {
        var caretPos = document.sc_form.message.caretPos;
        caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
        document.sc_form.message.focus();
    } else {
        document.sc_form.message.value  += text;
        document.sc_form.message.focus();
    }
}

AMecards = function() {
    this.previewStep3 = function(sSubmitUrl) {
        var sRecipient = $(".me_step2 input[name='recipient']").val();
        var sMessage = $(".me_step2 textarea[name='message']").val();
        if (sMessage == '') alert('Custom message is empty');

        if (sRecipient != '' && sMessage != '') {
            var oRequest = {};
            oRequest['recipient'] = sRecipient;
            oRequest['image'] = $(".me_step2 input[name='image']").val();
            oRequest['fontface'] = $(".me_step2 select[name='fontface']").val();
            oRequest['fontcolor'] = $(".me_step2 input[name='fontcolor']:checked").val();
            oRequest['template'] = $(".me_step2 input[name='template']:checked").val();
            oRequest['stamp'] = $(".me_step2 input[name='stamp']:checked").val();
            oRequest['music1'] = $(".me_step2 select[name='music1']").val();
            oRequest['music2'] = $(".me_step2 select[name='music2']").val();
            oRequest['applet'] = $(".me_step2 select[name='applet']").val();
            oRequest['message'] = sMessage;
            oRequest['notify'] = $(".me_step2 input[name='notify']:checked").val();
            oRequest['mode'] = 'json';

            var sReceiver = $('#mainx');
            $.post(sSubmitUrl, oRequest, function(oData) {
                if (oData.status == 1) {
                    sReceiver.html(oData.post);
                }
            }, 'json');
        }
    }
    this.finalizeStep4 = function(sSubmitUrl) {
        var sRecipient = $(".me_step4 input[name='recipient']").val();
        var sMessage = $(".me_step4 input[name='message']").val();

        if (sRecipient != '' && sMessage != '') {
            var oRequest = {};
            oRequest['recipient'] = sRecipient;
            oRequest['image'] = $(".me_step4 input[name='image']").val();
            oRequest['fontface'] = $(".me_step4 input[name='fontface']").val();
            oRequest['fontcolor'] = $(".me_step4 input[name='fontcolor']").val();
            oRequest['template'] = $(".me_step4 input[name='template']").val();
            oRequest['stamp'] = $(".me_step4 input[name='stamp']").val();
            oRequest['music'] = $(".me_step4 input[name='music']").val();
            oRequest['applet'] = $(".me_step4 input[name='applet']").val();
            oRequest['message'] = sMessage;
            oRequest['notify'] = $(".me_step4 input[name='notify']").val();
            oRequest['mode'] = 'json';

            var sReceiver = $('#mainx');
            $.post(sSubmitUrl, oRequest, function(oData) {
                if (oData.status == 1) {
                    sReceiver.html(oData.post);
                }
            }, 'json');
        }
    }

    this.selImgStep1 = function(sSubmitUrl) {

        if (sSubmitUrl != '') {
            var oRequest = {};
            oRequest['mode'] = 'json';

            var sReceiver = $('#mainx');
            $.post(sSubmitUrl, oRequest, function(oData) {
                if (oData.status == 1) {
                    sReceiver.html(oData.post);
                }
            }, 'json');
        }
    }

    this.mo_get_win_width = function() {
        var myWidth = 0;
        if( typeof( window.innerWidth ) == 'number' ) //Non-IE
            myWidth = window.innerWidth;
        else if( document.documentElement && document.documentElement.clientWidth ) //IE 6+ in 'standards compliant mode'
            myWidth = document.documentElement.clientWidth;
        else if( document.body && document.body.clientWidth) //IE 4 compatible
            myWidth = document.body.clientWidth;
        return myWidth;
    }

    this.mo_get_win_height = function() {
        var myHeight = 0;
        if( typeof( window.innerHeight ) == 'number' ) //Non-IE
            myHeight = window.innerHeight;
        else if( document.documentElement && document.documentElement.clientHeight ) //IE 6+ in 'standards compliant mode'
            myHeight = document.documentElement.clientHeight;
        else if( document.body && document.body.clientHeight) //IE 4 compatible
            myHeight = document.body.clientHeight;
        return myHeight;
    }

    this.mo_get_scrol = function() {
        var yPos = 0;
        if (self.pageYOffset) {
            yPos = self.pageYOffset;
        } else if (document.documentElement && document.documentElement.scrollTop){
            yPos = document.documentElement.scrollTop;
        } else if (document.body) {
            yPos = document.body.scrollTop;
        }
        return yPos;
    }

    this.mo_show_box = function(sSubmitUrl) {
        if (!$('#dolPopupFog').length) {
            $('<div id="dolPopupFog" style="display: none;">&nbsp;</div>')
                .prependTo('body')
                .css({
                    position: 'fixed',
                    top: 0,
                    left: 0,
                    width: $(window).width(),
                    height: '100%',
                    opacity: '0.7',
                    backgroundColor: '#000',
                    zIndex: 999
                });
        }
        $('#dolPopupFog').fadeIn(500);

        if (sSubmitUrl != '') {
            var oRequest = {};
            oRequest['mode'] = 'json';

            var sReceiver = $('#mainx');
            $.post(site_url + sSubmitUrl, oRequest, function(oData) {
                if (oData.status == 1) {
                    sReceiver.html(oData.post);
                }
            }, 'json');
        }


        var l = this.mo_get_win_width()/2 - 425;
        var t = this.mo_get_win_height()/2 - 370 + this.mo_get_scrol();
        document.getElementById('mysug_box').style.top  = t+'px';
        document.getElementById('mysug_box').style.left = l+'px'; 
        document.getElementById('mysug_box').style.display='block';
    }

    this.mo_hide_box = function() {
        $('#dolPopupFog').fadeOut(250);
        document.getElementById('mysug_box').style.display='none';
    }
}

var oMecards = new AMecards();

