/* 
   Description:
   This collection of JavaScript functions should help you in validating html forms.
   I hope it works nice for you!!!
   Author: 
   Matteo Foccoli, E-Farm Group
   Thanx to:
   - www.html.it
   - Netscape's "Client Side JavaScript Guide"
   - Enrico Amigoni
   Date:
   02-28-2002
*/

//---- ERROR MESSAGES ----
var emptyError = "";
var mailError = "";
var dateError = "";
var numberError = "";
var integerError = "";
var minSizeError = "";
var maxSizeError = "";
var stringError = "";
var paperFormatError = "";
//------------------------

/* Allows you to initialize language of error reports.
*/
function initializeLang(language) {
  var lang = language.toLowerCase();
  switch(lang){
  	case "it":
  	  emptyError = "Inserire un valore nel campo";
  	  mailError = "Inserire un indirizzo di mail corretto";
  	  dateError = "Inserire una dta valida";
  	  numberError = "Inserire un numero";
  	  integerError = "Inserire un numero intero";
  	  minSizeError = "Il valore inserito è troppo corto";
  	  maxSizeError = "Il valore inserito è troppo lungo";
  	  stringError = "Inserire solo dei caratteri"; 
  	  minValueError = "Il valore inserito è troppo piccolo";
  	  maxValueError = "Il valore inserito è troppo grande";
  	  paperFormatError = "Inserire due numeri separati da una x\n oppure: risma";	  
  	  break;
  	default:
  	  emptyError = "please don't leave blank";
  	  mailError = "Please check mail address"; 
  	  dateError = "Please insert a valid date";
  	  numberError = "Please insert a valid number";
  	  integerError = "Please insert an integer";
  	  minSizeError = "Value is too short";
  	  maxSizeError = "Value is too long";
  	  stringError = "Please use only chars";   
  	  minValueError = "Value is too little";
  	  maxValueError = "Value is too long";
  	  paperFormatError = "Insert two numbers separed by x or: risma";	  
  }
}

/* Just for testing purposes!!! 
*/
function prova() {
  alert("prova!!!");
  return true;
}

/* Use this function for debug purposes.
   As parameters it takes:
   - name of the function to debug
   - name of the elemet passed as argument to the function
   - additional function parameters
   ex) myDebugger("checkDate", element, language)
*/
function myDebugger() {
	var debString = "";
	var paramString = "";
	debString = "Debugging: " + arguments[0] + "\nElement: " + arguments[1];
	for(var i=2; i<arguments.length; i++){
		paramString = "\nParameter[" + (i-2) + "]: " + arguments[i];
	  debString += paramString;
	}
	alert(debString);  
}


/* Checks if a form fill is not empty. Returns false if field is left empty.
*/
function checkNotEmpty(formElement) {
	if(formElement.value == ""){
		alert(emptyError);
		formElement.focus();
		return false;
	}
	return true;
}

/* Checks if a form field contains a valid mail address.
*/
function checkMailAddress(formElement) {
	var mailValue = formElement.value;
  //var emailRe = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
  var emailRe = "[A-Za-z\\.^]+@[A-Za-z\\.]+";
  var regEx = new RegExp(emailRe);
  if(mailValue == "" || !regEx.test(mailValue)){
    alert(mailError);
    formElement.focus();
    return false;
  }
  return true;
}

/* Checks paper format
*/
function checkPaperFormat(formElement) {
  var isOk = false;
  var formatValue = formElement.value;
  temp = formatValue.toLowerCase();
  if(!temp==""){
	  if(temp=="risma")
	    //isOk = true; 
	    isOk = false;
	  else{
		  /* If is not "risma", split string and verify if its peieces 
		  are two numbers separated by a x*/  
		  var pieces = temp.split("x");
		  if(pieces.length==2){
		  	isOk = true;
		    for(var i=0; i<pieces.length; i++){
		      //One of the pieces is not a number
		      if(isNaN(pieces[i]))
		        isOk = false;
		    }   
		  }
		  else{
		    //Too many elements   
		    isOk = false;
		  }   
	  }
  } 
  if(!isOk){
	  alert(paperFormatError);
	  formElement.focus();
		return false;
  }
  return true;	
}

/* Checks if a form field contains a valid date.
*/
function checkDate(formElement, nation) {
	myDebugger("checkDate", formElement.name, nation);
	return true;
}

/* Checks if a form field is a number.
*/
function checkNumber(formElement) {
	var isNumber = true;
	var numValue = formElement.value;
	var elements = numValue.split(",");
	//Se la stringa è composta da + di due pezzi
	if (elements.lenght>2) 
		isNumber = false;
	//Sostitisco le "," con i "."
	else {
		var temp = elements[0];
		for (var i=1; i<elements.length; i++){
		temp = temp + "." + elements[i];  	
	}
	//Se non è un numero
	if (isNaN(temp))
		isNumber = false;	
	}		 
	if (!isNumber) {
		alert(numberError);
		formElement.focus();
		return false;
	}		  
	else
		return true;		
}
 
/* Checks if a form field is an integer.
*/
function checkInteger(formElement) {
	var isNumber = true;
	var numValue = formElement.value;
	var elements = numValue.split(",");
	var elements2 = numValue.split(".");	
	//Se la stringa è composta da + di un pezzi
	if ((elements.length > 1) || (elements2.length > 1)) { 
		isNumber = false;
	}
	//Se non è un numero
	if (isNaN(numValue)){
		isNumber = false;	
	}	
	if (!isNumber) {
		alert(integerError);
		formElement.focus();
		return false;
	}		  
	else
		return true;	
}


/* Checks if a form field doesn't overrun maximum size.
*/
function checkMaxSize(formElement, maxSize) {
	var textValue = formElement.value.length;
	if(textValue > maxSize){
	  alert(maxSizeError);
	  formElement.focus();
	  return false;
	}  
	else  
	  return true;
}

/* Checks if a form field doesn't underrun minimum size.
*/
function checkMinSize(formElement, minSize) {
	var textValue = formElement.value.length;
	if(textValue < minSize){
	  alert(minSizeError);
	  formElement.focus();
	  return false;
	}  
	else  
	  return true;
}

/* Checks if a form field doesn't overrun maximun value and if it is empty.
*/
function checkMaxValue(formElement, maxValue){
  if(!checkNotEmpty(formElement))
    return false;
  var numValue = parseInt(formElement.value);
  if(numValue > maxValue){
    alert(maxValueError);
    formElement.focus();
    return false;
  }
  else  
    return true;
}

/* Checks if a form field doesn't underrun minimum value and if it is empty.
*/
function checkMinValue(formElement, minValue){
  if(!checkNotEmpty(formElement))
    return false;
  var numValue = parseInt(formElement.value);
  if(numValue < minValue){
    alert(minValueError);
    formElement.focus();
    return false;
  }
  else  
    return true;
  return true;
}

/* Checks if a numeric value contained in a form field is 
   between a specified numeric range and if the field is not empty.
*/
function checkRange(formElement, minValue, maxValue){
  if(checkMinValue(formElement, minValue) &&
     checkMaxValue(formElement, maxValue))
    return true;
  else
    return false;  
}

/* Check if a form field contains a not null string.
*/
function checkString(formElement) {
  //myDebugger("checkString", formElement.name);
  return true;
}

/* Validates a form. 
   The first arg is the language used by form.
   As other arguments it takes: field to be checked, type of check to be done, additional parameters.
   Types 				Parameters
   - NUMBER			 
   - NOT_EMPTY	
   - MAIL		
   - DATE				string specifing date type (US, IT)
   - MAX_SIZE		int specifing maximun allowed size
   - MIN_SIZE   int specifing minimum allowed size 
   - STRING
   - MAX_VALUE	int specifing maximun allowed value
   - MIN_VALUE  int specifing minimum allowed value 
   - RANGE      int, int specifing allowed numeric range
   - PAPER_FORMAT 
   - INTEGER
   ex) validateForm("IT", document.form.name, 'NOT_EMPTY', 
                    document.form.mail, 'MAIL',
                    document.form.date, 'DATE', 'US',
                    document.form.num,  'MAX_SIZE', 12)
*/
function validateForm(language) {
	var k = 0;
	var i = 0;
	var isOk = true;
	initializeLang(language);
  for(i=1; i<arguments.length; i++){
  	if(!isOk)
  	  return false;
  	switch(arguments[i]){
  		case "NUMBER":
  		  if(!checkNumber(arguments[k]))
  		    isOk = false;
  		  break;
  		case "INTEGER":
  		  if(!checkInteger(arguments[k]))
  		    isOk = false;
  		  break;      
  		case "NOT_EMPTY":
  		  if(!checkNotEmpty(arguments[k]))
  		    isOk = false;
  		  break;
  		case "MAIL":
  		  if(!checkMailAddress(arguments[k]))
  		    isOk = false;
  		  break;
  		case "STRING":
  		  if(!checkString(arguments[k]))
  		    isOk = false;
  		  break;   
  		case "PAPER_FORMAT":
  		  if(!checkPaperFormat(arguments[k]))
  		    isOk = false;  
  		  break; 
  		case "DATE":
  		  if(!checkDate(arguments[k], arguments[++i]))
  		    isOk = false;
  		  break; 
  		case "MAX_SIZE":
  		  if(!checkMaxSize(arguments[k], arguments[++i]))
  		    isOk = false;   
  		  break;
  		case "MIN_SIZE":
  		  if(!checkMinSize(arguments[k], arguments[++i]))
  		    isOk = false;   
  		  break; 
  		case "MIN_VALUE":
  		  if(!checkMinValue(arguments[k], arguments[++i]))
  		    isOk = false;   
  		  break;
  		case "MAX_VALUE":
  		  if(!checkMaxValue(arguments[k], arguments[++i]))
  		    isOk = false; 		      
  		  break;
  		case "RANGE":
  		  if(!checkRange(arguments[k], arguments[++i], arguments[++i]))
  		    isOk = false; 		      
  		  break; 
  		default:
  		  //Stores last element position
  		  k = i;    
  	}
  }
  if(isOk)
    return true;
  else
    return false;  
}




