
// Form validation for non-members' updating their details.
function checkNonMembersForm() {
	var csurname, cfirst_names, chome_email, csubscription_date;
	with (window.document.nonMembersUpdateForm) {
		cfirst_names		= first_names;
		csurname			= surname;
		chome_email			= home_email;
		csubscription_date	= subscription_date;
	}
	
	// Clear any errors around field labels
	cfirst_names.className	='';
	csurname.className		='';
	chome_email.className	='';
	
	var errorExplanations = new Array();

	if(trim(cfirst_names.value) == '') {
		errorExplanations.push('Please enter a First name(s)');
		cfirst_names.className='fieldWithErrors';
		cfirst_names.focus();
	}
	if(trim(csurname.value) == '') {
		errorExplanations.push('Please enter a Surname');
		csurname.className='fieldWithErrors';
		csurname.focus();
	}
	if(trim(chome_email.value) == '') {
		errorExplanations.push('Please enter a home Email address');
		chome_email.className='fieldWithErrors';
		chome_email.focus();
	} else if (!isEmail(trim(chome_email.value))) {
		errorExplanations.push('Home Email address is not valid');
		chome_email.className='fieldWithErrors';
		chome_email.focus();
	}
	if(trim(csubscription_date.value) == '') {
		var now = new Date();
		csubscription_date.value=ISODateString(now);
	}
	
	if (errorExplanations.length>0) {		
		var errors = window.document.getElementById('errors');
		emptyDiv(errors);
		var errorDiv = document.createElement('div');
		errorDiv.setAttribute('id', 'errorExplanation');
		errorDiv.innerHTML = '<h2>' + errorExplanations.length + ' ' + pluralError(errorExplanations) + ' prohibits subscribing/non-subscribing ' + cfirst_names.value + ' ' + csurname.value + '\'s non-membership details</h2>\n';
		errorDiv.innerHTML += '\t<p>' + pluralErrorIntro(errorExplanations) + '</p>\n';
		errorDiv.innerHTML += '\t<ul>' + printErrorList(errorExplanations) + '</ul>\n';
		errors.appendChild(errorDiv);

		return false;
	} else {
		cfirst_names.value			= trim(cfirst_names.value);
		csurname.value				= trim(csurname.value);
		chome_email.value			= trim(chome_email.value);
		csubscription_date.value	= trim(csubscription_date.value);
		return true;
	}
}

// Form validation for members' updating their details.
function checkForm() {
	var csurname, cfirst_names, caddress_1, caddress_2, ctown, ccounty, cpostcode, ccountry, chome_email, cwork_email, chome_tel, cstatus, cwork_tel, cmobile_tel, cjoin_date, cmember_no, ccommunication, cusername, cpassword1, cpassword2;
	with (window.document.membersUpdateForm) {
		cfirst_names		= first_names;
		csurname			= surname;
		caddress_1			= address_1;
		caddress_2			= address_2;
		ctown				= town;
		ccounty				= county;
		cpostcode			= postcode;
		ccountry			= country;
		chome_email			= home_email;
		cwork_email			= work_email;
		chome_tel			= home_tel;
		cwork_tel			= work_tel;
		cmobile_tel			= mobile_tel;
		cstatus				= status;
		cjoin_date			= join_date;
		cmember_no			= member_no;
		ccommunication		= communication;
		cusername			= username;
		cpassword1			= password1;
		cpassword2			= password2;
	}
	var errorExplanations = new Array();

	if(trim(cfirst_names.value) == '') {
		errorExplanations.push('Please enter a First name(s)');
		cfirst_names.className='fieldWithErrors';
		cfirst_names.focus();
	}
	if(trim(csurname.value) == '') {
		errorExplanations.push('Please enter a Surname');
		csurname.className='fieldWithErrors';
		csurname.focus();
	}
	if(trim(cusername.value) == '') {
		errorExplanations.push('Please enter a Username');
		cusername.className='fieldWithErrors';
		cusername.focus();
	}
	if(trim(caddress_1.value) == '' && trim(caddress_2.value) == '' && trim(ctown.value) == '' && trim(ccounty.value) == '' && trim(cpostcode.value) == '' && trim(ccountry.value) == '') {
		errorExplanations.push('Please enter an address');
		caddress_1.className='fieldWithErrors';
		caddress_2.className='fieldWithErrors';
		ctown.className='fieldWithErrors';
		ccounty.className='fieldWithErrors';
		cpostcode.className='fieldWithErrors';
		ccountry.className='fieldWithErrors';
	}
	if(trim(chome_email.value) == '' && trim(cwork_email.value) == '') {
		errorExplanations.push('Please enter an Email address');
		chome_email.className='fieldWithErrors';
		cwork_email.className='fieldWithErrors';
		chome_email.focus();
	} else if (!isEmail(trim(chome_email.value)) && trim(cwork_email.value) == '') {
		errorExplanations.push('Home Email address is not valid');
		chome_email.className='fieldWithErrors';
		chome_email.focus();
	} else if (trim(chome_email.value) == '' && !isEmail(trim(cwork_email.value))) {
		errorExplanations.push('Work Email address is not valid');
		cwork_email.className='fieldWithErrors';
		cwork_email.focus();
	} else if (!isEmail(trim(chome_email.value)) && !isEmail(trim(cwork_email.value))) {
		errorExplanations.push('Work Email address is not valid');
		cwork_email.className='fieldWithErrors';
		cwork_email.focus();
	}
	if(trim(chome_tel.value) == '' && trim(cwork_tel.value) == '' && trim(cmobile_tel.value) == '') {
		errorExplanations.push('Please enter a telephone number');
		chome_tel.className='fieldWithErrors';
		cwork_tel.className='fieldWithErrors';
		cmobile_tel.className='fieldWithErrors';
		chome_tel.focus();
	}
	if(trim(cpassword1.value) == '') {
		errorExplanations.push('Please enter a password');
		cpassword1.className='fieldWithErrors';
		cpassword1.focus();
	}
	if(trim(cpassword2.value) == '') {
		errorExplanations.push('Please reenter a password');
		cpassword2.className='fieldWithErrors';
		cpassword2.focus();
	}
	if(trim(cpassword1.value) != trim(cpassword2.value)) {
		errorExplanations.push('Passwords are not the same.');
		cpassword1.className='fieldWithErrors';
		cpassword2.className='fieldWithErrors';
		cpassword1.focus();
		cpassword2.focus();
	}
	
	if (errorExplanations.length>0) {		
		var errors = window.document.getElementById('errors');
		emptyDiv(errors);
		var errorDiv = document.createElement('div');
		errorDiv.setAttribute('id', 'errorExplanation');
		errorDiv.innerHTML = '<h2>' + errorExplanations.length + ' ' + pluralError(errorExplanations) + ' prohibits updating ' + cfirst_names.value + ' ' + csurname.value + '\'s membership details</h2>\n';
		errorDiv.innerHTML += '\t<p>' + pluralErrorIntro(errorExplanations) + '</p>\n';
		errorDiv.innerHTML += '\t<ul>' + printErrorList(errorExplanations) + '</ul>\n';
		errors.appendChild(errorDiv);
		  
		return false;
	} else {
		cfirst_names.value		= trim(cfirst_names.value);
		csurname.value			= trim(csurname.value);
		caddress_1.value		= trim(caddress_1.value);
		caddress_2.value		= trim(caddress_2.value);
		ctown.value				= trim(ctown.value);
		ccounty.value			= trim(ccounty.value);
		cpostcode.value			= trim(cpostcode.value);
		ccountry.value			= trim(ccountry.value);
		chome_email.value		= trim(chome_email.value);
		cwork_email.value		= trim(cwork_email.value);
		chome_tel.value			= trim(chome_tel.value);
		cwork_tel.value			= trim(cwork_tel.value);
		cmobile_tel.value		= trim(cmobile_tel.value);
		cstatus.value			= trim(cstatus.value);
		cjoin_date.value		= trim(cjoin_date.value);
		cmember_no.value		= trim(cmember_no.value);
		ccommunication.value	= trim(ccommunication.value);
		cusername.value			= trim(cusername.value);
		cpassword1.value		= trim(cpassword1.value);
		cpassword2.value		= trim(cpassword2.value);
		return true;
	}
}

function checkNonMembersMailingListForm() {
	var csurname, cfirst_names, chome_email;
	with (window.document.nonMembersMailingListForm) {
		cfirst_names		= first_names;
		csurname			= surname;
		chome_email			= home_email;
	}
	var errorExplanations = new Array();

	if(trim(cfirst_names.value) == '') {
		errorExplanations.push('Please enter a First name(s)');
		cfirst_names.className='fieldWithErrors';
		cfirst_names.focus();
	}
	if(trim(csurname.value) == '') {
		errorExplanations.push('Please enter a Surname');
		csurname.className='fieldWithErrors';
		csurname.focus();
	}
	if(trim(chome_email.value) == '') {
		errorExplanations.push('Please enter an Email address');
		chome_email.className='fieldWithErrors';
		chome_email.focus();
	} else if (!isEmail(trim(chome_email.value))) {
		errorExplanations.push('Home Email address is not valid');
		chome_email.className='fieldWithErrors';
		chome_email.focus();
	}
	if (errorExplanations.length>0) {		
		var errors = window.document.getElementById('errors');
		emptyDiv(errors);
		var errorDiv = document.createElement('div');
		errorDiv.setAttribute('id', 'errorExplanation');
		errorDiv.innerHTML = '<h2>' + errorExplanations.length + ' ' + pluralError(errorExplanations) + ' prohibits sending an email to request to subscribe or unsubscribe to non-members mailing list</h2>\n';
		errorDiv.innerHTML += '\t<p>' + pluralErrorIntro(errorExplanations) + '</p>\n';
		errorDiv.innerHTML += '\t<ul>' + printErrorList(errorExplanations) + '</ul>\n';
		errors.appendChild(errorDiv);
		  
		return false;
	} else {
		cfirst_names.value		= trim(cfirst_names.value);
		csurname.value			= trim(csurname.value);
		chome_email.value		= trim(chome_email.value);
		return true;
	}
}
function pluralError (errorArray) {
	if (errorArray.length>1)
		return 'errors';
	else
		return 'error';
}

function pluralErrorIntro (errorArray) {
	if (errorArray.length>1)
		return 'There are problems with the following fields:';
	else
		return 'There is a problem with the following field:';
}

function emptyDiv (divToClear) {
	var i;

	while (i=divToClear.childNodes[0]) {
		if (i.nodeType == 1 || i.nodeType == 3){
			divToClear.removeChild(i);
		}
	}
}

function printErrorList(errorArray) {
	var errorList = '';

	for (var i=0; i<errorArray.length; i++) {
		errorList += '\t\t<li>' + errorArray[i] + '</li>\n';
	}
	
	return errorList;
}

function trim(str) {
	return str.replace(/^\s+|\s+$/g,'');
}

function isEmail(str) {
	var regex = /^[-_.a-z0-9]+@(([-_a-z0-9]+\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i;

	return regex.test(str);
}

function pad(n) {
	return n<10 ? '0'+n : n
}

function ISODateString(d){
	return d.getUTCFullYear()+'-'
      + pad(d.getUTCMonth())+'-'
      + pad(d.getUTCDate())
}

/**
 *
 * Return webmaster's email address
 * Trying to reduce SPAM.
 *
 **/
function webmasterEmailAddress() {
	var webmasterArray = new Array(119,101,98,109,97,115,116,101,114,64,103,108,97,115,103,111,119,106,109,99,115,46,111,114,103,46,117,107);
	var webmasterEmailVar= "";

	for (i = 0; i < webmasterArray.length; i++) {
		webmasterEmailVar += String.fromCharCode(webmasterArray[i]);
	}
	return webmasterEmailVar;
}

function getEmailAddress() {
	document.getElementById('emailAddress').innerHTML = '<a href="mailto:'+webmasterEmailAddress()+'">Webmaster<\/a>';
}

function putEmailAddress() {
	document.write('<a href="mailto:'+webmasterEmailAddress()+'">Webmaster<\/a>');
}

