// Request encrypted PayPal data.
var paypal_request_status;
function request_paypal_data ()
{
	paypal_request_status = "pending";

	// Assemble the form's fields into a URL string.
	var sample_form = find_element ("sample_form").elements;
	var url = "";
	for (var i = 0, len = sample_form.length; i < len; i++)
	{
		if (sample_form[i].type == "checkbox" && sample_form[i].checked == true) url += "&" + encodeURIComponent (sample_form[i].name) + "=true";
	}

	// Bail if nothing was selected.
	if (url == "") return;

	// Create and send the HTTP request.
	http_request = make_http_request ();
	if (http_request.overrideMimeType) http_request.overrideMimeType("text/plain");
	if (!http_request) return false;
	http_request.onreadystatechange = receive_paypal_data; // Start monitoring our request with receive_paypal_data().
	http_request.open("GET", "http://thoughtfulday.com/js/../xml.php?do=get_samples_paypal" + url, true); // Create the request.
	http_request.send(null);

	setTimeout('if (paypal_request_status == "pending") { paypal_request_status = false; alert ("The server took too long to answer.  Please try placing your order again."); }', 15000);
}

// Receive encrypted PayPal data.
function receive_paypal_data ()
{
	// Once the request is answered, proceed.
	if (http_request.readyState == 4 && http_request.status == 200)
	{
		if (paypal_request_status == "pending")
		{
			if (http_request.responseText.match('^-----BEGIN PKCS7-----\n[0-9a-zA-Z\+/=\n]+\n-----END PKCS7-----$') != null)
			{
				find_element("paypal_data").value = http_request.responseText;
				find_element("paypal_form").submit();
				paypal_request_status = true;
			} else {
				paypal_request_status = false;
alert (http_request.responseText);
				alert ("There was a problem assembling your shopping cart. Please use the \"Contact\" link at the top of the page to contact us for help placing your order.");
			}
		}
	}
}