/* var gServerUrl = "http://admin9.credit.or.kr/pki_auth"; */
var gServerUrl = "http://www.credit.or.kr/pki_auth";
var gService  = "kait"
var debug=false;
function cqweb_load()
{
    document.writeln("<OBJECT");
    document.writeln("classid=clsid:EBAF8970-3EC3-4608-8ED1-370BC407F4B6");
    //document.writeln("CODEBASE=/CQWeb/TrustWeb.cab#version=3,4,2,3");
    //document.writeln("CODEBASE=/CQWeb/TrustWeb.cab#version=3,4,4,1");
    //document.writeln("CODEBASE=/CQWeb/TrustWeb.cab#version=3,5,5,5");
    document.writeln("CODEBASE=/CQWeb/TrustWeb.cab#version=3,5,7,9");
    document.writeln("id=cqweb");
    document.writeln(">");
    document.writeln("</OBJECT>");
}
cqweb_load();
if (cqweb.CQWIGetEnvironment(gService, gServerUrl) != 0) {
    alert ((debug?"fail to CQWIGetEnvironment()":"") + cqweb.CQWIGetErrorMsg());
	alert (cqweb.CQWIGetErrorMsg());
}

function cqweb_login()
{
    var strRequestData;
    var theForm = document.forms[0];
    var strCAFilter;

    if (cqweb.CQWIInit(gService) != 0)
    {
        alert((debug?"CQWIInit() error :":"") + cqweb.CQWIGetErrorMsg());
        return;
    }
    if (theForm.SessionID.value == "") {
        alert("form.SessionID.value is bad.");
        return;
    }
    if (theForm.AlgID.value == "") {
        alert("form.AlgID.value is bad.");
        return;
    }

    strCAFilter = "";
    strRequestData = cqweb.CQWISessionRequestWithSignature("", 1, "ou=company,o=kait", strCAFilter, 
        theForm.SessionID.value, theForm.AlgID.value, -1);
    if(strRequestData == "")
    {
      if(debug)  alert("Fail to generate session request message in client");
      if(debug)  alert("Error code : " + cqweb.CQWIGetErrorNum());
        if (cqweb.CQWIGetErrorNum() != 295239680)  // Ãë¼ÒÇÏ¼Ì½À´Ï´Ù
		alert((debug?"Error Msg : ":"") + cqweb.CQWIGetErrorMsg());
        return;
    }
    else{
        if(debug)alert("Session ID : " + theForm.SessionID.value);
        theForm.SessionRequestData.value = strRequestData;
        theForm.submit();
    }
}

function cqweb_redirection(new_url)
{
    //location.href = new_url;
    location.replace(new_url);
    return true;
}

function cqweb_encrypt(sessID)
{
    var strEncryptedData;
    var theForm = document.forms[0];

    if (sessID == "") {
        alert("bad sessID");
        return;
    }
    if (theForm.OrgData.value == "") {
        alert("Input a plain text and then click the 'next' button");
        return;
    }
    if(cqweb.CQWIInit(gService) != 0) {
        alert("CQWIInit() error :" + cqweb.CQWIGetErrorMsg());
        return;
    }

    strEncryptedData = cqweb.CQWISessionEncrypt(sessID,theForm.OrgData.value);
    //strEncryptedData = cqweb.CQWISessionEncrypt(sessID, strData);
    if (strEncryptedData == ""){
        alert("Fail to encrypt a data by Client");
        return;
    }
    else{
        //alert ("Data: " + cqweb.CQWISessionDecrypt(sessID, strEncryptedData));
        //alert("Encrypted Data by Client : \n["+strEncryptedData+"]");
        theForm.EncryptedData.value = strEncryptedData;
        theForm.submit();
    }
}

function cqweb_auth(form)
{
    var theForm = form;
    if(theForm==null){
        theForm = document.forms[0];
    }

    if (cqweb.CQWIInit(gService) != 0) 
    {
        alert("CQWIInit() error :" + cqweb.CQWIGetErrorMsg());
        return;
    }
    if (theForm.SessionID.value == "")
    {
        if(debug)alert("SessionID is not defined.");
        return;
    }

   // °øÀÎ ÀÎÁõ¼­¸¸ ÇÊÅÍ¸µ
    var strCAFilter = "CROSSCERT$KNCA$KTNET$SIGNGATE$SIGNKOREA$YESSIGN$TRADESIGN";
    strRequestData = cqweb.CQWIRequestCheckUserID("", strCAFilter, 
        theForm.sin_p1.value + theForm.sin_p2.value);
    if (strRequestData == "")
    {
        if(debug)alert(cqweb.CQWIGetErrorNum() + ":" + cqweb.CQWIGetErrorMsg());
        return;
    } 
    else 
    { 
	if(debug)alert("Session ID : " + theForm.SessionID.value); 
	theForm.SessionRequestData.value = strRequestData; 
	theForm.submit();
    }
}

