// Handle date fields to convert them to the extjs form
var datefields = new Array();

Ext.onReady(function(){
    //loop the dates and create extjs date fields from them
    for(x = 0; x < datefields.length; x++){
        var datefield = new Ext.form.DateField({
            applyTo :       datefields[x],
            hideLabel:      true,
            height:         "5px",
            selectOnFocus:  true,
            format :        gDateFormat
        });
    }
    
    var statuscombo = new Ext.form.ComboBox({
        id:             'statuscombo',
        typeAhead:      false,
        editable:       false,
        triggerAction:  'all',
        transform:      'dropdown_adminstatus',
        width:          80,
        x:              "-8px",
        forceSelection: true,
        listeners:{
            'select': function(cb,record){
                UpdateStatus(record.data.value, record.data.text);
            },
            'render': function(cb){
                cb.setVisible(false);
            }
        }
    });
    
    var groupcombo = new Ext.form.ComboBox({
        id:             'groupcombo',
        typeAhead:      false,
        editable:       false,
        triggerAction:  'all',
        transform:      'dropdown_adminGroup',
        width:          200,
        x:              "-8px",
        forceSelection: true,
        listeners:{
            'select': function(cb,record){
                UpdateGroup(record.data.value, record.data.text);
            },
            'render': function(cb){
                cb.setVisible(false);
            }
        }
    });
});

function replaceString(oldS, newS, fullS)
{
    for(var i = 0; i < fullS.length; i++){
        if(fullS.substring(i, i + oldS.length) == oldS){
            fullS = fullS.substring(0, i) + newS + fullS.substring(i + oldS.length, fullS.length);
        }
    }
    
    return fullS;
}

function fixaddress(myfield)
{
    myfield.value = replaceString(" ", "", myfield.value);
    myfield.value = myfield.value.toLowerCase();
}

function checkPassword(form)
{
    if(form.confirm_password.value != ""
      && form.new_password.value != ""){
        if(form.confirm_password.value != form.new_password.value){
            alert(lang("Your confirmation password does not match your new password"));
            return (false);
        }
    }else{
        if(form.confirm_password.value != ""
          || form.new_password.value != ""){
            alert(lang("You must fill all password fields if you wish to change this password"));
            return (false);
        }
    }
    
    return (true);
}

function onChangeDepartment(combo)
{
    // always keep the general department selected
    combo.options[0].selected = true;
}

function UpdateStatus(newstatus, statusname)
{
    if(newstatus == oldstatus){
        return false;
    }
    
    ToggleStatus();
    
    Ext.Ajax.request({
        url:      "index.php?fuse=admin&action=UpdateAdminStatus",
        success:  function(xhr) {
            var data = Ext.util.JSON.decode(xhr.responseText);
            
            if(data.error){
                Ext.getCmp('statuscombo').setValue(oldstatus);
                
                //we do not need to repopulate the filters if there was a validation error
                if(data.message){
                    msg(data.message, "");
                }else{
                    msg(lang("There was an error when updating your status"), "");
                }
            }else{
                if(data.message){
                    msg(data.message, "");
                }else{
                    msg(lang("Status has been updated successfully"), "");
                }
                
                oldstatus = newstatus;
                $('label_adminstatus_text').innerHTML = statusname;
                
                switch(newstatus){
                    case '1':
                        $('profile-status').className = "profile-status-active";
                        break;
                    case '-1':
                        $('profile-status').className = "profile-status-inactive";
                        break;
                }
            }
        },
        failure:  function() {
            Ext.Msg.alert("Fail");
        },
        
        params:   {
            adminId:  adminid,
            statusid: newstatus
        }
    });
}

function ToggleStatus()
{
    if($('label_adminstatus').style.display == "none"){
        $('label_adminstatus').style.display = "";
        Ext.getCmp('statuscombo').setVisible(false);
    }else{
        $('label_adminstatus').style.display = "none";
        Ext.getCmp('statuscombo').setVisible(true);
    }
}

function UpdateGroup(newgroup, groupname)
{
    if(newgroup == oldgroup){
        return false;
    }
    
    ToggleGroup();
    
    Ext.Ajax.request({
        url:      "index.php?fuse=admin&action=UpdateAdminGroup",
        success:  function(xhr) {
            var data = Ext.util.JSON.decode(xhr.responseText);
            
            if(data.error){
                Ext.getCmp('groupcombo').setValue(oldgroup);
                
                //we do not need to repopulate the filters if there was a validation error
                if(data.message){
                    msg(data.message, "");
                }else{
                    msg(lang("There was an error when updating the group"), "");
                }
            }else{
                if(data.message){
                    msg(data.message, "");
                }else{
                    msg(lang("The group has been updated successfully"), "");
                }
                
                oldgroup = newgroup;
                $('profile-group-label_text').innerHTML = groupname;
            }
        },
        failure:  function() {
            Ext.Msg.alert("Fail");
        },
        params:   {
            adminId:  adminid,
            groupid:  newgroup
        }
    });
}

function ToggleGroup()
{
    if($('profile-group-label').style.display == "none"){
        $('profile-group-label').style.display = "";
        Ext.getCmp('groupcombo').setVisible(false);
    }else{
        $('profile-group-label').style.display = "none";
        Ext.getCmp('groupcombo').setVisible(true);
    }
}