// ==UserScript==
// @name           Gmail attachment reminder v2.4.1
// @namespace      http://www.norcimo.com/fun/greasmonkey/
// @description    Reminds you to attach a file to your email if it appears that you have not.
// @include        http*://mail.google.tld/mail/*
// @include		   http*://mail.google.tld/a/*
// @version		   2.4.1
// ==/UserScript==

/*
Version 2.4.1
Modified by Gina Trapani to include Google Apps for your Domain and user-configurable attachment words

Version 2.4

Borked thanks to the new Google services bar at the top including an iframe. Fixed that. Also added hyphenation into the regex (so attach-ment works, just for the sake of it)

Version 2.3

Added checking of the subject line (thanks to Simon Wheatley for the intitial patch)

Version 2.2

Gmail changed the ID of the send button to snd

Version 2.1

This is a rewrite to deal with the fact that the addEventListener doesn't get attached to the Send button with replies etc

*/

var wordTrigger = 'attach';

if (!GM_getValue("attach_trigger")) {
	GM_setValue("attach_trigger", wordTrigger);
}

GM_registerMenuCommand('Set word trigger', setWordTrigger);

function setWordTrigger(){
	wordTrigger = prompt('Enter word trigger (like "attach"). Separate multiple triggers with |', wordTrigger);
    GM_setValue('attach_trigger', wordTrigger );
}

document.addEventListener('click',function(event) {
 	if (event.target.id=='snd') {
		// About to send the message	 	
		var msg;
		var subj="";
		if (document.getElementsByTagName("iframe").length>1) {
			// Silly rich format form
			// Grab the HTML content of the message by cloning the body of the iframe
			var grabContent=document.getElementsByTagName("iframe")[1].contentDocument.getElementsByTagName("body")[0].cloneNode(true);
			// We don't want false positives on quoted text. Assume anything quoted isn't going to refer
			// to an attachment we meant to add, so get rid of the content the quick and dirty way
			// This will of course remove user inserted quotes from the checks but what are the odds of
			// finding an attachment reference there anyway?
			var contentQuotes=grabContent.getElementsByTagName("blockquote");
			if (contentQuotes) {
				for (var i=0; i<contentQuotes.length; i++) {
					contentQuotes[i].innerHTML="";
				}
			}
			// Grab everything, tags included. Then replace the <br>'s with new lines.
			// This means we should still hit > text being recognised at as a quoted line
			// later (maybe, anyway)
			msg=grabContent.innerHTML;
			var fixupReg=new RegExp(/<br>/gi);
			msg=msg.replace(fixupReg,"\n");
		} else {
			// Normal plain text -- so much simpler!
			msg=document.getElementsByName('msgbody')[0].value;
		}	
		var subjBox=document.getElementsByName('subject')[0];
		//Only check the subject when it's visible (prevents checking when just replying etc)
		if (subjBox.parentNode.parentNode.style.display!="none")
			subj=subjBox.value;
//-----------------------------------------------------------
// Check for references to attachments etc
//-----------------------------------------------------------
		
				/*
				 To add further checks change the regular expression below.
				 You're mainly interested in the (...) after .*
				 e.g. (attach|adjunt) will match words beginning
				 attach or adjunt (i.e. attachment, attached,
				 adjunto, adjuntado (but not attach). You can of course use a more complex regex if
				 you want to! 
				 The first part of the regex ^(?!>) is necessary to avoid matching
				 against any quoted text				
				 */

				var attach_word_trigger = GM_getValue("attach_trigger");
				var myTest=new RegExp("^(?!>).*("+attach_word_trigger+")[a-z-]+","mi");
				if(msg.match(myTest) || subj.match(myTest)){
	 				// There's an attachment reference, so check if there's an attachment present!
	 				var fileattached=false;
					var inputs=document.getElementsByTagName('input');
					for (var i=0;i<inputs.length; i++) {
						if (inputs[i].type=="file")
							if (inputs[i].value!="")
								fileattached=true;
					}
	 				
 					var attachpos=document.getElementsByName('attach');
 					if (attachpos && !fileattached) {
	 					for (var i=0; i<attachpos.length; i++) {
		 					if (attachpos[i].checked)
		 						fileattached=true;
		 				}
	 				}
					if(!fileattached){
						// There isn't an attachment. Should we send?
						if(!confirm('It appears that you wanted to attach a file to this email.\n Send anyway?')){
							event.stopPropagation();
						    event.preventDefault();
						}
					}
 				}
			}
		}, true);