var TellAFriendPopUp;
var TellAFriendCompletePopUp;

var TellAFriendBaseContentId;
var TellAFriendLoggedIn;
var TellAFriendUserId;
var TellAFriendIsland;

var TellAFriendDefaultText = true;

//function dss_addEvent(el,etype,fn) {
//  if(el.addEventListener && (!window.opera || opera.version) &&
//  (etype!='load')) {
//    el.addEventListener(etype,fn,false);
//  } else if(el.attachEvent) {
//    el.attachEvent('on'+etype,fn);
//  } else {
//    if(typeof(fn) != "function") return;
//    if(typeof(window.earlyNS4)=='undefined') {
//      // to prevent this function from crashing Netscape versions before 4.02
//      window.earlyNS4=((navigator.appName.toLowerCase()=='netscape')&&
//      (parseFloat(navigator.appVersion)<4.02)&&document.layers);
//    }
//    if((typeof(el['on'+etype])=="function")&&!window.earlyNS4) {
//      var tempFunc = el['on'+etype];
//      el['on'+etype]=function(e){
//        var a=tempFunc(e),b=fn(e);
//        a=(typeof(a)=='undefined')?true:a;
//        b=(typeof(b)=='undefined')?true:b;
//        return (a&&b);
//      }
//    } else {
//      el['on'+etype]=fn;
//    }
//  }
//}
//dss_addEvent(window,'load',function()
//{
//    TellAFriendPopUp = new RmzPopup("TellAFriend", 465);
//    TellAFriendCompletePopUp = new RmzPopup("TellAFriendComplete", 465);
//    $('TellAFriend').style.display = 'block';
//    $('TellAFriendComplete').style.display = 'block';
//});

document.observe('dom:loaded', function()
{
	if (!$('TellAFriend')) { return }
	if (!$('TellAFriendComplete')) { return }
	
    TellAFriendPopUp = new RmzPopup("TellAFriend", 465);
    TellAFriendCompletePopUp = new RmzPopup("TellAFriendComplete", 465);
    $('TellAFriend').style.display = 'block';
    $('TellAFriendComplete').style.display = 'block';
});

function ShowTellAFriendPopUp(baseContentId, loggedIn)
{
    TellAFriendBaseContentId = baseContentId;
    TellAFriendLoggedIn = loggedIn;
    TellAFriendLoadData(baseContentId);
};

function TellAFriendLoadData(baseContentId)
{
    if(Trail != null) Trail.show();

	Rmz.Web.WebServices.DataService.Profiles_GetUser(baseContentId, TellAFriendOnReadSuccess, OnAjaxRequest_Error);
}

function TellAFriendOnReadSuccess(result)
{
    if(Trail != null) Trail.hide();

    TellAFriendIsland = result.evalJSON();           
    TellAFriendUserId = TellAFriendIsland.Data.userId;

    $(TAFURLTextBox).value = window.document.URL;
    $(TAFMessageTextBox).value = 'Start typing...';
    $(TAFSubjectTextBox).value = TellAFriendIsland.Data.SenderName + " wants to share this webpage with you";

    $(TAFSendMeACopyCheckBox).checked = false;
    TellAFriendDefaultText = true;

    TellAFriendPopUp.show();
}

function TellAFriendSendEmails()
{                     
    if($(TAFMessageTextBox).value == 'Start typing...')
    {
						$(TAFMessageTextBox).value = '';
    }
    
    var isValid = Page_ClientValidate('TellAFriend')

    if (!isValid) return;

    TellAFriendPopUp.close();
    
    if (TellAFriendLoggedIn) {

        if (Trail != null) Trail.show();

        Rmz.Web.WebServices.DataService.Messages_SendTAFMessage($(TAFEmailTextBox).value, $(TAFSubjectTextBox).value, $(TAFURLTextBox).value, $(TAFMessageTextBox).value, $(TAFSendMeACopyCheckBox).checked, TellAFriendOnMessagesSent, OnAjaxRequest_Error);
    }
    else {
        cta_PostponeAction('Rmz.Web.WebServices.DataService.Messages_SendTAFMessage($(TAFEmailTextBox).value, $(TAFSubjectTextBox).value, $(TAFURLTextBox).value, $(TAFMessageTextBox).value, $(TAFSendMeACopyCheckBox).checked, TellAFriendOnMessagesSent, OnAjaxRequest_Error);');
    }
}

function TellAFriendOnMessagesSent(result)
{   
    if(Trail != null) Trail.hide();
    
    TellAFriendCompletePopUp.show();
}

function HideTellAFriendDefaultText(sender)
{
    if(TellAFriendDefaultText)
    {
        sender.value = '';
        TellAFriendDefaultText = false;
    }
}
