/************************************************************************
           File: mailto.js 
    Description: A JavaScript to email the results of a form to 
		 a specified email address.  
        Version: 2.0
        Created: 2003-03-25
         Author: Michael McDonnell <mailtojs@winterstorm.ca>
        License: GPL 2.0
      Copyright: (c) 1999-2003 Michael McDonnell
 ************************************************************************/


/************************************************************************
   Function: sendEmail
   Description: Generate an email message containing data submitted
                in a form.
   Parameters: myForm  -- required -- a refernece to an HTML form 
               Address -- optional -- The email address to send to
               Subject -- optional -- The subject to use in the email
               
               If Address or Subject are blank, then we will look for
               HTML elements named "Mailto_To" or "Mailto_Subject" 
               respectively.

               Additionally the following HTML elements have special
               meaning:

                 Mailto_To : Used to set the To: field
                 Mailto_Subject : Used to set the Subject: field
                 Mailto_CC : Sets the CC: field
                 Mailto_BCC : Sets the BCC: field
                 Mailto_ReplyTo: Sets the ReplyTo: Field

               If no Address is specified via the function parameter
               or the HTML element Mailto_To, the address field will
               be left blank the user will have to fill it in.  This 
               would only be useful in very specific situations!

 ************************************************************************/

function sendEmail(Form, Address, Subject, CC, BCC, ReplyTo) {
    var emailBody;
    var emailTo;
    var emailSubject;
    var emailCC;
    var emailBCC;

    var emailURL; // The URL that will open the user's email client
    var formSeparator = "\n\r |--> ";  // looks good in Mozilla and other e-mail clients

    // temporary variables
    var elementCount;
    var elementIsHeader;

    var i;
    var j;

    // Set some default values
    emailBody = "Submission Results" + formSeparator;
    emailTo = Address;
    emailSubject = Subject;
    emailCC = CC ? CC : '';
    emailBCC = BCC ? BCC : '';
    emailReplyTo = ReplyTo ? ReplyTo : '';

    for(i = 0; i < Form.elements.length; i++) {
	fe = Form.elements[i];
	elementIsHeader = 0;

	/* Check for special form elements whose names start with 'Mailto_'
	   These are used to set our headers from HTML form elements instead
	   of from the function parameters.
	 */
	
	switch(fe.name) {
	case "Mailto_To":
	    emailTo = fe.value;
	    elementIsHeader = 1;
	    break;
	case "Mailto_Subject":
	    emailSubject = fe.value;
	    elementIsHeader = 1;
	    break;
	case "Mailto_CC":
	    emailCC = fe.value;
	    elementIsHeader = 1;
	    break;
	case "Mailto_BCC":
	    emailBCC = fe.value;
	    elementIsHeader = 1;
	    break;
	case "Mailto_ReplyTo":
	    emailReplyTo = fe.value;
	    elementIsHeader = 1;
	    break;
	}

	if(!elementIsHeader) {
	    switch(fe.type) {

	    case "hidden":
	    case "password":
	    case "text":
	    case "textarea":
		if(fe.value.length > 0) {
		    emailBody += fe.name + ": " + fe.value + formSeparator;
		}
		break;

	    case "select-multiple":
	    case "select-one":
		elementCount = 0;
		for(j = 0; j < fe.options.length; j++) {
		    if(fe.options[j].selected) {
			elementCount++;
			if(elementCount > 1) {
			    emailBody += ", " + fe.options[j].value;
			} else {
			    emailBody += fe.name + ": " + fe.options[j].value;
			}
		    }
		}
		if(elementCount > 0) {
		    emailBody += formSeparator;
		}
		break;

	    case "radio":
	    case "checkbox":
		if(fe.checked) {
		    emailBody += fe.name + ": " + fe.value + formSeparator;
		}
		break;
		
	    case "button":
	    case "reset":
	    case "image":
	    case "submit":
	    case "file":
		/* We do not record the name or value of buttons.  Please 
		   note that if a button is named with one of the special
		   element names (Mailto_To, Mailto_Subject etc.) that it
		   will not be ignored.  This means that you can have 
		   several different buttons, and name them Mailto_Subject
		   with different values.  Whichever button the user 
		   clicks on will cause the subject to be set.
		*/
		break;

	    default :
		// If we have never heard of this type of input we record
		// the name, input type, and value.
		emailBody += fe.name + "(" + fe.type + "): " + fe.value + formSeparator;
	    }
	}
    }

    emailURL = "mailto:" + escape(emailTo) + 
	"?subject=" + escape(emailSubject) +
	"&cc=" + escape(emailCC) +
	"&bcc=" + escape(emailBCC) +
	"&replyto=" + escape(emailReplyTo) +
	"&body=" + escape(emailBody);

    window.location = emailURL;
    return true;
}
