var pluginURL = "/secui/SecuiSECIE.cab"; 
var cabVersion = "#Version=1,0,0,3"; 

var encALGO = "DES"; 
var signALGO = "SHA1";
var signcert = null; 
var signValue = null; 

var serverCert = "-----BEGIN CERTIFICATE-----|MIICEDCCAXmgAwIBAgIDAY05MA0GCSqGSIb3DQEBBQUAMDUxEDAOBgNVBAoTB3Nh|bXN1bmcxEDAOBgNVBAsTB1RydXN0Q0ExDzANBgNVBAMTBkNMQVNTMTAeFw0wMzEx|MjcwMTE1NTNaFw0wODExMjYwMTE1NTNaMDYxEDAOBgNVBAoTB3NhbXN1bmcxFDAS|BgNVBAsTC2FwcGxpY2F0aW9uMQwwCgYDVQQDEwNNQ1MwgZ8wDQYJKoZIhvcNAQEB|BQADgY0AMIGJAoGBAJD1JBWuxDl2XRXjd92GhoS2NgYv2AWYr+wBXCfz4jbKMygU|K3oUPESKEQH4wiyABKnyazXGDjT11NH2Xm+GHx4KcIgIJsQsBW7ggB3XotqTsLJJ|we0tWxHc9e9w/En+DqnFivk9LiospDgib+Lb35lfYSbpH3cVJQ7TVLp/12/VAgMB|AAGjLTArMAsGA1UdDwQEAwIDODAJBgNVHRMEAjAAMBEGCWCGSAGG+EIBAQQEAwIH|gDANBgkqhkiG9w0BAQUFAAOBgQAQxvnbi+o+st6H7RsBHyc4wDvMyCHH9B8qvAoi|35omR1kzmtLoGopVorNUeaRvkufZaKQ4LyfnGDV4uUpH6mmDNgvIFQPRGJbOUEW4|hMH9C4oP3taibPU+IBEtqoyJ2j97ZfZS/sM3tlIOlNp5N6+iIQRGhbdS+OrXQl63|FZ5N0A==|-----END CERTIFICATE-----";


document.writeln('<OBJECT ID="SecuiSS" CLASSID="clsid:A93FB56D-2F76-4DD7-8E38-9B1EB38C88A5" width=0 height=0 ');
document.writeln('CODEBASE='+ pluginURL + cabVersion + '>');
document.writeln('</OBJECT>');
	
document.writeln('<OBJECT ID="SecuiTS" CLASSID="clsid:2F932BDC-AE12-4CCA-B58A-2C850955579E" width=0 height=0 ');
document.writeln('CODEBASE='+ pluginURL + cabVersion + '>');
document.writeln('</OBJECT>');


document.writeln('<form name=secuiform METHOD=POST>');
document.writeln( '<input name=ENCSYMM type=hidden>'); 
document.writeln( '<input name=SIGNCERT type=hidden>'); 
document.writeln( '<input name=ENCDATA type=hidden>'); 
document.writeln( '<input name=ENCALGO type=hidden>'); 
document.writeln( '<input name=SIGNALGO type=hidden>'); 
document.writeln( '<input name=SIGNVALUE type=hidden>'); 
document.writeln('</form>');


function makeSerialForm(objForm)
{
	var strSerialForm = ""
 	var startFlag = false 
 	
	for(i = 0; i < objForm.elements.length; i++) 
	{
		
		if((objForm.elements[i].type != "submit") && (objForm.elements[i].type != "reset") && (objForm.elements[i].type != "button"))
		{
			if(objForm.elements[i].type == "radio" || objForm.elements[i].type == "checkbox")
		        {
				if (objForm.elements[i].checked == true && objForm.elements[i].disabled != true )
				{
					if(startFlag)
					{
						strSerialForm += "&";
					}
					else
						startFlag = true;
						
					strSerialForm += objForm.elements[i].name; 
					strSerialForm += "=";
					if (objForm.elements[i].value.length > 0)
					{
						strSerialForm += SecuiSS.Base64Encode(objForm.elements[i].value);
					}
					
				}
			}
			else
			{
				if (startFlag )
				{
					strSerialForm += "&";
				}
				else
					startFlag = true;
						
				strSerialForm += objForm.elements[i].name; 
				strSerialForm += "=";
				
				if (objForm.elements[i].type == "select-one") 
				{
					if (objForm.elements[i].options[objForm.elements[i].selectedIndex].value != '')
						strSerialForm += SecuiSS.Base64Encode(objForm.elements[i].options[objForm.elements[i].selectedIndex].value);
						
					else
						strSerialForm += SecuiSS.Base64Encode(objForm.elements[i].options[objForm.elements[i].selectedIndex].text);
						
						
				}
				else 
				{
					
					//strSerialForm += SecuiSS.Base64Encode(objForm.elements[i].value);
					if (objForm.elements[i].value.length > 0)
					{
						strSerialForm += SecuiSS.Base64Encode(objForm.elements[i].value);
					}
				}
			}
		}
	}
	
	return strSerialForm;
}
 

function SecuiEncryptEx(plainData)
{
	SecuiSS.GenerateEx(encALGO, serverCert);
	
	return SecuiSS.EncipherEx(plainData);
}

function SecuiEncrypt(plainData)
{
	return SecuiSS.EncipherEx(plainData);
}


function SecuiDecrypt(EncData)
{	
	var result;
	
	result = SecuiSS.DecipherEx(EncData);
	
	if (result.length <= 0 ) 
	{
	}
	else
	{
		return result	;
	}
}

function SecuiSign(oriMesg)
{
	var signVal = SecuiTS.Sign(signALGO, oriMesg, "", "");
	signcert = SecuiTS.getCert();
	
	return signVal;
}

function SecuiSubmitEx(form, svrAction, svrTarget)
{
	var plainText;
	var encText;
	var encSymmKey;


	plainText = makeSerialForm(form);
	encText = SecuiEncryptEx(plainText);
	encSymmKey = SecuiSS.getKEYIV();

	document.secuiform.ENCDATA.value = encText;
	document.secuiform.ENCSYMM.value = encSymmKey;
	document.secuiform.ENCALGO.value = encALGO;
    if(svrTarget != null ) document.secuiform.target = svrTarget;
    document.secuiform.action = svrAction;
	document.secuiform.submit();
}

function SecuiSubmit(form, svrAction)
{
	var plainText;
	var encText;
	var encSymmKey;

	plainText = makeSerialForm(form);
	encText = SecuiEncrypt(plainText);
	encSymmKey = SecuiSS.getKEYIV();

	document.secuiform.ENCDATA.value = encText;
	document.secuiform.ENCSYMM.value = encSymmKey;
	document.secuiform.ENCALGO.value = encALGO;
        document.secuiform.action = svrAction;
	document.secuiform.submit();
}

function SecuiSignSubmitEx(form, svrAction)
{
	var plainText;  
	var encText;    
	var encSymmKey; 
	var signValue;  

	plainText = makeSerialForm(form);
	signValue = SecuiSign(plainText);
    
	if (signValue.length > 0)
	{
		plainText +=  "&signvalue=" + signValue;
	}
	else
	{
		return;
	}

	encText = SecuiEncryptEx(plainText);
	encSymmKey = SecuiSS.getKEYIV();

	document.secuiform.ENCDATA.value = encText;
	document.secuiform.ENCSYMM.value = encSymmKey;
	document.secuiform.SIGNCERT.value = signcert;
	document.secuiform.ENCALGO.value = encALGO;
 	document.secuiform.SIGNALGO.value = signALGO;
        document.secuiform.action = svrAction;
	document.secuiform.submit();
}

function SecuiSignSubmit(form, svrAction)
{
	var plainText;  
	var encText;    
	var encSymmKey; 
	var signValue;  

	plainText = makeSerialForm(form);
	signValue = SecuiSign(plainText);

	if (signValue.length > 0)
	{
		plainText +=  "&signvalue=" + signValue;
	}
	else
	{
		return;
	}

	encText = SecuiEncrypt(plainText);
	encSymmKey = SecuiSS.getKEYIV();

	document.secuiform.ENCDATA.value = encText;
	document.secuiform.ENCSYMM.value = encSymmKey;
	document.secuiform.SIGNCERT.value = signcert;
	document.secuiform.ENCALGO.value = encALGO;
	document.secuiform.SIGNALGO.value = signALGO;
        document.secuiform.action = svrAction;
	document.secuiform.submit();
}

function SecuiSubmitTarget(form, targetname)
{
	var plainText;
	var encText;
	var encSymmKey;

	plainText = makeSerialForm(form);
	encText = SecuiEncrypt(plainText);
	encSymmKey = SecuiSS.getKEYIV();

	document.secuiform.ENCDATA.value = encText;
	document.secuiform.ENCSYMM.value = encSymmKey;
	document.secuiform.ENCALGO.value = encALGO;
    document.secuiform.action = form.action;
    document.secuiform.target = targetname;
	document.secuiform.submit();
}

function SecuiFileSubmit(form, srvAction)
{
	var filenum;

	for(i=0;i<form.elements.length;i++)
	{
		if(form.elements[i].type == "file")
		{
			filenum = i ;
		}
	}
	
	ret = SecuiSS.EncipherFile(form.elements[filenum].value,serverCert);
	if(ret != 0)
	{
		alert(SecuiSS.GetErrorString());
		return;
	}
	
	form.action = srvAction;
	form.submit();
}

function change()
{
	SecuiSS.RecoverFile();
}
