<cfparam name="form_submitted" default="0">
<cfparam name="messages" default="">
<!--- <cfparam name="form.human_yes" default="0">
<cfparam name="form.human_no" default="0"> --->
<cfparam name="highlight_challenge" default="0">

<cfif form_submitted EQ 0>
	
	<cfparam name="values.Password" default="">
    <cfparam name="values.Password_verify" default="">
    <cfparam name="values.EmailAddress" default="">
    <cfparam name="values.EmailAddress_verify" default="">
    <cfparam name="values.UserContactNumber" default="">
    <cfparam name="values.UserContactNumber_verify" default="">
    
<cfelseif form_submitted EQ 1>

	<cfparam name="form.Password" default="">
	<cfparam name="form.Password_verify" default="">
	<cfparam name="form.EmailAddress" default="">
	<cfparam name="form.EmailAddress_verify" default="">
	<cfparam name="form.UserContactNumber" default="">
	<cfparam name="form.UserContactNumber_verify" default="">

	<cfset values.Password = trim(form.Password)>
	<cfset values.Password_verify = trim(form.Password_verify)>
	<cfset values.EmailAddress = trim(form.EmailAddress)>
	<cfset values.EmailAddress_verify = trim(form.EmailAddress_verify)>
	<cfset values.UserContactNumber = trim(form.UserContactNumber)>
	<cfset values.UserContactNumber_verify = trim(form.UserContactNumber_verify)>
	
<cfelse>
	
	Problem!<cfabort>

</cfif>

<cfif form_submitted EQ 1>

    <!--- <cfdump var=#values#> --->
	
	<cfif REFindNocase("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.(([a-z]{2,3})|(aero|coop|info|museum|name))$", values.EmailAddress) EQ 0>
		<cfset messages = listappend(messages, "invalid email address entered")>
	<cfelseif values.EmailAddress NEQ values.EmailAddress>
		<cfset messages = listappend(messages, "email address values do not match")>
	<cfelse>
		<CFQUERY name="check_dupe_email" datasource="#application.datasource#" dbtype="ODBC">
			SELECT		UserID
			FROM		Users
			WHERE		UserEmailAddress='#values.EmailAddress#'
                            AND 
                        UserIsEmailVerified = 1
                            AND 
                        UserIsContactVerified = 1
		</CFQUERY>
		<cfif check_dupe_email.recordcount GT 0>
			<cfset messages = listappend(messages, "that email address already has a Payfrit account")>
		</cfif>
	</cfif>

	<cfmodule credential = "#values.UserContactNumber#" template = "modules/parse_phnum.cfm">

	<cfset values.UserContactNumber = parsed_phnum>

	<cfmodule credential = "#values.UserContactNumber_verify#" template = "modules/parse_phnum.cfm">

	<cfset values.UserContactNumber_verify = parsed_phnum>

	<cfif len(values.UserContactNumber_verify) LT 10 OR  len(values.UserContactNumber_verify) GT 11>
	    <cfset messages = listappend(messages, "Mobile number must be a 10-digit USA number")>
	<cfelseif values.UserContactNumber_verify NEQ values.UserContactNumber>
    	<cfset messages = listappend(messages, "Values for contact number do not match")>
	<cfelse>
		<CFQUERY name="check_dupe_ContactNumber" datasource="#application.datasource#" dbtype="ODBC">
			SELECT		UserID
			FROM		Users
			WHERE		UserContactNumber='#values.UserContactNumber#'
							AND 
						UserIsEmailVerified = 1
							AND 
						UserIsContactVerified = 1
		</CFQUERY>

		<cfif check_dupe_ContactNumber.recordcount NEQ 0>
			<cfset messages = listappend(messages, "that mobile number already has a Payfrit account")>
		</cfif>
	</cfif>
	
	<cfif values.password EQ "">
		<cfset messages = listappend(messages, "password is required")>
	<cfelseif values.password NEQ values.password_verify>
		<cfset messages = listappend(messages, "password values do not match")>
		<cfset values.password = "">
		<cfset values.password_verify = "">
	</cfif>

</cfif>

<cfif form_submitted EQ 0 OR messages GT "">

    <br><h2>Register for Payfrit:</h2>
		
	<cfif messages GT "">
	
		<CFLOOP INDEX="ListElement" LIST=#messages#> 
		<CFOUTPUT><div align="center"><font color="FF0000"><h2>#ListElement#</h2></font></div></CFOUTPUT>
		</CFLOOP><br><br>
		
		</cfif>
	
	<cfoutput>
	
	<div align="center">
	
	<form action="#request.cgiPath#" method="post" name="register" id="register">
	
		<table border="0" cellspacing="2" cellpadding="2">
			<tr>
				<td align="right"><cfinclude template="#application.wwwrootprefix#includes/required_indicator.cfm">Email:</td>
				<td><input type="text" name="EmailAddress" id="EmailAddress" size="25" maxlength="100" tabindex="1" value=#values.EmailAddress#></td>
			</tr>
			<tr>
				<td align="right"><cfinclude template="#application.wwwrootprefix#includes/required_indicator.cfm">Email verify:</td>
				<td><input type="text" name="EmailAddress_verify" id="EmailAddress_verify" size="25" maxlength="100" tabindex="2" value=#values.EmailAddress_verify#></td>
			</tr>            
			<tr>
				<td align="right"><cfinclude template="#application.wwwrootprefix#includes/required_indicator.cfm">Mobile number:</td>
				<td><input type="text" name="UserContactNumber" id="UserContactNumber" size="25" maxlength="100" tabindex="3" value=#values.UserContactNumber#></td>
			</tr>            
			<tr>
				<td align="right"><cfinclude template="#application.wwwrootprefix#includes/required_indicator.cfm">Mobile verify:</td>
				<td><input type="text" name="UserContactNumber_verify" id="UserContactNumber_verify" size="25" maxlength="100" tabindex="4" value=#values.UserContactNumber_verify#></td>
			</tr>
			<tr>
				<td align="right"><cfinclude template="#application.wwwrootprefix#includes/required_indicator.cfm">Password:</td>
				<td><input type="password" name="Password" id="Password" size="12" maxlength="30" tabindex="5" value=#values.Password#></td>
			</tr>
			<tr>
				<td align="right"><cfinclude template="#application.wwwrootprefix#includes/required_indicator.cfm">Password verify:</td>
				<td><input type="password" name="Password_verify" id="Password_verify" size="12" maxlength="30" tabindex="6" value=#values.Password_verify#></td>
			</tr>
			<tr>
				<td>&nbsp;</td>
				<td><input type="submit" tabindex="10" value="register" tabindex="7"></td>
			</tr>
		</table>
		
		<input type="hidden" name="form_submitted" value="1">
	
	</form>
	
	</div>
						
	</cfoutput>
	
<cfelseif form_submitted EQ 1>

	<cfset UUID_to_insert = CreateUUID()>

	<cfset customer_OPT_confirm = round(rand()*899996+100000)>

	<cfset isMessageSent = application.twilioObj.sendSMS(recipientNumber=#values.UserContactNumber#, messageBody = "Payfrit six-digit code is #customer_OPT_confirm#, please input it into your registration page, or tap here: https://payfr.it/confirm_mobile.cfm")>

	<cfquery name="insert_account" datasource="#application.datasource#" dbtype="ODBC">
	    INSERT INTO		Users
						(
						 UserEmailAddress,
						 UserPassword,
                         UserProfileImage,
                         UserContactNumber,
                         UserIsActive,
                         UserIsContactVerified,
                         UserDeviceToken,
                         UserPlatform,
                         UserAddedOn,
                         UserUUID,
						 UserMobileVerifyCode
						)
		VALUES
						(
						 '#values.EmailAddress#',
						 '#hash(values.Password)#',
                         '',
                         '#values.UserContactNumber#',
                          0,
                          0,
                          '',
                         'web signup register.cfm',
                          #createODBCDateTime(now())#,
                         '#UUID_to_insert#',
						 '#customer_OPT_confirm#'
						 )
	</cfquery>

    <cfset link_to_build = "https://"&cgi.server_name&application.wwwrootprefix&"confirm_email.cfm?UUID="&UUID_to_insert>
	
	<cfmodule template="#application.wwwrootprefix#modules/notifier.cfm"
		domain="box.payfrit.com"
		from_email="admin@admin.com"
		to_email="#values.EmailAddress#"
		subject="new registration"
		email_body="someone just registered"
		>
		
	<cfset this_email_body = "Thank you for registering for Payfrit!<br>Click this link to confirm your email:<br><br>
	<br>
	<cfoutput><a href='#link_to_build#'>click here to confirm</a></cfoutput><br>
	<br>
	if that doesn't work you can try to copy and paste this link into your browser:<br>
	<br>
	<cfoutput>#link_to_build#<br>
	<br>
	if that doesn't work then <a href='admin@payfrit.com'>email</a> us and we will figure out how to fix it.<br><br><br>
	regards,<br>
	<br>
	Payfrit<br>
	<cfoutput>"&cgi.server_name&"</cfoutput><br>">
	
	<cfmodule template="#application.wwwrootprefix#modules/notifier.cfm"
		domain="box.payfrit.com"
		from_email="admin@payfrit.com"
		to_email="#values.EmailAddress#"
		subject="Payfrit - please confirm your email address"
		email_body="#this_email_body#"
		>
	
	<br>

	<cfoutput>now let's validate your mobile number!<br><br>

	<cfmodule credential = "#values.UserContactNumber#" template = "modules/format_phnum.cfm">

	we just sent a six-digit code to #formatted_phnum#, please input that code here:<br><br>

	<script language="JavaScript">
		function submitformconfirmmobile()
		{
		document.myformconfirmmobile.submit();
		}
	</script>
	
	<form action="confirm_mobile.cfm" method="post" name="myformconfirmmobile" id="myformconfirmmobile" style="display:inline;">
	
		<input type="text" size="5" maxlength="6" name="OTP">&nbsp;<a href="javascript: submitformconfirmmobile()">confirm mobile</a>
		
		<input type="hidden" name="UUID" value="#UUID_to_insert#">
		<input type="hidden" name="mode" value="confirm">
	
	</form><br><br>
	
	in addition we sent you an email with a link to click on to confirm your email, after you submit your six-digit code above<br><br>thanks.<br></cfoutput>

</cfif>