home *** CD-ROM | disk | FTP | other *** search
/ ftp.tcs3.com / ftp.tcs3.com.tar / ftp.tcs3.com / DRIVERS / Audio / Office2010 / ProPlus.WW / ProPsWW.cab / VALIDATION.JS < prev    next >
Text File  |  2007-02-04  |  14KB  |  386 lines

  1. // ========================================================================
  2. // ===                          validation.js                           ===
  3. // ========================================================================
  4.  
  5. // Global arrays used by the validation and formatting functions.
  6. var g_arrMonth = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
  7. var g_arrDay = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
  8. var g_arrError = new Array();
  9. var g_objDispatch = getScriptDispatch();
  10.  
  11. // =============================VALIDATION===============================
  12. // === the following functions apply to validating the entered values ===
  13. // === according to the rules set up when the form was created        ===
  14. // ======================================================================
  15.  
  16. // === validateRequired(object, string) =========================
  17. // called for input types that may be required, displays an error
  18. // if the value is blank
  19. function validateRequired(i_objInput, i_strType)
  20. {
  21.     if (!g_IsSearch)
  22.     {
  23.         var blnClear = true;
  24.         var strValue = "";
  25.  
  26.         if (i_objInput.type == "radio")
  27.         {
  28.             var DocumentForm = getDocumentForm();
  29.             var RadioGroup = DocumentForm.elements[i_objInput.name];
  30.             for (var i = 0; i < RadioGroup.length; i++)
  31.             {
  32.                 if (RadioGroup[i].checked)
  33.                 {
  34.                     strValue = RadioGroup[i].value;
  35.                     break;
  36.                 }
  37.             }
  38.         }
  39.         else
  40.             strValue = i_objInput.value;
  41.  
  42.         if (i_strType == 1)
  43.             i_objInput = i_objInput.parentElement;
  44.  
  45.         var strRequired = i_objInput.getAttribute("VldtRequired");
  46.         if (strRequired != null && strRequired != "" && strValue == "")
  47.         {
  48.             displayError("This field is required, and must have a value.", i_objInput, "Required");
  49.             blnClear = false;
  50.         }
  51.  
  52.         if (blnClear)
  53.             clearError(i_objInput, "Required");
  54.     }
  55. }
  56.  
  57. // === validateNumeric(object) ==================================
  58. // called for numeric input types, displays an error if it is not
  59. // a number, or does not conform to min/max rules
  60. function validateNumeric(i_objInput)
  61. {
  62.     var blnClear = true;
  63.  
  64.     var strNumeric = i_objInput.value;
  65.     if (strNumeric == "")
  66.         var intNumeric = Number.NaN;
  67.     else
  68.         var intNumeric = getValidNumber(strNumeric);
  69.  
  70.     if ((strNumeric != "" || i_objInput.value != "") && isNaN(intNumeric))
  71.         displayError("You must input a valid number.", i_objInput, "InvalidNumeric");
  72.     else
  73.         clearError(i_objInput, "InvalidNumeric");
  74.  
  75.     var strMin = i_objInput.getAttribute("VldtValueMin");
  76.     var strMax = i_objInput.getAttribute("VldtValueMax");
  77.     var intMin = Number(strMin);
  78.     var intMax = Number(strMax);
  79.  
  80.     if (strMin != null && intNumeric < intMin)
  81.     {
  82.         displayError("The lowest allowable value for this field is " + intMin + ".", i_objInput, "Numeric");
  83.         blnClear = false;
  84.     }
  85.     if (strMax != null && intNumeric > intMax)
  86.     {
  87.         displayError("The highest allowable value for this field is " + intMax + ".", i_objInput, "Numeric");
  88.         blnClear = false;
  89.     }
  90.  
  91.     if (blnClear)
  92.         clearError(i_objInput, "Numeric");
  93.  
  94.     return blnClear;
  95. }
  96.  
  97. // === validateDate(object) ====================================
  98. // called for date types, displays an error if it is not a valid
  99. // date, or does not conform to min/max rules
  100. function validateDate(i_objInput)
  101. {
  102.     var blnBlankErr = false;
  103.     var blnNaNErr = false;
  104.     var blnMinErr = false;
  105.     var blnMaxErr = false;
  106.  
  107.     var strInput = i_objInput.value;
  108.     i_objInput = i_objInput.parentElement;
  109.  
  110.     var strFormatError = "Please enter a valid date in the short date format for your locale.";
  111.     var intFormat = g_objDispatch.GrooveIntlDateFormatStyle_Short;
  112.     var strFormat = i_objInput.getAttribute("FormatDateShortFormat");
  113.     if (strFormat == null || strFormat == "")
  114.     {
  115.         strFormatError = "Please enter a valid date in the long date format for you locale.";
  116.         intFormat = g_objDispatch.GrooveIntlDateFormatStyle_Full;
  117.         strFormat = i_objInput.getAttribute("FormatDateLongFormat");
  118.     }
  119.  
  120.     var intInput = getValidDateMillis(strInput, strFormat, intFormat);
  121.     if (strInput != "" && isNaN(intInput))
  122.     {
  123.         blnNaNErr = true;
  124.         displayError(strFormatError, i_objInput, "InvalidDate");
  125.     }
  126.     else if (strInput == "")
  127.     {
  128.         blnBlankErr = true;
  129.         clearError(i_objInput, "InvalidDate");
  130.     }
  131.     else
  132.         clearError(i_objInput, "InvalidDate");
  133.  
  134.     var objMin = i_objInput.getAttribute("VldtDateMin");
  135.     var objMax = i_objInput.getAttribute("VldtDateMax");
  136.     var datMin = new Date(objMin);
  137.     var datMax = new Date(objMax);
  138.     var intMin = Number(datMin);
  139.     var intMax = Number(datMax);
  140.  
  141.     if (!isNaN(intMin) && intMin != 0 && intInput < intMin)
  142.         blnMinErr = true;
  143.     if (!isNaN(intMax) && intMax != 0 && intInput > intMax)
  144.         blnMaxErr = true;
  145.  
  146.     if ((objMin != null && objMax != null) && (blnMinErr || blnMaxErr))
  147.         displayError("Please enter a date between " + g_arrMonth[datMin.getMonth()] + " " + datMin.getDate() + ", " + datMin.getFullYear() + " and " + g_arrMonth[datMax.getMonth()] + " " + datMax.getDate() + ", " + datMax.getFullYear() + ".", i_objInput, "Date");
  148.     else if (objMin != null && blnMinErr)
  149.         displayError("Please enter a date on, or later than, " + g_arrMonth[datMin.getMonth()] + " " + datMin.getDate() + ", " + datMin.getFullYear() + ".", i_objInput, "Date");
  150.     else if (objMax != null && blnMaxErr)
  151.         displayError("Please enter a date on, or earlier than, " + g_arrMonth[datMax.getMonth()] + " " + datMax.getDate() + ", " + datMax.getFullYear() + ".", i_objInput, "Date");
  152.  
  153.     if (!blnMinErr && !blnMaxErr)
  154.         clearError(i_objInput, "Date");
  155.  
  156.     return (!blnMinErr && !blnMaxErr && !blnBlankErr && !blnNaNErr);
  157. }
  158.  
  159.  
  160. // =======================VALIDATION=FORMATTING==========================
  161. // === the following functions apply to formatting the entered values ===
  162. // === according to the rules set up when the form was created        ===
  163. // ======================================================================
  164.  
  165. // === formatNumeric(object) ==========================================
  166. // formats the value of the numeric input according to formatting rules
  167. function formatNumeric(i_objInput)
  168. {
  169.     var NumericValue = getValidNumber(i_objInput.value);
  170.     var FormatCancel = i_objInput.getAttribute("FrmtCancel");
  171.     if (!isNaN(NumericValue) && FormatCancel != null && FormatCancel != "")
  172.     {
  173.         i_objInput.value = NumericValue;
  174.     }
  175.     else
  176.     {
  177.         var NumericPrecision = getFormatAttribute(i_objInput, "PRECISION", "2");
  178.         if (!isNaN(NumericValue) && i_objInput.value != "")
  179.             i_objInput.value = g_objDispatch.LocalFormatNumber(NumericValue, NumericPrecision, null);
  180.     }
  181. }
  182.  
  183. function formatNumericFromDouble(i_objInput)
  184. {
  185.     var NumericValue = Number(i_objInput.value);
  186.     var FormatCancel = i_objInput.getAttribute("FrmtCancel");
  187.     if (FormatCancel != null && FormatCancel != "")
  188.     {
  189.         i_objInput.value = NumericValue;
  190.     }
  191.     else
  192.     {
  193.         var NumericPrecision = getFormatAttribute(i_objInput, "PRECISION", "2");
  194.         if (!isNaN(NumericValue) && i_objInput.value != "")
  195.             i_objInput.value = g_objDispatch.LocalFormatNumber(NumericValue, NumericPrecision, null);
  196.     }
  197. }
  198.  
  199. // === formatCurrency(object) ==========================================
  200. // formats the value of the currency input according to formatting rules
  201. function formatCurrency(i_objInput)
  202. {
  203.     var CurrencyPrecision = getFormatAttribute(i_objInput, "PRECISION", "2");
  204.     var CurrencySymbol = getFormatAttribute(i_objInput, "SYMBOL", "\u00A4");
  205.     var CurrencyValue = getValidNumber(i_objInput.value);
  206.  
  207.     if (!isNaN(CurrencyValue) && i_objInput.value != "")
  208.         i_objInput.value = g_objDispatch.LocalFormatNumber(CurrencyValue, CurrencyPrecision, CurrencySymbol);
  209. }
  210.  
  211. // === formatDate(object) ==========================================
  212. // formats the value of the date input according to formatting rules
  213. function formatDate(i_objInput)
  214. {
  215.     var InputValue = i_objInput.value;
  216.  
  217.     if (InputValue != "")
  218.     {
  219.         var ParentObject = i_objInput.parentElement;
  220.  
  221.         // Use the formatting option specified by the user, if neither is selected use short.
  222.         var intFormat = g_objDispatch.GrooveIntlDateFormatStyle_Short;
  223.         var strFormat = ParentObject.getAttribute("FormatDateShortFormat");
  224.         if (strFormat == null || strFormat == "")
  225.         {
  226.             strFormat = ParentObject.getAttribute("FormatDateLongFormat");
  227.             intFormat = g_objDispatch.GrooveIntlDateFormatStyle_Full;
  228.         }
  229.  
  230.         var DateValue = getValidDateMillis(InputValue, strFormat, intFormat);
  231.         DateValue = g_objDispatch.LocalFormatDate(DateValue, strFormat, intFormat);
  232.         i_objInput.value = DateValue;
  233.     }
  234. }
  235.  
  236.  
  237. // =======================VALIDATION=UTILITY===========================
  238. // === the following section contains utility functions used within ===
  239. // === this javascript file                                            ===
  240. // ====================================================================
  241.  
  242. // === cleanDate(object) ================================
  243. // cleans date objects that are passed in; no longer used
  244. function cleanDate(i_objInput)
  245. {
  246. }
  247.  
  248. // === displayError(string, object, string) =========================
  249. // displays the passed in string as an error message next to the html
  250. // object that is passed in
  251. function displayError(i_strError, i_objError, i_strName)
  252. {
  253.     if (!g_IsPreviewPane)
  254.     {
  255.         var strObjName = i_objError.name;
  256.         if (strObjName == "" || strObjName == null)
  257.             strObjName = i_objError.getAttribute("NAME");
  258.         // Create the name for the new DIV that will contain the error message.
  259.         var strDivName = "divError" + i_strName + strObjName;
  260.         var objDivError = document.getElementById(strDivName);
  261.  
  262.         var strTop = getOffset(i_objError, "top") + "px";
  263.         var strLeft = getOffset(i_objError, "left") + i_objError.offsetWidth + 15 + "px";
  264.  
  265.         if (objDivError == null)
  266.         {
  267.             var objForm = document.GrooveFormBase;
  268.             objForm.insertAdjacentHTML("beforeEnd", "<DIV ID=\"" + strDivName + "\" STYLE=\"position:absolute; padding:2px 4px; color:rgb(255,255,255); background-color:rgb(200,0,0); border:1px solid rgb(75,0,0); font-weight:bold; z-index:3;\"></DIV>");
  269.             objDivError = document.getElementById(strDivName);
  270.             g_arrError[g_arrError.length] = strDivName;
  271.         }
  272.  
  273.         objDivError.innerText = i_strError;
  274.         objDivError.style.top = strTop;
  275.         objDivError.style.left = strLeft;
  276.     }
  277. }
  278.  
  279. // === clearError(object, string) ===================================
  280. // clears the error for the passed in object from the page and erases
  281. // it from the error array
  282. function clearError(i_objError, i_strName)
  283. {
  284.     var strObjName = i_objError.name;
  285.     if (strObjName == "" || strObjName == null)
  286.         strObjName = i_objError.getAttribute("NAME");
  287.     // Create the name for the new DIV that contains the error message.
  288.     var strDivName = "divError" + i_strName + strObjName;
  289.     var objDivError = document.getElementById(strDivName);
  290.     if (objDivError != null)
  291.     {
  292.         var arrError = new Array();
  293.         var i = 0;
  294.         var iCount = g_arrError.length;
  295.         while (i < iCount)
  296.         {
  297.             if (g_arrError[i] != strDivName)
  298.                 arrError[arrError.length] = g_arrError[i];
  299.             i++;
  300.         }
  301.         g_arrError = arrError;
  302.         objDivError.outerHTML = "";
  303.     }
  304. }
  305.  
  306. // === getValidNumber(string) =========================================
  307. // get the numerical value from a number string, NaN if it is not valid
  308. function getValidNumber(i_NumberString)
  309. {
  310.     var ValidNumber;
  311.  
  312.     if (i_NumberString == "")
  313.     {
  314.         ValidNumber = Number.NaN;
  315.     }
  316.     else
  317.     {
  318.         // Replace all non-valid characters in the number string.
  319.         var NumberString = i_NumberString.replace(/[^\d,-\.\(]/g, "");
  320.         // If the number string has a paren in it, replace with a negative symbol.
  321.         NumberString = NumberString.replace(/\(/g, "-");
  322.         // Parse the number string into a valid numeric.
  323.         var ValidNumber = g_objDispatch.LocalParseNumber(NumberString);
  324.         if (isNaN(ValidNumber))
  325.         {
  326.             ValidNumber = Number(NumberString);
  327.             if (isNaN(ValidNumber))
  328.                 ValidNumber = Number.NaN;
  329.         }
  330.     }
  331.  
  332.     return ValidNumber;
  333. }
  334.  
  335. // === getValidDateMillis(string, string) =============================
  336. // get the millisecond value from a date string, NaN if it is not valid
  337. function getValidDateMillis(i_DateString, i_strFormat, i_intFormat)
  338. {
  339.     var ValidDate = g_objDispatch.LocalParseDate(i_DateString, i_strFormat, i_intFormat);
  340.     if (isNaN(ValidDate))
  341.     {
  342.         ValidDate = new Date(i_DateString);
  343.         if (isNaN(ValidDate))
  344.             ValidDate = Number.NaN
  345.         else if (i_DateString.search(/^\d{1,2}[\/|\-]\d{1,2}[\/|\-]\d{1,2}(\-|\/|$)/) == 0)
  346.         {
  347.             // Fix dates with only two digit years.
  348.             var CurrentDate = new Date();
  349.             var CutoffYear = CurrentDate.getFullYear() - 80;
  350.             if (CutoffYear > ValidDate.getFullYear())
  351.                 ValidDate.setFullYear(ValidDate.getFullYear() + 100);
  352.         }
  353.         ValidDate = ValidDate.valueOf();
  354.     }
  355.  
  356.     return ValidDate;
  357. }
  358.  
  359. // === getFormatAttribute(object, string, string) =====================
  360. // utility function to retrieve attribute from the passed in object, it
  361. // returns the passed in default value if the attribute is not found
  362. function getFormatAttribute(i_Object, i_Attribute, i_Default)
  363. {
  364.     var FormatAttribute = i_Object.getAttribute(i_Attribute);
  365.     if (FormatAttribute == null || FormatAttribute == "")
  366.         FormatAttribute = i_Default;
  367.     return FormatAttribute
  368. }
  369.  
  370. // === getOffset(object, string) ======================================
  371. // a utility function to get the top or left position of a given object
  372. function getOffset(i_objPos, i_strDir)
  373. {
  374.     var intOffset = 0;
  375.     var objOffset = i_objPos;
  376.     while (objOffset.tagName != "BODY")
  377.     {
  378.         if (i_strDir == "top")
  379.             intOffset += objOffset.offsetTop;
  380.         else if (i_strDir == "left")
  381.             intOffset += objOffset.offsetLeft;
  382.  
  383.         objOffset = objOffset.offsetParent;
  384.     }
  385.     return intOffset;
  386. }