/*   ////////////////////////////////////////////
	JavaScript Document :: theftcheck1.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.Theft1.orgname.value == '') { 
alert('Please fill in the field:\n\nName of Organisation'); 
// the double "\n\n" creates two new lines (like a paragraph break) in the alert box 
document.Theft1.orgname.focus(); 
return false; 
}

//check a field to make sure it's not blank 
if(document.Theft1.contname.value == '') { 
alert('Please fill in the field:\n\nContact name'); 
document.Theft1.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.Theft1.contemail1a.value == '') && (document.Theft1.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.Theft1.contemail1a.focus(); 
return false; 
}
}
// check both email fields to make sure just the first isn't blank 
if((document.Theft1.contemail1b.value != '') && (document.Theft1.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.Theft1.contemail1a.focus(); 
return false; 
}
// check both email fields to make sure just the second isn't blank 
if((document.Theft1.contemail1a.value != '') && (document.Theft1.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.Theft1.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.Theft1.contemail1a.value != '') && (document.Theft1.contemail1b.value != '')) { 
// check if email address entered has got valid syntax 
if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.Theft1.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.Theft1.contemail1a.focus(); 
return false; 
}  //end the first if within if
if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.Theft1.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.Theft1.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.Theft1.contemail1a.value.toLowerCase() != document.Theft1.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.Theft1.contemail1a.focus(); 
return false; 
} //end if 

/* 		NEW EMAIL PROCEDURE ENDS HERE 	*/



//check a field to make sure it's not blank 
if(document.Theft1.contphone1.value == '') { 
alert('Please fill in the field:\n\nContact phone number'); 
document.Theft1.contphone1.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.Theft1.indemnity_limit_required); 

//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: \n'Please state the Limit of Indemnity required for any one claim'  \n \nSorry, but you MUST make a selection!"); 
document.Theft1.indemnity_limit_required[0].focus(); // and focus on the first option
return false; 
}

//check a field to make sure it's not blank 
// There's a pound sign here anyway, so check for nothing OR (||) just the pound sign plus nothing - the danger is that just a space will satisfy the conditional check, but if we check for spaces and then go for checking for numbers, where do we stop? They can write 12K, so we have to allow letters, too.
// OK, a more thorough test would check for alphanumeric characters, but this'll do unless and until we find that users make mistakes
if((document.Theft1.annual_wages_of_employees.value == '') || (document.Theft1.annual_wages_of_employees.value == '£')) { 
alert('Please fill in the field:\n\nestimated annual wages and salaries of all employees'); 
document.Theft1.annual_wages_of_employees.focus(); 
return false; 
}

//check a field to make sure it's not blank 
if(document.Theft1.total_employees.value == '') { 
alert('Please fill in the field:\n\ntotal number of employees'); 
document.Theft1.total_employees.focus(); 
return false; 
}

//check a field to make sure it's not blank 
if(document.Theft1.total_volunteers.value == '') { 
alert('Please fill in the field:\n\ntotal number of volunteers'); 
document.Theft1.total_volunteers.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.Theft1.occasion_to_question_honesty); 

//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 there been occasion to question the honesty of any Insured Person...' question.  \n \nSorry, but you MUST make a selection!"); 
document.Theft1.occasion_to_question_honesty[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.Theft1.occasion_to_question_honesty); 
if((radioVal == 'Yes') && (document.Theft1.question_honesty_details.value == '')) { 
alert("As you have answered 'Yes' to the question:\n\n'Has there been occasion to question the honesty of any Insured Person...', \n\nplease fill in the field:\n\nIf \'Yes\' please give details"); 
document.Theft1.question_honesty_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.Theft1.e_funds_transfer); 
//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'Do you use any form of Electronic Funds Transfer?' question.  \n \nSorry, but you MUST make a selection!"); 
document.Theft1.e_funds_transfer[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.Theft1.e_funds_transfer); 
if((radioVal == 'Yes') && (document.Theft1.e_funds_transfer_details.value == '')) { 
alert("As you have answered 'Yes' to the question:\n\n'Do you use any form of Electronic Funds Transfer?', \n\nplease fill in the field:\n\nIf \'Yes\' please give full details of the Systems in use..."); 
document.Theft1.e_funds_transfer_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.Theft1.computerised_accounting); 
//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 any of your Accounting, Salary or \n Purchasing functions computerised?' question.  \n \nSorry, but you MUST make a selection!"); 
document.Theft1.computerised_accounting[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.Theft1.computerised_accounting); 
if((radioVal == 'Yes') && (document.Theft1.details_of_computerised_accounting.value == '')) { 
alert("As you have answered 'Yes' to the question:\n\n'Are any of your Accounting, Salary or \n Purchasing functions computerised?', \n\nplease fill in the field:\n\nIf the answer to 5 is 'Yes', please give details"); 
document.Theft1.details_of_computerised_accounting.focus(); 
return false; 
}




/* check to see whether a particular radio button has a particular value, and IFF it does, check that another radio button has been selected */
var radioVal1 = valRadiobutton(document.Theft1.computerised_accounting);
var radioVal2 = valRadiobutton(document.Theft1.security_checks_in_comp_ops);
// Check the value of the FIRST radio button group in question
if((radioVal1 == 'Yes') &&
// Check the value of the SECOND radio button group in question
(radioVal2 == '')) { 
alert("As you have answered 'Yes' to the question:\n\n'Are any of your Accounting, Salary or \nPurchasing functions computerised?', \n\nplease answer the question:\n\nAre security checks built into your computer operations?"); 
document.Theft1.security_checks_in_comp_ops[0].focus(); // and focus on the first option
return false; 
}

/* check to see whether a particular radio button has a particular value, and IFF it does, check that another radio button has been selected */
var radioVal1 = valRadiobutton(document.Theft1.computerised_accounting);
var radioVal2 = valRadiobutton(document.Theft1.security_examined_by_external_auditors);
// Check the value of the FIRST radio button group in question
if((radioVal1 == 'Yes') &&
// Check the value of the SECOND radio button group in question
(radioVal2 == '')) { 
alert("As you have answered 'Yes' to the question:\n\n'Are any of your Accounting, Salary or \nPurchasing functions computerised?', \n\nplease answer the question:\n\nIs your computer security examined by external auditors?"); 
document.Theft1.security_examined_by_external_auditors[0].focus(); // and focus on the first option
return false; 
}

/* check to see whether a particular radio button has a particular value, and IFF it does, check that another radio button has been selected */
var radioVal1 = valRadiobutton(document.Theft1.computerised_accounting);
var radioVal2 = valRadiobutton(document.Theft1.vetted_security);
// Check the value of the FIRST radio button group in question
if((radioVal1 == 'Yes') &&
// Check the value of the SECOND radio button group in question
(radioVal2 == '')) { 
alert("As you have answered 'Yes' to the question:\n\n'Are any of your Accounting, Salary or \nPurchasing functions computerised?', \n\nplease answer the question:\n\nHas a security firm vetted your computer security?"); 
document.Theft1.vetted_security[0].focus(); // and focus on the first option
return false; 
}

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

//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 question:\n\n'In respect of any of the risks to which this Proposal relates and any\norganisation in which your personnel have been engaged, have there been\nany losses or claims within the last 5 years, whether insured or not?'\n\nSorry, but you MUST make a selection!"); 
document.Theft1.losses_or_claims[1].focus(); 
return false; 
}

//check a field to make sure it's not blank IFF particular radio selection made
var radioVal = valRadiobutton(document.Theft1.losses_or_claims); 
if((radioVal == 'Yes') && (document.Theft1.date.value == '' || document.Theft1.date.value==" " )) { 
alert("As you have answered 'Yes' to the question:\n\n'In respect of any of the risks...\nhave there been any losses or claims \nwithin the last 5 years, whether insured or not?', \n\nplease fill in the field:\n\n'date', using the format: \n\n[dd/mm/yyyy] "); 
document.Theft1.date.focus(); 
return false; 
}



//check a field to make sure it's not blank IFF particular radio selection made
var radioVal = valRadiobutton(document.Theft1.losses_or_claims); 
if((radioVal == 'Yes') && (document.Theft1.circumstances.value == '')) { 
alert("As you have answered 'Yes' to the question:\n\n'In respect of any of the risks...\nhave there been any losses or claims \nwithin the last 5 years, whether insured or not?', \n\nplease fill in the field:\n\n'circumstances'"); 
document.Theft1.circumstances.focus(); 
return false; 
}

//check a field to make sure it's not blank IFF particular radio selection made
var radioVal = valRadiobutton(document.Theft1.losses_or_claims); 
if((radioVal == 'Yes') && (document.Theft1.amount.value == '')) { 
alert("As you have answered 'Yes' to the question:\n\n'In respect of any of the risks...\nhave there been any losses or claims \nwithin the last 5 years, whether insured or not?', \n\nplease fill in the field:\n\n'amount'"); 
document.Theft1.amount.focus(); 
return false; 
}

//check a field to make sure it's not blank IFF particular radio selection made
var radioVal = valRadiobutton(document.Theft1.losses_or_claims); 
if((radioVal == 'Yes') && (document.Theft1.steps_taken.value == '')) { 
alert("As you have answered 'Yes' to the question:\n\n'In respect of any of the risks...\nhave there been any losses or claims \nwithin the last 5 years, whether insured or not?', \n\nplease fill in the field:\n\n'steps taken to prevent a recurrence'"); 
document.Theft1.steps_taken.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.Theft1.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.Theft1.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
