function toggleBillingName(toggle){

	// Toggle the billing name input box if required
	if (toggle == "extended"){
		document.paymentForm.shipName1.disabled=false;
	}else{
		document.paymentForm.shipName1.disabled=true;
	}
}


function toggleBillingAddress(toggle){

	// Toggle the billing name input box if required
	if (toggle == "extended"){
		document.paymentForm.shipAddress1.disabled=false;
	}else{
		document.paymentForm.shipAddress1.disabled=true;
	}
}

function toggleAcceptCheck(){

	// Toggle the send payment button if the accept checkbox is clicked
	if (document.paymentForm.authorize.checked==true){
		document.paymentForm.paymentButton.disabled=false;
	}else{
		document.paymentForm.paymentButton.disabled=true;
	}
}

// Hide various elements that we don't want to dislay on load.
function hideElements(){
	$('donationDiv').hide();
	$('spinningGear').hide();
}

// Hide various elements that we don't want to dislay on load.
function hideSpinningGear(){
	$('spinningGear').hide();
}

function toggleDonation(){
	if (document.paymentForm.membership.value=="donation"){
		$('donationDiv').show();
	}else{
		$('donationDiv').hide();
	}

}

function printReceipt()
{
	var a = window.open('','','width=300,height=300');
	a.document.open("text/html");
	a.document.write(document.getElementById('form').innerHTML);
	a.document.close();
	a.print();
}


function formVerification(){

	Form = document.paymentForm;

	// Various regular expression validations
	var regThreeDigit = '[0-9]{3}';
	var regFiveDigit = '[0-9]{5}';
	var regSixteenDigit = '^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$';
	var regFirstName = '[a-zA-Z]';
	var regSurname = '[a-zA-Z’ ]+';
	var regEmailAddress = '[a-zA-Z0-9_\.]+@[a-zA-Z0-9-]+\.[a-zA-Z]{0,4}';
	var regAddress = '[a-zA-Z0-9_\. ]{5,50}';
	var regCity = '[a-zA-Z_\. ]{2,50}';

	if (Form.firstname.value.search(regFirstName)==-1){
		alert("First name required. Please enter alphabetical characters only");

	}else if (Form.lastname.value.search(regSurname)==-1){
		alert("Last name required. Please enter alphabetical characters only");

	}else if (Form.email.value.search(regEmailAddress)==-1){
		alert("Please enter a valid email address");

	}else if (Form.street.value.search(regAddress)==-1){
		alert("Please enter a valid address.");

	}else if (Form.city.value.search(regCity)==-1){
		alert("Please enter a valid city. Please check spaces");

	}else if (Form.state.value == "None"){
		alert("Please enter a valid state.");

	}else if (Form.zip.value.search(regFiveDigit)==-1){
		alert("Please enter a valid 5 digit zip. Please check spaces");

	}else if (Form.ccnumber.value.search(regSixteenDigit)==-1){
		alert("Please enter a valid credit card number. Please check spaces");

	}else if ((Form.shipName1.value.search(regSurname)==-1) && Form.shipName1.disabled==false){
		alert("Please enter a valid shipping name.");

	}else if ((Form.shipAddress1.value.search(regAddress)==-1) && Form.shipAddress1.disabled==false){
		alert("Please enter a valid shipping address.");

	}else if (Form.ccvnum.value.search(regThreeDigit)==-1){
		alert("Please enter a valid 3 digit CVV2 number.");

	}else if (Form.membership.value == "none"){
		alert("Please select a membership type");

	}else if ((Form.membership.value=="donation") && document.paymentForm.donationAmount.value==0){
		alert("Please enter a donation amount");

	}else {

	// Finally, we've checked everthing is A-OK so we'll submit the information to paypal whilst
	// crossing our fingers.

	submitForm();

	}

}

function submitForm()
{

	var Form = '';
	Form = document.paymentForm;
	var Post = '';
	var PostSuccess = '';

	// Disable the payment button after clicked once. Add the spinning gear
	Form.paymentButton.value = "Processing Payment. Wait.";
	Form.paymentButton.disabled=true;

	// Show the loading gear, and hide the main form
	$('spinningGear').show()
	$('paymentForm').hide();

	// Concatenate the main POST variables we're going to send to the PAYPAL API
	Post += 'paymentType=' + Form.paymentType.value;
	Post += '&firstName=' + Form.firstname.value;
	Post += '&lastName='    + Form.lastname.value;
	Post += '&address1='    + Form.street.value;
	Post += '&address2='    + Form.street2.value;
	Post += '&creditCardType='    + Form.cctype.value;
	Post += '&creditCardNumber='    + Form.ccnumber.value;
	Post += '&expDateMonth='    + Form.expiration1.value;
	Post += '&expDateYear='    + Form.expiration2.value;
	Post += '&cvv2Number='    + Form.ccvnum.value;
	Post += '&city='    + Form.city.value;
	Post += '&state='    + Form.state.value;
	Post += '&zip='    + Form.zip.value;
	Post += '&email='    + Form.email.value;
	Post += '&shipPhone='    + Form.phone.value;
	Post += '&membership='    + Form.membership.value;
	Post += '&donationAmount='    + Form.donationAmount.value;

	// What shipping name we send depends on if they added a difference shipping name.
	if (Form.shipName1.disabled==false){
		Post += '&shipName='    + Form.shipName1.value;
	}else{
		Post += '&shipName='    + Form.firstname.value
		+ " " + Form.lastname.value;
	}

	// What shipping address we send depends on if they added a difference shipping address.
	if (Form.shipAddress1.disabled==false){
		Post += '&shipStreet='    + Form.shipAddress1.value;
		Post += '&shipStreet2=NA';
		Post += '&shipCity=NA';
		Post += '&shipState=NA';
		Post += '&shipZip=00000';
		Post += '&shipCountry=US';
	}else{
		Post += '&shipStreet='    + Form.street.value;
		Post += '&shipStreet2='   + Form.street2.value;
		Post += '&shipCity='   + Form.city.value;
		Post += '&shipState='    + Form.state.value;
		Post += '&shipZip='   + Form.zip.value;
		Post += '&shipCountry=US';
	}


	// Thse are the post variables we only send to our own custom php success page
	// and not paypal. This is because we don't want to send credit card numbers, etc.

	PostSuccess += 'email='    + Form.email.value;

	if (Form.shipName1.disabled==false){
		PostSuccess += '&shipName='    + Form.shipName1.value;
	}else{
		PostSuccess += '&shipName='    + Form.firstname.value
		+ " " + Form.lastname.value;
	}

	if (Form.shipAddress1.disabled==false){
		PostSuccess += '&shipAddress='    + Form.ccbiling1.value;
	}else{
		PostSuccess += '&shipAddress='    + Form.street.value
									+ ", "+ Form.street2.value
									+ ", "+ Form.city.value
									+ ", "+ Form.state.value
									+ " "+ Form.zip.value;
	}

	PostSuccess += '&phone=' + Form.phone.value;
	PostSuccess += '&membership='    + Form.membership.value;


	var url = "/form/php/DoDirectPaymentReceipt.php";
	var options = {
		method:"post",
		parameters:Post,
		onComplete:ajax_response
	};

	new Ajax.Request(url,options);

	// Now we parse the output of the response to see if the transaction completed.
	function ajax_response(resp)
	{
		var response = resp.responseText;

		$('paymentForm').remove();

		if (response.match("Failure")){
			// Find out the actual failure. alert(response);

			var htmlError = "<span class='big-title'>Error.</span>" +
			"<p>There was an error processing your payment details." +
			"<p>No money has been charged to your account." +
			"<p> Please go back, check billing information, and resubmit. " +
			"Your billing address <strong>MUST</strong> be the same as the one on your card" +
			"<p><input type='button' name='errorButton' Value='Back' onclick='populatePaymentForm()'>";

			 $('form').insert(htmlError);
			 //$('form').insert(response);
			 $('spinningGear').hide();

		}else{
			// If we get a successful transaction response from paypal, we'll push off to our own
			// success page that handles the emails spool et cetera.

			new Ajax.Request('https://www.theecologycenter.org/form/php/paymentFormSuccess.php',
			  {
				method:'post',
				parameters:PostSuccess,
				onSuccess: function(transport){
				  $('form').insert(transport.responseText);
				  $('spinningGear').hide();
				  //alert("Success! \n\n" + response);
				},
					onFailure: function(){

					// If something breaks this badly, I want an email!
					//mail('drew@westcoastdevelopers.com', 'Eco PHP exception', transport.responseText);

					alert('Something went wrong...')
					$('form').insert(htmlError);
					$('form').insert(transport.responseText);
				}
			  });
		}
	}
}

function formSpeakerSeriesVerification(){

	Form = document.paymentForm;

	// Various regular expression validations
	var regThreeDigit = '[0-9]{3}';
	var regFiveDigit = '[0-9]{5}';
	var regSixteenDigit = '^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$';
	var regFirstName = '[a-zA-Z]';
	var regSurname = '[a-zA-Z’ ]+';
	var regEmailAddress = '[a-zA-Z0-9_\.]+@[a-zA-Z0-9-]+\.[a-zA-Z]{0,4}';
	var regAddress = '[a-zA-Z0-9_\. ]{5,50}';
	var regCity = '[a-zA-Z_\. ]{2,50}';

	if (Form.firstname.value.search(regFirstName)==-1){
		alert("First name required. Please enter alphabetical characters only");

	}else if (Form.lastname.value.search(regSurname)==-1){
		alert("Last name required. Please enter alphabetical characters only");

	}else if (Form.email.value.search(regEmailAddress)==-1){
		alert("Please enter a valid email address");

	}else if (Form.street.value.search(regAddress)==-1){
		alert("Please enter a valid address.");

	}else if (Form.city.value.search(regCity)==-1){
		alert("Please enter a valid city. Please check spaces");

	}else if (Form.state.value == "None"){
		alert("Please enter a valid state.");

	}else if (Form.zip.value.search(regFiveDigit)==-1){
		alert("Please enter a valid 5 digit zip. Please check spaces");

	}else if (Form.ccnumber.value.search(regSixteenDigit)==-1){
		alert("Please enter a valid credit card number. Please check spaces");

	}else if (Form.ccvnum.value.search(regThreeDigit)==-1){
		alert("Please enter a valid 3 digit CVV2 number.");
		
	}else if (Form.eventAll.value == 0 && Form.eventFood.value == 0 && Form.eventWater.value == 0
			  && Form.eventClothing.value == 0 && Form.eventShelter.value == 0){
		alert("Please choose at least one event.");
	}else if (Form.phone.value == ""){
		alert("Please fill in your phone number.");

	}else {

	// Finally, we've checked everthing is A-OK so we'll submit the information to paypal whilst
	// crossing our fingers.

	submitSpeakerSeriesPaymentForm();

	}

}

function submitSpeakerSeriesPaymentForm()
{

	var Form = '';
	Form = document.paymentForm;
	var Post = '';
	var PostSuccess = '';

	// Disable the payment button after clicked once. Add the spinning gear
	Form.paymentButton.value = "Processing Payment. Wait.";
	Form.paymentButton.disabled=true;

	// Show the loading gear, and hide the main form
	$('spinningGear').show()
	$('paymentForm').hide();

	// Concatenate the main POST variables we're going to send to the PAYPAL API
	Post += 'paymentType=' + Form.paymentType.value;
	Post += '&firstName=' + Form.firstname.value;
	Post += '&lastName='    + Form.lastname.value;
	Post += '&address1='    + Form.street.value;
	Post += '&address2='    + Form.street2.value;
	Post += '&creditCardType='    + Form.cctype.value;
	Post += '&creditCardNumber='    + Form.ccnumber.value;
	Post += '&expDateMonth='    + Form.expiration1.value;
	Post += '&expDateYear='    + Form.expiration2.value;
	Post += '&cvv2Number='    + Form.ccvnum.value;
	Post += '&city='    + Form.city.value;
	Post += '&state='    + Form.state.value;
	Post += '&zip='    + Form.zip.value;
	Post += '&email='    + Form.email.value;
	Post += '&shipPhone='    + Form.phone.value;
	Post += '&membership='    + Form.membership.value;
    Post += '&shipName='    + Form.firstname.value
		+ " " + Form.lastname.value;
	
	Post += '&shipStreet='    + Form.street.value;
	Post += '&shipStreet2='   + Form.street2.value;
	Post += '&shipCity='   + Form.city.value;
	Post += '&shipState='    + Form.state.value;
	Post += '&shipZip='   + Form.zip.value;
	Post += '&shipCountry=US';
	Post += '&eventAll='    + Form.eventAll.value;
	Post += '&eventFood='    + Form.eventFood.value;
	Post += '&eventWater='    + Form.eventWater.value;
	Post += '&eventShelter='    + Form.eventShelter.value;
	Post += '&eventClothing='    + Form.eventClothing.value;

	// Thse are the post variables we only send to our own custom php success page
	// and not paypal. This is because we don't want to send credit card numbers, etc.

	PostSuccess += 'email='    + Form.email.value;

	
	PostSuccess += '&shipName='    + Form.firstname.value
	+ " " + Form.lastname.value;
	
	
	PostSuccess += '&shipAddress='    + Form.street.value
								+ ", "+ Form.street2.value
								+ ", "+ Form.city.value
								+ ", "+ Form.state.value
								+ " "+ Form.zip.value;

	PostSuccess += '&phone=' + Form.phone.value;
	PostSuccess += '&membership='    + Form.membership.value;
	PostSuccess += '&eventAll='    + Form.eventAll.value;
	PostSuccess += '&eventFood='    + Form.eventFood.value;
	PostSuccess += '&eventWater='    + Form.eventWater.value;
	PostSuccess += '&eventShelter='    + Form.eventShelter.value;
	PostSuccess += '&eventClothing='    + Form.eventClothing.value;

	var url = "/form/phpsummer/DoDirectPaymentForSummerSeriesReceipt.php";
	var options = {
		method:"post",
		parameters:Post,
		onComplete:ajax_response
	};

	new Ajax.Request(url,options);

	// Now we parse the output of the response to see if the transaction completed.
	function ajax_response(resp)
	{
		var response = resp.responseText;

		$('paymentForm').remove();

		if (response.match("Failure")){
			// Find out the actual failure. 
			//alert(response);

			var htmlError = "<span class='big-title'>Error.</span>" +
			"<p>There was an error processing your payment details." +
			"<p>No money has been charged to your account." +
			"<p> Please go back, check billing information, and resubmit. " +
			"Your billing address <strong>MUST</strong> be the same as the one on your card" +
			"<p><input type='button' name='errorButton' Value='Back' onclick='populatePaymentForm()'>";

			 $('form').insert(htmlError);
			 //$('form').insert(response);
			 $('spinningGear').hide();

		}else{
			// If we get a successful transaction response from paypal, we'll push off to our own
			// success page that handles the emails spool et cetera.

			new Ajax.Request('/form/phpgreenfeast/paymentFormForGreanFeastSuccess.php',
			  {
				method:'post',
				parameters:PostSuccess,
				onSuccess: function(transport){
				  $('form').insert(transport.responseText);
				  $('spinningGear').hide();
				  //alert("Success! \n\n" + response);
				},
					onFailure: function(){

					// If something breaks this badly, I want an email!
					//mail('drew@westcoastdevelopers.com', 'Eco PHP exception', transport.responseText);

					alert('Something went wrong...')
					$('form').insert(htmlError);
					$('form').insert(transport.responseText);
				}
			  });
		}
	}
}

function formGreenFeastVerification(){

	Form = document.paymentForm;

	// Various regular expression validations
	var regThreeDigit = '[0-9]{3}';
	var regFiveDigit = '[0-9]{5}';
	var regSixteenDigit = '^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$';
	var regFirstName = '[a-zA-Z]';
	var regSurname = '[a-zA-Z’ ]+';
	var regEmailAddress = '[a-zA-Z0-9_\.]+@[a-zA-Z0-9-]+\.[a-zA-Z]{0,4}';
	var regAddress = '[a-zA-Z0-9_\. ]{5,50}';
	var regCity = '[a-zA-Z_\. ]{2,50}';

	if (Form.firstname.value.search(regFirstName)==-1){
		alert("First name required. Please enter alphabetical characters only");

	}else if (Form.lastname.value.search(regSurname)==-1){
		alert("Last name required. Please enter alphabetical characters only");

	}else if (Form.email.value.search(regEmailAddress)==-1){
		alert("Please enter a valid email address");

	}else if (Form.street.value.search(regAddress)==-1){
		alert("Please enter a valid address.");

	}else if (Form.city.value.search(regCity)==-1){
		alert("Please enter a valid city. Please check spaces");

	}else if (Form.state.value == "None"){
		alert("Please enter a valid state.");

	}else if (Form.zip.value.search(regFiveDigit)==-1){
		alert("Please enter a valid 5 digit zip. Please check spaces");

	}else if (Form.ccnumber.value.search(regSixteenDigit)==-1){
		alert("Please enter a valid credit card number. Please check spaces");

	}else if (Form.ccvnum.value.search(regThreeDigit)==-1){
		alert("Please enter a valid 3 digit CVV2 number.");
		
	}else if (Form.eventDinner.value == 0 ){
		alert("Please specify at least one ticket.");
	}else if (Form.phone.value == ""){
		alert("Please fill in your phone number.");

	}else {

	// Finally, we've checked everthing is A-OK so we'll submit the information to paypal whilst
	// crossing our fingers.

	submitGreenFeastPaymentForm();

	}

}

function submitGreenFeastPaymentForm()
{

	var Form = '';
	Form = document.paymentForm;
	var Post = '';
	var PostSuccess = '';

	// Disable the payment button after clicked once. Add the spinning gear
	Form.paymentButton.value = "Processing Payment. Wait.";
	Form.paymentButton.disabled=true;

	// Show the loading gear, and hide the main form
	$('spinningGear').show()
	$('paymentForm').hide();

	// Concatenate the main POST variables we're going to send to the PAYPAL API
	Post += 'paymentType=' + Form.paymentType.value;
	Post += '&firstName=' + Form.firstname.value;
	Post += '&lastName='    + Form.lastname.value;
	Post += '&address1='    + Form.street.value;
	Post += '&address2='    + Form.street2.value;
	Post += '&creditCardType='    + Form.cctype.value;
	Post += '&creditCardNumber='    + Form.ccnumber.value;
	Post += '&expDateMonth='    + Form.expiration1.value;
	Post += '&expDateYear='    + Form.expiration2.value;
	Post += '&cvv2Number='    + Form.ccvnum.value;
	Post += '&city='    + Form.city.value;
	Post += '&state='    + Form.state.value;
	Post += '&zip='    + Form.zip.value;
	Post += '&email='    + Form.email.value;
	Post += '&shipPhone='    + Form.phone.value;
    Post += '&shipName='    + Form.firstname.value
		+ " " + Form.lastname.value;
	
	Post += '&shipStreet='    + Form.street.value;
	Post += '&shipStreet2='   + Form.street2.value;
	Post += '&shipCity='   + Form.city.value;
	Post += '&shipState='    + Form.state.value;
	Post += '&shipZip='   + Form.zip.value;
	Post += '&shipCountry=US';
	Post += '&eventDinner='    + Form.eventDinner.value;
	
	// These are the post variables we only send to our own custom php success page
	// and not paypal. This is because we don't want to send credit card numbers, etc.

	PostSuccess += 'email='    + Form.email.value;

	
	PostSuccess += '&shipName='    + Form.firstname.value
	+ " " + Form.lastname.value;
	
	
	PostSuccess += '&shipAddress='    + Form.street.value
								+ ", "+ Form.street2.value
								+ ", "+ Form.city.value
								+ ", "+ Form.state.value
								+ " "+ Form.zip.value;

	PostSuccess += '&phone=' + Form.phone.value;
	PostSuccess += '&eventDinner='    + Form.eventDinner.value;
	

	var url = "/form/phpgreenfeast/DoDirectPaymentForGreenFeastReceipt.php";
	var options = {
		method:"post",
		parameters:Post,
		onComplete:ajax_response
	};

	new Ajax.Request(url,options);

	// Now we parse the output of the response to see if the transaction completed.
	function ajax_response(resp)
	{
		var response = resp.responseText;

		$('paymentForm').remove();

		if (response.match("Failure")){
			// Find out the actual failure. 
			//alert(response);

			var htmlError = "<span class='big-title'>Error.</span>" +
			"<p>There was an error processing your payment details." +
			"<p>No money has been charged to your account." +
			"<p> Please go back, check billing information, and resubmit. " +
			"Your billing address <strong>MUST</strong> be the same as the one on your card" +
			"<p><input type='button' name='errorButton' Value='Back' onclick='populatePaymentForm()'>";

			 $('form').insert(htmlError);
			 //$('form').insert(response);
			 $('member_spinningGear').hide();

		}else{
			// If we get a successful transaction response from paypal, we'll push off to our own
			// success page that handles the emails spool et cetera.

			new Ajax.Request('/form/phpgreenfeast/paymentFormForGreenFeastSuccess.php',
			  {
				method:'post',
				parameters:PostSuccess,
				onSuccess: function(transport){
				  $('form').insert(transport.responseText);
				  $('spinningGear').hide();
				  //alert("Success! \n\n" + response);
				},
					onFailure: function(){

					// If something breaks this badly, I want an email!
					// mail('drew@westcoastdevelopers.com', 'Eco PHP exception', transport.responseText);

					alert('Something went wrong...')
					$('form').insert(htmlError);
					$('form').insert(transport.responseText);
				}
			  });
		}
	}
}
