String.prototype.trim = function() 
{
   return( this.replace( /^\s+|\s+$/g, "" ) );
}
String.prototype.ltrim = function() 
{
   return( this.replace( /^\s+/, "" ) );
}
String.prototype.rtrim = function() 
{
   return( this.replace( /\s+$/, "" ) );
}

function numbersOnly( el )
{
   el.value = el.value.replace( /[^0-9.]/g, "" );
}

function limitTextArea( el, lbl, limitNum )
{
   if ( el.value.length > limitNum )
      el.value = el.value.substring( 0, limitNum )
   lbl.value = ( limitNum - el.value.length );
}

function limitText( box, id, limit )
{
   if ( box.value.length > limit ) 
      { box.value = box.value.substring( 0, limit ); }
   document.getElementById( id ).value = ( limit - box.value.length );
}

function login( form, name ) 
{
   var file = ( 'content/scripts/_' + name + '.php' );
   form.action = file;
   return( true );
}

function openPDF( type, name, dir )
{
   cme = ( dir + 'content/pdfs/2007.' + type + '.' + name + '.pdf' );
   window.open( cme, 'mywindow', 'location=no,menubar=no,toolbar=no,resizable=yes,directories=no' );
   return( true );
}

function openPDF2( year, type, name, dir )
{
   cme = ( dir + 'content/pdfs/' + year + '.' + type + '.' + name + '.pdf' );
   window.open( cme, 'mywindow', 'location=no,menubar=no,toolbar=no,resizable=yes,directories=no' );
   return( true );
}


function toggleInsurance( document )
{
   if ( ( document.prev_insurance[0].checked ) && ( document.prev_insurance_company.disabled ) )
   {
      document.prev_insurance_company.disabled=false;
      document.prev_insurance_company.value='';
      document.prev_insurance_policy.disabled=false;
      document.prev_insurance_policy.value='';
      document.date_d_99.disabled=false;
      document.date_m_99.disabled=false;
      document.date_y_99.disabled=false;
      document.date_but_99.disabled=false;
   }
   else if ( document.prev_insurance[1].checked )
   {
      document.prev_insurance_company.disabled=true;
      document.prev_insurance_company.value='Not Applicable';
      document.prev_insurance_policy.disabled=true;
      document.prev_insurance_policy.value='Not Applicable';
      document.date_d_99.disabled=true;
      document.date_m_99.disabled=true;
      document.date_y_99.disabled=true;
      document.date_but_99.disabled=true;
   }
}

String.prototype.trim = function () 
{
   return this.replace(/^\s*|\s*$/,"");
}

function fieldcheck( form, name ) 
{
   var doSubmit=new Boolean();
   doSubmit = true;

   if ( !document.getElementsByTagName )
      { return; }

   var file         = ('../scripts/submit.' + name + '.php');
   var count        = 0;
   var allinputs    = document.getElementsByTagName( "input" );
   var allselects   = document.getElementsByTagName( "select" );
   var alltextareas = document.getElementsByTagName( "textarea" );
   //
   //
   //
   for ( var i = 0; i < alltextareas.length; ++i )
   {
      var textarea = alltextareas[ i ];
      textarea.value = textarea.value.replace(/['"<>!&;`]/g,'');
   }
   //
   // Loop through all input tags and locate incomplete fields
   //
   for ( var i = 0; i < allinputs.length; ++i )
   {
      var input = allinputs[ i ];
      //
      // Remove formatting on filled fields
      //
      input.style.backgroundColor = 'white';
      input.style.color           = 'brown';
      //
      // Flag as incomplete if...
      //    Field class IS 'req' AND
      //    Field is not disabled AND
      //    Field is empty 
      //
      input.value = input.value.replace(/['"<>!&;`]/g,'');
      if ( ( input.className == 'req' ) &&
           ( !(input.disabled) ) && 
           ( input.value.trim() == '' ) )	
      {
         ++count;
         input.focus();
         input.style.backgroundColor = 'red';
         input.style.color           = 'white';
      }
   }
   //
   // Loop through all select tags locate incomplete fields
   //
   for ( var i = 0; i < allselects.length; ++i )
   {
      var select = allselects[i];
      // 
      // Remove formatting on filled fields
      //
      select.style.backgroundColor = 'white';
      select.style.color           = 'brown';
      //
      // Flag as incomplete if...
      //    Field class IS 'req' AND
      //    Field is not disabled AND
      //    Field is empty 
      //
      if ( ( select.className == 'req' ) &&
           ( !(select.disabled) ) && 
           ( select.value.trim() == '' ) )
      {
         ++count;
         select.focus();
         select.style.backgroundColor = 'red';
         select.style.color           = 'white';
      }
   }
   //
   // If any required but incomplete fields were found, 
   // warn user and return 'false' indicating that page should
   // not be submitted.
   //
   if ( count > 0 )
   {
      alert( 'NOTE: fields marked red must be filled to continue' );
      doSubmit = false;
      return( false );
   }
   //
   // All required fields are present. Submit the form.
   //
   if ( doSubmit )
   {
      form.action = file;
      form.submit();
      return( true );
   }
}

function formsend( form, name )
{
   var doSubmit=new Boolean();
   doSubmit = true;
   var file = ('../scripts/submit.' + name + '.php');

   if( doSubmit )
   {
      form.action = file;
      form.submit();
      return( true );
   }
}

