<?php
/*
License: Copyright 2010 Daniel Watrous, All Rights Reserved (see README.txt in the plugin directory)
*/

global $unwanted_useragents;
$unwanted_useragents = array(
	"iPhone",  			// Apple iPhone
	"iPod", 			// Apple iPod touch
	"iPad", 			// Apple iPod touch
	"Android", 			// 1.5+ Android
	"dream", 			// Pre 1.5 Android
	"CUPCAKE", 			// 1.5+ Android
	"blackberry9500",	// Storm
	"blackberry9530",	// Storm
	"blackberry9520",	// Storm	v2
	"blackberry9550",	// Storm v2
	"blackberry9800",	// Torch
	"webOS",			// Palm Pre Experimental
	"incognito", 		// Other iPhone browser
	"webmate" 			// Other iPhone browser
);

// browser detection to prevent showing the footer in IE6 and apple mobile products
function detect_mobile_browser() {
	global $unwanted_useragents;
	$unwanted_mobile = false;
	$container = $_SERVER['HTTP_USER_AGENT'];
	foreach ($unwanted_useragents as $useragent) {
		if (preg_match( "#$useragent#i", $container )) {
			$unwanted_mobile = true;
			break;
		}
	}
	return $unwanted_mobile;
}

function detect_ie() {
	if (isset($_SERVER['HTTP_USER_AGENT']) &&
	(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6.') !== false))
		return true;
	else
		return false;
}

// check page/post whether or not the optincrusher will show
function exclude_page($post_id) {
	// assume that the $post_id isn't excluded
	$exclude_this_post_id = false;
	$exclude_pages =  get_option('optincrusher_excludepages');
	// check to see if there are any exclude pages
	if ($exclude_pages !== false && !empty($exclude_pages)) {
		// exclude if this is a page or post and it's ID is in $exclude_pages
		$exclude_this_post_id = ((is_single($post_id) || is_page($post_id)) && in_array($post_id, explode(',', $exclude_pages))) ? true:false;
	}
	// finally return
	return $exclude_this_post_id;
}

// form generation functions
function optincrusher_dropdown($name, $data, $option="") {
   if (get_option($name)) { $option = get_option($name); }

   ?>
   <select name="<?php echo $name ?>">
   <?php

   // If the array is a vector (0, 1, 2...)
   if (is_vector($data)) {
      foreach ($data as $item) {
         if ($item == $option) {
            echo '<option selected="selected">' . $item . "</option>\n";
         }
         else {
            echo "<option>$item</option>\n";
         }
      }
   }

   // If the array contains name-value pairs
   else {
      foreach ($data as $value => $text) {
         if ($value == $option) {
            echo '<option value="' . $value . '" selected="selected">' . $text . "</option>\n";
         }
         else {
            echo '<option value="' . $value . '">' . "$text</option>\n";
         }
      }
   }

   ?>
   </select>
   <?php
}

function optincrusher_textbox($name, $value="", $size=15) {
   if (get_option($name)) { $value = get_option($name); }

   ?>
   <input type="text" name="<?php echo $name ?>" size="<?php echo $size ?>" value="<?php echo $value ?>" />
   <?php
}

function optincrusher_radio($name, $values=array(), $selected=false, $include_break = false) {
   if (get_option($name)) { $selected = get_option($name); }
	foreach ($values as $option_name => $option_value) {
   ?>
   <label style="text-align: left; width: auto; padding-left: 12px;"><?php echo $option_name; ?> <input type="radio" name="<?php echo $name ?>" value="<?php echo $option_value ?>" <?php echo ($option_value==$selected) ? "checked":""; ?> /></label>
   <?php echo ($include_break) ? "<br />":""; ?>
   <?php
	}
}

function optincrusher_colorpickertextbox($name, $value="", $size=15) {
   if (get_option($name)) { $value = get_option($name); }

   ?>
   <input type="text" class="color" name="<?php echo $name ?>" size="<?php echo $size ?>" value="<?php echo $value ?>" />
   <?php
}

function optincrusher_textarea($name, $value="") {
   if (get_option($name)) { $value = get_option($name); }

   ?>
   <textarea name="<?php echo $name ?>" cols="50" rows="8"><?php echo $value ?></textarea>
   <?php
}

function optincrusher_checkbox($name) {
   ?>
   <?php if (get_option($name)): ?>
   <input type="checkbox" name="<?php echo $name ?>" checked="checked" />
   <?php else: ?>
   <input type="checkbox" name="<?php echo $name ?>" />
   <?php endif; ?>
   <?php
}


?>