<cfparam name="form.register_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.register_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.ContactNumber" default="">
    <cfparam name="values.ContactNumber_verify" default="">
    <cfparam name="values.UserReferralPromoCode" default="">
    
<cfelseif form.register_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.ContactNumber" default="">
	<cfparam name="form.ContactNumber_verify" default="">
	<cfparam name="form.UserReferralPromoCode" 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.ContactNumber = trim(form.ContactNumber)>
	<cfset values.ContactNumber_verify = trim(form.ContactNumber_verify)>
	<cfset values.UserReferralPromoCode = trim(form.UserReferralPromoCode)>
	
<cfelse>
	
	Problem!<cfabort>

</cfif>

<cfif register_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#">
			SELECT		ID
			FROM		Users
			WHERE		EmailAddress='#values.EmailAddress#'
                            AND 
                        IsEmailVerified = 1
                            AND 
                        IsContactVerified > 0
		</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.ContactNumber#" template = "#application.wwwrootprefix#modules/parse_phnum.cfm">

	<cfset values.ContactNumber = parsed_phnum>

	<cfmodule credential = "#values.ContactNumber_verify#" template = "#application.wwwrootprefix#modules/parse_phnum.cfm">

	<cfset values.ContactNumber_verify = parsed_phnum>

	<cfif len(values.ContactNumber_verify) LT 10 OR  len(values.ContactNumber_verify) GT 11>
	    <cfset messages = listappend(messages, "Mobile number must be a 10-digit USA number")>
	<cfelseif values.ContactNumber_verify NEQ values.ContactNumber>
    	<cfset messages = listappend(messages, "Values for contact number do not match")>
	<cfelse>
		<CFQUERY name="check_dupe_ContactNumber" datasource="#application.datasource#">
			SELECT		ID
			FROM		Users
			WHERE		ContactNumber='#values.ContactNumber#'
							AND 
						IsEmailVerified = 1
							AND 
						IsContactVerified > 0
		</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 (len(values.UserReferralPromoCode) NEQ 7 AND len(values.UserReferralPromoCode) NEQ 0) OR isnumeric(values.UserReferralPromoCode) eq 0>
		<cfset messages = listappend(messages, "valid referral codes should be a 7-digit number")>
		<cfset values.UserReferralPromoCode = "">
	</cfif>

</cfif>

<cfif register_form_submitted EQ 0 and form.mode eq "manual_confirm">
		
	<cfquery name="manually_confirm_cell" datasource="#application.datasource#">
	    UPDATE 			Users
		SET				IsContactVerified = 2
		WHERE			UUID = '#form.UUID#'
	</cfquery>
	
	<cfquery name="check_newuser_info" datasource="#application.datasource#">
	    SELECT			IsEmailVerified, EmailAddress, ContactNumber
		FROM 			Users
		WHERE			UUID = '#form.UUID#'
	</cfquery>
	
	<cfif check_newuser_info.IsEmailVerified eq 1>
		Your cell number has now been temporarily confirmed<br><br>
		
		We may follow up with a manual text to verify but in the meantime, go order some food!
	<cfelse>
		Your cell number has been temporarily confirmed<br><br>please check your email (<cfoutput>#check_newuser_info.EmailAddress#</cfoutput>) to confirm your email then you're good to go!<br><br>
	</cfif>
	
	<cfmodule template="#application.wwwrootprefix#modules/notifier.cfm"
		domain="box.payfrit.com"
		from_email="admin@payfrit.com"
		to_email="admin@payfrit.com"
		subject="manually confirm a cell phone"
		email_body="please manually text the number in order to verify their cell - #check_newuser_info.ContactNumber#"
		>

<cfelseif register_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="15" 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="15" 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="ContactNumber" id="ContactNumber" size="10" maxlength="100" tabindex="3" value=#values.ContactNumber#></td>
			</tr>            
			<tr>
				<td align="right"><cfinclude template="#application.wwwrootprefix#includes/required_indicator.cfm">Mobile verify:</td>
				<td><input type="text" name="ContactNumber_verify" id="ContactNumber_verify" size="10" maxlength="100" tabindex="4" value=#values.ContactNumber_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="6" 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="6" maxlength="30" tabindex="6" value=#values.Password_verify#></td>
			</tr>
			<tr>
				<td align="right"><cfinclude template="#application.wwwrootprefix#includes/not_required_indicator.cfm">Referral code?</td>
				<td><input type="UserReferralPromoCode" name="UserReferralPromoCode" id="UserReferralPromoCode" size="6" maxlength="30" tabindex="7" value=#values.UserReferralPromoCode#></td>
			</tr>
			<tr>
				<td>&nbsp;</td>
				<td><input type="submit" tabindex="10" value="register" tabindex="8"></td>
			</tr>
		</table>
		
		<input type="hidden" name="mode" value="register">
		<input type="hidden" name="register_form_submitted" value="1">
	
	</form>
	
	</div>
						
	</cfoutput>
	
<cfelseif register_form_submitted EQ 1>

	<cfset UUID_to_insert = CreateUUID()>
	
	<cfmodule ContactNumber = "#values.ContactNumber#" template = "#application.wwwrootprefix#modules/send_reg_text.cfm">

	<!--- <cfset isMessageSent = application.twilioObj.sendSMS(recipientNumber=#values.ContactNumber#, 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#">
	    INSERT INTO		Users
						(
						 EmailAddress,
						 Password,
						 PromoCode,
						 UserReferralPromoCode,
                         ImageExtension,
                         ContactNumber,
                         IsActive,
                         IsContactVerified,
                         AddedOn,
                         UUID,
						 MobileVerifyCode
						)
		VALUES
						(
						 '#values.EmailAddress#',
						 '#hash(values.Password)#',
						 '#round(rand()*8999996+1000000)#',
                         '#values.UserReferralPromoCode#',
						 '',
                         '#values.ContactNumber#',
                          0,
                          0,
                          #createODBCDateTime(now())#,
                         '#UUID_to_insert#',
						 '#customer_OPT_confirm#'
						 )
	</cfquery>
	
	<cfquery name="get_inserted_account_id" datasource="#application.datasource#">
	    SELECT 			ID 
		FROM 			Users
		ORDER BY		ID Desc
		LIMIT 			0,1
	</cfquery>
			
	<!--- <CFQUERY name="insert_empty_user_address" datasource="#application.datasource#">
		INSERT INTO 	Addresses
						(
						UserID,
						BusinessID,
						AddressTypeID,
						AddedOn
						)
		VALUES 			(
						#get_inserted_account_id.UserID#,
						0,
						1,								
						#createODBCDateTime(now())#
						)
	</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><br>Click this link to confirm your email:<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.ContactNumber#" template = "#application.wwwrootprefix#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>&nbsp; &nbsp; 

	<script language="JavaScript">
		function submitformconfirmmobile1()
		{
		document.myformconfirmmobile1.submit();
		}
	</script>
	
	<form action="register.cfm" method="post" name="myformconfirmmobile1" id="myformconfirmmobile1" style="display:inline;">
	
		<a href="javascript: submitformconfirmmobile1()">didn't get the code?</a>
		
		<input type="hidden" name="UUID" value="#UUID_to_insert#">
		<input type="hidden" name="mode" value="manual_confirm">
		<input type="hidden" name="register_form_submitted" value="0">
	
	</form><br><br>
	
	in addition we sent an email to <cfoutput>#values.EmailAddress#</cfoutput> with a link to confirm your email address.<br><br>thanks.<br></cfoutput>

</cfif>