/*
File: internship/scripts/intern-submit-validate-script.js

MAKE SURE YOU KEEP TRACK OF EVERYTHING YOU DO!

Author: Dwight VanTuyl
DD-MMM-YYYY: 14-Mar-2008
Purpose: Validates the submit form either per tab or the whole shebang.
+++++++++++++
Modifications
+++++++++++++
Editor:		
DD-MMM-YYYY:
Watcha did:	
~~~~~~~~~~~~~
*/

Ext.namespace('Intern.submit');

Intern.submit.validate = {

  //Validate the whole submit form
  validateAll: function() {
    //collect all input fields for all form tabs
    var inputElements = $("div#formtabs :input");
    
    return this.validateInputElements(inputElements);
  },
  
  //Validate just a specific tab based on its id
  validateTab: function(tabID) {
    //collect all input fields for the specific tab
    var inputElements = $("div#" + tabID + " :input");
    
    return this.validateInputElements(inputElements);
  },
  
  //Validate any collection of input elements and return false if any are invalid
  validateInputElements: function(inputElements){
    //init error messages
    $("div#errorMsgs").hide();
    $("div#errorMsgs").empty();  
    
    //init valid boolean
    var isValid = true;
    
    //check all required fields
    inputElements.filter(".required").each(function(){   
      if( Intern.submit.validate.required($(this)) &&
          Intern.submit.validate.email($(this)) &&
          Intern.submit.validate.radio($(this))
      ){
        //reset any valid input elements to white
        $(this).css({ background:"white"});
      }else{
        //show error messages and set invalid field background to pink
        $(this).css({ background:"pink"});
        $("div#errorMsgs").show();
        $("div#errorMsgs").append("** " + $(this).attr('error') + "<br/><br/>");
        
        //activate invalid tab based on tab's id extracted from input element's name
        var tabID = $(this).attr('name').split('_')[0];
        Intern.submit.global.tabContainer.activate(tabID);
        $(this).focus();

        //if any input elements are invalid then set valid boolean to false
        isValid = false;
      }
    });	
    
    return isValid;
  },
  
  //test if required input elements have any value
  required: function(input) {
    if (jQuery.trim(input.val()) != ''){
      return true;
    }
  },
  
  //test if the required input element is a valid email address
  email: function(input){	
  	if (input.attr('name').indexOf("email") > -1){  //if the field's name has "email" somewhere, then assume it's an email
  		var validemail = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
  		if (validemail.test(input.val())){
  			return true;
  		}
  	}else{
      return true;
    }
  },
  
  //test whether a required radio button has been selected
  radio: function(input){
    if(input.attr('type') == 'radio'){
      var requiredRadio = $("input[name='" + input.attr('name') + "'][checked]");
      if(requiredRadio.length == 0){
        return false;
      }
    }
    return true;
  }
}
