<?php

if( isset( $_GET['logout'] ) ) {
    update_option( 'wpspin_login', '' );
    update_option( 'wpspin_pass', '' );
}
    
if( ( $_POST['activate'] && $_POST['wpspin_login'] && $_POST['wpspin_pass'] ) OR ( get_option('wpspin_login') && get_option('wpspin_pass') ) ) {
    
    if( $_POST['activate'] ) {
        $login = stripslashes( $_POST['wpspin_login'] );
        $pass  = stripslashes( $_POST['wpspin_pass'] );
    } else {
        $login = get_option('wpspin_login');
        $pass  = get_option('wpspin_pass');
    }
    
    if( $this->mySession = $this->checkAuth( $login, $pass ) ) {
        if( $_POST['activate'] ) {
            update_option( 'wpspin_login', $login );
            update_option( 'wpspin_pass', $pass );
        }
    } else {
        echo "<div id='invalid'><font color='red'>Invalid username or password</font></div>";
        update_option( 'wpspin_login', '' );
        update_option( 'wpspin_pass', '' );
    }
    
}
    
if( $_POST['submit'] ) {

    if( isset( $_POST['wpspin_autospin'] ) ) {
        if( $_POST['wpspin_autospin'] == 1 ) {
            update_option( 'wpspin_autospin', '1' );
  
            if( $_POST['wpspin_spin_quality'] == "1" OR $_POST['wpspin_spin_quality'] == "2" OR $_POST['wpspin_spin_quality'] == "3" ) {
                update_option( 'wpspin_spin_quality', $_POST['wpspin_spin_quality'] );
            } else {
                update_option( 'wpspin_spin_quality', "1" );
            }
                
            if( isset( $_POST['wpspin_auto_options'] ) ) {
                switch( $_POST['wpspin_auto_options'] ) {
                    case "1":
                        update_option( 'wpspin_auto_options', '1' );
                        
                        if( $this->mySession ) {
                            $this->spinAll();
                        } else {
                            echo "<script>alert('Please activate WP Spin API');</script>";
                        }
                        
                        break;
                    case "2":
                        update_option( 'wpspin_auto_options', '2' );
                        break;
                    case "3":
                        update_option( 'wpspin_auto_options', '3' );
                        if( preg_match( "/[0-9]/", trim($_POST['wpspin_time_count']) ) ) {
                            update_option( 'wpspin_time_count', trim($_POST['wpspin_time_count']) );
                        } else { 
                            update_option( 'wpspin_time_count', "1" ); 
                        }
                        if( $_POST['wpspin_time_type'] == "1" OR $_POST['wpspin_time_type'] == "2" OR $_POST['wpspin_time_type'] == "3" OR $_POST['wpspin_time_type'] == "4") {
                            update_option( 'wpspin_time_type', $_POST['wpspin_time_type'] );
                        } else { 
                            update_option( 'wpspin_time_type', "1" ); 
                        }
                        if( isset( $_POST['wpspin_spin_count'] ) AND preg_match( "/[0-9]/", $_POST['wpspin_spin_count'] ) ) {
                            update_option( 'wpspin_spin_count', $_POST['wpspin_spin_count'] );
                        } else {
                            update_option( 'wpspin_spin_count', "0" );
                        }
                        if( isset( $_POST['wpspin_publish'] ) ) {
                            if( $_POST['wpspin_publish'] == '1' ) {
                                update_option( 'wpspin_publish', '1' );
                            } else {
                                update_option( 'wpspin_publish', '2' );
                            }
                        } else {
                            update_option( 'wpspin_publish', '1' );
                        }
                        break;
                    default:
                        update_option( 'wpspin_auto_options', '1' );
                }
            }
        } else {
            update_option( 'wpspin_autospin', '2' );
        }				
    } else {
        update_option( 'wpspin_autospin', '2' );
    }
        
    if( isset( $_POST['wpspin_spin_title'] ) ) {
        if( $_POST['wpspin_spin_title'] == 'on' ) {
            update_option( 'wpspin_spin_title', '1' );
        } else {
            update_option( 'wpspin_spin_title', '0' );
        }
    } else {
        update_option( 'wpspin_spin_title', '0' );
    }
        
    if( isset( $_POST['wpspin_excluded_words'] ) ) {
        $words = explode( "\n", $_POST['wpspin_excluded_words'] );
        $clean = "";
            
        foreach( $words as $word ) {
            $w = stripslashes( trim( $word ) );
            $clean.= "$w\n";
        }
        update_option( 'wpspin_excluded_words', $clean );
    }
        
}

$this->getOptions();
//$this->spinSchedule();

?>
<script>
function horror1($go) {
    if($go) { jQuery("#horror1").css("display","none"); }
    else { jQuery("#horror1").css("display","block"); }
}

function horror2($go) {
    if($go == 3) jQuery("#horror2").css("display","block");
    else {
        jQuery("#horror2").css("display","none");
        if( $go == 1 ) jQuery('#submit').attr('value','Spin All Posts!');
        else jQuery('#submit').attr('value','Save Changes');
    }
}

function logout() {
    jQuery('#loginbar').html('<img src="images/loading.gif" border="0"> Try to logout ...');
    jQuery.ajax({  
        type: "GET",  
        url: "<?php echo WP_PLUGIN_URL; ?>/wpspin/ajax.php", 
        data: "logout=1",
        success: function(get) {
            jQuery('#wpspin_login').removeAttr('disabled');
            jQuery('#wpspin_pass').removeAttr('disabled');
            jQuery('#wpspin_login').attr('value','');
            jQuery('#wpspin_pass').attr('value','');
            jQuery('#loginbar').html('<p class="submit"><input type="button" onclick="checklogin();" value="Activate account" /></p>');
        }
    });
    return false;
}

function checklogin() {
    var login = jQuery('#wpspin_login').val();
    var pass = jQuery('#wpspin_pass').val();
    jQuery('#loginbar').html('<img src="images/loading.gif" border="0"> Try to login ...');
    jQuery.ajax({  
        type: "POST",  
        url: "<?php echo WP_PLUGIN_URL; ?>/wpspin/ajax.php", 
        data: "login="+login+"&pass="+pass,
        success: function(get) {
            if( get == '1' ) {
                jQuery('#wpspin_login').attr('disabled', 'disabled');
                jQuery('#wpspin_pass').attr('disabled', 'disabled');
                jQuery('#wpspin_login').attr('value',login);
                jQuery('#wpspin_pass').attr('value',pass);
                jQuery('#invalid').html('');
                jQuery('#loginbar').html('<b><font color="green">Successful login</font></b> (<a href="#" onclick="logout();">logout</a>)<br><br>');
            } else if( get == '28' ) {
                jQuery('#wpspin_login').removeAttr('disabled');
                jQuery('#wpspin_pass').removeAttr('disabled');
                jQuery('#wpspin_login').attr('value',login);
                jQuery('#wpspin_pass').attr('value',pass);
                jQuery('#invalid').html('<font color="red">Server busy, try again later</font>');
                jQuery('#loginbar').html('<p class="submit"><input type="button" onclick="checklogin();" value="Activate account" /></p>');
            } else {
                jQuery('#wpspin_login').removeAttr('disabled');
                jQuery('#wpspin_pass').removeAttr('disabled');
                jQuery('#wpspin_login').attr('value','');
                jQuery('#wpspin_pass').attr('value','');
                jQuery('#loginbar').html('<p class="submit"><input type="button" onclick="checklogin();" value="Activate account" /></p>');
            }
        }
    });
    return false;
}

</script>

<style>
select {
    text-align: center;
}
input {
    text-align: center;
}
</style>

<div class="wrap"><div id='invalid'></div>
<img src='<?php echo WP_PLUGIN_URL; ?>/wpspin/images/small_WPSpin.jpg' width="300" style='opacity: 0.8;'><br><br>
<h3>Activate <a href='http://www.wpspin.com/BestSpinnerDiscount'>The Best Spinner</a> here:</h3>
<table width="300">
<tr>
<td><?php echo __('Login','wpspin'); ?></td><td><input type="text" id='wpspin_login' <?php if( $this->mySession ) echo "disabled"; ?>  value="<?php echo $this->option['wpspin_login']; ?>" size="25"></td>
</tr>
<td><?php echo __('Password','wpspin'); ?></td><td><input type="password" id='wpspin_pass' <?php if( $this->mySession ) echo "disabled"; ?> value="<?php echo $this->option['wpspin_pass']; ?>" size="25"></td>
</tr>
</table>
<div id='loginbar'>
<?php if( $this->mySession ) echo "<b><font color='green'>Successful login</font></b> (<a href='#' onclick='logout();'>logout</a>)<br><br>";
else echo '<p class="submit"><input type="button" onclick="checklogin();" value="Activate account" /><br></p>';?>
</div>
<form method="post" action="<?php echo $_SERVER['REQUEST_URI'];?>">
<h4><font size='2'><?php echo __('Sitewide API Spin Settings','wpspin'); ?></font></h4>
<table cellpadding="5" cellspacing="4" width="800">
<tr><td><input name="wpspin_autospin" onClick="horror1(1);" type="radio" value='2' <?php if($this->option['wpspin_autospin']=='2' OR !$this->option['wpspin_autospin'])echo "checked";?>> </td><td> <?php echo __('Individual Post Spinning only (only use spin functions in editor)','wpspin'); ?></td></tr>
<tr><td>&nbsp</td><td>&nbsp</td></tr>
<tr><td><input name="wpspin_autospin" onClick="horror1(0);" type="radio" value='1' <?php if($this->option['wpspin_autospin']=='1')echo "checked";?>> </td><td> <?php echo __('Auto Spin All Posts','wpspin'); ?></td></tr>
<tr><td>&nbsp</td><td><div id='horror1' style='display:<?php if($this->option['wpspin_autospin']=='1')echo "block"; else echo "none"; ?>'>
<table cellpadding="5" cellspacing="4" width="800">
<tr><td>&nbsp</td><td><?php _e('Quality') ?>: <select name='wpspin_spin_quality' style='width: 100px'><?php
$option = str_replace( "value='".$this->option['wpspin_spin_quality']."'", "value='".$this->option['wpspin_spin_quality']."' selected", "<option value='1'>Good</option><option value='2'>Better</option><option value='3'>Best</option>" );
echo $option; ?></select></td></tr><tr><td>&nbsp</td></tr>
<tr><td><input name="wpspin_auto_options" value="1" onClick="horror2(1);" type="radio" <?php if($this->option['wpspin_auto_options']=='1')echo "checked";?>> </td><td> <?php echo __('Spin Once (Post article will Only by Spun Once and Published)','wpspin'); ?></td></tr>
<tr><td><input name="wpspin_auto_options" value="2" onClick="horror2(2);" type="radio" <?php if($this->option['wpspin_auto_options']=='2')echo "checked";?>> </td><td> <?php echo __('Spin When Viewed (Post article will Show New version each time Viewed)','wpspin'); ?></td></tr>
<tr><td><input name="wpspin_auto_options" value="3" onClick="horror2(3);" type="radio" <?php if($this->option['wpspin_auto_options']=='3')echo "checked";?>> </td><td> <?php echo __('Schedule Post Updates (Will Update and Replace Posts with New Version)','wpspin'); ?></td></tr>
<tr><td>&nbsp</td><td><div id='horror2' style='display:<?php if($this->option['wpspin_auto_options']=='3')echo "block"; else echo "none"; ?>'><input type="text" name="wpspin_time_count" size="2" value="<?php echo $this->option['wpspin_time_count'];?>"><select name="wpspin_time_type" style="width: 150px"><option value="1" <?php if($this->option['wpspin_time_type']=='1')echo "selected";?>><?php _e('Hours') ?></option><option value="2" <?php if($this->option['wpspin_time_type']=='2')echo "selected";?>><?php _e('Days') ?></option><option value="3" <?php if($this->option['wpspin_time_type']=='3')echo "selected";?>><?php _e('Weeks') ?></option><option value="4" <?php if($this->option['wpspin_time_type']=='4')echo "selected";?>><?php _e('Months') ?></option></select><br><br>
<select name="wpspin_spin_count">
<?php for($i=1;$i<11;$i++) {
    if( $this->option['wpspin_spin_count'] == $i ) echo "<option value='$i' selected>$i</option>";
    else echo "<option value='$i'>$i</option>";
}
?></select> <?php _e('Number of Post Updates to Schedule') ?><br><br>
<table width="300"><tr><td><input name="wpspin_publish" type="radio" value="1" <?php if($this->option['wpspin_publish']=='1')echo "checked";?>> <?php echo __('Auto publish','wpspin'); ?></td><td><input name="wpspin_publish" type="radio" value="2" <?php if($this->option['wpspin_publish']=='2')echo "checked";?>> </td><td> <?php echo __('Publish as Draft','wpspin'); ?>
</td></tr></table></div></td></tr>
</table><br></div></td></tr>
<tr><td>&nbsp</td><td>&nbsp</td></tr>
<tr><td><input type='checkbox' name='wpspin_spin_title' <?php if($this->option['wpspin_spin_title']=='1')echo "checked";?>></td><td> Spin Titles</td></tr></table>
<br>
<?php _e('Excluded words') ?>:<br>
<textarea rows="10" cols="35" name="wpspin_excluded_words"><?php
if( $this->option['wpspin_excluded_words'] ) { echo $this->option['wpspin_excluded_words']; } ?></textarea><br>
<!--<p class="submit"><input type="submit" id='submit' style='width:117px' name="submit" value="Save Changes" /></p>-->
<br>
<input type="submit" id='submit' style='width:140px !important;background-color:white !important;font-size:12px !important;height:32px !important' name="submit" value="Save Changes" />
</form>
</div>