/*
Required Form Attribute
	onSubmit="return valid(this);"

Extend input tags to contain:

	notRequired - The user is not required to enter data into this field
	numerical		-	This input must contain numberical digits only

An input with the id = "emailTo" is checked for a correct email format
*/

function valid(myForm)
{
	for(var mainInputNo = 0; mainInputNo < myForm.elements.length; mainInputNo++)
	{
		var mainInput = myForm.elements[mainInputNo];

		if ( (mainInput.value == "") && ( (mainInput.notRequired == undefined) || (mainInput.notRequired == "false") ) )
		{
		  alert("please enter a value for " + mainInput.name);
		  mainInput.focus();

		  return false;
		}

		// at this point we only have fields that have value or are not required

		if (mainInput.id == "emailTo")
		{
			if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(mainInput.value)))
			{
				alert("Email format incorrect.");
				mainInput.focus();

				return false;
			}
		}

		if ( (mainInput.numerical != undefined) && (!(/\d+/.test(mainInput.value))) )
		{
		  alert(mainInput.name + " must contain numerical characters only");
		  mainInput.focus();

		  return false;
		}
	}

	return true;
}



            function onlyNumbers(evt) {
                var e = event || evt;
                var charCode = e.which || e.keyCode;

                if (charCode > 31 && (charCode < 48 || charCode > 57))
                        return false;
                    return true;
            }

            function formValidation() {
                var errStr = "";
                var errId = "";
                
                //personal details.
                if ( document.getElementById("txtFirstname").value == "" ) { 
                    errStr += "\n - Firstname"; 
                    if (errId=="") { 
                        errId = "txtFirstname";
                    } 
                }
                if ( document.getElementById("txtSurname").value == "" ) { 
                    errStr += "\n - Surname"; 
                    if (errId=="") { 
                        errId = "txtSurname";
                    }
                }
                if ( document.getElementById("txtAdd1").value == "" ) { 
                    errStr += "\n - Address Line 1"; 
                    if (errId=="") { 
                        errId = "txtAdd1";
                    } 
                }
                if ( document.getElementById("txtAdd2").value == "" ) 
                    { errStr += "\n - Address Line 2"; 
                    if (errId=="") { 
                    errId = "txtAdd2";
                    } 
                }
                if ( document.getElementById("txtAdd3").value == "" ) 
                    { errStr += "\n - Address Line 3"; 
                    if (errId=="") { 
                    errId = "txtAdd3";
                    } 
                }
                if ( document.getElementById("txtPostal").value == "" ) { 
                    errStr += "\n - Postal Code"; 
                    if (errId=="") { 
                        errId = "txtPostal";
                    } 
                }
                
                //contact details.
                if ( document.getElementById("txtHome").value == "" ) { 
                    errStr += "\n - Home Telephone"; 
                    if (errId=="") { 
                        errId = "txtHome";
                    } 
                }
                
                //email address validation
                //------------------------
                //check format of string supplied for valid address
                if ( document.getElementById("txtEmail").value != "" ) { 
	                if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.getElementById("txtEmail").value))) {
		                errStr += "\n - Email incorrect format"; 
                        if (errId=="") { 
                            errId = "txtEmail";
                        } 
	                }
	                //see if the user has tried to confirm the email
                    else if ( document.getElementById("txtEmail").value != "" && document.getElementById("txtEmailConfirm").value == "" ) {
                        errStr += "\n - Please confirm your email"; 
                        if (errId=="") { 
                            errId = "txtEmailConfirm";
                        } 
                    }
                    //check to see if the email addess are entered the same
                    else if ( document.getElementById("txtEmail").value != document.getElementById("txtEmailConfirm").value ) {
                        errStr += "\n - Your email address does not match."; 
                        if (errId=="") { 
                            errId = "txtEmailConfirm";
                        } 
                    }
                }
                //check if a email address has been supplied
                else {
                    errStr += "\n - Email Address"; 
                    if (errId=="") { 
                        errId = "txtEmail";
                    } 
                }
                //------------------------------------------------------------
                
                if ( document.getElementById("txtResidence").value == "" ) { 
                    errStr += "\n - Country of Residence"; 
                    if (errId=="") { 
                        errId = "txtResidence";
                    } 
                }
                
                //dropdown . select
                if ( document.getElementById("txtCurrently").value == "None" ) { 
                    errStr += "\n - I am Currently.."; 
                    if (errId=="") { 
                    errId = "txtCurrently";
                    } 
                }
                
                //if the user does want to be contact they need to select the method of contact from the list.
                if ( document.getElementById("advisor").value == "Yes" && document.getElementById("method").value == "None" ) { 
                    errStr += "\n - Method of contact"; 
                        if (errId=="") { 
                        errId = "txtResidence";
                    } 
                }          
                            
                if (errStr != "") {
                    alert("Please complete the following field(s)\n" + errStr);
                    document.getElementById(errId).focus();
                    return false;
                }                    

 
                 
            }
            
            //hide field unless user wants to be contacted.
            function contactMe() {
                if (document.getElementById("advisor").value == "Yes") {
                    
                    document.getElementById("showHide").style.display = "block"
                
                } else if (document.getElementById("advisor").value == "No") {
                
                    document.getElementById("showHide").style.display = "none"
                
                } else if (document.getElementById("advisor").value == "None") {
                
                    document.getElementById("showHide").style.display = "none"
                
                } else {
                
                    document.getElementById("showHide").style.display == "none"
                
                }
            }