home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 2003 August / VPR0308.ISO / ADOBEREADER60 / Installer / Data1.cab / aform.js < prev    next >
Text File  |  2003-05-21  |  44KB  |  1,633 lines

  1. /*
  2.     ==========================================================================
  3.     Module: AForm.js
  4.     ==========================================================================
  5.     Pre-canned functions to map the user interface into JavaScripts.
  6.     ==========================================================================
  7.     The Software, including this file, is subject to the End User License
  8.     Agreement.
  9.     Copyright (c) 1998-2002, Adobe Systems Incorporated, All Rights Reserved.
  10.     ==========================================================================
  11. */
  12.  
  13. // The following code "exports" any strings in the list into the current scope.
  14. var esStrsToExport = new Array(
  15.     "IDS_GREATER_THAN", 
  16.     "IDS_GT_AND_LT", 
  17.     "IDS_LESS_THAN", 
  18.     "IDS_INVALID_MONTH",
  19.     "IDS_INVALID_DATE",
  20.     "IDS_INVALID_DATE2",
  21.     "IDS_INVALID_VALUE", 
  22.     "IDS_AM", "IDS_PM", 
  23.     "IDS_MONTH_INFO", 
  24.     "IDS_STARTUP_CONSOLE_MSG");
  25.  
  26. for(var nToExport = 0; nToExport < esStrsToExport.length; nToExport++)
  27.     eval(esStrsToExport[nToExport] + " = " + app.getString("EScript", esStrsToExport[nToExport]).toSource());
  28.  
  29. console.println(IDS_STARTUP_CONSOLE_MSG);
  30.  
  31. RE_NUMBER_ENTRY_DOT_SEP = new Array(
  32.     "[+-]?\\d*\\.?\\d*"
  33. );
  34. RE_NUMBER_COMMIT_DOT_SEP = new Array(
  35.     "[+-]?\\d+(\\.\\d+)?",        /* -1.0 or -1 */
  36.     "[+-]?\\.\\d+",                /* -.1 */
  37.     "[+-]?\\d+\\."                /* -1. */
  38. );
  39. RE_NUMBER_ENTRY_COMMA_SEP = new Array(
  40.     "[+-]?\\d*,?\\d*"
  41. );
  42. RE_NUMBER_COMMIT_COMMA_SEP = new Array(
  43.     "[+-]?\\d+([.,]\\d+)?",        /* -1,0 or -1 */
  44.     "[+-]?[.,]\\d+",                /* -,1 */
  45.     "[+-]?\\d+[.,]"                /* -1, */
  46. );
  47. RE_ZIP_ENTRY = new Array(
  48.     "\\d{0,5}"
  49. );
  50. RE_ZIP_COMMIT = new Array(
  51.     "\\d{5}"
  52. );
  53. RE_ZIP4_ENTRY = new Array(
  54.     "\\d{0,5}(\\.|[- ])?\\d{0,4}"
  55. );
  56. RE_ZIP4_COMMIT = new Array(
  57.     "\\d{5}(\\.|[- ])?\\d{4}"
  58. );
  59. RE_PHONE_ENTRY = new Array(
  60.     "\\d{0,3}(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}",        /* 555-1234 or 408 555-1234 */
  61.     "\\(\\d{0,3}",                                            /* (408 */
  62.     "\\(\\d{0,3}\\)(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}",    /* (408) 555-1234 */
  63.         /* (allow the addition of parens as an afterthought) */
  64.     "\\(\\d{0,3}(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}",    /* (408 555-1234 */
  65.     "\\d{0,3}\\)(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}",    /* 408) 555-1234 */
  66.     "011(\\.|[- \\d])*"                                        /* international */
  67. );
  68. RE_PHONE_COMMIT = new Array(
  69.     "\\d{3}(\\.|[- ])?\\d{4}",                            /* 555-1234 */
  70.     "\\d{3}(\\.|[- ])?\\d{3}(\\.|[- ])?\\d{4}",            /* 408 555-1234 */
  71.     "\\(\\d{3}\\)(\\.|[- ])?\\d{3}(\\.|[- ])?\\d{4}",    /* (408) 555-1234 */
  72.     "011(\\.|[- \\d])*"                                    /* international */
  73. );
  74. RE_SSN_ENTRY = new Array(
  75.     "\\d{0,3}(\\.|[- ])?\\d{0,2}(\\.|[- ])?\\d{0,4}"
  76. );
  77. RE_SSN_COMMIT = new Array(
  78.     "\\d{3}(\\.|[- ])?\\d{2}(\\.|[- ])?\\d{4}"
  79. );
  80.  
  81. /* Function definitions for the color object. */
  82.  
  83. function ColorConvert(oColor, cColorspace)
  84. {    // Converts a color to a specific colorspace.
  85.     var oOut = oColor;
  86.  
  87.     switch (cColorspace) {
  88.         case "G":
  89.             // Note that conversion to the DeviceGray colorspace is lossy in the same
  90.             // way that a color signal on a B/W TV is lossy.
  91.             if (oColor[0] == "RGB")
  92.                 oOut = new Array("G", 0.3 * oColor[1] + 0.59 * oColor[2] + 0.11 * oColor[3]);
  93.             else if (oColor[0] == "CMYK")
  94.                 oOut = new Array("G", 1.0 - Math.min(1.0, 
  95.                     0.3 * oColor[1] + 0.59 * oColor[2] + 0.11 * oColor[3] + oColor[4]));
  96.         break;
  97.         case "RGB":
  98.             if (oColor[0] == "G")
  99.                 oOut = new Array("RGB", oColor[1], oColor[1], oColor[1]);
  100.             else if (oColor[0] == "CMYK")
  101.                 oOut = new Array("RGB", 1.0 - Math.min(1.0, oColor[1] + oColor[4]), 
  102.                     1.0 - Math.min(1.0, oColor[2] + oColor[4]),
  103.                     1.0 - Math.min(1.0, oColor[3] + oColor[4]));
  104.         break;
  105.         case "CMYK":
  106.             if (oColor[0] == "G")
  107.                 oOut = new Array("CMYK", 0, 0, 0, 1.0 - oColor[1]);
  108.             else if (oColor[0] == "RGB")
  109.                 oOut = new Array("CMYK", 1.0 - oColor[1], 1.0 - oColor[2], 1.0 - oColor[3], 0); 
  110.         break;
  111.     }
  112.  
  113.     return oOut;
  114. }
  115.  
  116. function ColorEqual(c1, c2)
  117. {    // Compare two colors. 
  118.     /* The gray colorspace conversion is lossy so we avoid if possible. */
  119.     if (c1[0] == "G")
  120.         c1 = color.convert(c1, c2[0]);
  121.     else
  122.         c2 = color.convert(c2, c1[0]);
  123.  
  124.     /* Colorspace must be equal. */
  125.     if (c1[0] != c2[0])    {
  126.         return false;
  127.     }
  128.  
  129.     /* Compare the individual components. */
  130.     var nComponents = 0;
  131.         
  132.     switch (c1[0]) {
  133.         case "G":
  134.             nComponents = 1;
  135.         break;
  136.         case "RGB":
  137.             nComponents = 3;
  138.         break;
  139.         case "CMYK":
  140.             nComponents = 4;
  141.         break;
  142.     }
  143.  
  144.     for (var i = 1; i <= nComponents; i++) {
  145.         if (c1[i] != c2[i])    {
  146.             return false;
  147.         }
  148.     }
  149.  
  150.     return true;
  151. }
  152.  
  153. /* ==== Convenience Objects ==== */
  154.  
  155. /* Stock color definitions for ease of use. */
  156. color = new Object();
  157. color.equal = ColorEqual;
  158. color.convert = ColorConvert;
  159. color.transparent = new Array("T");
  160. color.black = new Array("G", 0);
  161. color.white = new Array("G", 1);
  162. color.dkGray = new Array("G", 0.25);
  163. color.gray = new Array("G", 0.5);
  164. color.ltGray = new Array("G", 0.75);
  165. color.red = new Array("RGB", 1, 0, 0);
  166. color.green = new Array("RGB", 0, 1, 0);
  167. color.blue = new Array("RGB", 0, 0, 1);
  168. color.cyan = new Array("CMYK", 1, 0, 0, 0);
  169. color.magenta = new Array("CMYK", 0, 1, 0, 0);
  170. color.yellow = new Array("CMYK", 0, 0, 1, 0);
  171.     
  172. /* Font definitions for ease of use */
  173. font = new Object();
  174. font.Times = "Times-Roman";
  175. font.TimesB = "Times-Bold";
  176. font.TimesI = "Times-Italic";
  177. font.TimesBI = "Times-BoldItalic";
  178. font.Helv = "Helvetica";
  179. font.HelvB = "Helvetica-Bold";
  180. font.HelvI = "Helvetica-Oblique";
  181. font.HelvBI = "Helvetica-BoldOblique";
  182. font.Cour = "Courier";
  183. font.CourB = "Courier-Bold";
  184. font.CourI = "Courier-Oblique";
  185. font.CourBI = "Courier-BoldOblique";
  186. font.Symbol = "Symbol";
  187. font.ZapfD = "ZapfDingbats";
  188. font.KaGo = "HeiseiKakuGo-W5-UniJIS-UCS2-H";
  189. font.KaMi = "HeiseiMin-W3-UniJIS-UCS2-H";
  190.  
  191. /* Border style definitions for ease of use */
  192. border = new Object();
  193. border.s = "solid";
  194. border.d = "dashed";
  195. border.b = "beveled";
  196. border.i = "inset";
  197. border.u = "underline";
  198.  
  199. /* Radio/Check button style definitions for ease of use */
  200. style = new Object();
  201. style.ch = "check";
  202. style.cr = "cross";
  203. style.di = "diamond";
  204. style.ci = "circle";
  205. style.st = "star";
  206. style.sq = "square"; 
  207.  
  208. /* highlight modes of on a push button */
  209. highlight = new Object();
  210. highlight.n = "none";
  211. highlight.i = "invert";
  212. highlight.p = "push";
  213. highlight.o = "outline";
  214.  
  215. /* zoom types for a document */
  216. zoomtype = new Object();
  217. zoomtype.none = "NoVary";
  218. zoomtype.fitW = "FitWidth";
  219. zoomtype.fitH = "FitHeight";
  220. zoomtype.fitP = "FitPage";
  221. zoomtype.fitV = "FitVisibleWidth";
  222. zoomtype.pref = "Preferred";
  223. zoomtype.refW = "ReflowWidth";
  224.  
  225. /* Cursor behavior in full screen mode. */
  226. cursor = new Object();
  227. cursor.visible = 0;
  228. cursor.hidden = 1;
  229. cursor.delay = 2;
  230.  
  231. /* Transition definitions. */
  232. trans = new Object();
  233. trans.blindsH        = "BlindsHorizontal";
  234. trans.blindsV        = "BlindsVertical";
  235. trans.boxI            = "BoxIn";
  236. trans.boxO            = "BoxOut";
  237. trans.dissolve        = "Dissolve";
  238. trans.glitterD        = "GlitterDown";
  239. trans.glitterR        = "GlitterRight";
  240. trans.glitterRD        = "GlitterRightDown";
  241. trans.random        = "Random";
  242. trans.replace        = "Replace";
  243. trans.splitHI        = "SplitHorizontalIn";
  244. trans.splitHO        = "SplitHorizontalOut";
  245. trans.splitVI        = "SplitVerticalIn";
  246. trans.splitVO        = "SplitVerticalOut";
  247. trans.wipeD            = "WipeDown";
  248. trans.wipeL            = "WipeLeft";
  249. trans.wipeR            = "WipeRight";
  250. trans.wipeU            = "WipeUp";
  251.  
  252. /* Icon/Text placement. */
  253. position = new Object();
  254. position.textOnly    = 0;
  255. position.iconOnly    = 1;
  256. position.iconTextV    = 2;
  257. position.textIconV    = 3;
  258. position.iconTextH    = 4;
  259. position.textIconH    = 5;
  260. position.overlay    = 6;
  261.  
  262. /* When does icon scale. */
  263. scaleWhen = new Object();
  264. scaleWhen.always    = 0;
  265. scaleWhen.never        = 1;
  266. scaleWhen.tooBig    = 2;
  267. scaleWhen.tooSmall    = 3;
  268.  
  269. /* How does icon scale. */
  270. scaleHow = new Object();
  271. scaleHow.proportional    = 0;
  272. scaleHow.anamorphic        = 1;
  273.  
  274.  
  275. /* Field display. */
  276. display = new Object();
  277. display.visible        = 0;
  278. display.hidden        = 1;
  279. display.noPrint        = 2;
  280. display.noView        = 3;
  281.  
  282. /* Permission values */
  283. permission = new Object();
  284. /* Request Objects */
  285. permission.document     = "Document";
  286. permission.page         = "Page";
  287. permission.link         = "Link";
  288. permission.bookmark     = "Bookmark";
  289. permission.thumbnail     = "Thumbnail";
  290. permission.annot         = "Annot";
  291. permission.form         = "Form";
  292. permission.signature     = "Signature";
  293.  
  294. /* Request Operations */
  295. permission.all             = "All";
  296. permission.any             = "Any";
  297. permission.create         = "Create";
  298. permission.remove         = "Delete";
  299. permission.modify         = "Modify";
  300. permission.copy         = "Copy";
  301. permission.accessible     = "Accessible";
  302. permission.select         = "Select";
  303. permission.open         = "Open";
  304. permission.secure         = "Secure";
  305. permission.print         = "Print";
  306. permission.printLowQuality     = "PrintLowQuality";
  307. permission.fillIn         = "FillIn";
  308. permission.rotate         = "Rotate";
  309. permission.crop         = "Crop";
  310. permission.summarize     = "Summarize";
  311. permission.insert         = "Insert";
  312. permission.replace         = "Replace";
  313. permission.reorder         = "Reorder";
  314. permission.fullSave     = "FullSave";
  315. permission.canImport     = "Import";
  316. permission.canExport     = "Export";
  317.  
  318. /* ==== Functions ==== */
  319.  
  320. /* these may be used a lot -- they are language independent */
  321.  
  322. AFDigitsRegExp = new RegExp();
  323. AFDigitsRegExp.compile("\\d+");
  324. AFPMRegExp = new RegExp();
  325. AFPMRegExp.compile(IDS_PM, "i");
  326. AFAMRegExp = new RegExp();
  327. AFAMRegExp.compile(IDS_AM, "i");
  328. AFTimeLongRegExp = new RegExp();
  329. AFTimeLongRegExp.compile("\\d{1,2}:\\d{1,2}:\\d{1,2}");
  330. AFTimeShortRegExp = new RegExp();
  331. AFTimeShortRegExp.compile("\\d{1,2}:\\d{1,2}");
  332. AFTimeGarbageRegExp = new RegExp();
  333. AFTimeGarbageRegExp.compile("\\d{1,2}:\\d{1,2}(:\\d{1,2})?(\\s)*(am|pm)?", "i");
  334.  
  335. function AFBuildRegExps(array)
  336. /* Takes an array of strings and turns it into an array of compiled regular
  337.  * expressions -- is used for the definitions that follow */
  338. {
  339.     var retVal = new Array();
  340.  
  341.     retVal.length = array.length;
  342.     for(var it = 0; it < array.length; it++)
  343.     {
  344.         retVal[it] = new RegExp();
  345.         retVal[it].compile(array[it], "i");
  346.     }
  347.     return retVal;
  348. }
  349.  
  350. /* these may be used a lot -- they are NOT language independent and are 
  351.  * derived from the localizable (RE_xxx) stuff above */
  352.  
  353. AFNumberDotSepEntryRegExp = AFBuildRegExps(RE_NUMBER_ENTRY_DOT_SEP);
  354. AFNumberDotSepCommitRegExp = AFBuildRegExps(RE_NUMBER_COMMIT_DOT_SEP);
  355. AFNumberCommaSepEntryRegExp = AFBuildRegExps(RE_NUMBER_ENTRY_COMMA_SEP);
  356. AFNumberCommaSepCommitRegExp = AFBuildRegExps(RE_NUMBER_COMMIT_COMMA_SEP);
  357. AFZipEntryRegExp = AFBuildRegExps(RE_ZIP_ENTRY);
  358. AFZipCommitRegExp = AFBuildRegExps(RE_ZIP_COMMIT);
  359. AFZip4EntryRegExp = AFBuildRegExps(RE_ZIP4_ENTRY);
  360. AFZip4CommitRegExp = AFBuildRegExps(RE_ZIP4_COMMIT);
  361. AFPhoneEntryRegExp = AFBuildRegExps(RE_PHONE_ENTRY);
  362. AFPhoneCommitRegExp = AFBuildRegExps(RE_PHONE_COMMIT);
  363. AFSSNEntryRegExp = AFBuildRegExps(RE_SSN_ENTRY);
  364. AFSSNCommitRegExp = AFBuildRegExps(RE_SSN_COMMIT);
  365. AFMonthsRegExp = AFBuildRegExps(IDS_MONTH_INFO.split(/\[\d+\]/));
  366.  
  367. function AFExactMatch(rePatterns, sString)
  368. {    /* match a string against an array of RegExps */
  369.     var it;
  370.  
  371.     if(!rePatterns.length && rePatterns.test(sString) && RegExp.lastMatch == sString)
  372.         return true;
  373.     for(it = 0; it < rePatterns.length; it++)
  374.         if(rePatterns[it].test(sString) && RegExp.lastMatch == sString)
  375.             return it + 1;
  376.     return 0;
  377. }
  378.  
  379. function AFExtractNums(string)
  380. {    /* returns an array of numbers that it managed to extract from the given 
  381.      * string or null on failure */
  382.     var nums = new Array();
  383.  
  384.     if (string.charAt(0) == '.' || string.charAt(0) == ',')
  385.         string = "0" + string;
  386.          
  387.     while(AFDigitsRegExp.test(string)) {
  388.         nums.length++;
  389.         nums[nums.length - 1] = RegExp.lastMatch;
  390.         string = RegExp.rightContext;
  391.     }
  392.     if(nums.length >= 1) return nums;
  393.     return null;
  394. }
  395.  
  396. function AFMakeNumber(string)
  397. {    /* attempts to make a number out of a string that may not use '.' as the
  398.      * seperator; it expects that the number is fairly well-behaved other than
  399.      * possibly having a non-JavaScript friendly separator */
  400.     var type = typeof string;
  401.  
  402.     if (type == "number")
  403.         return string;
  404.     if (type != "string")
  405.         return null;
  406.  
  407.     var array = AFExtractNums(string);
  408.  
  409.     if(array)
  410.     {
  411.         var joined = array.join(".");
  412.  
  413.         if (string.indexOf("-.") >= 0)
  414.             joined = "0." + joined;
  415.         return joined * (string.indexOf("-") >= 0 ? -1.0 : 1.0);
  416.     }
  417.     else
  418.         return null;
  419. }
  420.  
  421. function AFExtractRegExp(rePattern, string)
  422. {    /* attempts to match the pattern given against the string given; on 
  423.      * success, returns an array containing (at index 0) the initial
  424.      * string with the matched text removed and (at index 1) the matched
  425.      * text; on failure, returns null */
  426.     var retVal = new Array();
  427.  
  428.     if(rePattern.test(string))
  429.     {
  430.         retVal.length = 2;
  431.         retVal[0] = RegExp.leftContext + RegExp.rightContext;
  432.         retVal[1] = RegExp.lastMatch;
  433.         return retVal;
  434.     }
  435.     return null;
  436. }
  437.  
  438. function AFMakeArrayFromList(string)
  439. {
  440.   var type = typeof string;
  441.  
  442.   if(type == "string")
  443.   {
  444.      var reSep = new RegExp();
  445.     reSep.compile(",[ ]?");
  446.     return string.split(reSep);
  447.   }
  448.   return string;
  449. }
  450.  
  451. function AFExtractTime(string)
  452. {    /* attempts to extract a WELL FORMED time from a string; returned 
  453.      * is an array in the same vein as AFExtractRegExp or null on
  454.      * failure. a WELL FORMED time looks like 12:23:56pm */
  455.     
  456.     var pm = "";
  457.     var info;
  458.  
  459.     info = AFExtractRegExp(AFPMRegExp, string);
  460.     if(info)
  461.     {
  462.         pm = info[1];
  463.         string = info[0];
  464.     }
  465.     info = AFExtractRegExp(AFAMRegExp, string);
  466.     if(info)
  467.     {
  468.         string = info[0];
  469.     }
  470.     info = AFExtractRegExp(AFTimeLongRegExp, string);
  471.     if(info)
  472.     {
  473.         info[1] += pm;
  474.         return info;
  475.     }
  476.     info = AFExtractRegExp(AFTimeShortRegExp, string);
  477.     if(info)
  478.     {
  479.         info[1] += pm;
  480.         return info;
  481.     }
  482.  
  483.     return null;
  484. }
  485.  
  486. function AFGetMonthIndex(string)
  487. {    /* attempts to identify the given string as a month or a valid abbreviation,
  488.      * it expects the given string to be the valid month from the matced RegExp.
  489.      * returns the month index (January = 1) or zero on failure */
  490.     var monthre = new RegExp(string + "\\[(\\d+)\\]", "i");
  491.     var result = monthre.exec(IDS_MONTH_INFO);
  492.     
  493.     if(string && result) return 1.0 * result[1];
  494.     return 0;
  495. }
  496.  
  497. function AFMatchMonth(string)
  498. {    /* attempts to find a valid month embedded in a string; returns the month
  499.      * index (January = 1) or zero on failure */
  500.  
  501.     for(var it = 0; it < AFMonthsRegExp.length; it++)
  502.         if(AFMonthsRegExp[it].test(string))
  503.             return AFGetMonthIndex(RegExp.lastMatch);
  504.     return 0;
  505. }
  506.  
  507. function AFGetMonthString(index)
  508. {    /* returns the string corresponding to the given month or a string that
  509.      * is indicative of the fact that the index was invalid */
  510.     var monthre = new RegExp("(\\w+)\\[" + index + "\\]");
  511.     var result = monthre.exec(IDS_MONTH_INFO);
  512.  
  513.     if(result) return result[1];
  514.     return IDS_INVALID_MONTH;
  515. }
  516.  
  517. function AFParseTime(string, date)
  518. {    /* attempts to parse a string containing a time; returns null on failure
  519.      * or a Date object on success. Time can be in ugly format. */
  520.     var pm, am;
  521.     var nums = AFExtractNums(string);
  522.     if (!date)
  523.         date = new Date();
  524.     var hour, minutes, seconds;
  525.  
  526.     if(!string) return date;
  527.  
  528.     if(!(AFTimeGarbageRegExp.test(string) && string == RegExp.lastMatch))
  529.         return null;
  530.  
  531.     if(!nums) return null;
  532.     if(nums.length < 2 || nums.length > 3) return null;
  533.     if(AFPMRegExp.test(string)) pm = true;
  534.     else pm = false;
  535.     if(AFAMRegExp.test(string)) am = true;
  536.     else am = false;
  537.     hour = new Number(nums[0]); /* force it to number */
  538.     if(pm)
  539.     {
  540.         if(hour < 12) hour += 12;
  541.     }
  542.     else if (am)
  543.     {
  544.         if(hour >= 12) hour -= 12;
  545.     }
  546.     minutes = nums[1];
  547.     if(nums.length == 3) seconds = nums[2];
  548.     else seconds = 0;
  549.     date.setHours(hour);
  550.     date.setMinutes(minutes);
  551.     date.setSeconds(seconds);
  552.     if(date.getHours() != hour)
  553.         return null;
  554.     if(date.getMinutes() != minutes)
  555.         return null;
  556.     if(date.getSeconds() != seconds)
  557.         return null;
  558.     return date;
  559. }
  560.  
  561. function AFDateFromYMD(nYear, nMonth, nDate)
  562. {    /* Validates the given fields and returns a date based on them */
  563.     var dDate = new Date();
  564.  
  565.     dDate.setFullYear(nYear, nMonth, nDate);
  566.     if(dDate.getFullYear() != nYear)
  567.         return null;
  568.     if(dDate.getMonth() != nMonth)
  569.         return null;
  570.     if(dDate.getDate() != nDate)
  571.         return null;
  572.     return dDate;
  573. }
  574.  
  575. function AFParseDateEx(cString, cFormat)
  576. {    /* Attempts to parse a string containing some form of date; returns null
  577.     ** on failure or a Date object on success.  cFormat should be the format 
  578.     ** in which the date is entered. ( eg y/m/d, ddmmyy, etc ). 
  579.     ** compatibility note: this function used to take cOrder as a paramter,
  580.     ** which was the order in which the date was entered (e.g. ymd, mdy, etc.)
  581.     ** However, if the date is entered numerically with no separators.  ie "121902" 
  582.     ** a cOrder of dmy, could be 1-2-1902 or 12-19-02 etc, so now we require cFormat
  583.     ** to resolve the ambiguity. */
  584.     var nYear;
  585.     var nMonth;
  586.     var nDate;
  587.     var nYCount;
  588.     var cOrder;
  589.     var dDate = new Date();
  590.  
  591.     dDate.setHours(12, 0, 0);
  592.  
  593.     /* Empty string returns current date/time. */
  594.     if (!cString) { 
  595.         return dDate;
  596.     }
  597.  
  598.     nYCount = AFParseDateYCount(cFormat); /* count the number of digits for year in the selected format */
  599.     cOrder = AFParseDateOrder(cFormat); /* make sure its in the "ymd" format */
  600.  
  601.     /* Extract any time information in the string. */
  602.     var info = AFExtractTime(cString);
  603.     if (info)
  604.         cString = info[0];
  605.  
  606.     /* Break down the date into an array of numbers. */
  607.     var aNums = AFExtractNums(cString);
  608.     if(!aNums) 
  609.         return null;    /* No numbers? */
  610.  
  611.     /* User supplied three numbers. */
  612.     if (aNums.length == 3) {
  613.         nYear = 1.0 * aNums[cOrder.indexOf("y")];
  614.         if ((nYCount > 2 && nYear < 100) /* must enter 4 digits for the year to match with the format of the field */
  615.             || nYear > 9999)
  616.             return null; 
  617.         
  618.         nYear = AFDateHorizon(nYear);
  619.  
  620.         dDate = AFDateFromYMD(nYear, aNums[cOrder.indexOf("m")] - 1, aNums[cOrder.indexOf("d")]);
  621.         if (info)
  622.             dDate = AFParseTime(info[1], dDate);
  623.         return dDate;
  624.     }
  625.  
  626.     /* Find text based month, if supplied. */
  627.     nMonth = AFMatchMonth(cString);    
  628.  
  629.     /* User supplied two numbers. */
  630.     if(aNums.length == 2) {
  631.         if (nMonth) {
  632.             /* Easy case, the month was text and we have two numbers. */
  633.             if (cOrder.indexOf("y") < cOrder.indexOf("d")) {
  634.                 nYear = 1.0 * aNums[0];
  635.                 nDate = aNums[1];
  636.             } else {
  637.                 nYear = 1.0 * aNums[1];
  638.                 nDate = aNums[0];
  639.             }
  640.             if ((nYCount > 2 && nYear < 100) /* must enter 4 digits for the year to match with the format of the field */
  641.                 || nYear > 9999)
  642.                 return null; 
  643.         
  644.             nYear = AFDateHorizon(nYear);
  645.             dDate = AFDateFromYMD(nYear, nMonth - 1, nDate);
  646.  
  647.             if (info)
  648.                 dDate = AFParseTime(info[1], dDate);
  649.             return dDate;
  650.         }
  651.  
  652.         /* More difficult case. We have two numbers and three slots, how
  653.         ** to allocate them? */
  654.         if (cOrder.indexOf("y") < cOrder.indexOf("d"))    {
  655.             /* Year comes before date and as such we allocate the two
  656.             ** numbers to the month and the year only. */
  657.             if (cOrder.indexOf("y") < cOrder.indexOf("m")) {
  658.                 nYear = 1.0 * aNums[0];
  659.                 nMonth = aNums[1];
  660.             } else {
  661.                 nYear = 1.0 * aNums[1];
  662.                 nMonth = aNums[0];
  663.             }
  664.             if ((nYCount > 2 && nYear < 100) /* must enter 4 digits for the year to match with the format of the field */
  665.                 || nYear > 9999)
  666.                 return null; 
  667.         
  668.             nYear = AFDateHorizon(nYear);
  669.             dDate = AFDateFromYMD(nYear, nMonth - 1, 1);
  670.         } else {
  671.             /* Date comes before year and so we allocate the two numbers
  672.             ** to the date and the month only. */
  673.             nYear = dDate.getFullYear();
  674.             if (cOrder.indexOf("d") < cOrder.indexOf("m")) {
  675.                 dDate = AFDateFromYMD(nYear, aNums[1] - 1, aNums[0]);
  676.             } else {
  677.                 dDate = AFDateFromYMD(nYear, aNums[0] - 1, aNums[1]);
  678.             }
  679.         }
  680.     
  681.         if (info)
  682.             dDate = AFParseTime(info[1], dDate);
  683.         return dDate;
  684.     }
  685.  
  686.     /* User supplied one number. */
  687.     if(aNums.length == 1)    {
  688.         if (nMonth) {
  689.             /* We have one number and two slots (y/d) and need to allocate
  690.             ** them based on who came first in the format. */
  691.             if(cOrder.indexOf("y") < cOrder.indexOf("d")) {
  692.                 nYear = 1.0 * aNums[0];
  693.                 if ((nYCount > 2 && nYear < 100) /* must enter 4 digits for the year to match with the format of the field */
  694.                     || nYear > 9999)
  695.                     return null; 
  696.             
  697.                 nYear = AFDateHorizon(nYear);
  698.                 dDate = AFDateFromYMD(nYear, nMonth - 1, 1);
  699.             } else {
  700.                 nYear = dDate.getFullYear();
  701.                 dDate = AFDateFromYMD(nYear, nMonth - 1, aNums[0]);
  702.             }
  703.             if (info)
  704.                 dDate = AFParseTime(info[1], date);
  705.             return dDate;
  706.         }
  707.  
  708.         /* We have one number and three slots and need to allocate them
  709.         ** based on who came first in the format. */
  710.         // transcribe the format exactly.  There should be a one to one correspondence.
  711.         if (cFormat.length != cString.length)
  712.             return null;
  713.  
  714.         nYear = "";
  715.         nMonth = "";
  716.         nDate = "";
  717.  
  718.         for (var i = 0; i < cFormat.length; i++) {
  719.             switch (cFormat.charAt(i)) {
  720.                 case "\\":    /* Escape character. */
  721.                     i++;
  722.                     break;
  723.                 case "y":
  724.                     nYear += cString.charAt(i);
  725.                     break;
  726.                 case "m":
  727.                     nMonth += cString.charAt(i);
  728.                     break;
  729.                 case "d":
  730.                     nDate += cString.charAt(i);
  731.                     break;
  732.             }
  733.         }
  734.         
  735.         nYear *= 1.0;
  736.         nMonth *= 1.0;
  737.         nDate *= 1.0
  738.         
  739.         if(nMonth)
  740.             nMonth -= 1.0;  // Month range is [0-11]
  741.         
  742.         if(!nDate)
  743.             nDate += 1.0;   // Date range is [1-31]
  744.  
  745.         if ((nYCount > 2 && nYear < 100) /* must enter 4 digits for the year to match with the format of the field */
  746.             || nYear > 9999)
  747.             return null; 
  748.         
  749.         nYear = AFDateHorizon(nYear);
  750.  
  751.         dDate = AFDateFromYMD(nYear, nMonth, nDate);
  752.  
  753.         if (info)
  754.             dDate = AFParseTime(info[1], date);
  755.         return dDate;
  756.     }
  757.  
  758.     /* No idea how to deal with the other combinations. */
  759.     return null;
  760. }
  761.  
  762. function AFDateHorizon(nYear)
  763. {    /* Takes the year supplied and applies the date horizon heuristic.
  764.     ** All years between 50 and 100 we add 1900. All years less than 50 we add 2000. */
  765.     if (nYear < 100 && nYear >= 50) {
  766.         nYear += 1900;
  767.     } else if (nYear >= 0 && nYear < 50) {
  768.         nYear += 2000;
  769.     }
  770.  
  771.     return nYear;
  772. }
  773.  
  774. function AFParseDate(string, longEntry, shortEntry, wordMonthEntry, monthYearEntry)
  775. {    /* OBSOLETE: Use AFParseDateEx instead. */
  776.     var nums;
  777.     var year, month;
  778.     var date;
  779.     var info = AFExtractTime(string);
  780.  
  781.     if(!string) return new Date();
  782.  
  783.     if(info)
  784.         string = info[0];
  785.  
  786.     date = new Date();
  787.     nums = AFExtractNums(string);
  788.     if(!nums) return null;
  789.     if(nums.length == 3)
  790.     {
  791.         year = 1.0 * nums[eval(longEntry.charAt(0))];
  792.         year = AFDateHorizon(year);
  793.         date = AFDateFromYMD(year, nums[eval(longEntry.charAt(1))] - 1, nums[eval(longEntry.charAt(2))]);
  794.         if (info)
  795.             date = AFParseTime(info[1], date);
  796.         return date;
  797.     }
  798.     month = AFMatchMonth(string);
  799.     if(nums.length == 2)
  800.     {
  801.         if(month)
  802.         {
  803.             year = 1.0 * nums[eval(wordMonthEntry.charAt(0))];
  804.             year = AFDateHorizon(year);
  805.             date = AFDateFromYMD(year, month - 1, nums[eval(wordMonthEntry.charAt(1))]);
  806.             if (info)
  807.                 date = AFParseTime(info[1], date);
  808.             return date;
  809.         }
  810.         if(monthYearEntry)
  811.         {
  812.             year = 1.0 * nums[eval(monthYearEntry.charAt(0))];
  813.             year = AFDateHorizon(year);
  814.             date = AFDateFromYMD(year, nums[eval(monthYearEntry.charAt(1))] - 1, 1);
  815.         }
  816.         else
  817.             date = AFDateFromYMD(date.getFullYear(),
  818.                 nums[eval(shortEntry.charAt(0))] - 1,
  819.                 nums[eval(shortEntry.charAt(1))]);
  820.         if (info)
  821.             date = AFParseTime(info[1], date);
  822.         return date;
  823.     }
  824.     if(month && nums.length == 1)
  825.     {
  826.         if(monthYearEntry)
  827.         {
  828.             year = 1.0 * nums[0];
  829.             year = AFDateHorizon(year);
  830.             date = AFDateFromYMD(year, month - 1, 1);
  831.         }
  832.         else
  833.             date = AFDateFromYMD(date.getFullYear(), month - 1,    nums[0]);
  834.         if (info)
  835.             date = AFParseTime(info[1], date);
  836.         return date;
  837.     }
  838.  
  839.     return null;
  840. }
  841.  
  842. function AFParseDateWithPDF(value, pdf)
  843. { /* OBSOLETE: Use AFParseDateEx instead. */
  844.     var cOldFormats = new Array(
  845.         "m/d", "m/d/yy", "mm/dd/yy", "mm/yy", "d-mmm", "d-mmm-yy", "dd-mmm-yy",
  846.         "yy-mm-dd", "mmm-yy", "mmmm-yy", "mmm d, yyyy", "mmmm d, yyyy",
  847.         "m/d/yy h:MM tt", "m/d/yy HH:MM" );
  848.    
  849.     return AFParseDateEx(value, cOldFormats[pdf]);
  850. }
  851.  
  852. function AFMergeChange(event)
  853. {    /* merges the last change with the uncommitted change */
  854.     var prefix, postfix;
  855.     var value = event.value;
  856.  
  857.     if(event.willCommit) return event.value;
  858.     if(event.selStart >= 0)
  859.         prefix = value.substring(0, event.selStart);
  860.     else prefix = "";
  861.     if(event.selEnd >= 0 && event.selEnd <= value.length)
  862.         postfix = value.substring(event.selEnd, value.length);
  863.     else postfix = "";
  864.     return prefix + event.change + postfix;
  865. }
  866.  
  867. function AFRange_Validate(bGreaterThan, nGreaterThan, bLessThan, nLessThan)
  868. {       /* This function validates the current event to ensure that its value is 
  869.     ** within the specified range. */
  870.     var cError = "";
  871.  
  872.     if (event.value == "")
  873.         return;
  874.  
  875.     if (bGreaterThan && bLessThan) {
  876.         if (event.value < nGreaterThan || event.value > nLessThan)
  877.             cError = util.printf(IDS_GT_AND_LT, nGreaterThan, nLessThan);
  878.     } else if (bGreaterThan) {
  879.         if (event.value < nGreaterThan)
  880.             cError = util.printf(IDS_GREATER_THAN, nGreaterThan);
  881.     } else if (bLessThan) {
  882.         if (event.value > nLessThan)
  883.             cError = util.printf(IDS_LESS_THAN, nLessThan);
  884.     }
  885.     
  886.     if (cError != "") {
  887.         if (!event.silenceErrors)
  888.             app.alert(cError, 0);
  889.         event.rc = false;
  890.     }
  891. }
  892.  
  893. function AFSimpleInit(cFunction)
  894. {    /* Convenience function used by AFSimple_Calculate. */
  895.     switch (cFunction)
  896.     {
  897.         case "PRD":
  898.             return 1.0;
  899.             break;
  900.     }
  901.  
  902.     return 0.0;
  903. }
  904.  
  905. function AFSimple(cFunction, nValue1, nValue2)
  906. {    /* Convenience function used by AFSimple_Calculate. */
  907.     var nValue = 1.0 * nValue1;
  908.  
  909.     /* Have to do this otherwise JavaScript thinks it's dealing with strings. */
  910.     nValue1 = 1.0 * nValue1;
  911.     nValue2 = 1.0 * nValue2;
  912.  
  913.     switch (cFunction)
  914.     {
  915.         case "AVG":
  916.         case "SUM":
  917.             nValue = nValue1 + nValue2;
  918.             break;
  919.         case "PRD":
  920.             nValue = nValue1 * nValue2;
  921.             break;
  922.         case "MIN":
  923.             nValue = Math.min(nValue1,nValue2);
  924.             break;
  925.         case "MAX":
  926.             nValue = Math.max(nValue1, nValue2);
  927.             break;
  928.     }
  929.  
  930.     return nValue;
  931. }
  932.  
  933. function AFSimple_Calculate(cFunction, cFields)
  934. {   /* Calculates the sum, average, product, etc. of the listed field values. */
  935.     var nFields = 0;
  936.     var nValue = AFSimpleInit(cFunction);
  937.  
  938.     /* Field name separator is one or more spaces followed by a comma, 
  939.     ** followed by one or more spaces.
  940.     ** or an array of field names */
  941.      var aFields = AFMakeArrayFromList(cFields);
  942.  
  943.     for (var i = 0; i < aFields.length; i++) {
  944.         /* Found a field, process it's value. */
  945.         var f = this.getField(aFields[i]);
  946.         var a = f.getArray();
  947.  
  948.         for (var j = 0; j < a.length; j++) {
  949.             var nTemp = AFMakeNumber(a[j].value); 
  950.             if (i == 0 && j == 0 && (cFunction == "MIN" || cFunction == "MAX"))
  951.                 nValue = nTemp;
  952.             nValue = AFSimple(cFunction, nValue, nTemp);
  953.             nFields++;
  954.         }
  955.     }
  956.  
  957.     if (cFunction == "AVG" && nFields > 0)
  958.         nValue /= nFields;
  959.  
  960.     // make sure that the value we put out is actually sane.  Sometimes because
  961.     // of rounding issues and the binary to decimal conversion we get tiny exponential 
  962.     // numbers, ex 1.0-.99-.01 will return 8.673617379884035e-18
  963.     // I chose toFixed(6) since after 6, js starts using the 1.0e-7 notation
  964.  
  965.     if( cFunction == "AVG" || cFunction == "SUM" || cFunction == "PRD" )
  966.         nValue = nValue.toFixed(6) *1.0; 
  967.  
  968.     event.value = nValue;
  969. }
  970.  
  971. function AFNumber_Keystroke(nDec, sepStyle, negStyle, currStyle, strCurrency, bCurrencyPrepend)
  972. {       /* This function validates the current keystroke event to make sure the
  973.         key pressed is reasonable for a numeric field. */
  974.  
  975.     var value = AFMergeChange(event);
  976.     var commit, noCommit;
  977.  
  978.     if(!value) return;
  979.     if(sepStyle > 1)
  980.     {
  981.         commit = AFNumberCommaSepCommitRegExp;
  982.         noCommit = AFNumberCommaSepEntryRegExp;
  983.     }
  984.     else
  985.     {
  986.         commit = AFNumberDotSepCommitRegExp;
  987.         noCommit = AFNumberDotSepEntryRegExp;
  988.     }
  989.     if(!AFExactMatch(event.willCommit ? commit : noCommit, value))
  990.     {
  991.         if (event.willCommit && !event.silenceErrors) {
  992.             var cAlert = IDS_INVALID_VALUE;
  993.             if (event.target != null)
  994.                 cAlert += " [ " + event.target.name + " ]";
  995.             app.alert(cAlert);
  996.         }
  997.         else
  998.             app.beep(0);
  999.         event.rc = false;
  1000.     }
  1001.  
  1002.     if (event.willCommit && sepStyle > 1)
  1003.     {
  1004.         // convert from a string to a number 
  1005.         strval = event.value;
  1006.         commas = new RegExp();
  1007.         commas.compile(",");
  1008.  
  1009.         strval = strval.replace(commas, ".");
  1010.         event.value = strval *1.0;
  1011.     } 
  1012. }
  1013.  
  1014. function AFPercent_Keystroke(nDec, sepStyle)
  1015. {
  1016.         AFNumber_Keystroke(nDec, sepStyle, 0, 0, "", true);
  1017. }
  1018.  
  1019. function isNumber(ch)
  1020. {
  1021.     return (ch >= "0" && ch <= "9");
  1022. }
  1023.  
  1024. function isAlphabetic(ch)
  1025. {
  1026.     return ((ch >= "a" && ch <= "z") ||
  1027.               (ch >= "A" && ch <= "Z"));
  1028. }
  1029.  
  1030. function isAlphaNumeric(ch)
  1031. {
  1032.     return (isNumber(ch) || isAlphabetic(ch));
  1033. }
  1034.  
  1035. function isReservedMaskChar(ch)
  1036. {
  1037.     return (ch == '9' || ch == 'X' || ch == 'A' || ch == 'O');
  1038. }
  1039.  
  1040. function maskSatisfied(vChar, mChar)
  1041. {
  1042.     switch(mChar)
  1043.     {
  1044.         case "9":
  1045.             return isNumber(vChar);
  1046.  
  1047.         case "A":
  1048.             return isAlphabetic(vChar);
  1049.  
  1050.         case "O":
  1051.             return isAlphaNumeric(vChar);
  1052.  
  1053.         case "X":
  1054.             return true;
  1055.  
  1056.         default:
  1057.             return (vChar == mChar);
  1058.     }
  1059. }
  1060.  
  1061. function indexOfNextEssential(mask, startIndex)
  1062. {
  1063.     for(var i = startIndex; i < mask.length; i++)
  1064.     {
  1065.         if(isReservedMaskChar(mask.charAt(i)))
  1066.             return i;
  1067.     }
  1068.     return -1;
  1069. }
  1070.  
  1071. function AFSpecial_KeystrokeEx(mask)
  1072. {
  1073.     var value = AFMergeChange(event);
  1074.     
  1075.     if(!value || !mask.length)
  1076.         return;
  1077.             
  1078.     var cAlert = IDS_INVALID_VALUE + ' = "' + mask + '"';
  1079.  
  1080.     if(event.willCommit)
  1081.     {
  1082.         if(indexOfNextEssential(mask, value.length) != -1)
  1083.         {
  1084.             if (!event.silenceErrors)
  1085.                 app.alert(cAlert);
  1086.                 
  1087.             event.rc = false;
  1088.             return;
  1089.         }
  1090.         else {
  1091.             event.value = event.value + mask.substring(value.length, mask.length);
  1092.             return;
  1093.         }
  1094.     }
  1095.  
  1096.     var nMaskIndex = 0;
  1097.     var nValueIndex = 0;
  1098.     
  1099.     var nOffset = 0;  // number of characters we've added to change to make it legal
  1100.     var nChangeStart = event.selStart;    
  1101.     
  1102.     while(nValueIndex < value.length)
  1103.     {
  1104.         var mChar = mask.charAt(nMaskIndex);
  1105.         var vChar = value.charAt(nValueIndex);
  1106.  
  1107.         if(!maskSatisfied(vChar, mChar)) 
  1108.         {
  1109.             var nextEssential = indexOfNextEssential(mask, nMaskIndex);
  1110.             var nChangeEnd = event.selStart + event.change.length + nOffset;
  1111.             
  1112.             if(((nMaskIndex < mask.length) && !isReservedMaskChar(mChar)) && 
  1113.                 (nextEssential != -1) && maskSatisfied(vChar, mask.charAt(nextEssential)) &&
  1114.                 ( (nValueIndex + nOffset) >= nChangeStart) && ( (nValueIndex + nOffset) <= nChangeEnd) ) // we can only modify values w/in the change range
  1115.             {
  1116.                 var maskSubstring = mask.substring(nMaskIndex, nextEssential);
  1117.                 
  1118.                 event.change = event.change.substring(0, nValueIndex + nOffset - nChangeStart) + maskSubstring + event.change.substring(nValueIndex + nOffset - nChangeStart)
  1119.  
  1120.                 nOffset = nOffset + maskSubstring.length;
  1121.                 nMaskIndex = nextEssential;
  1122.             }
  1123.             else
  1124.             {
  1125.                 if (!event.silenceErrors)
  1126.                     app.alert(cAlert);
  1127.                     
  1128.                 event.rc = false;
  1129.                 
  1130.                 return;
  1131.             }
  1132.         }
  1133.         
  1134.         nValueIndex++;
  1135.         nMaskIndex++;
  1136.     }
  1137. }
  1138.  
  1139.  
  1140.  
  1141. function AFSpecial_Keystroke(psf)
  1142. {       /* This function validates the current keystroke event to make sure the
  1143.         key pressed is reasonable for a "special" field. */
  1144.         
  1145.     /* The special formats, indicated by psf, are:
  1146.     
  1147.     psf             format
  1148.     ---             ------
  1149.     0               zip code
  1150.     1               zip + 4
  1151.     2               phone
  1152.     3                SSN
  1153.     
  1154.     */
  1155.  
  1156.     var value = AFMergeChange(event);
  1157.     var commit, noCommit;
  1158.  
  1159.     if(!value) return;
  1160.     switch (psf)
  1161.     {
  1162.         case 0:
  1163.             commit = AFZipCommitRegExp;
  1164.             noCommit = AFZipEntryRegExp;
  1165.             break;
  1166.         case 1:
  1167.             commit = AFZip4CommitRegExp;
  1168.             noCommit = AFZip4EntryRegExp;
  1169.             break;
  1170.         case 2:
  1171.             commit = AFPhoneCommitRegExp;
  1172.             noCommit = AFPhoneEntryRegExp;
  1173.             break;
  1174.         case 3:
  1175.             commit = AFSSNCommitRegExp;
  1176.             noCommit = AFSSNEntryRegExp;
  1177.             break;
  1178.     }        
  1179.     if(!AFExactMatch(event.willCommit ? commit : noCommit, value))
  1180.     {
  1181.         if (event.willCommit && !event.silenceErrors) {
  1182.             var cAlert = IDS_INVALID_VALUE;
  1183.             if (event.target != null)
  1184.                 cAlert += " [ " + event.target.name + " ]";
  1185.             app.alert(cAlert);
  1186.         }
  1187.         else
  1188.             app.beep(0);
  1189.         event.rc = false;
  1190.     }
  1191. }
  1192.  
  1193. function AFDate_KeystrokeEx(cFormat)
  1194. {    /* This function validates the current keystroke event to make sure the
  1195.     ** key pressed is reasonable for a date field. */
  1196.     if(event.willCommit && !AFParseDateEx(AFMergeChange(event), cFormat)) {
  1197.         /* Dates are only validated on commit */
  1198.         if (event.willCommit && !event.silenceErrors) {
  1199.             var cAlert = IDS_INVALID_DATE;
  1200.             var cAlert2 = IDS_INVALID_DATE2;
  1201.             if (event.target != null)
  1202.                 cAlert += "[ " + event.target.name + " ]";
  1203.                 cAlert += cAlert2 + cFormat;
  1204.             app.alert(cAlert);
  1205.         }
  1206.         else
  1207.             app.beep(0);
  1208.         event.rc = false;
  1209.     }
  1210. }
  1211.  
  1212. function AFDate_Keystroke(pdf)
  1213. {    /* OBSOLETE: Use AFDate_KeystrokeEx. */
  1214.     var cOldFormats = new Array(
  1215.         "m/d", "m/d/yy", "mm/dd/yy", "mm/yy", "d-mmm", "d-mmm-yy", "dd-mmm-yy",
  1216.         "yy-mm-dd", "mmm-yy", "mmmm-yy", "mmm d, yyyy", "mmmm d, yyyy",
  1217.         "m/d/yy h:MM tt", "m/d/yy HH:MM" );
  1218.  
  1219.     AFDate_KeystrokeEx(cOldFormats[pdf]);
  1220. }
  1221.  
  1222. function AFTime_Keystroke(ptf)
  1223. {    /* This function validates the current keystroke event to make sure the
  1224.     key pressed is reasonable for a time field. */
  1225.  
  1226.     if(event.willCommit && !AFParseTime(event.value, null))
  1227.                     /* times are only validated on commit */
  1228.     {
  1229.         if (event.willCommit && !event.silenceErrors) {
  1230.             var cAlert = IDS_INVALID_VALUE;
  1231.             if (event.target != null)
  1232.                 cAlert += " [ " + event.target.name + " ]";
  1233.             app.alert(cAlert);
  1234.         }
  1235.         else
  1236.             app.beep(0);
  1237.         event.rc = false;
  1238.     }
  1239. }
  1240.  
  1241. function AFNumber_Format(nDec, sepStyle, negStyle, currStyle, strCurrency, bCurrencyPrepend)
  1242. {       /* This function formats a numeric value according to the parameters. */
  1243.  
  1244.     var value = AFMakeNumber(event.value);
  1245.     var sign = (value < 0 ? -1 : 1);
  1246.     var f = event.target;
  1247.  
  1248.     if(value == null)
  1249.     {
  1250.         event.value = "";
  1251.         return;
  1252.     }    
  1253.     if ((negStyle == 2 /* ParensBlack */ || negStyle == 3 /* ParensRed */) && value < 0)
  1254.         var formatStr = "(";
  1255.     else 
  1256.         var formatStr = "";
  1257.     
  1258.     if (bCurrencyPrepend)
  1259.         formatStr = formatStr + strCurrency;
  1260.         
  1261.     formatStr = formatStr + "%," + sepStyle + "." + nDec + "f";
  1262.     if (! bCurrencyPrepend)
  1263.         formatStr = formatStr + strCurrency;
  1264.         
  1265.     if ((negStyle == 2 /* ParensBlack */ || negStyle == 3 /* ParensRed */) && value < 0)
  1266.         formatStr = formatStr + ")";
  1267.  
  1268.     if (negStyle != 0 /* MinusBlack */ || bCurrencyPrepend)
  1269.         value = Math.abs(value);
  1270.         
  1271.     if (negStyle == 1 /* Red */ || negStyle == 3 /* ParensRed */) {
  1272.         if (sign > 0 )
  1273.             f.textColor = color.black;
  1274.         else 
  1275.             f.textColor = color.red;
  1276.     }
  1277.  
  1278.     var tmp = util.printf(formatStr, value);
  1279.     if (sign < 0 && bCurrencyPrepend && negStyle == 0)
  1280.         tmp = '-' + tmp; /* prepend the -ve sign */
  1281.     event.value = tmp;
  1282. }
  1283.  
  1284. function AFPercent_Format(nDec, sepStyle)
  1285. {       /* This function formats a percentage value according to the parameters. */
  1286.  
  1287.     var value = AFMakeNumber(event.value) * 100;
  1288.     
  1289.     var formatStr = "%," + sepStyle + "." + nDec + "f";
  1290.         
  1291.     if(value == null)
  1292.     {
  1293.         event.value = "";
  1294.         return;
  1295.     }    
  1296.  
  1297.     value = util.printf(formatStr, value);
  1298.     
  1299.     event.value = value + "%";
  1300. }
  1301.  
  1302. function AFSpecial_Format(psf)
  1303. {   /* This function formats a "special" value according to the "PropsSpecialFormat" parameter psf. */
  1304.     /* The special formats, indicated by psf, are: 0 = zip code, 1 = zip + 4, 2 = phone, 3 = SSN. */
  1305.     var value = event.value;
  1306.  
  1307.     if(!value) return;    
  1308.     switch (psf) {
  1309.     
  1310.         case 0:                         
  1311.             var formatStr = "99999";
  1312.             break;
  1313.         case 1:                         
  1314.             var formatStr = "99999-9999";
  1315.             break;
  1316.         case 2:                         /* must distinguish between 2 styles: with and without area code */
  1317.             var NumbersStr = util.printx("9999999999", value);      /* try to suck out 10 numeric chars */
  1318.             if (NumbersStr.length >= 10 )
  1319.                 var formatStr = "(999) 999-9999";
  1320.             else 
  1321.                 var formatStr = "999-9999";
  1322.             break;
  1323.         case 3:
  1324.             var formatStr = "999-99-9999";
  1325.             break;
  1326.     }
  1327.         
  1328.     event.value = util.printx(formatStr, value);
  1329. }
  1330.  
  1331. function AFParseDateYCount(cFormat)
  1332. {
  1333.     /* Determine the order of the date. */
  1334.     var yCount = 0;
  1335.     for (var i = 0; i < cFormat.length; i++) {
  1336.         switch (cFormat.charAt(i)) {
  1337.             case "\\":    /* Escape character. */
  1338.                 i++;
  1339.             break;
  1340.             case "y":
  1341.                 yCount += 1;
  1342.             break;
  1343.         }
  1344.     }
  1345.     return yCount;
  1346. }
  1347.  
  1348. function AFParseDateOrder(cFormat)
  1349. {
  1350.     /* Determine the order of the date. */
  1351.     var cOrder = "";
  1352.     for (var i = 0; i < cFormat.length; i++) {
  1353.         switch (cFormat.charAt(i)) {
  1354.             case "\\":    /* Escape character. */
  1355.                 i++;
  1356.             break;
  1357.             case "m":
  1358.                 if (cOrder.indexOf("m") == -1)
  1359.                     cOrder += "m";
  1360.             break;
  1361.             case "d":
  1362.                 if (cOrder.indexOf("d") == -1)
  1363.                     cOrder += "d";
  1364.             break;
  1365.             case "j":
  1366.             case "y":
  1367.                 if (cOrder.indexOf("y") == -1)
  1368.                     cOrder += "y";
  1369.             break;
  1370.         }
  1371.     }
  1372.  
  1373.     /* Make sure we have a full complement of 3 chars. */
  1374.     if (cOrder.indexOf("m") == -1)
  1375.         cOrder += "m";
  1376.     if (cOrder.indexOf("d") == -1)
  1377.         cOrder += "d";
  1378.     if (cOrder.indexOf("y") == -1)
  1379.         cOrder += "y";
  1380.  
  1381.     return cOrder;
  1382. }
  1383.  
  1384. function AFDate_FormatEx(cFormat)
  1385. {    /* cFormat is a format string with which the date is to be formatted. */
  1386.     if (!event.value) 
  1387.         return;    /* Blank fields remain blank */
  1388.  
  1389.     var date = AFParseDateEx(event.value, cFormat);
  1390.     if (!date) {
  1391.         event.value = "";
  1392.         return;
  1393.     }
  1394.     
  1395.     event.value = util.printd(cFormat, date);
  1396. }
  1397.  
  1398. function AFDate_Format(pdf)
  1399. {    /* OBSOLETE: Use AFDate_FormatEx. */
  1400.     var cOldFormats = new Array(
  1401.         "m/d", "m/d/yy", "mm/dd/yy", "mm/yy", "d-mmm", "d-mmm-yy", "dd-mmm-yy",
  1402.         "yy-mm-dd", "mmm-yy", "mmmm-yy", "mmm d, yyyy", "mmmm d, yyyy",
  1403.         "m/d/yy h:MM tt", "m/d/yy HH:MM" );
  1404.  
  1405.     AFDate_FormatEx(cOldFormats[pdf]);
  1406. }
  1407.  
  1408. function AFTime_Format(ptf)
  1409. {    /* This function formats a time value according to the "PropsTimeFormat" parameter ptf.
  1410.     ** The time formats, indicated by ptf, are:
  1411.     ** ptf             format                                                          
  1412.     ** ---             ------                                                          
  1413.     ** 0               PTF_24HR_MM     [ 14:30      ]
  1414.     ** 1               PTF_12HR_MM     [ 2:30 PM    ]
  1415.     ** 2               PTF_24HR_MM_SS  [ 14:30:15   ]
  1416.     ** 3               PTF_12HR_MM_SS  [ 2:30:15 PM ] */
  1417.  
  1418.     if(!event.value) return;    /* Blank fields remain blank */
  1419.  
  1420.     var date = AFParseTime(event.value, null);
  1421.     if(!date) {
  1422.         event.value = "";
  1423.         return;
  1424.     }
  1425.  
  1426.     var cFormats = new Array(
  1427.         "HH:MM", "h:MM tt", "HH:MM:ss", "h:MM:ss tt" ); 
  1428.     
  1429.     event.value = util.printd(cFormats[ptf], date);
  1430. }
  1431.  
  1432. function AFTime_FormatEx(cFormat)
  1433. {
  1434.     if(!event.value) return;    /* Blank fields remain blank */
  1435.  
  1436.     var date = AFParseTime(event.value, null);
  1437.     if(!date) {
  1438.         event.value = "";
  1439.         return;
  1440.     }
  1441.  
  1442.     event.value = util.printd(cFormat, date);
  1443. }
  1444.  
  1445. function AFSignatureLock(doc, cOperation, cFields, bLock)
  1446. {    // Locks or unlocks a set of fields according to the specified operation.
  1447.     /* Field name separator is one or more spaces followed by a comma, 
  1448.     ** followed by one or more spaces.
  1449.     ** or an array of field names */
  1450.      var aFields = AFMakeArrayFromList(cFields);
  1451.  
  1452.     /* Three cases: ALL, EXCEPT, THESE for the field name list. */
  1453.     if (cOperation != "THESE") {
  1454.         for (var i = 0; i < doc.numFields; i++) {
  1455.             var f = doc.getField(doc.getNthFieldName(i));
  1456.                 
  1457.             f.readonly = bLock;
  1458.          }
  1459.     }
  1460.     
  1461.     if (cOperation == "EXCEPT")
  1462.         /* EXCEPT = ALL(lock) then THESE(unlock) */
  1463.         bLock = !bLock;
  1464.  
  1465.     if (cOperation == "THESE" || (cOperation == "EXCEPT" && !bLock)) {
  1466.         for (var i = 0; i < aFields.length; i++) {
  1467.             var f = doc.getField(aFields[i]);
  1468.             var a = f.getArray();
  1469.  
  1470.             for (var j = 0; j < a.length; j++) {
  1471.                 a[j].readonly = bLock;
  1472.             }
  1473.         }
  1474.     }
  1475. }
  1476.  
  1477. function AFSignature_Format(cOperation, cFields)
  1478. {    /* This function is invoked at format time but really is used to lock fields
  1479.     ** in the document. We unlock all the specified fields if the value is
  1480.     ** null (which means the signature hasn't been applied). */
  1481.  
  1482.     var bLock = (event.value != "");
  1483.  
  1484.     AFSignatureLock(this, cOperation, cFields, bLock);
  1485. }
  1486. function AFStringReplace(cString, oRegExp, cReplacement)
  1487. {
  1488.     return cString.replace(oRegExp, cReplacement);
  1489. }
  1490.  
  1491.  
  1492. function SearchBuildURL(query, mode, country)
  1493. {/* Builds a URL for google.com.
  1494.  
  1495.     Parameters:
  1496.         query: string to be searched such as "find newport"
  1497.         mode:  one of the search modes, kMatchPhrase, kMatchAllWords, or kMatchAnyWords.
  1498.         country: 3 character string such as "ENU", describing the country.
  1499. */
  1500.     var url, queryStr;
  1501.  
  1502.     if (mode < 0 || mode > 2)
  1503.         return "";
  1504.  
  1505.     if (country == "JPN")
  1506.         url = "http://www.google.co.jp/search?";
  1507.     else
  1508.         url = "http://www.google.com/search?";
  1509.  
  1510.  
  1511.     queryStr = "";
  1512.     for (i=0; i < query.length; i++)
  1513.         if (query.charAt(i) == ' ')
  1514.             queryStr = queryStr + '+';
  1515.         else
  1516.             queryStr = queryStr + query.charAt(i);
  1517.  
  1518.             
  1519.     switch (mode)
  1520.     {
  1521.         case 0: /* kMatchPhrase */
  1522.             url = url + "&as_epq=" + queryStr;
  1523.             break;
  1524.         case 1: /* kMatchAllWords */
  1525.             url = url + "&as_q=" + queryStr;
  1526.             break;
  1527.         case 2: /* kMatchAnyWords */
  1528.             url = url + "&as_oq=" + queryStr;
  1529.             break;
  1530.     }
  1531.  
  1532.     url = url + "&ie=UTF-8&oe=UTF-8&hl=";
  1533.  
  1534.  
  1535.     /* country specific stuff */
  1536.     if (country == "ENU")
  1537.         url = url + "en";
  1538.     else if (country == "DEU")
  1539.         url = url + "de";
  1540.     else if (country == "FRA")         
  1541.         url = url + "fr";
  1542.     else if (country == "ITA")
  1543.         url = url + "it";
  1544.     else if (country == "NLD")
  1545.         url = url + "nl";
  1546.     else if (country == "ESP")
  1547.         url = url + "es";
  1548.     else if (country == "SVE")
  1549.         url = url + "sv";
  1550.     else if (country == "JPN")
  1551.         url = url + "ja";
  1552.     else if (country == "KOR")
  1553.         url = url + "ko";
  1554.     else if (country == "CHT")         
  1555.         url = url + "zh-TW";
  1556.     else if (country == "CHS")
  1557.         url = url + "zh-CN";
  1558.     else if (country == "PTB")
  1559.         url = url + "pt";
  1560.     else if (country == "DAN")
  1561.         url = url + "da";
  1562.     else if (country == "SUO")
  1563.         url = url + "fi";
  1564.     else if (country == "NOR")
  1565.         url = url + "no";
  1566.     else if (country == "ARA")
  1567.         url = url + "ar";
  1568.     else if (country == "HEB")
  1569.         url = url + "iw";
  1570.     else
  1571.         url = url + "en";
  1572.  
  1573.     url = url + "&lr=&safe=images&as_filetype=pdf";
  1574.  
  1575.     return url;
  1576. }
  1577.  
  1578.  
  1579. function LookUpWordEnable(country)
  1580. {/* Check a locatization for Look Up Word definition ability.
  1581.  
  1582.     Parameters:
  1583.         country: 3 character string such as "ENU", describing the country.
  1584. */
  1585.     var bEnable = false;
  1586.  
  1587.     switch (country)
  1588.     {
  1589.         case "ENU": /* English */
  1590.             bEnable = true;
  1591.             break;
  1592.  
  1593.         default: /* unsupported country */
  1594.             break;
  1595.     }
  1596.  
  1597.     return bEnable;
  1598. }
  1599.  
  1600.  
  1601. function LookUpWordDefinitionURL(cWord, country)
  1602. {/* Builds a URL for dictionary.com.
  1603.  
  1604.     Parameters:
  1605.         cWord: word to be defined such as "orthographic"
  1606.         country: 3 character string such as "ENU", describing the country.
  1607. */
  1608.     var wordStr, url = "";
  1609.  
  1610.     switch (country)
  1611.     {
  1612.         case "ENU": /* English */
  1613.         {
  1614.             url = "http://www.dictionary.com/cgi-bin/dict.pl?term=";
  1615.  
  1616.             wordStr = "";
  1617.             for (i=0; i < cWord.length; i++)
  1618.                 if (cWord.charAt(i) == ' ')
  1619.                     wordStr = wordStr + '+';
  1620.                 else
  1621.                     wordStr = wordStr + cWord.charAt(i);
  1622.  
  1623.             url = url + wordStr;
  1624.             break;
  1625.         }
  1626.  
  1627.         default: /* unsupported country */
  1628.             break;
  1629.     }
  1630.  
  1631.     return url;
  1632. }
  1633.