// Common JavaScript code across your application goes here.

formReadyCheck = function ( whichForm ) {
  var tehReturn = true;

  $( '#'+whichForm.id+' input.required').each( function(i) {
    //alert('testing ' + $(this).attr('name'));
    if( $(this).val() == '' ){
      alert( $(this).attr('title') );
      $(this).focus();
      tehReturn = false;
      if (!tehReturn) return false;
    }
  });

  if (!tehReturn) return false;
  
  $( '#'+whichForm.id+' input.validate').each( function(i) {
    if( $(this).val() != '' ){
      //alert('validating ' + $(this).attr('name'));
      if($(this).hasClass('email')) {
        var exp = /^([\w-]+(?:\.[\w-]+)*)\@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$|(\[?(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\]?)$/i;
        if( !exp.test( $(this).val() ) ){         
          alert ( $(this).attr('title') );
          $(this).focus();
          tehReturn = false;
          if (!tehReturn) return false;
        }
      } else if($(this).hasClass('phone')) {
        var tehValue = $(this).val();
        tehValue = tehValue.replace(/[^0-9]+/g, '');
        if(( tehValue.length < 10 ) || ( tehValue.length > 11 )){
          alert ( $(this).attr('title') );
          $(this).focus();
          tehReturn = false;
          if (!tehReturn) return false;
        }
      } else if($(this).hasClass('number')) {
        var tehValue = $(this).val();
        if( isNaN( tehValue ) ){
          alert ( $(this).attr('title') );
          $(this).focus();
          tehReturn = false;
          if (!tehReturn) return false;
        }
      } else if( $(this).hasClass('url') ) {
        var exp = /^[A-Za-z0-9\-_]+\.[~A-Za-z0-9-_%&\\\?\/.=]+$/i;
        if( !exp.test( $(this).val() ) ){         
          alert ( $(this).attr('title') );
          $(this).focus();
          tehReturn = false;
          if (!tehReturn) return false;
        }
      }
    }
  });
  return tehReturn;
}