//                   : 2002.09.12  - Insert fucntion GetUserCertCount

// Copyright(c) 2000-2002 BankTown
// http://www.banktown.com 
// ibtClientLib.js -  »óÈ£¿¬µ¿ ¾÷±×·¹ÀÌµå
// The following interfaces are included in this library.
//
// iWallet_CheckValid()			-
// iWallet_StartSession()		StartSession()
// iWallet_BuildCipher()		-
// iWallet_Envelop()			Envelop()
// iWallet_Encrypt()			Encrypt()
// iWallet_EncryptSubmit()		EncryptSubmit()
// iWallet_Decrypt()			Decrypt()

var IE4 = (document.all) ? true : false;
var NS4 = (document.layers) ? true : false;
var VER4 = (IE4 || NS4);
var iWalletFrame = null;
var iWalletObject = null;


if(top.iWallet != null)
	iWalletFrame = (IE4) ? top.iWallet : top.iWallet.document;
else if( window.opener && !window.opener.closed )
	iWalletFrame = (IE4) ? opener.top.iWallet : opener.iWallet.document;

function iWallet_CheckValid() {
	if ( iWalletFrame )
		iWalletObject = iWalletFrame.iWalletCrypto;
	if ( !iWalletObject ) {
		window.alert("ERROR: iWallet ÇÃ·¯±×ÀÎ ¸ðµâÀÌ ½ÇÇàµÇ°í ÀÖÁö ¾Ê½À´Ï´Ù.");
		return false;
	} return true;
}

function iWallet_StartSession(form) {
	if ( ! iWallet_CheckValid() ) return false;

	form.elements['cipher'].value = iWalletObject.StartSession(serversigneddata);
	if ( form.elements['cipher'].value == 'ERROR!!' ) return false;
	else return true;
}

function iWallet_BuildCipher(form) {
	var plaintext = '';
	for (i=0; i<form.elements.length; i++) {
		var e = form.elements[i];
		if ( e.type == "radio"){
			if(e.checked == true)	
				plaintext += (e.name + '=' + e.value + '&');
		}
		else if ( e.type == "select-one" && e.selectedIndex >= 0 ){
			plaintext += e.name + "=";
			if ( e.options[e.selectedIndex].value != '' )
				plaintext += e.options[e.selectedIndex].value;
			else	plaintext += e.options[e.selectedIndex].text;
			plaintext += "&";
		}
		else if ( e.name != 'cipher' && e.type != 'submit' )
			plaintext += (e.name + '=' + e.value + '&');
		if (e.name != 'cipher' && e.type != 'submit' && e.type != 'button')
			e.value = '';
	}
	return plaintext;
}


function iWallet_Envelop(form) {
	if ( ! iWallet_CheckValid() ) return false;
	form.elements['cipher'].value = iWalletObject.Envelop(casigneddata, iWallet_BuildCipher(form));
	return true;
}


function iWallet_Seal(form, signeddata){
	if(! iWallet_CheckValid() )
		return false;
	form.cipher.value = iWalletObject.Envelop(signeddata, iWallet_BuildCipher(form));
	return true;

}
	
function iWallet_Encrypt(form) {
	if ( ! iWallet_CheckValid() ) return false;
	form.elements['cipher'].value = iWalletObject.Encrypt(iWallet_BuildCipher(form));
	return true;
}

function iWallet_EncryptSubmit(form) {
	if ( ! iWallet_CheckValid() ) return false;
	form.elements['cipher'].value = iWalletObject.Encrypt(iWallet_BuildCipher(form));
	form.submit();
	return true;
}

function iWallet_Decrypt(cipher) {
	if ( ! iWallet_CheckValid() ) return 'ERROR!!';
	return iWalletObject.Decrypt(cipher);
}


function iWallet_Authenticate(plaintext, bankcode, idtext, pwtext, ExtOption) {
	if ( ! iWallet_CheckValid() ) return false; 
	return iWalletObject.AuthenticateEx(plaintext, bankcode, idtext, pwtext, ExtOption);
}

function iWallet_GetUserCertInfo(ExtOption){
	if ( ! iWallet_CheckValid() ) return false;
	return iWalletObject.GetUserCertInfo("", "", ExtOption);
}

function iWallet_VerifyIdentity(UserSSN, UserRNum, UserCert){
	if ( ! iWallet_CheckValid() ) return false;
	return parseInt(iWalletObject.VerifyIdentity(UserSSN, UserRNum, UserCert))
}


function iWallet_RequestSetBankLoginInfo(bankcode, id, password){
	if( ! iWallet_CheckValid() ) return false;
	return iWalletObject.RequestSetBankLoginInfo(bankcode, id, password); 
}

function iWallet_PermitSetBankLoginInfo(bankcode){
	if ( ! iWallet_CheckValid() ) return false; 
	return parseInt(iWalletObject.PermitSetBankLoginInfo(bankcode));
}

function iWallet_IssueUserCertEx(bankcode, ExtOption){
	if ( ! iWallet_CheckValid() ) return false;
	return parseInt(iWalletObject.IssueCert(bankcode, ExtOption));
}

function iWallet_RevokeUserCertEx(bankcode, ExtOption){
	if ( ! iWallet_CheckValid() ) return false;
	return parseInt(iWalletObject.RevokeCert(bankcode, ExtOption));
}


function iWallet_RenewUserCertEx(bankcode, ExtOption){
	if ( ! iWallet_CheckValid() ) return false;
	return parseInt(iWalletObject.RenewCert(bankcode, ExtOption)); 
}

function iWallet_HoldUserCertEx(bankcode, ExtOption){
	if( ! iWallet_CheckValid() ) return false;
	return parseInt(iWalletObject.HoldCert(bankcode, ExtOption));
}


function iWallet_RemoveBankCert(bankcode, bankid){
	if ( ! iWallet_CheckValid() ) return false;
	return parseInt(iWalletObject.RevokeBankCert(bankcode, bankid));
}

function iWallet_OpenCertManage(bankcode, mode){
	if( ! iWallet_CheckValid() ) return false;
	return parseInt(iWalletObject.OpenCertManage(bankcode, mode));

}

function iWallet_CertKeyGen(bankcode, login, passwd, cn, rrn, email) {
	if ( ! iWallet_CheckValid() ) return false; 
	return iWalletObject.RequestCertificate1(bankcode, login, passwd, cn, rrn, email);
}

function iWallet_CertRequest(casigneddata, login, passwd, rrn) {
	if ( ! iWallet_CheckValid() ) return false; 
	return iWalletObject.RequestCertificate2(casigneddata, login, passwd, rrn);
}

function iWallet_CertInstall(userid, cert) {
	if ( ! iWallet_CheckValid() ) return false; 
	return iWalletObject.InstallCertificate(userid, cert);
}

function iWallet_RemoveUserCert(userdn){
	if( ! iWallet_CheckValid() ) return false;
	return parseInt(iWalletObject.RemoveUserCert(userdn));

}

function iWallet_DeleteUserInfo(bankcode){
	if( ! iWallet_CheckValid() ) return false;
	return parseInt(iWalletObject.DeleteUserInfo(bankcode, ""));

}

function iWallet_DataSign(InputData, ExtOption){
	if( ! iWallet_CheckValid() ) return "0|0000|¸ðµâÀÌ ¼³Ä¡µÇ¾î ÀÖÁö¾Ê½À´Ï´Ù";
	return iWalletObject.DataSignDlg(InputData, ExtOption);
}

function iWallet_GetUserCertCount(BankCode, ExtOption){
	if( ! iWallet_CheckValid() ) return 0;
	return parseInt(iWalletObject.GetUserCertCount(BankCode, ExtOption));

}

// insert by lvforyou : 2003.08.26 - Excel File Save
function iWallet_StoreExcel(InputData, ExtOption){
	if ( ! iWallet_CheckValid() ) return false;
	return parseInt(iWalletObject.SaveEncData(InputData, ExtOption));
}
// For Backward compatability

function StartSession(form) { return iWallet_StartSession(form); }
function Envelop(form) { return iWallet_Envelop(form); }
function Encrypt(form) { return iWallet_Encrypt(form); }
function EncryptSubmit(form) { return iWallet_EncryptSubmit(form); }
function Decrypt(cipher) { return iWallet_Decrypt(cipher); }
function Authenticate(plaintext, bankcode, idtext, pwtext, mode) { return iWallet_Authenticate(plaintext, bankcode, idtext, pwtext, mode); }
function RequestCertificate1(bankcode, login, passwd, cn, rrn, email) { return iWallet_CertKeyGen(bankcode, login, passwd, cn, rrn, email); }
function RequestCertificate2(casigneddata, login, passwd, rrn) { return iWallet_CertRequest(casigneddata, login, passwd, rrn); }
function InstallCertificate(userid, cert) { return iWallet_CertInstall(userid, cert); }
function PermitChangeBankPassword(bankcode){ return iWallet_PermitSetBankLoginInfo(bankcode);}
