var MembershipPrice = new Array(
0.00,
36.00,
42.00
);

var MembershipAreas = new Array(
"Select a region",
"UK & Europe",
"Rest of the world"
);

var MembershipTypes = new Array(
"Select a Membership Type",
"New Member",
"Existing Member"
);

function setValue(sName, sValue)
{
	document.cookie = sName + "=" + escape(sValue) + "; ";
}

function getValue(sName)
{
	var aCookie = document.cookie.split("; ");
	for (var i=0; i < aCookie.length; i++)
	{
		var aCrumb = aCookie[i].split("=");
		if(sName == aCrumb[0])
		{
			if(aCrumb[1] == null)
				return "";
			else
				return unescape(aCrumb[1]);
		}
	}
	return "";
}
function formatPrice(price)
{
	return price.toFixed(2);
}
function formatPoundPrice(price)
{
	return '&pound;' + formatPrice(price);
}
function writePrice(price)
{
	document.write(formatPrice(price));
}
function writePoundPrice(price)
{
	document.write(formatPoundPrice(price));
}

function getNumber(value)
{
	var intVal = parseInt(value);
	if(isNaN(intVal))
	{
		intVal = 0;
	}
	return intVal;
}

function removeAll()
{
	var value = getValue("a");
	var idx = firstItem(value);
	if(idx >= 0)
	{
		setValue("a", "");
		location.reload(false);
	}
}

function cookiesEnabled()
{
	var enabled = 1;
	if(document.cookie == "")
	{
		setValue("a", ":");
		if(document.cookie == "")
		{
			enabled = 0;
			alert("You must have cookies enabled - check your browser settings");
		}
		setValue("a", "");
	}
	return enabled;
}
function getMembershipYear()
{
  var date = new Date();
  var year = date.getFullYear()+2;
  if(date.getMonth() == 0)
  {
    year--;
  }
  var str = "";
  str += year;
  return str;
}
function collectInfo()
{
	var valid = 1;
	var mn = document.input.number.value;
	setValue("mn", mn);
	var tl = document.input.title.value;
	setValue("tl", tl);
	var nm = document.input.name.value;
	setValue("nm", nm);
	var ad = document.input.address.value;
	setValue("ad", ad);
	var em = document.input.email.value;
	setValue("em", em);
	var ga = "";
	if(document.input.giftaid.checked)
	{
		ga = "1";
	}
	setValue("ga", ga);

	var ma = getNumber(getValue("ma"));
	var mt = getNumber(getValue("mt"));
	if(ma == 0)
	{
		alert('Please select a region');
		valid = 0;
	}
	else if(mt == 0)
	{
		alert('Please select a membership type');
		valid = 0;
	}
	else if(mt == 2 && mn.length == 0)
	{
		alert('Please give your membership number or insert a question mark');
		valid = 0;
	}
	else if(nm.length == 0)
	{
		alert('Please give your name');
		valid = 0;
	}
	else if(ad.length == 0)
	{
		alert('Please give your address');
		valid = 0;
	}
	return valid;
}

function setInfo()
{
	var mn = getValue("mn");
	document.input.number.value = mn;
	var tl = getValue("tl");
	document.input.title.value = tl;
	var nm = getValue("nm");
	document.input.name.value = nm;
	var ad = getValue("ad");
	document.input.address.value = ad;
	setValue("ad", ad);
	var em = getValue("em");
	document.input.email.value = em;
	var ga = getValue("ga");
	document.input.giftaid.checked = (ga.length > 0);
}
function resetAll()
{
	setValue("mn", "");
	setValue("tl", "");
	setValue("nm", "");
	setValue("ad", "");
	setValue("em", "");
	setValue("ga", "");
	setValue("ma", "");
	setValue("mt", "");
	location.reload(false);
}

function payPalString()
{
	var string;
	if(collectInfo())
	{
		var ma = getNumber(getValue("ma"));

		var number = getValue("mn");
		var title = getValue("tl");
		var name = getValue("nm");
		var address = getValue("ad");
		var email = getValue("em");
		var ga = getValue("ga");

		string = 'https://www.paypal.com/cgi-bin/webscr';
		string += '?cmd=_cart';
		string += '&upload=1';
		string += '&business=membership@nifhs.org';

		var estring;
		if(number.length > 0)
		{
			estring = "Membership Renewal ";
			estring += number;
		}
		else
		{
			estring = "New Member";
		}
		estring += " ";
		estring += getMembershipYear();

		string += '&item_name_1';
		string += '=';
		string += escape(estring);
		string += '&amount_1';
		string += '=';
		string += formatPrice(MembershipPrice[ma]);
		string += '&currency_code=GBP';

		string += '&on0_1';
		string += '=';
		string += escape('Subscription Address');
		estring = title;
		if(title.length)
		{
			estring += " ";
		}
		estring += name;
		estring += "\n";
		estring += address;
		string += '&os0_1';
		string += '=';
		string += escape(estring);

		if(email.length)
		{
			string += '&on1_1';
			string += '=';
			string += escape('Email');
			estring = email;
			string += '&os1_1';
			string += '=';	
			string += escape(estring);
		}
		if(ga.length)
		{
			string += '&on2_1';
			string += '=';
			string += escape('Gift Aid');
			estring = email;
			string += '&os2_1';
			string += '=';	
			string += escape('UK Tax Payer');
		}
	}
	else
	{
		string = document.location;
	}
	return string;
}

function payPal()
{
	document.write('<input type="submit" name="paypal" value="Online via PayPal" ');
	document.write('onClick="javascript:document.location = payPalString()"');
	document.writeln(' />');
}

function formString(str)
{
	var string = str
	if(!collectInfo())
	{
		string = document.location;
	}
	return string;
}

function paymentMethods()
{
	document.writeln('<p></p><table align="center" border="0"><tr><th colspan="3">Pay by PayPal</th></tr><tr>');

	document.writeln('<td>');
	payPal();
	document.writeln('</td></tr><tr><td></td><td align="center">');

	document.writeln('<input type="button" name="reset" value="Reset Form" onClick="resetAll()" />');

	document.writeln('</td></tr></table><p></p>');
}

function updateMembershipArea(ma)
{
	var price = document.getElementById("price");
	if(MembershipPrice[ma])
	{
		price.innerHTML = "Fee: " + formatPoundPrice(MembershipPrice[ma]);
	}
	else
	{
		price.innerHTML = "";
	}
}
function updateMembershipType(mt)
{
	var num = document.getElementById("num");
	if(mt == 2)
	{
		num.style.visibility = "visible"
	}
	else
	{
		num.value = "";
		num.style.visibility = "hidden"
	}
}

function writeMembershipYear()
{
	document.write('<tr><td>Membership Years 2010-2012, ending:</td><td>');
	document.write(getMembershipYear());
	document.writeln('</td></tr>');

}

function writeMembershipPrices()
{
	document.writeln('<h3>Annual Subscriptions</h3>');
	document.writeln('<table>');
	for( idx = 0; idx < MembershipAreas.length; idx++ )
	{
		if(MembershipPrice[idx])
		{
			document.write('<tr><td>');
			document.write(MembershipAreas[idx]);
			document.write(' Membership</td><td>');
			writePoundPrice(MembershipPrice[idx]);
			document.writeln('</td></tr>');
		}
	}
	document.writeln('</table>');
}

function writeMembershipArea()
{
	var value = getValue("ma");
	var ma = getNumber(value);
	document.write('<tr><td>Region:</td><td>');
	document.writeln('<select name="area" onChange="selectMembershipArea()">');
	for( idx = 0; idx < MembershipAreas.length; idx++ )
	{
		document.write('<option value="');
		document.write(idx + '">' + MembershipAreas[idx]);
		document.writeln('</option>');
	}
	document.writeln('</select></td>');
	document.writeln('<td><div id="price"></div></td></tr>');
	document.input.area.selectedIndex = ma;
	updateMembershipArea(ma);
}

function writeMembershipType()
{
	var value = getValue("mt");
	var mt= getNumber(value);

	document.write('<tr><td>Membership type:</td><td>');
	document.writeln('<select name="type" onChange="selectMembershipType()">');
	for( idx = 0; idx < MembershipTypes.length; idx++ )
	{
		document.write('<option value="');
		document.write(idx + '">' + MembershipTypes[idx]);
		document.writeln('</option>');
	}
	document.writeln('</select></td>');
	document.writeln('<td><div id="num">Membership Number: <input width="6" id="number"></input></div></td></tr>');
	document.input.type.selectedIndex = mt;
	updateMembershipType(mt);
}
function writeAddressForm()
{
	document.writeln('<tr><td colspan="3"><br></td></tr>');
	document.writeln('<tr><td colspan="3"><h3>Membership Address</h3></td></tr>');
	document.writeln('<tr><td colspan="3">Please give your contact details. This is the address the Society will send your journal to (the payment billing address can be different).</td></tr>');
	document.writeln('<tr><td>Title:</td><td colspan="2"><input size="10" id=title></input></td></tr>');
	document.writeln('<tr><td>Name:</td><td colspan="2"><input size="50" id=name></input></td></tr>');
	document.writeln('<tr><td>Address:</td><td colspan="2">');
	document.writeln('<textarea cols="40" rows="6" id="address"></textarea>');
	document.writeln('</input></td></tr>');
	document.writeln('<tr><td>Email:</td><td colspan="2"><input size="50" id=email></input></td></tr>');
}
function writeGiftAidMessage()
{
	document.writeln('<font size="5">[please tick] I am a UK taxpayer and I would like the North of Ireland Family History Society to treat all my subscriptions/donations, until I notify them otherwise, as Gift Aid donations.</font>');
	document.writeln('<br><small>The amount of Income Tax I pay each year will be greater than the Gift Aid claimed.</small>');
}
function writeGiftAid()
{
	document.writeln('<tr><td colspan="3"><br></td></tr>');
	document.writeln('<tr><td colspan="3"><h3>Gift Aid</h3></td></tr>');
	document.writeln('<tr><td colspan="3"><small>If you are a UK taxpayer, the value of your subscription can be increased by almost a third under the Gift Aid scheme at no extra cost to you. To benefit the Society in this way, please simply <u>tick the box below</u> to join the Gift Aid scheme.</small></td></tr>');
	document.writeln('<tr><td colspan="3"><input type="checkbox" style="width: 40; height: 40;" id="giftaid"></input> ');
	writeGiftAidMessage();
	document.writeln('</td></tr>');
}
function writeOrder()
{
	var ma = getNumber(getValue("ma"));
	var mt = getNumber(getValue("mt"));
	var title = getValue("tl");
	var name = getValue("nm");
	var address = getValue("ad");
	var email = getValue("em");
	var ga = getValue("ga");

	document.writeln('<table align="center" border="1">');
	document.writeln('<tr><td>');
	if(mt == 2)
	{
		document.writeln('Membership Renewal:');
		document.writeln('</td><td>');
		var mn = getNumber(getValue("mn"));
		document.writeln(mn);
	}
	else
	{
		document.writeln('New Member');
	}
	document.write('</td><td>');
	document.write(getMembershipYear());
	document.writeln('</td></tr><tr><td>');

	if(name.length)
	{
		document.writeln('Name:');
		document.writeln('</td><td>');
		if(title.length)
		{
			document.writeln(title);
			document.writeln(" ");
		}
		document.writeln(name);
		document.writeln('</td></tr><tr><td>');
	}
	if(address.length)
	{
		document.writeln('Address:');
		document.writeln('</td><td>');
		while(address.indexOf("\n") != -1)
		{
			address = address.replace("\n", "<br>");
		}
		document.writeln(address);
		document.writeln('</td></tr><tr><td>');
	}
	if(email.length)
	{
		document.writeln('Email:');
		document.writeln('</td><td>');
		document.writeln(email);
		document.writeln('</td></tr><tr><td>');
	}
	if(ga.length)
	{
		document.writeln('Gift Aid:');
		document.writeln('</td><td>');
		writeGiftAidMessage();
		document.writeln('</td></tr><tr><td>');
	}
	document.writeln('<b>Total Payment</b></td><td><b>');
	writePoundPrice(MembershipPrice[mt]);
	document.writeln('</td></tr></table>');
}
function writeForm()
{
	document.writeln('<form name="input">');
	document.writeln('<table width="100%">');
	writeMembershipYear();
	writeMembershipArea();
	writeMembershipType();
	writeAddressForm();
	writeGiftAid();
	setInfo();
	document.writeln('</table>');
	document.writeln('</form>');
	paymentMethods();
}
function selectMembershipArea()
{
	var ma = document.input.area.value;
	setValue("ma", ma);
	updateMembershipArea(ma);
	updatePayment();
}
function selectMembershipType()
{
	var mt = document.input.type.value;
	setValue("mt", mt);
	updateMembershipType(mt);
	updatePayment();
}

