function admin_manage()
 {
     // Get our options and see if we're handling a form submission.
     global $_adsensem, $_adsensem_networks;
     $update_adsensem = false;
     /* Submissions from the manage ads listing at the top of the page */
     //if ( $_POST['adsensem-submit']=='manage' ) {
     switch ($_POST['adsensem-mode'] . ':' . $_POST['adsensem-action']) {
         case 'manage:copy unit':
             //Copy selected advert
             $copyto = adsensem_admin::validate_name($_POST['adsensem-action-target']);
             $_adsensem['ads'][$copyto] = adsensem_clone($_adsensem['ads'][$_POST['adsensem-action-target']]);
             //clone() php4 hack
             $_adsensem['ads'][$copyto]->name = $copyto;
             //update internal name reference
             $_adsensem['ads'] = adsensem_admin::sort_ads_by_network($_adsensem['ads']);
             $update_adsensem = true;
             break;
         case 'manage:delete unit':
             //Delete selected advert
             if ($_POST['adsensem-action-target'] != $_adsensem['default-ad']) {
                 unset($_adsensem['ads'][$_POST['adsensem-action-target']]);
                 $update_adsensem = true;
             }
             break;
         case 'manage:set default':
             //Set selected advert as default
             $_adsensem['default-ad'] = $_POST['adsensem-default-name'];
             $update_adsensem = true;
             break;
         case 'save:edit new':
             $name = adsensem_admin::validate_name($_POST['adsensem-name']);
             $_adsensem['ads'][$name] = new $_POST['adsensem-action-target']();
             //temporary to access network-specific functions
             $_adsensem['ads'][$name]->name = $name;
             //Update internal name reference (always, to ensure accuracy)
             $_adsensem['ads'][$name]->save_settings();
             $_POST['adsensem-mode'] = 'manage';
             $_POST['adsensem-action'] = '';
             $update_adsensem = true;
             break;
         case 'save:edit unit':
         case 'apply:edit unit':
             /* Changing the name of an Ad, copy and delete old */
             if ($_POST['adsensem-name'] != $_POST['adsensem-name-old']) {
                 $name = adsensem_admin::validate_name($_POST['adsensem-name']);
                 $_adsensem['ads'][$name] = adsensem_clone($_adsensem['ads'][$_POST['adsensem-name-old']]);
                 //$_adsensem['ads'][$name]->name=$name; //Update object-held name
                 unset($_adsensem['ads'][$_POST['adsensem-name-old']]);
                 /* We can now use the new $name from this point forward, lovely */
                 /* Update default if neccessary */
                 if ($_adsensem['default-ad'] == $_POST['adsensem-name-old']) {
                     $_adsensem['default-ad'] = $name;
                 }
                 $_adsensem['ads'][$name]->name = $name;
                 //Update internal name reference (always, to ensure accuracy)
             } else {
                 $name = stripslashes($_POST['adsensem-name']);
             }
             $_adsensem['ads'][$name]->save_settings();
             if ($_POST['adsensem-mode'] !== 'apply') {
                 //Only for Save (Apply leave as is, return to edit page)
                 $_POST['adsensem-mode'] = 'manage';
                 $_POST['adsensem-action'] = '';
             }
             //if($_adsensem['default-ad']==''){$_adsensem['default-ad']=$name;}
             $update_adsensem = true;
             break;
         case 'save:restore defaults':
             $temp = new $_POST['adsensem-action-target']();
             //temporary to access network-specific functions
             $temp->reset_defaults();
             $_POST['adsensem-mode'] = 'edit';
             $_POST['adsensem-action'] = 'edit defaults';
             $update_adsensem = true;
             break;
         case 'save:edit defaults':
             $temp = new $_POST['adsensem-action-target']();
             //temporary to access network-specific functions
             $temp->save_settings();
             $_adsensem['defaults'][$_POST['adsensem-action-target']] = $temp->p;
             $_POST['adsensem-mode'] = 'manage';
             $_POST['adsensem-action'] = '';
             $update_adsensem = true;
             break;
         case 'import:edit code':
             $ad = new Ad_Code();
             $ad->save_settings();
             //adsensem_admin::import_ad($_POST['adsensem-code']);
             $name = adsensem_admin::validate_name($_POST['adsensem-name'], $ad->network());
             $_adsensem['ads'][$name] = $ad;
             $_adsensem['ads'][$name]->name = $name;
             //Forces imported unit into the edit mode for changes
             $_POST['adsensem-action'] = 'edit unit';
             $_POST['adsensem-action-target'] = $name;
             $update_adsensem = true;
             $sort_adsensem = true;
             break;
         case 'import:edit unit':
             $ad = adsensem_admin::import_ad($_POST['adsensem-code']);
             $name = adsensem_admin::validate_name($_POST['adsensem-name'], $ad->network());
             $_adsensem['ads'][$name] = $ad;
             $_adsensem['ads'][$name]->name = $name;
             //Ensure that account-id passed to defaults when it does not match current submission
             //if($_adsensem['defaults'][$ad->network()]['account-id']!==$ad->p['account-id']){$_adsensem['defaults'][$ad->network()]['account-id']=$ad->p['account-id'];}
             //Forces imported unit into the edit mode for changes
             $_POST['adsensem-action'] = 'edit unit';
             $_POST['adsensem-action-target'] = $name;
             $update_adsensem = true;
             break;
         case 'import:edit defaults':
             $ad = adsensem_admin::import_ad($_POST['adsensem-code']);
             if ($ad->network == 'Ad_Code') {
                 $_POST['adsensem-action'] = '';
                 break;
             }
             $_adsensem['defaults'][$ad->network()] = $ad->p;
             //Forces imported unit into the edit mode for changes, to skip change mode to 'manage';
             $_POST['adsensem-action'] = 'edit defaults';
             $_POST['adsensem-action-target'] = $ad->network();
             $update_adsensem = true;
             break;
         case 'manage:edit unit':
             //Pass through stuff to switch modes between pages.
         //Pass through stuff to switch modes between pages.
         case 'manage:edit defaults':
         case 'import:edit new':
             $_POST['adsensem-mode'] = 'edit';
             break;
         default:
             $_POST['adsensem-mode'] = 'manage';
             $_POST['adsensem-action'] = '';
     }
     //End switch cases
     //Set default if possible
     if ($_adsensem['default-ad'] == '') {
         if (sizeof($_adsensem['ads'] !== 0)) {
             reset($_adsensem['ads']);
             $_adsensem['default-ad'] = key($_adsensem['ads']);
             $update_adsensem = true;
         }
     }
     //BUGFIX CLEANUP, REMOVE WHEN STABLE
     foreach ($_adsensem['ads'] as $n => $c) {
         if (get_class($c) == 'stdClass' || !is_object($c)) {
             unset($_adsensem['ads'][$n]);
             $update_adsensem = true;
         }
     }
     //END BUGFIX CLEANUP, REMOVE WHEN STABLE
     if ($update_adsensem) {
         $_adsensem['ads'] = adsensem_admin::sort_ads_by_network($_adsensem['ads']);
         update_option('plugin_adsensem', $_adsensem);
         $_GET['pagesub'] = '';
         //Show listing
     }
     if ($_POST['adsensem-mode'] == 'manage') {
         //Managing (i.e. not Importing, or editing)
         if ($_GET['pagesub'] == 'create_new' || sizeof($_adsensem['ads']) == 0) {
             adsensem_admin::admin_manage_create();
         } else {
             //Only if ads available
             adsensem_admin::admin_manage_table();
         }
     } else {
         //If in edit mode, output the editing form (create, edit, defaults, etc.)
         adsensem_admin::admin_manage_edit();
     }
 }