//Constants
var DEFAULT_SIGNED_DATA_CONTAINER_ID="signedData";
//Global Variables
var signIssue=false;
var ObjSign_Certificate;
var remoteSignEnabled = false;

// Firma del riepilogo
function DefaultSignClick(summaryContentId)
{
    signIssue=SignDataClick(summaryContentId,DEFAULT_SIGNED_DATA_CONTAINER_ID);
	return signIssue;
}

function SignDataClick(summaryContentId,signedDataContainerId)
{
    var content = "<html><body>" + document.getElementById(summaryContentId).innerHTML + "</body></html>";
    signIssue=SignContentData(content,signedDataContainerId);
	return signIssue;
}

function SignContent(content)
{
    signIssue=SignContentData(content,DEFAULT_SIGNED_DATA_CONTAINER_ID);
	return signIssue;
}

function SignContentData(content,signedDataContainerId)
{
    var objSign=new WebSign();
	var err=null;
	var response=null;
	signIssue=false;
	try
	{
		// Cristina 08/02/2008
		if (FILTER_CERTIFICATES)
		{
			var ret=objSign.SetCertificateFilter(FILTER_NON_REPUDIATION_ENABLED);
			if(ret==RETURN_ERROR_VALUE)
			{
				alert("Si è verificato un errore:\n" + objSign.LastError());
				signIssue=false;
				return false;
			}
		}
		// Cristina 26/11/2009: faccio sempre impostare il filtro perchè altrimenti non si vede la CRS
		var ret=objSign.SetCertificateFilter(FILTER_DIGITAL_SIGNATURE_ENABLED);
		if(ret==RETURN_ERROR_VALUE)
		{
			alert("Si è verificato un errore:\n" + objSign.LastError());
			signIssue=false;
			return false;
		}
		// Fine 26/11/2009
		//tentativo di firma con certificato avente il flag di non ripudio abilitato
		if(objSign.Sign(content, CONTENT_TYPE_STRING, SIGN_MODE_ATTACHED)==RETURN_ERROR_VALUE)
		{
			err=objSign.LastError();
			response=false;
			//verifico se lo store è vuoto: in questo caso chiedo all'utente se vuole procedere con la firma remota
			if(err==ERROR_NONE_CERTIFICATE)
			{
				if(remoteSignEnabled)
				{
					response=confirm((remoteSignDisclaimer=="" ? "Procedere con la firma remota ?" : remoteSignDisclaimer));
					if(remoteSignEnabled&&response)//firma remota abilitata
					{
						SignContent_nextAction();
						signIssue=false;
						return false;
					}
					else
					{
						alert(ERROR_SIGN_OPERATION_ABORTED);
						signIssue=false;
						return false;
					}
				}
				else
				{
					alert(ERROR_UNAVAILABLE_CERTIFICATE_DISABLED_REMOTE_SIGN);
					signIssue=false;
					return false;
				}
			}
			response=false;
			if(weakSignEnabled&&(err==ERROR_NONE_FILTERED_CERTIFICATE||err==ERROR_SIGN_OPERATION_ABORTED)) response=confirm((weakSignDisclaimer=="" ? "Si intende procedere con la firma debole ?" : weakSignDisclaimer));	
			if(weakSignEnabled&&response)//firma debole abilitata
			{
				if(err==ERROR_NONE_FILTERED_CERTIFICATE||err==ERROR_SIGN_OPERATION_ABORTED)
				{
					//firma con certificato avente il flag "digital signature" abilitato e il flag "non ripudio" disabilitato
					var ret=objSign.SetCertificateFilter(FILTER_DIGITAL_SIGNATURE_ENABLED|FILTER_NON_REPUDIATION_DISABLED);
					if(ret==RETURN_ERROR_VALUE)
					{
						alert("Si è verificato un errore:\n" + objSign.LastError());
						signIssue=false;
						return false;
					}
					if(objSign.Sign(content, CONTENT_TYPE_STRING, SIGN_MODE_ATTACHED)==RETURN_ERROR_VALUE)//errore avvenuto durante la firma debole
					{
						err=objSign.LastError();
						response=false;
						if(remoteSignEnabled&&(err==ERROR_NONE_FILTERED_CERTIFICATE||err==ERROR_SIGN_OPERATION_ABORTED)) response=confirm((remoteSignDisclaimer=="" ? "Procedere con la firma remota ?" : remoteSignDisclaimer));
						if(remoteSignEnabled&&response)//firma remota abilitata
						{
							if(err==ERROR_NONE_FILTERED_CERTIFICATE||err==ERROR_SIGN_OPERATION_ABORTED)
							{
								//firma remota
								SignContent_nextAction();
								signIssue=false;
								return false;
								
							}
							else//errore generico in fase di firma debole; lo visualizzo
							{
								alert(err);
								signIssue=false;
								return false;
							}
						}
						else//firma forte fallita, firma debole fallita, firma remota disabilitata; visualizzo l'errore
						{
							alert(err);
							signIssue=false;
							return false;
						}
					}
					else//firma debole avvenuta con successo
					{
						var signedData = document.getElementById(signedDataContainerId);
						signedData.value = objSign.ContentSigned;
						//Cristina 08/02/2008: nella form di autenticazione servono le informazioni del certificato
						ObjSign_Certificate = objSign.Certificate;
						signIssue=true;
						return true;
					}
				}
				else//errore generico in fase di firma forte, lo visualizzo
				{
					alert(err);
					signIssue=false;
					return false;
				}
			}
			else 
			{
				response=false;
				if(remoteSignEnabled&&(err==ERROR_NONE_FILTERED_CERTIFICATE||err==ERROR_SIGN_OPERATION_ABORTED)) response=confirm((remoteSignDisclaimer=="" ? "Procedere con la firma remota ?" : remoteSignDisclaimer));
				if(remoteSignEnabled&&response)//firma debole non abilitata, firma remota abilitata
				{
					if(err==ERROR_NONE_FILTERED_CERTIFICATE||err==ERROR_SIGN_OPERATION_ABORTED)
					{
						//firma remota
						SignContent_nextAction();
						signIssue=false;
						return false;
					}
					else//errore generico, lo visualizzo
					{
						alert(err);
						signIssue=false;
						return false;
					}
				}
				else//firma forte errore, firma debole non abilitata, firma remota non abilitata
				{
					alert(err);
					signIssue=false;
					return false;
				}
			}
		}
		else //firma forte avvenuta con successo
		{
			var signedData = document.getElementById(signedDataContainerId);
			signedData.value = objSign.ContentSigned;
			//Cristina 08/02/2008: nella form di autenticazione servono le informazioni del certificato
			ObjSign_Certificate = objSign.Certificate;
			signIssue=true;
			return true;
		}
	}
	catch (exc)
	{
		alert("Si e' verificato il seguente errore:\n" + exc.description);
		signIssue=false;
		return false;
	}
}

function SignContentOld(content)
{
	var ObjSign = new WebSign();
	if(ObjSign.Sign(content, CONTENT_TYPE_STRING, SIGN_MODE_ATTACHED) == RETURN_ERROR_VALUE)
	{
		alert(ObjSign.LastError());
		//return false;
		// Cerca di eseguire, se definita, la funzione SignContent_nextAction
        try {
			SignContent_nextAction();
			return false;
        } catch(e) {return false;}
	}	
	else
	{
		var signedData = document.getElementById(DEFAULT_SIGNED_DATA_CONTAINER_ID);
		signedData.value = ObjSign.ContentSigned;
		return true;
	}	
}

function EnableSignButton(signBtnId)
{
    var signBtn=document.getElementById(signBtnId);
    if(signBtn!=null)
        signBtn.disabled=false;
}

function EnableRemoteSignDocLink(docLnkId)
{
    var docLnk=document.getElementById(docLnkId);
    docLnk.attributes["class"].value="visible";
    docLnk.setAttribute("target","_blank");
}

