
/**************************************************************
* Funzioni base (utilizzate in altre funzioni di validazione) *
**************************************************************/

// ritorna false se l'argomento ha lunghezza 0, true altrimenti
// Argomenti: str tipo stringa
function notNull(str) {
	if (str.length == 0 )
		return false
	else 
		return true
}

// ritorna false se l'argomento è costituito da soli spazi o
// ha lunghezza 0, true altrimenti
// Argomenti: str tipo stringa
function notBlank(str) {
	for (i = 0; i < str.length; i++) {
		if (str.charAt(i) != " ")
			return true
	}
	return false
}

// ritorna true se il primo argomento è una stringa di 
// lunghezza pari al valore del secondo argomento
// Argomenti: str tipo stringa, size intero
function isSize(str, size) {
	if (str.length == size) 
		return true
	else
		return false
}

// ritorna true se l'argomento è una stringa costituita
// unicamente da caratteri numerici, false altrimenti
// Argomenti: str stringa
function isDigits(str){ 
	var myExp=/^[0-9]*$/;
	
	return myExp.test(str)
}

// ritorna true se l'argomento è una stringa composta unicamente
// da caratteri alfanumerici
// Argomenti: str stringa
function isAlfanumeric(str){
	var myExp=/^[a-z0-9]*$/i;
	
	return myExp.test(str)
}

// ritorna true se la stringa rappresenta un numero valido,
// con o senza decimali, false altrimenti. Il formato .nn 
// (Es: .123) è considerato valido
// Argomenti: str stringa
function isNumber(str){
	var myExp=/(^\d+$)|(^\d*\,\d+$)/
	
	return myExp.test(str)
}

// ritorna true se il primo argomento rappresenta un numero
// compreso tra il secondo (valore minimo) e il terzo 
// (valore massimo) argomento
// Argomenti: str stringa,  num1,num2 numerici
function isInRange(str, num1, num2) {
	var i = parseInt(str,10)
	
	return((i >= num1) && (i <= num2))
}

/*******************************
* Validazione formati speciali *
*******************************/

// ritorna true se l'argomento è una password valida
// Argomenti: str stringa
function isPassword(str){
	var myExp=/^[a-z0-9_]*$/i;
	
	if (!isInside(str,5,20)) return false;
	return myExp.test(str)
}

// ritorna true se l'argomento è un nickname valido
// Argomenti: str stringa
function isNickname(str){
	var myExp=/^[a-z0-9_]*$/i;
	
	if (!isInside(str,3,10)) return false;
	return myExp.test(str)
}


// ritorna true se la data rappresentata dal giorno pari al
// primo argomento, dal mese pari al secondo e dall'anno pari 
// al terzo è valida, false altrimenti
// Argomenti gg,mm,aaaa stringhe rappresentanti interi
// (aaaa anno in formato esteso a 4 cifre)
function isDate(gg,mm,aaaa){ 
	// gg, mm, aaaa sono stringhe tutte di caratteri numerici
	if (!isDigits(gg)) return false;
	if (!isDigits(mm)) return false;
	if (!isDigits(aaaa)) return false;
	
	giorno = parseInt(gg,10);
	mese = parseInt(mm,10);
	anno = parseInt(aaaa,10);
	
	if (anno<1900) return false;
	if (anno>2010) return false;
	
	if (!isInRange(giorno,1,31)) return false;
	if (!isInRange(mese,1,12)) return false;
	
	testDate = new Date(anno,mese-1,giorno);
	
	return ( testDate.getMonth()==(mese-1) && 
			 testDate.getDate()==(giorno)    )
}

// ritorna true se la stringa è contiene solo caratteri
// numerici e  +-/ o spazio, false altrimenti
// Argomenti: str stringa
function isPhone(str){	
	
	newstring = stripChars(str, "+ -/");
	if (isDigits(newstring)) return true;
	return false
}


/*******************************
* Validazione formati standard *
*******************************/

// ritorna true se l'argomento è una stringa non 
// vuota e non costituita da soli spazi
// Argomenti: str stringa
function isValidString(str) {
	if (notNull(str)&& notBlank(str)) 
		return true;
	return false;
}

// ritorna true se il primo argomento è una stringa
// di lunghezza superiore al valore del secondo argomento,
// false altrimenti
// Argomenti: str stringa, maxLength numerico
function isLonger(str,maxLength) {
	if (str.length>maxLength)
		return true;
	return false
}

// ritorna true se il primo argomento è una stringa
// di lunghezza inferiore al valore del secondo argomento,
// false altrimenti
// Argomenti: str stringa, minLength numerico
function isShorter(str,minLength) {
	if (str.length<minLength)
		return true;
	return false
}

// ritorna true se la lunghezza della stringa passata come 
// primo argomento è compresa tra i valori rappresentati
// dal secondo (minimo) e il terzo (massimo) argomento,
// false altrimenti
// Argomenti: str stringa, minLength,maxLength numerici
function isInside(str,minLength,maxLength) {
	if (!isShorter(str,minLength) && !isLonger(str,maxLength))
		return true;
	return false
}

/********
* Varie *
********/

// ritorna true se l'oggetto radio passato per argomento 
// ha una selezione effettuata, false altrimenti
// Argomenti: radio INPUT di tipo radio
function isRadioSelected(radio) {
	for (i=0 ; i< radio.length ; i++){
		if (radio[i].checked) return true;
	}
	return false
}

// ritorna l'indice dell'oggetto radio passato per argomento 
// che ha una selezione effettuata, -1 altrimenti
// Argomenti: radio INPUT di tipo radio
function RadioSelected(radio) {
	for (i=0 ; i< radio.length ; i++){
		if (radio[i].checked) return i;
	}
	return -1;
}


function RadioSelectedValue(radio) {
	for (i=0;i<radio.length;i++) { 
			if (radio[i].checked) { 
					return radio[i].value; 
			} 
	} 
}



function isEmail(emailStr)
{
	var x = emailStr;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)) return true;
	else return false;
}




function isValidIp( IPvalue ) {
	errorString = "";
	theName = "IPaddress";
	
	var ipPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
	var ipArray = IPvalue.match(ipPattern); 
	
	if (IPvalue == "0.0.0.0")
	errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
	else if (IPvalue == "255.255.255.255")
	errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
	if (ipArray == null)
	errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.';
	else {
	for (i = 0; i < 4; i++) {
	thisSegment = ipArray[i];
	if (thisSegment > 255) {
	errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.';
	i = 4;
	}
	if ((i == 0) && (thisSegment > 255)) {
	errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
	i = 4;
		  }
	   }
	}
	extensionLength = 3;
	if (errorString == "")
		return true;	//alert ("That is a valid IP address.");
	else
		return false;	//alert (errorString);
}


function isTime (sTime, bSeconds){
	var timePat;
	if (bSeconds == true){
	   timePat = /^(\d{1,2})(\:)(\d{2})(\:)(\d{2})$/;                         
	}else{
	   timePat = /^(\d{1,2})(\:)(\d{2})$/;                          
	}
	var matchArray = sTime.match(timePat); // is the format ok?
	if (matchArray == null) {
	   return false;
	}
	var intHour, intMinute, intSecond;
	intHour = matchArray[1]; 
	intMinute = matchArray[3]; 
	if (bSeconds == true) { intSecond = matchArray[5] } else { intSecond = 00 };
	var objTempTime = new Date( 0, 0, 1, intHour, intMinute, intSecond );
	return ( ( objTempTime.getHours()   == intHour   ) &&
			 ( objTempTime.getMinutes() == intMinute ) &&
			 ( objTempTime.getSeconds() == intSecond ) ) ? true : false
}







function isDateITA( sDate ) {
	
	if (sDate=='')  return false;

	var aDate = sDate.split("/");
	if (aDate.length!=3) return false;

	// gg, mm, aaaa sono stringhe tutte di caratteri numerici
	if (!isDigits(aDate[0])) return false;
	if (!isDigits(aDate[1])) return false;
	if (!isDigits(aDate[2])) return false;
	
	var giorno = parseInt(aDate[0],10);
	var mese = parseInt(aDate[1],10);
	var anno = parseInt(aDate[2],10);
	
	if (anno<1900) return false;
	if (anno>2010) return false;
	
	if (!isInRange(giorno,1,31)) return false;
	if (!isInRange(mese,1,12)) return false;
	
	testDate = new Date(anno,mese-1,giorno);
	
	return ( testDate.getMonth()==(mese-1) && 
			 testDate.getDate()==(giorno) )
}


/*
	****************************************************************************************
	****************************************************************************************
	****************************************************************************************
	****************************************************************************************
		funzioni specifiche per i confronti tra date, ore, ecc.
	****************************************************************************************
	****************************************************************************************
	****************************************************************************************
	****************************************************************************************
*/


var sInvalidDate = '01/01/2001';
var nMillisecPerDay = 1000*60*60*24;

var dTemp = new Date();
var dToday = new Date( dTemp.getYear(), dTemp.getMonth(), dTemp.getDate() );
var sTodayDate = dTemp.getDate()+'/'+(dTemp.getMonth()+1)+'/'+dTemp.getYear();


// se la data non è valida (data ITA in stringa) torna sInvalidDate
function str2date( sDate ) {
	var sTmpDate = sDate;
	// controllo formattazione data
	if (!isDateITA(sDate)) sTmpDate = sInvalidDate;
	// conversione e ritorno data
	var aDate = sTmpDate.split("/");
	return new Date( parseInt(aDate[2],10), parseInt(aDate[1],10)-1, parseInt(aDate[0],10) );
}


function areDateInSequence( sDate1, sDate2 ) {
	// controllo validità date
	if (!isDateITA(sDate1)) return false;
	if (!isDateITA(sDate2)) return false;
	return ( str2date(sDate1) <= str2date(sDate2) )
}



// ritorna l'indice dell'oggetto radio passato per argomento 
// che ha una selezione effettuata, -1 altrimenti
// Argomenti: radio INPUT di tipo radio
function ComboSelectedValue(combo) {
	var nIndex = -1;
	for (i=0 ; i< combo.length ; i++) {
		if (combo[i].selected) nIndex = i;
	}
	if (nIndex==-1) {
		return ''; 
	} else {
		return combo[nIndex].value;
	}
}


