// Copyright (c) 2008 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.
//
// newjoin.js
// Nov 17, 2008
//
// Javascript specific to the New Join page.

// Handler for when the user clicks of the get_erf checkbox.
function getERFClicked() {
	getERF = document.getElementById("get_erf");
	selectERFQty = document.getElementById("select_erf_qty");
	if(getERF.checked) {
		selectERFQty.disabled = false;
		setERFQty(selectERFQty.value);
	} else {
		selectERFQty.disabled = true;
		setERFQty(0);
	}
	calculate(document.BuyForm);
}

// Sets quantity of "Ending the Royal Farce" the user wants to buy
function setERFQty(value) {
	qty = document.BuyForm.qty;
	// Only one item for sale, finding the correct variable is trivial
	if(qty.length == undefined) {
		qty.value = value;
		return;
	}
	
	// Multiple items for sale - search for the correct variable.
	for (i=0; i<qty.length; i++) {
		if (document.BuyForm.code[i].value == "copy(ies) of 'Ending the Royal Farce'") {
			qty[i].value = value;
			return;
		}
	}
}

// Handler for when the quantity select box is changed
function selectERFQtyChanged() {
	selectERFQty = document.getElementById("select_erf_qty");
	setERFQty(selectERFQty.value);
	calculate(document.BuyForm);
}

// Explains Network 25
function whatsNetwork25() {
	alert("Explain Network '25 here.");
	return false;
}

// Clears all the membership rate radio boxes.
function clearMembershipRate() {
	var rateBox = document.BuyForm.MC_membership_rate_1;
	var r;
	var rLength = rateBox.length;

	for (r=0; r<rLength; r++) {
		rateBox[r].checked = false;
	}
}

// Handles annual recurring payment frequency choice
function annualRecurringClicked() {
	document.getElementById("select_payment_monthly").className = "select_payment_type";
	document.getElementById("select_payment_annual_recurring").className = "select_payment_type_clicked";
	document.getElementById("select_payment_annual_one_off").className = "select_payment_type";
	document.getElementById("monthly_rate").className = "hidden";
	document.getElementById("annual_recurring_rate").className = "active";
	document.getElementById("annual_one_off_rate").className = "hidden";
	document.getElementById("network_25_rate").style.display = "none";
	for(i=1; i<7; i++) {
		document.getElementById("monthly_rate_"+i).className = "hidden";
		document.getElementById("annual_rate_"+i).className = "active";
	}
	document.getElementById("one_off_gift").className = "hidden";
	clearMembershipRate();
	document.BuyForm.MC_membership_rate_other.value = "";
	document.BuyForm.MC_payment_type_1.value = "futurepay annual online";
	document.getElementById("donation_qty").value = 0;
}

function monthlyClicked() {
	document.getElementById("select_payment_monthly").className = "select_payment_type_clicked";
	document.getElementById("select_payment_annual_recurring").className = "select_payment_type";
	document.getElementById("select_payment_annual_one_off").className = "select_payment_type";
	document.getElementById("monthly_rate").className = "active";
	document.getElementById("annual_recurring_rate").className = "hidden";
	document.getElementById("annual_one_off_rate").className = "hidden";
	document.getElementById("network_25_rate").style.display = "";
	for(i=1; i<7; i++) {
		document.getElementById("monthly_rate_"+i).className = "active";
		document.getElementById("annual_rate_"+i).className = "hidden";
	}
	document.getElementById("one_off_gift").className = "active";
	clearMembershipRate();
	document.BuyForm.MC_membership_rate_other.value = "";
	document.BuyForm.MC_payment_type_1.value = "futurepay monthly online";
}

function annualOneOffClicked() {
	document.getElementById("select_payment_monthly").className = "select_payment_type";
	document.getElementById("select_payment_annual_recurring").className = "select_payment_type";
	document.getElementById("select_payment_annual_one_off").className = "select_payment_type_clicked";
	document.getElementById("monthly_rate").className = "hidden";
	document.getElementById("annual_recurring_rate").className = "hidden";
	document.getElementById("annual_one_off_rate").className = "active";
	document.getElementById("network_25_rate").style.display = "none";
	for(i=1; i<7; i++) {
		document.getElementById("monthly_rate_"+i).className = "hidden";
		document.getElementById("annual_rate_"+i).className = "active";
	}
	document.getElementById("one_off_gift").className = "hidden";
	clearMembershipRate();
	document.BuyForm.MC_membership_rate_other.value = "";
	document.BuyForm.MC_payment_type_1.value = "worldpay";
	document.getElementById("donation_qty").value = 0;
}

// Standard membership rate radio box clicked - validate accordingly.
function stdMembershipRateRadioClicked() {
	document.BuyForm.MC_membership_type_1 = "standard";
	document.BuyForm.MC_membership_rate_other.value = "";
	calculate(document.BuyForm);
}

// Other cardholder radio button clicked - display other cardholder details
function CHchoiceOtherClick() {
	window.document.getElementById('cardHolderDetails').style.display='';
}

// Cardholder is myself radio button clicked - hide other cardholder details
function CHchoiceMeClick() {
	window.document.getElementById('cardHolderDetails').style.display='none';
}

// Other membership rate has been changed - validate accordingly
function membershipRateOtherChanged() {
	other = document.BuyForm.MC_membership_rate_other;
	if (other.value == "") {
		return;
	}
	if (isNaN(other.value) || other.value <= 0) {
		alert("Please enter a positive number.");
		other.value = "";
		other.focus();
	}
	clearMembershipRate();
}

// Returns the membership rate selected by the user
function getMembershipRate() {
	var rate = 0;
	var rateBox = document.BuyForm.MC_membership_rate_1;
	var r = 0;
	var rLength = rateBox.length;
	
	for (r=0; r<rLength; r++) {
		if (rateBox[r].checked) {
			rate = rateBox[r].value;
		}
	}
	
	other = document.BuyForm.MC_membership_rate_other.value;
	if(other != "" && !isNaN(other) && other > 0) {
		rate = other;
	}
	
	return rate;
}

// Saves all the details of the BuyForm for later restoration
function saveFormDetails() {
	var details = new Array();

	details.push(document.BuyForm.MC_title_1.value);
	details.push(document.BuyForm.MC_firstname_1.value);
	details.push(document.BuyForm.MC_middlename_1.value);
	details.push(document.BuyForm.MC_surname_1.value);
	details.push(document.BuyForm.MC_address_1.value);
	details.push(document.BuyForm.MC_town_1.value);
	details.push(document.BuyForm.MC_county_1.value);
	details.push(document.BuyForm.MC_postcode_1.value);
	details.push(document.BuyForm.MC_country_1.value);
	details.push(document.BuyForm.MC_home_phone_1.value);
	details.push(document.BuyForm.MC_work_phone_1.value);
	details.push(document.BuyForm.MC_mobile_phone_1.value);
	details.push(document.BuyForm.MC_email_1.value);
	
	details.push(document.BuyForm.MC_title_CH.value);
	details.push(document.BuyForm.MC_firstname_CH.value);
	details.push(document.BuyForm.MC_middlename_CH.value);
	details.push(document.BuyForm.MC_surname_CH.value);
	details.push(document.BuyForm.MC_address_CH.value);
	details.push(document.BuyForm.MC_town_CH.value);
	details.push(document.BuyForm.MC_county_CH.value);
	details.push(document.BuyForm.MC_postcode_CH.value);
	details.push(document.BuyForm.MC_country_CH.value);
	details.push(document.BuyForm.MC_home_phone_CH.value);
	details.push(document.BuyForm.MC_work_phone_CH.value);
	details.push(document.BuyForm.MC_mobile_phone_CH.value);
	details.push(document.BuyForm.MC_email_CH.value);
	
	if(document.getElementById("role_e_newsletter") != null) {
		details.push(document.getElementById("role_e_newsletter").checked);
	}
	if(document.getElementById("role_urn") != null) {
		details.push(document.getElementById("role_urn").checked);
	}

	return details;
}

// Restores form details to a previously saved state.
function restoreFormDetails(details) {
	if(document.getElementById("role_urn") != null) {
		document.getElementById("role_urn").checked = details.pop();
	}
	if(document.getElementById("role_e_newsletter") != null) {
		document.getElementById("role_e_newsletter").checked = details.pop();
	}
	
	document.BuyForm.MC_email_CH.value = details.pop();
	document.BuyForm.MC_mobile_phone_CH.value = details.pop();
	document.BuyForm.MC_work_phone_CH.value = details.pop();	
	document.BuyForm.MC_home_phone_CH.value = details.pop();
	document.BuyForm.MC_country_CH.value = details.pop();
	document.BuyForm.MC_postcode_CH.value = details.pop();
	document.BuyForm.MC_county_CH.value = details.pop();
	document.BuyForm.MC_town_CH.value = details.pop();
	document.BuyForm.MC_address_CH.value = details.pop();
	document.BuyForm.MC_surname_CH.value = details.pop();
	document.BuyForm.MC_middlename_CH.value = details.pop();
	document.BuyForm.MC_firstname_CH.value = details.pop();
	document.BuyForm.MC_title_CH.value = details.pop();

	document.BuyForm.MC_email_1.value = details.pop();
	document.BuyForm.MC_mobile_phone_1.value = details.pop();
	document.BuyForm.MC_work_phone_1.value = details.pop();	
	document.BuyForm.MC_home_phone_1.value = details.pop();
	document.BuyForm.MC_country_1.value = details.pop();
	document.BuyForm.MC_postcode_1.value = details.pop();
	document.BuyForm.MC_county_1.value = details.pop();
	document.BuyForm.MC_town_1.value = details.pop();
	document.BuyForm.MC_address_1.value = details.pop();
	document.BuyForm.MC_surname_1.value = details.pop();
	document.BuyForm.MC_middlename_1.value = details.pop();
	document.BuyForm.MC_firstname_1.value = details.pop();
	document.BuyForm.MC_title_1.value = details.pop();
}

// Outputs the FuturePay parameters onto the form.
// Interval is set to '3' for monthly, and '4' for annually.
function writeFuturePayParameters(interval) {
	details = saveFormDetails();
	rate = getMembershipRate();
	html = '<input type="hidden" name="futurePayType" value="regular" />';
	html += '<input type="hidden" name="option" value="0" />';
	html += '<input type="hidden" name="intervalMult" value="1" />';
	html += '<input type="hidden" name="intervalUnit" value="'+interval+'" />';
	html += '<input type="hidden" name="startDelayMult" value="1" />';	
	html += '<input type="hidden" name="startDelayUnit" value="'+interval+'" />';
	html += '<input type="hidden" name="normalAmount" value="'+rate+'" />';
	document.BuyForm.innerHTML = html + document.BuyForm.innerHTML;
	restoreFormDetails(details);
}

// populateWPDetails: This populutes the Worldpay fields with the appropriate details.
function populateWPDetails(form) {
	
	// Get the cardholder's details from the correct place
	var title = form.MC_title_1.value;
	var firstname = form.MC_firstname_1.value;
	var middlename = form.MC_middlename_1.value;
	var surname = form.MC_surname_1.value;
	var address = form.MC_address_1.value;
	var town = form.MC_town_1.value;
	var county = form.MC_county_1.value;
	var postcode = form.MC_postcode_1.value;
	var country = form.MC_country_1.value;
	var home_phone = form.MC_home_phone_1.value;
	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;
    }
    
	// 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;
   	form.email.value = email;

   	// Populate membership rate details. 
    document.getElementById("membership_price").value = getMembershipRate();
}

// Custom validations for this screen only.  If no errors, returns an empty string, otherwise
// returns an error message.
function customValidation(form) {
	var error_message = "";

	// Ensure that a membership rate has been nominated.
	if (!getMembershipRate()) {
		error_message += "\nPlease select a membership rate.";
	}
	
	// Validate cardholder name and contact detail fields, if cardholder is different person
	if (window.document.getElementById('CH_choice_other').checked) {
		if(form.MC_firstname_1.value == "" || form.MC_surname_1.value == "") {
			error_message += "\nPlease enter your full name.";
		}
		if(form.MC_address_1.value == "" || form.MC_town_1.value == "") {
			error_message += "\nPlease enter your complete address.";
		}
		if(form.MC_country_1.value == "" || form.MC_country_1.value == undefined) {
			error_message += "\nPlease select your country of residence.";
		}
		if(form.MC_postcode_1.value == "") {
			error_message += "\nPlease enter your postcode.";
		}
		if(form.MC_email_1.value == "") {
			error_message += "\nPlease enter your e-mail address.";
		}

		if(form.MC_firstname_CH.value == "" || form.MC_surname_CH.value == "") {
			error_message += "\nPlease enter the cardholder's full name.";
		}
		if(form.MC_address_CH.value == "" || form.MC_town_CH.value == "") {
			error_message += "\nPlease enter the cardholder's complete address.";
		}
		if(form.MC_country_CH.value == "" || form.MC_country_CH.value == undefined) {
			error_message += "\nPlease select the cardholder's country.";
		}
		if(form.MC_postcode_CH.value == "") {
			error_message += "\nPlease enter the cardholder's postcode.";
		}
		if(form.MC_email_CH.value == "") {
			error_message += "\nPlease enter the cardholder's e-mail address.";
		}
		
		// Disable wordpay validation if an error has already been found
		// Fudge it by inserting spaces to give the appearance of valid fields
		if (error_message != "") {
			form.name.value = ' ';
			form.address.value = ' ';
			form.country.value = ' ';
			form.postcode.value = ' ';
			form.email.value = ' ';
		}
	}
	
	return error_message;
}

// Custom tasks that need to be done before submission to WorldPay.
function customSubmit(form) {
   	var rate = getMembershipRate();
   	
	if(document.getElementById("monthly_rate").className == "active") {
		writeFuturePayParameters(3);   		
   	} else if (document.getElementById("annual_recurring_rate").className == "active") {
   		writeFuturePayParameters(4);
   	} else { // Annual one-off rate
   	}
   	
   	// Fudge "other" membership rate
   	clearMembershipRate();
   	document.BuyForm.MC_membership_rate_1[0].checked = true;
   	document.BuyForm.MC_membership_rate_1[0].value = rate;
   	
   	// Fill in the roles as a string.
   	if (document.BuyForm.roles_1 == undefined) { return; }
   	var i=0;
   	while (i < document.BuyForm.roles_1.length && !document.BuyForm.roles_1[i].checked) { i++; }
   	if (i >= document.BuyForm.roles_1.length) { return; }
   	document.BuyForm.MC_roles_1.value = "_MultiselectFieldArrayAsString_:" + 
   											document.BuyForm.roles_1[i].value;
   	i++;
	while (i < document.BuyForm.roles_1.length) {
		if (document.BuyForm.roles_1[i].checked) {
			document.BuyForm.MC_roles_1.value += "_Joiner_" + document.BuyForm.roles_1[i].value;
		}
		i++;
	} 
}
