﻿
        //¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯//
       //    File Name:  claim.js                                     //
      //   Programmer:  Josh Stodola                                 //
     // Date Written:  November 21, 2006                            //
    //      Purpose:  To validate the fields on the new claim      //
   //                reporting web application.                   //
  //_____________________________________________________________//

function validateForm() {
	var hold, hold2;

	// POLICY NUMBER
	hold = document.frmReportClaim.txtPolicy.value;
	if(containsBlanks(hold,8)) {
		alert('You must enter an 8 character policy number!');
		document.frmReportClaim.txtPolicy.select();
		return false;
	}
	else {
		hold2 = hold.substring(0,2).toUpperCase();
		if(hold2 != 'AR' && hold2 != 'AU' && hold2 != 'DC' && hold2 != 'DO' && hold2 != 'DP' && hold2 != 'FG' && hold2 != 'HG' && hold2 != 'MC' && hold2 != 'OL' && hold2 != 'PL' && hold2 != 'RR' && hold2 != 'RV' && hold2 != 'UL') {
			alert('Invalid policy prefix - ' + hold2 + '!');
			document.frmReportClaim.txtPolicy.select();
			return false;
		}
		else {
			hold2 = hold.substring(2,8);
			if(!isNumeric(hold2)) {
				alert('Last 6 digits of policy number must be numeric!');
				document.frmReportClaim.txtPolicy.select();
				return false;	
			}
		}
	}
    
    // INSURED'S NAME
	hold = document.frmReportClaim.txtName.value;
	if(isBlanks(hold)) {
	    alert('You must enter your name!');
		document.frmReportClaim.txtName.select();
		return false;
	}
	
	// PHONE NUMBERS
	if(isBlanks(document.frmReportClaim.txtHome.value)) {
	    if(isBlanks(document.frmReportClaim.txtCell.value)) {
	        if(isBlanks(document.frmReportClaim.txtWork.value)) {
	            alert('You must enter at least one phone number!');
		        document.frmReportClaim.txtHome.focus();
		        return false;
		    }
		    else {
		        if(countNums(document.frmReportClaim.txtWork.value) < 7) {
		            alert('Invalid work phone number!');
		            document.frmReportClaim.txtWork.select();
		            return false;
		        }
		    }
		}
		else {
		    if(countNums(document.frmReportClaim.txtCell.value) < 7) {
		        alert('Invalid cell phone number!');
		        document.frmReportClaim.txtCell.select();
		        return false;
		    }
		}
	}
	else {
		if(countNums(document.frmReportClaim.txtHome.value) < 7) {
		    alert('Invalid home phone number!');
		    document.frmReportClaim.txtHome.select();
		    return false;
		}
	}
	
	// EMAIL ADDRESS
	hold = document.frmReportClaim.txtEmail.value;
	if(!isBlanks(hold)) {
	    if(hold.indexOf('@') == -1 || hold.indexOf('.') == -1) {
		    alert('Invalid email address!');
		    document.frmReportClaim.txtEmail.select();
		    return false;
	    }
	}
	
	// DATE OF LOSS
	hold = document.frmReportClaim.txtLossDate.value;
	if(countNums(hold) < 6) {
		alert('Please enter a valid date of loss in MM/DD/YYYY format!');
		document.frmReportClaim.txtLossDate.select();
		return false;
	}
	
	// LOCATION
	hold = document.frmReportClaim.txtLocation.value;
	if(isBlanks(hold)) {
	    alert('You must enter the location of the loss!');
		document.frmReportClaim.txtLocation.select();
		return false;
	}
	
	// VEHICLE DESCRIPTION	
	hold = document.frmReportClaim.txtPolicy.value.substring(0,2);
	if(hold == 'AU' || hold == 'AR' || hold == 'MC' || hold == 'RV') {
	    if(isBlanks(document.frmReportClaim.txtAutoDesc.value)) {
			alert('Please enter the vehicle description for an auto loss!');
		    document.frmReportClaim.txtAutoDesc.select();
		    return false;
		}
	}
	
	// LOSS DESCRIPTION
	hold = document.frmReportClaim.txtLoss.value;
	if(isBlanks(hold)) {
	    alert('You must enter a loss description!');
		document.frmReportClaim.txtLoss.select();
		return false;
	}
	
	return true;
}

function isBlanks(val) {
	for(var i = 0; i < val.length; i++) {
		var c = val.charAt(i);
		if((c != ' ') && (c != '\n') && (c != ''))
		    return false;
	}
	return true;
}

function isNumeric(val) {
	var valstr = val + "";
	for (var i = 0; i < valstr.length; i++) {
		if (valstr.charAt(i) < "0" || valstr.charAt(i) > "9")
			return false;
	}
	return true;
}

function containsBlanks(val,len) {
    if(val == null || val == '' || val.length != len)
        return true;
        
	for(var i = 0; i < val.length; i++) {
		var c = val.charAt(i);
		if((c == ' ') || (c == '\n') || (c == ''))
		    return true;
	}
	return false;
}

function countNums(val) {
    var cnt = 0;
	var valstr = val + "";
	for (var i = 0; i < valstr.length; i++) {
		if (valstr.charAt(i) >= "0" && valstr.charAt(i) <= "9")
			cnt += 1;
	}
	return cnt;
}