/*
Mostly taken from the unobtrusive javascript tutorial by Christian Heilmann
http://www.onlinetools.org/articles/unobtrusivejavascript/chapter5.html

The code is licensed under the Creative Commons License - http://creativecommons.org/licenses/by-nd-nc/1.0/ 

Adapted for the Voidspace Python Guestbook, by Michael Foord
http://www.voidspace.org.uk/python/guestbook.html

guestform.js
Voidspace Version 0.1.0     2005/04/22

ISSUES/TODO
Contains some presentation elements defined in the Javascript.
It would be better to define different classes and change class (rather than apply the style) from the script. 
*/

   function checkform(of)
    {
     var reqfields, em, i, f, ty;
     if(document.getElementById('required'))
     {
      reqfields = document.getElementById('required').value.split(',');
      if(document.getElementById('errormsg')){
          em=document.getElementById('errormsg');
          em.parentNode.removeChild(em);
          }

      for(i=0;i<reqfields.length;i++)
      {
       f = document.getElementById(reqfields[i]);
      
       if (f.previousSibling && /img/i.test(f.previousSibling.nodeName)) {
           f.parentNode.removeChild(f.previousSibling); }
       f.style.background='transparent';

       if(f){
        ty = f.type.toLowerCase();
        switch(ty)
        {
         case 'text':
          if(f.value==''){adderr(f.id, of)}       
         break;
         case 'textarea':
          if(f.value==''){adderr(f.id, of)}       
         break;
        }
       }
      }

     f = document.getElementById('email');
     if (f && f.previousSibling && /img/i.test(f.previousSibling.nodeName)) {
       f.parentNode.removeChild(f.previousSibling); }
     f.style.background='transparent';      

     f = document.getElementById('homepage');
     if (f && f.previousSibling && /img/i.test(f.previousSibling.nodeName)) {
       f.parentNode.removeChild(f.previousSibling); }
     f.style.background='transparent';   

     if(document.getElementById('email') && document.getElementById('email').value && !isEmailAddr(document.getElementById('email').value)){
          pn=document.createElement('p');
          pn.appendChild(document.createTextNode('Your email seems to be invalid'))
          adderr('email', of)
          document.getElementById('errormsg').appendChild(pn);
     }
     if(document.getElementById('homepage') && document.getElementById('homepage').value && !isURL(document.getElementById('homepage').value)){
          pn=document.createElement('p');
          pn.appendChild(document.createTextNode('Your homepage URL seems to be invalid'))
          adderr('homepage', of)
          document.getElementById('errormsg').appendChild(pn);
     }

     }       

     if(document.getElementById('errormsg'))
     {
      return false;
     }
    }

    function adderr(id,of)
    {
         var se, i, nli, na;
         i = document.createElement('img');
         i.src='/images/alert.gif';
         i.alt='Error';
         i.title='This field has an error!';
         se=document.getElementById(id);
         se.parentNode.insertBefore(i,se)
         se.style.background='#fcc';

         if(!document.getElementById('errormsg')){
          var em=document.createElement('p');
          em.id='errormsg';
          em.style.border='2px solid #c00';
          em.style.padding='5px';
          em.style.width='20em';
          em.appendChild(document.createTextNode('Please enter or change the fields marked with an '))
          i=document.createElement('img');
          i.src='/images/alert.gif';
          i.alt='Error';
          i.title='This field has an error!';
          em.appendChild(i);

          for(var i=0;i<of.getElementsByTagName('input').length;i++)
          {
           nowelm=of.getElementsByTagName('input')[i];
           if(/submit/i.test(nowelm.getAttribute('type')))
           {
            var sb=nowelm;
            break;
           }
      }
      sb.parentNode.insertBefore(em,sb);
     }

    }
    function isEmailAddr(str) 
    {
        return str.match(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/);
    }

    function isURL(str) 
    {
        return str.match(/^(http\:\/\/)?[\w-]+(\.[\w-]+)*\.+[a-zA-Z]{2,7}$/);
    }
