
/**
 * vaidate email
 */
function valid_email(address) {
  var pattern = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/;
  return pattern.test(address);
}


/**
 * shows an error in the given element
 *
 * @param string el
 * @param string text (optional)
 * @param boolean stay (optional)
 */ 
function show_error(el, text, stay)
{
  // fetch error heading
  var error = global_error
  // append text
  if (text) error += text;
  // show error
  $j(el)
    .removeClass('success')
    .addClass('error')  
    .html(error)
    .slideDown('slow'); 
  // set for hiding?
  if (!stay) setTimeout(function() { $j(el).slideUp('slow'); }, timeout_message);
}

/**
 * shows an error in the given element
 *
 * @param string el
 * @param string text (optional)
 * @param boolean stay (optional)
 */ 
function show_form_error(el, text, stay)
{
  // show error
  $j(el)
    .removeClass('success')
    .addClass('error')  
    .html(text)
    .slideDown('slow'); 
  // set for hiding?
  if (!stay) setTimeout(function() { $j(el).slideUp('slow'); }, timeout_message);
}


/**
 * shows message in the given element
 *
 * @param string el
 * @param string text (optional)
 * @param boolean stay (optional)
 */ 
function show_message(el, text, stay)
{
  // show error
  $j(el)
    .removeClass('error')
    .addClass('success')  
    .html(text)
    .slideDown('slow'); 
  // set for hiding?
  if (!stay) setTimeout(function() { $j(el).slideUp('slow'); }, timeout_message);
}


/**
 * shows message (ajax object) in the given element
 *
 * @param string el
 * @param object data (optional)
 */ 
function show_field_message(el, data)
{
  try {
    // check answer is correctly formed
    if (!('ok' in data && 'message' in data)) throw 'missing properties';
    // check if it is an exception 
    if (data.ok == 'E!') return show_application_exception(el, data);
    // determine message class
    var class_add    = (data.ok) ? 'success' : 'error';
    var class_remove = (data.ok) ? 'error' : 'success';
    // show message
    $j(el)
      .addClass(class_add)
      .removeClass(class_remove)
      .html(data.message);
    // show element
    if ($j(el).is(':hidden')) $j(el).slideDown('slow');       
  }
  catch(e)
  {
    show_error(el, 'resposta ajax incompleta !', 1);
  }  
}


/**
 * shows message (ajax object) in the given element
 *
 * @param string el
 * @param object data (optional)
 */ 
function show_application_exception(el, data)
{
  try {
    // check answer is correctly formed
    if (!('ok' in data && 'message' in data)) throw 'missing properties';
    // determine message class
    var class_add    = 'error';
    var class_remove = 'success';
    // show message
    $j(el)
      .addClass(class_add)
      .removeClass(class_remove)
      .html(data.message);
    // show element
    if ($j(el).is(':hidden')) $j(el).slideDown('slow');       
  }
  catch(e)
  {
    show_error(el, 'resposta ajax incompleta !', 1);
  }  
}
