/**
 *
 *@desc Validation object 
 *
 */  
 Validation = {}
 
 // holds any error messages
 Validation.errors = new Array();
 
 /**
  *
  *@desc Adds onclick event handler and function call if a form contains 
  *a reqd class name
  *
  */           
 Validation.bindEvents = function() {
 
    // check for forms with reqd class name and assign event handler
    if (document.forms.length > 0) {
    
        var forms = document.forms;
        for (var x = 0; x < forms.length; x++) {
        
          var elems = forms[x].childNodes;
          
          for (var y = 0; y < elems.length; y++) {
          
            // need to put this in otherwise it throws errors
            if (elems[y].className) {
          
              if (elems[y].className.indexOf("reqd") > -1) {
              
                forms[x].onclick = function() {
                
                    return Validation.checkForm(this);
                
                }
                
                break;
              
              }
          
            }
          
          } // closing loop
        
        } // closing loop
    
    } // if contains forms
    
 } // closing function
 
 /**
  *
  *@return boolean
  *@desc This function checks if a given form field is empty
  *
  */         
 Validation.isEmpty = function (field) {
 
    if (field == "") {
    
        return true;
    
    }
    
    return false;
 
 } // closing function
 
 
 Validation.stripUnderscoreAndOrDash = function (text) {
  
    var reg = new RegExp("_");
    text = text.replace(reg, ' ');
    reg = new RegExp("-");
    text = text.replace(reg, ' ');
    return text;
    
    //return text;
 }
 

 Validation.isValidEmail = function (email) {
 
    // RE to capture many, but not all, valid email addresses
    var reg = new RegExp("^[-/0-9a-zA-Z_\.\-]+@[-0-9a-zA-Z_\.\-]+[\.][-0-9a-zA-Z_\.\-]+");
    if (reg.test(email) == true) {
        
        return true;
  
    }
    
    return false;
  
 } // closing function
 
 Validation.isValidNumber = function (telephone) {
 
    // use RE to check for numbers only
    var reg = new RegExp("^[0-9]+$");
    if (reg.test(telephone) == false) {
    
        return false;
    
    }
    
    return true;
   
 } // closing function
 
 
 Validation.isValidPassword = function (password, passwordCopy) {
 
 
    if (password == '') {
    
      return false;
    
    }
 
    if (password == passwordCopy) {
    
      return true;
    
    } else {
    
      return false;
    
    }
   
 } // closing function
  
  
 Validation.implode = function(args) {
 
    var parts = "";
    
    for (var x = 0; x < args.length; x++) {
    
      parts += args[x] + '<br \/>';
    
    }
    
    return parts;
 
 } // closing function
 
  
  /**
   *
   *@desc This function is called by onsubmit.  It calls the appropriate 
   *functions to check the form is completed properly.  Any errors are stored
   *in an array. Valid fields are also stored in array.
   *@return boolean         
   *   
   */        
  Validation.checkForm = function (form) {
  
    // store form elements in array
    var elements = form.elements;
    // empty errors array
    Validation.errors = new Array();
    
    for (var i = 0; i < elements.length; i++) {
    
        // we only need to check any fields marked as reqd
        if (elements[i].className.indexOf("reqd") > -1) {
        
            // if we're specifcally checking an email field is valid...
            if (elements[i].className.indexOf("email") > -1) {
            
              if (Validation.isValidEmail(elements[i].value) == false) {
              
                Validation.errors[Validation.errors.length] = 'Please enter a valid email address';
                elements[i].value = '';
              
              } // closing if false....
            
            } else if (elements[i].className.indexOf("phone") > -1) {
            
             if (Validation.isValidNumber(elements[i].value) == false) {
              
                Validation.errors[Validation.errors.length] = 'Please enter a valid phone number';
                elements[i].value = '';
              
              } // closing if false....
            
           
            // here, the default behaviour
            } else if (elements[i].className.indexOf("password") > -1) {
            
             // cycle through elements looking for password-copy
              for(var z = 0; z < elements.length; z++) {
              
                if (elements[z].className.indexOf("pass-copy") > -1) {
                
                
                    if (Validation.isValidPassword(elements[i].value, elements[z].value) == false) {
                  
                    Validation.errors[Validation.errors.length] = 'Please enter passwords that match!';
                    
                    // no need to continue looping over passwords
                    break;
                  
                    } else {
                    // no need to continue looping over passwords
                    break;
                    
                    }
                
                } // closing if match...
                
              } // closing inner loop
            
            // here, the default behaviour
            } else {
            
            if (Validation.isEmpty(elements[i].value) == true) {
            
                Validation.errors[Validation.errors.length] = 'Please complete the ' + Validation.stripUnderscoreAndOrDash(elements[i].name) + ' field.';
            
            }
            
             }
       
        } 
        
    } // closing element loop
    
    if (Validation.errors.length > 0) {
    
        // we need to display error messages...
        document.getElementById("messages").innerHTML = '<p class="error">' + Validation.implode(Validation.errors) + '<\/p>';
        return false;
    
    } else {
    
        return true;
    
    }
    
  } // closing 
  
  
  window.onload = Validation.bindEvents;
 