// ==UserScript==
// @name OWA quotefix
// @author Peter Krefting
// @version 1.3
// @include https://*/Exchange/*
// @description  Fix quotes on Outlook 2003 Web Access
// ==/UserScript==
//
// Replace INSERT-HOSTNAME-HERE below with the host name of your
// Outlook Web Access install.

document.addEventListener('load',
	function()
	{
		if (window.location.hostname == 'INSERT-HOSTNAME-HERE' && document.forms[0])
		{
			// Find subject field
			var subject = document.forms[0].elements["urn:schemas:httpmail:subject"];
			if (subject && subject.value != "")
			{
				if (subject.value.match(/^([^:]*): (.*)$/))
				{
					// Add any translations for "Re:" here
					if (RegExp.$1 == 'SV' || RegExp.$1 == 'AW')
					{
						// Fixed localized "Re:"
						subject.value = 'Re: ' + RegExp.$2;
					}
					// Add any translations for "Fwd:" here (including the
					// original English, so that we do not reformat a
					// forwarded message).
					else if (RegExp.$1 == 'VB' || RegExp.$1 == 'Fwd')
					{
						// Fixed localized "Fwd:", and avoid transforming
						// message body
						subject.value = 'Fwd: ' + RegExp.$2;
						return;
					}
				}
			}

			// Find mail <textarea>
			var textarea = document.forms[0].elements["urn:schemas:httpmail:textdescription"];
			if (!textarea) return;

			// Check text
			var text = textarea.value;
			if (text == "") return;

			// Check if already processed
			if (text.match(/\n-----.*-----/))
			{
				// No...
				var strings = text.split("\n");
				var numstrings = strings.length;
				var newtext = "";
				var newsignature = "";
				var foundseparator = 0;
				var foundheader = 0;
				var sender = "";
				var quoteprefix = "> ";
				var newquote = 1;
				for (var i = 0; i < numstrings; i ++)
				{
					if (!foundseparator)
					{
						// Signature comes first
						if (strings[i].match(/-----.*-----)/))
						{
							foundseparator = 1;
						}
						else
						{
							newsignature += strings[i] + "\n";
						}
					}
					else if (!foundheader)
					{
						var s = strings[i];
						// After signature comes the mail header.
						// The first line contains the sender's name, keep
						// it and discard the rest.
						if (s == "" || s == "\r")
						{
							foundheader = 1;
						}
						else if (sender == "" && s.match(/.*: (.*)\r/))
						{
							sender = RegExp.$1 + ":\r\n\r\n";
							newtext = sender;
						}
					}
					else
					{
						// Then reply, unquoted. Each string can be a paragraph,
						// so we split it into chunks first.
						var s = strings[i];

						// Try to find the start of a new quote by looking for a
						// line of underscores followed by a line with a 
						// token (perhaps localized), a colon and a name.
						if (1 == newquote)
						{
							if (s.match(/.*: .*\r/))
							{
								quoteprefix = ">" + quoteprefix;
							}
							newquote = 0;
						}
						else if (s.match(/________________________________/))
						{
							newquote = 1;
						}
						else
						{
							newquote = 0;
						}

						if (s == "" || s == "\r")
						{
							newtext += "\r\n";
						}
						else
						{
							// Line Splitter Function copyright Stephen Chapman,
							// 19th April 2006. You may copy this code but
							// please keep the copyright notice as well.
							while (s.length > 70)
							{
								var segment = s.substring(0, 70);
								var lastspace = segment.lastIndexOf(' ');
								if (lastspace == -1)
								{
									lastspace = s.indexOf(' ');
								}
								if (lastspace != -1)
								{
									newtext += quoteprefix + s.substring(0, lastspace) + '\n';
									s = s.substring(lastspace + 1);
								}
								else
								{
									break;
								}
							}
							newtext += quoteprefix + s + "\n";
						}
					}
				}

				// Insert edited text
				textarea.value = newtext + "-- \n" + newsignature;
			}
		}
	}
, false);

