// Javascript form validator


function SetHandlers(){
			for(var i=0;i<document.forms.length;i++){
				f=document.forms[i];
				for(var j=0;j<f.length;j++){
					c=f[j];
					if(c.type=="text" || c.type=="textarea" || c.type=="password"){
						c.onfocus=clearDefault;
						c.onblur=restoreDefault;
					}
				}
			}
		}
		function restoreDefault(){
			if (!this.value){
				this.value=this.defaultValue;
			}
		}
		function clearDefault(){
			if (this.value==this.defaultValue){
				this.value="";
			}
		}
          
  function ValidateForm()
{
	validation=true;
	validMessage='';
	
	var emailID=document.registration.email.value;
	var passwordID = document.registration.password.value;

	if(document.registration.title.value=='') 
	{
		validMessage=validMessage + 'Title is a mandatory field\n';
		validation=false;
	}
	
	if(document.registration.firstname.value=='') 
	{
		validMessage=validMessage + 'Firstname is a mandatory field\n';
		validation=false;
	}
        

	if(document.registration.lastname.value=='') 
	{
		validMessage=validMessage + 'Lastname is a mandatory field\n';
		validation=false;
	}
 
	if (document.registration.email.value==''){
	
		validMessage=validMessage + 'Email is a mandatory field\n';
		validation=false;
	}
	
	if (document.registration.email.value!='' && ValidateEmail(document.registration.email.value))
	{
			validMessage=validMessage + 'Email is in an incorrect format\n';
		validation=false;
	}
	

  if(document.registration.company.value=='') 
	{
	
		validMessage=validMessage + 'Company is a mandatory field\n';
		validation=false;
	}
	
  if(document.registration.job_title.value=='') 
	{
		
		validMessage=validMessage + 'Job title is a mandatory field\n';
		validation=false;
	}
	
if((passwordID==null)|| (passwordID=='')) 
    {
    		validMessage=validMessage + 'Password is a mandatory field\n';
		validation=false;
	
	}
	
	if(passwordID!='' && passwordID.length<6) 
    {
		validMessage=validMessage + 'Password must be at least 6 characters numbers\n';
		validation=false;
	
	}
	
	if(document.registration.password.value!=document.registration.confirm_password.value)
	{
	    validMessage=validMessage + 'Password and Confirm must match\n';
		validation=false;
	
	}
	
	if(document.registration.how_find_site.selectedIndex==0)
		{
	    validMessage=validMessage + 'How you found this site is a mandatory field\n';
		validation=false;
		}


	if (!document.registration.financial_planner[0].checked && !document.registration.financial_planner[1].checked)
	{
	    validMessage=validMessage + 'Are you a financial planner is a mandatory field\n';
		validation=false;
	}


	if (document.registration.financial_planner[0].checked && document.registration.pref_platform_super_investments.value=='')
	{
	    validMessage=validMessage + 'Preferred platform for super investments is a mandatory field\n';
		validation=false;
	}

	if (document.registration.financial_planner[0].checked && document.registration.pref_platform_non_super_investments.value=='')
	{
	    validMessage=validMessage + 'Preferred platform for non super investments is a mandatory field\n';
		validation=false;
	}
	
  if(document.registration.confirm_terms_conditions.checked == false) 
	{
		validMessage=validMessage + 'Registration cannot continue if you did not agree to our terms and conditions\n';
		validation=false;
	}
	if(!validation)
	{
		alert(validMessage);

	}

	return validation;
	
}


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("this email address is invalid")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("this email address is invalid")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("this email address is invalid")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("this email address is invalid")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("this email address is invalid")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("this email address is invalid")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("this email address is invalid")
		    return false
		 }

 		 return true					
	}


function ValidateEmail(email)
{

	

	invalidChars = " /:,;"
	
	if (email == "")
	{
		return true
	}
	
	for (i=0; i<invalidChars.length; i++)
	{
		badChar = invalidChars.charAt(i)
		if (email.indexOf(badChar,0) != -1)
		{
			return true
		}
	}
	
	atPos = email.indexOf("@",1)
	if (atPos == -1)
	{
		return true
	}
	
	if (email.indexOf("@",atPos+1) != -1)
	{
		return true
	}
	
	periodPos = email.indexOf(".",atPos)
	if (periodPos == -1)
	{
		return true
	}

	if(periodPos==(atPos+1))
	{
		return true
	}
	
	if (periodPos+3 > email.length)
	{
		return true
	}
	
	return false
}

	function isNum(passedVal)
	{
		if (passedVal == "")
		{
			return true
		}
		
		for (i=0; i<passedVal.length; i++)
		{
			if (passedVal.charAt(i)< "0")
			{
				return true
			}
			if (passedVal.charAt(i) > "9")
			{
				return true
			}
		}
		return false
	}


function checkForm(form) {
	
		if(document.registration.password.value != "" && document.registration.password.value == document.registration.confirm_password.value) 
		{ 
		if(document.registration.password.value.length < 6) 
		{ 
		alert("Error: Password must contain at least six characters!"); 
		document.registration.password.focus(); 
		return false; 
		} 
		if(document.registration.password.value == document.registration.email.value) 
		{
			alert("Error: Password must be different from email!"); 
			document.registration.password.focus(); 
			return false; 
	     } 
		}
			if(document.registration.password.value != document.registration.confirm_password.value) 
			{
   				 alert("Please check your password; the confirmation entry does not match");
    			 return false;
  			}		
					
					return true;
}