/*   ////////////////////////////////////////////
	JavaScript Document :: persacccheck1.js
	version 1.1 28.6.03
	copyright (c) 2003, Chris Whincop, neurvana.com
	email: chrisATneurvanaDOTcom
	tel: +44 (0)1896 820161
	adapted from various Open Source scripts and
	released under the GPL, see:
	http://www.gnu.org/copyleft/gpl.html
 	No warranty, freely distributable and reusable.
	If you make improvements, please let me know.
	Thanks! 
///////////////////////////////////////// */

function validate() { 

//check a field to make sure it's not blank 
if(document.PersAcc1.orgname.value == '') { 
alert('Please fill in the field:\n\nName of Organisation'); 
document.PersAcc1.orgname.focus(); 
return false; 
}

//check a field to make sure it's not blank 
if(document.PersAcc1.contname.value == '') { 
alert('Please fill in the field:\n\nContact name'); 
document.PersAcc1.contname.focus(); 
return false; 
}

/* 		NEW EMAIL PROCEDURE - 28.6.03 - STARTS HERE 	*/

// offer the person the option of leaving the two email fields blank if they are both blank
if((document.PersAcc1.contemail1a.value == '') && (document.PersAcc1.contemail1b.value == '')) { 
if(!confirm("Are you sure you want to leave the two 'Contact Email Address' fields blank?\n\nPlease click on 'OK' to confirm, or click on 'Cancel' to go back and enter your email address.")) { 
document.PersAcc1.contemail1a.focus(); 
return false; 
}
}
// check both email fields to make sure just the first isn't blank 
if((document.PersAcc1.contemail1b.value != '') && (document.PersAcc1.contemail1a.value == '')){ 
alert("It looks like you have filled in the second email field, but not the first.\n\nPlease EITHER:\n\n1. fill in the first email field: \n\n'Contact Email Address' \n\nOR:\n\n2. make sure the second email field is also empty"); 
document.PersAcc1.contemail1a.focus(); 
return false; 
}
// check both email fields to make sure just the second isn't blank 
if((document.PersAcc1.contemail1a.value != '') && (document.PersAcc1.contemail1b.value == '')){ 
alert("It looks like you have filled in the first email field, but not the second.\n\nPlease EITHER:\n\n1. fill in the second email field: \n\n'Please retype the Contact Email Address...'\n\nOR:\n\n2. make sure the first email field is also empty"); 
document.PersAcc1.contemail1b.focus(); 
return false; 
}

// check both email fields again, and this time, if neither is blank, proceed to check each of them for valid syntax
// if both are blank, do nothing
if((document.PersAcc1.contemail1a.value != '') && (document.PersAcc1.contemail1b.value != '')) { 
// check if email address entered has got valid syntax 
if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.PersAcc1.contemail1a.value))) { 
alert("What you\'ve entered in the first email field is not a valid email address!\n\nPlease check it for spaces and other typos."); 
document.PersAcc1.contemail1a.focus(); 
return false; 
}  //end the first if within if
if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.PersAcc1.contemail1b.value))) { 
alert("What you\'ve entered in the second email field is not a valid email address!\n\nPlease check it for spaces and other typos."); 
document.PersAcc1.contemail1b.focus(); 
return false; 
}  //end the second if within if
} //end the main if

// Now make sure that the two email addresses entered match
  if (document.PersAcc1.contemail1a.value.toLowerCase() != document.PersAcc1.contemail1b.value.toLowerCase()) {
alert("The two email addresses you\'ve just entered must match each other exactly!\n\nPlease check them both for spaces and other typos."); 
document.PersAcc1.contemail1a.focus(); 
return false; 
} //end if 

/* 		NEW EMAIL PROCEDURE ENDS HERE 	*/



//check a field to make sure it's not blank 
if(document.PersAcc1.contphone1.value == '') { 
alert('Please fill in the field:\n\nContact phone number'); 
document.PersAcc1.contphone1.focus(); 
return false; 
}


//check a field to make sure it's not blank 
if(document.PersAcc1.describe_activities.value == '') { 
alert('Please fill in the field:\n\nDescribe activities...'); 
document.PersAcc1.describe_activities.focus(); 
return false; 
}

//check a field to make sure it's not blank 
if(document.PersAcc1.number_of_persons.value == '') { 
alert('Please fill in the field:\n\nNumber of persons'); 
document.PersAcc1.number_of_persons.focus(); 
return false; 
}

//check a field to make sure it's not blank 
if(document.PersAcc1.number_of_units.value == '') { 
alert('Please fill in the field:\n\nNo. of Units'); 
document.PersAcc1.number_of_units.focus(); 
return false; 
}


// Get the value of the radio button group in question
// valRadioButton is a function we define down below 
var radioVal = valRadiobutton(document.PersAcc1.good_health); 
//make sure that the user actually selected one of the radio buttons: 
if (radioVal == '') { 
alert("It looks like you forgot to click on your choice in response to the\n\n'Are all persons proposed for cover in good health...' question.  \n \nSorry, but you MUST make a selection!"); 
document.PersAcc1.good_health[1].focus(); // and focus on the second option
return false; 
}


//check a field to make sure it's not blank IFF particular radio selection made
var radioVal = valRadiobutton(document.PersAcc1.good_health); 
if((radioVal == 'No') && (document.PersAcc1.not_good_health_details.value == '')) { 
alert("As you have answered 'No' to the question:\n\n'Are all persons proposed for cover in good health...', \n\nplease fill in the field:\n\nIf \'No\', give details"); 
document.PersAcc1.not_good_health_details.focus(); 
return false; 
}

// Get the value of the radio button group in question
// valRadioButton is a function we define down below 
var radioVal = valRadiobutton(document.PersAcc1.refusal); 
//make sure that the user actually selected one of the radio buttons: 
if (radioVal == '') { 
alert("It looks like you forgot to click on your choice in response to the \n'Has any previous Insurer declined a proposal...' question. \n \nSorry, but you MUST make a selection!"); 
document.PersAcc1.refusal[0].focus(); // and focus on the first option
return false; 
}

//check a field to make sure it's not blank IFF particular radio selection made
var radioVal = valRadiobutton(document.PersAcc1.refusal); 
if((radioVal == 'Yes') && (document.PersAcc1.refusal_details.value == '')) { 
alert("As you have answered 'Yes' to the question:\n\n'Has any previous Insurer declined a proposal...', \n\nplease fill in the field:\n\nIf \'Yes\', give details"); 
document.PersAcc1.refusal_details.focus(); 
return false; 
}

// Get the value of the radio button group in question
// valRadioButton is a function we define down below 
var radioVal = valRadiobutton(document.PersAcc1.resubmitting); 

//make sure that the user actually selected one of the radio buttons: 
if (radioVal == '') { 
alert("It looks like you forgot to click on your choice in response to the \n'Are you resubmitting...' question. \n \nSorry, but you MUST make a selection!"); 
document.PersAcc1.resubmitting[1].focus(); 
return false; 
}


//if the checking gets this far, everything checked is OK 

return; 

}// end validate function 



/*  this function gets the value of a required radio group */
function valRadiobutton(radiobutton) { 
myOption = -1; 
for (i=0; i< radiobutton.length; i++) { 
if (radiobutton[i].checked) { 
myOption = i; 
}
}
if (myOption == -1) { 
return ""; 
} else { 
return radiobutton[myOption].value 
}
}// end function valRadioButton 


//EOF