// SecurityPlugin128 Java Script
// 1. update 1999.12.22 brson@initech.com
// 2. update 2000.0210 brson@initech.com v2.0.0.0
// 3. update 2000.03.10 
// 4. update 2000.03.27 
//	GatherValue¿¡¼­ radio, checkbox ÀÏ¶§ ¼öÁ¤.
//	Initcache, SetCacheTimeÃß°¡	
// 5. update 2000.04.19
//	algorithmÀ» constant ´ë½Å¿¡ RC4 | BF_OFB | SEED_OFB | EGIS | SEED_CBC
//	INIpluginData¿¡ algorithm(alg) Á¤º¸ Ãß°¡
// 6. update 2000.05.02 dykim@initech.com
//  Plugin Module check routine Ãß°¡. ¸¸¾à ¼³Ä¡µÇÁö ¾Ê¾ÒÀ¸¸é ¼öµ¿¼³Ä¡ ÆäÀÌÁö¸¦ º¸¿©ÁÜ.

var  ManualInstallPageHome = "/initech/plugin";
var  ManualInstallPageNS = ManualInstallPageHome + "/manual_install_ns.html";
var  ManualInstallPageIE = ManualInstallPageHome + "/manual_install_ie.html";
var  ManualInstallTarget = "_new";
var  ManualInstallErrorMsg = "º¸¾ÈÇÃ·¯±×ÀÎ(INIplugin-128)ÀÌ ¼³Ä¡µÇÁö ¾Ê¾Ò½À´Ï´Ù. ´ÙÀ½¿¡ ³ªÅ¸³ª´Â º¸¾ÈÇÃ·¯±×ÀÎ ¼³Ä¡ È­¸é¿¡ µû¶ó¼­ ¼³Ä¡ÇØ ÁÖ½Ê½Ã¿À. È®ÀÎ¹öÆ°À» ´©¸£¼¼¿ä.";

var TimeURL         = 'http://' + window.location.host;
	TimeURL             += '/servlets/Time';

var YessignCAIP = "203.233.91.48";
var YessignCMPPort = 4512;

var  SecureFrame;
var  SharedPlugin = true;

var  BF_OFB 	  = 1;
var  BF_OFB_str   = "BF-OFB";
var  SEED_OFB 	  = 2;
var  SEED_OFB_str = "SEED-OFB";
var  EGIS 	  = 3;
var  EGIS_str 	  = "EGIS";
var  RC4	  = 4;
var  RC4_str 	  = "RC4";
var  SEED_CBC 	  = 5;
var  SEED_CBC_str = "SEED-CBC";

var alg = SEED_CBC;
var algstr = SEED_CBC_str;
//var alg = RC4;
//var algstr = RC4_str;

function MsgBox(szMsg)
{
	//alert(szMsg);
}

function CryptCheck(szMsg)
{
	//alert(szMsg);
}

function FrameCheck()
{
//2002.12.4 ¾È¼®¹Î ¼öÁ¤	if ((typeof top.secureframe) != "undefined")
	if ((typeof parent.secureframe) != "undefined")
		SecureFrame = parent.secureframe;
	else if (((typeof opener)!= "undefined") && ((typeof opener.top)!= "undefined") && ((typeof opener.top.secureframe) != "undefined"))
		SecureFrame = opener.top.secureframe;
	else
		SecureFrame = null; 
}

function ModuleInstallCheck()
{
	FrameCheck();
	
	if(SecureFrame != null){
		if (navigator.appName == 'Netscape') {
			if (SecureFrame.document.INIplugin == null) {
				//alert(ManualInstallErrorMsg);
				//window.open(ManualInstallPageNS, ManualInstallTarger);
				return null;
			} else {
				return SecureFrame.document.INIplugin;
			}
		} else {
			if (SecureFrame.INIplugin.object == null) {
				//alert(ManualInstallErrorMsg);
				//window.open(ManualInstallPageIE, ManualInstallTarget);
				return null;
			} else {
				return SecureFrame.INIplugin;
			}
		}
	} else {
    	if (navigator.appName == 'Netscape') {
			if (document.INIplugin == null) {
				//alert(ManualInstallErrorMsg);
				//window.open(ManualInstallPageNS, ManualInstallTarger);
				return null;
			} else {
				return document.INIplugin;
			}
		} else {
			if (INIplugin.object == null) {
				//alert(ManualInstallErrorMsg);
				//window.open(ManualInstallPageIE, ManualInstallTarger);
				return null;
			} else {
				return INIplugin;
			}
		}
    }
}

function ModuleInstallCheck2()
{
	FrameCheck();
	
	if(SecureFrame != null){
		if (navigator.appName == 'Netscape') {
			if (SecureFrame.document.INIplugin == null) {
				alert(ManualInstallErrorMsg);
				window.open(ManualInstallPageNS, ManualInstallTarger);
				return null;
			} else {
				return SecureFrame.document.INIplugin;
			}
		} else {
			if (SecureFrame.INIplugin.object == null) {
				alert(ManualInstallErrorMsg);
				window.open(ManualInstallPageIE, ManualInstallTarget);
				return null;
			} else {
				return SecureFrame.INIplugin;
			}
		}
	} else {
    	if (navigator.appName == 'Netscape') {
			if (document.INIplugin == null) {
				alert(ManualInstallErrorMsg);
				window.open(ManualInstallPageNS, ManualInstallTarger);
				return null;
			} else {
				return document.INIplugin;
			}
		} else {
			if (INIplugin.object == null) {
				alert(ManualInstallErrorMsg);
				window.open(ManualInstallPageIE, ManualInstallTarger);
				return null;
			} else {
				return INIplugin;
			}
		}
    }
}

function URLencode(url) 
{
	// INIplugin-128 Install Check
	INIpluginObject = ModuleInstallCheck();
	if (INIpluginObject == null) return false;
	
	return INIpluginObject.URLEncode(url);
}

function GatherValue(form, start, bErase)
{
	var strResult = "";
	var name = "";
	var value = "";
	var sel=0;

	// INIplugin-128 Install Check
	INIpluginObject = ModuleInstallCheck();
	if (INIpluginObject == null) return false;
	
	len = form.elements.length;
	for(i=start; i<len; i++) 
	{
		element = form.elements[i];

		if(element.name=="INIpluginData")
			continue;
		if(element.name=="filedata")
			continue;
		if (!((form.elements[i].type != "button") && (form.elements[i].type != "reset") && (form.elements[i].type != "submit"))) 
			continue;

	//	if ((element.type == "button") || (element.type = "reset") || (element.type == "submit")) 
	//		continue;
		if ( ((element.type == "radio") || (element.type == "checkbox")) && (element.checked!=true) ) 
			continue;
		// File Field´Â SKIPÇÑ´Ù.
        if(form.elements[i].name.indexOf('file_', 0) >= 0) {
			continue;
        }

	    if (element.type == "select-one") 
		{
   			sel = element.selectedIndex;
            if (element.options[sel].value != '') 
			{	
            	value = element.options[sel].value;
			} 
			else 
			{
               	value = element.options[sel].text;
			}
			if(bErase) element.selectedIndex = 0;
		} 
		else 
		{
       		value = element.value;
			if(bErase) element.value = "";
       	}
		if (strResult!="") strResult += "&";
		strResult += element.name;
		strResult += "=";
		strResult += INIpluginObject.URLEncode(value);
	}
	return strResult;
}

function GatherFileValue(form, start, bErase)
{
	var strResult = "";
	var name = "";
	var value = "";
	var sel=0;

	// INIplugin-128 Install Check
	INIpluginObject = ModuleInstallCheck();
	if (INIpluginObject == null) return false;
	
	len = form.elements.length;
	for(i=start; i<len; i++) 
	{
		element = form.elements[i];

		if(element.name=="INIpluginData")
			continue;
		if(element.name=="filedata")
			continue;
		if (!((form.elements[i].type != "button") && (form.elements[i].type != "reset") && (form.elements[i].type != "submit"))) 
			continue;

	//	if ((element.type == "button") || (element.type = "reset") || (element.type == "submit")) 
	//		continue;
		if ( ((element.type == "radio") || (element.type == "checkbox")) && (element.checked!=true) ) 
			continue;
		// File Field
        if(form.elements[i].name.indexOf('file_', 0)>=0){
	        if(strResult!=""){
 	        	strResult += "&";
            }
            strResult+= form.elements[i].name;
            strResult += "=";
            strResult += URLencode(form.elements[i].value);
            continue;
       } else {
			continue;
		}
	}
	return strResult;
}

function MakeINIpluginData(vf, encData, Random)
{
	var INIdata = "vf=";
	INIdata += vf;
	var temp;	
	
	// INIplugin-128 Install Check
	INIpluginObject = ModuleInstallCheck();
	if (INIpluginObject == null) return "";

	// vd : Verify Data
	INIdata += "&vd=";
	if(vf=="1"){
		if((temp=INIpluginObject.VData(Random))=="") return "";
		INIdata += INIpluginObject.URLEncode(temp);

	}
	// sk : SessionKey
	INIdata += "&sk=";
	if (SharedPlugin == true) {
		if( (temp = INIpluginObject.GetSessionKeys())=="") return "";
	} else { 
	    if( (temp = INIpluginObject.GetSessionKey())=="") return "";
	}
	
	INIdata += INIpluginObject.URLEncode(temp);	

	// cc : ClientCert
	INIdata += "&cc=";
	if(vf=="1"){
		if((temp = INIpluginObject.GetClientCert())=="") return "";
		if(temp == "CERT_NOT_FOUND") {
			alert("»ç¿ëÇÒ ÀÎÁõ¼­°¡ ¾ø½À´Ï´Ù. \n\nÀÎÁõ¼¾ÅÍ¿¡¼­ ÀÎÁõ¼­¸¦ ¹ß±Þ¹Þ¾Æ »ç¿ëÇÏ½Ã±â ¹Ù¶ø´Ï´Ù.");
			return "";
		}
		INIdata += INIpluginObject.URLEncode(temp);
	}
	
	// sg : Signature
	INIdata += "&sg=";
	if(vf=="1") {
		if(encData!="") {
			if((temp = INIpluginObject.Sign(encData))=="") return "";
			INIdata += INIpluginObject.URLEncode(temp);
		}
	}
	
	// alg : Algorithm
	INIdata += "&alg=";
	if(algstr != "") {
		INIdata += INIpluginObject.URLEncode(algstr);
	}
	
	// dt : Data 
	INIdata += "&dt=";
	if(encData!=""){
		if (SharedPlugin == true) {
			if((temp = INIpluginObject.Encrypts(alg, encData))=="") return "";
		} else {
			if((temp = INIpluginObject.Encrypt(alg, encData))=="") return "";
		}
		INIdata += INIpluginObject.URLEncode(temp);
	}			
//	alert(INIdata);
	return INIdata;
}

function EncForm(form) 
{
	var eletemp = "";
	var filetemp = "";

	// INIplugin-128 Install Check
	INIpluginObject = ModuleInstallCheck();
	if (INIpluginObject == null) return false;

	eletemp = GatherValue(form, 0, true);
	filetemp = GatherFileValue(form, 0, true);
	if ((form.INIpluginData.value = MakeINIpluginData("0", eletemp, ""))=="") return false;
	if (filetemp !=  "") {
		if ((form.filedata.value = INIpluginObject.MakeFileData(alg, filetemp, 0)) == "") return false; 
	}

//	alert(form.INIpluginData.value);

   	return true;
}

function EncForm2(form1, form2) 
{
	var eletemp = "";
	var filetemp = "";
	
	// INIplugin-128 Install Check
	INIpluginObject = ModuleInstallCheck();
	if (INIpluginObject == null) return false;
	
	eletemp = GatherValue(form1, 0, false);
	filetemp = GatherFileValue(form1, 0, false);
	if ((form2.INIpluginData.value = MakeINIpluginData("0", eletemp, ""))=="") return false;
	if (filetemp !=  "") {
		if ((form2.filedata.value = INIpluginObject.MakeFileData(alg, filetemp, 0)) == "") return false; 
	}

   	return true;
}

function EncLink(url, encData, target, style)
{
	var queryString = "INIpluginData=";
	var INIdata;

	// INIplugin-128 Install Check
	INIpluginObject = ModuleInstallCheck();
	if (INIpluginObject == null) return false;
	
	if ((INIdata = MakeINIpluginData("0", encData, ""))=="") return;
	if (navigator.appName == 'Netscape') {
		queryString += URLencode(INIdata);
	} else {
		queryString += URLencode(INIdata);		   	   		
	}	

	if(url.indexOf('?', 0) < 0) url += "?";
	if((url.charAt(url.length-1)!='?') && (url.charAt(url.length-1)!='&')) url += "&";

	url += queryString;
	
	window.open(url, target, style);
}

function Idecrypt(strin)
{
	// INIplugin-128 Install Check
	INIpluginObject = ModuleInstallCheck();
	if (INIpluginObject == null) return "";
	
	if (SharedPlugin == true) {
		return unescape(INIpluginObject.Decrypts(alg, strin));
	} else {
		return unescape(INIpluginObject.Decrypt(alg, strin));
	}
}

function LoadCert(Cert)
{
	// INIplugin-128 Install Check
	INIpluginObject = ModuleInstallCheck();
	if (INIpluginObject == null) return false;
	
	if (SharedPlugin == true) {
		INIpluginObject.LoadCerts(Cert);
	} else {
		INIpluginObject.LoadCert(Cert);
	}	
	return true;
}

function ReSession()
{
	// INIplugin-128 Install Check
	INIpluginObject = ModuleInstallCheck();
	if (INIpluginObject == null) return false;
	
	INIpluginObject.ReSession();
	
	return true;
}

function EncFormVerify(form) 
{
	var eletemp = "";
	var filetemp = "";
	var Random = TimeURL; 

	// INIplugin-128 Install Check
	INIpluginObject = ModuleInstallCheck();
	if (INIpluginObject == null) return false;

	eletemp = GatherValue(form, 0, true);
	filetemp = GatherFileValue(form, 0, true);

	if ((form.INIpluginData.value = MakeINIpluginData("1", eletemp, Random))=="") {
		return false;
	}
	if (filetemp !=  "") {
		if ((form.filedata.value = INIpluginObject.MakeFileData(alg, filetemp, 1)) == "") return false; 
	}

//	alert(form.INIpluginData.value);
    
   	return true;
}

function EncFormVerify2(form1, form2) 
{
	var eletemp = "";
	var filetemp = "";
	var Random = TimeURL;

	// INIplugin-128 Install Check
	INIpluginObject = ModuleInstallCheck();
	if (INIpluginObject == null) return false;
	
	eletemp = GatherValue(form1, 0, false);
	filetemp = GatherFileValue(form1, 0, false);
		
	if((form2.INIpluginData.value = MakeINIpluginData("1", eletemp, Random))=="") return false;
	if (filetemp !=  "") {
		if ((form2.filedata.value = INIpluginObject.MakeFileData(alg, filetemp, 1)) == "") return false; 
	}
    
   	return true;
}

function EncLinkVerify(url, encData, target)
{
	var queryString = "INIpluginData=";
	var INIdata;
	var Random = TimeURL;

	// INIplugin-128 Install Check
	INIpluginObject = ModuleInstallCheck();
	if (INIpluginObject == null) return false;
	
	if((INIdata = MakeINIpluginData("1", encData, Random))=="") return;
	if (navigator.appName == 'Netscape') {
		queryString += URLencode(INIdata);
	} else {
		queryString += URLencode(INIdata);		   	   		
	}	

	if(url.indexOf('?', 0) < 0) url += "?";
	if((url.charAt(url.length-1)!='?') && (url.charAt(url.length-1)!='&')) url += "&";
	
	url += queryString;
	window.open(url, target);
}

function InsertUserCert(cert)
{
	// INIplugin-128 Install Check
	INIpluginObject = ModuleInstallCheck();
	if (INIpluginObject == null) return false;
	INIpluginObject.InsertUserCert(cert);
	
	return true;
}

function ManageCert()
{
	// INIplugin-128 Install Check
	INIpluginObject = ModuleInstallCheck();
	if (INIpluginObject == null) return; // false;
	
	INIpluginObject.ManageCert();
	
	//return true;
}

function CertRequest(form)
{
	var dn="";
	var temp=""
	len = form.elements.length;

	form.req.value="";

	// INIplugin-128 Install Check
	INIpluginObject = ModuleInstallCheck();
	if (INIpluginObject == null) return false;
	
	for (i = 0; i < len; i++) {
		var name = form.elements[i].name.toUpperCase();
		var temp = form.elements[i].value;
		if(name == "C")	dn = dn + "C=" + URLencode(temp) + "&";
		if(name == "L")	dn = dn + "L=" + URLencode(temp) + "&";
		if(name == "O")	dn = dn + "O=" + URLencode(temp) + "&";
		if(name == "OU") dn = dn + "OU=" + URLencode(temp) + "&";
		if(name == "CN") dn = dn + "CN=" + URLencode(temp) + "&";
		if(name == "EMAIL")	dn = dn + "EMAIL=" + URLencode(temp) + "&";
	}
	
	req = INIpluginObject.CertRequest(dn, form.challenge.value); 
	if(req=="") return false;
	form.req.value = req;
	
	return true;		
}

function INIAbout()
{
	// INIplugin-128 Install Check
	INIpluginObject = ModuleInstallCheck();

	if (INIpluginObject == null) return false;
	
	INIpluginObject.About();
	
	return true;
}

function IssueCertificate(szRef, szCode)
{
        INIpluginObject = ModuleInstallCheck();
        if (INIpluginObject == null) return false;

        INIpluginObject.IssueCertificate(szRef, szCode, YessignCAIP, YessignCMPPort);
        return true;
}

function UpdateCertificate()
{
        INIpluginObject = ModuleInstallCheck();
        if (INIpluginObject == null) return false;

        INIpluginObject.UpdateCertificate(YessignCAIP, YessignCMPPort);
        return true;
}

function InsertCACert(cert)
{
	// INIplugin-128 Install Check
	INIpluginObject = ModuleInstallCheck();
	if (INIpluginObject == null) return false;
	
	INIpluginObject.InsertCACert(cert);
	
	return true;
}

function EncCertReq(form1)
{
	// INIplugin-128 Install Check
	INIpluginObject = ModuleInstallCheck();
	if (INIpluginObject == null) return false;
	
	if(CertRequest(form1)) return EncForm(form1);

	return false;
	
}

function EncCertReq2(form1, form2)
{
	// INIplugin-128 Install Check
	INIpluginObject = ModuleInstallCheck();
	if (INIpluginObject == null) return false;
	
	if(CertRequest(form1)) return EncForm2(form1, form2);

	return false;
}

function InitCache()
{
	// INIplugin-128 Install Check
	INIpluginObject = ModuleInstallCheck();
	if (INIpluginObject == null) return false;
	
	INIpluginObject.InitCache();
	
	return true;
}

function SetCacheTime(gap)
{
	// INIplugin-128 Install Check
	INIpluginObject = ModuleInstallCheck();
	if (INIpluginObject == null) return false;
	
	INIpluginObject.SetCacheTime(gap);
	
	return true;
}


function SelectFile()
{
	// INIplugin-128 Install Check
	INIpluginObject = ModuleInstallCheck();
	if (INIpluginObject == null) return false;
	
	INIpluginObject.SelectFile();
	
	return true;
}

function PrintCertReq2(form1, form2)
{
	// INIplugin-128 Install Check
	INIpluginObject = ModuleInstallCheck();
	if (INIpluginObject == null) return false;
	
	if(CertRequest(form1)){
		alert(form1.req.value);
		form2.INIpluginData.value = form1.req.value;
		return true;
	}
	return false;
}

function LoadCACert(CACert)
{
	// INIplugin-128 Install Check
	INIpluginObject = ModuleInstallCheck();
	if (INIpluginObject == null) return false;
	
	INIpluginObject.LoadCACert(CACert);
	
	return true;
}

function DeleteUserCert(DelCert)
{
	INIpluginObject = ModuleInstallCheck();
	if (INIpluginObject == null) return;
	
	if (INIpluginObject.DeleteUserCert(DelCert)==0) {
	//	alert("ÇöÀç »ç¿ëÇÏ½Ã´Â ÄÄÇ»ÅÍ¿¡ ÇØ´ç ÀÎÁõ¼­°¡ ¾ø¾î¼­ »èÁ¦ÇÏÁö ¸øÇÏ¿´½À´Ï´Ù.");
		return;
	}
	
	return;		
}

function SetUserDN(UserDN)
{
	INIpluginObject = ModuleInstallCheck();
	if (INIpluginObject == null) return false;

	if(INIpluginObject.SetSelUserDN(UserDN)==false) {
		alert("»ç¿ëÀÚÀÇ DNÀÌ Àû¿ëµÇÁö ¾Ê¾Ò½À´Ï´Ù.");
		return false;
	} else {
		alert("»ç¿ëÀÚ DNÀÌ Àû¿ëµÇ¾ú½À´Ï´Ù.");  	
	}
	
	return true;
}

function SelFile(field)
{
    // INIplugin-128 Install Check
    INIpluginObject = ModuleInstallCheck();
    if (INIpluginObject == null) return false;

    field.value = INIpluginObject.SelectFile();
}

