' VB Script Document

sub agreement_onclick
  Dim TheForm
  Set TheForm = Document.forms("register")
    TheForm.alldone.Disabled = False
		TheForm.agreement.Disabled = True
		set TheForm = nothing
End Sub

sub fname_onfocus
formmess.innerhtml= "<b>Required</b><br>Enter your Christian name"
End Sub
sub fname_onblur
formmess.innerhtml= "This box will give you help in completing your registration form."
End Sub

sub lname_onfocus
formmess.innerhtml= "<b>Required</b><br>Enter your Surname"
End Sub
sub lname_onblur
formmess.innerhtml= "This box will give you help in completing your registration form."
End Sub

sub street_onfocus
formmess.innerhtml= "<b>Required</b><br>Enter your full postal address"
End Sub
sub street_onblur
formmess.innerhtml= "This box will give you help in completing your registration form."
End Sub

sub town_onfocus
formmess.innerhtml= "<b>Required</b><br>Enter your full postal address"
End Sub
sub town_onblur
formmess.innerhtml= "This box will give you help in completing your registration form."
End Sub

sub county_onfocus
formmess.innerhtml= "<b>Required</b><br>Enter your full postal address"
End Sub
sub county_onblur
formmess.innerhtml= "This box will give you help in completing your registration form."
End Sub

sub country_onfocus
formmess.innerhtml= "<b>Required</b><br>Enter your full postal address"
End Sub
sub country_onblur
formmess.innerhtml= "This box will give you help in completing your registration form."
End Sub

sub mobile_onfocus
formmess.innerhtml= "<b>Optional</b><br>Enter your mobile phone number"
End Sub
sub mobile_onblur
formmess.innerhtml= "This box will give you help in completing your registration form."
End Sub

sub telephone_onfocus
formmess.innerhtml= "<b>Required</b><br>Enter your land line phone number"
End Sub
sub telephone_onblur
formmess.innerhtml= "This box will give you help in completing your registration form."
End Sub

sub email_onfocus
formmess.innerhtml= "<b>Required</b><br>This will be your account login for car Trader Spain."
End Sub
sub email_onblur
formmess.innerhtml= "This box will give you help in completing your registration form."
End Sub

sub password_onfocus
formmess.innerhtml= "<b>Required</b><br>Enter a password, up to 10 characters. If you forget your password we will resend it to the email address above."
End Sub
sub password_onblur
formmess.innerhtml= "This box will give you help in completing your registration form."
End Sub

sub scode_onfocus
formmess.innerhtml= "<b>Required</b><br>Enter the code you see in the box on the right. This code helps prevent automated entries from our web site."
End Sub
sub scode_onblur
formmess.innerhtml= "This box will give you help in completing your registration form."
End Sub

sub agreement_onmouseover
formmess.innerhtml= "<b>Required</b><br>Click now to confirm that you have read and agree to our terms and conditions."
End Sub
sub agreement_onmouseout
formmess.innerhtml= "This box will give you help in completing your registration form."
End Sub

sub alldone_onmouseover
formmess.innerhtml= "<b>Click the send button to open your account.</b>"
End Sub


'check the form
sub alldone_onclick
errormessage=""

Dim TheForm
Set TheForm = Document.forms("register")

if len(trim(TheForm.fname.value)) < 3 then
	errormessage = errormessage+"&bull;&nbsp;Christian name<br>"
end if
if len(trim(TheForm.lname.value)) < 3 then
	errormessage = errormessage+"&bull;&nbsp;Surname<br>"
end if
if len(trim(TheForm.street.value)) < 5 then
	errormessage = errormessage+"&bull;&nbsp;House No. & Street<br>"
end if
if len(trim(TheForm.town.value)) < 5 then
	errormessage = errormessage+"&bull;&nbsp;Town<br>"
end if
if len(trim(TheForm.county.value)) < 5 then
	errormessage = errormessage+"&bull;&nbsp;Province / County<br>"
end if
if len(trim(TheForm.country.value)) < 5 then
	errormessage = errormessage+"&bull;&nbsp;Country<br>"
end if
if len(trim(TheForm.telephone.value)) < 5 then
	errormessage = errormessage+"&bull;&nbsp;Telephone<br>"
end if
if trim(TheForm.email.value) = "" then
	errormessage = errormessage+"&bull;&nbsp;Email address<br>"
end if
if trim(TheForm.email.value) <> "" then
	if not IsEmailValid(TheForm.email.Value) then
		errormessage = errormessage+"&bull;&nbsp;The e-mail address you entered is not correct<br>"
	end if
end if
if trim(TheForm.password.value) = "" then
	errormessage = errormessage+"&bull;&nbsp;You need a password<br>"
end if
if trim(len(TheForm.scode.value)) <> 5 then
	errormessage = errormessage+"&bull;&nbsp;Security code<br>"
end if

if errormessage <> "" then 
	formmess.innerhtml ="You have not completed all the required fields correctly. Please check<br><b>"+errormessage+"</b>and send again."
	set theform = nothing
	exit sub
end if 

'do the emailing here
formmess.innerhtml = "Processing your application.<br><br>Please wait....<br><br><img src='images/loading.gif'>"

TheForm.submit
set theform = nothing

end sub



'email checks
Function IsEmailValid(strEmail) 
    Dim strArray 
    Dim strItem 
    Dim i 
    Dim c 
    Dim emailOK  
    emailOK = True      ' assume the email address is correct
    strArray = Split(strEmail, "@")     ' split the email address in two parts: name@domain.ext 
    If UBound(strArray) <> 1 Then     ' if there are more or less than two parts  
        emailOK = False 
        IsEmailValid = emailOK 
        Exit Function 
    End If 
    For Each strItem In strArray     ' check each part 
        If Len(strItem) <= 0 Then         ' no part can be void 
            emailOK = False 
            IsEmailValid = emailOK 
            Exit Function 
        End If 
        ' check each character of the part 
        ' only following "abcdefghijklmnopqrstuvwxyz_-." 
        ' characters and the ten digits are allowed 
        For i = 1 To Len(strItem) 
               c = LCase(Mid(strItem, i, 1)) 
               ' if there is an illegal character in the part 
               If InStr("abcdefghijklmnopqrstuvwxyz_-.", c) <= 0 And Not IsNumeric(c) Then 
                   emailOK = False 
                   IsEmailValid = emailOK 
                   Exit Function 
               End If 
        Next 
        If Left(strItem, 1) = "." Or Right(strItem, 1) = "." Then       ' the first and the last character in the part cannot be . (dot) 
           emailOK = False 
           IsEmailValid = emailOK 
           Exit Function 
        End If 
    Next 
    If InStr(strArray(1), ".") <= 0 Then     ' the second part (domain.ext) must contain a . (dot) 
        emailOK = False 
        IsEmailValid = emailOK 
        Exit Function 
    End If 
    i = Len(strArray(1)) - InStrRev(strArray(1), ".")     ' check the length of the extension  
    ' the length of the extension can be only 2, 3, or 4 
    ' to cover the new "info" extension 
    If i <> 2 And i <> 3 And i <> 4 Then 
        emailOK = False 
        IsEmailValid = emailOK 
        Exit Function 
    End If 
    If InStr(strEmail, "..") > 0 Then     ' after . (dot) cannot follow a . (dot) 
        emailOK = False 
        IsEmailValid = emailOK 
        Exit Function 
    End If 
    IsEmailValid = emailOK     ' finally it's OK  
 End Function 
