//======================================================================================================
    
function documentSubst(str, str1, str2)
{
	while (str.indexOf(str1)>-1)
		str = str.replace(str1,str2)
	return str;
}

function documentTrim(str)
{
	var ret = ''+str;
	while (ret.length>0)
	{
		if (ret.charAt(0)==' ' || ret.charAt(0)=='\t')
			ret = ret.substr(1);
		else
		if (ret.charAt(ret.length-1)==' ' || ret.charAt(ret.length-1)=='\t')
			ret = ret.substr(0,ret.length-1);
		else
			return ret;
	}
	return ret;
}

function documentLeftPad( str, size, padchar )
{
	while (str.length<size)
		str = padchar + str;
	return str;
}

function documentIsdigit( c )
{
	return c.charCodeAt(0)>="0".charCodeAt(0) && c.charCodeAt(0)<="9".charCodeAt(0);
}

function documentAtoi( str )
{
	var ret = "";
	var i = 0;
	while (i<str.length && documentIsdigit(str.charAt(i)))
	{
		if (ret.length>0 || str.charAt(i)!='0')
			ret += str.charAt(i);
		i++;
	}
	if (ret.length==0)
		return 0;
	return parseInt(ret);
}


function CheckEstension(file,estensions) {

  var aExt
  var sExt
  var sLength

  if (file == '')  return true;
  file = file.substr(file.length-3,3);
  file = file.toUpperCase();
  file = documentTrim(file);
  aExt = estensions.split(";");
  sLength = aExt.length - 1;
  for (i=0;i<=sLength;i++) {
      if (aExt[i] != '' ) {
      sExt = aExt[i].substr(aExt[i].length-3,3);
      sExt = sExt.toUpperCase();
      if (file == sExt)
	     return true;
	  }
  }
  return false;
}





function documentEmailValida(email)
{
	 var i;
	 var jM
	 //
	 email =  documentTrim(email);
	 //
	 if (email == "")
		return false;

	 // Contiene spazi -> false
	 if (email.indexOf(" ") != -1)
		return false;
	 //
	 i = email.indexOf("@")
	 // Non contiene @ -> false
	 if (i == -1)
	  return false;
	 // @ ? il 1 car -> false
	 if (i == 0)
		return false;
	 // Non contiene . dopo @ -> false
	 j = email.indexOf(".",i+1);
	 if (j == -1)
		return false;
	 // . dopo @ senza carattere -> false
	 if (j == i +1)
		return false;
	 // . ultimo carattere -> false
	 if (j == email.length-1)
		 return false;
	 // Contiene @ dopo @ -> false
	 j = email.indexOf("@",i+1);
	 if (j != -1)
		return false;
	 //
	 return true;
}

function documentTelefonoValido( tel )
{
	// alert(tel);
	tel =  documentTrim(tel);
	// int i
	var num = 0;
	var spaces = 0;
	// il primo char pu? essere un +
	var i;
	for (i=0;i<tel.length;i++)
	{
		if (tel.charAt(i)=='+')
		{
			if (i!=0)
				return false;
		}
		else
		if (documentIsdigit(tel.charAt(i)))
		{
			num++;
		}
		else
		if (tel.charAt(i)==' ')
		{
			spaces++;
		}
		else
		{
			return false;
		}
	}
	//
	return num>=3;
}

function documentCheckNickname( e )
{
	var i;
	var okMin = "qwertyuiopasdfghjklzxcvbnm1234567890'";
	var okMaiu = okMin.toUpperCase();
	
	e =  documentTrim(e);
	if (e.length<2 || e.length>16)
		return false;
	
	// controllo primo carattere
	var okMin1 = "qwertyuiopasdfghjklzxcvbnm1234567890";
	var okMaiu1 = okMin1.toUpperCase();
	if( (okMin1.indexOf(e.charAt(0))<0) && (okMaiu1.indexOf(e.charAt(0))<0) )
		return false;
	
	// caratteri dal secondo in poi
	for(i=1; i < e.length ;i++)
	{
		if( (okMin.indexOf(e.charAt(i))<0) && (okMaiu.indexOf(e.charAt(i))<0) )
			return false;
	}
	return true;
}

function documentCheckPassword( e )
{
	var i;
	var okMin = "qwertyuiopasdfghjklzxcvbnm1234567890\u00E0\u00E1\u00E8\u00E9\u00EC\u00ED\u00F2\u00F3\u00F9\u00FA\'";
	var okMaiu = okMin.toUpperCase();
	e =  documentTrim(e);
	//
	if (e.length<8 || e.length>16)
		return false;
	//
	for(i=0; i < e.length ;i++)
	{
		if( (okMin.indexOf(e.charAt(i))<0) && (okMaiu.indexOf(e.charAt(i))<0) )
			return false;
	}
	
	// deve contenere almeno un numero
  	var numero = 0;
  	var caratt = 0;
  	for (var x=0;x<e.length;x++)
  	{
    	var c = e.charCodeAt(x)
    	if ((c>=48) && (c<=57))
    	{
      		numero++;
    	}
    	else
    	{
    		caratt++;
    	}
  	}
  	// almeno un numero
  	if(numero<1)
  		return false;
  		
  	// almeno un carattere
  	if(caratt<1)
  		return false;
  	//
  	return true;
}

//======================================================================================================
//======================================================================================================

function documentCheck_I(pVal,pSize,pFormat,pMandatory,pDefault)
{
	var v = documentTrim(pVal);
	if (v != pDefault )
	{
		if (v.indexOf(".")>-1 || v.indexOf(",")>-1 || isNaN(v))
			v = pDefault;
	}
	return(v);
}

function documentCheck_UI(pVal,pSize,pFormat,pMandatory,pDefault)
{
	var v = documentTrim(pVal);
	if (v != pDefault )
	{
		if (v.indexOf(".")>-1 || v.indexOf(",")>-1 || isNaN(v))
			v = pDefault;
		else
		if (parseInt(v)<0)
			v = pDefault;
	}
	return(v);
}

function documentCheck_F(pVal,pSize,pFormat,pMandatory,pDefault)
{
	var v = documentTrim(pVal);
	v = documentSubst(v,",",".");
	if (v != pDefault )
	{
		if (isNaN(v))
			v = pDefault;
	}
	return(v);
}

function documentCheck_UF(pVal,pSize,pFormat,pMandatory,pDefault)
{
	var v = documentTrim(pVal);
	v = documentSubst(v,",",".");
	if (v != pDefault )
	{
		if (isNaN(v))
			v = pDefault;
		else
		if (parseFloat(v)<0)
			v = pDefault;
	}
	return(v);
}

var documentLastDate = null;
function documentCheck_D(pVal,pSize,pFormat,pMandatory,pDefault)
{
	documentLastDate = null;
	var strDate = documentTrim(pVal);
	//
	if (pFormat.length==0)
		pFormat = 'gma';
	if (strDate.length<6 || pFormat.length<3)
		return "";
	//
	var i;
	var dtn = new Array(3);
	var gPos=-1;
	var mPos=-1;
	var aPos=-1;
	//
	for (i=0;i<3;i++)
	{
		switch (pFormat.charAt(i))
		{
		case 'g':	gPos = i; break;
		case 'm':	mPos = i; break;
		case 'a':	aPos = i; break;
		}
	}
	if (gPos<0 || mPos<0 || aPos<0)
		return "";
	//
	var pc = 0;
	for (i=0;i<3;i++)
	{
		// skippo non digit
		while (pc<strDate.length && !documentIsdigit(strDate.charAt(pc)))
			pc++;
		//
		dtn[i] = documentAtoi(strDate.substr(pc));
		// skippo digit
		if (pc<strDate.length)
		{
			pc++;
			while (pc<strDate.length && documentIsdigit(strDate.charAt(pc)))
				pc++;
		}
	}
	// month-1 (0 based)
	dtn[mPos]--;
	// a questo punto verifico
	if (dtn[aPos]<1000)
		dtn[aPos]+=2000;
	//
	var dt = new Date(dtn[aPos],dtn[mPos],dtn[gPos]);
	var buff = "";
	var ret = "";
	//
	for (i=0;i<3;i++)
	{
		if (i==aPos)
			buff = documentLeftPad(dt.getFullYear().toString(),4,"0")
		else
		if (i==mPos)
			buff = documentLeftPad((dt.getMonth()+1).toString(),2,"0")
		else
		if (i==gPos)
			buff = documentLeftPad(dt.getDate().toString(),2,"0")
		if (i>0)
			ret += "/";
		ret += buff;
	}
	//
	documentLastDate = dt;
	//
	return ret;
}

function documentCheck_O(pVal,pSize,pFormat,pMandatory,pDefault)
{
	var strDate = documentTrim(pVal);
	//
	if (pFormat.length==0)
		pFormat = 'hms';
	if (strDate.length<2 || pFormat.length<2)
		return "";
	//
	var i;
	var dtn = new Array(3);
	var hPos=-1;
	var mPos=-1;
	var sPos=-1;
	//
	for (i=0;i<3;i++)
	{
		switch (pFormat.charAt(i))
		{
		case 'h':	hPos = i; break;
		case 'm':	mPos = i; break;
		case 's':	sPos = i; break;
		}
	}
	if (hPos<0 || mPos<0 )
		return "";
	//
	var pc = 0;
	for (i=0;i<3;i++)
	{
		// skippo non digit
		while (pc<strDate.length && !documentIsdigit(strDate.charAt(pc)))
			pc++;
		//
		dtn[i] = documentAtoi(strDate.substr(pc));
		// skippo digit
		if (pc<strDate.length)
		{
			pc++;
			while (pc<strDate.length && documentIsdigit(strDate.charAt(pc)))
				pc++;
		}
	}
	//
	// alert(sPos);
	//
	var dt = new Date(1899,11,30,dtn[hPos],dtn[mPos],sPos>-1 ? dtn[sPos] : 0);
	var buff = "";
	var ret = "";
	//
	for (i=0;i<3;i++)
	{
		buff = "";
		if (i==hPos)
		{
			if (i>0) ret += ":";
			buff = documentLeftPad(dt.getHours().toString(),2,"0");
		}
		else
		if (i==mPos)
		{
			if (i>0) ret += ":";
			buff = documentLeftPad((dt.getMinutes()).toString(),2,"0");
		}
		else
		if (i==sPos)
		{
			if (i>0) ret += ":";
			buff = documentLeftPad(dt.getSeconds().toString(),2,"0");
		}
		ret += buff;
	}
	//
	return ret;
}

function documentCheck_C(pVal,pSize,pFormat,pMandatory,pDefault)
{
	var v = documentTrim(pVal);
	//
	if (pSize>0)
	{
		if (v.length>pSize)
			v = v.substr(0,pSize);
	}
	//
	return v;
}

function documentCheck_T(pVal,pSize,pFormat,pMandatory,pDefault)
{
	var v = documentTrim(pVal);
	//
	if (pSize>0)
	{
		if (v.length>pSize)
			v = v.substr(0,pSize);
	}
	//
	return v;
}

function documentCheck_B(pVal,pSize,pFormat,pMandatory,pDefault)
{
	return documentTrim(pVal);
}

function documentCheck_IMG(pVal,pSize,pFormat,pMandatory,pDefault)
{
	return documentTrim(pVal);
}

function documentCheck_FILE(pVal,pSize,pFormat,pMandatory,pDefault)
{
	return documentTrim(pVal);
}

function documentCheck_R(pVal,pSize,pFormat,pMandatory,pDefault)
{
	return(pVal);
}

function documentCheck_RDOC(pVal,pSize,pFormat,pMandatory,pDefault)
{
	return documentTrim(pVal);
}

function documentCheck_E(pVal,pSize,pFormat,pMandatory,pDefault)
{
	var v = documentTrim(pVal);
	//
	if (pSize>0)
	{
		if (v.length>pSize)
			v = v.substr(0,pSize);
	}
	//
	if (!documentEmailValida(v))
		v = pDefault;
	//
	return v;
}

function documentCheck_TEL(pVal,pSize,pFormat,pMandatory,pDefault)
{
	var v = documentTrim(pVal);
	//
	if (pSize>0)
	{
		if (v.length>pSize)
			v = v.substr(0,pSize);
	}
	//
	if (!documentTelefonoValido(v))
		v = pDefault;
	//
	return v;
}

function documentCheck_USR(pVal,pSize,pFormat,pMandatory,pDefault)
{
	var v = documentTrim(pVal);
	//
	if (pSize>0)
	{
		if (v.length>pSize)
			v = v.substr(0,pSize);
	}
	//
	if (!documentCheckNickname(v))
		v = pDefault;
	//
	return v;
}

function documentCheck_PWD(pVal,pSize,pFormat,pMandatory,pDefault)
{
	var v = documentTrim(pVal);
	//
	if (pSize>0)
	{
		if (v.length>pSize)
			v = v.substr(0,pSize);
	}
	//
	if (!documentCheckPassword(v))
		v = pDefault;
	//
	return v;
}
//======================================================================================================
//======================================================================================================

function frm_documentCheck_I(pVal,pSize,pFormat,pMandatory,pDefault)
{
	pVal.value = documentCheck_I(pVal.value,pSize,pFormat,pMandatory,pDefault)
	return(pVal.value);
}

function frm_documentCheck_UI(pVal,pSize,pFormat,pMandatory,pDefault)
{
	pVal.value = documentCheck_UI(pVal.value,pSize,pFormat,pMandatory,pDefault)
	return(pVal.value);
}

function frm_documentCheck_F(pVal,pSize,pFormat,pMandatory,pDefault)
{
	pVal.value = documentCheck_F(pVal.value,pSize,pFormat,pMandatory,pDefault)
	return(pVal.value);
}

function frm_documentCheck_UF(pVal,pSize,pFormat,pMandatory,pDefault)
{
	pVal.value = documentCheck_UF(pVal.value,pSize,pFormat,pMandatory,pDefault)
	return(pVal.value);
}

function frm_documentCheck_D(pVal,pSize,pFormat,pMandatory,pDefault)
{
	pVal.value = documentCheck_D(pVal.value,pSize,pFormat,pMandatory,pDefault)
	return(pVal.value);
}

function frm_documentCheck_O(pVal,pSize,pFormat,pMandatory,pDefault)
{
	pVal.value = documentCheck_O(pVal.value,pSize,pFormat,pMandatory,pDefault)
	return(pVal.value);
}

function frm_documentCheck_C(pVal,pSize,pFormat,pMandatory,pDefault)
{
	pVal.value = documentCheck_C(pVal.value,pSize,pFormat,pMandatory,pDefault)
	return(pVal.value);
}

function frm_documentCheck_T(pVal,pSize,pFormat,pMandatory,pDefault)
{
	pVal.value = documentCheck_T(pVal.value,pSize,pFormat,pMandatory,pDefault)
	return(pVal.value);
}

function frm_documentCheck_B(pVal,pSize,pFormat,pMandatory,pDefault)
{
	return(pVal.value);
}

function frm_documentCheck_IMG(pVal,pSize,pFormat,pMandatory,pDefault)
{
	pVal.value = documentCheck_IMG(pVal.value,pSize,pFormat,pMandatory,pDefault)
	return(pVal.value);
}

function frm_documentCheck_FILE(pVal,pSize,pFormat,pMandatory,pDefault)
{
	pVal.value = documentCheck_FILE(pVal.value,pSize,pFormat,pMandatory,pDefault)
	return(pVal.value);
}

function frm_documentCheck_R(pVal,pSize,pFormat,pMandatory,pDefault)
{
	return(pVal.value);
}

function frm_documentCheck_RDOC(pVal,pSize,pFormat,pMandatory,pDefault)
{
	pVal.value = documentCheck_RDOC(pVal.value,pSize,pFormat,pMandatory,pDefault)
	return(pVal.value);
}

function frm_documentCheck_E(pVal,pSize,pFormat,pMandatory,pDefault)
{
	pVal.value = documentCheck_E(pVal.value,pSize,pFormat,pMandatory,pDefault)
	return(pVal.value);
}

function frm_documentCheck_TEL(pVal,pSize,pFormat,pMandatory,pDefault)
{
	pVal.value = documentCheck_TEL(pVal.value,pSize,pFormat,pMandatory,pDefault)
	return(pVal.value);
}


function frm_documentCheck_USR(pVal,pSize,pFormat,pMandatory,pDefault)
{
	pVal.value = documentCheck_USR(pVal.value,pSize,pFormat,pMandatory,pDefault)
	return(pVal.value);
}

function frm_documentCheck_PWD(pVal,pSize,pFormat,pMandatory,pDefault)
{
	pVal.value = documentCheck_PWD(pVal.value,pSize,pFormat,pMandatory,pDefault)
	return(pVal.value);
}

//======================================================================================================
//======================================================================================================

function chk_documentCheck_I(pVal,pSize,pFormat,pMandatory,pDefault)
{
	v = frm_documentCheck_I(pVal,pSize,pFormat,pMandatory,pDefault)
	return (!pMandatory || (pMandatory && v.length>0));
}

function chk_documentCheck_UI(pVal,pSize,pFormat,pMandatory,pDefault)
{
	v = frm_documentCheck_UI(pVal,pSize,pFormat,pMandatory,pDefault)
	return (!pMandatory || (pMandatory && v.length>0));
}

function chk_documentCheck_F(pVal,pSize,pFormat,pMandatory,pDefault)
{
	v = frm_documentCheck_F(pVal,pSize,pFormat,pMandatory,pDefault)
	return (!pMandatory || (pMandatory && v.length>0));
}

function chk_documentCheck_UF(pVal,pSize,pFormat,pMandatory,pDefault)
{
	v = frm_documentCheck_UF(pVal,pSize,pFormat,pMandatory,pDefault)
	return (!pMandatory || (pMandatory && v.length>0));
}

function chk_documentCheck_D(pVal,pSize,pFormat,pMandatory,pDefault)
{
	v = frm_documentCheck_D(pVal,pSize,pFormat,pMandatory,pDefault)
	return (!pMandatory || (pMandatory && v.length>0));
}

function chk_documentCheck_O(pVal,pSize,pFormat,pMandatory,pDefault)
{
	v = frm_documentCheck_O(pVal,pSize,pFormat,pMandatory,pDefault)
	return (!pMandatory || (pMandatory && v.length>0));
}

function chk_documentCheck_C(pVal,pSize,pFormat,pMandatory,pDefault)
{
	v = frm_documentCheck_C(pVal,pSize,pFormat,pMandatory,pDefault)
	return (!pMandatory || (pMandatory && v.length>0));
}

function chk_documentCheck_T(pVal,pSize,pFormat,pMandatory,pDefault)
{
	v = frm_documentCheck_T(pVal,pSize,pFormat,pMandatory,pDefault)
	return (!pMandatory || (pMandatory && v.length>0));
}

function chk_documentCheck_B(pVal,pSize,pFormat,pMandatory,pDefault)
{
	return(true);
}

function chk_documentCheck_IMG(pVal,pSize,pFormat,pMandatory,pDefault)
{
	v = frm_documentCheck_IMG(pVal,pSize,pFormat,pMandatory,pDefault)
	return (!pMandatory || (pMandatory && v.length>0));
}

function chk_documentCheck_FILE(pVal,pSize,pFormat,pMandatory,pDefault)
{
	v = frm_documentCheck_FILE(pVal,pSize,pFormat,pMandatory,pDefault)
	return (!pMandatory || (pMandatory && v.length>0));
}

function chk_documentCheck_R(pVal,pSize,pFormat,pMandatory,pDefault)
{
	return(true);
}

function chk_documentCheck_RDOC(pVal,pSize,pFormat,pMandatory,pDefault)
{
	v = frm_documentCheck_RDOC(pVal,pSize,pFormat,pMandatory,pDefault)
	return (!pMandatory || (pMandatory && v.length>0));
}

function chk_documentCheck_E(pVal,pSize,pFormat,pMandatory,pDefault)
{
	v = frm_documentCheck_E(pVal,pSize,pFormat,pMandatory,pDefault)
	return (!pMandatory || (pMandatory && v.length>0));
}

function chk_documentCheck_TEL(pVal,pSize,pFormat,pMandatory,pDefault)
{
	v = frm_documentCheck_TEL(pVal,pSize,pFormat,pMandatory,pDefault)
	return (!pMandatory || (pMandatory && v.length>0));
}

function chk_documentCheck_USR(pVal,pSize,pFormat,pMandatory,pDefault)
{
	v = frm_documentCheck_USR(pVal,pSize,pFormat,pMandatory,pDefault)
	return (!pMandatory || (pMandatory && v.length>0));
}
function chk_documentCheck_PWD(pVal,pSize,pFormat,pMandatory,pDefault)
{
	v = frm_documentCheck_PWD(pVal,pSize,pFormat,pMandatory,pDefault)
	return (!pMandatory || (pMandatory && v.length>0));
}

//======================================================================================================
//======================================================================================================

function documentTodayDate(formato)
{
	var dToday
	var sDate
	var sDay
	var sMonth
	var sYear
	//
	dToday = new Date();
	sDay = documentLeftPad(dToday.getDate(),2,'0');
	sMonth = documentLeftPad((dToday.getMonth()+1).toString(),2,'0')
	sYear = dToday.getFullYear();
	if (formato=="gma")
		sDate = sDay + '/' + sMonth + '/' + sYear
	else
		sDate = sMonth + '/' + sDay + '/' + sYear
	//
	return sDate;
}

function frm_documentTodayDate(frm,ichk,itxt,formato)
{
	if (frm[ichk].checked == true)
	{
		frm[itxt].value = documentTodayDate(formato);
	}
	else
	{
		frm[itxt].value =	'';
	}
}

//======================================================================================================
//======================================================================================================

//======================================================================================================
//======================================================================================================

//
function documentCheckAndSend(frm,subm)
{
	if (documentCheckErrors(frm))
	{
		if (!subm)
			return true;
		else
			frm.submit();
	}
	else
	{
		if (!subm)
			return false;
	}
}

function documentOnSendFormSuccess(frm)
{
	return true;
}

function documenOnSendFormError(frme,descr)
{
	frme.focus();
	//
	alert("Errore.\n"+descr);
	//
	return false;
}


function documenOnSendFormError2(descr)
{
	alert("Errore.\n"+descr);
	//
	return false;
}

//======================================================================================================
// NEW
//======================================================================================================

function documentSearchGetForm( frmName )
{
	return document.getElementById(frmName);
}

function documentGetAge( dataDiNascita )
{
	var bday	=	dataDiNascita.getDate();
	var bmo		=	dataDiNascita.getMonth();
	var byr		=	dataDiNascita.getFullYear();
	//
	var age;
	var now = new Date();
	//
	var tday	=	now.getDate();
	var tmo		=	now.getMonth();
	var tyr		=	now.getFullYear();
	//
	if((tmo > bmo)||(tmo==bmo & tday>=bday))
		age=byr
	else
		age=byr+1;
	//
	return tyr-age;
}

