// account.js
function account_populate_vals(oSrcData, oIdList) {
    for (var i in oIdList) {
        aList = oIdList[i];
        if ( aList.length ) {
            for ( lp = 0; lp < aList.length; lp++ ) {
                var datav = document.getElementById(aList[lp] + ".datav");
                var datai = document.getElementById(aList[lp] + ".datai");
                if ( datav ) {
                    if ( datav.nodeName == 'INPUT' ) {
                        datav.value = eval("oSrcData." + aList[lp]);
                    } else {
                        datav.innerHTML = eval("oSrcData." + aList[lp]);
                    }
                }
                if ( datai ) { alert(datai); }
            }
        }
    }
}

function showBillto () {
    showPanel('bill','');
}

function showShipto () {
    showPanel('ship','');
}

function showEditAccount () {
    showPanel('account','');
}

/**
 * This function opens the edit form for the selected device holder.
 */
function showEditDeviceHolder(deviceHolderNo, device_id){
    var extra = '';
    device_id = device_id + '';
    if (device_id && (/^\d+$/.test(device_id))) {
        extra = '&device_id=' + device_id;
    };
    showPanel('holder',deviceHolderNo + extra);
}

/**
 *  This function opens the edit form for the selected caregiver.
 */
function showEditcaregiver(caregiverNo, device_holder_id){
    showPanel('caregiver',caregiverNo +'&device_holder_id='+device_holder_id);
}

function showRemovecaregiver (caregiver_id, device_holder_id) 
{
    if (confirm('Are you sure you want to remove this caregiver?')) 
    {
        var postData = "caregiver_id=" + caregiver_id + '&device_holder_id=' + device_holder_id;
        var callback = {
            success: function(o) {
                var response = o.responseXML;
                
                var deleted = response.getElementsByTagName('deleted')[0];
                if (deleted.firstChild.nodeValue == '1') 
                {
                    window.location.reload();
                }
                else
                {
                    var body = response.getElementsByTagName('message')[0];
                    if (typeof error_remove_caregiver != 'undefined')
                    {
                        try
                        {
                            error_remove_caregiver.destroy();
                        }
                        catch(e)
                        {
                            error_remove_caregiver = null;
                        }
                        
                    };
                    
                    error_remove_caregiver = new YAHOO.widget.Panel("error_remove_caregiver", {
                        width:"400px", 
                        fixedcenter: true, 
                        constraintoviewport: true, 
                        underlay:"shadow", 
                        close:true, 
                        visible:false, 
                        draggable:true,
                        modal: true
                    });
                    error_remove_caregiver.setBody(body.firstChild.nodeValue + '<br><a href="#" onclick="error_remove_caregiver.destroy();return false;">Close</a>');
                    error_remove_caregiver.render(document.body);
                    error_remove_caregiver.show();
                }
                
            },
            failure: function (o) {}
        };

        YAHOO.util.Connect.asyncRequest(
            'POST',
            '/account/removeCaregiver',
            callback,
            postData);
    };
}

/**
 * This function receives the method to call and if the received metod is
 * deviceHolder or caregiver then it receives the id of the object to edit.
 * @param m This is the method to call: ship, bill, account, deviceHolder or caregiver.
 * @param id This is the id of the deviceHolder or the caregiver to edit.
 */
function showPanel (m, id)
{
    panelWidth = '400px';
    // execute the scripts in the response
    var script = true;
    switch (m) {
        case 'addCaregiver':
            method = 'login?editPage=addCaregiver&id='+id;
            script = true;
        break;
        case 'ship':
            method = 'login?editPage=editShipto';
        break;
        case 'bill':
            method = 'login?editPage=editBillto';
        break;
        case 'account':
            method = 'login?editPage=editAccount';
        break;
        case 'holder':
            method = 'login?editPage=deviceHolder&id='+id;
            panelWidth = '750px';
        break;
        case 'caregiver':
            method = 'login?editPage=caregiver&id='+id;
        break;
        default:
            method = 'error';
        break;
    }

    if (typeof edit_account_overlay != 'undefined'
    && (typeof edit_account_overlay == 'function')) { edit_account_overlay.destroy(); };

    edit_account_overlay = new YAHOO.widget.Panel("edit_account_overlay", {
        // width: panelWidth,
        // height: '400px',
        fixedcenter: 'contained',
        constraintoviewport: true,
        underlay: "shadow",
        close: false,
        visible: false,
        draggable: false,
        modal: true,
        effect: { effect:YAHOO.widget.ContainerEffect.FADE, duration: 0.25 }
    });

    var postData = '';
    var callback = {
        success: function(o) {
            edit_account_overlay.setBody('<div id="edit_account_overlay_replace"></div>');

            edit_account_overlay.render(document.body);
            
            YAHOO.util.Dom.get('edit_account_overlay_replace').innerHTML = o.responseText;
            if (script) {
                var overlay = YAHOO.util.Dom.get('edit_account_overlay');
                var scripts = overlay.getElementsByTagName('script');
                for(i = 0, ii = scripts.length; i < ii; i++)
                {
                    eval(scripts[i].innerHTML);
                }
            };
            edit_account_overlay.show();
        },
        failure: function (o) {
            edit_account_overlay.hide();
            edit_account_overlay.destroy();
        }
    };

    YAHOO.util.Connect.asyncRequest(
        'GET',
        '/account/' + method,
        callback,
        postData);
}

function editAccountComplete () 
{
    if (edit_account_overlay)
    {
        edit_account_overlay.hide();
    };

    window.reload();
}

AdditionalEntities =
{
    init : function()
    {
        var links = AdditionalEntities.getLinksByClassName('additionalCaregiver');
        for(i = 0, ii = links.length; i < ii; i++)
        {
            YAHOO.util.Event.addListener(links[i], 'click', AdditionalEntities.addCaregiverListener);
        }
        
        var add_dh_links = AdditionalEntities.getLinksByClassName('addDeviceHolder');
        for(i = 0, ii = add_dh_links.length; i < ii; i++)
        {
            YAHOO.util.Event.addListener(add_dh_links[i], 'click', AdditionalEntities.addDeviceHolderListener);
        }
    },

    getLinksByClassName : function(className)
    {
        var links = document.getElementsByTagName('a');
        var matches = [];
        for(i = 0, ii = links.length; i < ii; i++)
        {
            if (YAHOO.util.Dom.hasClass(links[i], className)) {
                matches[matches.length] = links[i];
            };
        }

        return matches;
    },

    addCaregiverListener : function(event)
    {
        var id = this.getAttribute('rel');
        AdditionalEntities.showAddCaregiver(id);
        event.preventDefault();
    },

    showAddCaregiver: function(id)
    {
        showPanel('addCaregiver', id);
    },
    
    addDeviceHolderListener: function(event)
    {
        var device_id = this.getAttribute('rel');
        showEditDeviceHolder('-1', device_id);
        event.preventDefault();
    }
};

YAHOO.util.Event.onDOMReady(function()
{
    AdditionalEntities.init();
});