//vars used for hosting edit screen
var serverSharedIp = "";

var hosting_dispatch = function(){
    //setup environment for hosting
}

/**
 * Toggle selecting server in hosting editing screen
 */
function hostingToggleServer()
{
    if (Ext.getCmp('ctrlserverid').getValue() == 0) {
        Ext.getCmp('username').allowBlank = true;
        Ext.getCmp('domainname').allowBlank = true;
        Ext.getCmp('password').allowBlank = true;
        Ext.getCmp('ctrlavailableips').allowBlank = true;
        Ext.getCmp('ctrlavailableips').vtype = null
    } else {
        
        Ext.getCmp('username').allowBlank = false;
        Ext.getCmp('password').allowBlank = false;
        Ext.getCmp('domainname').allowBlank = false;
        Ext.getCmp('ctrlavailableips').allowBlank = false;
        Ext.getCmp('ctrlavailableips').vtype = "IPAddress";
        //has server so get ips
        serverId = Ext.getCmp('ctrlserverid').getValue();
        packageemanager.loadingMask.show();
        Ext.Ajax.request({
           url: 'index.php?fuse=admin&action=getAvailableIPAddresses',
           success: function(xhr) {
                //update dropdownlist
                var json = ce.parseAjaxResponse(xhr);
                var ctr_store = Ext.getCmp('ctrlavailableips').getStore();
                ctr_store.removeAll();
                ctr_store.loadData(json.data);
                serverSharedIp = json.sharedip;
                Ext.getCmp('ctrlusesharedip').setValue(1);
                hostingToggleUseSharedIP();
                packageemanager.loadingMask.hide();
           },
           params: {serverId: serverId, includeSharedIP: true, userPackageId: packageemanager.package_id}
        });
    }
    $('.fieldblock').each(function (index, domEle) {
         if (domEle.id != "fieldblock_serverid" && domEle.id != 'fieldblock_accountnotonserver') {
            if (Ext.getCmp('ctrlserverid').getValue() > 0) {
                domEle.style.display = "";
            } else {
                domEle.style.display = "none";
            }
         }
    });
   
}

/**
 * toggle available ip dropdownlist
 */
function hostingToggleAvailableIPs()
{
    Ext.getCmp('ipaddress').setValue(Ext.getCmp('ctrlavailableips').getRawValue());
}

/**
 * toggle using shared ip or not
 */
function hostingToggleUseSharedIP()
{    
    if (Ext.getCmp('ctrlusesharedip').getValue() == 0 || Ext.getCmp('ctrlusesharedip').getValue() == "No") {
        Ext.getCmp('ipaddress').setValue(' --- ');
        Ext.getCmp('ctrlavailableips').setDisabled(false);
        Ext.getCmp('ctrlavailableips').setValue(' - ' + lang('Select IP Address') + ' - ');
        Ext.getCmp('ctrlavailableips').clearInvalid();
    } else {
        Ext.getCmp('ctrlavailableips').setDisabled(true);
        Ext.getCmp('ctrlavailableips').setValue(' --- ');
        if (serverSharedIp=="") {
            serverSharedIp = $('#sharedip').val();
        }
        Ext.getCmp('ipaddress').setValue(serverSharedIp);
        //put in ip address the shared ip
    }
}