String.isIntegerRegEx = /[0-9{0,}]/;
String.isNotNumeric = /^[0-9{0,}]/g;
String.isNumberRegEx = /^(-)?(\d*)(\.?)(\d*)$/;
String.isAlphaRegEx = /[a-zA-Z{0,}]/;
String.whiteSpace = /[ {0,}]/;
String.isNotAlphaNumeric = /[^a-zA-Z0-9_]/;

String.prototype.isAlpha = function()
{
   return String.isAlphaRegEx.test( this );
}
String.prototype.isNumeric = function()
{
   return String.isNumberRegEx.test( this );
}
String.prototype.isAlphaNumeric = function()
{
	return !String.isNotAlphaNumeric.test(this);
}
String.prototype.isInteger = function()
{
   return String.isIntegerRegEx.test( this );
}
String.prototype.removeSpaces = function()
{
   return this.split(" ").join("");
}
String.prototype.removeNonNumeric = function()
{
   return this.replace(/\D/g,"");//.split(" ").join("");
}
var FormUtil =
{
   getRadioValue: function( formObj )
   {
      for (var i = 0; i < formObj.length; i ++)
      {
         if (formObj[i].checked)
         {
            return formObj[i].value;
         }
      }
      return null;
   },

   getSelectBoxValue: function( formObj )
   {
      return formObj[formObj.selectedIndex].value;
   },

   setSelectBoxValue: function( formObj, value )
   {
   
      for ( var i=0; i<formObj.options.length; i++ )
      {
      	 var option = formObj.options[i];
         if ( option.value == value )
         {
            option.selected = true;
         }
      }
   },

    isValidNpa: function(npa)
   {
      if ( (npa == null) || (npa.length != 3) || !npa.isInteger() )
      {
         return false;
      }

      var c0 = parseInt(npa.charAt(0));
      var c1 = parseInt(npa.charAt(1));
      var c2 = parseInt(npa.charAt(2));

      if( npa == "911" )
      {
          return false;
      }

      if ( (c0 < 2) || (c0 > 9) )
      {
         return false;
      }

      if ( (c1 < 0) || (c1 > 9) )
      {
         return false;
      }

      if ( (c2 < 0) || (c2 > 9) )
      {
         return false;
      }
      
      // 555 is reserved so we don't consider it val
       if (c0==5 && c1==5 && c2==5)
      {
         return false;
      }
      return true;
   },

   isValidNxx: function(nxx)
   {
      if ( (nxx == null) || (nxx.length != 3) || !nxx.isInteger()  )
      {
         return false;
      }

      var c0 = parseInt(nxx.charAt(0));
      var c1 = parseInt(nxx.charAt(1));
      var c2 = parseInt(nxx.charAt(2));

      if ( (c0 < 2) || (c0 > 9) )
      {
         return false;
      }

      if ( (c1 < 0) || (c1 > 9) )
      {
         return false;
      }

      if ( (c2 < 0) || (c2 > 9) )
      {
         return false;
      }

      // 555 is reserved so we don't consider it valid
      if (c0==5 && c1==5 && c2==5)
      {
         return false;
      }

      return true;
   },

   isValidLine: function(line)
   {
      if (line == null || !line.isInteger() )
      {
         return false;
      }

      if (line.length != 4)
      {
         return false;
      }

      return true;
   },

   updateCharCount: function(sourceElement, countElement, max)
   {
      var sourceForm = document.getElementById(sourceElement);
      var totalChars = sourceForm.value.length;
      if (max != undefined)
      {
         if ( totalChars <= max )
         {
            if ( ( countElement != undefined ) && ( countElement != '' ) )
            {
               if (arguments[3] == 'countdown')
                {
                    document.getElementById(countElement).value = max - totalChars;
                }
                 else
                {
                    document.getElementById(countElement).value = totalChars;
                }
            }
         }
         else
         {
            sourceForm.value = sourceForm.value.substring(0, max);
         }
      }
      else
      {
         if ( ( countElement != undefined ) && ( countElement != '' ) )
         {
                document.getElementById(countElement).value = totalChars;
         }
      }
      var myCounter = document.getElementById(countElement);
      if (myCounter && myCounter.update && myCounter.update instanceof Function )
      {
         myCounter.update();
      }
   },

   checkTextArea: function (flag, tid, countid, count)
   {
      if (flag)
      {
         var counterID = ( ( countid  != undefined ) && ( countid != '' ) ? countid : '' );
         var textAreaID = tid.id;
         FormUtil.textAreaChecker = setInterval("FormUtil.updateCharCount('" + textAreaID +"', '" + counterID + "', " + count + ");", 50);
      }
      else
      {
         clearInterval(FormUtil.textAreaChecker);
      }
   },

   checkTextAreaCountDown: function (flag, tid, countid, count)
   {
      if (flag)
      {
         var counterID = ( ( countid  != undefined ) && ( countid != '' ) ? countid : '' );
         var textAreaID = tid.id;
         FormUtil.textAreaChecker = setInterval("FormUtil.updateCharCount('" + textAreaID +"', '" + counterID + "', " + count + ", 'countdown');", 50);
      }
      else
      {
         clearInterval(FormUtil.textAreaChecker);
      }
   },

   autoFormatPhoneField: function( phoneField, defaultText, phoneFormatter )
   {
      if( defaultText == null ) defaultText = '';
      if( phoneFormatter == null ) phoneFormatter = FormUtil.defaultPhoneFormatter; //use the (xxx) xxx - xxxx formatter by default
      //only assign the functions if they haven't been already
      var _okd = phoneField.onkeydown;
      var _oku = phoneField.onkeyup;
      var _okp = phoneField.onkeypress;
      var _ob = phoneField.onblur;
      var _of = phoneField.onfocus;
      phoneField.onkeydown = function( )
      {
         if( _okd != null && typeof _okd == "function" && !phoneField.autoFormat ) _okd.apply();
         FormUtil._autoFormatPhoneField( phoneField, phoneFormatter );
      }
      phoneField.onkeyup = function( )
      {
         if( _oku != null && typeof _oku == "function" && !phoneField.autoFormat ) _oku.apply();
         FormUtil._autoFormatPhoneField( phoneField, phoneFormatter );
      }
      phoneField.onkeypress = function( )
      {
         if( _okp != null && typeof _okp == "function" && !phoneField.autoFormat ) _okp.apply();
         FormUtil._autoFormatPhoneField( phoneField, phoneFormatter );
      }
      phoneField.onblur = function ( )
      {
         if( _ob != null && typeof _ob == "function" && !phoneField.autoFormat ) _ob.apply();
         if( phoneField.value != '' && phoneField.value != defaultText &&
             ( phoneFormatter[0]["0"] != null && phoneFormatter[0]["0"] != phoneField.value ) )
         {
            FormUtil._autoFormatPhoneField( phoneField, phoneFormatter );
         }
         else
         {
            phoneField.value = (defaultText != null) ? defaultText : '';
         }
      }
      phoneField.onfocus = function ( )
      {
         if( _of != null && typeof _of == "function" ) _ob.apply();
         if( phoneField.value == defaultText && phoneField.value != '' ) phoneField.value = '';
      }
      phoneField.autoFormat = true; //always set this to true so the autoformatting will happen
      if( phoneField.value != '' && phoneField.value != defaultText &&
             ( phoneFormatter[0]["0"] != null && phoneFormatter[0]["0"] != phoneField.value ) )
         {
            FormUtil._autoFormatPhoneField( phoneField, phoneFormatter );
         }
         else
         {
            phoneField.value = (defaultText != null) ? defaultText : '';
         }
   },
   clearPhoneAutoFormat: function( phoneField )
   {
      phoneField.autoFormat = false;
   },

   _autoFormatPhoneField: function( phone, formatter )
   {
      if( !phone.autoFormat ) return;
      var charAdded;
      try
      {
         var formatted = (formatter[0]["0"] != null) ? formatter[0]["0"] : ""; //since we are building a string (instead of splicing into an existing) take care of 0 here
         var fa = (formatter[0]["0"] != null) ? 1 : 0; //formats added
         var stripped = phone.value.removeNonNumeric(); //remove all non numbers from text
         for( var i=0, l=stripped.length;i<l; i++ )
         {
            for( var f=0, len=formatter.length; f<len; f++ )
            {
               for( var o in formatter[f] )
               {
                  if( o == i + fa )
                  {
                     if(formatter[f][o].length > 1){ console.warn("Formatters can only be single characters! Error in index {" + o + ":'" + formatter[f][o] + "'} skipping." ); continue; }
                     formatted += formatter[f][o];
                     fa++;
                  }
               }
            }
            formatted += stripped.charAt(i);
         }
         var endIndex = Math.min( formatted.length, formatter.length + 10 );
         formatted = formatted.substr( 0, endIndex );
         phone.value = formatted;
      }
      catch( e ) {}
   },
   
   autoFormatSSNField: function( ssnField, defaultText, ssnFormatter )
   {
      if( defaultText == null ) defaultText = '';
      if( ssnFormatter == null ) ssnFormatter = FormUtil.defaultSSNFormatter; //use the (xxx) xxx - xxxx formatter by default
      //only assign the functions if they haven't been already
      var _okd = ssnField.onkeydown;
      var _oku = ssnField.onkeyup;
      var _okp = ssnField.onkeypress;
      var _ob = ssnField.onblur;
      var _of = ssnField.onfocus;
      ssnField.onkeydown = function( )
      {
         if( _okd != null && typeof _okd == "function" && !ssnField.autoFormat ) _okd.apply();
         FormUtil._autoFormatSSNField( ssnField, ssnFormatter );
      }
      ssnField.onkeyup = function( )
      {
         if( _oku != null && typeof _oku == "function" && !ssnField.autoFormat ) _oku.apply();
         FormUtil._autoFormatSSNField( ssnField, ssnFormatter );
      }
      ssnField.onkeypress = function( )
      {
         if( _okp != null && typeof _okp == "function" && !ssnField.autoFormat ) _okp.apply();
         FormUtil._autoFormatSSNField( ssnField, ssnFormatter );
      }
      ssnField.onblur = function ( )
      {
         if( _ob != null && typeof _ob == "function" && !ssnField.autoFormat ) _ob.apply();
         if( ssnField.value != '' && ssnField.value != defaultText &&
             ( ssnFormatter[0]["0"] != null && ssnFormatter[0]["0"] != ssnField.value ) )
         {
            FormUtil._autoFormatSSNField( ssnField, ssnFormatter );
         }
      }
      ssnField.onfocus = function ( )
      {
         if( _of != null && typeof _of == "function" ) _ob.apply();
         if( ssnField.value == defaultText && ssnField.value != '' ) ssnField.value = '';
      }
      ssnField.autoFormat = true; //always set this to true so the autoformatting will happen
      if( ssnField.value != '' && ssnField.value != defaultText &&
             ( ssnFormatter[0]["0"] != null && ssnFormatter[0]["0"] != ssnField.value ) )
         {
            FormUtil._autoFormatSSNField( ssnField, ssnFormatter );
         }
         else
         {
            ssnField.value = (defaultText != null) ? defaultText : '';
         }
   },
   clearSSNAutoFormat: function( ssnField )
   {
      ssnField.autoFormat = false;
   },

   _autoFormatSSNField: function( ssn, formatter )
   {
      if( !ssn.autoFormat ) return;
      var charAdded;
      try
      {
         var formatted = (formatter[0]["0"] != null) ? formatter[0]["0"] : ""; //since we are building a string (instead of splicing into an existing) take care of 0 here
         var fa = (formatter[0]["0"] != null) ? 1 : 0; //formats added
         var stripped = ssn.value.removeNonNumeric(); //remove all non numbers from text
         for( var i=0, l=stripped.length;i<l; i++ )
         {
            for( var f=0, len=formatter.length; f<len; f++ )
            {
               for( var o in formatter[f] )
               {
                  if( o == i + fa )
                  {
                     if(formatter[f][o].length > 1){ console.warn("Formatters can only be single characters! Error in index {" + o + ":'" + formatter[f][o] + "'} skipping." ); continue; }
                     formatted += formatter[f][o];
                     fa++;
                  }
               }
            }
            formatted += stripped.charAt(i);
         }
         var endIndex = Math.min( formatted.length, formatter.length + 10 );
         formatted = formatted.substr( 0, endIndex );
         ssn.value = formatted;
      }
      catch( e ) {}
   },
   
   autoFormatBirthField: function( birthField, defaultText, birthFormatter )
   {
      if( defaultText == null ) defaultText = '';
      if( birthFormatter == null ) birthFormatter = FormUtil.defaultBirthFormatter; //use the MM/DD/YYYY formatter by default
      //only assign the functions if they haven't been already
      var _okd = birthField.onkeydown;
      var _oku = birthField.onkeyup;
      var _okp = birthField.onkeypress;
      var _ob = birthField.onblur;
      var _of = birthField.onfocus;
      birthField.onkeydown = function( )
      {
         if( _okd != null && typeof _okd == "function" && !birthField.autoFormat ) _okd.apply();
         FormUtil._autoFormatBirthField( birthField, birthFormatter );
      }
      birthField.onkeyup = function( )
      {
         if( _oku != null && typeof _oku == "function" && !birthField.autoFormat ) _oku.apply();
         FormUtil._autoFormatBirthField( birthField, birthFormatter );
      }
      birthField.onkeypress = function( )
      {
         if( _okp != null && typeof _okp == "function" && !birthField.autoFormat ) _okp.apply();
         FormUtil._autoFormatBirthField( birthField, birthFormatter );
      }
      birthField.onblur = function ( )
      {
         if( _ob != null && typeof _ob == "function" && !birthField.autoFormat ) _ob.apply();
         if( birthField.value != '' && birthField.value != defaultText &&
             ( birthFormatter[0]["0"] != null && birthFormatter[0]["0"] != birthField.value ) )
         {
            FormUtil._autoFormatBirthField( birthField, birthFormatter );
         }
      }
      birthField.onfocus = function ( )
      {
         if( _of != null && typeof _of == "function" ) _ob.apply();
         if( birthField.value == defaultText && birthField.value != '' ) birthField.value = '';
      }
      birthField.autoFormat = true; //always set this to true so the autoformatting will happen
      if( birthField.value != '' && birthField.value != defaultText &&
             ( birthFormatter[0]["0"] != null && birthFormatter[0]["0"] != birthField.value ) )
         {
            FormUtil._autoFormatBirthField( birthField, birthFormatter );
         }
         else
         {
            birthField.value = (defaultText != null) ? defaultText : '';
         }
   },
   clearBirthAutoFormat: function( birthField )
   {
      birthField.autoFormat = false;
   },

   _autoFormatBirthField: function( birth, formatter )
   {
      if( !birth.autoFormat ) return;
      var charAdded;
      try
      {
         var formatted = (formatter[0]["0"] != null) ? formatter[0]["0"] : ""; //since we are building a string (instead of splicing into an existing) take care of 0 here
         var fa = (formatter[0]["0"] != null) ? 1 : 0; //formats added
         var stripped = birth.value.removeNonNumeric(); //remove all non numbers from text
         for( var i=0, l=stripped.length;i<l; i++ )
         {
            for( var f=0, len=formatter.length; f<len; f++ )
            {
               for( var o in formatter[f] )
               {
                  if( o == i + fa )
                  {
                     if(formatter[f][o].length > 1){ console.warn("Formatters can only be single characters! Error in index {" + o + ":'" + formatter[f][o] + "'} skipping." ); continue; }
                     formatted += formatter[f][o];
                     fa++;
                  }
               }
            }
            formatted += stripped.charAt(i);
         }
         var endIndex = Math.min( formatted.length, formatter.length + 10 );
         formatted = formatted.substr( 0, endIndex );
         birth.value = formatted;
      }
      catch( e ) {}
   }

}
FormUtil.defaultPhoneFormatter = [{0:"("},{4:")"}, {5:" "},{9:" "},{10:"-"},{11:" "}];
FormUtil.decimalFormatter = [{3:"."},{7:"."}];
FormUtil.dashFormatter = [{3:"-"},{7:"-"}];
FormUtil.defaultSSNFormatter = [{3:"-"}, {6:"-"}];
FormUtil.defaultBirthFormatter = [{2:"/"}, {5:"/"}, {10:" "}];
FormUtil.textAreaChecker = 0;