var NetscapeBrowser = (navigator.appName == "Netscape");
var BrowserVersion = parseInt(navigator.appVersion);
function RepeatString(RepeatStr, RepeatNum) {
	var newString = ""; 
	for (var x=1; x<=parseInt(RepeatNum, 10); x++) { 
        newString = newString + RepeatStr; 
    } 
	return newString
}
function ValidLicense(LicenseState,LicenseNum,licnumfldname) {
	//send values not field references. calling script deals with the fields
	var cLicState = trimString(LicenseState);
	var cLicNum = trimString(LicenseNum);
	if ((cLicState != 'MT') && (cLicState != 'WY')) {
		cLicNum = cLicNum.replace(/-/g,''); //rwg 2008-06-17
	}
	if (cLicState != 'MT') {
		cLicNum = cLicNum.replace(/ /g,''); //rwg 2008-06-17
	}
	cLicNum = cLicNum.toUpperCase();  //rwg 2008-06-11
	var len = cLicNum.length;
	var lok = (len > 1 && cLicState.length == 2);
	var nZeroPad = 0;
	if (lok) {
		//make sure not all the same character
		lok = false;
		c1 = cLicNum.charAt(0);
	  	for (var i = 0; i < len; i++) {
			if (c1 != cLicNum.charAt(i)) {
				lok = true;
				break;
			}
		}
	}
	if (lok) {
	   	var lnum = !isNaN(cLicNum);
	  	var a1 = cLicNum.charAt(0);
		//first char is not a num and last is a number
	    var la1 = isNaN(a1) && !isNaN(cLicNum.charAt(len));
		var oneletterprefix = isNaN(a1) && !isNaN(cLicNum.substring(1));
		var a2 = cLicNum.substring(0,2);
	   	var la2 = (isNaN(a2) &&  !isNaN(cLicNum.substring(len-2,len)));
		switch (cLicState) {
			case 'PA' :
				lok = lnum && (len == 8);
				break; 
			case 'AK' :
				//rwg 2008-06-10 lok = (lnum && len < 8);
				lok = (lnum && (len <= 7));
				if (lok && (len < 7)) {
					nZeroPad = (7 - len);
				}
				break;
			case 'AL' :
				lok = (lnum && len == 7);
				break;
			case 'AR' :
				//rwg 2008-06-10 lok = (lnum && (len == 8 || len == 9));
				lok = (lnum && (len == 9));
				break;
			case 'AZ' :
				//rwg 2008-06-10 lok = ((la1 || la2) && len < 10) || (la1 && len == 8) || (lnum && len == 9);
				//9 Numeric   OR 9  [1 Alpha (A, B, D or Y only) + 8 Numeric]
				lok = ((len == 9) && (lnum || oneletterprefix));
				if (lok && oneletterprefix) {
					var str = 'ABDY';
					lok = (str.search(a1) > -1);
				}
				break;
			case 'CA' :
				//rwg 2008-06-10 lok = (la1 && len < 9);
				lok = (oneletterprefix && len == 8);
				break;
			case 'CO' :
				//rwg 2008-06-10 lok = ((la1 || la2) && len < 9) || (lnum && len == 9);
				lok = ((lnum) && (len <= 9) && (len > 1));
				if ((lok) && (len < 9)) {
					nZeroPad = (9 - len);
				}
				break;
			case 'CT' :
				//rwg 2008-06-10 lok = lnum && ((a2 >= 1) && (a2 <= 24));
				lok = ((len == 9) && (lnum) && ((a2 >= 1) && (a2 <= 24)));
				break;
			case 'DC' :
				lok = lnum && (len == 7) || (len == 9);
				break;
			case 'DE' :
				lok = (lnum && (len == 7) || (len == 5) || (len == 6));
				break;
			case 'FL' :
			// ADDED THAT FL LICENSE MUST BE 13 CHARS -NOT JUST LESS THAN 14
			// BROGOWSKI 04/13/2006 revised to use oneletterprefix and allow 12 rwg 2008-06-17
				lok = (oneletterprefix && ((len == 13) || (len == 12)));
				if ((lok) && (len == 12)) {
					nZeroPad = 1; //added rwg 2008-06-16
				}
				break;
			case 'GA' :
				//rwg 2008-06-10 lok = lnum && (len > 6 && len < 10);
				lok = lnum && (len > 6 && len < 10);
				if ((lok) && (len != 9)) { 
					nZeroPad = (9 - len);
				}
				break;
			case 'HI' :
				//rwg 2008-06-10 lok = lnum && len == 9;
				lok = ((len == 9) && (oneletterprefix) && (a1 == 'H'));
				break;
			case 'IA' :
				//rwg 2008-06-10 lok = len < 10;
				//DL# (9 character numeric or 9-character alpha-numeric with 4th and 5th character being alpha)				
				lok = (len == 9);
				if ((lok) && (!lnum)) {
					lok = (!isNaN(cLicNum.substring(0, 2))) && (isNaN(cLicNum.charAt(3)))  && (isNaN(cLicNum.charAt(4)))  && (!isNaN(cLicNum.substring(5)));
				}
				break;
			case 'ID' :
				lok = len == 9 && (lnum) || ((isNaN(a2)) && (!isNaN(cLicNum.substring(2, 6))) && (isNaN(cLicNum.charAt(len-1))));
				break;
			case 'IL' : 
				//revised rwg 2008-06-10
				lok = (oneletterprefix && (len == 12));
				break;
			case 'IN' :
				lok = (lnum && (len == 8 || len == 9 || len == 10));
				if ((lok) && (len < 10)) {
					nZeroPad = (10 - len); //rwg 2008-06-10
				}
				break;
			case 'KS' :
				// rwg 2008-06-10 lok = (l == 9 && (lnum || la1));
				lok = ((len == 9) && (lnum || oneletterprefix));
				if ((!lok) && (len == 6)) {
					for (var i = 0; i < 6; i++) {
				  		if (!isNaN(cLicNum.substring(i,i+2))) {
			    	 		lok = true;
			 	 		}
					}
				}
				break;
			case 'KY' :
				lok = ((len == 9) && (lnum || oneletterprefix));  //revised rwg 2008-06-10
				break;
			case 'LA' :
				//rwg 2008-06-10 lok = lnum && len < 10;
				lok = ((lnum) && (len <= 9) && (len >=7));
				if ((lok) && (len < 9)) {
					nZeroPad = (len - 9);
				}
				break;
			case 'MA' :
				lok = ((len == 9) && (lnum || oneletterprefix)); //revised rwg 2008-06-10
				break;
			case 'MD' :
				lok = (oneletterprefix && (len == 13));  //revised rwg 2008-06-10
				break;
			case 'ME' :
				lok = (lnum && (len == 7));
				break;
			case 'MI' :
				//rwg 2008-06-10 lok = la1 && len == 13;
				lok = oneletterprefix && (len == 13);
				if (lok) {
					lok = (cLicNum.charAt(2) < 7 && cLicNum.charAt(3) < 7);
		            if (lok) {
		               if ((Mid(cLicNum, 2, 3) >= 726) && (Mid(cLicNum, 2, 3) <= 750)) {
		                  lok = true;
		               } else if ((Mid(cLicNum, 2, 1) > 6) || (Mid(cLicNum, 3, 1) > 6) || (Mid(cLicNum, 4, 1) > 6)) {
		                  lok = false;
		               }
		            }
				}
				break;
			case 'MN' :
				lok = (oneletterprefix && (len === 13));  //revised rwg 2008-06-10
				break;
			case 'MO' :
				//not a complete format check
				//rwg 2008-06-10 lok = ((len >= 7) && (len <= 17));
				if (((len == 9) || (len == 10)) && (lnum)) {
					//9-10 Numeric                         OR
					lok = true;
				} else if (((len == 16) || (len == 17)) && (oneletterprefix)) {
					//16 ( 1 Alpha + 15 Numeric)    OR
					//17 (1 Alpha + 16 Numeric]     OR
					lok = true;
				} else if ((len >= 6) && (len <= 10) && isNaN(a1)) {
					//6-10 [1 Alpha + 5-9 Alpha/Numeric or Spaces]   OR
					lok = true;
				} else if ((len == 10) && (!isNaN(cLicNum.substring(0,8))) && (isNaN(cLicNum.charAt(len)))) {
					//10 [ 9 Numeric + 1 Alpha)     OR
					lok = true;
				} else if ((len == 17) && (isNaN(a1)) && (!isNaN(cLicNum.substring(1,14))) && (isNaN(cLicNum.charAt(len)))) {
					//17  [1 Alpha + 15 Numeric + 1 Alpha}
					lok = true;
				} else {
					lok = false;
				}
				break;
			case 'MS' :
				lok = lnum && (len == 9);
				break;
			case 'MT' :
				//rwg 2008-06-10 lok = (lnum && (len == 9)) || ((len >= 5) && (len <= 9));
				//And (isNaN(a1) && !isNaN(cLicNum.charAt(1) && IsNaN(cLicNum.charAt(2)))
				if ((lnum) && ((len == 9) || (len == 13))) {
					//9 Numeric     OR    13 Numeric        OR				
					lok = true;
				} else if ((len == 9) && (la1) && (!isNaN(cLicNum.charAt(1))) && (!isNaN(cLicNum.charAt(3))) && (!isNaN(cLicNum.charAt(3)))) {
					//9  [1 Alpha + 1 Numeric + 1 Alpha/Numeric + 2 Numeric + 3 Special* + 1 Numeric]
	               if (Mid(cLicNum, 6, 1) == ' ') {
	                  //* If 6th pos. is space, then pos.7-8 must be spaces.
	                  lok = (Mid(cLicNum, 7, 2) == '  ');
	               } else if (Mid(cLicNum, 6, 1) == '-') {
	                  //If 6th pos. is a hyphen, then pos. 7-8 must be Hyphens.
	                  lok = (Mid(cLicNum, 7, 2) == '--');
	               } else if (isNaN(Mid(cLicNum, 6, 1))) {
	                  //If 6th pos. is Alpha, then pos. 7-8 must be alpha, spaces or hyphens.
	                  lok = isNaN(Mid(cLicNum, 7, 1)) && isNaN(Mid(cLicNum, 8, 1));
	               } else if (isNaN(Mid(cLicNum, 7, 1))) {
	                  //If the 7th pos. is Alpha, then pos. 8 must be alpha, space or hyphen.
	                  lok = isNaN(Mid(cLicNum, 8, 1));
	               } else if (Mid(cLicNum, 7, 1) == ' ') {
	                  //If 7th pos. is space, then pos. 8 must be space or hyphen.
	                  lok = (Mid(cLicNum, 8, 1) == ' ') || (Mid(cLicNum, 8, 1) == '-');
	               } else if (Mid(cLicNum, 7, 1) == '-') {
	                  //If the 7th position is a hyphen, then position 8 must be alpha or a hyphen.
	                  lok = (isNaN(Mid(cLicNum, 8, 1)));
	               } else {
	                  lok = true;
	               }
				} else {
					lok = false;
				}
				break;
			case 'NC' :
				lok = lnum && (len < 13);
				break;
			case 'ND' :
				//revised rwg 2008-06-10
            	lok = ((len == 9) && (isNaN(Mid(cLicNum, 1, 1))) && (isNaN(Mid(cLicNum, 2, 1))) && (isNaN(Mid(cLicNum, 3, 1))) && (!isNaN(Mid(cLicNum, 4,6))));
				break;
			case 'NE' :
	            lok = (oneletterprefix && (len >= 4) && (len <= 9));  //revised rwg 2008-06-10
	            if (lok) {
					var str= 'ABCEGHV';
					lok = str.search(a1) > -1;
	            }
				break;
			case 'NH' :
				//revised rwg 2008-06-10
	            lok = ((ilen == 10) && (!isNaN(Mid(cLicNum, 1, 2))) && (isNaN(Mid(cLicNum, 3, 1))) && (isNaN(Mid(cLicNum, 4, 1))) && (isNaN(Mid(cLicNum, 5, 1))) && (!isNaN(Mid(cLicNum, 6,5))) && (cLicNum.charAt(len-1) != 0));
				break;
			case 'NJ' :
				lok = ((len == 15) && oneletterprefix); //revised rwg 2008-06-10
				break;
			case 'NM' :
				lok = lnum && ((len == 8) || (len == 9));
				if ((lok) && (len == 8)) {
					nZeroPad = 1;  //rwg 2008-06-10
				}
				break;
			case 'NV' :
				lok = (lnum && ((len == 9) || (len == 12) || (len == 10))) || (oneletterprefix && (a1 == 'X') && (len == 9));
				if ((lok) && (lnum) && (len == 9)) {
					nZeroPad = 1;  //rwg 2008-06-10
				}
				break;
			case 'NY' :
			    //not a complete format check
				//rwg 2008-06-10 lok = (lnum && (len == 9)) || ((len >= 16) && (len <= 21));
				lok = (lnum && (len == 9)) || ((len >= 17) && (len <= 21) && (oneletterprefix));
				break;
			case 'OH' :
				lok = la2 && (len == 8);
				break;
			case 'OK' :
				//rwg 2008-06-10 lok = lnum && (len < 10);
				//9 Numeric    OR    10 [1 Alpha + 9 Numeric]
				//NOTE:   Alpha character may not be I, O, Q or X.
				lok = (lnum && (len == 9)) || ((len == 10) && (oneletterprefix) && ('IOQX'.indexOf(a1) == -1));
				break;
			case 'OR' :
				lok = lnum && (len < 8);
				break;
			case 'PR' :
				lok = lnum && (len == 7);
				break; 
			case 'RI' :
				lok = (len == 7) && ((lnum) || (oneletterprefix && (a1 == 'V')));  //revised rwg 2008-06-10
				break;
			case 'SC' :
				lok = lnum && (len >= 7) && (len <= 9);
				if (lok && (len < 9)) {
					nZeroPad = (9 - len);  //rwg 2008-06-10
				}
				break;
			case 'SD' :
				//rwg 2008-06-10 lok = lnum && (((len >= 6) && (len <= 10)) || (len == 11));
				lok = lnum && ((len == 6) || (len == 8) || (len == 9));
				//6 Numeric    OR    8 Numeric    OR    9 Numeric
				break;
			case 'TN' :
				//rwg 2008-06-10 lok = lnum && ((len == 7) || (len == 8) || (len == 9));
				lok = lnum && ((len == 8) || (len == 9));
				break;
			case 'TX' :
				lok = lnum && len == 8;
				break;
			case 'UT' :
				//rwg 2008-06-10 lok = lnum && ((len >= 4) && (len <= 9));
				lok = lnum && ((len >= 4) && (len <= 10));
				break;
			case 'VA' :
				lok = (lnum && ((len == 9) || (len == 12))) || ((len == 9) && oneletterprefix && ('ABCDEFHKPRT'.indexOf(a1) > -1));  // added letter E per Ginger's email 9/10/2007 amg 
				break;
			case 'VI' :
				lok = ((len == 10) && oneletterprefix);  //revised rwg 2008-06-10
				break;
			case 'VT' :
				//rwg 2008-06-10 lok = (len == 8) && (lnum || !isNaN(cLicNum.substring(0, 6)) && (cLicNum.charAt(len-1) == 'A'));
				lok = (len == 8);
				if (lok) {
					//	8 Numeric     OR   7 Numeric + "A"
					//The MVR Book states:  Eight digits;  the last digit is a check digit which may be numeric or alpha 'A'.				
					lok = ((lnum) || ((!isNaN(cLicNum.substring(0, 6))) && (cLicNum.charAt(len-1) == 'A')));
				}
				break;
			case 'WA' :
			   //not a complete format check
				lok = len == 12 && !isNaN(cLicNum.charAt(8));
				break;
			case 'WI' :
				lok = (oneletterprefix && (len == 14));  //revised rwg 2008-06-10
				break;
			case 'WV' :
				lok = (len == 7);
				if (lok) {
					lok = (cLicNum.substring(0, 2) == 'XX' || cLicNum.substring(0, 2) == '1X') && !isNaN(cLicNum.substring(2))
					if (!lok) {
						var str = '01ABCDEFIOS'; //revised rwg 2008-06-10
						lok = str.search(a1) > -1 && !isNaN(cLicNum.substring(1));
					}
				}
				break;
			case 'WY' :
				lok = len == 10 && (lnum || ( cLicNum.charAt(6) == '-' && !isNaN(substring(cLicNum, 0, 6)) && !isNaN(substring(cLicNum, 7))));
				break;
			case 'IT' :
				// 2011/02/04 scs: Added logic to allow IT license numbers to have a length >= 4 for the guided app
				if(typeof(window['IsPublicApplication']) == 'undefined')
					IsPublicApplication = 'no';
				if(IsPublicApplication == 'yes') {
					lok = len > 3;						
				} else {
					lok = len > 5;					
				}
				if (lok) {
					//added rwg 2008-06-18
               		if ((cLicNum.indexOf('EXCLUDE') > -1) || (cLicNum.indexOf('LICENSE') > -1) || (cLicNum.indexOf('NOTLIC') > -1) || (cLicNum.indexOf('NOLIC') > -1)) {
						lok = false;
					} else if ((cLicNum.indexOf('LISTED') > -1) || (cLicNum.indexOf('SUPPLIED') > -1) || (cLicNum.indexOf('NEVER') > -1) || (cLicNum.indexOf('SUSPEND') > -1)) {
						lok = false;					
               		} else if ((cLicNum.indexOf('UNLIC') > -1) || (cLicNum.indexOf('UNKNOWN') > -1) || (cLicNum.indexOf('PERMIT') > -1) || (cLicNum.indexOf('INTERNAT') > -1)) {
						lok = false;					
	                } else if ((cLicNum.indexOf('DRIVE') > -1) || (cLicNum.indexOf('UNDER') > -1) || (cLicNum.indexOf('LIENSED') > -1) || (cLicNum.indexOf('PHOTO') > -1)) {
						lok = false;
    	            } else if ((cLicNum.indexOf('INCOMPLETE') > -1) || (cLicNum.indexOf('AGENT') > -1)) {
						lok = false;
					}
				}
				break;
			case 'LP' :
					//added cmu 2009-02-09 to validate LP = Arkansas Learner permit.
					lok = lnum && (len >= 7) && (len <= 9);
			    break;
			default :
				lok = false; 	
		}
	}
	if (nZeroPad > 0) {	
		//rwg 2008-06-10
		if (la1) {
			document.all(licnumfldname).value = cLicNum + RepeatString('0', nZeroPad);
		} else {
			document.all(licnumfldname).value = RepeatString('0', nZeroPad) + cLicNum;
		}
		alert('The license number entered was padded with ' + nZeroPad + ' zero(s) to conform with the current ' + cLicState + ' format.  Please verify the DL# before proceeding.');
	} else if (lok && (cLicNum != LicenseNum)) { //rwg 2008-06-11 
		document.all(licnumfldname).value = cLicNum; //rwg 2008-06-11 
    }
	if (!lok && len > 0) {
		return (false);
		}
	else {
		return (true);
	}
}

function chkLicense(fld) {
	//assumes this is either the State or Number and has a prefix and/or suffix
	var lok;
	var LicenseState;
	var LicenseNum;
	var prefix = '';
	var suffix = '';
	var n;
	var x;
	var y;
	n = fld.name;
	x = n.indexOf('State');
	if (x > 0) {
		y = x + 'State'.length + 1;
	}
	else {
		x = n.indexOf('Number');
		y = x + 'Number'.length + 1;
	}
	prefix = Mid(n,0,x);
	suffix = Mid(n,y,n.length);
	LicenseState = document.all(prefix + 'State' + suffix).value;
	var licnumfldname = prefix + 'Number' + suffix; //rwg 2008-06-10	
	LicenseNum = document.all(licnumfldname).value;  //rwg 2008-06-10
	if (LicenseState.length > 0 && LicenseNum.length > 0) {
		lok = ValidLicense(LicenseState,LicenseNum,licnumfldname);
		if (!lok) {
			alert('License number is not valid.');
			document.all(prefix + 'Number' + suffix).focus();
		}
	}
}


