﻿/*
 *
 * USED TO VALIDATE THE CONTROLS
 *   ON THE CERTIFICATES SCREEN
 *
 */

function validateForm(frm) {
	var hold;

	// POLICY NUMBER
	hold = frm.txtPolicy;
	if(containsBlanks(hold.value, 8)) {
		alert('You must enter an 8 character policy number');
		hold.select();
		hold.focus();
		return false;
	}
	else {
		hold2 = hold.value.substring(0, 2).toUpperCase();
		if(hold2 != 'AR' && hold2 != 'AU' && hold2 != 'MC' && hold2 != 'RV') {
			alert('Invalid policy prefix: ' + hold2 + '\n\nCertificates may be printed for auto policies only');
			hold.select();
		    hold.focus();
			return false;
		}
		else {
			hold2 = hold.value.substring(2, 8);
			if(!isNumeric(hold2)) {
				alert('Last 6 digits of policy number must be numeric');
				hold.select();
		        hold.focus();
				return false;	
			}
		}
	}
	
	//FILE NUMBER / ZIP CODE
	hold = frm.txtFileZip;
	if(hold.value.length < 5 || containsBlanks(hold.value, hold.value.length)) {
		alert('You must enter a valid file number or zip code.');
		hold.select();
		hold.focus();
		return false;
	}
	else {
	    if(!isNumeric(hold.value)) {
	        alert('File number must be numeric');
		    hold.select();
		    hold.focus();
		    return false;
	    }
	}
	
	return true;
}

function printCertificate(vehNum) {
    try {
        var winOpt = 'width=500, height=420, resizable=0, scrollbars=0';
        var theWin = window.open('certificates3.aspx?veh=' + vehNum, 'theWin', winOpt);
        theWin.focus();
    }
    catch(e) {
        alert('You will need to allow popup windows to print your proof of insurance certificates');
    }
}

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;
}