function validate_required(field,alerttxt)
{
with (field)
{
if (value==null||value=="")
  {alert(alerttxt);field.select();return false;}
else {return true}
}
}


function validate_postcode(field,alerttxt)
{
with (field)
{
if (value.length<5)
  {alert(alerttxt);field.select();return false;}
else {return true}
}
}



function validate_email(field,alerttxt)
{
with (field)
{
apos=value.indexOf("@");
dotpos=value.lastIndexOf(".");
if (value==null||value=="")
   {return true;}
if (apos<1||dotpos-apos<2) 
  {alert(alerttxt);field.select();return false;}
else {return true;}
}
}

function validate_numeric(field,alerttxt)
{
with (field)
{

var str = value
var vallength = value.length

if (value==null||value=="")
   {return true;}
else
for (var i = 0; i < vallength; i++)
{
if (str.charAt(i) >= 0 && str.charAt(i) <= 9)	
   loop 
else
   {alert(alerttxt);field.select();return false;}
} 
}
}


function checkdate(input,alerttxt){
var validformat=/^\d{2}\/\d{2}\/\d{4}$/
var returnval=false
if(input.value==null||input.value=="")
returnval=true
else
if (!validformat.test(input.value))
alert(alerttxt)
else{
var monthfield=input.value.split("/")[0]
var dayfield=input.value.split("/")[1]
var yearfield=input.value.split("/")[2]
var dayobj = new Date(yearfield, monthfield-1, dayfield)
if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
alert("Invalid Day, Month, or Year range detected. Please correct and submit again.")
else
returnval=true
}
if (returnval==false) input.select()
return returnval
}





function validate_form(thisform)
{
with (thisform)
{

//empty textbox validation & numeric validation

if (validate_required(ApcFullName,"[Applicant Full Name] must be filled out !")==false)
  {ApcFullName.focus();return false;}

if (validate_required(ApcPhoneNumber,"[Applicant Phone/ Mobile] must be filled out !")==false)
  {ApcPhoneNumber.focus();return false;}

if (validate_numeric(ApcPhoneNumber,"[Applicant Phone/ Mobile] Please make sure entries are numbers only !")==false)
  {ApcPhoneNumber.focus();return false;}

if (validate_numeric(ApcFax,"[Applicant Fax No] Please make sure entries are numbers only !")==false)
  {ApcFax.focus();return false;}

if (validate_required(ContactPerson,"[Applicant Contact Person] must be filled out !")==false)
  {ContactPerson.focus();return false;}

if (validate_required(CPersonPhoneNumber,"[Applicant Phone Number] must be filled out !")==false)
  {CPersonPhoneNumber.focus();return false;}

if (validate_numeric(CPersonPhoneNumber,"[Contact Person Phone / Mobile] Please make sure entries are numbers only !")==false)
  {CPersonPhoneNumber.focus();return false;}

if (validate_required(FMFullName,"[First Manager Full Name] must be filled out !")==false)
  {FMFullName.focus();return false;}

if (validate_required(FMNricNew,"[First Manager Nric (New)] must be filled out !")==false)
  {FMNricNew.focus();return false;}

if (validate_numeric(FMPhone,"[First Manager Phone] Please make sure entries are numbers only !")==false)
  {FMPhone.focus();return false;}

if (validate_numeric(FMFax,"[First Manager Fax] Please make sure entries are numbers only !")==false)
  {FMFax.focus();return false;}

if (validate_numeric(FMMobile,"[First Manager Mobile] Please make sure entries are numbers only !")==false)
  {FMMobile.focus();return false;}

if (validate_required(FMResidentialAddress,"[First Manager Residential Address] must be filled out !")==false)
  {FMResidentialAddress.focus();return false;}

if (validate_required(FMPostalCode,"[First Manager Post Code] must be filled out !")==false)
  {FMPostalCode.focus();return false;}

if (validate_postcode(FMPostalCode,"[Second Manager Post Code] Please make sure numbers are five digits !")==false)
  {FMPostalCode.focus();return false;}

if (validate_numeric(FMPostalCode,"[First Manager Post Code] Please make sure entries are numbers only !")==false)
  {FMPostalCode.focus();return false;}

if (validate_required(FMState,"[First Manager State] must be filled out !")==false)
  {FMState.focus();return false;}



if (validate_required(SMFullName,"[Second Manager Full Name] must be filled out !")==false)
  {SMFullName.focus();return false;}

if (validate_required(SMNricNew,"[Second Manager Nric (New)] must be filled out !")==false)
  {SMNricNew.focus();return false;}

if (validate_numeric(SMPhone,"[Second Manager Phone] Please make sure entries are numbers only !")==false)
  {SMPhone.focus();return false;}

if (validate_numeric(SMFax,"[Second Manager Fax] Please make sure entries are numbers only !")==false)
  {SMFax.focus();return false;}

if (validate_numeric(SMMobile,"[Second Manager Mobile] Please make sure entries are numbers only !")==false)
  {SMMobile.focus();return false;}

if (validate_required(SMResidentialAddress,"[Second Manager Residential Address] must be filled out !")==false)
  {SMResidentialAddress.focus();return false;}

if (validate_required(SMPostalCode,"[Second Manager Post Code] must be filled out !")==false)
  {SMPostalCode.focus();return false;}

if (validate_numeric(SMPostalCode,"[Second Manager Post Code] Please make sure entries are numbers only !")==false)
  {SMPostalCode.focus();return false;}

if (validate_postcode(SMPostalCode,"[Second Manager Post Code] Please make sure numbers are five digits !")==false)
  {SMPostalCode.focus();return false;}

if (validate_required(SMState,"[Second Manager State] must be filled out !")==false)
  {SMState.focus();return false;}



//email format validation

if (validate_email(ApcEmail,"[Applicant Email Address] not a valid e-mail address !")==false)
  {ApcEmail.focus();return false;}

if (validate_email(FMPersonalEmail,"[First Manager Personal Email Address] not a valid e-mail address !")==false)
  {FMPersonalEmail.focus();return false;}

if (validate_email(SMPersonalEmail,"[Second Manager Personal Email Address] not a valid e-mail address !")==false)
  {SMPersonalEmail.focus();return false;}


//date validation
if (checkdate(FMDob,"[First Manager DOB] Invalid Date Format. Please correct and submit again !")==false)
  {FMDob.focus();return false;}

if (checkdate(SMDob,"[Second Manager DOB] Invalid Date Format. Please correct and submit again !")==false)
  {SMDob.focus();return false;}

}
}
