'UPD-DETN 	UPDATES CUSTOMER DETAILS

external 	vloadif() messboxwait() colpopup() entryline() sch scw dpath fentrybox() refresh()
external	messbox() bgi bgs fgp bgp fgi fgs udelstr() vunloadif()
external	cust_title custname custaddr1 custaddr2 custcity custpostcode
external	custcontact hometel offtel mobile offax cust_email abbrv_name clf clb

public	ptstr $line custdet[1,1] dsa recs plist[1,1] psa

global	x Approve() UpdateCustomer() Update() ReplaceHardSpace() ShowTitles() $update $pref
global	$cd1 $cd2 rec p5 popuplist() uistrcnt() j r
global	mr sym blen l c c2 r2 dc c1 lc sc pad pl i padc pc drows k pg tr y1 y
global	CheckPostCode() MainCode() $mincode


MAIN

	redimension custdet[12,2]
	p5=1

	UpdateCustomer()

END MAIN


FUNCTION UpdateCustomer()
  	custdet[1,1] = [Title]
  	custdet[1,2] = dbfldinfo("[Title]",2)
  	custdet[2,1] = [Customer_Name]
  	custdet[2,2] = dbfldinfo("[Customer_Name]",2)
  	custdet[3,1] = [Address_1]
  	custdet[3,2] = dbfldinfo("[Address_1]",2)
  	custdet[4,1] = [Address_2]
  	custdet[4,2] = dbfldinfo("[Address_2]",2)
  	custdet[5,1] = [City/Town]
  	custdet[5,2] = dbfldinfo("[City/Town]",2)
  	custdet[6,1] = [Postcode]
  	custdet[6,2] = dbfldinfo("[Postcode]",2)
  	custdet[7,1] = [Contact_Name]
  	custdet[7,2] = dbfldinfo("[Contact_Name]",2)
  	custdet[8,1] = [Home_Tel]
  	custdet[8,2] = dbfldinfo("[Home_Tel]",2)
  	custdet[9,1] = [Office_Tel]
  	custdet[9,2] = dbfldinfo("[Office_Tel]",2)
  	custdet[10,1] = [Mobile/Other_Nr]
  	custdet[10,2] = dbfldinfo("[Mobile/Other_Nr]",2)
  	custdet[11,1] = [Office_Fax]
  	custdet[11,2] = dbfldinfo("[Office_Fax]",2)
  	custdet[12,1] = [Email_Addr]
  	custdet[12,2] = dbfldinfo("[Email_Addr]",2)

  	x=Approve()				' message "l202/x is:"&str(x)
  	if x = -1
    		return (-1)
  	end if
  	abbrv_name = proper(left(custdet[2,1],7))
  	if len(custdet[2,1]) = 0
    		messboxwait(" Customer's name has been omitted - pls contact David @ HO ",0,0,1)
  	end if
  	if len(abbrv_name)=0
    		messboxwait(" Customer's abbrv'd name has been omitted - pls contact David @ HO ",0,0,1)
  	end if
	vloadif(dpath|"custstrt.vw")
	lock-record
     		[Title]         = custdet[1,1]
'       		[CustOrd_Name]  = custdet[2,1]
      		[Address_1]     = custdet[3,1]
      		[Address_2]     = custdet[4,1]
      		[City/Town]     = custdet[5,1]
      		[Postcode]      = custdet[6,1]
		     [Contact_Name]  = custdet[7,1]
		     [Home_Tel]      = custdet[8,1]
	     	[Office_Tel]    = custdet[9,1]
	    		[Mobile/Other_Nr]= custdet[10,1]
		    	[Office_Fax]    = custdet[11,1]
    			[Email_Addr]    = custdet[12,1]
	write-record
  	clear custdet[]
  	return (0)
END FUNCTION ' UpdateCustomer()


FUNCTION Approve()
local t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12
  	repaint off
  	while true
    		t1 = ReplaceHardSpace(custdet[1,1])
    		t2 = ReplaceHardSpace(custdet[2,1])
    		t3 = ReplaceHardSpace(custdet[3,1])
    		t4 = ReplaceHardSpace(custdet[4,1])
    		t5 = ReplaceHardSpace(custdet[5,1])
    		t6 = ReplaceHardSpace(custdet[6,1])
    		t7 = ReplaceHardSpace(custdet[7,1])
    		t8 = ReplaceHardSpace(custdet[8,1])
    		t9 = ReplaceHardSpace(custdet[9,1])
    		t10 = ReplaceHardSpace(custdet[10,1])
    		t11 = ReplaceHardSpace(custdet[11,1])
    		t12 = ReplaceHardSpace(custdet[12,1])

    		ShowTitles()' 	screen clear box 11 22 23 36 cfb cbb no-border

    		x = popuplist(11,37,25,t1&t2&t3&t4&t5&t6&t7&t8&t9&t10&t11&t12,chr(24)&chr(25)&"{Enter} amends - {F10} to continue",1,0)
' message "x) is:"&str(x)
' message "ptstr) is:"&str(ptstr)
    		if x = -1
    			continue while
    		elseif x = 1						' F10 pressed
      		return (0)
    		end if
    		screen shortrestore dsa
    		if rec = 1						' title
      		$line = 1
			if len(custdet[1,1])=0
				while true
    					y1=format(" Select Title ","M70")
	    				screen print 21 5 15 1 y1
    					y=popuplist(11,5,18,"Mr Mrs Mr˙&˙Mrs Miss Ms Other Ltd˙Co","",1,0)
    					if ptstr = "Other"
      					while true
        						x = entryline(" Enter title ",20,"","",21,5,70)
        						if x = 0
          						cust_title = ptstr
          						exit while
	        					end if
     	 				end while
    					elseif ptstr = "Ltd˙Co"
      					cust_title = ""
      					exit while
    					else
	      				cust_title = ptstr
'       					x=entryline(" Enter Forename or Initial ",35,"","",21,5,70)
			      		custdet[1,1] = Update(custdet[1,1],custdet[1,2])
      					cust_title = cust_title&proper(custdet[1,1])
    					end if
	    				exit while
  				end while
			else
	      		custdet[1,1] = Update(custdet[1,1],custdet[1,2])
			end if

    		elseif rec = 2
			messboxwait(" Cannot alter Customer name - contact Head Office ",0,0,1)

    		elseif rec = 3						' custaddr1
      		$line = 3
      		custdet[3,1] = Update(custdet[3,1],custdet[3,2])

    		elseif rec = 4						' custaddr2
      		$line = 4
      		custdet[4,1] = Update(custdet[4,1],custdet[4,2])

    		elseif rec = 5						' custcity
      		$line = 5
      		if len(custdet[5,1])=0
     	 		custdet[5,1] = Update("LONDON",custdet[5,2])
			else
	      		custdet[5,1] = Update(custdet[5,1],custdet[5,2])
			end if

    		elseif rec = 6						' custpostcode
      		$line = 6
			while true
	      		custdet[6,1] = Update(custdet[6,1],custdet[6,2])
'check for LONDON postcodes
				if custdet[5,1]="LONDON"
					repaint off
	      			custpostcode = ptstr			'message "custpostcode) is:"&str(custpostcode)
					y=CheckPostCode()
					vunloadif("post1.vws")
					if y=1				'correct
	     	 			exit while
					elseif y=0			'postcode not in "LONDON"
						messboxwait(custpostcode&"is NOT a LONDON code - check CITY or POSTCODE ",0,0,1)
	     	 			continue while
					end if
				end if
    				exit while
			end while

    		elseif ptstr = t7					' custcontact
        		$line = 7
        		custdet[7,1] = Update(custdet[7,1],custdet[7,2])

    		elseif ptstr = t8					' hometel
        		$line = 8
        		custdet[8,1] = Update(custdet[8,1],custdet[8,2])

    		elseif ptstr = t9					' offtel
        		$line = 9
        		custdet[9,1] = Update(custdet[9,1],custdet[9,2])

    		elseif ptstr = t10					' mobile
        		$line = 10
        		custdet[10,1] = Update(custdet[10,1],custdet[10,2])

    		elseif ptstr = t11					' offax
        		$line = 11
        		custdet[11,1] = Update(custdet[11,1],custdet[11,2])

    		elseif ptstr = t12					' cust_email
        		$line = 12
			while true
        			custdet[12,1] = Update(custdet[12,1],custdet[12,2])
			     cust_email =custdet[12,1]
				if left(cust_email,1)="@"
					messboxwait(" Invalid email address - 1st char is @ ",0,0,1)
					continue while
				elseif right(cust_email,1)="."
					messboxwait(" Invalid email address - last char is .  ",0,0,1)
					continue while
				elseif right(cust_email,1)="@"
					messboxwait(" Invalid email address - last char is @  ",0,0,1)
					continue while
				end if
				for j=1 to len(cust_email)
					r = mid(cust_email,j,1)			' message "r is:"&str(r)
					if r=chr(64)
						exit while
					end if
				end for
				messboxwait(" Invalid email address format - no @ sign ",0,0,1)
			end while
'         		custdet[12,1] = Update(custdet[12,1],custdet[12,2])

    		elseif ptstr==p5					' "CORRECT" selected
       		screen clear box 1 1 sch scw 0 0 no-border
       		repaint off
      		exit while
    		end if
  	end while
END FUNCTION ' Approve()


FUNCTION Update(e,w)
local $m
'e=default
'W=elen
'$m=mask
' message "L171 field width is:"&str(w)
   	case $line
      	when 5
       		$m="*20{[A-Za-z\ ]U}"
      	when 6
       		$m="AU*7{[A-Za-z1234567890\ ]U}"
      	when 8
       		$m="\0*14{[1234567890\-]}"
      	when 9
       		$m="\0*14{[1234567890\-]}"
      	when 10
       		$m="07###-######"
      	when 11
       		$m="\0*14{[1234567890\-]}"
      	otherwise
       		$m = ""
    	end case

' FUNCTION entryline("msg",elen,msk,dfalt,row1,col1,maxlen)

    	x = entryline(" Correct as necessary ",w,$m,e,26,37,58)
    	if $line=5
     	ptstr=UPPER(ptstr)
    	end if
    	return (ptstr)
'   	end while
END FUNCTION ' Update(e,w)


FUNCTION ReplaceHardSpace(str1)
local j r m bw l_last #addn
'   bw = 39                              ' boxwidth
  bw=54                              ' boxwidth
  m = ""
  for j = 1 to len(str1)
    r = mid(str1,j,1)
    if r = " "
      r = "˙"                          ' replace hard space
    end if
    m = m|r
  end for

  if len(m) < bw
    #addn = bw-len(m)
  else
    #addn = mod(len(m),bw)
  end if
  m = m|repeat("˙",#addn)
  return (m)
END FUNCTION ' ReplaceHardSpace()


FUNCTION ShowTitles()
local r1b r1t c1b c1t r2b c2b cfb cbb cft cbt
  	r1b = 11
  	r1t = r1b+1
  	c1b = 22
  	c1t = c1b+1
  	r2b = 23
  	c2b = 36
  	cfb = 15		'foreground - WHITE
  	cbb = 10		'background - GREEN
  	cft = 15
  	cbt = 10
'   	screen clear box 11 22 23 36 cfb cbb no-border
  	screen clear box r1b c1b r2b c2b cfb cbb no-border
'   	screen print r1t-1 c1b+1 0 cbt custcode
  	screen print r1t c1b+1 cft cbt "Title"
  	screen print r1t+1 c1b+1 cft cbt "Surname"
  	screen print r1t+2 c1b+1 cft cbt "Address 1"
  	screen print r1t+3 c1b+1 cft cbt "Address 2"
  	screen print r1t+4 c1b+1 cft cbt "TOWN/CITY"
  	screen print r1t+5 c1b+1 cft cbt "Postcode"
  	screen print r1t+6 c1b+1 cft cbt "Contact"
' 	if $preffone="H"
' 		cft=12
'   		screen print r1t+7 c1b+1 cft cbt "Home 'phone P"
' 		cft=1
' 	else
  		screen print r1t+7 c1b+1 cft cbt "Home 'phone"
' 	end if
'
' 	if $preffone="O"
' 		cft=12
'   		screen print r1t+8 c1b+1 cft cbt "Office 'phone P"
' 		cft=1
' 	else
  		screen print r1t+8 c1b+1 cft cbt "Office 'phone"
' 	end if

' 	if $preffone="M"
' 		cft=12
'   		screen print r1t+9 c1b+1 cft cbt "Mobile 'phone P"
' 		cft=1
' 	else
  		screen print r1t+9 c1b+1 cft cbt "Mobile 'phone"
' 	end if
  	screen print r1t+10 c1b+1 cft cbt "Fax nr"
  	screen print r1t+11 c1b+1 cft cbt "Email"

END FUNCTION ' ShowTitles()


FUNCTION	CheckPostCode()
local	left4code
 	$mincode=MainCode(custpostcode)					'message "$mincode) is:"&str($mincode)				'
	vloadif(dpath|"post1.vws")
    	order change key "[E1]"
    	data find "[E1]" equal $mincode options ""
    	if cerror                               '   if none - then return
		vunloadif("post1.vws")
		return (0)
	else
		vunloadif("post1.vws")
		return (1)					' found - is a LONDON code
	end if
END FUNCTION ' CheckPostCode()


FUNCTION MainCode(x)
local y j z b
	z=""
	b=""
     for j = 1 to len(x)
      	z = mid(x,j,1)						'message "mid(y,j,1) is:"&str(z)
      	if z = " "					' SOFT space
 			return (b)				'
      	end if
		b=b|z							'message "b is:"&str(b)
    	end for
	return (b)				'
END FUNCTION ' MainCode()


function popuplist(r1,c1,br,list,msg,num,mnu)
local t hml hm cnum mscn pad padc ret
  	if exact(trim(list),NULL)=FALSE
     	recs = uistrcnt(list)
     	if recs = 0
          	return (-3)
     	end if
  	else
     	return (-2)
  	end if

	redimension plist[recs,3]
	smartpeek $_l1 hml
	if br-r1<1
     	return (-4)
	elseif br+1 > scrheight
     	mr=scrheight-1
	     msg = ""
	else
     	mr=br
	end if
	if br >= hml
     	mnu = 0
	end if

	screen save hml 1 hml scrwidth mscn
	if recs > scrheight
     	if mnu = 1
          	screen clear box hml 1 hml scrwidth 0 0 no-border
	          screen print hml 1 bgi bgs "Building list..."
     	end if
	end if
	ptstr=NULL
	if mnu = 1
     	hm =  case num (0,"Enter = select/unselect   F10 = done   Esc = exit") \
                    (1,"Enter = select   Esc = exit      (select: 1 item)") \
                    else "Enter = select/unselect   F10 = done   Esc = exit  " & \
                         "   (select up to:" & str(num) & "items)"
	else
     	hm = NULL
	end if
	sym = spsymmap(28)
	cnum=0
	blen=0
	l=blen
	for c=1 to recs
     	plist[c,2]=group(list,c)
	     l=len(plist[c,2])
     	plist[c,1]=0
	     if l>blen
     		blen=l
	     end if
	end for
	c2=c1+blen+2
	r2=r1+recs
	if r2>mr
	     r2=mr
	end if
	dc=(c2-c1)
	lc=c1+1
	pad = case num (1,1) else 2
	sc=c1+pad-1
	pl=(r2-r1)
	padc = repeat(chr(32),pad)
	for i = 1 to recs
     	pc = 1
		plist[i,2]=padc|format(plist[i,2],"l",dc-1)
	     plist[i,3] = i
     	if i = pl
          	pc=pc+1
	     end if
	end for
	if recs > scrheight
    		screen shortrestore mscn
	end if
	screen save r1 c1 r2+2 c2+pad psa
' x=c2+pad
' message "L336 c2+pad is:"&str(x)
	screen clear box r1 c1 r2+1 c2+pad fgp bgp
	pc=1
	for c=1 to pl
     	screen print c+r1 lc fgp bgp plist[c,2]
	end for
  	if msg > null
    		screen print r2+2 c1 fgi bgi str(format(left(msg,dc+pad+1),"M"|str(dc+pad+1)))
  	end if
  	if mnu = 1
    		screen clear box hml 1 hml scrwidth fgs bgs no-border
		screen print hml 1 fgs bgs (format(hm,"L"|str(scrwidth)))
  	end if

  	c=1
  	rec=1
  	screen print r1+c lc fgi bgi plist[rec,2]
  	drows = pl
  	while TRUE
    		k=inchar
	     screen print r1+c lc fgp bgp plist[rec,2]
    		if plist[rec,1]=1
      		screen print r1+c sc fgp bgp sym
    		end if
    		if k={Down}
      		if rec=recs
        			if recs<=pl
          			rec=1
          			c=1
        			else
          			beep
        			end if
      		else
        			if c = pl
          			screen scroll up r1+1 lc r2 (c2+case num (1,0) else 1) fgp bgp 1
       			end if
        			c= case c (pl,c) else (c+1)
	        		rec=rec+1
     	 	end if
    		elseif k={Up}
      		if rec=1
        			if recs <= pl
          			rec = recs
          			c = pl
               	else
                    	beep
               	end if
          	else
               	if c = 1
                    	screen scroll down r1+1 lc r2 (c2+case num (1,0) else 1) fgp bgp 1
	               end if
     	          c= case c (1,c) else (c-1)
          	     rec=rec-1
	          end if
     	elseif k={Home}
          	if c>1
               	if rec =(rec-c)+1
                    	rec = 1
	               else
     	               rec =(rec-c)+1
          	     end if
               	c=1
	          else
     	          rec=1
          	     c=1
	          end if
     	elseif k={^Home}
          	if rec = c
               	rec = 1
	               c=1
     	     else
          	     rec = 1
               	c=1
	               refresh(c,r1,c1,r2,c2,pad)
     	     end if
	     elseif k={End}
     	     if rec < recs and c < pl
          	     if drows < pl
               	     rec = recs-pl+1
                    	refresh(c,r1,c1,r2,c2,pad)
	                    rec = recs
     	               c = pl
          	     else
               	     if rec+(pl-c) < recs
                    	     rec = rec+(pl-c)
                         	c = pl
	                    else
     	                    rec = recs
          	               c = pl
               	     end if
	               end if
     	     end if
	     elseif k={^End}
     	     rec = recs-pl+1
          	c = 1
	          refresh(c,r1,c1,r2,c2,pad)
     	     c = pl
          	rec = recs
	     elseif k={PgDn}
     	     if rec = recs and c = pl
          	     beep
	          elseif c <= pl
     	          if rec = recs or rec+pl >= recs
          	          rec = recs-pl+1
               	     c = 1
                    	refresh(c,r1,c1,r2,c2,pad)
	                    c = pl
     	               rec = recs
          	     else
               	     rec = rec+pl
                    	refresh(c,r1,c1,r2,c2,pad)
	               end if
     	     end if
	     elseif k={PgUp}
     	     if rec = 1 and c = 1
          	     beep
	          else
     	          if recs > pl
          	          if (rec-pl)-c <= 1
               	          c = rec-pl
                    	     if c < 1
                         	     c = 1
	                         end if
     	                    rec = 1
          	               refresh(1,r1,c1,r2,c2,pad)
               	          rec = c
                    	else
                         	rec=(rec-pl)
	                         refresh(1,r1,c1,r2,c2,pad)
     	               end if
          	     else
               	     if rec > 1
                    	     rec=1
                         	c=1
	                    end if
     	          end if
          	end if
	     elseif k={Enter}
     		screen print r1+c lc fgi bgi plist[rec,2]
	          if num = 1
                    ret=trim(plist[rec,2])
                    exit while
     	     end if
          	if plist[rec,1] = 1
               	if udelstr(trim(plist[rec,2]),ret) = 0
                    	ret = ptstr
	               end if
     	          plist[rec,1] = 0
          	     cnum=cnum-1
	          else
     	          if cnum = num and not(num=0)
          	          beep
               	else
                    	ret=trim(ret&plist[rec,2])
	                    plist[rec,1] = 1
     	               cnum=cnum+1
          	     end if
	          end if
     	     if rec < recs
          	  smartpoke $_key {Down}
	          end if
     	elseif k={Esc}
       		ret=null
       		exit while
     	elseif k={F10}
      		return (1)
    		end if
    		if k <> {Enter}
      		screen print r1+c lc fgi bgi plist[rec,2]
    		end if
    		if plist[rec,1]=1
      		screen print r1+c sc fgi bgi sym
    		end if
  	end while
  	screen save r1 c1 r2+2 c2+1+pad dsa
  	screen shortrestore mscn
  	screen shortrestore psa
  	clear c k dc lc sc recs c2 r2 l blen pl mr pc sym pg i tr drows
  	redimension  plist[1,3]
  	if trim(ret) = NULL
    		ptstr = NULL
    		if k={F10}
      		return (0)
    		else
      		return (-1)
    		end if
  	else
    		ptstr = trim(ret)
    		return (0)
  	end if
end function  'popuplist()


function uistrcnt(sl)
 local i s lo hi c
 s=20
 while exact(group(sl,s),NULL)=FALSE
      s=s+20
 end while
 hi = s
 lo = 1
 while lo <= hi
      i = int((lo+hi)/2)
      c = group(sl,i)
      if c = NULL
           hi = i-1
      else
           lo = i+1
      end if
 end while
 while (exact(group(sl,i),NULL)=TRUE and i>0)
      i=i-1
 end while
 return (i)
end function  'uistrcnt()



' custname=[Customer_Name]
' message "custname) is:"&str(custname)
' abbrv_name=[Abbrv_Name]
' message "abbrv_name) is:"&str(abbrv_name)
' custcode=[Customer_Code]
' message "custcode) is:"&str(custcode)
' custcity=[City/Town]
' message "custcity) is:"&str(custcity)
' custpostcode=[Postcode]
' message "custpostcode) is:"&str(custpostcode)
' custaddr1=[Address_1]
' message "custaddr1) is:"&str(custaddr1)
' custaddr2=[Address_2]
' message "custaddr2) is:"&str(custaddr2)
' hometel= [Home_Tel]
' message "hometel) is:"&str(hometel)
' offtel=[Office_Tel]
' message "offtel) is:"&str(offtel)
' offax=[Office_Fax]
' message "offax) is:"&str(offax)
' mobile=[Mobile/Other_Nr]
' message "mobile) is:"&str(mobile)
' cust_title=[Title]
' message "cust_title) is:"&str(cust_title)
' message "cust_email is:"&str(cust_email)
' custcontact=[Contact_Name]
' message "L56 custcontact) is:"&str(custcontact)

'    		while true
'     			x = fentrybox(" Customer's EMAIL address - {Esc} if none ",50,"",cust_email)
' 	    		if x = 0
'  			     cust_email=ptstr
'        			exit while
' 	    		else							' message "Confirm no email address"
'  			     cust_email = ""
'      	  		return (0)
'     			end if
' 	   		end while


