
// function taken and modified from phpMyAdmin javascript libraries
function confirmLink(theLink, message)
{
  // Confirmation is not required if Opera (crappy js implementation)
  if( typeof(window.opera) != 'undefined' ) {
    return true;
  }
  
  return confirm(message);
}


// function for popping up a window
function popup(url, name, width, height)
{
  settings = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=" + width + ",height=" + height;
  mwin=window.open(url,name,settings);
  mwin.resizeTo(width,height);
}



////////////////////////////////////////////////////////////////////////////////
// javascript functions to check the required fields of a form
////////////////////////////////////////////////////////////////////////////////

// function for checking if a text field is empty
function isEmpty( field ) {
  str = field.value;
  if(str == "") {
    return true;
  }
  else {
    for( j = 0; j < str.length; j++) {
      if( str.charAt(j) != " " ) {
	return false;
      }
    }
  }
  
  return true;
}


// function for checking if a text field is empty
function isEmpty( field ) {
  str = field.value;
  if(str == "") {
    return true;
  }
  else {
    for( j = 0; j < str.length; j++) {
      if( str.charAt(j) != " " ) {
	return false;
      }
    }
  }
  
  return true;
}


// function for checking if a text field is a vaild e-mail
function isValidEmail( field ) {
  str = field.value;
  eml = new RegExp("^([0-9a-zA-Z]([-_.\w]*[0-9a-zA-Z])*@(([0-9a-zA-Z])+([-\w]*[0-9a-zA-Z])*\.)+[a-zA-Z]{2,9})$" );
  return eml.exec(str);
}


// function for checking if a text field is a vaild password
function isValidPassword( field ) {
  str = field.value;

  pwd = new RegExp("^[a-zA-Z0-9_.]{4,12}$" );
  return pwd.exec( str );
}


// function for checking if a text field is a vaild username
function isValidUsername( field ) {
  str = field.value;
  unm = new RegExp("^[a-zA-Z0-9_.]{4,12}$" );
  return true;
}



// function to validate a form
// 
// Function expects form elements to have properties            
// added in the HTML code, like this:
//
// <FORM onSubmit = "
//   this.element_name.required = true;
//   this.element_name.email = true; 		for email fields
//   this.element_name.passwd = true; 		for password fields
//   this.element_name.username = true; 	for username fields
//   return validateForm(this);
// ">
// N.B.: this code will be produced by a php function
function validateForm( form ) {
  message = "Alcuni valori obbligatori non sono stati definiti:\n";
  goOn = true;

  // loop through form elements
  for( i = 0; i < form.elements.length; i++ ) {
    var el = form.elements[i];
    if( el.required && isEmpty(el) ) {
      message += "\n   - '" + el.itname + "' è un campo obbligatorio";
      goOn     = false;
    }
    else if( el.eml && !isValidEmail(el) ) {
      message += "\n   - il valore immesso nel campo'" + el.itname + "' non è valido";
      goOn     = false;
    }
    else if( el.pwd && !isValidPassword(el) ) {
      message += "\n   - il valore immesso nel campo'" + el.itname + "' non è valido";
      goOn     = false;
    }
    else if( el.unm && !isValidUsername(el) ) {
      message += "\n   - il valore immesso nel campo'" + el.itname + "' non è valido";
      goOn     = false;
    }
  }  

  if( !goOn )
    alert( message );
  return goOn;
}

