<?php
/*
TODO:
	options page
	option: sort order
	option: select which as in list
	tidy
*/
function squirrelmail_plugin_init_ccontacts()
{
  global $squirrelmail_plugin_hooks;

  $squirrelmail_plugin_hooks['compose_button_row']['ccontacts'] = 'ccontacts_display_list';

  //$squirrelmail_plugin_hooks['menuline']['ccontacts'] = 'ccontacts_link';
  //$squirrelmail_plugin_hooks['options_display_inside']['ccontacts'] = 'ccontacts_options';
  //$squirrelmail_plugin_hooks['options_display_save']['ccontacts'] = 'ccontacts_save';
  //$squirrelmail_plugin_hooks['loading_prefs']['ccontacts'] = 'ccontacts_load';

}

function ccontacts_sort($a, $b)
{
	$i = strcmp($a['firstname'], $b['firstname']);
	if ($i == 0)
		$i = strcmp($a['lastname'], $b['lastname']);
	return $i;
}

function ccontacts_display_list()
{
	$abook = addressbook_init(true, true);
	if($abook->localbackend == 0)
	{
		echo "No address book";
		return;
	}
	$alist = $abook->list_addr();
	if(!is_array($alist))
	{
		echo "Address is not array";
		return;
	}
	if (count($alist) == 0)
	{
		echo "Address is empty";
		return;
	}

	usort($alist, 'ccontacts_sort');
	$select_list = "";
	while(list($undef,$row) = each($alist))
	{
		$name = htmlspecialchars($row['firstname']) . " " . htmlspecialchars($row['lastname']);
		$email = htmlspecialchars($row['email']);
		$select_list .= "<option value='".$email."'>".$name."</option>";
	}

	echo "<table><tr><td>";

	echo "<table>".
		"<tr><td>"._("To")."</td><td><input type=radio name=ccontacts_field value=to checked></td></tr>".
		"<tr><td>"._("Cc")."</td><td><input type=radio name=ccontacts_field value=cc></td></tr>".
		"<tr><td>"._("Bcc")."</td><td><input type=radio name=ccontacts_field value=bcc></td></tr>".
		"<tr><td>"._("WhatIs")."</td><td><input type=radio name=ccontacts_field value=whatis></td></tr>".
		"</table>";
	echo "</td><td>";

	echo "<select name=ccontacts_list size=5 onclick='ccontacts_get(this)'>".
		$select_list .
		//"<option value='david.newcomb@bigsoft.co.uk'>BigSoft</option>".
		//"<option value='amy.shi@bigsoft.co.uk'>amyb</option>".
		//"<option value='drswifty@yahoo.co.uk'>Steve Swift</option>".
		"</select>\n";
	echo "<script language=Javascript type='text/javascript'>\n".
		"function ccontacts_radio_2_string(radio_button)\n".
		"{\n".
			"for (i = 0; i < radio_button.length; i++)\n".
			"{\n".
				"if (radio_button[i].checked)\n".
					"return radio_button[i].value;\n".
			"}\n".
		"}\n".
		"function ccontacts_get(i)\n".
		"{\n".
			"var emailaddr = i.value;\n".
			"var cmd = ccontacts_radio_2_string(compose.ccontacts_field);\n".
			"var obj = compose.send_to;\n".
			"\n".
			"if (cmd == 'whatis') {\n".
				"alert(emailaddr);\n".
				"return;\n".
			"} else if (cmd == 'cc') {\n".
				"obj = compose.send_to_cc;\n".
			"} else if (cmd == 'bcc') {\n".
				"obj = compose.send_to_bcc;\n".
			"}\n".
			"if (obj.value == '')\n".
				"obj.value = emailaddr;\n".
			"else\n".
			"{\n".
				"var l = obj.value;\n".
				"if (l.indexOf(emailaddr) == -1)\n".
					"obj.value = obj.value + ', ' + emailaddr;\n".
			"}\n".
		"}\n".
		"</script>\n";
	echo "</tr></table>";
}

function ccontacts_link()
{

  global $color, $ccontacts_visible;

  if (! $ccontacts_visible) {
    return;
  }

  displayInternalLink('plugins/ccontacts/weather-uk.php', 'Weather', '');

  echo "&nbsp;&nbsp;\n";
}

function ccontacts_save()
{
    global $username,$data_dir;
    global $ccontacts_ccontacts_visible;
    global $ccontacts_ccontacts_region;

    if (sqgetGlobalVar('ccontacts_ccontacts_visible', $vis, SQ_POST)) {
        setPref($data_dir, $username, 'ccontacts_visible', '1');
    } else {
        setPref($data_dir, $username, 'ccontacts_visible', '');
    }
    if (sqgetGlobalVar('ccontacts_ccontacts_region', $vis, SQ_POST)) {
        setPref($data_dir, $username, 'ccontacts_region', $ccontacts_ccontacts_region );
    } else {
        setPref($data_dir, $username, 'ccontacts_region', $ccontacts_ccontacts_region );
    }
}


function ccontacts_load()
{
    global $username, $data_dir;
    global $ccontacts_visible;
    global $ccontacts_region;

    $ccontacts_visible = getPref($data_dir, $username, 'ccontacts_visible');
    $ccontacts_region = getPref($data_dir, $username, 'ccontacts_region');
}


function ccontacts_options()
{
	global $ccontacts_visible;
	global $ccontacts_region;
	$regions = array(
		"CSE" => "South Central England",
		"DC" => "Devon Cornwall",
		"EC" => "Eastern Counties",
		"M" => "Middlands",
		"NES" => "North East Scotland",
		"NE" => "Yorkshire & Lincolnshire",
		"NI" => "Northern Ireland",
		"NWS" => "North West Scotland",
		"NW" => "North West England",
		"N" => "Northern England",
		"SES" => "South East Scotland",
		"SE" => "South East England",
		"SWS" => "South West Scotland",
		"WC" => "West Country",
		"W" => "Walse");

	$select_box = "<select name=ccontacts_ccontacts_region>";
	while (list($m, $t) = each($regions))
	{
		$checked = ($ccontacts_region == $m ? "selected" : "");
		$select_box .= "<option value=$m $checked> $t </option>";
	}
	$select_box .= "</select>";
	$visiable_chk = ($ccontacts_visible ? "checked" : "");


	echo "<tr><td align=right nowrap>" . _("UK weather forecast:") . "</td>\n" .
"<td><input name='ccontacts_ccontacts_visible' type=CHECKBOX $visiable_chk>".
 _("Show weather link in toolbar") . "</td></tr>\n".
"<tr><td>&nbsp;</td><td>"._("Region").": $select_box</td></tr>";
}


?>
