
function friends_onSendMessageClick(evt, toName){
    var lblToTitle= getElement('lblToTitle');
    var lblTo = getElement('lblTo');
    lblToTitle.innerHTML = toName;
    lblTo.innerHTML = toName;
    
    var dialog = sendMessageBox(toName, evt.clientX, evt.clientY);

return false;
}

function sendMessageBox(toName, xpos, ypos){
    var statusObj = getElement('lblSendFriendMessageStatus');
    statusObj.innerHTML = '';

    var handleMessageSubmit = function() {
        this.form.txtTo.value = toName;
        this.submit();
        //this.show();
    };
    var handleCancel = function() {
        this.cancel();
    };

    var onSuccess = function(obj) {
        try {
            var xmldoc = obj.responseXML;
            var root = xmldoc.getElementsByTagName('root').item(0);
            var status = root.getElementsByTagName("status")[0].firstChild.nodeValue;

            if(status == 'true'){
                dialog.form.reset();
                //dialog.hide();
                displayDialogStatusMessage(statusObj.id, STATUS_SUCCESS, MSG_DISPLAY_MESSAGE_SENT );
            }
            else{
                var desc = root.getElementsByTagName('description')[0].firstChild.nodeValue;
                displayDialogStatusMessage(statusObj.id, STATUS_ERROR, desc);
            }        
        }
        catch(ex){
            alert('There was an error sending your message! ' + ex.message);
        }
    }
    
    var onFailure = function(obj) {
        displayDialogStatusMessage(statusObj.id, STATUS_ERROR, 'Your submission failed! Status: ' + obj.status);
    }

    var sendMessageButtons = [ { text:"Send", handler:handleMessageSubmit, isDefault:true },
                               { text:"Cancel", handler:handleCancel }  ];

    var divObj = getElement('pnlSendFriendMessage');
    if(divObj) divObj.style.display = '';

    var dialog;
    if(yahooDialogs['pnlSendFriendMessage']){
        dialog = yahooDialogs['pnlSendFriendMessage'];
        dialog.show();
    }
    else{
        //dialog = new YAHOO.widget.Dialog('pnlSendFriendMessage', { postmethod:"async", width:"360px", height:"260px", x:xpos, y:ypos, underlay:"none", draggable:false, fixedcenter:true, constraintoviewport:true } );
        dialog = new YAHOO.widget.Dialog('pnlSendFriendMessage', { postmethod:"async", width:"370px", x:xpos, y:ypos, underlay:"none", draggable:false, fixedcenter:true } );
        yahooDialogs['pnlSendFriendMessage'] = dialog;
    }

    dialog.submit = function() {
        this.form.txtTo.value = toName;
        if(this.validate()){
            this.beforeSubmitEvent.fire();
            this.doSubmit();
            this.submitEvent.fire();
            return true;
        }
        else{
            return false;
        }
    };
    dialog.validate = function() { return messageValidator(this.getData(), statusObj); }
    dialog.callback.success = onSuccess;
    dialog.callback.failure = onFailure;
    dialog.cfg.queueProperty("buttons", sendMessageButtons);
    dialog.render();

    return dialog;
}

function friends_onInviteAFriendClick(){
    var statusObj = getElement('lblInviteAFriendStatus');
    inviteMessageBox('pnlInviteAFriend', statusObj);
    
    return false;
}

function global_onInviteFriendClick(){
    var statusObj = getElement('lblInviteFriendStatus');
    inviteMessageBox('pnlInviteFriend', statusObj);
    
    return false;
}

function inviteMessageBox(divID, statusObj){
    statusObj.innerHTML = '';

    var handleFriendInvite = function() {
        this.submit();
        //this.show();
    };
    var handleCancel = function() {
        this.cancel();
    };

    var onFailure = function(obj) {
        displayDialogStatusMessage(statusObj.id, STATUS_ERROR, 'Your submission failed! Status: ' + obj.status);
    }

    var buttons = [ { text:"Invite", handler:handleFriendInvite, isDefault:true },
                    {text:"Cancel", handler:handleCancel }  ];

    var divObj = getElement(divID);
    if(divObj) divObj.style.display = '';

    var dialog;
    if(yahooDialogs[divID]){
        dialog = yahooDialogs[divID];
        dialog.show();
    }
    else{
        //dialog = new YAHOO.widget.Dialog(divID, { postmethod:"async", width:"360px", height:"400px", x:200, y:200, underlay:"none", draggable:false, fixedcenter:true } );
        dialog = new YAHOO.widget.Dialog(divID, { postmethod:"async", width:"360px", x:200, y:200, underlay:"none", draggable:false, fixedcenter:true } );
        yahooDialogs[divID] = dialog;
    }

    var onSuccess = function(obj) {
        try {
            var xmldoc = obj.responseXML;
            var root = xmldoc.getElementsByTagName('root').item(0);
            var status = root.getElementsByTagName("status")[0].firstChild.nodeValue;

            if(status == 'true'){
                dialog.form.reset();
                //dialog.hide();
                displayDialogStatusMessage(statusObj.id, STATUS_SUCCESS, MSG_DISPLAY_MESSAGE_SENT );

            }
            else{
                var desc = root.getElementsByTagName('description')[0].firstChild.nodeValue;
                displayDialogStatusMessage(statusObj.id, STATUS_ERROR, desc);
            }        
        }
        catch(ex){
            alert('There was an error sending your message! ' + ex.message);
        }
    }

    dialog.submit = function() {
        if(this.validate()){
            this.beforeSubmitEvent.fire();
            this.doSubmit();
            this.submitEvent.fire();
            return true;
        }
        else{
            return false;
        }
    };
    dialog.validate = function() { return inviteValidator(this.getData(), statusObj); }
    dialog.callback.success = onSuccess;
    dialog.callback.failure = onFailure;
    dialog.cfg.queueProperty("buttons", buttons);
    dialog.render();

    return false;
}

function inviteValidator(data, statusObj){
    if(data.txtTo.trim() == ''){
        displayDialogStatusMessage(statusObj.id, STATUS_ERROR, MSG_ERROR_REQUIRED_USERNAME );
        return false;
    }

    return true;
}

function messageValidator(data, statusObj){
    if(data.txtBody.trim() == ''){
        displayDialogStatusMessage(statusObj.id, STATUS_ERROR, MSG_ERROR_REQUIRED_MESSAGE_BODY );
        return false;
    }
    return true;
}

function friends_onCountryChange(req, country){
    if(country == '') friends_clearCities();
    else var cityAjax = new AjaxObj('get', '../friends/friendservice.php?req=' + req + '&country=' + escape(country), friends_onCityReceived);
}

function friends_onCityReceived(request){
    try{
        var xmldoc = request.responseXML;
        var root = xmldoc.getElementsByTagName('root')[0];
        var cityList = root.getElementsByTagName("cities")[0];
        var cities = cityList.getElementsByTagName("city");
        var ddl = getElement('ddlCity');
        friends_clearCities();
    
        for (var i = 0 ; i < cities.length ; i++) {
            var city = cities[i];
            var opt = document.createElement('option', opt);
            opt.value = city.firstChild.nodeValue;
            //opt.text = city.firstChild.nodeValue;
            opt.appendChild(document.createTextNode(city.firstChild.nodeValue));
            ddl.appendChild(opt);
        }
    }
    catch(ex){
        alert('Unable to retrieve cities for this country! ' + ex.message);
    }
}

function friends_clearCities(){
    var ddl = getElement('ddlCity');
    if(ddl){
        while(ddl.length > 1) ddl.remove(1);
    }
}

function friends_onAdd2FriendsClick(evt, toName){
    var lblTitle = getElement('lblInviteToTitle');
    var lblTo = getElement('lblInviteTo');
    var frm = document.forms['frmInvite'];	
    if(!frm) frm = document.frmInvite;
    
    frm.txtTo.value = toName;
    lblTitle.innerHTML = toName;
    lblTo.innerHTML = toName;

    var statusObj = getElement('lblInviteFriendStatus');
    inviteMessageBox('pnlInviteFriend', statusObj);
    
    return false;
}
