/**
 * Update The Slide styles on the SimpleReach site
 *
 * @author Eric Lubow <*****@*****.**>
 * @param Array $styles Style to be converted to JSON
 * @return String OK or error string
 */
function srslide_update_slide_styles()
{
    // Pull all the necessary options
    // Setup our POST parameters
    $params = array();
    $params['ts'] = (int) gmdate('U');
    $params['pid'] = get_option('srslide_pid');
    $srslide_api_key = get_option('srslide_api_key');
    // Handle if the user's API key isn't saved in WP
    if (strlen($srslide_api_key) == 32) {
        $params['api_key'] = get_option('srslide_api_key');
    } else {
        $params['login'] = get_option('srslide_sr_login');
        $params['pass'] = get_option('srslide_sr_pass');
    }
    $params['style'] = get_option('srslide_styles');
    if (!srslide_validate_hex_color($params['style']['link']['color'])) {
        return __('Please specify a valid link color which starts with a "#" and has 6 of the following characters [A-F], [0-9]', 'srslide');
    }
    $encoded_params = http_build_query($params);
    $url = 'https://www.simplereach.com/publisher/generate_slide_css';
    $response = srslide_post_url($url, $encoded_params);
    // Decode the JSON
    $resp = json_decode($response);
    if ($resp === NULL) {
        return __('There was an http error contacting SimpleReach. Please try saving your options again.', 'srslide');
    }
    if ($resp->success) {
        if ($resp->api_key && !srslide_validate_api_key(get_option('srslide_api_key'))) {
            update_option('srslide_api_key', $resp->api_key);
        }
        return 'OK';
    } else {
        $errorResponse = !empty($resp->error) ? strtolower($resp->error) : '';
        $errorFooter = sprintf(__("Please try again or contact <a href='mailto:%s'>SimpleReach support</a> if you believe you have received this message in error.", 'srslide'), SRSLIDE_PLUGIN_SUPPORT_EMAIL);
        switch ($errorResponse) {
            case 'incorrect timestamp':
                $errorMessage = __('There was an error while you were attempting to register.', 'srslide');
                break;
            case 'invalid pid':
                $errorMessage = __('The PID (publisher ID) that you are attempting to sign in with does not exist.', 'srslide');
                break;
            case 'invalid user':
                $errorMessage = __('The user name that you are attempting to sign in with does not exist.', 'srslide');
                break;
            case 'invalid password':
                $errorMessage = __('The user name that you are attempting to register with already exists, but the password you entered was incorrect.', 'srslide');
                break;
            case 'invalid api key':
                $errorMessage = __('The API key that you are using does not have access to this account or is invalid.', 'srslide');
                break;
            case 'invalid user on account':
                $errorMessage = __('The user name that you are registered with does not have access to this account.', 'srslide');
                break;
            default:
                $errorMessage = __('There was an error contacting SimpleReach. Please try saving your options again.', 'srslide');
                break;
        }
        return $errorMessage . " " . $errorFooter;
    }
}
         } else {
             $srslide_css_url = str_replace(array('"', "'", '<'), array(''), $srslide_css_url);
         }
         if (!$url_error) {
             update_option('srslide_css_option', $srslide_css_option);
             update_option('srslide_css_url', $srslide_css_url);
         }
     }
 }
 // Validate link color
 if ($srslide_style_link_color == 'other') {
     if (!srslide_validate_hex_color($srslide_style_link_color_text)) {
         $errors[] = __("The link color specified is not a valid hex color.", 'srslide');
     }
 } else {
     if (!srslide_validate_hex_color($srslide_style_link_color)) {
         $errors[] = __("The link color specified is not a valid hex color.", 'srslide');
     }
 }
 // If we have no errors, validate api_key and pid
 if (!$errors) {
     // only make api request if api_key and/or pid has changed.
     if ($srslide_api_key != get_option('srslide_api_key') || $srslide_pid != get_option('srslide_pid')) {
         // TODO Write comparison function to see if styles that need to be
         // sent to CDN haven't been changed. Skip CURL if there was no change
         $encoded_params['api_key'] = $srslide_api_key;
         $encoded_params['pid'] = $srslide_pid;
         $encoded_params['ts'] = (int) gmdate('U');
         // Curl call for validation
         $url = 'http://simplereach.com/wordpress/validate';
         $response = srslide_post_url($url, $encoded_params);