/* begin implementation specifics */
var ComboGroup = function( options )
{
   this.name = options.name || "defCGName";
   this.regex = options.regex || false;
   this.elements = options.elements || [];
   return this;
}
var ValidationDisplay =
{
   comboGroups: {},
   addComboGroup: function( ComboGroup )
   {
      this.comboGroups[ComboGroup.name] = ComboGroup;
   },
   friendlyFieldNames:
   {
      addr1: 'Address',
      addr1Add: 'Address',
      addr1Edit: 'Address',
      addr1Single: 'Address',
      citySingle: 'City',
      zipSingle: 'Zip code',
      cityAdd: 'City',
      zipAdd: 'Zip code',
      cityEdit: 'City',
      zipEdit: 'Zip code'
   },
   errorMessages:
   {
      required: ' is required.',
      phone: 'Please provide a valid phone number.',
      email: 'Please provide a valid email address.',
      password: 'Please enter at least 4 characters.',
      zipcode: 'Please provide a valid zip code.',
      nameCombo: 'First and Last name are required.',
      cityZipCombo : 'City and Zip Code are required'
   },
   checkForGroup: function( field )
   {
      var fieldName = field.name;
      var belongsTo = false;
      if( field.grouped && field.grouped.name )
      {
         belongsTo = field.grouped;
      }
      else
      {
         for( group in this.comboGroups )
         {
            var comboName = this.comboGroups[group];
            if( comboName.regex && comboName.regex.test(fieldName) )
            {
               belongsTo = comboName;
            }
            if( belongsTo )
            {
               field.grouped = belongsTo;
               break;
            }
         }
      }
      return belongsTo;
   },
   getValidationInfoName: function( field )
   {
      var fieldName = field.name;
      var fieldNameFnl = fieldName;
      var myGroup = this.checkForGroup( field );
      if( myGroup )
      {
          fieldNameFnl = myGroup.name;
      }
      return 'iconDiv' + fieldNameFnl;;
   },
   getFriendlyFieldNames: function( name )
   {
      if( this.friendlyFieldNames[name] )
      {
         name = this.friendlyFieldNames[name];
      }
      return Helper.fromCamelCase( name );
   },
   runComboGroupCheck: function( field )
   {
      var fieldName = field.name;
      var pass = 0;
      var myGroup = this.checkForGroup( field );
      if( !myGroup )
         return true; // exit fast if this element doesn't belong to a group
      else if( myGroup )
      {
         for( var i = 0; i < myGroup.elements.length; i++ )
         {
            var theElement = myGroup.elements[i];
            if ( theElement.value.replace(/\s/gi, '') != '' )
            {
               pass = pass + 1;
            }
         }
      }
      return pass > 0;
   },
   getErrorMessage: function( field )
   {
      var msg = '';
      var fieldName = field.name;
      var friendlyFieldName = this.getFriendlyFieldNames(fieldName);
      var passedCombo = this.runComboGroupCheck(field);
      if( fieldName.match(/name/gi) && !passedCombo )
      {
          msg = this.errorMessages.nameCombo;
          return msg;
      }
      if( ( fieldName.match(/city/gi) || fieldName.match(/zip/gi)) && !passedCombo )
      {
          msg = this.errorMessages.cityZipCombo;
          return msg;
      }
      if( field.required && ( field.value.replace( /\s/gi, '' ) == '' || field.value.replace( /\s/gi, '' ) == '(' ) )
      {
         msg = friendlyFieldName +  this.errorMessages.required;
      }
      else if( fieldName.match(/phone/gi) )
      {
         msg = this.errorMessages.phone;
      }
      else if( fieldName.match(/email/gi) )
      {
         msg = this.errorMessages.email;
      }
      else if( fieldName.match(/passw/gi) )
      {
         msg = this.errorMessages.password;
      }
      else if ( fieldName.match(/zip/gi) )
      {
         msg = this.errorMessages.zipcode;
      }
      return msg;
   },
   removeAnyNotices: function(field)
   {
      var iconDivName = this.getValidationInfoName( field );
      var msgDivName = 'msg' + this.getValidationInfoName( field );
      if( Helper.getEl(iconDivName ) != null )
      {
         Helper.removeElement(iconDivName);
      }
      if( Helper.getEl(msgDivName) != null )
      {
         Helper.removeElement( msgDivName );
      }
   },
   injectIcon: function( pass, field )
   {
      var source = 'error_icon.gif';
      if( pass )
      {
         source = 'approve_icon.gif';
      }
      var idName = this.getValidationInfoName( field );
      if( Helper.getEl( idName ) )
      {
         Helper.removeElement( idName )
      }
      var iconDiv;
          iconDiv = document.createElement( 'div' );
          iconDiv.setAttribute( 'class', 'iconDiv' );
          iconDiv.setAttribute( 'id', idName );
          iconDiv.style.position = 'relative';
          iconDiv.style.marginLeft = '-24px';
          if( document.all )
          {
            iconDiv.style.position = 'absolute';
          }
      var icon = document.createElement( 'img' );
          icon.setAttribute( 'src', '/images/' + source );
          icon.setAttribute( 'class', 'validationIcon' );
      var parent = field.parentNode;
      iconDiv.appendChild( icon );
      //parent.style.position = 'relative';
      parent.insertBefore( iconDiv, parent.firstChild );
   },
   injectErrorMsg: function( field )
   {
      var msg = this.getErrorMessage( field );
      this.displayErrorMsg( field,msg );
   },
   displayErrorMsg: function( field, msg )
   {
      if( msg == null )
      {
         msg = this.getErrorMessage( field );
      }
      var myId = 'msg' + this.getValidationInfoName( field );
      var parentN = field.parentNode;
      var msgDiv;
          msgDiv = document.createElement( 'div' );
          msgDiv.setAttribute( 'id', myId );
          msgDiv.className = 'sfErrorMsg';
      msgDiv.innerHTML = msg;
      parentN.appendChild( msgDiv );
   },
   checkForNotRequiredAndEmpty: function( field )
   {
      return !field.required && ( field.value.replace(/\s/gi, '') == '' || field.value.replace(/\s/gi, '') == '(' );
   }
 };

function myFail( field, errMsg, loginLink )
{
   ValidationDisplay.removeAnyNotices( field );
   field.style.border = 'solid 2px red';
   field.validData = false;
   
   if (ValidationDisplay.checkForNotRequiredAndEmpty( field ) )
      return;
      
   ValidationDisplay.injectIcon( false, field );
   if( errMsg != null && errMsg.match(/previously registered/gi) && loginLink != null)
   {
     errMsg += '<br />&nbsp;&nbsp;&nbsp;<a href=\"' + loginLink + '&link_id=2834\" onclick=\"setSkip(true);\"><b>Click Here to Login</b></a>';
   }
   ValidationDisplay.displayErrorMsg( field, errMsg );

 }    
function myPass( field )
{
   field.style.border = 'solid 1px #666';
   field.validData = true;
   
   if( field.name == 'primaryExt' )
      return;
      
   ValidationDisplay.removeAnyNotices(field);
   if( ValidationDisplay.checkForNotRequiredAndEmpty( field ) )
      return;
   ValidationDisplay.injectIcon( true, field );
   if( field.grouped )
   {
      var theGroup = field.grouped;
      for( var i=0; i < theGroup.elements.length; i++ )
      {
         var theEl = theGroup.elements[i];
         if( !theEl.validData && theEl.hasValidated )
         {
            theEl.validate();
         }
      }
   }
 }

var allowSubmit = true;
function spFail( field, errMsg, loginLink )
{
   var err = document.getElementById( 'busPhone-error' );
   var ico = document.getElementById( 'busPhone-icon' );
   field.style.border = 'solid 2px red';
   field.validData = false;
   
   if( ValidationDisplay.checkForNotRequiredAndEmpty( field ) )
      return;
      
   err.innerHTML = "Please enter a valid phone number.";
   ico.innerHTML = "<img src=\"http://www.just-contractors.com/images/error_icon.gif\" class=\"validationIcon\" />";
   allowSubmit = false;
}
function spPass( field )
{
   if( field.name != 'businessPhone' )
      return;

   ValidationDisplay.removeAnyNotices( field );
   
   var err = document.getElementById( 'busPhone-error' );
   var ico = document.getElementById( 'busPhone-icon' );
   field.style.border = 'solid 1px #666';
   field.validData = true;
   
   if( ValidationDisplay.checkForNotRequiredAndEmpty( field ) )
      return;

   if( '' != field.value )
   {
      err.innerHTML = '';
      ico.innerHTML = "<img src=\"http://www.just-contractors.com/images/approve_icon.gif\" class=\"validationIcon\" />";
   }
   allowSubmit = true;
}
function checkSubmit()
{
   if( !allowSubmit )
   {
      alert( "An error has occurred. Please review the following fields:\nBusiness Phone" );
      return false;
   }
   return duplicateSubmitCheck();
}
/* end implementation specifics */