//dispatch sets up setting panel for panel type of domaininfo
var domaininfo_dispatch = function(){
    if ( canaddeditpackages == true ) {
        btn = $('<div style="float:right;padding-left:2px;" id="divassignorderid"></div>')
        $('#textfield_registrarorderid').append(btn);
        new Ext.Button({
             id: 'reassignorderid',
             renderTo : 'divassignorderid',
             width: 20,
             cls: 'x-box-text-small',
             text: lang('Reassign'),
             listeners: {
                 click : function (btn,event) {
                     Ext.MessageBox.prompt('Assign Order Id', 'Please enter the order id:', domaininfoAssignOrderId);
                 }
             }
        });
    }
    domaininfoToggleRegistrar();

    // Work out what fields we have and their values so we can show an error message if required
    showErrorBox = false;
    isTransferring = false;
    for(var iterator = 0; iterator < packageemanager.product_store_general.data.length; iterator++){
        var currentFieldData = packageemanager.product_store_general.getAt(iterator).data;

        // Check if we have an Unknown returned
        if(currentFieldData.id == 'purchasestatus' && currentFieldData.value == 'Unknown') {
            showErrorBox = true;
        }

        // Only way we can check for a domain transfer is look for EPP Code
        if(currentFieldData.id == 'eppCode') {
            isTransferring = true;
        }
    }

    // Do we have something to show
    if(showErrorBox && document.getElementById('panelErrorBox')) {
        if(isTransferring) {
            // Set text to transfer
            document.getElementById('panelErrorBox').innerHTML = 'Unable to get domain information, this is typically because the domain transfer has not yet completed.';
        } else {
            // Set default text
            document.getElementById('panelErrorBox').innerHTML = 'Unable to get domain information, possibly because of a communication error with the registrar.';
        }
        // Show the box
        document.getElementById('panelErrorBox').style.visibility = 'visible';
        document.getElementById('panelErrorBox').style.display = 'block';
    }
}

//perform action after assigning an existing order id
function domaininfoAssignOrderId(btn, text){
    if (btn == "cancel") return;
    
    //add hidden field for new orderid
    $('#productsettingsform').append("<input type='hidden' name='neworderid' value='"+text+"'></input>");
    Ext.getCmp('registrarorderid').setValue(text);
    //Ext.getCmp('reassignorderid').setVisible(false);
    domaininfoToggleRegistrar();
    //now submit as a save
    $('#btnUpdateProduct').click();
    
    //if text was "" then we might have to add some logic to show register button once more
    if (text == "") {
        Ext.getCmp('registrarorderid').setValue(lang("Unknown"));
        Ext.getCmp('registartionstatus').setValue("Not Yet Registered");
        domaininfoToggleRegistrar();
    }
    
}

function domaininfoToggleRegistrar() {
    if ( (Ext.getCmp('ctrlregistrar').getValue() != "0") && (Ext.getCmp('registrarorderid').getValue() == lang("Unknown")) )
    {
        //$('#btn_registerdomain').show(); should be able to register
        $('#fieldblock_registrarorderid').show();
        $('#fieldblock_registartionstatus').show();
        
        //if we have an order id but status is still not yet registered
        //change the order id to invalid as apparently we still need to get
        //the valid order id
        if (Ext.getCmp('registrarorderid').getValue() != lang("Unknown")) {
            Ext.getCmp('registrarorderid').setValue(lang("Invalid"));
        }
        
    } else {
        if (Ext.getCmp('ctrlregistrar').getValue() == "0") {
            $('#fieldblock_registrarorderid').hide();
            $('#fieldblock_registartionstatus').hide();
            
        } else {
            $('#fieldblock_registartionstatus').show();
            $('#fieldblock_registrarorderid').show();    
        }
        //$('#btn_registerdomain').hide(); shouldn't be able to register
    }
    
    //if we have an orderid then set to manually registered if status is Not Yet Registred
     if ( (Ext.getCmp('registartionstatus').getValue() == lang("Not Yet Registered")) &&
         (Ext.getCmp('registrarorderid').getValue() != lang("Unknown")) ){
         Ext.getCmp('registartionstatus').setValue("Registered");
     }
    
}