selectAllListItems = function( obj) {
  for ( var i=0; i<obj.form.elements[obj.name].length; i++) {
    obj.form.elements[obj.name][i].checked = obj.checked;
  }
}
open_image_preview = function( a) {
  var img = new Image();
  img.src = a.href;
  var _onload_ = function() {
    var popup_ = window.open( "", "image", "scrollbars=0,width=" + img.width + ",height=" + img.height);
    popup_.document.open();
    popup_.document.write( "<html><body style='margin:0px;padding:0px;'><img src='" + img.src + "'/></body></html>");
    popup_.document.close();
    popup_.focus();
  }
  if ( img.width > 0 ) {
    _onload_();
  } else {
    img.onload = _onload_;
  }
}

isEmpty = new RegExp( "^\s*$", "i");
isNumber = new RegExp( "^\s*[0-9]+\s*$", "i");
isEmail = new RegExp( "^\s*[a-z_0-9]+@[a-z_0-9]+.[A-z_0-9]+\s*$", "i");

validate = function( f) {
  var errors = [];
  for( var i=0; i<f.elements.length; i++) {
    var valid_types = f.elements[i].getAttribute( "valid_types");
    var valid_label = f.elements[i].getAttribute( "valid_label");
    if ( valid_types ) {
      var value = f.elements[i].value;
      valid_types = valid_types.split( /\s*,\s*/);
      for( var i2=0; i2<valid_types.length; i2++) {
        switch( valid_types[i2] ) {
          case "req": {
            if ( isEmpty.test( value) ) {
              errors.push( "'" + valid_label + "' is required.");
              i2 = 100000;
            }
            break;
          }
          case "email": {
            if ( !isEmail.test( value) ) {
              errors.push( "Enter valid '" + valid_label + "'.");
              i2 = 100000;
            }
            break;
          }
          case "number": {
            if ( !isNumber.test( value) ) {
              errors.push( "Enter valid '" + valid_label + "'.");
              i2 = 100000;
            }
            break;
          }
        }
      }
    }
  }
  if ( errors.length ) {
    alert( errors.join( "\n") )
    return false;
  } else {
    return true;
  }
}
