window.onload = initialise;

function initialise()
{
	var objForms = document.getElementsByTagName('form');
	var iCounter;

	// Attach an event handler for each form
	for (iCounter=0; iCounter<objForms.length; iCounter++)
	{
		// only need for membership app form
		if (objForms[iCounter].name == "membershipApp")
		objForms[iCounter].onsubmit = function(){return validateForm(this);};
	}
	
}


// Event handler for the form
function validateForm(objForm)
{
	var arClass = [];
	var iErrors = 0;
	var objField = objForm.getElementsByTagName('input');
	var objLabel = objForm.getElementsByTagName('label');
	var objList = document.createElement('ol');
	var objError, objExisting, objNew, objTitle, objParagraph, objAnchor, objPosition;
	var strLinkID, iFieldCounter, iClassCounter, iCounter;


	// Get the id or name of the form, to make a unique
	// fragment identifier
	if (objForm.id)
	{
		strLinkID = objForm.id + 'ErrorID';
	}
	else
	{
		strLinkID = objForm.name + 'ErrorID';
	}

	// Iterate through input form controls, looking for validation classes
	for (iFieldCounter=0; iFieldCounter<objField.length; iFieldCounter++)
	{
		// Get the class for the field, and look for the appropriate class
		arClass = objField[iFieldCounter].className.split(' ');
		for (iClassCounter=0; iClassCounter<arClass.length; iClassCounter++)
		{
			switch (arClass[iClassCounter])
			{
				case 'string':
					if (!isString(objField[iFieldCounter].value, arClass))
					{
						if (iErrors === 0)
						{
							logError(objField[iFieldCounter], objLabel, objList, strLinkID);
						}
						else
						{
							logError(objField[iFieldCounter], objLabel, objList, '');
						}
						iErrors++;
					}
					break;
				case 'number':
					if (!isNumber(objField[iFieldCounter].value, arClass))
					{
						if (iErrors === 0)
						{
							logError(objField[iFieldCounter], objLabel, objList, strLinkID);
						}
						else
						{
							logError(objField[iFieldCounter], objLabel, objList, '');
						}
						iErrors++;
					}
					break;

				case 'email' :
					if (!isEmail(objField[iFieldCounter].value, arClass))
					{
						if (iErrors === 0)
						{
							logError(objField[iFieldCounter], objLabel, objList, strLinkID);
						}
						else
						{
							logError(objField[iFieldCounter], objLabel, objList, '');
						}
						iErrors++;
					}
					break;
					
				case 'phone':
					if (!isPhone(objField[iFieldCounter].value, arClass))
					{
						if (iErrors === 0)
						{
							logError(objField[iFieldCounter], objLabel, objList, strLinkID);
						}
						else
						{
							logError(objField[iFieldCounter], objLabel, objList, '');
						}
						iErrors++;
					}
					break;	
			}
		}
	}
// Iterate through radio form controls, looking for radio inputs
// radios are named MembershipType, Title, contactme and Paymentoptions
//contactme special case
// if user changed from default contact me by email, check related entries exist
//var contactchoice = ;

switch (getRadioArrayValue("contactme"))
			{
				case 'phone':
					//chose phone - check Telephone
					var relatedentry = document.getElementById("Telephone");
					if (!isPhone(relatedentry.value, arClass))
					{
						if (iErrors === 0)
						{
							logError(relatedentry, objLabel, objList, strLinkID);
						}
						else
						{
							logError(relatedentry, objLabel, objList, '');
						}
						iErrors++;
					}
					break;
				case 'mobile':
					//chose mobile - check Mobile
					var relatedentry = document.getElementById("Mobile");
					if (!isPhone(relatedentry.value, arClass))
					{
						if (iErrors === 0)
						{
							logError(relatedentry, objLabel, objList, strLinkID);
						}
						else
						{
							logError(relatedentry, objLabel, objList, '');
						}
						iErrors++;
					}
					break;
				case 'email':
					break;
			}

var arry1 = document.getElementsByName("MembershipType");
var radio1 = document.getElementById(arry1[1].id);
	
if (!checkRadioArray("MembershipType")){

	if (iErrors === 0)
			{
			// use the first input for focus
			logError(radio1, objLabel, objList, strLinkID);
			}
			else
			{
			logError(radio1, objLabel, objList, '');
			}
			iErrors++;
	
	} else {
		//one was checked, but if it is 'family', need secondname
		 	if (radio1.checked){
		 		//then second name must not be empty
				var relatedentry = document.getElementById("SecondName");
				if (!isString(relatedentry.value, arClass))
					{
						if (iErrors === 0)
						{
							logError(relatedentry, objLabel, objList, strLinkID);
						}
						else
						{
							logError(relatedentry, objLabel, objList, '');
						}
						iErrors++;
					}
		 	 	}
		 
	}
			
			
	if (!checkRadioArray("Title")){
	var arry2 = document.getElementsByName("Title");
	var radio2 = document.getElementById(arry2[0].id);
		if (iErrors === 0)
			{
			// use the first field 
			logError(radio2, objLabel, objList, strLinkID);
			}
			else
			{
			logError(radio2, objLabel, objList, '');
			}
			iErrors++;
			}
			
	if (!checkRadioArray("Paymentoptions")){
	var arry3 = document.getElementsByName("Paymentoptions");
	var radio3 = document.getElementById(arry3[0].id);
		if (iErrors === 0)
			{
			// use the first field 
			logError(radio3, objLabel, objList, strLinkID);
			}
			else
			{
			logError(radio3, objLabel, objList, '');
			}
			iErrors++;
			}
			
	if (iErrors > 0)
	{
		// If not valid, display error messages
		objError = objForm.getElementsByTagName('div');
		
		// Look for existing errors
		for (iCounter=0; iCounter<objError.length; iCounter++)
		{
			if (objError[iCounter].className == 'validationerrors')
			{
				objExisting = objError[iCounter];
			}
		}

		objNew = document.createElement('div');
		objTitle = document.createElement('h2');
		objParagraph = document.createElement('p');
		objAnchor = document.createElement('a');

		if (iErrors == 1)
		{
			objAnchor.appendChild(document.createTextNode('1 Error in Submission'));
		}
		else
		{
			objAnchor.appendChild(document.createTextNode(iErrors + ' Errors in Submission'));
		}
		objAnchor.href = '#' + strLinkID;
		objAnchor.className = 'submissionerror';

		objTitle.appendChild(objAnchor);
		objParagraph.appendChild(document.createTextNode('Please review the following'));
		objNew.className = 'validationerrors';

		objNew.appendChild(objTitle);
		objNew.appendChild(objParagraph);
		objNew.appendChild(objList);
		
		// If there were existing error, replace them with the new lot,
		// otherwise add the new errors to the start of the form
		if (objExisting)
		{
			objExisting.parentNode.replaceChild(objNew, objExisting);
		}
		else
		{
			objPosition = objForm.firstChild;
			objForm.insertBefore(objNew, objPosition);
		}

		// Allow for latency
		setTimeout(function() { objAnchor.focus(); }, 50);
		
		// Don't submit the form
		objForm.submitAllowed = false;
		return false;
	}

	// Submit the form
	window.print();
	return true;

}

// Function to add a link in a list item that points to problematic field control
function addError(objList, strError, strID, strErrorID)
{
	var objListItem = document.createElement('li');
	var objAnchor = document.createElement('a');
	
	// Fragment identifier to the form control
	objAnchor.href='#' + strID;

	// Make this the target for the error heading
	if (strErrorID.length > 0)
	{
		objAnchor.id = strErrorID;
	}

	// Use the label prompt for the error message
	objAnchor.appendChild(document.createTextNode(strError));
	// Add keyboard and mouse events to set focus to the form control
	objAnchor.onclick = function(event){return focusFormField(this, event);};
	objAnchor.onkeypress = function(event){return focusFormField(this, event);};
	objListItem.appendChild(objAnchor);
	objList.appendChild(objListItem);
}

function focusFormField(objAnchor, objEvent)
{
	var strFormField, objForm;

	// Allow keyboard navigation over links
	if (objEvent && objEvent.type == 'keypress')
	{
		if (objEvent.keyCode != 13 && objEvent.keyCode != 32)
		{
			return true;
		}
	}

	// set focus to the form control
	strFormField = objAnchor.href.match(/[^#]\w*$/);
	objForm = getForm(strFormField);
	objForm[strFormField].focus();
	return false;
}

// Function to return the form element from a given form field name
function getForm(strField)
{
	var objElement = document.getElementById(strField);

	// Find the appropriate form
	do
	{
		objElement = objElement.parentNode;
	} while (!objElement.tagName.match(/form/i) && objElement.parentNode);

	return objElement;
}

// Function to log the error in a list
function logError(objField, objLabel, objList, strErrorID)
{
	var iCounter, strError;

	// Search the label for the error prompt
	for (iCounter=0; iCounter<objLabel.length; iCounter++)
	{
		if (objLabel[iCounter].htmlFor == objField.id)
		{
			strError = objLabel[iCounter].firstChild.nodeValue;
			//special cases for radios
			// radios are named MembershipType, Title, and Paymentoptions
			switch (document.getElementById(objField.id).name)
			{
				case 'MembershipType' :
					strError = 'Please choose a Membership Type';
					break;
				case 'Title' :
					strError = 'Please choose a Title';
					break;
				case 'Paymentoptions' :
					strError = 'Please choose a Payment option';
					break;
					
			}
		}
	}

	addError(objList, strError, objField.id, strErrorID);
}

// Validation routines - add as required

function isString(strValue, arClass)
{
	var bValid = (typeof strValue == 'string' && strValue.replace(/^\s*|\s*$/g, '') !== '' && isNaN(strValue));

	return checkOptional(bValid, strValue, arClass);
}

function isEmail(strValue, arClass)
{
	var objRE = /^[\w-\.\']{1,}\@([\da-zA-Z\-]{1,}\.){1,}[\da-zA-Z\-]{2,}$/;
	var bValid = objRE.test(strValue);

	return checkOptional(bValid, strValue, arClass);
}

function isNumber(strValue, arClass)
{
	var bValid = (!isNaN(strValue) && strValue.replace(/^\s*|\s*$/g, '') !== '');

	return checkOptional(bValid, strValue, arClass);
}

function checkOptional(bValid, strValue, arClass)
{
	var bOptional = false;
	var iCounter;

	// Check if optional
	for (iCounter=0; iCounter<arClass.length; iCounter++)
	{
		if (arClass[iCounter] == 'optional')
		{
			bOptional = true;
		}
	}

	if (bOptional && strValue.replace(/^\s*|\s*$/g, '') === '')
	{
		return true;
	}

	return bValid;
}

function isPhone(strValue, arClass)
{
		var stripped = strValue.replace(/[\(\)\.\-\ ]/g, '');
		
	//It should have 10 or 11 digits -- otherwise we reject it.
		var bValid = (!isNaN(stripped) && strValue.replace(/^\s*|\s*$/g, '') !== '' && (stripped.length == 10 || stripped.length == 11) );
	
	return checkOptional(bValid, strValue, arClass);
}

// exactly one radio button is chosen

function checkRadioArray(radioname){
	var bValid = false;
	var strValue = "";
	var x = document.getElementsByName(radioname);
	for (var j = 0; j < x.length; j++)  {
		if (x[j].checked)  {
		strValue = x[j].value;
		bValid = true;
		}
	} 
	return bValid;
}

function getRadioArrayValue(radioname) {
	//get checked value
	var strValue = "";
	var x = document.getElementsByName(radioname);
	for (var j = 0; j < x.length; j++)  {
		if (x[j].checked)  {
		strValue = x[j].value;
		}
	}
	return strValue;
}