function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function echeck(str) 
{
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   //alert("Invalid E-mail ID")
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   //alert("Invalid E-mail ID")
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    //alert("Invalid E-mail ID")
	    return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
	    //alert("Invalid E-mail ID")
	    return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    //alert("Invalid E-mail ID")
	    return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
	    //alert("Invalid E-mail ID")
	    return false
	 }
	
	 if (str.indexOf(" ")!=-1){
	    //alert("Invalid E-mail ID")
	    return false
	 }

	 return true					
}

//MY OWN FUNCTIONS\\

function ToggleFields()
{
	if (document.reg.anon.checked)
	{
		document.getElementById('regname').style.display = 'none';
		document.getElementById('regsurname').style.display = 'none';
		document.getElementById('regdob').style.display = 'none';
		document.getElementById('regprovince').style.display = 'none';
		document.getElementById('regcellno').style.display = 'none';
		document.getElementById('regemail').style.display = 'none';
		document.getElementById('regphysadr').style.display = 'none';
		document.getElementById('regpostadr').style.display = 'none';
		document.getElementById('regposition').style.display = 'none';
		document.getElementById('regpassword').style.display = 'none';
		document.getElementById('regretypepassword').style.display = 'none';
		document.reg.submitbtn.value = 'Click here to make a Contribution';
		document.reg.submitbtn.disabled = false;
	}
	else
	{
		document.getElementById('regname').style.display = '';
		document.getElementById('regsurname').style.display = '';
		document.getElementById('regdob').style.display = '';
		document.getElementById('regprovince').style.display = '';
		document.getElementById('regcellno').style.display = '';
		document.getElementById('regemail').style.display = '';
		document.getElementById('regphysadr').style.display = '';
		document.getElementById('regpostadr').style.display = '';
		document.getElementById('regposition').style.display = '';
		document.getElementById('regpassword').style.display = '';
		document.getElementById('regretypepassword').style.display = '';
		document.reg.submitbtn.value = 'Click here to Register';
		document.reg.submitbtn.disabled = true;
	}
}

function DoSubmit()
{
	if (document.reg.anon.checked)
	{
		document.reg.submit();
	}
	else
	{
		//he must register, validate fields
		
		if (trim(document.reg.name.value) == '')
		{
			alert('Please enter your name.');
			document.reg.name.focus();
		}
		else if (trim(document.reg.surname.value) == '')
		{
			alert('Please enter your surname.');
			document.reg.surname.focus();
		}		
		else if (trim(document.reg.email.value) == '')
		{
			alert('Please enter your email address.');
			document.reg.email.focus();
		}
		else if (!echeck(document.reg.email.value))
		{
			alert('Please enter a VALID email address.');
			document.reg.email.focus();
		}
		else if (trim(document.reg.postadr.value) == '')
		{
			alert('Please enter your postal address.');
			document.reg.postadr.focus();
		}
		else if (trim(document.reg.postadr.value) == '')
		{
			alert('Please enter your postal address.');
			document.reg.postadr.focus();
		}
		else if (document.reg.password.value.length < 6)
		{
			alert('Please enter your a password with at least 6 characters.');
			document.reg.password.focus();
		}
		else if (document.reg.password.value != document.reg.retypepassword.value)
		{
			alert('The passwords do not match');
			document.reg.password.focus();
		}
		else
		{
			document.reg.submit();
		}
	}
}