/***********************************************************************   
   Filename: functions.js
   Purpose:  Generic page level functions
   Authors:  Pat Heard
 ***********************************************************************/


/*
 * Page level functions
 */
var page = {

  onload : function(){
  
    // Force the center column to be the longest for the dumber browsers
    var c = document.getElementById('center');
    var l = document.getElementById('left');    
    if(c && l) {
      var cOH = c.offsetHeight;
      var lOH = l.offsetHeight;      
      if(lOH > cOH) c.style.marginBottom = lOH - cOH + 30 + 'px';
    }
  
    // Setup value toggle on elements
    var togglers = utils.getElementsByClass('toggler'); 
    for(var i = 0; i < togglers.length; i++){    
      togglers[i].onfocus = function(){
        toggle.value(this)
      };
      togglers[i].onblur = function(){
        toggle.value(this)
      };
    } 
    
    // Setup submit handlers on forms    
    validate.submit();  
  }
};



/*
 * Toggles an element's value
 */
var toggle = {    
  
  value : function(elem){ 
    if(elem) {
      if(elem.value == elem.title) 
        elem.value = "";        
      else if(elem.value == "") 
        elem.value = elem.title;      
    }
  }
};


/*
 * Creates the top yellow bar popup
 */
var popup = {

  closeImg : '<img src="/images/bg/messages-close.png" alt="Close"/>',

  create : function(msg, clazz){
    var msgs = document.getElementById('messages');
    if(msgs) {
      msgs.innerHTML = '<p class="'+clazz+'">' + msg + popup.closeImg + '</p>'
      msgs.style.display = 'block';      
    }
    msgs.onclick = function(){    
      this.style.display = 'none';
      this.innerHTML = '';
    };
  }

};



/*
 * Validates form submission
 */
var validate = {
    
  errorNoTerm   : 'You must enter a search term',
  errorTooShort : 'The search term must be at least 3 characters',
  errorIgnore   : 'Your search term is too generic: ',  
  errorNotBlank : ' cannot be left blank',
  errorEmail    : 'E-mail address is not valid',
  
    
  submit : function(){
  
  
    // Search Form
    var sf = document.getElementById('searchForm');
    if(sf){
      sf.onsubmit = function(){
        var error = '';
        var keywords = this.keywords.value.trim();
        
        if (keywords == '' || keywords == 'Find the DVLA information you need fast') 
          error = validate.errorNoTerm;          

        if (keywords.length < 6)
          error = validate.ignore(keywords);

        if (keywords.length < 3)
          error = validate.errorTooShort;

        if (error.length > 0) {
          popup.create(error, 'error');
          return false;
        }  
          
        return true;
      }
    }
    
    // Autoresponder Submission Form
    var af = document.getElementById('formSpecialReport');
    if(af){
      af.onsubmit = function(){
        
        var error = '';
        
        var name = this.name.value.trim();
        var from = this.from.value.trim();
        
        if(name.length == 0 || name == 'enter your name')
          error = "Name" + validate.errorNotBlank;
          
        if(from.length == 0 || from == 'enter your e-mail')
          error = "E-mail " + validate.errorNotBlank;
          
        if(!validate.isValidEmail(from))
          error = validate.errorEmail;          
      
        if (error.length > 0) {
          popup.create(error, 'error');
          return false;
        }       
      
        return true;
      }
    }
    
    // Contact Us Form
    var cf = document.getElementById('contact_form');
    if(cf){
      cf.onsubmit = function(){
        
        var error = '';
        
        var name = this.name.value.trim();
        var from = this.from.value.trim();
        var subject = this.subject.value.trim();
        var message = this.message.value.trim();
        var captcha = this.captcha.value.trim();
        
        if(captcha.length == 0)
          error = "Captcha" + validate.errorNotBlank;
          
        if(message.length == 0)
          error = "Message" + validate.errorNotBlank;
          
        if(subject.length == 0)
          error = "Subject" + validate.errorNotBlank;
         
        if(from.length == 0)
          error = "E-mail " + validate.errorNotBlank;
          
        if(!validate.isValidEmail(from))
          error = validate.errorEmail;    
          
          
        if(name.length == 0)
          error = "Name" + validate.errorNotBlank;
          
        if (error.length > 0) {
          popup.create(error, 'error');
          return false;
        }       
      
        return true;
      }
    }    
  }, 

  
  ignore : function(keywords){
    
    var ignore = ['about','and','but','from','how','that','the','this','was','what','when','where','which', 'with'];
    for(var i = 0; i < ignore.length; i++)
      if(keywords == ignore[i])      
        return validate.errorIgnore + '<strong>' + ignore[i] + '</strong>'     
    
    return '';
  },
  
  isValidEmail : function(email){
     var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
     return re.test(email);

  }
  
}; 


/*
 * Helper functions
 */
var utils = {

  // Get elements by className
  getElementsByClass : function(searchClass, node, tag) {
    var classElements = new Array();
    if ( node == null )
      node = document;
    if ( tag == null )
      tag = '*';
    var els = node.getElementsByTagName(tag);
    var elsLen = els.length;
    var pattern = new RegExp("(^|\\\\s)"+searchClass+"(\\\\s|$)");
    for (i = 0, j = 0; i < elsLen; i++) {
      if ( pattern.test(els[i].className) ) {
        classElements[j] = els[i];
        j++;
      }
    }
    return classElements;
  }
  
};



// Left and right trim
String.prototype.trim = function() {
  a = this.replace(/^\s+/, '');
  return a.replace(/\s+$/, '');
};

// Event handlers
window.onload = page.onload;

// 'Add This' account code
addthis_pub  = 'DVLAGuide';