/**
 * @AUTHOR		: PKI team @ ATC, Hyd
 * @VERSION     : 1.0
 * @INFORMATION : Utility for the selection of certificates and 
 *				  signing data (strings and files) 
 * @DATE        : 09-May-2005
 * 
**/

/**
 * Pops up a dialog box showing the certificates found in the security database of the browser.
 */
function selectCert(iformNum)
{
	with(document.forms[iformNum])
	{
	
	    //document.JSignApplet.setLoggingEnabled(true);
	    
	    var st;
	    var data = "SHA2";
        var brow = navigator.appName;
       var browAppName = navigator.appCodeName;
       
       if(brow!=null && brow!='')
       {
        
         if(brow=="Microsoft Internet Explorer")
          {
          st = document.JSignApplet.setStoreType("WINDOWS-MY");
          }
         else if(brow=="Netscape" )
          {
          st = document.JSignApplet.setStoreType("WINDOWS-MY");
          }
          else
          {
          st = document.JSignApplet.setStoreType("WINDOWS-MY");
          }
       }
       var  v = document.JSignApplet.updateData(data);
	   if(!document.JSignApplet.selectSigningCertFromUI()) {
		
		alert("Certificate has not been selected. To complete the registration you need to select a certificate");

		}
		else {
			Cert.value = document.JSignApplet.getSelectedSignerCertDetails(1);
		}
	}
}	

/**
 * Signs the data(data) string with the selected certificate.
 */
function signData(iformNum, strData)
{	
	with(document.forms[iformNum])
	{
	      
	    document.JSignApplet.setHashAlgorithm("SHA-256");
	    if( document.JSignApplet.getSelectedSignerCertDetails(3).length <= 0) {
			return false;
		}
		if(document.JSignApplet.updateData(strData)) {
		
			strSignature.value = document.JSignApplet.sign();
		//	alert("JSignApplet.sign : " +document.JSignApplet.getErrorMessage());
			return true;
		}
	}
}	

/**
 * Signs the file(filePath) with the selected certificate.
 */
function signFile(iformNum, strFileToBeSigned)
{	
	with(document.forms[iformNum])
	{
	     document.JSignApplet.setHashAlgorithm("SHA-256");
		if( document.JSignApplet.getSelectedSignerCertDetails(3).length <= 0) {
			alert("Certificate has not been selected. To complete the registration you need to select a certificate");
			return false;
		}
		if(document.JSignApplet.updateDataFile(strFileToBeSigned.value)) {
		
			document.JSignApplet.setDetached(true);
			signature.value = document.JSignApplet.sign();
		}
	}
}

function selectCertOld(iformNum)
{
	with(document.forms[iformNum])
	{
	
	     var data = "SHA2";
        var brow = navigator.appName;
       
       if(brow!=null && brow!='')
       {
         if(brow=="Microsoft Internet Explorer")
          {
           st = document.JSignApplet.setStoreType("WINDOWS-MY");
           
          }
         else if(brow=="Netscape")
          {
           st = document.JSignApplet.setStoreType("WINDOWS-MY");
          }
          else
          {
          st = document.JSignApplet.setStoreType("WINDOWS-MY");
          }
       }

	
		
		
		var  v = document.JSignApplet.updateData(data);
		
	    
		if(!document.JSignApplet.selectSigningCertFromUI()) {
			alert("Certificate has not been selected. To complete the registration you need to select a certificate");

		}
		else {
			CertOld.value = document.JSignApplet.getSelectedSignerCertDetails(1);
		}
	}
}	

/**
 * Signs the data(data) string with the selected certificate.
 */
function signDataOld(iformNum, strData)
{	
	with(document.forms[iformNum])
	{
	    document.JSignApplet.setHashAlgorithm("SHA-256");
		if( document.JSignApplet.getSelectedSignerCertDetails(3).length <= 0) {
			return false;
		}
		if(document.JSignApplet.updateData(strData)) {
			strSignatureOld.value = document.JSignApplet.sign();
			return true;
		}
	}
}
