Library /lib/FormHandler.php

Language PHP Lines 4009
MD5 Hash 56e41ea1486b68b7f725b436fffcdfe3
Repository https://github.com/reshadf/Library.git View Raw File
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
<?php
/**
 * FormHandler v3.2
 *
 * Look for more info at http://www.formhandler.net
 * @package FormHandler
 */

// make sure this file is not accessed directly
if(strtolower(basename($_SERVER['PHP_SELF'])) == strtolower(basename(__FILE__)))
{
	die('This file cannot be accessed directly! Include it in your script instead!');
}

/******* BUILD IN VALIDATOR FUNCTIONS *******/
define('FH_STRING',     'IsString',    true);	// any string that doesn't have control characters (ASCII 0 - 31) but spaces are allowed
define('FH_ALPHA',      'IsAlpha',     true);	// only letters a-z and A-Z
define('FH_DIGIT',      'IsDigit',     true);	// only numbers 0-9
define('FH_ALPHA_NUM',  'IsAlphaNum',  true);	// letters and numbers
define('FH_INTEGER',    'IsInteger',   true);	// only numbers 0-9 and an optional - (minus) sign (in the beginning only)
define('FH_FLOAT',      'IsFloat',     true);	// like FH_INTEGER, only with , (comma)
define('FH_FILENAME',   'IsFilename',  true);	// a valid file name (including dots but no slashes and other forbidden characters)
define('FH_BOOL',       'IsBool',      true);	// a boolean (TRUE is either a case-insensitive "true" or "1". Everything else is FALSE)
define('FH_VARIABLE',   'IsVariabele', true);	// a valid variable name (letters, digits, underscore)
define('FH_PASSWORD',   'IsPassword',  true);	// a valid password (alphanumberic + some other characters but no spaces. Only allow ASCII 33 - 126)
define('FH_URL',        'IsURL',       true);	// a valid URL
define('FH_URL_HOST',   'IsURLHost',   true);   // a valid URL (http connection is used to check if url exists!)
define('FH_EMAIL',      'IsEmail',     true);	// a valid email address (only checks for valid format: xxx@xxx.xxx)
define('FH_EMAIL_HOST', 'IsEmailHost', true);   // like FH_EMAIL only with host check
define('FH_TEXT',       'IsText',      true);	// like FH_STRING, but newline characters are allowed
define('FH_NOT_EMPTY',  'notEmpty',    true);   // check if the value is not empty
define('FH_NO_HTML',	'NoHTML',	   true);   // check if the value does not contain html
define('FH_IP',		    'IsIp',		   true);   // check if the value is a valid ip adres (xxx.xxx.xxx.xxx:xxxx)

// for dutch people
define('FH_POSTCODE',   'IsPostcode',  true);   // valid dutch postcode (eg. 9999 AA)
define('FH_PHONE',      'IsPhone',     true);   // valid dutch phone-number(eg. 058-2134778)

// same as above, but with these the value is not required
define('_FH_STRING',     '_IsString',    true);
define('_FH_ALPHA',      '_IsAlpha',     true);
define('_FH_DIGIT',      '_IsDigit',     true);
define('_FH_ALPHA_NUM',  '_IsAlphaNum',  true);
define('_FH_INTEGER',    '_IsInteger',   true);
define('_FH_FLOAT',      '_IsFloat',     true);
define('_FH_FILENAME',   '_IsFilename',  true);
define('_FH_BOOL',       '_IsBool',      true);
define('_FH_VARIABLE',   '_IsVariabele', true);
define('_FH_PASSWORD',   '_IsPassword',  true);
define('_FH_URL',        '_IsURL',       true);
define('_FH_URL_HOST',   '_IsURLHost',   true);
define('_FH_EMAIL',      '_IsEmail',     true);
define('_FH_EMAIL_HOST', '_IsEmailHost', true);
define('_FH_TEXT',       '_IsText',      true);
define('_FH_POSTCODE',   '_IsPostcode',  true);
define('_FH_PHONE',      '_IsPhone',     true);
define('_FH_NO_HTML',	 '_NoHTML',	     true);
define('_FH_IP',		 '_IsIp',		 true);

// Mask for titles above the fields..
// This is not used by default but can be handy for the users
define('FH_TITLE_ABOVE_FIELD_MASK',
"  <tr>\n".
"    <td>%title% %seperator%</td>\n".
"  </tr>\n".
"  <tr>\n".
"    <td>%field% %help% %error%</td>\n".
"  </tr>\n"
);

// make some variables global when the version < 4.1.0
if(intval( str_replace('.', '', phpversion()) ) < 410)
{
	define('_global', false);
	$_GET    = $HTTP_GET_VARS;
	$_POST   = $HTTP_POST_VARS;
	$_FILES  = $HTTP_POST_FILES;
	$_SERVER = $HTTP_SERVER_VARS;
}
// set the var so that we dont have to make the $_GET arrays global
else
{
	define('_global', true);
}

// include needed files
define('FH_INCLUDE_DIR', str_replace('\\', '/', dirname(__FILE__)).'/');
require_once( FH_INCLUDE_DIR . 'fields/class.Field.php' );
require_once( FH_INCLUDE_DIR . 'buttons/class.Button.php' );
require_once( FH_INCLUDE_DIR . 'includes/config.inc.php' );
require_once( FH_INCLUDE_DIR . 'includes/error.inc.php' );
require_once( FH_INCLUDE_DIR . 'includes/class.Validator.php' );
require_once( FH_INCLUDE_DIR . 'includes/class.MaskLoader.php' );

/**
 * class FormHandler
 *
 * FormHandler without DB options
 *
 * @author Teye Heimans
 * @link http://www.formhandler.net
 */
class FormHandler
{
	// protected !!
	var $_fields;           // array: contains all the fields
	var $_posted;           // boolean: if the form is posted or not
	var $_name;             // string: the name of the form
	var $_action;           // string: the action of the form
	var $_displayErrors;    // boolean: if we have to display the errors in the form
	var $_mask;             // string: the mask which should be used
	var $_upload;           // array: contains the names of the uploadfields
	var $_date;             // array: contains the names of the datefields
	var $_onCorrect;        // string: the callback function when the form is correct
	var $_add;              // array: contains the data which was added by the user
	var $_focus;            // string: the field which should get the focus
	var $_convert;          // array: fields which should be converted (eg. resizeimage or mergeimage)
	var $_buffer;           // array: buffer of set values (used when the field does not exists yet)
	var $_text;             // array: the language array we are using to display the messages etc
	var $_lang;				// string: the language used
	var $_setTable;			// boolean: set a html table arround the fields or has the user done that in the mask ?
	var $_extra;			// string: extra tag information for the <form> tag (like CSS or javascript)
	var $_pageCounter;      // int: how many pages has this form
	var $_curPage;          // int: current page
	var $_mail;             // array: contains the mailing data
	var $_tabindexes;       // array: tab indexes of the fields...
	var $_js;				// array: contains all the needed javascript for the form
	var $_help;				// array: contains the help text for the fields
	var $_helpIcon;		    // string: the path to the help image
	var $_cache;			// array: save the values of the field in this array after the flush is called (then the objects are deleted!)
	var $_viewMode;			// boolean: is view mode enabled or not
	var $_tableSettings;    // array: array with all table settings
	var $_ajaxValidator;	// boolean: if Ajax validation must be used or not.
	var $_ajaxValidatorScript;	// boolean: if Ajax validation must include library or not.

	/**
     * FormHandler::FormHandler()
     *
     * constructor: initialisation of some vars
     *
     * @param string $name: the name for the form (used in the <form> tag
     * @param string $action: the action for the form (used in <form action="xxx">)
     * @param string $extra: extra css or js which is included in the <form> tag
     * @author Teye Heimans
     * @return FormHandler
     */
	function FormHandler( $name = null, $action = null, $extra = null )
	{
		// initialisation
		$this->_viewMode        = false;
		$this->_ajaxValidator	= false;
		$this->_ajaxValidatorScript	= true;
		$this->_fields          = array();
		$this->_date            = array();
		$this->_upload          = array();
		$this->_add             = array();
		$this->_js		  	    = array();
		$this->_buffer          = array();
		$this->_convert         = array();
		$this->_mail            = array();
		$this->_tabindexes      = array();
		$this->_customMsg       = array();
		$this->_help            = array();
		$this->_cache           = array();
		$this->_tableSettings   = array();
		$this->_displayErrors   = true;
		$this->_setTable        = true;
		$this->_focus           = null;
		$this->_pageCounter     = 1;

		// make vars global if needed
		if(!_global) global $_SERVER, $_POST, $_GET;

		// try to disable caching from the browser if possible
		if(!headers_sent())
		{
			header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
			header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
			header('Cache-Control: no-store, no-cache, must-revalidate');
			header('Cache-Control: post-check=0, pre-check=0', false);
			header('Pragma: no-cache');
			header("Cache-control: private");
		}

		// set all config values
		fh_conf();

		// get config setting for _setTable, since 08-10-2009 JW
		$this->_setTable = FH_USE_TABLE;

		// get config setting for _focus, since 14-01-2010 JW
		$this->_focus = FH_SET_FOCUS;

		// set the name of the form (the user has submitted one)
		if( !empty($name) )
		{
			$this->_name = $name;
		}
		// get a unique form name because the user did not give one
		else
		{
			// get a unique form name!
			$i = null;
			while(defined('FH_'.FH_DEFAULT_FORM_NAME.$i))
			{
				$i = is_null($i) ? 1 : ($i+1);
			}

			define('FH_'.FH_DEFAULT_FORM_NAME.$i, 1);
			$this->_name = FH_DEFAULT_FORM_NAME.$i;
			$i = null;
		}

		// set the action of the form if none is given
		if( !empty($action) )
		{
			$this->_action = $action;
		}
		else
		{
			$this->_action = $_SERVER['PHP_SELF'];
			if( !empty($_SERVER['QUERY_STRING']) )
			{
				$this->_action .= '?'.$_SERVER['QUERY_STRING'];
			}
		}

		// get the $extra (JS, css, etc..) to put into the <form> tag
		if( !empty( $extra ) )
		{
			$this->_extra = $extra;
		}

		// set the default mask
		$this->setMask( FH_DEFAULT_ROW_MASK );
			
		// set the default help icon
		$this->setHelpIcon( FH_FHTML_DIR.'images/helpicon.gif' );

		// check if the form is posted
		$this->_posted = ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST[$this->_name.'_submit']));

		// make a hidden field so we can identify the form
		$this->hiddenField( $this->_name.'_submit', '1' );

		// get the current page
		$this->_curPage = isset($_POST[$this->_name.'_page']) ? $_POST[$this->_name.'_page'] : 1;

		// set our own error handler
		if(FH_DISPLAY_ERRORS)
		{
			error_reporting( E_ALL );
			set_error_handler( 'catchErrors' );
		}

		// set the language...
		$this->setLanguage();

		// set the default table settings
		$this->setTableSettings();
	}

	/********************************************************/
	/************* FIELDS ***********************************/
	/********************************************************/

	/**
     * FormHandler::browserField()
     *
     * Creates a browserfield on the form
     *
     * @param string $title: The title of the field
     * @param string $name: The name of the field
     * @param string $path: The path to browse
     * @param string $validator: The validator which should be used to validate the value of the field
     * @param int $size: The size of the field
     * @param string $extra: CSS, Javascript or other which are inserted into the HTML tag
     * @return void
     * @access public
     * @author Johan Wiegel
     */
	function browserField(
	$title,
	$name,
	$path,
	$validator = null,
	$size      = null,
	$extra     = null)
	{
		require_once(FH_INCLUDE_DIR.'fields/class.BrowserField.php');
		require_once(FH_INCLUDE_DIR.'buttons/class.Button.php');

		// create the field
		$fld = new BrowserField($this, $name, $path);
		if(!empty($validator)) $fld->setValidator( $validator );
		if(!empty($size))      $fld->setSize( $size );
		if(!empty($extra))     $fld->setExtra( $extra );

		// register the field
		$this->_registerField( $name, $fld, $title );
	}

	/**
     * FormHandler::textField()
     *
     * Creates a textfield on the form
     *
     * @param string $title: The title of the field
     * @param string $name: The name of the field
     * @param string $validator: The validator which should be used to validate the value of the field
     * @param int $size: The size of the field
     * @param int $maxlength: The allowed max input of the field
     * @param string $extra: CSS, Javascript or other which are inserted into the HTML tag
     * @return void
     * @access public
     * @author Teye Heimans
     */
	function textField(
	$title,
	$name,
	$validator = null,
	$size      = null,
	$maxlength = null,
	$extra     = null)
	{
		require_once(FH_INCLUDE_DIR.'fields/class.TextField.php');

		// create the field
		$fld = new TextField($this, $name);
		if(!empty($validator)) $fld->setValidator( $validator );
		if(!empty($size))      $fld->setSize( $size );
		if(!empty($maxlength)) $fld->setMaxlength( $maxlength );
		if(!empty($extra))     $fld->setExtra( $extra );

		// register the field
		$this->_registerField( $name, $fld, $title );
	}

	/**
     * FormHandler::captchaField()
     *
     * Creates a captchafield on the form using Securimage - A PHP class for creating and managing form CAPTCHA images
     *
     * @param string $title: The title of the field
     * @param string $name: The name of the field
     * @param int $size: The size of the field
     * @param int $maxlength: The allowed max input of the field
     * @param string $extra: CSS, Javascript or other which are inserted into the HTML tag
     * @return void
     * @access public
     * @author Johan Wiegel
     * @since 27-11-2007
     */
	function CaptchaField(
	$title,
	$name,
	$size      = null,
	$maxlength = null,
	$extra     = null)
	{
		static $bCaptcha = true;
		if ($bCaptcha)
		{
			$bCaptcha = false;
			require_once(FH_INCLUDE_DIR.'fields/class.TextField.php');

			// create the field
			$fld = new TextField($this, $name);
			if( $this->isPosted() )
			{
				$fld->setValidator( 'FH_CAPTCHA' );
			}
			if(!empty($size))      $fld->setSize( $size );
			if(!empty($maxlength)) $fld->setMaxlength( $maxlength );
			if(!empty($extra))     $fld->setExtra( $extra );

			$this->ImageButton( FH_FHTML_DIR .'securimage/securimage_show.php?sid='.md5(uniqid(time())),null,'onclick="return false;" style="cursor:default;"' );

			// register the field
			$this->_registerField( $name, $fld, $title );

			// empty the field if the value was not correct.

			if ($this->isPosted() && !$this->isCorrect())
			{
				$this->setValue($name, "", true);
			}
		}
		else
		{
			trigger_error( "Only one captchafield in a form", E_USER_WARNING );
		}
	}

	/**
     * FormHandler::textSelectField()
     *
     * Creates a textSelectfield on the form
     *
     * @param string $title: The title of the field
     * @param string $name: The name of the field
     * @param array $aOptions : the options for the select part
     * @param string $validator: The validator which should be used to validate the value of the field
     * @param int $size: The size of the field
     * @param int $maxlength: The allowed max input of the field
     * @param string $extra: CSS, Javascript or other which are inserted into the HTML tag
     * @return void
     * @access public
     * @author Johan wiegel
     * @since 22-10-2008
     */
	function textSelectField(
	$title,
	$name,
	$aOptions,
	$validator = null,
	$size      = null,
	$maxlength = null,
	$extra     = null)
	{
		require_once(FH_INCLUDE_DIR.'fields/class.TextField.php');
		require_once(FH_INCLUDE_DIR.'fields/class.TextSelectField.php');

		// create the field
		$fld = new TextSelectField($this, $name, $aOptions);
		if(!empty($validator)) $fld->setValidator( $validator );
		if(!empty($size))      $fld->setSize( $size );
		if(!empty($maxlength)) $fld->setMaxlength( $maxlength );
		if(!empty($extra))     $fld->setExtra( $extra );

		// register the field
		$this->_registerField( $name, $fld, $title );
	}

	/**
     * FormHandler::passField()
     *
     * Create a password field
     *
     * @param string $title: The title of the field
     * @param string $name: The name of the field
     * @param string $validator: The validator which should be used to validate the value of the field
     * @param int $size: The size of the field
     * @param int $maxlength: The allowed max input of the field
     * @param string $extra: CSS, Javascript or other which are inserted into the HTML tag
     * @return void
     * @access public
     * @author Teye Heimans
     */
	function passField(
	$title,
	$name,
	$validator = null,
	$size      = null,
	$maxlength = null,
	$extra     = null)
	{
		require_once(FH_INCLUDE_DIR.'fields/class.TextField.php');
		require_once(FH_INCLUDE_DIR.'fields/class.PassField.php');

		// create the field
		$fld = new PassField( $this, $name );

		if(!empty($validator)) $fld->setValidator( $validator );
		if(!empty($size))      $fld->setSize( $size );
		if(!empty($maxlength)) $fld->setMaxlength( $maxlength );
		if(!empty($extra))     $fld->setExtra( $extra );

		// register the field
		$this->_registerField( $name, $fld, $title );
	}

	/**
     * FormHandler::hiddenField()
     *
     * Create a hidden field
     *
     * @param string $name: The name of the field
     * @param string $value: The value of the field
     * @param string $validator: The validator which should be used to validate the value of the field
     * @param string $extra: CSS, Javascript or other which are inserted into the HTML tag
     * @return void
     * @access public
     * @author Teye Heimans
     */
	function hiddenField(
	$name,
	$value     = null,
	$validator = null,
	$extra     = null)
	{
		require_once(FH_INCLUDE_DIR.'fields/class.HiddenField.php');

		// create new hidden field
		$fld = new HiddenField($this, $name);

		// only set the hidden field value if there is not a value in the $_POST array
		if(!is_null($value) && !$this->isPosted() )
		$fld->setValue( $value );
		if(!empty($validator)) $fld->setValidator( $validator );
		if(!empty($extra))     $fld->setExtra( $extra );

		// register the field
		$this->_registerField( $name, $fld, '__HIDDEN__' );
	}

	/**
     * FormHandler::textArea()
     *
     * Create a textarea on the form
     *
     * @param string $title: The title of the field
     * @param string $name: The name of the field
     * @param string $validator: The validator which should be used to validate the value of the field
     * @param int $cols: How many cols (the width of the field)
     * @param int $rows: How many rows (the height of the field)
     * @param string $extra: CSS, Javascript or other which are inserted into the HTML tag
     * @return void
     * @access public
     * @author Teye Heimans
     */
	function textArea(
	$title,
	$name,
	$validator = null,
	$cols      = null,
	$rows      = null,
	$extra     = null)
	{
		require_once(FH_INCLUDE_DIR.'fields/class.TextArea.php');

		// create new textarea
		$fld = new TextArea($this, $name);

		if(!empty($validator)) $fld->setValidator( $validator );
		if(!empty($cols))      $fld->setCols( $cols );
		if(!empty($rows))      $fld->setRows( $rows );
		if(!empty($extra))     $fld->setExtra( $extra );

		// register the field
		$this->_registerField( $name, $fld, $title );
	}

	/**
     * FormHandler::selectField()
     *
     * Create a selectField on the form
     *
     * @param string $title: The title of the field
     * @param string $name: The name of the field
     * @param array $options: The options used for the field
     * @param string $validator: The validator which should be used to validate the value of the field
     * @param boolean $useArrayKeyAsValue: If the array key's are the values for the options in the field
     * @param boolean $multiple: Should it be possible to select multiple options ? (Default: false)
     * @param int $size: The size of the field (how many options are displayed)
     * @param string $extra: CSS, Javascript or other which are inserted into the HTML tag
     * @return void
     * @access public
     * @author Teye Heimans
     */
	function selectField(
	$title,
	$name,
	$options,
	$validator          = null,
	$useArrayKeyAsValue = null,
	$multiple           = null,
	$size               = null,
	$extra              = null)
	{
		require_once(FH_INCLUDE_DIR.'fields/class.SelectField.php');

		// options has to be an array
		if(!is_array($options))
		{
			trigger_error(
			"You have to give an array as value with the selectfield '$name'",
			E_USER_WARNING
			);
			return;
		}

		// create new selectfield
		$fld = new SelectField( $this, $name );
		$fld->setOptions( $options );

		if(!empty($validator))            $fld->setValidator( $validator );
		if(!is_null($useArrayKeyAsValue)) $fld->useArrayKeyAsValue( $useArrayKeyAsValue );
		if(!empty($extra))                $fld->setExtra( $extra );
		if($multiple)                     $fld->setMultiple( $multiple );

		// if the size is given
		if(!empty($size))
		{
			$fld->setSize( $size );
		}
		// if no size is set and multiple is enabled, set the size default to 4
		else if( $multiple )
		{
			$fld->setSize( 4 );
		}

		// register the field
		$this->_registerField( $name, $fld, $title );
	}

	/**
     * FormHandler::checkBox()
     *
     * Create a checkBox on the form
     *
     * @param string $title: The title of the field
     * @param string $name: The name of the field
     * @param array|string $value: The option(s) used for the field
     * @param string $validator: The validator which should be used to validate the value of the field
     * @param boolean $useArrayKeyAsValue: If the array key's are the values for the options in the field
     * @param string $extra: CSS, Javascript or other which are inserted into the HTML tag
     * @param string $mask: if more the 1 options are given, glue the fields together with this mask
     * @return void
     * @access public
     * @author Teye Heimans
     */
	function checkBox(
	$title,
	$name,
	$value              = 'on',
	$validator          = null,
	$useArrayKeyAsValue = null,
	$extra              = null,
	$mask               = null)
	{
		require_once(FH_INCLUDE_DIR.'fields/class.CheckBox.php');

		// create a new checkbox
		$fld = new CheckBox($this, $name, $value);

		if(!empty($validator))            $fld->setValidator( $validator );
		if(!is_null($useArrayKeyAsValue)) $fld->useArrayKeyAsValue( $useArrayKeyAsValue );
		if(!empty($extra))                $fld->setExtra( $extra );
		if(!empty($mask))                 $fld->setMask( $mask );

		// register the field
		$this->_registerField( $name, $fld, $title );
	}

	/**
     * FormHandler::radioButton()
     *
     * Create a radioButton on the form
     *
     * @param string $title: The title of the field
     * @param string $name: The name of the field
     * @param array $options: The options used for the field
     * @param string $validator: The validator which should be used to validate the value of the field
     * @param boolean $useArrayKeyAsValue: If the array key's are the values for the options in the field
     * @param string $extra: CSS, Javascript or other which are inserted into the HTML tag
     * @param string $mask: if more the 1 options are given, glue the fields together with this mask
     * @return void
     * @access public
     * @author Teye Heimans
     */
	function radioButton(
	$title,
	$name,
	$options,
	$validator          = null,
	$useArrayKeyAsValue = null,
	$extra              = null,
	$mask               = null)
	{
		require_once(FH_INCLUDE_DIR.'fields/class.RadioButton.php');

		// value has to be an array
		if(!is_array($options))
		{
			trigger_error(
			"You have to give an array as value with the radiobutton '$name'",
			E_USER_WARNING
			);
			return;
		}

		// create a new checkbox
		$fld = new RadioButton($this, $name, $options);

		if(!empty($validator))            $fld->setValidator( $validator );
		if(!is_null($useArrayKeyAsValue)) $fld->useArrayKeyAsValue( $useArrayKeyAsValue );
		if(!empty($extra))                $fld->setExtra( $extra );
		if(!empty($mask))                 $fld->setMask( $mask );

		// register the field
		$this->_registerField( $name, $fld, $title );
	}

	/**
     * FormHandler::uploadField()
     *
     * Create a uploadField on the form
     *
     * @param string $title: The title of the field
     * @param string $name: The name of the field
     * @param array $config: The configuration used for the field
     * @param string $validator: The validator which should be used to validate the value of the field
     * @param string $extra: CSS, Javascript or other which are inserted into the HTML tag
     * @param string $alertOverwrite: Do we have to alert the user when he/she is going to overwrite a file?
     * @return void
     * @access public
     * @author Teye Heimans
     */
	function uploadField(
	$title,
	$name,
	$config         = array(),
	$validator      = null,
	$extra          = null,
	$alertOverwrite = null)
	{
		require_once(FH_INCLUDE_DIR.'fields/class.UploadField.php');

		// create a new uploadfield
		$fld = new UploadField($this, $name, $config);

		if(!empty($validator))        $fld->setValidator( $validator );
		if(!empty($extra))            $fld->setExtra( $extra );
		if(!is_null($alertOverwrite)) $fld->setAlertOverwrite( $alertOverwrite );

		// register the field
		$this->_registerField( $name, $fld, $title );

		// set that this form is using uploadfields
		$this->_upload[] = $name;
	}

	/**
     * FormHandler::listField()
     *
     * Create a listField on the form
     *
     * @param string $title: The title of the field
     * @param string $name: The name of the field
     * @param array $options: The options used for the field
     * @param string $validator: The validator which should be used to validate the value of the field
     * @param string $onTitle: The title used above the ON section of the field
     * @param string $offTitle: The title used above the OFF section of the field
     * @param boolean $useArrayKeyAsValue: If the array key's are the values for the options in the field
     * @param int $size: The size of the field (how many options are displayed)
     * @param string $extra: CSS, Javascript or other which are inserted into the HTML tag
     * @param string $verticalMode: Verticalmode
     * @return void
     * @access public
     * @author Teye Heimans
     */
	function listField(
	$title,
	$name,
	$options,
	$validator          = null,
	$useArrayKeyAsValue = null,
	$onTitle            = null,
	$offTitle           = null,
	$size               = null,
	$extra              = null,
	$verticalMode       = null)
	{
		require_once(FH_INCLUDE_DIR.'fields/class.SelectField.php');
		require_once(FH_INCLUDE_DIR.'fields/class.ListField.php');

		// options has to be an array
		if(!is_array($options))
		{
			trigger_error(
			"You have to give an array as value with the listfield '$name'",
			E_USER_WARNING
			);
			return;
		}

		// create a listfield
		$fld = new ListField( $this, $name, $options );

		if(!empty($validator))            $fld->setValidator( $validator );
		if(!is_null($useArrayKeyAsValue)) $fld->useArrayKeyAsValue( $useArrayKeyAsValue );
		if(!empty($size))                 $fld->setSize( $size );
		if(!empty($extra))                $fld->setExtra( $extra );
		if(!empty($onTitle))	          $fld->setOnTitle( $onTitle );
		if(!empty($offTitle))             $fld->setOffTitle( $offTitle );
		if(!empty($verticalMode))         $fld->setVerticalMode( $verticalMode );
		// register the field
		$this->_registerField( $name, $fld, $title );
	}

	/**
     * FormHandler::editor()
     *
     * Create a editor on the form
     *
     * @param string $title: The title of the field
     * @param string $name: The name of the field
     * @param string $validator: The validator which should be used to validate the value of the field
     * @param string $path: Path on the server where we have to upload the files
     * @param string $toolbar: The toolbar we have to use
     * @param string $skin: The skin to use
     * @param int $width: The width of the field
     * @param int $height: The height of the field
     * @param boolean $useArrayKeyAsValue: If the array key's are the values for the options in the field
     * @param string $extra: CSS, Javascript or other which are inserted into the HTML tag
     * @return void
     * @access public
     * @author Teye Heimans
     */
	function editor(
	$title,
	$name,
	$validator = null,
	$path      = null,
	$toolbar   = null,
	$skin      = null,
	$width     = null,
	$height    = null,
	$config    = null)
	{
		require_once(FH_INCLUDE_DIR.'fields/class.Editor.php');
		require_once(FH_FHTML_INCLUDE_DIR . 'ckeditor/ckeditor.php');

		// create a new editor
		$fld = new Editor( $this, $name );

		if(!empty($validator)) $fld->setValidator( $validator );
		if(!is_null($path))    $fld->setServerPath( $path );
		if(!empty($toolbar))   $fld->setToolbar( $toolbar );
		if(!empty($skin))      $fld->setSkin( $skin );
		if(!empty($width))     $fld->setWidth( $width );
		if(!empty($height))    $fld->setHeight( $height );
		if(is_array($config))  $fld->setConfig( $config );

		// register the field
		$this->_registerField( $name, $fld, $title );
	}

	/**
     * FormHandler::dateField()
     *
     * Create a dateField on the form
     *
     * @param string $title: The title of the field
     * @param string $name: The name of the field
     * @param string $validator: The validator which should be used to validate the value of the field
     * @param boolean $required: If the field is required to fill in or can the user leave it blank
     * @param string $mask: How do we have to display the fields? These can be used: d, m and y.
     * @param string $interval: The interval between the current year and the years to start/stop.Default the years are beginning at 90 yeas from the current. It is also possible to have years in the future. This is done like this: "90:10" (10 years in the future).
     * @param string $extra: CSS, Javascript or other which are inserted into the HTML tag
     * @return void
     * @access public
     * @author Teye Heimans
     */
	function dateField(
	$title,
	$name,
	$validator = null,
	$required  = null,
	$mask      = null,
	$interval  = null,
	$extra     = null)
	{
		require_once(FH_INCLUDE_DIR.'fields/class.SelectField.php');
		require_once(FH_INCLUDE_DIR.'fields/class.TextField.php');
		require_once(FH_INCLUDE_DIR.'fields/class.DateField.php');

		// create a new datefield
		$fld = new DateField(
		$this,
		$name,
		!empty($mask) ? $mask : null,
		$required,
		$interval
		);

		if(!empty($validator))  $fld->setValidator( $validator );
		if(!empty($extra))      $fld->setExtra( $extra );

		/// register the field
		$this->_registerField( $name, $fld, $title );

		// save the field in the datefields array (special treatment! :)
		$this->_date[] = $name;
	}

	/**
     * FormHandler::jsDateField()
     *
     * Create a dateField with a jscalendar popup on the form
     *
     * @param string $title: The title of the field
     * @param string $name: The name of the field
     * @param string $validator: The validator which should be used to validate the value of the field
     * @param boolean $required: If the field is required to fill in or can the user leave it blank
     * @param string $mask: How do we have to display the fields? These can be used: d, m and y.
     * @param string $interval: The interval between the current year and the years to start/stop.Default the years are beginning at 90 yeas from the current. It is also possible to have years in the future. This is done like this: "90:10" (10 years in the future).
     * @param string $extra: CSS, Javascript or other which are inserted into the HTML tag
     * @param boolean $bIncludeJS: Should we include the js file (only needed once on a page)
     * @return void
     * @access public
     * @author Teye Heimans
     */
	function jsDateField(
	$title,
	$name,
	$validator = null,
	$required  = null,
	$mask      = null,
	$interval  = null,
	$extra     = null,
	$bIncludeJS = true
	)
	{
		require_once(FH_INCLUDE_DIR.'fields/class.SelectField.php');
		require_once(FH_INCLUDE_DIR.'fields/class.TextField.php');
		require_once(FH_INCLUDE_DIR.'fields/class.DateField.php');
		require_once(FH_INCLUDE_DIR.'fields/class.jsDateField.php');

		// create a new datefield
		$fld = new jsDateField( $this, $name, $mask, $required, $interval, $bIncludeJS );

		if(!empty($validator))  $fld->setValidator( $validator );
		if(!empty($extra))      $fld->setExtra( $extra );

		// register the field
		$this->_registerField( $name, $fld, $title );

		// save the field in the datefields array (special treatment! :)
		$this->_date[] = $name;
	}

	/**
     * FormHandler::timeField()
     *
     * Create a timeField on the form
     *
     * @param string $title: The title of the field
     * @param string $name: The name of the field
     * @param string $validator: The validator which should be used to validate the value of the field
     * @param int $format: 12 or 24. Which should we use?
     * @param string $extra: CSS, Javascript or other which are inserted into the HTML tag
     * @return void
     * @access public
     * @author Teye Heimans
     */
	function timeField(
	$title,
	$name,
	$validator = null,
	$required  = null,
	$format    = null,
	$extra     = null)
	{
		require_once(FH_INCLUDE_DIR.'fields/class.SelectField.php');
		require_once(FH_INCLUDE_DIR.'fields/class.TimeField.php');

		// create a new timefield
		$fld = new TimeField($this, $name);

		if(!empty($validator))  $fld->setValidator( $validator );
		if(!is_null($required)) $fld->setRequired( $required );
		if(!empty($format))     $fld->setHourFormat( $format );
		if(!empty($extra))      $fld->setExtra( $extra );

		// register the field
		$this->_registerField( $name, $fld, $title );
	}

	/**
	 * FormHandler::colorPicker()
	 * 
	 * Creates a colorpicker on the form
	 * 
	 * @param string $title: The title of the field
	 * @param string $name: The name of the field
	 * @param string $validator: The validator which should be used to validate the value of the field
	 * @param int $size: The size of the field
	 * @param int $maxlength: The allowed max input of the field
	 * @param string $extra: CSS, Javascript or other which are inserted into the HTML tag
	 * @return void
	 * @access public
	 * @author Johan Wiegel
	 * @since 23-10-2008
     */
	function colorPicker(
	$title,
	$name,
	$validator = null,
	$size      = null,
	$maxlength = null,
	$extra     = null)
	{
		require_once(FH_INCLUDE_DIR. 'fields/class.ColorPicker.php');

		// create the field
		$fld = new ColorPicker($this, $name);
		if(!empty($validator)) $fld->setValidator( $validator );
		if(!empty($size))      $fld->setSize( $size );
		if(!empty($maxlength)) $fld->setMaxlength( $maxlength );
		if(!empty($extra))     $fld->setExtra( $extra );

		// register the field
		$this->_registerField( $name, $fld, $title.$fld->sTitleAdd );
	}

	/**
     * FormHandler::dateTextField()
     *
     * Create a dateTextField on the form
     * Validator added by Johan Wiegel
     *
     * @param string $title: The title of the field
     * @param string $name: The name of the field
     * @param string $validator: The validator which should be used to validate the value of the field
     * @param string $mask: How do we have to display the fields? These can be used: d, m and y. (Only for DB-Field with Type 'Date')
     * @param bool $bParseOtherPresentations: try to parse other presentations of dateformat
     * @param string $extra: CSS, Javascript or other which are inserted into the HTML tag
     * @return void
     * @access public
     * @author Thomas Branius
     * @since 16-03-2010
      */	
	
	function dateTextField(
		$title,
		$name,
		$validator = null,
		$mask      = null,
		$bParseOtherPresentations = false,
		$extra     = null	
		)
	{
		require_once(FH_INCLUDE_DIR.'fields/class.TextField.php');
		require_once(FH_INCLUDE_DIR.'fields/class.DateTextField.php');

		// create a new datetextfield
		$fld = new DateTextField(
			$this,
			$name,
			!empty($mask) ? $mask : null,
			$bParseOtherPresentations
		);

		if(!empty($validator))  $fld->setValidator( $validator );
		if(!empty($extra))      $fld->setExtra( $extra );

		/// register the field
		$this->_registerField( $name, $fld, $title );

		// save the field in the datefields array (special treatment! :)
		$this->_date[] = $name;
	}
	
	/**
     * FormHandler::jsdateTextField()
     *
     * Create a dateTextField on the form
     * Validator added by Johan Wiegel
     *
     * @param string $title: The title of the field
     * @param string $name: The name of the field
     * @param string $mask: How do we have to display the fields? These can be used: d, m and y. (Only for DB-Field with Type 'Date')
     * @param bool $bParseOtherPresentations: try to parse other presentations of dateformat
     * @param boolean $bIncludeJS: Should we include the js file (only needed once on a page)
     * @param string $extra: CSS, Javascript or other which are inserted into the HTML tag
     * @param boolean $bIncludeJS: Should we include the js file (only needed once on a page)
     * @return void
     * @access public
     * @author Thomas Branius
     * @since 16-03-2010
     */
	function jsDateTextField(
		$title,
		$name,
		$validator = null,
		$mask      = null,
		$bParseOtherPresentations = false,
		$extra     = null,
		$bIncludeJS = true
		)
	{
		require_once(FH_INCLUDE_DIR.'fields/class.TextField.php');
		require_once(FH_INCLUDE_DIR.'fields/class.DateTextField.php');
		require_once(FH_INCLUDE_DIR.'fields/class.jsDateTextField.php');
		
		// create a new datetextfield
		$fld = new jsDateTextField(
			$this,
			$name,
			!empty($mask) ? $mask : null,
			$bParseOtherPresentations,
			$bIncludeJS
		);

		if(!empty($validator))  $fld->setValidator( $validator );
		if(!empty($extra))      $fld->setExtra( $extra );

		/// register the field
		$this->_registerField( $name, $fld, $title );

		// save the field in the datefields array (special treatment! :)
		$this->_date[] = $name;
	}	
	
	/*****************/
	/**** BUTTONS ****/
	/*****************/

	/**
     * FormHandler::button()
     *
     * Create a button on the form
     *
     * @param string $caption: The caption of the button
     * @param string $name: The name of the button
     * @param string $extra: CSS, Javascript or other which are inserted into the HTML tag
     * @return void
     * @access public
     * @author Teye Heimans
     */
	function button( $caption, $name = null, $extra = null)
	{
		// get new button name if none is given
		if( empty($name) )
		{
			$name = $this->_getNewButtonName();
		}

		// create new submitbutton
		$btn = new Button( $this, $name );
		$btn->setCaption( $caption );

		if(!empty($extra))
		{
			$btn->setExtra($extra);
		}

		// register the button
		$this->_registerField( $name, $btn );
	}

	/**
     * FormHandler::submitButton()
     *
     * Create a submitButton on the form
     *
     * @param string $caption: The caption of the button
     * @param string $name: The name of the button
     * @param string $extra: CSS, Javascript or other which are inserted into the HTML tag
     * @param boolean $disableOnSubmit: Disable the button when it is pressed
     * @return void
     * @access public
     * @author Teye Heimans
     */
	function submitButton( $caption = null, $name = null, $extra = null, $disableOnSubmit = null)
	{
		require_once(FH_INCLUDE_DIR.'buttons/class.SubmitButton.php');

		// get new button name if none is given
		if( empty($name) )
		{
			$name = $this->_getNewButtonName();
		}

		// create new submitbutton
		$btn = new SubmitButton( $this, $name );

		if(!empty($caption))           $btn->setCaption( $caption );
		if(!empty($extra))             $btn->setExtra( $extra );
		if(!is_null($disableOnSubmit)) $btn->disableOnSubmit( $disableOnSubmit );

		// register the button
		$this->_registerField( $name, $btn );
	}

	/**
     * FormHandler::imageButton()
     *
     * Create a imageButton on the form
     *
     * @param string $image: The image URL which should be a button
     * @param string $name: The name of the button
     * @param string $extra: CSS, Javascript or other which are inserted into the HTML tag
     * @param boolean $disableOnSubmit: Disable the button when it is pressed
     * @return void
     * @access public
     * @author Teye Heimans
     */
	function imageButton( $image, $name = null, $extra = null )
	{
		require_once(FH_INCLUDE_DIR.'buttons/class.ImageButton.php');

		// get new button name if none is given
		if( empty($name) )
		{
			$name = $this->_getNewButtonName();
		}

		// create the image button
		$btn = new ImageButton( $this, $name, $image );

		if(!empty($extra))             $btn->setExtra( $extra );

		// register the button
		$this->_registerField( $name, $btn );
	}

	/**
     * FormHandler::resetButton()
     *
     * Create a resetButton on the form
     *
     * @param string $caption: The caption of the button
     * @param string $name: The name of the button
     * @param string $extra: CSS, Javascript or other which are inserted into the HTML tag
     * @return void
     * @access public
     * @author Teye Heimans
     */
	function resetButton($caption = null, $name = null, $extra = null)
	{
		require_once(FH_INCLUDE_DIR.'buttons/class.ResetButton.php');

		// get new button name if none given
		if(empty($name))
		{
			$name = $this->_getNewButtonName();
		}

		// create new resetbutton
		$btn = new ResetButton( $this, $name );
		if(!empty($caption)) $btn->setCaption( $caption );
		if(!empty($extra))   $btn->setExtra( $extra );

		// register the button
		$this->_registerField( $name, $btn );
	}

	/**
     * FormHandler::cancelButton()
     *
     * Create a cancelButton on the form
     *
     * @param string $caption: The caption of the button
     * @param string $url: The URL to go to when the button is clicked
     * @param string $name: The name of the button
     * @param string $extra: CSS, Javascript or other which are inserted into the HTML tag
     * @return void
     * @access public
     * @author Teye Heimans
     */
	function cancelButton($caption = null, $url = null, $name = null, $extra = null)
	{
		// get new button name if none given
		if(empty($name))
		{
			$name = $this->_getNewButtonName();
		}

		if( !$url )
		{
			$url = 'history.back(-1)';
		}

		// where to go when the button is clicked...
		$extra .= preg_match('/history/', $url) ? ' onclick="'.$url.'"' : ' onclick="document.location.href=\''.$url.'\'"';

		// if no caption is given, get our own caption
		if(is_null($caption))
		{
			$caption = $this->_text( 28 );
		}

		// create new button
		$btn = new Button( $this, $name );
		$btn->setCaption( $caption );

		if(!empty($extra))
		{
			$btn->setExtra( $extra );
		}

		// register the button
		$this->_registerField( $name, $btn );
	}

	/**
     * FormHandler::backButton()
     *
     * Generate a back button to go one page back in a multi-paged form
     *
     * @param string $caption: The caption of the button
     * @param string $name: The name of the button
     * @param string $extra: CSS, Javascript or other which are inserted into the HTML tag
     * @return void
     * @access public
     * @author Teye Heimans
     */
	function backButton( $caption = null, $name = null, $extra = null)
	{
		static $setJS = false;

		// include the needed javascript file
		if( !$setJS )
		{
			$this->_setJS(FH_FHTML_DIR.'js/page_back.js', true);
			$setJS = true;
		}

		// get new button name if none given
		if(empty($name))
		{
			$name = $this->_getNewButtonName();
		}

		$extra .= ' onclick="pageBack(document.forms[\''.$this->_name.'\']);"';

		// if no caption is given, get our own caption
		if(is_null($caption))
		{
			$caption = $this->_text( 38 );
		}

		// create new button
		$btn = new Button( $this, $name );
		$btn->setCaption( $caption );

		if(!empty($extra))
		{
			$btn->setExtra( $extra );
		}

		// register the button
		$this->_registerField( $name, $btn );
	}

	/********************************************************/
	/************* LOOK & FEEL ******************************/
	/********************************************************/

	/**
     * FormHandler::setMaxLength()
     *
     * Set the maximum length of a TextArea
     *
     * @param string $field: The field for which the maximum length will be set
     * @param int $maxlength: The allowed max input length of the field
     * @param boolean $displaymessage: determines if a message is displayed with characters left
     * @return void
     * @access public
     * @author Teye Heimans
     */
	function setMaxLength( $field, $maxlength, $displaymessage = true )
	{
		static $setJSmaxlength = false;

		// check if the field exists and is a textarea
		if( !$this->fieldExists($field) || strtolower(get_class( $this->_fields[$field][1] )) != 'textarea')
		{
			trigger_error(
			'You have to declare the textarea first! '.
			'The field "'.$field.'" does not exists in the form!',
			E_USER_WARNING
			);
			return;
		}

		// check if the maxlength is numeric
		if( !is_numeric( $maxlength ) )
		{
			trigger_error( 'You have to give an numeric maxlength!', E_USER_WARNING );
			return;
		}

		// add the javascript file if not done yet
		if( !$setJSmaxlength )
		{
			$setJSmaxlength = true;
			$this->_setJS( FH_FHTML_DIR.'js/maxlength.js', true );
		}

		// set the max length PHP check
		$this->_fields[$field][1] -> setMaxLength( $maxlength, $displaymessage );
	}

	/**
     * FormHandler::parse_error_style()
     *
     * Set the style class on a by %error_style% specified element
     *
     * @param string $html: html for the field
     * @return string
     * @access public
     * @author Ronald Hulshof
     * @since 07-01-2009
     */

	function parse_error_style( $mask )
	{
		// Get element containing %error_style%
		$pattern = '/<[^<>]*%error_style%[^<>]*>/';

		if( preg_match( $pattern, $mask, $result ) )
		{
			$element = $result[0];

			// Check if class-attribute already exists in element
			if( preg_match( '/class=\"[^"]*"/', $element ) )
			{
				// Class-attribute exists; add style
				$pattern = array( '/class="/', '/\s*%error_style%\s*/' );
				$replace = array('class="error ', '');
				$new_elem = preg_replace( $pattern, $replace, $element );
				$mask = str_replace($element, $new_elem, $mask);
			}
			else
			{
				// Class-attribute does not exist; create it
				$new_elem = preg_replace('/%error_style%/', 'class="error"', $element);
				$mask = str_replace($element, $new_elem, $mask);
			}
		}
		return $mask;
	}

	/**
	 * Formhandler::parse_error_Fieldstyle
	 * 
	 * Set the error class to the field itself
	 *
	 * @param string $field
	 * @return string
	 * @access public
	 * @author Johan Wiegel
	 * @since 25-08-2009
	 */
	function parse_error_Fieldstyle( $field )
	{
		// Check if class-attribute already exists in element
		if( preg_match( '/class=\"[^"]*"/', $field ) OR preg_match( '/class=\'[^"]*\'/', $field ) )
		{
			// Class-attribute exists; add style
			$pattern = array( '/class="/', '/class=\'/' );
			$replace = array( 'class="error ', 'class=\'error ' );
			$field = preg_replace($pattern, $replace, $field);
		}
		elseif( preg_match( '/class=[^"]*/', $field ) )
		{
			// Class-attribute exists; add style
			$pattern = array( '/class=/' );
			$replace = array( 'class=error ' );
			$field = preg_replace($pattern, $replace, $field);
		}
		else
		{
			// Class-attribute does not exist; create it
			if( FH_XHTML_CLOSE != '' AND !preg_match( '/\<select /', $field ) AND !preg_match( '/\<textarea name/', $field ) )
			{
				$field = preg_replace('/\/>/', 'class="error" />', $field);
			}
			else
			{
				if( preg_match( '/\<textarea name/', $field ) )
				{
					$field = preg_replace('/<textarea /', '<textarea class="error" ', $field);
				}
				elseif( preg_match( '/\<select name/', $field ) )
				{
					$field = preg_replace('/<select /', '<select class="error" ', $field);
				}
				else
				{
					$field = preg_replace('/>/', 'class="error">', $field);
				}
			}
		}
		return $field;
	}


	/**
     * FormHandler::setHelpText()
     *
     * Set the help text for a specific field
     *
     * @param string $field: The name of the field to set the help text for
     * @param string $helpText: The help text for the field
     * @param string $helpTitle: The help title
     * @return void
     * @access public
     * @author Teye Heimans
     */
	function setHelpText( $field, $helpText, $helpTitle = null )
	{
		static $setJS = false;
		if( !FH_USE_OVERLIB )
		{
			$setJS = true;
		}

		// make sure that the overlib js file is included
		if(!$setJS)
		{
			$setJS = true;
			$this->_setJS( FH_FHTML_DIR.'overlib/overlib.js', true );
			$this->_setJS( FH_FHTML_DIR.'overlib/overlib_hideform.js', true );
		}

		// escape the values from dangerous characters
		$helpTitle = is_null($helpTitle) ? "%title% - " . $this -> _text( 41 ) : htmlentities( $helpTitle, null, 'UTF-8' );
		$helpTitle = preg_replace("/\r?\n/", "\\n", addslashes( $helpTitle ));
		$helpText  = preg_replace("/\r?\n/", "\\n", addslashes( $helpText ));

		// set the help text
		$this->_help[$field] = array(
		htmlentities( $helpText, null, 'UTF-8' ),
		$helpTitle
		);
	}

	/**
     * FormHandler::setHelpIcon()
     *
     * Set the help icon used for help messages
     *
     * @param string $helpIcon: The path to the help icon
     * @return void
     * @access public
     * @author Teye Heimans
     */
	function setHelpIcon( $helpIcon )
	{
		$this->_helpIcon = $helpIcon;
	}

	/**
     * FormHandler::addHTML()
     *
     * Add some HTML to the form
     *
     * @param string $html: The HTML we have to add to the form
     * @return void
     * @access public
     * @author Teye Heimans
     */
	function addHTML( $html )
	{
		$this->_fields[] = array( '__HTML__', $html );
	}

	/**
     * FormHandler::addLine()
     *
     * Add a new row to the form.
     *
     * @param string $data: Possible data to set into the row (line)
     * @return void
     * @access public
     * @author Teye Heimans
     */
	function addLine( $text = null )
	{
		$this->_fields[] = array( '__LINE__', sprintf( FH_LINE_MASK, $text ) );
	}

	/**
     * FormHandler::borderStart()
     *
     * Begin a new fieldset
     *
     * @param string $caption: The caption of the fieldset
     * @param string $name: The name of the fieldset
     * @param string $extra: Extra css or javascript which should be placed in the fieldset tag
     * @return void
     * @access public
     * @author Teye Heimans
     */
	function borderStart( $caption = null, $name = null, $extra = '' )
	{
		static $i = 1;

		if( empty( $name ) )
		{
			$name = 'fieldset'.$i++;
		}

		$this->_fields[] = array(
		'__FIELDSET__',
		array( $name, $caption, $extra )
		);
	}

	/**
     * FormHandler::borderStop()
     *
     * Stops a fieldset
     *
     * @return void
     * @access public
     * @author Teye Heimans
     */
	function borderStop()
	{
		$this->_fields[] = array('__FIELDSET-END__', true);
	}


	/**
     * FormHandler::useTable()
     *
     * Do we have to set the <table> tag arround the fields ?
     *
     * @param bool $setTable
     * @return void
     * @access public
     * @author Teye Heimans
     */
	function useTable( $setTable )
	{
		$this->_setTable = (bool) $setTable;

	}

	/**
     * FormHandler::setMask()
     *
     * Sets a mask for the new row of fields
     *
     * @param string $mask: The mask we have to use
     * @param int|bool $repeat: If we have to repeat the mask. When a integer is given, it will be countdown
     * @return void
     * @access public
     * @author Teye Heimans
     * @since 14-02-2008 Changed in order to also parse php as a template by Johan Wiegel
     */
	function setMask( $mask = null, $repeat = true )
	{
		// when no mask is given, set the default mask
		if(is_null($mask))
		{
			$mask = FH_DEFAULT_ROW_MASK;
		}
		// a mask is given.. is it a file ?
		else if( file_exists($mask) && is_file($mask) )  // double check of PHP bug in file_exists
		{
			// is the file readable ?
			if( is_readable($mask) )
			{
				// get the contents of the file and parse php code in it
				$mask = $this->get_include_contents($mask);
			}
			// the file is not readable!
			else
			{
				trigger_error('Could not read template '.$mask, E_USER_WARNING );
			}
		}

		// is there a third arument (the old way for disabling the table tag)
		if( func_num_args() == 3 )
		{
			// display deprectated message
			trigger_error(
			'This way of disabling the table tag is deprecated! '.
			'Use the method "useTable" instead!',
			E_USER_NOTICE
			);

			// save the var
			$this->_setTable = func_get_arg( 2 );
		}

		// save the mask
		$this->_fields[] = array( '__MASK__', array( $mask, $repeat ) );
	}

	/**
	 * Get the file contents by including it, to enable parsing of php files
	 *
	 * @param string $sFilename : the file to get/parse
	 * @return void
	 * @access public
	 * @author sid benachenhou
	 * @since 14-02-2008 added by Johan Wiegel
	 */
	function get_include_contents( $sFilename )
	{
		if( is_file( $sFilename ) )
		{
			ob_start();
			include $sFilename;
			$contents = ob_get_contents();
			ob_end_clean();
			return $contents;
		}
		return false;
	}

	/**
     * FormHandler::setErrorMessage()
     *
     * Set a spicified error message to a field
     *
     * @param string $field: The field to set the message for
     * @param string $message: The message to use when the fields value is invalid
     * @return void
     * @access public
     * @author Teye Heimans
     */
	function setErrorMessage( $field, $message, $useStyle = true )
	{
		$this->_customMsg[$field] = array( $message, $useStyle );
	}

	/**
     * FormHandler::setAutoComplete()
     *
     * Set a list of items for auto complete
     *
     * @param string $field: The field which should be auto complete
     * @param array $options: The list of options for the uto complete
     * @return void
     * @access public
     * @author Teye Heimans
     */
	function setAutoComplete( $field, $options )
	{
		static $setJS = false;

		// check if the field exists and is a textfield
		if( !$this->fieldExists($field) || strtolower(get_class( $this->_fields[$field][1] )) != 'textfield')
		{
			trigger_error(
			'You have to declare the textfield first! '.
			'The field "'.$field.'" does not exists in the form!',
			E_USER_WARNING
			);

			return;
		}

		// check if the options are correct
		if( !is_array( $options ) )
		{
			trigger_error( 'You have to give an array as options!', E_USER_WARNING );
			return;
		}

		// add the javascript file if not done yet
		if( !$setJS )
		{
			$setJS = true;
			$this->_setJS( FH_FHTML_DIR.'js/autocomplete.js', true );
		}

		// create the javascript array
		$js = $field.'_values = [';
		foreach( $options as $option )
		{
			$js .= '"'.htmlentities($option) .'", ';
		}
		$this->_setJS( substr($js, 0, -2)."];\n" );

		// add the javascript to the fields "extra" argument
		$this->_fields[$field][1]->_sExtra .= " onkeypress='return FH_autocomplete(this, event, ".$field."_values);' ";
	}

	/**
     * FormHandler::setAutoComplete()
     *
     * Set a list of items for auto complete after specified character
     *
     * @param string $field: The field which should be auto complete
	 * @param string $after: The character after wicht auto completion will start
     * @param array $options: The list of options for the uto complete
     * @return void
     * @access public
     * @author Rob Geerts
	 * @since 12-02-2008 ADDED BY Johan Wiegel
     */
	function setAutoCompleteAfter( $field, $after, $options )
	{
		static $setJS = false;

		// check if the field exists and is a textfield
		if( !$this->fieldExists($field) || strtolower(get_class( $this->_fields[$field][1] )) != 'textfield')
		{
			trigger_error(
			'You have to declare the textfield first! '.
			'The field "'.$field.'" does not exists in the form!',
			E_USER_WARNING
			);

			return;
		}

		// check if the options are correct
		if( !is_array( $options ) )
		{
			trigger_error( 'You have to give an array as options!', E_USER_WARNING );
			return;
		}

		// add the javascript file if not done yet
		if( !$setJS )
		{
			$setJS = true;
			$this->_setJS( FH_FHTML_DIR.'js/autocomplete.js', true );
		}

		// create the javascript array
		$js = $field.'_values = [';
		foreach( $options as $option )
		{
			$js .= '"'.htmlentities($option) .'", ';
		}
		$this->_setJS( substr($js, 0, -2)."];\n" );

		// add the javascript to the fields "extra" argument
		$this->_fields[$field][1]->_sExtra .= " onkeypress='return autocompleteafter(this, event,\"".$after."\", ".$field."_values);' ";
	}
	/***/

	/**
     * FormHandler::newPage()
     *
     * Put the following fields on a new page
     *
     * @return void
     * @access public
     * @author Teye Heimans
     */
	function newPage()
	{
		$this->_fields[] = array( '__PAGE__', $this->_pageCounter++ );
	}

	/**
     * FormHandler::setTabIndex()
     *
     * Set the tab index for the fields
     *
     * @param mixed $mTabs: array or comma seperated string with the field names.
     * When an array is given the array index will set as tabindex
     * @return void
     * @access public
     * @author Teye Heimans
     */
	function setTabIndex( $tabs )
	{
		// is the given value a string?
		if( is_string( $tabs ) )
		{
			// split the commas
			$tabs = explode(',', $tabs);

			// add an empty value so that the index 0 isnt used
			array_unshift($tabs, '');
		}
		// is the given value an array
		else if( is_array( $tabs ))
		{
			// is set element 0, then move all elements
			// (0 is not a valid tabindex, it starts with 1)
			if( isset( $tabs[0]))
			{
				ksort( $tabs );
				$new = array();

				foreach( $tabs as $key => $value )
				{
					while( array_key_exists( $key, $new) || $key <= 0) $key++;
					$new[$key] = $value;
				}
				$tabs = $new;
			}
			// the tabs array is good.. just use it
		}

		// array with tabs set ?
		if( isset( $tabs ) )
		{
			// walk each tabindex
			foreach($tabs as $key => $value )
			{
				// if there is a field..
				if( !empty($value) )
				{
					$tabs[$key] = trim($value);
				}
				// no field is given, remove it's index
				else
				{
					unset($tabs);
				}
			}

			// save the tab indexes
			$this->_tabindexes = $this->_tabindexes + $tabs ;
		}
	}

	/**
     * FormHandler::setLanguage()
     *
     * Set the language we should use for error messages etc.
     * If no language is given, try to get the language defined by the visitors browser.
     *
     * @param string $language: The language we should use
     * @return void
     * @access public
     * @author Teye Heimans
     */
	function setLanguage( $sLanguage = null )
	{
		if(!_global) global $_SERVER;

		// if nog language is given, try to get it from the visitors browser if wanted
		if( is_null($sLanguage))
		{
			// auto detect language ?
			$bSet = false;
			if( FH_AUTO_DETECT_LANGUAGE )
			{
				// get all accepted languages by the browser
				$aLang = array();
				if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
				{
					foreach(explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']) as $sValue)
					{
						if(strpos($sValue, ';') !== false) {
							list($sValue, ) = explode(';', $sValue);
						}
						if(strpos($sValue, '-') !== false) {
							list($sValue, ) = explode('-', $sValue);
						}
						$aLang[] = $sValue;
					}
				}

				// set the language which formhandler supports
				while (list (, $l) = each ($aLang))
				{
					// check if the language file exists
					if( file_exists( FH_INCLUDE_DIR.'language/'.strtolower($l).'.php') )
					{
						// set the language
						$this->setLanguage( $l );
						$bSet = true;
						break;
					}
				}
			}

			// no language is set yet.. set the default language
			if(!$bSet)
			{
				$this->setLanguage( FH_DEFAULT_LANGUAGE );
			}
		}
		// when a language is given
		else
		{
			// check if the language does not contain any slashes or dots
			if( preg_match('/\.|\/|\\\/', $sLanguage ) )
			{
				return;
			}

			// make sure that the language is set in lower case
			$sLanguage = strtolower( $sLanguage );

			// check if the file exists
			if( file_exists(FH_INCLUDE_DIR.'language/'.$sLanguage.'.php'))
			{
				// include the language file
				include( FH_INCLUDE_DIR.'language/'.$sLanguage.'.php' );

				// load the array from the text file
				$this->_text = $fh_lang;

				// save the language
				$this->_lang = $sLanguage;
			}
			// language file does not exists
			else
			{
				trigger_error(
				'Unknown language: '.$sLanguage.'. Could not find '.
				'file '.FH_INCLUDE_DIR.'language/'.$sLanguage.'.php!',
				E_USER_ERROR
				);
			}
		}
	}

	/**
     * FormHandler::catchErrors()
     *
     * Get the errors occoured in the form
     *
     * @param boolean $display: If we still have to display the errors in the form (default this is disabled)
     * @return array of errors or an empty array if none occoured
     * @access public
     * @author Teye Heimans
     */
	function catchErrors( $display = false )
	{
		// only return the errors when the form is posted
		// and the form is not correct
		if( $this->isPosted() && !$this -> isCorrect() )
		{
			$this->_displayErrors = $display;

			// walk each field and get the error of the field
			$errors = array();
			foreach( $this->_fields as $field => $obj )
			{
				// check if it's a field (where we can get an error message from )
				$obj = $this->_fields[$field];

				if(is_object( $obj[1] ) && method_exists($obj[1], 'getError') && method_exists($obj[1], 'isValid'))
				{
					// check if it's valid
					if( !$obj[1]->isValid() )
					{
						// save the error message if there is one
						$err = $obj[1]->getError();
						if( strlen($err) > 0)
						{
							// if there is an error, check if we should use a custom error message
							if( array_key_exists($field, $this->_customMsg) )
							{
								// use the default error mask ?
								if( $this->_customMsg[$field][1] )
								{
									$err = sprintf( FH_ERROR_MASK, $this->_customMsg[$field][1], $this->_customMsg[$field][0]  );
								}
								// dont use the default error mask...
								else
								{
									$err = $this->_customMsg[$field][0];
								}
							}
							$errors[$field] = $err;
						}
					}
				}
			}


			return $errors;
		}

		return array();
	}

	/**
     * FormHandler::setFocus()
     *
     * Set the focus to a sepcific field
     *
     * @param string $field: The field which should get the focus
     * @return boolean: true if the focus could be set, false if not
     * @access public
     * @author Teye Heimans
     */
	function setFocus( $field )
	{
		// if the field is false, no focus has to be set...
		if( $field === false )
		{
			$this->_focus = false;
			return true;
		}

		// check if the field exists
		if(! $this->fieldExists( $field) )
		{
			trigger_error(
			'Could net set focus to unknown field "'.$field.'"',
			E_USER_NOTICE
			);

			return;
		}

		// some fields have other names... change it.
		switch ( strtolower( get_class($this->_fields[$field][1]) ) )
		{
			case 'jsdatefield':
			case 'datefield':
				$field = $field.'_day';
				break;

			case 'listfield':
				$field = $field.'_ListOn';
				break;

			case 'timefield':
				$field = $field.'_hour';
				break;

				// these fields cant have the focus
			case 'editor':
			case 'radiobutton':
			case 'checkbox':
			case 'hiddenfield':
				// buttons cant have the focus
			case 'submitbutton':
			case 'resetbutton':
			case 'imagebutton':
			case 'button':
				$field = null;
				break;
		}

		$this->_focus = $field;

		return !is_null( $field );
	}

	/**
	 * FormHandler::enableAjaxValidator
	 *
	 * @param boolean $mode: The new state of the AjaxValidator
	 * @param boolean $bScript: Should the library (jQuery) be included by FH
	 * @return void
	 * 
	 * @since 03-12-2008
	 * @author Johan Wiegel
	 */

	function enableAjaxValidator( $mode = true, $bScript = true)
	{
		$this->_ajaxValidator = (bool) $mode;
		$this->_ajaxValidatorScript = (bool) $bScript;
	}

	/**
	 * FormHandler::enableViewMode()
	 *
	 * Set all fields in view mode
	 *
	 * @param boolean $mode: The new state of the Forms View Mode
	 * @return void
	 */
	function enableViewMode( $mode = true)
	{
		$this->_viewMode = (bool) $mode;
	}

	/**
	 * FormHandler::isViewMode()
	 *
	 * Gets the ViewMode state
	 *
	 * @return boolean
     * @access public
     * @author Teye Heimans
	 */
	function isViewMode()
	{
		return $this->_viewMode;
	}

	/**
     * FormHandler::setFieldViewMode()
     *
     * Sets and indiviual f ields display mode
     *
     * @param string $field: The name of the field to set the display mode for
     * @param boolean $mode: True = field is view only
     * @return void
     * @access public
     */
	function setFieldViewMode( $field, $mode = true )
	{
		// does the field exists?
		if( $this -> fieldExists( $field ) )
		{
			// set the new modes
			$this -> _fields[$field][1] -> setViewMode( $mode );
		}
		// the field does not exists! error!
		else
		{
			trigger_error(
			'Error, could not find field "'. $field .'"! Please define the field first!',
			E_USER_NOTICE
			);
		}
	}

	/**
     * FormHandler::isFieldViewMode()
     *
     * Check if the field should be displayed as view only
     *
     * @param string $field: The field to check
     * @return boolean
     * @access public
     */
	function isFieldViewMode( $field )
	{
		// does the field exists?
		if( $this -> fieldExists( $field ) && is_object( $this -> _fields[$field][1] ) && method_exists( $this -> _fields[$field][1], 'getViewMode' ) )
		{
			// get the mode
			return $this -> _fields[$field][1] -> getViewMode();
		}
		// the field does not exists! error!
		else
		{
			trigger_error(
			'Error, could not find field "'. $field .'"! Please define the field first!',
			E_USER_NOTICE
			);
		}
	}

	/**
	 * FormHandler::setTableSettings()
	 *
	 * @param int width
	 * @return void
	 * @author Teye Heimans
	 */
	function setTableSettings(
	$width       = null,
	$cellspacing = null,
	$cellpadding = null,
	$border      = null,
	$extra       = '')
	{
		// set the default
		if( is_null($width ))          $width       = FH_DEFAULT_TABLE_WIDTH;
		if( !is_numeric($cellspacing)) $cellspacing = FH_DEFAULT_TABLE_CELLSPACING;
		if( !is_numeric($cellpadding)) $cellpadding = FH_DEFAULT_TABLE_CELLPADDING;
		if( !is_numeric($border))      $border      = FH_DEFAULT_TABLE_BORDER;

		// save the table settings
		$this->_tableSettings = array(
		'width'       => $width,
		'cellspacing' => $cellspacing,
		'cellpadding' => $cellpadding,
		'border'      => $border,
		'extra'       => $extra
		);
	}


	/********************************************************/
	/************* DATA HANDLING ****************************/
	/********************************************************/

	/**
     * FormHandler::getValue()
     *
     * Alias for the function value
     *
     * @param string $field: The field which value we have to return
     * @return string
     * @access public
     * @author Teye Heimans
     */
	function getValue( $field )
	{
		return $this->value( $field );
	}

	/**
     * FormHandler::getAsArray()
     *
     * Return the value of a datefield as an array: array(y,m,d)
     *
     * @param string $datefield: return the value of the datefield as an array
     * @return array
     * @access public
     * @author Teye Heimans
     */
	function getAsArray( $datefield )
	{
		// check if the datefield exists
		if( in_array($datefield, $this->_date ) )
		{
			return $this->_fields[$datefield][1]->getAsArray();
		}
		// the datefield does not exists
		else
		{
			trigger_error(
			'The datefield "'.$datefield.'" does not exists!',
			E_USER_NOTICE
			);

			return false;
		}
	}

	/**
     * FormHandler::value()
     *
     * Get the value of the requested field
     *
     * @param string $field: The field which value we have to return
     * @return string
     * @access public
     * @author Teye Heimans
     */
	function value( $field )
	{
		if(!_global) global $_POST;

		// is it a field?
		if( isset( $this->_fields[$field] ) && is_object($this->_fields[$field][1]) && method_exists($this->_fields[$field][1], 'getvalue')  )
		{
			return $this->_fields[$field][1]->getValue();
		}
		// is it an user added value ?
		else if( isset($this->_add[$field]) )
		{
			return $this->_add[$field];
		}
		// _chache contains the values of the fields after flush() is called
		// (because then all objects are removed from the memory)
		else if( isset( $this->_cache[$field]) )
		{
			return $this->_cache[$field];
		}
		// is it a set value of a field which does not exists yet ?
		else if( isset( $this->_buffer[$field]) )
		{
			return $this->_buffer[$field][1];
		}
		// is it a value from the $_POST array ?
		else if( isset( $_POST[$field] ) )
		{
			// give a notice
			//trigger_error(
			//  'Notice: the value retrieved from the field "'.$field.'" could '.
			//  'only be fetched from the $_POST array. The field is not found in the form...',
			//  E_USER_NOTICE
			//);

			return $_POST[$field];
		}

		trigger_error(
		'Try to get the value of an unknown field "'.$field.'"!',
		E_USER_WARNING
		);

		return null;
	}

	/**
     * FormHandler::setValue()
     *
     * Set the value of the spicified field
     *
     * @param string $field: The field which value we have to set
     * @param string $value: The value we have to set
     * @param boolean $overwriteCurrentValue: Do we have to overwrite the current value of the field (posted value)
     * @return void
     * @access public
     * @author Teye Heimans
     */
	function setValue( $sField, $sValue, $bOverwriteCurrentValue = false )
	{
		// check if the field exists
		if( $this->fieldExists( $sField ) )
		{
			// if the field does not exists in the database
			if( $bOverwriteCurrentValue || !$this->isPosted() ||
			// only set the value if the page is not 'done' yet, otherwise
			// we will overwrite it
			$this->_curPage < $this->_pageCounter )
			{
				$this->_fields[$sField][1]->setValue( $sValue );
			}
		}
		// the field does not exists. Save the value in the buffer.
		// the field will check this buffer and use it value when it's created
		else
		{
			// save the data untill the field exists
			$this->_buffer[$sField] = array( $bOverwriteCurrentValue, $sValue );
		}
	}

	/**
     * FormHandler::addValue()
     *
     * Add a value to the data array which is going
     * to be saved/used in the oncorrect & onsaved functions
     *
     * @param string $field: The field which value we have to set
     * @param string $value: The value we have to set
     * @param boolean $sqlFunction: Is the value an SQL function ?
     * @return void
     * @access public
     * @author Teye Heimans
     */
	function addValue($field, $value, $sqlFunction = false)
	{
		// save the added value
		$this->_add[$field] = $value;

		// add to the sql list if the value is a sql function
		if( $sqlFunction )
		{
			$this->_sql[] = $field;
		}
	}

	/**
     * FormHandler::onCorrect()
     *
     * Set the function which has to be called when the form is correct
     *
     * @param string $callback: The name of the function
     * @return void
     * @access public
     * @author Teye Heimans
     */
	function onCorrect( $callback )
	{
		// is the given value a string ?
		if(!is_array($callback))
		{
			// does the function exists ?
			if( function_exists($callback) )
			{
				$this->_onCorrect = $callback;
			}
			// the given callback function does not exists
			else
			{
				trigger_error(
				'Error, the onCorrect function "'.$callback.'" does not exists!',
				E_USER_ERROR
				);
			}
		}
		// we have to call a method
		else
		{
			// check if the method exists in the given object
			if( method_exists($callback[0], $callback[1]) )
			{
				$this->_onCorrect = $callback;
			}
			// the method does not exists
			else
			{
				trigger_error(
				'Error, the onCorrect method "'.$callback[1].'" does not exists in the given object'.
				(is_object($callback[0]) ? ' "'.get_class($callback[0]).'"!' : '!'),
				E_USER_ERROR
				);
			}
		}
	}

	/**
     * FormHandler::setError()
     *
     * Set a specified error to a field
     *
     * @param string $field: The field to set the error for
     * @param string $error: The error message to use
     * @return boolean: Returns the success of the operation
     * @access public
     * @author Filippo Toso - filippotoso@libero.it
     */

	function setError( $field, $error )
	{
		if ( isset( $this->_fields[$field][1] ) )
		{
			$this -> _fields[$field][1] -> setError( $error );
			$this -> _fields[$field][1] -> _isValid = false;
			return true;
		}

		return false;
	}


	/********************************************************/
	/************* GENERAL **********************************/
	/********************************************************/

	/**
     * FormHandler::getFileInfo()
     *
     * Get the file info af an uploaded file
     *
     * @param string $uploadfield: the name of the uploadfield
     * @return array file info
     * @access public
     * @author Teye Heimans
     */
	function getFileInfo( $uploadfield )
	{
		// does the field exists ?
		if( $this -> fieldExists( $uploadfield ) )
		{
			// is it an uploadfield ?
			$obj = &$this -> _fields[$uploadfield][1];
			if( strtolower( get_class( $obj ) ) == 'uploadfield' )
			{
				// check if there is an file uploaded
				if( $obj -> isUploaded() )
				{
					// return the file info
					return $obj -> getFileInfo();
				}
			}
			// the field is not an uploadfield
			else
			{
				trigger_error(
				'Error, the field "'.$uploadfield.'" is not an uploadfield!',
				E_USER_NOTICE
				);
			}
		}
		// the field does not exists
		else
		{
			trigger_error(
			'Error, the uploadfield "'.$uploadfield.'" does not exists!',
			E_USER_NOTICE
			);
		}

		// if we come here, something went wrong. Return empty array
		return array();
	}

	/**
     * FormHandler::isUploaded()
     *
     * Check if the given uploadfield has a file which is uploaded
     *
     * @param string $uploadfield: the name of the uploadfield
     * @return boolean
     * @access public
     * @author Teye Heimans
     */
	function isUploaded( $uploadfield )
	{
		// does the field exists ?
		if( $this -> fieldExists( $uploadfield ) )
		{
			// is it an uploadfield ?
			$obj = &$this -> _fields[$uploadfield][1];
			if( strtolower( get_class( $obj ) ) == 'uploadfield' )
			{
				// check if there is an file uploaded
				return $obj -> isUploaded();
			}
			// the field is not an uploadfield
			else
			{
				trigger_error(
				'Error, the field "'.$uploadfield.'" is not an uploadfield!',
				E_USER_NOTICE
				);
			}
		}
		// the field does not exists
		else
		{
			trigger_error(
			'Error, the uploadfield "'.$uploadfield.'" does not exists!',
			E_USER_NOTICE
			);
		}

		// if we come here, something went wrong. Return false
		return false;
	}

	/**
	 * FormHandler::getLastSubmittedPage()
	 *
	 * Returns the page number of the last submitted page of the form
	 * 
	 * @return int
	 * @access public
	 * @author Remco van Arkelen & Johan Wiegel
	 * @since 21-08-2009
	 */
	function getLastSubmittedPage()
	{
		return $this->getPage();
	}

	/**
     * FormHandler::getPage()
     *
     * Returns the page number of the last submitted page the form (when getPage is called)
     *
     * @return int
     * @access public
     * @author Teye Heimans
     */
	function getPage()
	{
		return $this->_pageCounter;
	}

	/**
     * FormHandler::getCurrentPage()
     *
     * Returns the current page number of the current form (used when newPage is used!)
     *
     * @return int
     * @access public
     * @author Teye Heimans
     */
	function getCurrentPage()
	{
		return $this->_curPage;
	}

	/**
     * FormHandler::linkSelectFields()
     *
     * Link de given selectfields (load the values dynamicly)
     *
     * @param string $filename: the name of the file which will load the new values for the select field
     * @param string $fields: the name of the first dynamic select field.
     * @param ...: More fields which are linked to eachother
     * @return null
     * @access public
     * @author Teye Heimans
     */
	function linkSelectFields( $filename, $fields )
	{
		static $setJS = false;

		$page = $this -> isPosted() && !$this -> isCorrect() ? $this->_curPage - 1  : $this -> _curPage ;

		// when we are not at the correct page, do nothing
		// added && $this - > isCorrect() in order to keep validation 07-10-2009 JW
		if( $page != ($this -> _pageCounter-1 < 1 ? 1 : $this -> _pageCounter-1) && $this -> isCorrect() )
		{
			return ;
		}

		// add the javascript file if not done yet
		if( !$setJS )
		{
			$setJS = true;
			$this->_setJS(FH_FHTML_DIR.'js/linked_select.js', true);
		}

		// walk all arguments
		$js = '';
		$values = array();
		for( $i = 1; $i < (func_num_args()-1); $i++)
		{
			// get the "parent" and "child" field
			$fld1 = func_get_arg($i);
			$fld2 = func_get_arg($i+1);
			$extra = '';

			// extra arguments given ?
			if( is_array( $fld1 ) )
			{
				$arr = $fld1;
				$fld1 = array_shift( $arr );

				// walk all "extra" arguments
				while( $item = array_shift( $arr ) )
				{
					// is this argument a field? Then load the "db" value
					if( $this->fieldExists( $item ) )
					{
						$extra .= '&'.$item.'="+document.forms["'.$this->getFormName().'"].elements["'.$item.'"].value+"';
					}
					// just load the extra argument, it's a js string
					else
					{
						$extra .= $item;
					}
				}
			}
			if( is_array( $fld2 ) ) list( $fld2, ) = $fld2;

			// make sure that the fields exists
			if( !$this->fieldExists( $fld1) )
			{
				trigger_error(
				'Error, the field "'.$fld1.'" could not be found in the form!',
				E_USER_NOTICE
				);
				return false;
			}
			// make sure that the fields exists
			if( !$this->fieldExists( $fld2) )
			{
				trigger_error(
				'Error, the field "'.$fld2.'" could not be found in the form!',
				E_USER_NOTICE
				);
				return false;
			}

			// values opslaan
			$values[] = $this->getValue( $fld2 );

			// change the name of a listfield to {$fieldname}_ListOff[]
			if( strtolower(get_class($this->_fields[$fld1][1])) == 'listfield')
			{
				$fld1 .= '_ListOff';
			}
			if( strtolower(get_class($this->_fields[$fld2][1])) == 'listfield')
			{
				$fld2 .= '_ListOff';
			}

			// if this is the first field
			if( $i == 1)
			{
				$jsAfter =
				"// load the first item of the dynamic select fields\n".
				"attach".$fld1."(";
			}

			// create the javascript for dynamic loading..
			$func  = $i < ( func_num_args() - 2 ) ? 'attach'.$fld2 : 'null';
			$param = isset($this->edit) && $this->edit ? '&value=' . $values[count($values)-1] : '';
			$param .= $extra;

			$js.=
			'function attach'.$fld1.'( aArgs, sValue ) {'."\n".
			'    attachelement("'.$fld1.'", "change", load'.$fld2.');'."\n".
			'    load'.$fld2.'( aArgs, sValue );'."\n".
			'}'."\n\n".
			'function load'.$fld2.'( aArgs, sValue ) {'."\n".
			'    value = GetElement("'.$fld1.'").value;'."\n".
			//'    value = document.forms["'.$this->getFormName().'"].elements["'.$fld1.'"].value;'."\n".
			//'    //GetElement("'.$fld2.'").innerHTML = "loading";'."\n".
			'    loadexternal('."\n".
			'      "'.$filename.'",'."\n".
			'      "linkselect=true&filter="+value+"&field='.$fld2.$param.'",'."\n".
			'      "'.$fld2.'",'."\n". //display
			'      '.$func.",\n".
			'      aArgs,'."\n".
			'      sValue'."\n".
			'    )'."\n".
			'}'."\n";
		}

		// add the value of the last field to the values array
		//$values[] = $this->getValue( $fld2 );

		// finalize the js to load the values
		if( !empty( $jsAfter ) )
		{
			$jsAfter .= " new Array(  ";
			foreach( $values as $value )
			{
				if( is_array( $value ) )
				{
					$jsAfter .= " new Array(  ";
					foreach( $value as $item )
					{
						$jsAfter .= "'".addslashes($item)."', ";
					}
					$jsAfter = substr( $jsAfter, 0, -2) . "), ";
				}
				else
				{
					$jsAfter .= "'".addslashes($value)."', ";
				}
			}

			$jsAfter = substr( $jsAfter, 0, -2) ."));\n\n";

			$this -> _setJS( $jsAfter, 0, 0);
		}

		// set the js..
		$this->_setJS( $js, false );
	}

	/**
     * FormHandler::setDynamicOptions()
     *
     * Static: Make a javascript array of the given php array. This is
     * used for dynamic select fields
     *
     * @param array $options: the new options for the select field
     * @return null
     * @access public
     * @author Teye Heimans
     */
	function setDynamicOptions( $options, $useArrayKeyAsValue = true )
	{

		$output = 'var options = Array('." \n";

		// generate a javascript array from the given array
		foreach( $options as $key => $value )
		{
			$key = $useArrayKeyAsValue ? $key : $value;
			$output .= '  Array("'.addslashes($key).'", "'.addslashes($value).'"),'."\n";
		}

		$output = substr( $output, 0, -2 );
		$output .= "\n);\n";

		echo $output;
	}

	/**
     * FormHandler::getTitle()
     *
     * Return the title of the given field name
     *
     * @param string $sField: The fieldname where to retrieve the title from
     * @return string
     * @access public
     * @author Teye Heimans
     */
	function getTitle( $sField )
	{
		// check if the field exists
		if( isset($this->_fields[$sField]) && is_object( $this->_fields[$sField][1] ))
		{
			// check if the field is a child of the "field" class
			if( is_subclass_of( $this->_fields[$sField][1], 'field') )
			{
				// return the title
				return $this->_fields[$sField][0];
			}
			else
			{
				// print an error message
				$sClass = strtolower( get_class( $this->_fields[$sField][1] ) );
				trigger_error(
				'Error, cannot retrieve title of this kind of field: '.$sClass,
				E_USER_WARNING
				);
			}
		}
		// the given field does not exists!
		else
		{
			trigger_error(
			'Could not find field "'.$sField.'"',
			E_USER_WARNING
			);
		}

		return null;
	}

	/**
     * FormHandler::getLanguage()
     *
     * Return the language used for the form
     *
     * @return string: the language
     * @access public
     * @author Teye Heimans
     */
	function getLanguage()
	{
		return $this->_lang;
	}

	/**
     * FormHandler::fieldExists()
     *
     * Check if the field exists in the form
     *
     * @param string $sField: The field to check if it exists in the form or not
     * @return boolean
     * @access public
     * @author Teye Heimans
     */
	function fieldExists( $sField )
	{
		return array_key_exists( $sField, $this->_fields );
	}

	/**
     * FormHandler::getFormName()
     *
     * Return the name of the form
     *
     * @return string: the name of the form
     * @access public
     * @author Teye Heimans
     */
	function getFormName()
	{
		return $this->_name;
	}

	/**
     * FormHandler::getJavascriptCode()
     *
     * Return the needed javascript code for this form
     *
     * @param $header: Return the javascript code for in the header (otherwise the javascript code which hase to be beneath the form will be returned)
     * @return string: the needed javascript code for this form
     * @access public
     * @author Teye Heimans
     * 
     * @since 17-08-2009 removed static before $return in order to handle multiple forms on a page. JW
     */
	function getJavascriptCode( $header = true )
	{
		$return = array( 0 => false, 1 => false );;

		$s = $header ? 0 : 1;

		// if the javascript is not retrieved yet..
		if( !$return[$s] )
		{
			// generate the js "files" script
			$result = '';
			if( isset($this->_js[$s]['file']) && is_array($this->_js[$s]['file']) )
			{
				foreach( $this->_js[$s]['file'] as $line )
				{
					$result .= '<script type="text/javascript" src="'.$line.'"></script>'."\n";
				}
			}
			// generate the other js script
			if( isset($this->_js[$s]['code']) && is_array($this->_js[$s]['code']) )
			{
				$result .= '<script type="text/javascript">'."\n";
				foreach( $this->_js[$s]['code'] as $code )
				{
					$result .= $code;
				}
				$result .= "</script>\n";
			}

			$return[$s] = true;
			return $result;
		}

		return '';
	}

	/**
     * FormHandler::getAsMailBody()
     *
     * Returns the values of the form as mail body
     *
     * @param string $mask: The mask which should be used for creating the mail body
     * @return string
     * @access public
     * @author Teye Heimans
     * @since 25/11/2005
     */
	function getAsMailBody( $mask = null )
	{
		// TODO
		// replacement of %field% and of %{fieldname}%
		$loader = new MaskLoader();
		$loader -> setMask( $mask );

		// create the search and replace strings
		$search  = array();
		$replace = array();

		// walk all elements in this form
		reset( $this->_fields );
		$mail = '';
		while( list( $name, $fld ) = each( $this->_fields) )
		{
			// only use it in the mail if it has a view value (the fields)
			if( is_object( $fld[1] ) && method_exists($fld[1], 'getViewValue') && $name != $this->_name.'_submit')
			{
				// search and replace the %field% %value% items
				$loader -> setSearch( array( '/%field%/', '/%value%/') );

				$mail .= $loader -> fill(
				array(
				$name,
				$fld[1]->getViewValue()
				)
				);

				// add the %{fieldname}% seach item to the search string for later...
				$search[]  = '/%'.$name.'%/';
				$replace[] = $fld[1]->getViewValue();
			}
		}

		// add the user added values to the search and replace arrays
		foreach ( $this->_add as $name => $value )
		{
			$search[]  = '/%'.$name.'%/';
			$replace[] = $value;
		}

		$loader -> setSearch( $search );

		// check if there is still something to fill
		if( !$loader -> isFull() )
		{
			$mail .= $loader -> fill( $replace, -1 );
		}

		// get possible half filled mask
		$mail .= $loader -> fill();

		return $mail;
	}

	/**
     * FormHandler::resizeImage()
     *
     * Resize the image uploaded in the given field
     *
     * @param string $field: The field where the image is uploaded
     * @param string $saveAs: How the image has to be saved (if not given, the original wil be overwritten)
     * @param int $maxWidth: The maximum width of the resized image
     * @param int $maxHeight: the maximum height of the resized image
     * @param int $quality: the quality of the resized image
     * @param bool $constrainProportions: Keep the proportions when the image is resized?
     * @return void
     * @access public
     * @author Teye Heimans
     */
	function resizeImage( $field, $saveAs = null, $maxWidth = null, $maxHeight = null, $quality = null, $constrainProportions = true )
	{
		require_once( FH_INCLUDE_DIR.'includes/class.ImageConverter.php' );

		// is gd enabled ?
		if( ! ImageConverter::GDVersion() )
		{
			trigger_error(
			'Error! To use the function resizeImage you have to enable GD Libary!',
			E_USER_WARNING
			);
			return;
		}

		// set some default vars if none given
		if(is_null($maxWidth))  $maxWidth  = FH_DEFAULT_RESIZE_WIDTH;
		if(is_null($maxHeight)) $maxHeight = $maxWidth;

		// save the settings
		$this->_convert[$field]['resize'][] = array( $saveAs, $maxWidth, $maxHeight, $quality, $constrainProportions );
	}

	/**
     * FormHandler::mergeImage()
     *
     * Merge a image uploaded in the given field with another image
     *
     * @param string $field: The field where the image is uploaded
     * @param string $merge: The image which we should merge
     * @param int $align: The align of the merge image (eg: left, center, right)
     * @param int $valign: The vertical align of the merge image( eg: top, middle, bottom)
     * @return void
     * @access public
     * @author Teye Heimans
     */
	function mergeImage( $field, $merge, $align = 'center', $valign = 'bottom', $transparantColor = null )
	{
		require_once(FH_INCLUDE_DIR.'includes/class.ImageConverter.php');

		// is gd enabled ?
		if( ! ImageConverter::GDVersion() )
		{
			trigger_error(
			'Error! To use the function mergeImage you have to enable GD Libary!',
			E_USER_WARNING
			);
			return;
		}

		// save the settings
		$this->_convert[$field]['merge'][] = array( $merge, $align, $valign, $transparantColor );
	}

	/**
     * FormHandler::checkPassword()
     *
     * Preform a password check on 2 password fields:
     * - both values are the same
     * - the values are longer then a minimum length (configured in the config file)
     * - on an add-form, the fields are required
     * - on an edit-form, the fields can be left empty, and the old password will stay (no changes will take place)
     *
     * @param string $field1: The first password field we should check
     * @param string $field2: The second password field we should check
     * @param boolean $setEditMsg: Should a message beeing displayed in an edit form that when leaving the fields blank the current passwords will be kept?
     * @return void
     * @access public
     * @author Teye Heimans
     */
	function checkPassword($field1, $field2, $setEditMsg = true)
	{
		// check if the fields exists and that they are both passfields
		if( !$this->fieldExists( $field1 ) || !$this->fieldExists( $field2) ||
		strtolower( get_class( $this->_fields[$field1][1] ) ) != 'passfield' ||
		strtolower( get_class( $this->_fields[$field2][1] ) ) != 'passfield')
		{
			trigger_error('Error: unknown field used in checkPassword!');
			return;
		}

		// add some text to notify the user that he only has to enter his
		// password when he wants to change it
		if( isset($this->edit) && $this->edit && $setEditMsg )
		{
			$this->_fields[$field1][1]->setPre( $this->_text( 25 ) );
		}

		// is the form posted and this page is posted in case of mulitple page form.
		if( $this->isPosted() && ($this->getPage() == $this->getCurrentPage()) )
		{
			// let passfield 1 check if it matches passfield 2
			$this->_fields[$field1][1]->checkPassword( $this->_fields[$field2][1] );
		}
	}

	/**
     * FormHandler::isPosted()
     *
     * If the form is posted
     *
     * @return boolean: if the form is posted or not
     * @access public
     * @author Teye Heimans
     */
	function isPosted()
	{
		return $this->_posted;
	}

	/**
     * FormHandler::isCorrect()
     *
     * Return if the form is filled correctly (for the fields which are set!)
     *
     * @return boolean: the form values valid or not
     * @access public
     * @author Teye Heimans
     */
	function isCorrect()
	{
		if( !_global) global $_POST;

		$result = true;

		foreach( $this->_fields as $id => $data )
		{
			// check if the fields are valid
			if( is_object( $this->_fields[$id][1] ) && method_exists( $this->_fields[$id][1], 'isvalid') && $this->_fields[$id][1]->isValid() !== true)
			{
				// the field is not valid. If the focus is not set yet, set the focus to this field
				if( is_null($this->_focus) )
				{
					$this->setFocus( $id );
				}
				$result = false;

			}
			// if multiple pages are used, only make sure that
			// all pages untill the current page are correct
			else if( $data[0] == '__PAGE__' && $this->_curPage == $data[1] )
			{
				break;
			}
		}

		return $result;
	}

	/**
     * FormHandler::flush()
     *
     * Prints or returns the form
     *
     * @return string: the form or null when the form should be printed
     * @access public
     * @author Teye Heimans
     */
	function flush( $return = false )
	{
		if( $this->_ajaxValidator === true )
		{
			require_once( FH_INCLUDE_DIR . 'includes/class.AjaxValidator.php' );
			$oAjaxValidator = new AjaxValidator( $this->_ajaxValidatorScript );
			$oAjaxValidator->CreateObservers( $this );
		}

		// when the form is not posted or the form is not valid
		if( !$this->isPosted() || !$this->isCorrect() )
		{
			// check if a value is set of an unknown field
			if( sizeof( $this->_buffer ) > 0 )
			{
				// error messages for the values for unknown fields
				foreach($this->_buffer as $sField => $a)
				{
					trigger_error('Value set of unknown field "'.$sField.'"', E_USER_WARNING );
				}
			}

			// get the form
			$form = $this->_getForm();
		}
		// when the form is not totaly completed yet (multiple pages)
		else if( $this->_curPage < $this->_pageCounter )
		{
			// upload and convert uploads
			$this->_handleUploads();

			// get the next form
			$form = $this->_getForm( $this -> _curPage + 1 );
		}
		// form in view mode
		elseif($this->isViewMode() == true)
		{
		    $form = $this->_getForm();
		} 
		// when the form is valid
		else
		{
			// upload and convert uploads
			$this->_handleUploads();

			// generate the data array
			$data = array();
			reset( $this->_fields );
			while( list( $name, $fld ) = each( $this->_fields) )
			{
				if(is_object($fld[1]) && method_exists($fld[1], 'getValue') && $name != $this->_name.'_submit')
				{
					$data[$name] = $fld[1]->getValue();
				}
			}

			// add the user added data to the array
			$data = array_merge( $data, $this->_add );

			// call the users oncorrect function
			if(!empty($this->_onCorrect))
			{
				if(is_array($this->_onCorrect))
				{
					$hideForm = call_user_func_array( array(&$this->_onCorrect[0], $this->_onCorrect[1]), array($data, &$this) );
				}
				else
				{
					$hideForm = call_user_func_array( $this->_onCorrect, array($data, &$this) );
				}
			}

			// add the user added data again to the array (could have been changed!)
			$data = array_merge( $data, $this->_add );

			// display the form again if wanted..
			if(isset($hideForm) && $hideForm === false)
			{
				$form = $this->_getForm();
			}
			// the user want's to display something else..
			else if( isset( $hideForm ) && is_string($hideForm))
			{
				$form = $hideForm;
			}
			// dont display the form..
			else
			{
				$form = '';
			}
		}

		// cache all the fields values for the function value()
		reset( $this->_fields );
		while( list( $fld, $value ) = each( $this->_fields) )
		{
			// check if it's a field
			if( is_object($this->_fields[$fld][1]) && method_exists($this->_fields[$fld][1], "getvalue"))
			{
				$this->_cache[ $fld ] = $this->_fields[$fld][1]->getValue();
			}
		}

		/*
		// remove all vars to free memory
		$vars = get_object_vars($this);
		foreach( $vars as $name => $value )
		{
		// remove all vars except these..
		if( !in_array($name, array( '_cache', 'edit', 'insert', '_posted', '_name' ) ) )
		{
		unset( $this->{$name} );
		}
		}
		*/

		// disable our error handler!
		if( FH_DISPLAY_ERRORS )
		{
			restore_error_handler();
		}

		// return or print the form
		if( $return )
		{
			return $form;
		}
		else
		{
			echo $form;
			return null;
		}
	}

	/********************************************************/
	/************* BELOW IS ALL PRIVATE!! *******************/
	/********************************************************/


	/**
     * FormHandler::_getNewButtonName()
     *
     * when no button name is given, get a unique button name
     *
     * @access private
     * @return string: the new unique button name
     * @author Teye Heimans
     */
	function _getNewButtonName()
	{
		static $counter = 1;

		return 'button'.$counter++;
	}

	/**
     * FormHandler::_setJS()
     *
     * Set the javascript needed for the fields
     *
     * @param string $js: The javascript to set
     * @param boolean $isFile: Is the setted javascript a file?
     * @param boolean $before: should the javascript be placed before or after the form?
     * @return void
     * @access private
     * @author Teye Heimans
     */
	function _setJS( $js, $isFile = false, $before = true)
	{
		$this->_js[$before?0:1][$isFile?'file':'code'][] = $js;
	}

	/**
     * FormHandler::_text()
     *
     * Return the given text in the correct language
     *
     * @param int $index: the index of the text in the textfile
     * @return string: the text in the correct language
     * @access private
     * @author Teye Heimans
     */
	function _text( $iIndex )
	{

		// is a language set?
		if( !isset( $this->_text ) || !is_array($this->_text))
		{
			trigger_error('No language file set!', E_USER_ERROR);
			return false;
		}

		// does the index exists in the language file ?
		if( !array_key_exists($iIndex, $this->_text) )
		{
			trigger_error('Unknown index '.$iIndex.' to get language string!', E_USER_NOTICE);
			return '';
		}

		// return the language string
		return $this->_text[$iIndex];
	}

	/**
     * FormHandler::_registerField()
     *
     * Register a field or button at FormHandler
     *
     * @param string $name: The name of the field (or button)
     * @param object $field: The object of the field or button
     * @return string $title: The titlt of the field. Leave blank for a button
     * @access private
     * @author Teye Heimans
     */
	function _registerField( $name, &$field, $title = null )
	{
		// if no title is known then its a button..
		if( $title === null )
		{
			$title = '__BUTTON__';
		}

		$this->_fields[$name] = array( $title, &$field );
		return $field;
	}

	/**
     * FormHandler::_registerFileName()
     *
     * Register the filenames which upload fields are using for there
     * uploaded file so that other upload fields cannot use these.
     * This is to prevent double filenames assumed by the upload fields
     *
     * @param string $sFilename: the filename to register
     * @param string $sField: the field who is registering the file
     * @return bool: false if the filename is already registered, true otherwise
     * @access private
     * @author Teye Heimans
     */
	function _registerFileName( $sFilename, $sField )
	{
		static $aFilenames = array();

		// is the filename already registerd ?
		if( isset($aFilenames[$sFilename]) && $aFilenames[$sFilename] != $sField)
		{
			return false;
		}

		// file name is still free.. register it and return true
		$aFilenames[$sFilename] = $sField;
		return true;
	}

	/**
     * FormHandler::_handleUploads()
     *
     * Private: method to handle the uploads and image convertions
     *
     * @return void
     * @access public
     * @author Teye Heimans
     */
	function _handleUploads()
	{
		// upload the uploaded files
		foreach( $this->_upload as $name )
		{
			$this->_fields[$name][1]->doUpload();
		}

		// walk all convert actions for the upload fields
		foreach( $this->_convert as $field => $convertions )
		{
			// walk all convertions for this field
			foreach( $convertions as $action => $data )
			{
				// check if the field is an upload field and that there is a file uploaded
				if( in_array($field, $this->_upload) )
				{
					// is the file uploaded ?
					if( $this->_fields[$field][1]->isUploaded() )
					{
						// get the file which is uploaded
						$file =
						$this->_fields[$field][1]->getSavePath().
						$this->_fields[$field][1]->getValue();

						// check if the file exitst
						if( !file_exists($file) )
						{
							trigger_error("Error! Could not find uploaded file $file!", E_USER_WARNING);
							unset($file);
							continue;
						}
					}
					// the uploadfield is not uploaded yet... do nothing.
					else
					{
						// go to the next field
						continue 2;
					}
				}
				// it's not a uploadfield... is it an image ?
				else if( file_exists($field) )
				{
					$file = $field;
					// unknown field or file!
				}
				else
				{
					trigger_error('Could not find field or file to convert: '.$field , E_USER_WARNING);
					continue;
				}

				// do the convert actions with the image (when the uploaded file is a jpg or png!)
				if( isset($file) && in_array( strtolower(substr( $file, -4) ), array('.jpg', '.png', 'jpeg', '.gif')) )
				{
					// create a new image converter
					$img = new ImageConverter( $file );

					// stop when a error occoured
					if( $img->getError() )
					{
						trigger_error( $img->getError(), E_USER_WARNING );
						unset( $img );
						continue;
					}

					// walk each data (there can be more of the save convertions on the save file!)
					foreach( $data as $info )
					{
						// check if an error occured
						if( $img->getError() )
						{
							// stop converting and notice the user
							trigger_error( $img->getError(), E_USER_WARNING );
							break;
						}

						switch($action)
						{
							// merge the uploaded file with a merge image
							case 'merge':
								list( $stamp, $align, $valign, $transparant ) = $info;
								$img -> doMerge( $stamp, $align, $valign, $transparant );
								break;

								// resize the uploaded file
							case 'resize':
								list( $destination, $maxX, $maxY, $quality, $proportions ) = $info;
								if( empty( $destination ) ) {
									$destination = $file;
								}

								$img -> setQuality( $quality );
								$img -> setConstrainProportions( $proportions );
								$img -> doResize( $destination, $maxX, $maxY );
								break;
						}
					}

					unset( $img );
				}

				unset( $file );
			}
		}
	}

	/**
     * FormHandler::_getForm()
     *
     * Private: get the form
     *
     * @return string: the generated form
     * @access public
     * @author Teye Heimans
     */
	function _getForm( $iDisplayPage = null )
	{
		// is no specific page requested, then get the "current" page
		if( is_null( $iDisplayPage ) )
		{
			$iDisplayPage = $this->_curPage;
		}

		// make sure that the requested page cannot be negative
		if( $iDisplayPage <= 0)
		{
			$iDisplayPage = 1;
		}

		// set the tab indexes for the fields...
		reset( $this->_tabindexes );
		ksort( $this->_tabindexes );
		while( list( $index, $field ) = each( $this->_tabindexes ))
		{
			// check if the field exists in the form ?
			if( $this->fieldExists( $field ) )
			{
				// set the tab index
				$this->_fields[$field][1]->setTabIndex( $index );
			}
			// tab index set for unknown field... trigger_error
			else
			{
				trigger_error(
				'Error, try to set the tabindex of an unknown field "'.$field.'"!',
				E_USER_NOTICE
				);
			}
		}

		// set the focus to the first (tab index) field if no focus is set yet
		if( is_null($this->_focus) )
		{
			// are there tab indexes set ?
			if( sizeof( $this->_tabindexes) > 0 )
			{
				// set the focus to the element with the lowest positive tab index
				reset( $this->_tabindexes );
				while( list( $key, $field ) = each( $this->_tabindexes ))
				if( $key >= 0 && $this->setFocus( $field ))
				break;
			}

			// no focus set yet. Set the focus to the first field
			if( is_null($this->_focus))
			{
				// is it a object (only fields + buttons are objects)
				foreach( $this->_fields as $name => $data )
				if( is_object( $this->_fields[$name][1]) && $this->setFocus( $name ))
				break;
			}
		}


		// initialize the used vars
		$hidden = '';
		$form   = '';
		$buffer = array();
		$repeat = true;
		$page   = 1;

		// start a new mask loader
		$mask   = new MaskLoader();

		// set the seach values
		$mask -> setSearch(
		array(
		'/%field%/',
		'/%error%/',
		'/%title%/',
		'/%seperator%/',
		'/%name%/',
		'/%error_id%/',
		'/%value%/',
		'/%help%/'
		)
		);

		// walk trought the fields array
		foreach( $this->_fields as $id => $field )
		{
			switch( $field[0] )
			{
				// multiple pages in this form
				case '__PAGE__':
					# why did we stop at the current page ?
					//if( $field[1] == $iDisplayPage)
					//{
					//    break;
					//}
					$page++;
					break;

					// hidden field
				case '__HIDDEN__':
					$hidden .= $field[1] -> getField() ."\n";
					$hidden .= $field[1] -> getError() ."\n";
					break;

					// new mask to set
				case '__MASK__':
					if( !isset($this->_mask) || is_null($this->_mask) || $page == $iDisplayPage )
					{
						list($this->_mask, $repeat) = $field[1];
					}
					break;

					// insert html or a line
				case '__HTML__':
				case '__LINE__':
					// but only if the html or line is on this page!
					if($page == $iDisplayPage )
					{
						$form .= $field[1];
					}
					break;

					// begin new fieldset
				case '__FIELDSET__':
					if($page == $iDisplayPage )
					{
						array_unshift( $field[1], $form );
						array_push( $buffer, $field[1] );
						$form = '';
					}
					break;

					// end new fieldset
				case '__FIELDSET-END__':
					if($page == $iDisplayPage )
					{
						if( sizeof($buffer) > 0 )
						{
							$d = array_pop($buffer);
							$form = $d[0].
							str_replace(
							array('%name%', '%caption%', '%content%', '%extra%' ),
							array($d[1], $d[2], $form, $d[3] ),
							FH_FIELDSET_MASK
							);
						}
						else
						{
							trigger_error('Fieldset is closed while there is not an open fieldset!');
						}
					}
					break;

					// default action: field or button
				default:
					// the fields are not displayed in this page..
					// set them as hidden fields in the form
					if( $page != $iDisplayPage )
					{
						// put the data of the field in a hidden field
						// buttons are just ignored
						if( $field[0] != '__BUTTON__' )
						{
							// create a new hidden field to set the field's value in
							$h = new HiddenField( $this, $id );
							$value = $field[1] -> getValue();
							$h->setValue( is_array( $value ) ? implode(', ', $value) : $value );
							$hidden .= $h -> getField() ."\n";
							unset( $h );
						}
					}
					// the field is on the current page of the form
					else
					{
						// set the mask which should be filled
						$mask -> setMask( $this->_mask );

						// easy names for the data
						$title = $field[0];
						$obj   = &$field[1];
						$name  = $id;

						// buttons don't have a title :-)
						if($title == '__BUTTON__') $title = '';

						/**
	                	 * From this point, we are collecting the data
	                	 * to fill the mask.
	                	 */

						// Get the field or button value
						// can we get a field ?
						if( is_object( $obj ) && method_exists($obj, 'getField') )
						{
							$fld = $obj -> getField();
						}
						// can we get a button ?
						else if( is_object( $obj ) && method_exists($obj, 'getButton') )
						{
							$fld = $obj -> getButton();
						}
						// ai, not a field and not a button..
						else
						{
							// trigger error ?? (TODO)
							$fld = '';
						}

						// escape dangerous characters
						$fld = str_replace( '%', '____FH-percent____', $fld );

						/**
	                	 * Get the error message for this field
	                	 */

						// get possible error message
						$error = '';
						if( $this->_displayErrors && is_object( $obj ) && method_exists($obj, 'getError') )
						{
							// custom error message set and we got an error?
							if( array_key_exists($name, $this->_customMsg) && $obj->getError() != '' )
							{
								// use the default error mask ?
								if( $this->_customMsg[$name][1] )
								{
									$error = sprintf( FH_ERROR_MASK, $name,$this->_customMsg[$name][0] );
								}
								// dont use the default error mask...
								else
								{
									$error = $this->_customMsg[$name][0];
								}
							}
							// dont use a custom error message.. just get the FH error message
							else
							{
								$error = $obj->getError();
							}
						}

						// save the error messages
						// (when the user wants to use his own error displayer)
						$this->errors[$name] = $error;

						/**
	                	 * Get the value for of the field
	                	 */
						$value = '';
						if( is_object( $obj ) &&  method_exists($obj, 'getValue') )
						{
							if( is_array($obj->getValue() ) )
							{
								$value = implode(', ', $obj->getValue());
							}
							else
							{
								$value = $obj->getValue() ;
							}
						}

						/**
	                	 * Get the help string
	                	 */
						$help = '';
						if( array_key_exists( $name, $this->_help) && !$this -> isViewMode() && !$this->isFieldViewMode($name) )
						{
							if( strpos( FH_HELP_MASK, '%s' ) )
							{
								$help = sprintf(
								FH_HELP_MASK,
								$this->_helpIcon,
								$this->_help[$name][0],
								str_replace( '%title%', addslashes( htmlentities($title) ), $this->_help[$name][1])
								);

							}
							else
							{
								$help = str_replace( array( '%helpicon%','%helptext%','%helptitle%' ),array( $this->_helpIcon,$this->_help[$name][0],str_replace( '%title%',	addslashes( htmlentities( $title ) ), $this->_help[$name][1] ) ),FH_HELP_MASK );
							}
						}

						// give the field a class error added 25-08-2009 in order to give the field the error mask
						if( $this->isPosted() == true AND $error != '' )
						{
							$fld = $this->parse_error_Fieldstyle( $fld );
						}

						// now, put all the replace values into an array
						$replace = array(
						/* %field%     */ $fld,
						/* %error%     */ $error,
						/* %title%     */ !empty($title) ? $title : "",
						/* %seperator% */ ( !strlen($title) ? '' : ':' ),
						/* %name%      */ ( !empty($name) ? $name : '' ),
						/* %error_id%  */ ( !empty($name) ? 'error_'.$name : '' ),
						/* %value%     */ $value,
						/* %help%      */ $help
						);

						// fill the mask
						$html = $mask -> fill( $replace );

						// added 07-01-2009 in order to specify which element should get the error class
						if( $this->isPosted() == true AND $error != '' )
						{
							$html = $this->parse_error_style( $html );
						}
						else
						{
							$html = str_replace( '%error_style%','',$html );
						}

						// is the mask filled ?
						if($html)
						{
							// add it the the form HTML
							$form .= str_replace('____FH-percent____', '%', $html );

							// if we don't have to repeat the current mask, use the original
							if( !$repeat )
							{
								$this->_mask = FH_DEFAULT_ROW_MASK;
							}
							// if we have to repeat the mask, repeat it and countdown
							else if( is_numeric($repeat) )
							{
								$repeat--;
							}
						}
					}
					break;
			}
		}

		// add the page number to the forms HTML
		if($this->_pageCounter > 1)
		{
			$h = new HiddenField( $this, $this->_name .'_page' );
			$h->setValue( $iDisplayPage );
			$hidden .= $h->getField() ."\n";
			unset( $h );
		}

		// get a possible half filled mask and add it to the html
		$form .= str_replace('____FH-percent____', '%', $mask-> fill( null ) );

		// delete the mask loader
		unset( $mask );

		// get occured PHP errors
		$errors = catchErrors();
		$errmsg = '';

		// walk all error messages
		foreach($errors as $error)
		{
			switch ($error['no']) {
				case E_USER_WARNING: $type = 'Warning'; break;
				case E_USER_NOTICE:  $type = 'Notice';  break;
				case E_USER_ERROR:   $type = 'Error';   break;
				default: 			 $type = 'Warning ('.$error['no'].')'; break;
			}
			$errmsg .= "<b>".$type.":</b> ".basename($error['file'])." at ".$error['line']." ". $error['text'] ."<br />\n";
		}

		// set the javascript needed for setting the focus
		if($this->_focus)
		{
			$this -> _setJS(
			"// set the focus on a specific field \n".
			"var elem = document.getElementById ? document.getElementById('".$this->_focus."'): document.all? document.all['".$this->_focus."']: false; \n".
			"if( (elem) && (elem.type != 'hidden')) {\n".
			"    try {\n".
			"      elem.focus();\n".
			"    } catch(e) {}\n".
			"}\n", 0, 0
			);
		}

		// NOTE!!
		// DO NOT REMOVE THIS!
		// You can remove the line "This form is generated by FormHandler" in the config file!!
		// DONT REMOVE THE HTML CODE BELOW! Just set FH_EXPOSE to FALSE!
		$sHeader =
		$errmsg .
		"<!--\n".
		"  This form is automaticly being generated by FormHandler v3.\n".
		"  See for more info: http://www.formhandler.net\n".
		"  This credit MUST stay intact for use\n".
		"-->\n".
		$this->getJavascriptCode( true ).
		'<form id="'.$this->_name.'" method="post" action="'.htmlentities( $this->_action ).'"'.
		( sizeof($this->_upload) > 0 ? ' enctype="multipart/form-data"':'' ).
		(!empty($this->_extra) ? " ".$this->_extra : "" ).">\n".
		'<ins>'.$hidden.'</ins>'.
		( $this->_setTable ?
		sprintf(
		"<table border='%d' cellspacing='%d' cellpadding='%d'%s>\n",
		$this->_tableSettings['border'],
		$this->_tableSettings['cellspacing'],
		$this->_tableSettings['cellpadding'],
		(!empty($this->_tableSettings['width']) ? " width='".$this->_tableSettings['width']."'" : "").
		' '.$this->_tableSettings['extra']
		) : ''
		);
		$sFooter =		( $this->_setTable ? "\n</table>\n" : '').
		(FH_EXPOSE ? 
		"<p><span style='font-family:tahoma;font-size:10px;color:#B5B5B5;font-weight:normal;'>".
		'This form is generated by </span><a href="http://www.formhandler.net" >'.
		'<span style="font-family:Tahoma;font-size:10px;color:#B5B5B5;"><strong>FormHandler</strong></span></a></p>'."\n" :'' 
		).
		"</form>\n".
		"<!--\n".
		"  This form is automaticly being generated by FormHandler v3.\n".
		"  See for more info: http://www.formhandler.net\n".
		"-->". $this -> getJavascriptCode( false );

		$search = array( '%header%', '%footer%' );
		$replace = array( $sHeader, $sFooter );

		$new_form = str_replace( $search, $replace, $form, $num_replaced );

		if( $num_replaced === 2 )
		{
			return $new_form;
		}
		else 
		{
			return $sHeader . $form . $sFooter;
		}
	}
}
?>
Back to Top