// initialize variables
var emailInformationActive = false;
var recipient              = 0;

function showEmailInformation() {
	// initialize variables
	var html  = '';
	var mode  = 'emailinformation';
	var page  = 'content.cfm';
	var code  = (typeof(captchacode) != "undefined") ? captchacode : '';
	var vdate = (code != "") ? "requiredDefault" : "required";
	var elem  = "";

	if (typeof(emailInformationLink) != "undefined" && (" " + emailInformationLink).indexOf('itinerary.cfm') > 0) {
		page  = 'itinerary.cfm';
	}

	if (emailInformationActive == false) {
		html += '<FORM STYLE="padding:0px;margin:0px;" NAME="emailInformationForm" ID="emailInformationForm" ACTION="http://' + domain + '/' + page + '" METHOD="post" ONSUBMIT="return validateForm(emailInformationForm);">';
		html += '<INPUT TYPE="hidden" NAME="mode" ID="mode" VALUE="' + mode + '">';
		html += '<INPUT TYPE="hidden" NAME="link" ID="link" VALUE="' + emailInformationLink + '">';
		html += '<FIELDSET CLASS="outer_fieldset">';
		html += '<LEGEND>Email this Information</LEGEND>';
		html += '<FIELDSET CLASS="inner_fieldset">'
		html += '<LEGEND>Your Information</LEGEND>';
		html += '<LABEL FOR="sender_email">Email:</LABEL>';
		html += '<INPUT STYLE="width:300px;" CLASS="text_field" TYPE="text" NAME="sender_email" ID="sender_email" VALUE="" VALIDATE="email">';
		html += '<BR><BR>';
		html += '<LABEL FOR="sender_name">Name:</LABEL>';
		html += '<INPUT STYLE="width:300px;" CLASS="text_field" TYPE="text" NAME="sender_name" ID="sender_name" VALUE="">';
		html += '<BR><BR>';
		html += '<LABEL FOR="comments">Additional Comments:</LABEL>';
		html += '<TEXTAREA NAME="comments" ID="comments" STYLE="width:300px;height:100px;" CLASS="text_field"></TEXTAREA>';
		html += '</FIELDSET>';
		html += '<BR><BR>';
		html += '<FIELDSET CLASS="inner_fieldset" ID="recipientFieldset">'
		html += '<LEGEND>Recipient Information</LEGEND>';
		html += getRecipientHTML();
		html += '</FIELDSET>';
		html += '<BR><BR>';
		html += '<FIELDSET CLASS="inner_fieldset">'
		html += '<LEGEND>CAPTCHA</LEGEND>';
		html += '<LABEL FOR="captcha">Code:</LABEL>';
		html += '<INPUT STYLE="width:300px;" CLASS="text_field" TYPE="text" NAME="captcha" ID="captcha" VALUE="' + code + '" VALIDATE="' + vdate + '" VALIDATENAME="your captcha code">';
		html += '<BR><SPAN CLASS="instructions">Please enter the code shown above.</SPAN><BR>';
		html += '</FIELDSET>';
		html += '<BR><BR>';
		html += '<INPUT TYPE="button" NAME="add_recipient_btn" ID="add_recipient_btn" ONCLICK="addRecipient();" VALUE="Add Recipient">';
		html += '<INPUT TYPE="submit" NAME="submit_btn" ID="submit_btn" VALUE="Submit">';
		html += '<INPUT TYPE="button" NAME="close_btn" ID="close_btn" ONCLICK="hideEmailInformation();" VALUE="Close">';
		html += '</FIELDSET>';
		html += '</FORM>';

		jQuery("table#printHeaderTable").after(html);
		// document.getElementById("printHeaderTable").insertAdjacentHTML("afterEnd",html);

		if(jQuery("#captcha_image").length > 0) {
			jQuery("#captcha").after(jQuery("#captcha_image").html());
			// document.getElementById("captcha").insertAdjacentElement("afterEnd",document.getElementById("captcha_image"));
			// elem = document.emailInformationForm.captcha;
			// elem.insertAdjacentElement("afterEnd",document.getElementById("captcha_image"));
			// document.getElementById("captcha_image").style.display = "";
		}
		emailInformationActive = true;
	}
}

function addRecipient() {
	jQuery("#recipientFieldset").html(jQuery("#recipientFieldset").html() + "<BR><BR>" + getRecipientHTML());
	// document.getElementById("recipientFieldset").insertAdjacentHTML("beforeEnd","<BR><BR>" + getRecipientHTML());
}

function getRecipientHTML() {
	recipient = recipient + 1;
	var regex = new RegExp('%recipient%', 'g');
	var rhtml = '<LABEL FOR="recipient_email_%recipient%">Email %recipient%:</LABEL>';
	rhtml    += '<INPUT STYLE="width:300px;" CLASS="text_field" TYPE="text" NAME="recipient_email_%recipient%" ID="recipient_email_%recipient%" VALUE="">';
	rhtml    += '<BR><BR>';
	rhtml    += '<LABEL FOR="recipient_name_%recipient%">Name %recipient%:</LABEL>';
	rhtml    += '<INPUT STYLE="width:300px;" CLASS="text_field" TYPE="text" NAME="recipient_name_%recipient%" ID="recipient_name_%recipient%" VALUE="">';
	rhtml     = rhtml.replace(regex,recipient);

	return rhtml;
}

function hideEmailInformation() {
	// initialize variables
	var x = "";

	try {
		jQuery("form#emailInformationForm").remove();
		recipient = 0;
		emailInformationActive = false;
	} catch (x) {
		// do nothing
	}

	return x;
}