#include <stdlib.txt>
#include "smoke_test_lib.txt"

// This script will delete all emails from all folders.  For it to work
// properly, there MUST be at least one email message on the device (does
// not matter which folder.

gotoEmail();

// Jump directly to the top folder (Inbox)
clickButtonModified(END_BUTTON, ALT);

// Now, keep invoking "Discard Messages" on each folder until
// the "Discard" button becomes the "Empty Trash" button (this
// happens when we encounter the Trash folder at the bottom of
// the list.
var done; done = 0;
while (done == 0) {
	// This folder has messages in it
	if (isActionMenuItemEnabled ("Discard Messages")) {
		executeActionMenuItem ("Discard Messages");
		waitForWindow ("AlertWindow");
		if (isButtonEnabled("Discard")) {
			executeButton("Discard");
			sleep(100);
			// If we get an alert at this point, it's probably because
			// A draft message has not yet transmitted to the service.
			if (getActiveWindow() == "AlertWindow") {
				executeButton("OK");
				sleep(1000);
			} else {
				clickButton(DOWN_ARROW);  // Move down to the next folder
			}
		} else if (isButtonEnabled("Empty Trash")) {
			executeButton("Empty Trash");
			done = 1;
		}
		sleep(500);
	} else {
		// This folder was empty
		clickButton(DOWN_ARROW);  // Move down to the next folder
	}
}
sleep(5000);
sendService("scripttestcase", "DONE");
