﻿var TellFriendBox = Class.create({
    initialize: function(titleTxt, message, allowComment) {
        
        this.content = null;
        this.notfWindow = null;
        
        var mFrame = $(document.createElement('span'));
        mFrame.addClassName('notoficationBox');
        mFrame.addClassName('tellAFriendForm');
        
        var mTab = $(document.createElement('table'));
        mTab.cellSpacing = 0;
        mTab.cellPadding = 0;
        mTab.border = 0;
        mTab.addClassName('tellAFriend');
        mFrame.appendChild(mTab);
        
        var mTabBody = $(document.createElement('tbody'));
        mTab.appendChild(mTabBody);
        var tr = $(document.createElement('tr'));
        mTabBody.appendChild(tr);
        var td = $(document.createElement('td'));
        tr.appendChild(td);
        
        // add the title
        var titleDiv = $(document.createElement('div'));
        titleDiv.innerText = titleTxt;
        titleDiv.innerHTML = titleDiv.innerText;
        titleDiv.addClassName('title');
        td.appendChild(titleDiv);
        
        // add message if available
        if (message) {
            var msgDiv = $(document.createElement('div'));
            msgDiv.addClassName('staticMessage');
            msgDiv.innerText = message;
            msgDiv.innerHTML = msgDiv.innerText;
            td.appendChild(msgDiv);
        }
        
        var form = $(document.createElement('div'));
        form.addClassName('inputForm');
        
        // create a container for the validation summary
        var valSummCont = $(document.createElement('div'));
        valSummCont.id = 'validationSummCont';
        valSummCont.hide();
        form.appendChild(valSummCont);
        
        // friends email input field
        var tbFriendsEmail = $(document.createElement('input'));
        tbFriendsEmail.id = 'tbTFFriendsEmail';
        tbFriendsEmail.addClassName('standardField');
        form.appendChild(GenFormFieldSet("Friend's e-mail", tbFriendsEmail, true));
        
        // your name input field
        var tbYourName = $(document.createElement('input'));
        tbYourName.id = 'tbTFYourName';
        tbYourName.addClassName('standardField');
        form.appendChild(GenFormFieldSet("Your name", tbYourName, true));
        
        // your name input field
        var tbYourEmail = $(document.createElement('input'));
        tbYourEmail.id = 'tbTFYourEmail';
        tbYourEmail.addClassName('standardField');
        tbYourEmail.addClassName('separate');
        form.appendChild(GenFormFieldSet("Your e-mail", tbYourEmail, true));
        
        if (allowComment) {
            // additional comments
            var tbComments = $(document.createElement('textarea'));
            tbComments.id = 'tbTFComments';
            tbComments.addClassName('standardField');
            tbComments.addClassName('comment');            
            form.appendChild(GenFormFieldSet("Additional comments", tbComments, false));
        }
        
        td.appendChild(form);
        
        // add buttons
        var btnCont = $(document.createElement('div'));
        
        var lftBtnCont = $(document.createElement('div'));
        lftBtnCont.setStyle({'float': 'left'});
        
        var imgBtnSend = $(document.createElement('img'));
        imgBtnSend.src = '/images/interface/buttons/send_email_glossy_on.gif';
        imgBtnSend.setStyle({cursor: 'pointer'});
        
        var imgBtnCancel = $(document.createElement('img'));
        imgBtnCancel.src = '/images/interface/buttons/g_blue_close.gif';
        imgBtnCancel.setStyle({cursor: 'pointer'});
        
        var rgtBtnCont = $(document.createElement('div'));
        rgtBtnCont.setStyle({'float': 'right'});
        
        lftBtnCont.appendChild(imgBtnSend);
        rgtBtnCont.appendChild(imgBtnCancel);
        
        btnCont.appendChild(lftBtnCont);
        btnCont.appendChild(rgtBtnCont);
        
        td.appendChild(btnCont);
        
        this.tbFriendsEmail = tbFriendsEmail;
        this.tbYourName = tbYourName;
        this.tbYourEmail = tbYourEmail;
        this.tbComments = tbComments;
        this.valSummCont = valSummCont;
        this.content = mFrame;
        this.btnSend = imgBtnSend;
        this.btnCancel = imgBtnCancel;
    },
    show: function() {
        this.notifyWindow = new WindowTranspBg(null, null, this.content);        
    }
});

var tellAFriendRequestActive = false;

function ShowTellAFriendForm(titleTxt, message, allowComment, authKey, authMsg) {
    var tfBox = new TellFriendBox(titleTxt, message, allowComment);
    tfBox.show();
    
    var valSumm = new ValidationSummaryBox(tfBox.valSummCont);
    valSumm.addInputField(tfBox.tbYourName);
    valSumm.addInputField(tfBox.tbYourEmail);
    valSumm.addInputField(tfBox.tbFriendsEmail);
    valSumm.addInputField(tfBox.tbComments);
    
    // add the cancel event to the button
    Event.observe(tfBox.btnSend, 'click', function() {SendTellAFriendMessage_Request(tfBox.tbYourName, tfBox.tbYourEmail, tfBox.tbFriendsEmail, tfBox.tbComments, authKey, authMsg, valSumm, tfBox);});
    Event.observe(tfBox.btnCancel, 'click', function() {tfBox.notifyWindow.remove();});        
}

function SendTellAFriendMessage_Request(inputObj1, inputObj2, inputObj3, inputObj4, authKey, authMsg, valSumm, tfBox) {    
    if (!tellAFriendRequestActive) {
        tellAFriendRequestActive = true;
        tfBox.btnSend.src = tfBox.btnSend.src.replace('_on', '_off');
        tfBox.btnSend.setStyle({cursor: 'default'});
        Messaging.TellFriend(ConvertInputToJSObject(inputObj1), ConvertInputToJSObject(inputObj2), ConvertInputToJSObject(inputObj3), ConvertInputToJSObject(inputObj4), authKey, authMsg, function(r) {SendTellAFriendMessage_Callback(r, valSumm, tfBox);});
    }
}

function SendTellAFriendMessage_Callback(response, valSummObj, tfBox) {
    if (!response.error) {
        tellAFriendRequestActive = false;
        tfBox.btnSend.src = tfBox.btnSend.src.replace('_off', '_on');
        tfBox.btnSend.setStyle({cursor: 'pointer'});
        valSummObj.showSummary(response.value);
    }
}