var REG_WHTSPC_START = /^\s+/ig;  //global pattern matching (g), case insensitive (i), matches @ beginning of string (^)
var REG_WHTSPC_END   = /\s+$/ig;  //global pattern matching (g), case insensitive (i), matches @ end of string ($)
var whitespace       = "\t\n\r "; // horizontal tab,newline,carriage return,space etc
var isDefEmptyOK     = false;


function allTrim ( strIn )
  {
   //strIn.value = strIn.replace(REG_WHTSPC_START,"").replace(REG_WHTSPC_END,"");

   var i = 0;                  // trim leading spaces
   while ( i <= strIn.length-1 && whitespace.indexOf(strIn.charAt(i)) != -1 )
         { i++; }
   if ( i == strIn.length )
      { strIn = ""; }
   else
      { strIn = strIn.substring ( i, strIn.length); }

   i = strIn.length-1;         // trim trailing spaces
   while ( i >= 0 && whitespace.indexOf(strIn.charAt(i)) != -1 ) { i--; }
   strIn = strIn.substring ( 0, i+1 );

   return strIn;
  }


function trimAllFields ( docForm )
  {
   for ( var i=0; i<docForm.elements.length; i++ )
       {
        if ( docForm.elements[i].type == "text"     || docForm.elements[i].type == "textarea" ||
             docForm.elements[i].type == "password" || docForm.elements[i].type == "hidden" )
           {
            docForm.elements[i].value = allTrim(docForm.elements[i].value);
            docForm.elements[i].value = chkValidHTMLCodes(docForm.elements[i].value,true);
            //docForm.elements[i].value = docForm.elements[i].value.replace(REG_APOSTROPHE,"'");
           }
       }
  }

function chkValidHTMLCodes ( s )
  {
   // includes New Line "String.fromCharCode(10)" "\n", Carriage Return "String.fromCharCode(13)" "\r", Tab "String.fromCharCode(9)" "\t"
   // © 169   ® 174
   var strValidCodes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 " +
                       "~!@#$%^&*()_-+={}[]:;'<,>.?/|" + "\"" + "\\" + "©®" + "\n\r\t" +
                       String.fromCharCode(160);

   var i         = 0;
   var returnStr = "";
   var isReplace = chkValidHTMLCodes.arguments.length == 1 ? false : (chkValidHTMLCodes.arguments[1]==true);

   if ( isReplace )
      {
       for ( i=0; i<s.length; i++ )
         {
          var c = s.charAt(i);
          if ( s.charCodeAt(i) > 255 || strValidCodes.indexOf(c) == -1 )
             { returnStr = returnStr + "&#" + s.charCodeAt(i) + ";"; }
          else
             { returnStr += c; }
         }
       return returnStr;
      }
   else
      { return true; }
  }

function isOKFormText ( formElem, strErrMsg )
  {
   if ( formElem.value == "" )
      { alert(strErrMsg); formElem.focus(); return false; }
   else if ( isOKFormText.arguments.length == 3 )
      {
       if ( formElem.value.length > parseInt(isOKFormText.arguments[2]) )
          {
           alert ( "Text too long ! Maximum " + isOKFormText.arguments[2] + " characters" );
           formElem.focus(); return false;
          }
      }
   return true;
  }


function isOKCombo ( formElem )
  {
   if ( formElem.length == 0 ) { return ( isOKCombo.arguments.length == 1 ? isDefEmptyOK : (isOKCombo.arguments[1]==true) ); }

   var strIdx = formElem.selectedIndex;
   var strVal = formElem.options[strIdx].value;
   if ( strVal == "" )   //if ( strVal == "" || strVal == 0 )
      { return ( isOKCombo.arguments.length == 1 ? isDefEmptyOK : (isOKCombo.arguments[1]==true) ); }
   return true;
  }


// !isOKFormCombo(document.forms[0].cboCtry,"Please select country",false)     mandatory
// !isOKFormCombo(document.forms[0].cboState,"Please select state",true)       optional
function isOKFormCombo ( formElem, strErrMsg )
  {
   var isEmptyOK = isOKFormCombo.arguments.length == 2 ? isDefEmptyOK : (isOKFormCombo.arguments[2]==true);
   if ( !isOKCombo(formElem,isEmptyOK) ) { alert(strErrMsg); formElem.focus(); return false; }
   return true;
  }


function cboSingleValue ( objCombo )
  {
   //return objCombo[objCombo.selectedIndex].value;
   return objCombo.options[objCombo.selectedIndex].value;
  }


function isDigit (c)
  {
   //return ( !( s.charCodeAt(i) >= 48 && s.charCodeAt(i) <= 57 ) );
   //return ( c < "0" || c > "9" ? false : true );
   return ( c >= "0" && c <= "9" );
  }


// isDecimal (STRING s [, BOOLEAN emptyOK])
function isDecimal (s)
  {
   if ( s == "" )
      { return ( isDecimal.arguments.length == 1 ? isDefEmptyOK : (isDecimal.arguments[1]==true) ); }

   var decimalPointDelimiter = ".";
   var seenDecimalPoint = false;
   if ( s == decimalPointDelimiter && s.length == 1 ) { return false; }

   for ( var i = 0; i < s.length; i++ )
     {   
      var c = s.charAt(i);
      if ( c == decimalPointDelimiter && !seenDecimalPoint ) { seenDecimalPoint = true; }
      else if ( !isDigit(c) ) { return false; }
     }

   return true;
  }


function numberFormat ( iNumber, iPrecision )
  {
   //return parseFloat(iNumber).toFixed(iPrecision);
   if ( iPrecision == 0 ) { return ""+Math.round(eval(iNumber)); }
   var str = "" + Math.round( eval(iNumber) * Math.pow(10,iPrecision) );
   while ( str.length <= iPrecision )
    { str = "0" + str; }
   var iDecPoint = str.length - iPrecision;
   return str.substring(0,iDecPoint) + "." + str.substring(iDecPoint,str.length);

   /*
   if ( (iPrecision=parseInt(iPrecision) || 0 ) < 1 ) { return Math.round(iNumber)+""; }
   var str = Math.round( iNumber * Math.pow(10,iPrecision) ) + "";
   var iDecPoint = str.length - iPrecision;
   while ( iDecPoint <= 0 ){ str = "0" + str; iDecPoint++; }
   return str.substring(0,iDecPoint)+"."+str.substring(iDecPoint);
   */
  }


function isOKFormDecNonZero ( formElem, strErrMsg )
  {
   var isEmptyOK = isOKFormDecNonZero.arguments.length == 2 ? isDefEmptyOK : (isOKFormDecNonZero.arguments[2]==true);
   if ( formElem.value != "" ) { formElem.value = ltrimZeros(formElem.value); }
   if ( !isDecimal(formElem.value,isEmptyOK) ) { alert(strErrMsg); formElem.focus(); formElem.select(); return false; }
   else if ( parseFloat(formElem.value) == 0 ) { alert(strErrMsg); formElem.focus(); formElem.select(); return false; }
   return true;
  }


// Remove trailing 0, "parseInt" returns 0 for 09.  Can omit this function using "parseInt(eval("09"))" function rather than "parseInt"
function ltrimZeros ( s )
  {
   if ( s == "" ) { return s; }
   var i = 0;
   while ( i <= s.length-1 && s.charAt (i) == "0" ) { i++; }
   if ( i == s.length ) { s = "0"; }
   else if ( i == 1 && s.charAt(i) == "." ) { s = s; }
   else { s = s.substring ( i, s.length); }
   return s;
  }


// if ( !isOKFormIntegerNonZero(document.forms[0].txtAge,"Please enter valid age",true) ) { return false; }
function isOKFormIntegerNonZero ( formElem, strErrMsg )
  {
   var isEmptyOK = isOKFormIntegerNonZero.arguments.length == 2 ? isDefEmptyOK : (isOKFormIntegerNonZero.arguments[2]==true);
   if ( formElem.value != "" ) { formElem.value = ltrimZeros(formElem.value); }
   if ( !isInteger(formElem.value,isEmptyOK) ) { alert(strErrMsg); formElem.focus(); formElem.select(); return false; }
   else if ( parseInt(formElem.value) == 0 ) { alert(strErrMsg); formElem.focus(); formElem.select(); return false; }
   return true;
  }


// isInteger (STRING s [, BOOLEAN emptyOK])
// Alternatively, use isNaN(s) --> returns true if Not-A-Number
function isInteger (s)
  {
   if ( s == "" )
      { return ( isInteger.arguments.length == 1 ? isDefEmptyOK : (isInteger.arguments[1] == true) ); }

   for ( var i = 0; i < s.length; i++ )
       {   
        var c = s.charAt(i);
        if ( !isDigit(c) ) { return false; }
       }
   return true;
  }


// if ( !isOKFormDecimal(document.forms[0].txtPrice,3,"Please enter valid price",true) ) { return false; }
function isOKFormDecimal ( formElem, iPrecision, strErrMsg )
  {
   var isEmptyOK = isOKFormDecimal.arguments.length == 3 ? isDefEmptyOK : (isOKFormDecimal.arguments[3]==true);
   if ( formElem.value != "" ) { formElem.value = ltrimZeros(formElem.value); }
   if ( !isDecimal(formElem.value,isEmptyOK) ) { alert(strErrMsg); formElem.focus(); formElem.select(); return false; }
   else if ( formElem.value != "" ) { formElem.value = numberFormat(parseFloat(formElem.value),iPrecision); }
   return true;
  }


function isOKFormDec ( formElem, strErrMsg )
  {
   var isEmptyOK = isOKFormDec.arguments.length == 2 ? isDefEmptyOK : (isOKFormDec.arguments[2]==true);
   if ( formElem.value != "" ) { formElem.value = ltrimZeros(formElem.value); }
   if ( !isDecimal(formElem.value,isEmptyOK) ) { alert(strErrMsg); formElem.focus(); formElem.select(); return false; }
   return true;
  }


function isOKFormDecimalNonZero ( formElem, iPrecision, strErrMsg )
  {
   var isEmptyOK = isOKFormDecimalNonZero.arguments.length == 3 ? isDefEmptyOK : (isOKFormDecimalNonZero.arguments[3]==true);
   if ( formElem.value != "" ) { formElem.value = ltrimZeros(formElem.value); }
   if ( !isDecimal(formElem.value,isEmptyOK) ) { alert(strErrMsg); formElem.focus(); formElem.select(); return false; }
   else if ( parseFloat(formElem.value) == 0 ) { alert(strErrMsg); formElem.focus(); formElem.select(); return false; }
   else { formElem.value = numberFormat(parseFloat(formElem.value),iPrecision); }
   return true;
  }
