/*
** global.js -- hi :)
** joe@mrsomeone.com
*/

function isValidEmail(e) {
   var filter = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,6})+$/;
   if (filter.test(e))
      return true;
   return false;
}

function highlightFormElement(id) {
   var el = $(id);
   el.activate();

   if (this.className != 'contactFormBad')
   {
      //new Effect.Morph(id, { style: 'contactFormBad', duration: 1.0 });
      new Effect.Morph(id, { style: 'background: #FFF; border-color: #FF0000', duration: 1.0 });
      setTimeout("$('"+id+"').className = 'contactFormBad';", 1100);
   }

   if (id == 'f_mail')
   {
      Event.observe(id, 'keyup', function(e) {
         if (this.className == 'contactFormBad')
         {
            if(this.value != '' && isValidEmail(this.value))
            {
               new Effect.Morph(this.id, { style: 'background: #FFF; border-color: #AAF00F', duration: 1.0 });
               setTimeout("$('"+this.id+"').className = 'contactFormGood';", 110);
            }
         }
         if (this.className == 'contactFormGood')
         {
            if(this.value == '' || !isValidEmail(this.value))
            {
               this.className = '';
               new Effect.Morph(this.id, { style: 'contactFormBad', duration: 1.0 });
            }
         }
      });
   }
   else
   {
      Event.observe(id, 'keyup', function(e) {
         if (this.className == 'contactFormBad')
         {
            if(this.value != '')
            {
               new Effect.Morph(this.id, { style: 'background: #FFF; border-color: #AAF00F', duration: 1.0 });
               setTimeout("$('"+this.id+"').className = 'contactFormGood';", 110);
            }
         }
         if (this.className == 'contactFormGood')
         {
            if(this.value == '')
            {
               this.className = '';
               new Effect.Morph(this.id, { style: 'contactFormBad', duration: 1.0 });
            }
         }
      });
   }
}


function submitContactForm(ev) {
   // validate input
   var e = '';
   if($('f_msg').value == '')
   {
      e += "\n * Your Message";
      highlightFormElement('f_msg');
   }
   if($('f_mail').value == '' || !isValidEmail($('f_mail').value))
   {
      e += "\n * Your E-Mail Address";
      highlightFormElement('f_mail');
   }
   if($('f_who').value == '')
   {
      e += "\n * Your Name";
      highlightFormElement('f_who');
   }

   if ( e != '' )
   {
      //alert( "Please fill out the following required items:\n" + e );
   }
   else
   {
      $('f_status').style.display = 'inline';
      $('f_status').innerHTML = '<img src="img/contact-throbber.gif" style="vertical-align: middle;" /> E-mail is navigating the tubes.'
      var params = $('contactForm').serialize();
      params = params + '&fromJS=true';
      new Ajax.Request('http://mrsomeone.com/bland/?do=sendEmail', {
         method: 'post',
         parameters: params,
         onSuccess: function(transport) {
            if(transport.responseText.match(/result=success/))
            {
               $('contactForm').disable();
               $('f_status').innerHTML = 'E-mail successfully sent!';
               var is = $('contactForm').getInputs('text');
               var i;
               for(i in is)
               {
                  if(is[i].style)
                  {
                     is[i].style.color='#DFDFDF';
                     is[i].style.borderColor='#EFEFEF;';
                  }
               }
               $('f_msg').style.color='#DFDFDF;';
               $('f_msg').style.borderColor='#EFEFEF;';
            }
            else
            {
               $('f_status').innerHTML = '<strong>Error!</strong> Please try again.';
            }
         }
      });

   }
   Event.stop(ev);
}

function ucwords( s )
{
   if( 'object' == typeof(s) ) s = s.nextNode.text;
   var result = '';
   var words = s.split(' ');
   var count = words.length;

   for( var i=0; i<count; i++ )
   {
      result += parse( words[i] ) + ' ';
   }
   result += parse( words[i] );

   return result;

   /* private */ function parse( s )
   {
      if( isNaN(parseInt(s)) )
      {
         return s.charAt(0).toUpperCase() + s.substr(1).toLowerCase();
      }
      else {
         return s;
      }
   }
}

function preloadImages() {
   im1 = new Image(16,16);
   im1.src = "img/contact-throbber.gif"
}

Event.observe(window, 'load', function() {
   Event.observe('contactForm', 'submit', submitContactForm);
   preloadImages();
});