document.observe('dom:loaded', function () {
	// register events to clear the form fields
   $$('.searchBar input[type=text]').invoke('observe', 'focus', function(evt) {
      evt.stop();
      if(defaultValuesList.get(this.readAttribute('name')).toLowerCase() == $F(this).toLowerCase())
      {
      	this.clear();
      	this.removeClassName('grayed');
      }
   });

	// register events to put the default values back in the form field if the field is empty
   $$('.searchBar input[type=text]').invoke('observe', 'blur', function(evt) {
      evt.stop();
      if($F(this) == '')
      {
         this.addClassName('grayed');
      	this.setValue($w(defaultValuesList.get(this.readAttribute('name'))).join(' '));
      }
   });

   // register event to switch between different searchforms
   $$('.formToggle').invoke('observe', 'click', function ( evt ) {
      evt.stop();
      $$('.formToggle').invoke('removeClassName', 'current');
      this.addClassName('current');
      // get the name of the element to toggle from the classes
      var toggle = $w( this.className ).grep(/^toggle\-/).first().sub(/^toggle\-/, '');
      document.getElementById("errorMessage").innerHTML = "";
      $$('.searchBar').each(function(searchBar_element) {
      	   searchBar_element.select('form').each( function(form) {
               if(!form.hasClassName(toggle))
               {
                  form.hide();
               }
               else
               {
                  form.show();
               }
            });
      });
      // Focus the first field in this form
   });

   // allow to click on the picture around field element and still select the field element
   $$('.inputOpen').invoke('observe', 'click', function(evt){
   	if(evt.element().firstDescendant())
      {
      	evt.element().firstDescendant().activate();
      }
   });
});

function validateEmailForm(form)
{
   with(form)
   {
      if(qee.value != null)
      {
         if(qee.value == "Email (john123@example.com)")
         {
            document.getElementById("errorMessage").innerHTML = "Please fill in an email address before clicking 'iSearch'.";
            return false;
         }
         splitEmail = qee.value.split("@");
         if (splitEmail.length < 2)
         {
            document.getElementById("errorMessage").innerHTML = "You have entered an invalid email address.  Please enter a full email address.  (Ex: john@example.com).";
            return false;
         }
         if(splitEmail[0].length < 1 || splitEmail[1].length < 1)
         {
            document.getElementById("errorMessage").innerHTML = "You have entered an invalid email address.  Please enter a full email address.  (Ex: john@example.com).";
            return false;
         }
         splitDomain = splitEmail[1].split(".");
         if(splitDomain.length < 2)
         {
            document.getElementById("errorMessage").innerHTML = "You have entered an invalid email address.  Please enter a full email address.  (Ex: john@example.com).";
            return false;
         }
         if(splitDomain[0].length < 1 || splitDomain[1].length < 1)
         {
            document.getElementById("errorMessage").innerHTML = "You have entered an invalid email address.  Please enter a full email address.  (Ex: john@example.com).";
            return false;
         }
         return true;
      }
      else
      {
         document.getElementById("errorMessage").innerHTML = "Please enter an email address before pressing 'iSearch'.";
         return false;
      }
   }
}

function validateNameForm(form)
{
   with(form)
   {
      if(qname.value.length == 0 || qname.value == "Full Name")
      {
         document.getElementById("errorMessage").innerHTML = "Please fill in a name before clicking 'iSearch'.";
         return false;
      }
   }
}

function validatePhoneForm(form)
{
   with(form)
   {
      if(qp.value.length == 0 || qp.value == "Phone (555-555-1234)")
      {
         document.getElementById("errorMessage").innerHTML = "Please fill in a phone number before clicking 'iSearch'.";
         return false;
      }
   }
}

function validateScreenNameForm(form)
{
   with(form)
   {
      if(qscrn.value.length == 0 || qscrn.value == "Screen Name (John123)")
      {
         document.getElementById("errorMessage").innerHTML = "Please fill in a screen name before clicking 'iSearch'.";
         return false;
      }
   }
}