	// JavaScript Document
	//Checks if msg is empty and assign empty value
	function checkMsg(msg){
		if(msg.lenght<1){
			msg = ""		
		}
		return(msg)
	}
	
	// return the value of the radio button that is checked
	// return an empty string if none are checked, or
	// there are no radio buttons
	function getCheckedValue(radioObj) {
		if(!radioObj)
			return "";
		var radioLength = radioObj.length;
		if(radioLength == undefined)
			if(radioObj.checked)
				return radioObj.value;
			else
				return "";
		for(var i = 0; i < radioLength; i++) {
			if(radioObj[i].checked) {
				return radioObj[i].value;
			}
		}
		return "";
	}
	
	// set the radio button with the given value as being checked
	// do nothing if there are no radio buttons
	// if the given value does not exist, all the radio buttons
	// are reset to unchecked
	function setCheckedValue(radioObj, newValue) {
		if(!radioObj)
			return;
		var radioLength = radioObj.length;
		if(radioLength == undefined) {
			radioObj.checked = (radioObj.value == newValue.toString());
			return;
		}
		for(var i = 0; i < radioLength; i++) {
			radioObj[i].checked = false;
			if(radioObj[i].value == newValue.toString()) {
				radioObj[i].checked = true;
			}
		}
	}
	
	//#Email validation checking.
	function validateEmailv2(email){	 
		var splitted = email.match("^(.+)@(.+)$");
		if(splitted == null) return false;
		
		if(splitted[1] != null ){
		  var regexp_user=/^\"?[\w-_\.]*\"?$/;
		  if(splitted[1].match(regexp_user) == null) return false;
		}
		
		if(splitted[2] != null){
		  var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
		  if(splitted[2].match(regexp_domain) == null){
			var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
			if(splitted[2].match(regexp_ip) == null) return false;
		  }// if
		  return true;
		}
		return false;
	}

	//validate form2 - life insurance application	
	function validate(){
			//alert("Insurance Quote is about to be submitted for processing.")
			//form variables
			var insurancetype=document.getElementById("insurancetype").value
			var fName=document.getElementById("firstName").value
			var lInsurance=document.getElementById("lifeinsurance").value
			var TPDInsurance=document.getElementById("tpd").value
			var IPInsurance=document.getElementById("incomeprotection").value
			var TInsurance=document.getElementById("criticalillness").value
			var lName=document.getElementById("lastName").value
			var address=document.getElementById("address").value
			var suburb=document.getElementById("suburb").value
			var state=document.getElementById("state").value
			var postcode=document.getElementById("postcode").value
			var day=document.getElementById("day").value
			var month=document.getElementById("month").value
			var year=document.getElementById("year").value	
			var marital=document.getElementById("marital").value
			var occupation=document.getElementById("occupation").value
			var occupation_other=document.getElementById("occupation_other").value
			var referred_by=document.getElementById("referred_by").value
			var phone=document.getElementById("phone").value
			var at=document.getElementById("email").value
			var mobile=document.getElementById("mobile").value
			var referred_by=document.getElementById("referred_by").value
/**/	
			//error message variable
			var msg=""		
	
			//define background colors for error state (errorColor) and success state (noColor)
			var errorColor="#D2EFF5"
			var noColor = "white"
	
			//set all fields to be valid, and if invalid code wills et to false
			submitOK="true"
			
			
			//Validates insurance type
			if (insurancetype=='select'){
				submitOK="false"
				msg = checkMsg(msg)
				msg = "- Insurance Quote Type\n"; 

			}		
			
			//Validates Type of Cover
			if (document.getElementById("lifeinsurance").checked != true && document.getElementById("tpd").checked != true && document.getElementById("incomeprotection").checked != true && document.getElementById("criticalillness").checked != true){
				submitOK="false"
				msg = checkMsg(msg)
				
				document.getElementById("lifeinsurance").style.background=errorColor
				document.getElementById("tpd").style.background=errorColor
				document.getElementById("incomeprotection").style.background=errorColor
				document.getElementById("criticalillness").style.background=errorColor
				
				//console.log("No Type of Cover Selected")
				
				msg = msg + "- Type of Cover\n";
			}else{
				
				document.getElementById("lifeinsurance").style.background=noColor
				document.getElementById("tpd").style.background=noColor
				document.getElementById("incomeprotection").style.background=noColor
				document.getElementById("criticalillness").style.background=noColor

			}
			
			//Validates first name
			if (fName.length<1){
				submitOK="false"
				msg = checkMsg(msg)
				document.getElementById("firstName").style.background=errorColor
				msg = msg + "- First Name\n";
			}else{
				document.getElementById("firstName").style.background=noColor;
			}
			
			
			//Validates last name
			if (lName.length<1){
				submitOK="false"
				msg = checkMsg(msg)
				document.getElementById("lastName").style.background=errorColor
				msg = msg + "- Last Name\n";
			}else{
				document.getElementById("lastName").style.background=noColor;
			}
			//Validates address
			if (address.length<1){
				submitOK="false"
				msg = checkMsg(msg)
				document.getElementById("address").style.background=errorColor
				msg = msg + "- Address\n";
			}else{
				document.getElementById("address").style.background=noColor;
			}
			//Validates suburb
			if (suburb.length<1){
				submitOK="false"
				msg = checkMsg(msg)
				document.getElementById("suburb").style.background=errorColor
				msg = msg + "- Suburb\n";
			}else{
				document.getElementById("suburb").style.background=noColor;
			}
			//Validates state
			if (state.length<1){
				submitOK="false"
				msg = checkMsg(msg)
				document.getElementById("state").style.background=errorColor
				msg = msg + "- State\n";
			}else{
				document.getElementById("state").style.background=noColor;
			}
			//Validates postcode
			if (postcode.length<1){
				submitOK="false"
				msg = checkMsg(msg)
				document.getElementById("postcode").style.background=errorColor
				msg = msg + "- Postcode\n";
			}else{
				document.getElementById("postcode").style.background=noColor;
			}
			//Validates DOB - day
			if (day.length<2 || day<1 || day>31){
				submitOK="false"
				document.getElementById("day").style.background=errorColor
				msg = checkMsg(msg)
				msg = msg + "- Date of Birth (Day) \n";
			}else{
				document.getElementById("day").style.background=noColor;
			}
			//Validates DOB - month
			if (month.length<2 || month<1 || month>12){
				submitOK="false"
				document.getElementById("month").style.background=errorColor
				msg = checkMsg(msg)
				msg = msg + "- Date of Birth (Month)\n";
			}else{
				document.getElementById("month").style.background=noColor;
			}			
			//Validates DOB - year
			if (year.length<4 || year<1870 || year>2100){
				submitOK="false"
				document.getElementById("year").style.background=errorColor
				msg = checkMsg(msg)
				msg = msg + "- Date of Birth (Year) \n";
			}else{
				document.getElementById("year").style.background=noColor;
			}			
			//validate marital status
			var result = getCheckedValue(document.forms['form1'].elements['marital'])
			if (marital=='select'){
				submitOK="false"
				msg = checkMsg(msg)
				msg = msg + "- Your Marital Status\n";
			}
			//Validates gender
			var result = getCheckedValue(document.forms['form1'].elements['sex'])			
			if (result==''){
				submitOK="false"
				msg = checkMsg(msg)
				msg = msg + "- Gender\n";
			}
			
			//Validates Self-Employed status
			var result = getCheckedValue(document.forms['form1'].elements['self_employed'])			
			if (result==''){
				submitOK="false"
				msg = checkMsg(msg)
				msg = msg + "- Are You Self-Employed?\n";
			}
			
			//Validates Smoker status
			var result = getCheckedValue(document.forms['form1'].elements['smoker'])			
			if (result==''){
				submitOK="false"
				msg = checkMsg(msg)
				msg = msg + "- Are You a Smoker?\n";
			}		
			
			//Validates occupation
			if (occupation=='select'){
				submitOK="false"
				msg = checkMsg(msg)
				msg = msg + "- Your Occupation\n";
			}
			//Validates occupation other
			if (occupation=='other' && occupation_other.length<1){
				submitOK="false"
				msg = checkMsg(msg)
				msg = msg + "- Your Occupation\n";
			}	
			//validate Where Did You hear About US
			//var result = getCheckedValue(document.forms['form1'].elements['referred_by'])
			if (referred_by=='Information not provided'){
				submitOK="false"
				msg = checkMsg(msg)
				msg = msg + "- How did you find out about us?\n";
			}
			//Validates phone number
			if (phone.length<2){
				submitOK="false"
				msg = checkMsg(msg)
				document.getElementById("phone").style.background=errorColor	
				msg = msg + "- Phone Number\n";
			}else{
				document.getElementById("phone").style.background=noColor;		
			}		
			//Validates email address
			var result = validateEmailv2(at)
			if (result==false){
				submitOK="false"
				msg = checkMsg(msg)
				document.getElementById("email").style.background=errorColor			
				msg = msg + "- Email Address\n";
			}else{
				document.getElementById("email").style.background=noColor;	
			}		
			//####################################################################################
			// VALIDATION OF SHOW HIDE FORM STARTS HERE
			//####################################################################################
			//VALIDATES TOTAL LIFE INSURANCE AMOUNT IF LIFE INSURANCE IS SELECTED
			if(document.getElementById("lifeinsurance").checked){
				//alert("Life Insurance Checked")	
				var tli_cover_requested=document.getElementById("tli_cover_requested").value
				if (tli_cover_requested.length<2){
					submitOK="false"
					msg = checkMsg(msg)
					document.getElementById("tli_cover_requested").style.background=errorColor	
					msg = msg + "- Life Insurance Amount\n";
				}else{
					document.getElementById("tli_cover_requested").style.background=noColor;		
				}
			}
			//VALIDATES TOTAL TPD AMOUNT IF LIFE INSURANCE IS SELECTED
			if(document.getElementById("tpd").checked){
				//alert("TPD Checked")	
				var tpd_cover_requested=document.getElementById("tpd_cover_requested").value
				if (tpd_cover_requested.length<1){
					submitOK="false"
					msg = checkMsg(msg)
					document.getElementById("tpd_cover_requested").style.background=errorColor	
					msg = msg + "- Total Permanent Disability Amount\n";
				}else{
					document.getElementById("tpd_cover_requested").style.background=noColor;	
				}
			}
			
			//VALIDATES INCOME PROTECTION IF LIFE INSURANCE IS SELECTED
			if(document.getElementById("incomeprotection").checked){
				//alert("Income Protection Checked")	
				var gross=document.getElementById("gross").value
				if (gross.length<1){
					submitOK="false"
					msg = checkMsg(msg)
					document.getElementById("gross").style.background=errorColor	
					msg = msg + "- Gross annual income\n";
				}else{
					document.getElementById("gross").style.background=noColor;		
				}			

				var monthly_benefit_required=document.getElementById("monthly_benefit_required").value
				if (monthly_benefit_required.length<1){
					submitOK="false"
					msg = checkMsg(msg)
					document.getElementById("monthly_benefit_required").style.background=errorColor	
					msg = msg + "- Monthly benefit required\n";
				}else{
					document.getElementById("monthly_benefit_required").style.background=noColor;		
				}
			
			}
			
			//VALIDATES TOTAL CRITICAL ILLNESS AMOUNT IF LIFE INSURANCE IS SELECTED
			if(document.getElementById("criticalillness").checked){
				//alert("Critical Illness Checked")	
				var trauma_amount_requested=document.getElementById("trauma_amount_requested").value
				if (trauma_amount_requested.length<1){
					submitOK="false"
					msg = checkMsg(msg)
					document.getElementById("trauma_amount_requested").style.background=errorColor	
					msg = msg + "- Critical Illness Amount\n";
				}else{
					document.getElementById("trauma_amount_requested").style.background=noColor;		
				}
				
				
				if (valButton(document.getElementById("form1").product_options) == null){
				submitOK="false"
				msg = checkMsg(msg)				
				msg = msg + "- Product Options\n";				
				//console.log()				
				//msg = msg + "- Type of Cover\n";
			}else{
				
				//document.getElementById("product_options").value

			}
				
			}
	
			//display message if anything is found to be invalid
			if (submitOK=="false"){
				msg = "Please note all the following fields are required:\n\n" + msg
				alert(msg)
				return false;
			}
	}

function valButton(btn) {
    var cnt = -1;
    for (var i=btn.length-1; i > -1; i--) {
        if (btn[i].checked) {cnt = i; i = -1;}
    }
    if (cnt > -1) return btn[cnt].value;
    else return null;
}
    