<!--#include virtual="/_appDB/db_connection.asp" -->
<!--#include virtual="/_appDB/functions.asp" -->
<!--#include virtual="/schools/scripts/admin_functions.asp" -->
<!--#include virtual="/schools/scripts/enc_function.asp" -->
<!--#include virtual="/schools/scripts/adm_inst.asp" -->
<%
IF (checkPermission("drivers")="FAILED") THEN
	response.write("You do not have permission to view this page")
	response.end
END IF
%>

<script type="text/javascript" src="/schools/scripts/drivers/tst.js"></script>
<script type="text/javascript">
	$(document).ready(function(){
		$('#testDATE').datepicker( {dateFormat: 'dd-mm-yy'});
		$('#testDATENEXTDUE').datepicker( {dateFormat: 'dd-mm-yy'});
	});
</script>
<%
 response.buffer = true
 Function Qpad(tval)
	IF len(tval)=1 THEN
		Qpad="0"&tval
	ELSE
		Qpad=tval
	END IF
 End Function
 Function activeDriverCount(tcID)
	IF CStr(tcID) <> "" AND IsNumeric(tcID) THEN
		SET rsADC=dbCBK.Execute("SELECT COUNT(*) As adc from cbk_drivers WHERE `active`='1' limit 1")
		IF rsADC.EOF THEN
			activeDriverCount="0"
		END IF	
		DO WHILE NOT rsADC.EOF
			activeDriverCount=rsADC("adc")
		rsADC.MoveNext
		loop
		rsADC.Close
		SET rsADC = Nothing
	ELSE
		activeDriverCount="0"
	END IF
 End Function
 curdc=CInt(activeDriverCount("1"))

 StrError="None"

	IF REQUEST("add")="driver.account" THEN
		str_school_id=str_u_id
		acc_active=sanitizeRequest("active","form")
			IF acc_active="1" THEN
				IF curdc+1 > strDRVLMT THEN
					StrError = "You have reached your active driver limit"
				END IF
			END IF
		drivername=sanitizeRequest("driverNAME","form")
			IF LEN(drivername) < 2 THEN StrError = "The Drivers Name Is A Required Field" END IF
		driverEMAIL=sanitizeRequest("driverEMAIL","form")
		
		IF LEN(driverEMAIL) < 6 OR LEN(driverEMAIL) > 150 OR NOT(Instr(driverEMAIL,"@") > 0) OR NOT(Instr(driverEMAIL,".") > 0) THEN 
			StrError = "The Email Address Appears Invalid"
		ELSE
			driverEMAIL=lcase(driverEMAIL)
		END IF
		IF (checkDrvEmailExists(driverEMAIL,str_company_id) = "EXISTS") THEN
			StrError = "The Email Address Is Already In Use"
		END IF
		driverMOBILE=sanitizeRequest("driverMOBILE","form")
				IF driverMOBILE <> "" AND NOT(IsNull(driverMOBILE)) THEN
					driverMOBILE=sanitizeN(driverMOBILE)
				END IF
				IF LEN(driverMOBILE) < 11 OR LEN(driverMOBILE) > 14 THEN StrError = "The Mobile Number Appears Invalid" END IF
		vehicleTYPE=""
		vehicleMAKE=""
		vehicleMODEL=""
		vehicleREG=""

		userNAME=sanitizeRequest("userNAME","form")
		acpwd=sanitizeRequest("acp","form")
		IF acpwd <> "" AND NOT(IsNull(acpwd)) THEN
			ecpwd=DRVEncryptAndEncode(acpwd)
		ELSE
			ecpwd=""
		END IF
		
		
		address1=sanitizeRequest("address1","form")
			IF LEN(address1) > 100 THEN StrError = "Address 1 is too long" END IF
		address2=sanitizeRequest("address2","form")
			IF LEN(address2) > 100 THEN StrError = "Address 2 is too long" END IF
		address3=sanitizeRequest("address3","form")
			IF LEN(address3) > 100 THEN StrError = "Address 3 is too long" END IF
		countystate=sanitizeRequest("countystate","form")
			IF LEN(countystate) > 50 THEN StrError = "The County / State Is too long" END IF
		postcode=sanitizeRequest("postcode","form")
			IF LEN(postcode) > 50 THEN StrError = "The Postcode Is too long" & VbCrLf & "If you do not have a postcode enter 000" END IF
		

		driverTEL=sanitizeContent("driverTEL","form")
		IF driverTEL <> "" AND NOT(IsNull(driverTEL)) THEN
			driverTEL=sanitizeN(driverTEL)
		END IF
		
		driverNOTES=sanitizeContent("driverNOTES","form")

		ht_type=sanitizeRequest("testT","form")
		ht_DATE_posted=sanitizeRequest("testDATE","form")
			IF ht_DATE_posted <> "" AND (IsDate(ht_DATE_posted)) THEN
			'ok	
				f=formatDateTime(CDate(ht_DATE_posted),2)
				'format for mysql
				ht_DATE=year(f)&"-"&Qpad(month(f))&"-"&Qpad(day(f))&" 23:59:59"
				ht_DATE_SQL1=", `healthtestDATE`"
				ht_DATE_SQL2=", '"&ht_DATE&"'"
			ELSE
				ht_DATE=""
				ht_DATE_SQL1=""
				ht_DATE_SQL2=""
			END IF
		
		ht_RESULT=sanitizeRequest("testRESULT","form")
		ht_CODE=sanitizeRequest("testCODE","form")
		ht_DATENEXTDUE_posted=sanitizeRequest("testdateNEXTDUE","form")
			IF ht_DATENEXTDUE_posted <> "" AND (IsDate(ht_DATENEXTDUE_posted)) THEN
			'ok	
				t=formatDateTime(CDate(ht_DATENEXTDUE_posted),2)
				'format for mysql
				ht_DATENEXTDUE=year(t)&"-"&Qpad(month(t))&"-"&Qpad(day(t))&" 23:59:59"
				ht_DATENEXTDUE_SQL1=", `healthtestNEXTDATEDUE`"
				ht_DATENEXTDUE_SQL2=", '"&ht_DATENEXTDUE&"'"
			ELSE
				ht_DATENEXTDUE=""
				ht_DATENEXTDUE_SQL1=""
				ht_DATENEXTDUE_SQL2=""
			END IF



		IF StrError <> "None" THEN
			'print the errors
		ELSE	
			'add the account
			aSQL = "INSERT INTO cbk_drivers "
			aSQL = aSQL & "(`schoolID`,`driverNAME`,`driverUSERNAME`,`driverPWD`,`driverEMAIL`,`driverMOBILE`,`driverADDRESS1`,`driverADDRESS2`,`driverADDRESS3`,`driverCOUNTYSTATE`,`driverPOSTCODE`,`driverTEL`,`vehicleTYPE`,`vehicleMAKE`,`vehicleMODEL`,`vehicleREG`,`active`,`driverNOTES`,`dateADDED`,`healthtestT`"&ht_DATE_SQL1&",`healthtestRESULT`,`healthtestBARCODE`"&ht_DATENEXTDUE_SQL1&")"
			aSQL = aSQL & " VALUES"	
			aSQL = aSQL & "('"&str_school_id&"', '"&driverNAME&"', '"&userNAME&"', '"&ecpwd&"', '"&driverEMAIL&"', '"&driverMOBILE&"', '"&address1&"', '"&address2&"', '"&address3&"', '"&countystate&"', '"&postcode&"', '"&driverTEL&"', '"&vehicleTYPE&"', '"&vehicleMAKE&"', '"&vehicleMODEL&"', '"&vehicleREG&"', '1', '"&driverNOTES&"', '"&year(now())&"-"&month(now())&"-"&day(now())&"', '"&ht_type&"'"&ht_DATE_SQL2&",'"&ht_RESULT&"','"&ht_CODE&"'"&ht_DATENEXTDUE_SQL2&")"	
			dbCBK.Execute(aSQL)
			pfSQL = "SELECT @@IDENTITY AS 'Identity'"
			        Set rsSelectResultpf = dbCBK.Execute( pfSQL )
			        If rsSelectResultpf.EOF Then
			        	StrError = "An error occurred, please try again" & vbCrLf
			        Else
			          StrError = "None"
					  nDID=rsSelectResultpf(0).value
			          response.redirect("/schools/control-panel/?go=drivers&drivers=view&view="&nDID&"&notify=New+driver+added+successfuly&nact=success")
			        End If	
					
		END IF




	END IF	

	Function checkDrvEmailExists(str_email,str_cid)
	IF str_email <> "" THEN
			str_email=trim(str_email)
		SET rsEChk = dbCBK.Execute("SELECT `driverEMAIL` FROM cbk_drivers WHERE `driverEMAIL` = '"&str_email&"' limit 1")
		IF rsEChk.EOF THEN 
			checkDrvEmailExists = "NOTFOUND"
		ELSE	
			checkDrvEmailExists = "EXISTS"
		END IF
		rsEChk.Close
		SET rsEChk = Nothing
	ELSE
		checkDrvEmailExists = "FAILED"
	END IF
	End Function
	
	Function sanitizeN(requestVariable) 
        Set regEx = New RegExp 
        regEx.Global = true 
        regEx.Pattern = "[^0-9]"
        sanitizeN = regEx.Replace(requestVariable, "")
	End Function
	

%>










<%IF StrError <> "None" THEN%>
	<div class="error"><%=StrError%></div>
<%END IF%>
<form action="#" method="post" name="addForm">
<input type="hidden" name="add" value="driver.account" />
<div class="apptoast">
	<img class="epointer" src="/app/img/erpointer.png" alt="err" />
	<div class="toast"></div>
</div>
<div class="maincontent" id="maincontent">
	<div class="col1">
		<div class="col1third">
			<div class="bookingpanel" id="bookingpanel1">
				<h2>Add Driver</h2>
				<span>Enter the details below.</span>
				<div class="clear s20"></div>
				<table cellpadding="0" cellspacing="0">
					<tr>
						<td style="text-align:left;"><div class="adminbutton"><a href="/schools/control-panel/?go=drivers" style="width:99%;"><span class="adm_go_back">cancel</span></a></div></td>
					</tr>
				</table>
				<div class="clear"></div>
			</div>
		</div>
		<div class="col1third">
			<div class="bookingpanel" id="bookingpanel3">
				<h2>Important</h2>
				<span>Click the button to add a new driver.</span>
				<div class="clear s20"></div>
				<table cellpadding="0" cellspacing="0" style="width:100%;">
					<tr>
						<td style="text-align:left;"><div class="adminbutton savechanges"><a href="#" style="width:99%;"><span class="adm_save">save details</span></a></div></td>
					</tr>
				</table>
				<div class="clear s20"></div>
			</div>
		</div>
		<div class="clear"></div>
	</div>


	<div class="clear s20"></div>
		
<div class="col3">	
		
		<div style="width:100%;" class="dataTable customerdetails">
			<table cellpadding="0" cellspacing="0" style="height:400px;width:98%;" class="formfields">
				<tr>
					<th colspan="2">Drivers Details</th>
				</tr>
				<tr>
					<td style="width:20%;">Name</td>
					<td><input type="text" name="driverNAME" id="driverNAME" value="<%=driverNAME%>" style="width:81%;min-width:200px;" placeholder="Joe Bloggs" autocomplete="off" /></td>
				</tr>
				<tr>
					<td style="width:20%;">Mobile #.</td>
					<td><input type="text" name="driverMOBILE" id="driverMOBILE" value="<%=driverMOBILE%>" style="width:81%;min-width:200px;" placeholder="Mobile #" autocomplete="off" /></td>
				</tr>
				<tr>
					<td>Email</td>
					<td>
						<input type="text" name="driverEMAIL" id="driverEMAIL" value="<%=driverEMAIL%>" style="width:81%;min-width:200px;" placeholder="email (ie. joebloggs@domain.com)" autocomplete="off" />
					</td>
				</tr>
				<tr>
					<th colspan="2">Driver App Login Details (*optional)</th>
				</tr>
				<tr>
					<td>User Name</td>
					<td>
						<input type="text" name="userNAME" id="userNAME" value="<%=userNAME%>" style="width:40%;min-width:200px;" autocomplete="off" />
					</td>
				</tr>
				<tr>
					<td>Account&nbsp;Password</td>
					<td>
						<input type="text" name="acp" id="acp" value="<%=acpwd%>" style="width:40%;min-width:100px;float:left;" autocomplete="off" />				     
						<div class="adminbutton genpwd" style="float:left;width:40%;margin-left:1%;"><a href="#" style="width:99%;" id="gpbutton"><span class="adm_go_back">suggest&nbsp;password</span></a></div>
					</td>
				</tr>
				<tr>
					<th colspan="2">Additional Details (*optional)</th>
				</tr>
				<tr>
					<td>Home Address</td>
					<td>
						<div style="padding:0px 0px 4px 0px;">
							<input type="text" name="address1" id="address1" value="<%=address1%>" style="width:81%;min-width:200px;" placeholder="House No. or Name" autocomplete="off" />
						</div>
						<div style="padding:4px 0px;">
							<input type="text" name="address2" id="address2" value="<%=address2%>" style="width:81%;min-width:200px;" placeholder="Street / Road Name" autocomplete="off" />
						</div>
						<div style="padding:4px 0px;">
							<input type="text" name="address3" id="address3" value="<%=address3%>" style="width:81%;min-width:200px;" placeholder="Town / City" autocomplete="off" />
						</div>
						<div style="padding:4px 0px;"> 
							<input type="text" name="countystate" id="countystate" value="<%=countystate%>" style="width:81%;min-width:200px;<%IF ccn="US" THEN%>display:none;<%END IF%>" placeholder="County / State"  autocomplete="off"/>
						</div>
						<div style="padding:4px 0px;">
							<input type="text" name="postcode" id="postcode" value="<%=postcode%>" style="width:40%;min-width:200px;" placeholder="Postcode" autocomplete="off" />
						</div>
					</td>
				</tr>	
				<tr>
					<td style="width:20%;">Telephone #.</td>
					<td><input type="text" name="driverTEL" id="driverTEL" value="<%=driverTEL%>" style="width:81%;min-width:200px;" placeholder="Telephone #" /></td>
				</tr>
				<tr>
					<td>Driver&nbsp;Notes (*office&nbsp;use&nbsp;only)</td>
					<td>
						<textarea name="driverNOTES" id="driverNOTES" style="width:81%; min-width:200px; height:200px;"><%=driverNOTES%></textarea>
					</td>
				</tr>
				<tr style="border-top: 1px solid yellow;border-right: 1px solid yellow;border-left: 1px solid yellow;">
					<th colspan="2">Safety Testing (*optional)</th>
				</tr>
				<tr style="border-right: 1px solid yellow;border-left: 1px solid yellow;">
					<td>Test Type</td>
					<td>
						<select name="testT" id="testT" style="width:40%;min-width:200px;">
							<option value="covid19">Covid-19</option>
						</select>
					</td>
				</tr>
				<tr style="border-right: 1px solid yellow;border-left: 1px solid yellow;">
					<td>Test Date</td>
					<td>
						<input type="text" name="testDATE" id="testDATE" style="width:40%;min-width:100px;float:left;" autocomplete="off" />				     
					</td>
				</tr>
				<tr style="border-right: 1px solid yellow;border-left: 1px solid yellow;">
					<td>Test Result</td>
					<td>
						<select name="testRESULT" id="testRESULT" style="width:40%;min-width:200px;">
							<option value="--" selected="selected">Please Choose</option>
							<option value="pending">Pending</option>
							<option value="negative">Negative</option>
							<option value="positive">Positive</option>
							<option value="inconclusive">Inconclusive</option>
						</select>
					</td>
				</tr>
				<tr style="border-right: 1px solid yellow;border-left: 1px solid yellow;">
					<td>Test Code</td>
					<td>
						<input type="text" name="testCODE" id="testCODE" style="width:40%;min-width:100px;float:left;" autocomplete="off" />				     
					</td>
				</tr>
				<tr style="border-right: 1px solid yellow;border-bottom: 1px solid yellow;border-left: 1px solid yellow;">
					<td>Next Test Due</td>
					<td>
						<input type="text" name="testDATENEXTDUE" id="testDATENEXTDUE" style="width:40%;min-width:100px;float:left;" autocomplete="off" />				     
					</td>
				</tr>

			</table>
		</div>
		<div class="clear s20"></div>
	</div>
	<div class="clear s20"></div>
	<div class="col1">
		<div class="col1third">
			<div class="bookingpanel" id="bookingpanel1">
				<h2>Add Driver </h2>
				<span>Enter the details above.</span>
				<div class="clear s20"></div>
				<table cellpadding="0" cellspacing="0">
					<tr>
						<td style="text-align:left;"><div class="adminbutton"><a href="/schools/control-panel/?go=drivers" style="width:99%;"><span class="adm_go_back">cancel</span></a></div></td>
					</tr>
				</table>
				<div class="clear"></div>
			</div>
		</div>
		<div class="col1third">
			<div class="bookingpanel" id="bookingpanel3">
				<h2>Important</h2>
				<span>Click the button to add a new driver.</span>
				<div class="clear s20"></div>
				<table cellpadding="0" cellspacing="0" style="width:100%;">
					<tr>
						<td style="text-align:left;"><div class="adminbutton savechanges"><a href="#" style="width:99%;"><span class="adm_save">save details</span></a></div></td>
					</tr>
				</table>
				<div class="clear s20"></div>
			</div>
		</div>
		<div class="clear"></div>
	</div>
</div>
</form>
<script type="text/javascript">
 $('.genpwd a').click(function(e){
	e.preventDefault();
	var np = '<%=generatePassword(8)%>';
	$('#gpbutton').html('<img src="/schools/img/blue_load.gif" alt="" />');
	setTimeout(function(){$('#acp').val(np);$('#gpbutton').html('<span class="adm_go_back">suggest&nbsp;password</span>');},1500);
	
 });

  function resForm(){
		$('.savechanges a').html('<span class="adm_save">create account</span>');
	}
	$('.savechanges a').click(function(e) {
			e.preventDefault();
			$('.savechanges a').html('<img src="/schools/img/blue_load.gif" alt="" />');
			var preg = /^[0-9_\s]{6,}$/g;var mreg = /^[0-9_\s]{6,}$/g;var ereg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			if($('#driverNAME').val() == ''){showAppToast('The drivers name is a required field','driverNAME'); resForm(); return false;}			
			if($('#driverMOBILE').val() == ''){showAppToast('The mobile number is a required field','driverMOBILE'); resForm();return false;}
			if($('#driverMOBILE').val().length < 11){showAppToast('The mobile number appears invalid','driverMOBILE'); resForm();return false;}
			if(mreg.test($('#driverMOBILE').val()) == false){showAppToast('The mobile number should consist of numbers only','driverMOBILE');resForm();return false;}
			if($('#driverEMAIL').val() == ''){showAppToast('The email address is a required field','driverEMAIL'); resForm();return false;}
			if(ereg.test($('#driverEMAIL').val()) == false){showAppToast('The email address appears invalid','driverEMAIL');resForm();return false;}
			if($('#userNAME').val().length > 0 || $('#acp').val().length > 0){
				if($('#userNAME').val().length < 6){showAppToast('The username must be at least 6 characters','userNAME');resForm();return false;}
			}
			if($('#acp').val().length > 0 || $('#userNAME').val().length > 0){
				if($('#acp').val().length < 6 || $('#acp').val().length > 50){showAppToast('The password appears invalid','acp');resForm();return false;}
			}
			
			setTimeout(function(){document.forms.addForm.submit();},1500);
	});
</script>
