
function isEmailAddr(email)
{
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
  result = true;
  }
  return result;}

function validRequired(formField,fieldLabel)
{
  var result = true;
  if (formField.value == "")
  {
    alert('Please enter a value for the "' + fieldLabel +'" field.');
    formField.focus();
    result = false;
  }
  return result;}

function validEmail(formField,fieldLabel,required)
{
  var result = true;
  if (required && !validRequired(formField,fieldLabel))
    result = false;

  if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) )
  {
    alert("Please enter a complete email address in the form: yourname@yourdomain.com");
    formField.focus();
    result = false;
  }  
  return result;}


function validNum(formField,fieldLabel,required)
{
  var result = true;
  if (required && !validRequired(formField,fieldLabel))
    result = false;
   if (result)
   {
     var num = parseInt(formField.value,10);
     if (isNaN(num))
     {
       alert('Please enter a number for the "' + fieldLabel +'" field.');
      formField.focus();    
      result = false;
    }
  }   
  return result;}

function validDate(formField,fieldLabel,required)
{
  var result = true;
  if (required && !validRequired(formField,fieldLabel))
    result = false;
   if (result)
   {
     var elems = formField.value.split("/");
     result = (elems.length == 3); // should be three components
     if (result)
     {
       var day = parseInt(elems[0],10);
        var month = parseInt(elems[1],10);
       var year = parseInt(elems[2],10);
      result = !isNaN(month) && (month > 0) && (month < 13) &&
            !isNaN(day) && (day > 0) && (day < 32) &&
            !isNaN(year) && (elems[2].length == 4);
     }
      if (!result)
     {
       alert('Please enter a date in the format DD/MM/YYYY for the "' + fieldLabel +'" field.');
      formField.focus();    
    }
  } 
  return result;}
  
  
var submitcount=0;


function validateFormSignUp(theForm) {

  if (!theForm.company.value == "")
	  if (!validDate(theForm.company,"Date of birth",true))
   	  return false; 
   if (!validEmail(theForm.email,"Email",true))
    return false; 
  if (!validRequired(theForm.customerPassword,"Password"))
    return false;
	
  if (theForm.customerPassword.value.length<4 && theForm.customerPassword.value!='*!*^*$') {
    alert("Your password must be at least 4 characters in length.");
	theForm.customerPassword.focus();
    return false;
  }	
  
  if (!validRequired(theForm.customerPassword2,"Confirm Password"))
    return false;
	
  if (theForm.customerPassword2.value!=theForm.customerPassword.value) {
    alert("The Confirm Password field does not match the Password field.");
	theForm.customerPassword2.focus();
    return false;
  }	  
  
  if (!validRequired(theForm.firstName,"First Name"))
    return false;
  if (!validRequired(theForm.surname,"Last Name",true))
    return false;
  if (!validRequired(theForm.street,"Street",true))
    return false;
  if (!validRequired(theForm.suburb,"Town/Suburb",true))
    return false;
  if (!validRequired(theForm.postCode,"Post code",true))
    return false;	
  if (!validRequired(theForm.phone,"Contact phone",true))
    return false;		

   if (theForm.terms.checked == false || theForm.terms2.value == "") {
    alert("Please indicate that you accept the \nConditions of Use.");
    theForm.terms.focus();
    return false;
    }

  if (submitcount == 0) {
    submitcount++;
    return true;
  } else {
    alert("Processing...\n\nClick OK now and please wait for a response.");
    return false;
  }  
}

function validateFormNaturopath(theForm)
{
  if (!validRequired(theForm.name,"Name"))
    return false;
  if (!validRequired(theForm.phone,"Phone",true))
    return false;
   if (!validEmail(theForm.email,"Email",true))
    return false; 
  if (!validRequired(theForm.medications,"Current medications"))
    return false;
  if (!validRequired(theForm.question,"Your question"))
    return false;	
   if (!document.forms['naturopath_form'].iconfirm.checked) {
	 alert('Please check the box to confirm that you understand and agree to the PRIVACY DISCLOSURE statement.')
   return false; 
  }			
  return true;
}

function validateFormFeedback(theForm)
{
  if (!validRequired(theForm.name,"Name"))
    return false;
   if (!validEmail(theForm.email,"Email",true))
    return false; 
  if (!validRequired(theForm.phone,"Phone",true))
    return false;
  if (!validRequired(theForm.question,"Your question"))
    return false;		
  return true;
}

function validateFormEmailFriend(theForm)
{
   if (!validEmail(theForm.emailFrom,"Your Email Address",true))
    return false; 
   if (!validEmail(theForm.emailTo,"Friends Email Address",true))
    return false; 
   if (!document.forms['emailFriend'].iconfirm.checked) {
	 alert('Please tick the box - I confirm that I have made my friend aware of the above and my friend has consented to me providing their personal information to Golden Glow.')
   return false; 
  }	
  return true;
}

function validateFormHealthAssessment(theForm)
{
  if (!validRequired(theForm.name,"Full Name"))
    return false;
  if (!validRequired(theForm.gender,"Gender"))
    return false;
  if (!validRequired(theForm.age,"Age"))
    return false;		
  if (!validRequired(theForm.phone,"Best Contact number",true))
    return false;
  if (!validRequired(theForm.contactday,"Best Contact day"))
    return false;
   if (!validEmail(theForm.email,"Email address",true))
    return false; 
  if (!validRequired(theForm.allergies,"Do you have any known allergies/ sensitivities"))
    return false;
  if (!validRequired(theForm.supplements,"Are you on any medications/supplements"))
    return false;	
   if (!document.forms['healthassessment_form'].iconfirm.checked) {
	 alert('Please check the box to confirm that you understand and agree to the PRIVACY DISCLOSURE statement.')
   return false; 
  }			
  return true;
}

function validateFormSignUpMembership(theForm) {

   if (!validEmail(theForm.email,"Email",true))
    return false; 
  if (!validRequired(theForm.customerPassword,"Password"))
    return false;
	
  if (theForm.customerPassword.value.length<4 && theForm.customerPassword.value!='*!*^*$') {
    alert("Your password must be at least 4 characters in length.");
	theForm.customerPassword.focus();
    return false;
  }	
  
  if (!validRequired(theForm.customerPassword2,"Confirm Password"))
    return false;
	
  if (theForm.customerPassword2.value!=theForm.customerPassword.value) {
    alert("The Confirm Password field does not match the Password field.");
	theForm.customerPassword2.focus();
    return false;
  }	  
  
  if (!validRequired(theForm.firstName,"First Name"))
    return false;
  if (!validRequired(theForm.surname,"Last Name",true))
    return false;
  if (!validRequired(theForm.street,"Street",true))
    return false;
  if (!validRequired(theForm.suburb,"Town/Suburb",true))
    return false;
  if (!validRequired(theForm.postCode,"Post code",true))
    return false;	
  if (!validRequired(theForm.phone,"Contact phone",true))
    return false;		
  if (!validRequired(theForm.companySuburb,"Company Town/ Suburb",true))
    return false;	
  if (!validRequired(theForm.companyState,"Company State",true))
    return false;	

   if (theForm.terms.checked == false || theForm.terms2.value == "") {
    alert("Please indicate that you accept the \nConditions of Use.");
    theForm.terms.focus();
    return false;
    }

  if (submitcount == 0) {
    submitcount++;
    return true;
  } else {
    alert("Processing...\n\nClick OK now and please wait for a response.");
    return false;
  }  
}