/* =========================================================================

JavaScript Source File -- Created with SAPIEN Technologies Primalcode 3.0

NAME: 

AUTHOR: Aldo Lo Castro , Sevenlab
DATE  : 12/03/2007

COMMENT: 

============================================================================ */

var nbsp = 160;		// non-breaking space char
var node_text = 3;	// DOM text node-type
var emptyString = /^\s*$/ ;
var global_valfield;	// retain valfield for timer thread

// --------------------------------------------
//                  trim
// Trim leading/trailing whitespace off string
// -----

function trim(str)
{
  return str.replace(/^\s+|\s+$/g, '');
}

// --------------------------------------------
//                  msg
// Display warn/error message in HTML element.
// commonCheck routine must have previously been called
// --------------------------------------------

function msg(fld,     // id of element to display message in
             msgtype, // class to give element ("warn" or "error")
             message) // string to display
{
  // setting an empty string can give problems if later set to a 
  // non-empty string, so ensure a space present. (For Mozilla and Opera one could 
  // simply use a space, but IE demands something more, like a non-breaking space.)
  //alert("message:" + message);
  //alert("fld:" + fld);
  //alert("msgtype:" + msgtype);
  
  var dispmessage;
  if (emptyString.test(message)) 
    dispmessage = String.fromCharCode(nbsp);    
  else  
    dispmessage = message;

  var elem = document.getElementById(fld);
  elem.firstChild.nodeValue = dispmessage;  
  
  elem.className = msgtype;   // set the CSS class to adjust appearance of message
  
  //alert('end');
}

// --------------------------------------------
//            commonCheck
// Common code for all validation routines to:
// (a) check for older / less-equipped browsers
// (b) check if empty fields are required
// Returns true (validation passed), 
//         false (validation failed) or 
//         proceed (don't know yet)
// --------------------------------------------

var proceed = 2;  

function commonCheck    (valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required)   // true if required
{
  if (!document.getElementById) 
    return true;  // not available on this browser - leave validation to the server
  var elem = document.getElementById(infofield);
  if (!elem.firstChild) return true;  // not available on this browser 
  if (elem.firstChild.nodeType != node_text) return true;  // infofield is wrong type of node  

  if (emptyString.test(valfield.value)) {
    if (required) {
      msg (infofield, "error", "Errore: Campo Richiesto");  
      setfocus(valfield);
      return false;
    }
    else {
      msg (infofield, "warn", "");   // OK
      return true;  
    }
  }
  return proceed;
}

// --------------------------------------------
//            validatePresent
// Validate if something has been entered
// Returns true if so 
// --------------------------------------------

function validatePresent(valfield,   // element to be validated
                         infofield ) // id of element to receive info/error msg
{
  var stat = commonCheck (valfield, infofield, true);
  if (stat != proceed) return stat;

  msg (infofield, "warn", "");  
  return true;
}

// --------------------------------------------
//            validateNome
// Validate if name has been entered
// Returns true if so false otherwise and displays a message
// --------------------------------------------
function validateNome(valfield,   // element to be validated
                         infofield ) // id of element to receive info/error msg
{                        
	//alert('validateNome');
	 if (emptyString.test(valfield.value)) {
	 	msg (infofield, "error", "Il campo 'Nome' è obbligatorio!");  
	 	return false;
	 }
	 else
	 {	
	 	 //msg (infofield, "error", "corretto;");  
	 	 return true;  
	 }
}

// --------------------------------------------
//            validateCognome
// Validate if surname has been entered
// Returns true if so false otherwise and displays a message
// --------------------------------------------
function validateCognome(valfield,   // element to be validated
                         infofield ) // id of element to receive info/error msg
{                        
	//alert('validateCognome');
	 if (emptyString.test(valfield.value)) {
	 	msg (infofield, "error", "Il campo 'Cognome' è obbligatorio!");  
	 	return false;
	 }
	 else
	 {	
	 	 //msg (infofield, "error", "corretto;");  
	 	 return true;  
	 }
}

// --------------------------------------------
//            validateCotnacts
// Validate if at least on contact has been entered
// Returns true if so false otherwise and displays a message
// --------------------------------------------
function validateContacts(valfield,valfield1,valfield2,   // element to be validated
                         infofield ) // id of element to receive info/error msg
{                        
	//alert('validateContacts');
	 if (emptyString.test(valfield.value) && emptyString.test(valfield1.value) && emptyString.test(valfield2.value)) {
	 	msg (infofield, "error", "Almeno uno tra i campi 'Telefono', 'Email' e 'Fax' deve essere inserito!");  
	 	return false;
	 }
	 else
	 {	
	 	 //msg (infofield, "error", "corretto;");  
	 	 return true;  
	 }
}

// --------------------------------------------
//            validateAzienda
// Validate if at least on contact has been entered
// Returns true if so false otherwise and displays a message
// --------------------------------------------
function validateAzienda(valfield,   // element to be validated
                         infofield ) // id of element to receive info/error msg
{                        
	//alert('validateAzienda');
	 if (emptyString.test(valfield.value)) {
	 	msg (infofield, "error", "Il campo 'Azienda/Ente' è obbligatorio!");  
	 	return false;
	 }
	 else
	 {	
	 	 //msg (infofield, "error", "corretto;");  
	 	 return true;  
	 }
}

// --------------------------------------------
//            validateMessaggio
// Validate if at least on contact has been entered
// Returns true if so false otherwise and displays a message
// --------------------------------------------
function validateMessaggio(valfield,   // element to be validated
                         infofield ) // id of element to receive info/error msg
{                        
	
	 if (emptyString.test(valfield.value)) {
	 	
	 	msg (infofield, "error", "Il campo 'Testo Messaggio' è obbligatorio!");  
	 	//alert("Il campo 'Testo Messaggio' è obbligatorio!");
	 	return false;
	 }
	 else
	 {	
	 	 //msg (infofield, "error", "corretto;");  
	 	 return true;  
	 }
}
