var onloads = new Array();//Stack for calling multiple functions on document onload.  
function onload_init(){ onloads.invoke('call');
  //onloads.clear(); 
}
Event.observe(window, 'load', onload_init);
  
function validateField(event) {    
  if ($F(this) == "" && this.hasClassName('req')) {
    $(this.previous(0)).setStyle({ 'color': 'red' });
  } else {
    $(this.previous(0)).setStyle({ 'color': '#000' });
    
    var class_name = this.classNames().toArray().first();
    
    switch (class_name) {
      case "string":
        // no validation for strings, not empty being done
        break;
        
      case "email":
        re = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i;
        testRegExp(this, re.test($F(this))); 
        
        if (this.hasClassName('viral')) {
          var str = $F(this);
          console.log('search:', str.search(/gamestop.com/i))
          if (str.search(/gamestop.com/i) != -1) {
            testRegExp(this, false);
          }
        }
        break;
        
      case "zip":
        re = /^\d{5}$/;
        testRegExp(this, re.test($F(this)));
        break;
        
      case "phone1":
        re = /^\d{3}$/;
        testRegExp(this, re.test($F(this)));
        break;
      
      case "phone2":
        re = /^\d{4}$/;
        testRegExp(this, re.test($F(this)));
        break;
      
      case "store_num":
        if (($F(this) >= 4 && $F(this) <= 6399)) {
          testRegExp(this, true);
        } else {
          testRegExp(this, false);
        }
        break;
        
      case "emp_num":
        re = /^\d{6}$/;
        testRegExp(this, re.test($F(this)));
        break;
        
      default:
        
    }
  }
}

function testRegExp(i, re_result) {
  if(re_result == false) {
    $(i.previous(0)).setStyle({ 'color': 'red' });
  } else {
    $(i.previous(0)).setStyle({ 'color': '#000' });
  }
}

function validateForm(event) {
  var incomplete = this.getElements().findAll(function(i) {
                                      if (i.hasClassName('req')) {
                                        return i.value == "";
                                      } else {
                                        return false;
                                      }
                                    });

  if (incomplete.length > 0) {
    incomplete.each(function(i) {
                 $(i.previous(0)).setStyle({ 'color': 'red' });
                 document.getElementById('warnings').innerHTML = "please fill in all required fields (marked in red)";
               });
               
    Event.stop(event);
  }
  
}