// Copyright (c) 2009 Andrew Dockery
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// supporter.js
// Mar 10, 2009
//
// Javascript specific to the Supporter page.


function CheckDisplay(form) {	
	if(form.MC_sign_petition.checked) {
		document.getElementById('petition_box').style.display = '';
	} else {
		document.getElementById('petition_box').style.display = 'none';
	}
	
	if(form.donate.checked) {
		document.getElementById('donation_box').style.display = '';
	} else {
		document.getElementById('donation_box').style.display = 'none';
	}
	
	if(document.getElementById('CH_choice_member1').checked && form.MC_sign_petition.checked) {
		document.getElementById('otherCH').style.display = 'none';
	} else if (document.getElementById('CH_choice_member1').checked && !form.MC_sign_petition.checked) {
		document.getElementById('otherCH').style.display = '';
		document.getElementById('name_CH').style.display = 'none';
		document.getElementById('phone_CH').style.display = 'none';
	} else if (document.getElementById('CH_choice_other').checked) {
		document.getElementById('otherCH').style.display = '';
		document.getElementById('name_CH').style.display = '';
		document.getElementById('phone_CH').style.display = '';
	}
}

function securityBoxInfo()
{
	alert("This is a simple security device to prevent automated 'spam' from entering the database." +
	      " \nPlease type the code exactly as it is written into this box.");
	document.BuyForm.securityBox.focus();
	return false;
}

function donationSelectChange() {
	document.BuyForm.qty.value = document.BuyForm.MC_donation_amount.value;
	if (document.BuyForm.MC_donation_amount.options[0].selected) {
		document.getElementById('donation_select').style.display = 'none';
		document.getElementById('donation_edit').style.display = '';
		document.BuyForm.qty.focus();
	}
}

function donationEditChange() {
	// Check for numerical value
	if(isNaN(document.BuyForm.qty.value) || document.BuyForm.qty.value < 0.01) {
		if(document.BuyForm.qty.value != '') {
			alert('Please enter a number indicating how much you would like to donate.');
			document.BuyForm.qty.value = '';
		}
		document.BuyForm.MC_donation_amount.options[1].selected = true;
		document.getElementById('donation_select').style.display = '';
		document.getElementById('donation_edit').style.display = 'none';
		return;		
	}
	
	// We have a valid value - place it in the select box and continue
	newOption = new Option();
	newOption.text = document.BuyForm.qty.value;	
	newOption.value = document.BuyForm.qty.value;
	document.BuyForm.MC_donation_amount.options[document.BuyForm.MC_donation_amount.length] = newOption;
	document.BuyForm.MC_donation_amount.options[document.BuyForm.MC_donation_amount.length - 1].selected = true;
	document.getElementById('donation_select').style.display = '';
	document.getElementById('donation_edit').style.display = 'none';
}

//populateWPDetails: This populutes the Worldpay fields with the appropriate details.
function populateWPDetails(form) {
	
	// Get the cardholder's details from the correct place
	var title = '';
	var firstname = form.MC_firstname_1.value;
	var middlename = '';
	var surname = form.MC_surname_1.value;
	var address = form.MC_address_1.value;
	var town = form.MC_town_1.value;
	var county = '';
	var postcode = form.MC_postcode_1.value;
	var country = form.MC_country_1.value;
	var home_phone = '';
	var email = form.MC_email_1.value;
    if (window.document.getElementById('CH_choice_other').checked) {
    	title = form.MC_title_CH.value;
    	firstname = form.MC_firstname_CH.value;
    	middlename = form.MC_middlename_CH.value;
    	surname = form.MC_surname_CH.value;
    	address = form.MC_address_CH.value;
    	town = form.MC_town_CH.value;
    	county = form.MC_county_CH.value;
    	postcode = form.MC_postcode_CH.value;
    	country = form.MC_country_CH.value;
    	home_phone = form.MC_home_phone_CH.value;
    	email = form.MC_email_CH.value;
    }
    
    // Address comes from the "cardholder" fields if the petition isn't signed
    if (!form.MC_sign_petition.checked) {
    	address = form.MC_address_CH.value;
    	town = form.MC_town_CH.value;
    	postcode = form.MC_postcode_CH.value;
    	country = form.MC_country_CH.value;
        if (window.document.getElementById('CH_choice_member1').checked) {
        	form.MC_address_1.value = address;
    		form.MC_town_1.value = town;
    		form.MC_postcode_1.value = postcode;
    		form.MC_country_1.value = country;
        }
    }
    
	// Populate name and contact details.
	if (firstname != "" && surname != "") {
   		form.name.value = title + ' '+ firstname + ' ' + middlename + ' ' + surname;
   	} else {
   		form.name.value = "";
   	}
   	if (address != "" && town != "") {
   		form.address.value = address + "\n" + town + "\n" + county;
   	} else {
   		form.address.value = "";
   	}
   	form.postcode.value = postcode;
   	form.country.value = countryName2Code(country);
   	form.tel.value = home_phone;
   	document.BuyForm.email.value = email;
}

function customValidation(form) {
	
	var error_message = "";
	
	// Disable worldpay validation if no donation has been made
	if (!form.donate.checked && !form.MC_sign_petition.checked) {
		form.address.value = ' ';
		form.country.value = ' ';
		form.postcode.value = ' ';
	}
	
	if (!form.MC_free_enewsletter.checked && !form.MC_urgent_response.checked &&
			!form.MC_sign_petition.checked && !form.donate.checked &&
			!form.MC_info_volunteer.checked && !form.MC_info_regular_gift.checked &&
			!form.MC_info_will.checked && !form.MC_info_speaker.checked) {
		error_message += "\nPlease select at least one of the options for supporting Republic.";
	}
	
	if (form.securityBox.value == "") {
		error_message += "\nPlease enter the code into the anti-spam security box.";
	}
	
	return error_message;
}

function customSubmit(form) {
	return;
}

