// CHECK VALID EMAIL //
function isEmailAddress (string) {
            var addressPattern = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
            return addressPattern.test(string);
}

//Check Work Details //
function check_workdets(a,id){
    
    if(a.value == "Other"){
        document.getElementById('wother' + id).style.display='block';
    }
    else{
        document.getElementById('wother' + id).style.display='none';        
    }
    
}

// Check Driving Details //
function check_driving(a,id){
    
    if(a.value == "Yes"){
        document.getElementById('drive' + id).style.display='block';
    }
    else{
        document.getElementById('drive' + id).style.display='none';        
    }
    
}

// Check Employment Details //
function check_employment(a,id){
    
    if(a.value == "Employed"){
        document.getElementById('employ' + id).style.display='block';
    }
    else{
        document.getElementById('employ' + id).style.display='none';        
    }
    
}

// Check Joint App Toggle //
function joint_app(a){
    
    if(a.value == "Yes"){
        document.getElementById('app2').style.display='block';   
    }
    else{
        document.getElementById('app2').style.display='none';        
    }
    
}

// Check Benefit //
function check_benefit(a){
 
    if(a.value == "Other"){
        document.getElementById('obenefit').style.display='block';
    }
    else{
        document.getElementById('obenefit').style.display='none';        
    }
    
}  

// Check Term //
function check_term(a){
 
    if(a.value == "Other"){
        document.getElementById('oterm').style.display='block';
    }
    else{
        document.getElementById('oterm').style.display='none';        
    }
    
} 

// Check Mortgage Protection //
function check_mortprot(a){
 
    if(a.value == "Yes"){
        document.getElementById('mortprot').style.display='block';
    }
    else{
        document.getElementById('mortprot').style.display='none';        
    }
    
} 

// Is Currency //
function isCurrency(a){
 
 //remove non-currency chars
    var val = a.value;
    
    val = val.replace(/[^0-9.,]/g, ''); // strip non-currency chars leaving comma and dot
    if(a.value != val){
        a.value = val; // replace textbox value 
    }        
    
}

// Check Numbers if Telephone Number Display Best Contact Time option //
function check_num(obj){
    
    var tel = obj.value;
    tel = tel.replace(/[^0-9]/g, "");
    
    if(obj.value != tel){
          obj.value = tel;
    }
           
           
    if(obj.name == "telephone"){           
        if(obj.value != ""){
            document.getElementById('btime').style.display='block';
        }        
        else{
            document.getElementById('btime').style.display='none';        
        }
    }
    
}

// Parse Date Function //
function parseDate(value) {
 
  var date = NaN;
  var dateArray = value.split("/");
  
    var day = parseInt(dateArray[0],10);
    var month = parseInt(dateArray[1],10);
    var year = parseInt(dateArray[2],10);
   
    if (!(isNaN(day) || isNaN(month) || isNaN(year))) {
      date = new Date(year,month-1,day);

      // Verify that day, month and year are the same
      // as their original values:
      var newMonth = date.getMonth()+1;
      var newDay = date.getDate();
      var newYear = date.getFullYear();
        
      if ( (newYear != year) || (newMonth != month) ||
           (newDay != day) ) {
        date = NaN;
      }
    }
  
  return date;
}

// Make Money //
function toMoney(obj) {
    
    if(obj.value != ""){
        var mynum = parseDecimal(obj.value);        
        mynum = mynum.toFixed(2);
        obj.value = addCommas(mynum);
    }
    
}

// Check D.O.B //
function check_dob(a){
 
 //remove illegal chars
    var val = a.value;
    
    val = val.replace(/[^0-9\/]/g, ''); // strip non-currency chars leaving comma and dot
    if(a.value != val){
        a.value = val; // replace textbox value 
    }        
    
}


// Formatting routines //
function stripNonDecimal(value) {
   return value.replace(/[^0-9.]/g, "");
}

function parseDecimal(value) {
  return parseFloat(stripNonDecimal(value));
}

// add commas to currency //
function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

// Check Quote Form //
function check_form(a){

            var msg = "";
            var msg2 = "";
            var msg3 = "";
            
            if(a.jointapp.value==""){
                msg += "Joint Policy";
            }
            
            if(a.title1.value==""){
                msg += "Title\n";
            }

            if(a.firstname1.value==""){
                msg += "First Name\n";
            }
            
            if(a.lastname1.value==""){
                msg += "Last Name\n";
            } 
            
            if(a.homephone.value==""){
                msg += "Home Phone\n";
            }            
            
            if(a.email.value==""){
                msg += "Email\n";
            }
            else{
                if (a.email.value != "" & !isEmailAddress(a.email.value)) {
                    msg += "Invalid Email Address\n";
                }
            }            

            if(a.homephone.value==""){
                msg += "Home Phone\n";
            }
                        
            if(a.address.value==""){
                msg += "Address\n";
            }
            
            if(a.postcode.value==""){
                msg += "Post Code\n";
            }   
            
            if(a.dob1.value==""){
                msg += "D.O.B. (DD/MM/YYYY)\n";
            }                        
            else {
                if(isNaN(parseDate(a.dob1.value))){
                     msg += "D.O.B. Invalid (DD/MM/YYYY)\n";                
                }
            }
            
            if(a.smoke1.value==""){
                msg += "Smoker\n";
            }            
            
            if(a.residence1.value==""){
                msg += "Country of Residence\n";
            }
            
            if(a.nationality1.value==""){
                msg += "Nationality\n";
            } 
            
            if(a.occupation1.value==""){
                msg += "Occupation\n";
            } 
            
            if(a.workdets1.value==""){
                msg += "Work Details\n";
            }
            else{
                if(a.workdets1.value=="Other" && a.workother1.value==""){
                    msg += "Work Details, Please Specify\n";
                }
            }
            
            if(a.driving1.value==""){
                msg += "Driving Involvement\n";
            }
            else{
                if(a.driving1.value=="Yes" && a.milage1.value==""){
                    msg += "Annual Milage\n";
                }
            }
            
            if(a.employment1.value==""){
                msg += "Employment\n";
            }
            else{
                if(a.employment1.value=="Employed" && a.employer1.value==""){
                    msg += "Employer\n";
                }
            } 
            
            // Check if errors and add person indicator
            
            if(msg != ""){
                msg = "[ Your Details ]\n\n" + msg;
            }                                
            
            // Check second person details

            if(a.jointapp.value == "Yes"){
                        
                if(a.title2.value==""){
                    msg2 += "Title\n";
                }

                if(a.firstname2.value==""){
                    msg2 += "First Name\n";
                }
            
                if(a.lastname2.value==""){
                    msg2 += "Last Name\n";
                } 
                
                if(a.dob2.value==""){
                    msg2 += "D.O.B. (DD/MM/YYYY)\n";
                }                        
                else {
                    if(isNaN(parseDate(a.dob2.value))){
                        msg2 += "D.O.B. Invalid (DD/MM/YYYY)\n";                
                    }
                }
            
                if(a.smoke2.value==""){
                    msg2 += "Smoker\n";
                } 
                
                if(a.occupation2.value==""){
                    msg2 += "Occupation\n";
                } 
            
                if(a.workdets2.value==""){
                    msg2 += "Work Details\n";
                }
                else{
                    if(a.workdets2.value=="Other" && a.workother2.value==""){
                        msg2 += "Work Details, Please Specify\n";
                    }
                }
            
                if(a.driving2.value==""){
                    msg2 += "Driving Involvement\n";
                }
                else{
                    if(a.driving2.value=="Yes" && a.milage2.value==""){
                        msg2 += "Annual Milage\n";
                    }
                }
            
                if(a.employment2.value==""){
                    msg2 += "Employment\n";
                }
                else{
                    if(a.employment2.value=="Employed" && a.employer2.value==""){
                        msg2 += "Employer\n";
                    }
                }
                
                if(msg2 != ""){
                    msg += "\n[ Partner Details ]\n\n" + msg2;
                }                    
            }        
            
            if(a.benefit.value==""){
                msg3 += "Benefit Amount\n";
            }
            else {
                if(a.benefit.value=="Other" && a.benefitother.value==""){
                        msg3 += "Benefit, Please Specify\n";
                }
            }                
            
            if(a.indexlinked.value==""){
                msg3 += "Index Linked\n";
            }
            
            if(a.term.value==""){
                msg3 += "Term Of Contract\n";
            }
            else {
                if(a.term.value=="Other" && a.termother.value==""){
                    msg3 += "Term, Please Specify\n";
                }               
            }     
            
            if(a.mortgageprotection.value==""){
                msg3 += "Mortgage Protection\n";
            }
            else {
                if(a.mortgageprotection.value=="Yes" && a.mortgagetype.value==""){
                    msg3 += "Mortgage Type\n";
                }
            }                
                            
            if(a.covertype.value==""){
                msg3 += "Cover Type\n";
            }
            
            if(a.information.value==""){
                msg3 += "Existing Conditions\n";
            }
            
            if(msg3 != ""){
                msg += "\n[ Policy Requirements ]\n\n" + msg3;
            } 
            
            // Check and Display Message               
                
            if(msg != ""){
                alert("Please complete the following...\n\n" + msg);
                return false;
            }
            else{
                return true;
            }

}

