// -----------------------------------------------------------------------------------------------------
// Version	1.0
// Author 	Luca B.
// Data		17/11/2006
// Note		update documentEmailValida check valid char
// -----------------------------------------------------------------------------------------------------

function isPhoneNumber(s){
    var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (!documentIsdigit(c)) return false;
    }
    // All characters are numbers.
    return true;
}

function isCodFiscaleValid(s){
    var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (documentIsdigit(c)==false&&documentStandardChar(c)==false) return false;
    }
    // All characters are numbers or char
    return true;
}

function documentStandardChar(c)
{
	//lettere minuscole
	return  (c.charCodeAt(0)>="a".charCodeAt(0) && c.charCodeAt(0)<="z".charCodeAt(0)) || (c.charCodeAt(0)>="A".charCodeAt(0) && c.charCodeAt(0)<="Z".charCodeAt(0));
}

function isValidFirstNameLastName(s){
    var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (documentIsdigit(c)==true) return false;
        if (isValidCharForFirstNameLastName(c)==false) return false;
    }
    // All characters are numbers.
    return true;
}

function isValidCharForFirstNameLastName( c )
{
	//lettere minuscole
	return ((c.charCodeAt(0)>="a".charCodeAt(0) && c.charCodeAt(0)<="z".charCodeAt(0)) || (c.charCodeAt(0)>="A".charCodeAt(0) && c.charCodeAt(0)<="Z".charCodeAt(0)) || (c.charCodeAt(0)=="`".charCodeAt(0)) || (c.charCodeAt(0)=="'".charCodeAt(0))|| (c==" "));
}

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 documentEmailValida(email)
{
	 var i;
	 var j;
	 //
	 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;
	 // Caratteri validi
	 var isValidChar;
	 var iChar;
	 i = 0;
	 isValidChar = true;
	 while (i<email.length&&isValidChar)
	 {
		  iChar =  email.charAt(i);
		  isValidChar = documentIsdigit(iChar) || documentStandardChar(iChar) || iChar=="_" ||  iChar=="." ||  iChar=="@" || iChar=="-"
		  i++;
	  }
	  return isValidChar;
}

//======================================================================================================
//======================================================================================================

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);
}

function documentCheck_D(pVal,pSize,pFormat,pMandatory,pDefault)	
{
	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;
	}
	//
	return ret;
}

function documentCheck_O(pVal,pSize,pFormat,pMandatory,pDefault)	
{
	var strDate = documentTrim(pVal);
	//
	if (pFormat.length==0)
		pFormat = 'hms';
	if (strDate.length<3 || pFormat.length<3)
		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 || sPos<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++;
		}
	}
	//
	var dt = new Date(1899,11,30,dtn[hPos],dtn[mPos],dtn[sPos]); 
	var buff = "";
	var ret = "";
	//
	for (i=0;i<3;i++)
	{
		if (i==hPos)
			buff = documentLeftPad(dt.getHours().toString(),2,"0")
		else
		if (i==mPos)
			buff = documentLeftPad((dt.getMinutes()).toString(),2,"0")
		else
		if (i==sPos)
			buff = documentLeftPad(dt.getSeconds().toString(),2,"0")
		if (i>0)
			ret += ":";	
		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 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 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 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 =	'';
	} 
}

//======================================================================================================
//======================================================================================================

var documentUploadWin = null;
function documentUpload( id_doc, form_name, input_name, tipo, extensions, mode )
{
	var param = "EXTENSIONS="+extensions+"&";
	param+="TIPO="+tipo;
	param+="&FN="+form_name;
    param+="&IN="+input_name;
    param+="&REF_CLASS="+"DOC";
    param+="&REF_ID="+id_doc;
    param+="&MODE="+mode;
	//
	if (documentUploadWin!=null)
		documentUploadWin.close();
	var path = "uploader.jsp?"+param;
	documentUploadWin = window.open(path,"uploadWin","scrollbars=yes,width=640,height=300");
	documentUploadWin.focus();
}

function documentUpload1( id_doc, form_name, input_name, tipo, extensions, mode )
{
	documentUpload( id_doc, form_name, input_name, tipo, extensions, mode )
}


var documentPreviewImgWin = null;
function documentPreviewImg( form_name, input_name )
{
	var imageName = document.forms[form_name].elements[input_name].value;
	if (imageName.length==0)
	{
		alert("Non è stata selezionata una immagine valida");
		return;
	}
	//
	var param = "URI=" + imageName;
	//
	if (documentPreviewImgWin!=null)
		documentPreviewImgWin.close();
	var path = "imagePreview.jsp?"+param;
	documentPreviewImgWin = window.open(path,"imagePreviewWin","scrollbars=yes,width=640,height=300");
	documentPreviewImgWin.focus();
}

var documentPreviewAWAVWin = null;
function documentPreviewAWAV( uri1, nc )
{
	var param = "URI1=" + uri1;
	var url   = "uploader_view.jsp?"+param+"&nc="+nc;
	document_ac_applet.Sound(url);
	/*
	var param = "URI1=" + uri1;
	//
	if (documentPreviewAWAVWin!=null)
		documentPreviewAWAVWin.close();
	var path = "uploader_view.jsp?"+param;
	documentPreviewAWAVWin = window.open(path,"documentPreviewAWAVWin","scrollbars=yes,width=640,height=300");
	documentPreviewAWAVWin.focus();
	*/
}

//======================================================================================================
//======================================================================================================

function documentOnSendFormSuccess(frm)
{
	return true;	
}

function documenOnSendFormError(frm,frme,descr)
{
	frme.focus();
	//
	alert("Errore nel campo "+descr);
	//
	return false;	
}

//======================================================================================================
//======================================================================================================


function fld_select_value(fld)
{
	return fld.options[fld.selectedIndex].value;
}

function LoadCombo(DEP_SELECT,VALUE)
{
		var trovato = false;
		var keys = '';

		if (DEP_SELECT!=null) {
			for (var i=1;i<=DEP_SELECT.options.length;i++)
			{
				if (DEP_SELECT.options[i]!=null)
				{
					keys = DEP_SELECT.options[i].value;
					if (keys.indexOf(VALUE)==0)
					{
					trovato = true;
					break;
					}
				}
			}
			if (trovato==true)
				DEP_SELECT.selectedIndex = i;
		}
}
