/* *************    L o g i n S u b m i t    *************************/
// Next var's must be initialized globally outside this script
// var loginuserempty = ""; // "user field empty"
// var logincertURL = "";   // full URL for certified login
// var loginuncertURL = ""; // full URL for uncertified login
// var mobLoginStartError = ""; // "cannot start the XMLHTTPRequest"
// var mobLoginStartMessage = ""; // "starting Mobile ID login"
// var mobLoginWaitingMessage = ""; // "waiting for Mobile ID login accept"
// var mobLoginOKMessage = ""; // "waiting for Mobile ID login accept"
// var mobLoginTimeoutMessage=""; // waiting for answer is timed out
// var koodinumbritext = "";        // "the Magical number:"
// var reqtimes=[70,20,10,10,10,10]; // polling policy (seconds between polls)
// var moberroricon = "img ..."; // erroricon element content
//
var LoginStartError = ""; // "cannot start the XMLHTTPRequest"
var LoginStartMessage = ""; // "starting Mobile ID login"
var LoginWaitingMessage = ""; // "waiting for Mobile ID login accept"
var LoginOKMessage = ""; // "waiting for Mobile ID login accept"
var LoginTimeoutMessage=""; // waiting for answer is timed out

var ldrMob = null; // object for asynchronous communication to server
var timeout=0;     // timeout time in seconds
var intervalMob=0; // total time in seconds
var countMobind=0; // index to polling policy array (seconds between polls)
var countMob=0;    // current pollintervalMob countMober in seconds
var intervalMobID=0; // timer's ID for clearing it
var stopitMob = false;   // to block entering LoginSubmit twice
var mobSubmitType = "IDMOB";  // to identify MOBID , SMARTID, MOBILESCA
var isNewPhone = false;  // to determine from which screen phone number is sent
var isTypeLT = false;
var callbackFunction = null;   // callback function
var isActive = true;

function LoginSubmitWithCallBack(callback){ /* returns always FALSE : "do not continue with submit" */
	callbackFunction = callback;
	return LoginSubmit();
}

function LoginSubmit(){ /* returns always FALSE : "do not continue with submit" */
    if (stopitMob||!document.unmainform) {
        return false;
    }
    if(document.unmainform.user.className!=='hidden' && document.unmainform.user.value.length===0){
        setErrorMessage(loginuserempty);
        if(document.getElementById('username1')){
        	document.getElementById('username1').focus();
        }
        return false;
    }
    if ((typeof(IdcodeControl) !== 'undefined') && IdcodeControl){
      if(typeof($('INPUT[name=certsel]:checked').val()) ==='undefined'){
        setErrorMessage(logintypeempty);
        return false;
      }
      if (document.unmainform.cert&&(document.unmainform.cert.value=="MOB"||document.unmainform.cert.value=="SMARTID"||document.unmainform.cert.value=="MOBILESCA")){
        if (!document.unmainform.isTrustedDevice && document.unmainform.IDcode && document.unmainform.IDcode.value.length === 0){
            setErrorMessage(IDcodeempty);
            if (document.getElementById('idcode1') && (!document.getElementById('idcode1').hidden)){
            	document.getElementById('idcode1').focus();
            }
            return false;
        }
      }
    }
    // set action: if cert==ON, then certified, else (empty or MOB) uncertified
    if (document.unmainform.cert&&document.unmainform.cert.value=="ON"){
        document.unmainform.action=logincertURL;
    }
    else{
        document.unmainform.action=loginuncertURL;
    }
    var i=0;
    if(document.unmainform.uposcontainer){
        if(document.unmainform.uposcontainer[1].checked){i=1;}
        var ii = document.unmainform.action.lastIndexOf("/");
        if(ii<0) {document.unmainform.action=document.unmainform.uposcontainer[i].value;}
        else {document.unmainform.action=document.unmainform.action.substring(0,ii+1)+document.unmainform.uposcontainer[i].value;}
    }
    if (document.unmainform.cert&&(document.unmainform.cert.value=="MOB"||document.unmainform.cert.value=="SMARTID"||document.unmainform.cert.value=="MOBILESCA")){
        if(document.unmainform.cert.value=="MOB") mobSubmitType = "IDMOB";
        else mobSubmitType = document.unmainform.cert.value;

        if(isNewPhone&&mobSubmitType=="IDMOB")
        {
            if (validatePhone(document.unmainform.newphone.value)) {
                SendPhone();
            }
            else return false;
        }
        else MobPressed();

        return false;
    }
    else {
        document.unmainform.submit();
        stopitMob = true;
        return true;
    }
}

/****************************    M o b P r e s s e d    *********************/
function MobPressed(){
	processCallback("MobPressed");
	if(document.baseURI==logincertURL){
        document.unmainform.act.value="LOGIN1";
        document.unmainform.cert.value=mobSubmitType;
        document.unmainform.submit();
    return;
    }
    setMessages(mobSubmitType);

    setTimeoutVal();
    var mdiv;
    if(mobSubmitType==="SMARTID") {mdiv=document.getElementById("smartidcode");}
    else if (mobSubmitType==="MOBILESCA") {mdiv=document.getElementById("mobilescacode");}
    else mdiv=document.getElementById("midcode");
    if (mdiv){mdiv.innerHTML = '<span class="label">' + LoginStartMessage + '</span>';}
    mdiv=document.getElementById("mobmessage");
    if (mdiv){mdiv.innerHTML = "";}
    mdiv=document.getElementById("mobwait");
    if (mdiv){mdiv.innerHTML = "";}
    mdiv=document.getElementById("smartidwait");
    if (mdiv){mdiv.innerHTML = "";}
    mdiv=document.getElementById("smartidmessage");
    if (mdiv){mdiv.innerHTML = "";}
    mdiv=document.getElementById("mobilescawait");
    if (mdiv){mdiv.innerHTML = "";}
    mdiv=document.getElementById("mobilescamessage");
    if (mdiv){mdiv.innerHTML = "";}
    if(mobSubmitType==="SMARTID") {mdiv=document.getElementById("smartidkinnitan");}
    else if (mobSubmitType==="MOBILESCA") {mdiv=document.getElementById("mobilescalogin");}
    else {mdiv=document.getElementById("mobkinnitan");}
    if (mdiv&&mdiv.style&&mdiv.style.display) { mdiv.style.display="";}
    var ndiv=document.getElementById("loginpage");
    if (ndiv){ ndiv.style.display="none";}
    mdiv=document.getElementsByClassName("groupcontent")[0].getElementsByTagName("ul")[0];
    if (mdiv&&mdiv.id!="moberror"){mdiv.style.display="none";}
    mdiv=document.getElementById("moberror");
    if (mdiv){ mdiv.style.display="none";}

    formMobDisable(true);
    //setMobMessage(LoginStartMessage);

    countMobind=0;
    intervalMob=timeout;
    countMob=0;
    isTypeLT = false;

    setWaitingMessage(mobWaitingMessage+" "+intervalMob);
    var cAct="";
    if (typeof(document.unmainform.nonuserauth)!='undefined' && document.unmainform.nonuserauth.value!='')
        cAct=document.unmainform.nonuserauth.value;
    else if (typeof(document.unmainform.req)!='undefined' && document.unmainform.req.value!='')
         cAct='MOBLOGINTEST&pwd=' + mobSubmitType + '&req=' + document.unmainform.req.value + "&code="  + document.unmainform.req.value;
    else cAct='MOBLOGINSTART';
    var parameters = "act=" + cAct + "&usar="+document.unmainform.user.value+"&lang="+document.unmainform.lang.value+"&mobdevice="+mobSubmitType+"&logintype="+(document.unmainform.logintype?document.unmainform.logintype.value:"");
    parameters += "&idcode=" + encodeURIComponent(document.unmainform.IDcode ? document.unmainform.IDcode.value : "");
    intervalMobID=window.setInterval(testResponse,1000);
    isActive=true;
    ldrMob=new net.ContentLoader(document.unmainform.action,doServerResponse,doServerError,parameters,"GET");
    if (!ldrMob||!ldrMob.req) {
        doServerError();
        return;
    }
}
/****************************    S e n d P h o n e    *********************/
function SendPhone(){
    countMobind=0;
    intervalMob=timeout;
    countMob=0;

	processCallback("SendPhone");

    var cAct="";
    var cPhone="";
    if (typeof(document.unmainform.nonuserauth)!='undefined' && document.unmainform.nonuserauth.value!='')
        cAct=document.unmainform.nonuserauth.value;
    else if (typeof(document.unmainform.req)!='undefined' && document.unmainform.req.value!='')
         cAct='MOBLOGINTEST&pwd=' + mobSubmitType + '&req=' + document.unmainform.req.value + "&code="  + document.unmainform.req.value;
    else cAct='MOBLOGINSTART';
    if (isNewPhone){
        cPhone=encodeURIComponent(document.unmainform.newphone.value);
        document.unmainform.phone.value=document.unmainform.newphone.value;
    }
    else {
        cPhone=encodeURIComponent(document.unmainform.phone.value);
        if(document.unmainform.newphone){document.unmainform.newphone.value=document.unmainform.phone.value;}
    }

    var parameters = "act=" + cAct + "&usar="+document.unmainform.user.value+"&lang="+document.unmainform.lang.value+"&mobdevice="+mobSubmitType+"&logintype="+(document.unmainform.logintype?document.unmainform.logintype.value:"")+"&phone="+cPhone+"&setphone="+"yes" + "&idcode=" + encodeURIComponent(document.unmainform.IDcode ? document.unmainform.IDcode.value : "");

    var mdiv;
    if(mobSubmitType==="SMARTID") {mdiv=document.getElementById("smartidcode");}
    else if(mobSubmitType==="MOBILESCA") {mdiv=document.getElementById("mobilescacode");}
    else {mdiv=document.getElementById("midcode");}
    if (mdiv){ mdiv.innerHTML = '<span class="label">' + LoginStartMessage + '</span>';}
    mdiv=document.getElementById("mobmessage");
    if (mdiv){mdiv.innerHTML = "";}
    mdiv=document.getElementById("mobwait");
    if (mdiv){mdiv.innerHTML = "";}
    mdiv=document.getElementById("smartidwait");
    if (mdiv){mdiv.innerHTML = "";}
    mdiv=document.getElementById("smartidmessage");
    if (mdiv){mdiv.innerHTML = "";}
    mdiv=document.getElementById("mobilescawait");
    if (mdiv){mdiv.innerHTML = "";}
    mdiv=document.getElementById("mobilescamessage");
    if (mdiv){mdiv.innerHTML = "";}
    if(mobSubmitType==="SMARTID") {mdiv=document.getElementById("smartidkinnitan");}
    else if(mobSubmitType==="MOBILESCA") {mdiv=document.getElementById("mobilescalogin");}
    else {mdiv=document.getElementById("mobkinnitan");}
    if (mdiv&&mdiv.style&&mdiv.style.display) { mdiv.style.display="";}
    var ndiv=document.getElementById("loginpage");
    if (ndiv){ ndiv.style.display="none";}
    ndiv=document.getElementById("mobphone");
    if (ndiv){ ndiv.style.display="none";}
    mdiv=document.getElementById("mobinfo1");
    if(mdiv){ mdiv.style.display="none";}

    mdiv=document.getElementById("mobphone");
    if (mdiv){
        mdiv.classList.remove('error');
        mdiv.style.display="none";
    }
    mdiv=document.getElementById("mobphonemsg");
    if (mdiv){
        mdiv.classList.remove('error');
        mdiv.innerHTML = "";
    }
    mdiv=document.getElementById("moberror");
    if (mdiv){
        mdiv.style.display="none";
    }
    mdiv=document.getElementsByClassName("groupcontent")[0].getElementsByTagName("ul")[0];
    if (mdiv&&mdiv.id!="moberror"){mdiv.style.display="none";}

    formMobDisable(true);

    intervalMobID=window.setInterval(testResponse,1000);
    isActive=true;
    ldrMob=new net.ContentLoader(document.unmainform.action,doServerResponse,doServerError,parameters,"GET");
    if (!ldrMob||!ldrMob.req) {
        doServerError();
        return;
    }
}

/****************************    v a l i d a t e P h o n e    *********************/
function validatePhone(phone){
	var phoneRe;
    if (!phone || 0 === phone.length){
        setErrorMessage(emptyPhoneMessage);
        return false;
    }
    if  (phone.length < 7)  {
    	setErrorMessage(incorrectPhoneMessage);
        return false;
    }
	if ((phone.substring(0, 1) === "6") || (phone.substring(0, 4) === "3706") || (phone.substring(0, 5) === "+3706")) {
		phoneRe = /^(6|3706|\+3706)\d{7}$/; /* 6xxxxxxx, +3706xxxxxxx, 3706xxxxxxx */	
	} else {
		phoneRe = /^(5|8|3725|3728|\+3725|\+3728)\d{6,7}$/; /* 5xxxxxxx, +3725xxxxxxx  8xxxxxxx, +3728xxxxxxx 3725xxxxxxx 3728xxxxxxx */
	}
    if(!phoneRe.test(phone)) {
    	setErrorMessage(incorrectPhoneMessage);
        return false;
    }
    return true;
}

/***************************   S t o p M o b W a i t   *******************/
function StopMobWait(){
	processCallback("StopMobWait");
    formMobDisable(false);
    if(intervalMobID){window.clearInterval(intervalMobID);}
    isActive=false;
    var mdiv=document.getElementById("mobkinnitan");
    mdiv.style.display="none";
    mdiv=document.getElementById("smartidkinnitan");
    mdiv.style.display="none";
    mdiv=document.getElementById("mobilescalogin");
    mdiv.style.display="none";
    mdiv=document.getElementById("mobphone");
    if(mdiv){
        mdiv.style.display="none";
        if(document.unmainform.phone){document.unmainform.phone.value="";}
        if(document.unmainform.newphone){document.unmainform.newphone.value="";}
    }
    mdiv=document.getElementById("mobinfo");
    if(mdiv){mdiv.style.display="none";}
    mdiv=document.getElementById("mobinfo1");
    if(mdiv){mdiv.style.display="none";}
    var ndiv=document.getElementById("loginpage");
    if (ndiv){ ndiv.style.display="";}
    if(document.getElementById('idcode1') && (!document.getElementById('idcode1').hidden)){
    	document.getElementById('idcode1').focus();
    }else{if(document.getElementById('username1') && (!document.getElementById('username1').hidden)){
    	document.getElementById('username1').focus();
    }
    }
//    alert(mdiv.style.display);
}

/***************************   f o r m M o b D i s a b l e   *******************/
function formMobDisable(ifdisabled){
	processCallback("formMobDisable");
    stopitMob = ifdisabled;
    if(document.unmainform.uposcontainer){
        document.unmainform.uposcontainer[0].disabled=ifdisabled;
        document.unmainform.uposcontainer[1].disabled=ifdisabled;
    }
    if(document.unmainform.businessowner) {
        document.unmainform.businessowner.disabled=ifdisabled;
    }
    document.unmainform.user.disabled=ifdisabled;
    if(!ifdisabled){
        document.unmainform.pwd.value="";
        document.unmainform.code.value="";
    }
    if(document.getElementById('idcode1') && (!document.getElementById('idcode1').hidden)){
    	document.getElementById('idcode1').focus();
    }else{if(document.getElementById('username1') && (!document.getElementById('username1').hidden)){
    	document.getElementById('username1').focus();
    }
    }
}

/***************************   s e t M o b M e s s a g e   *******************/
function setMobMessage(messtext,iferror){
    var mdiv;
    processCallback("setMobMessage");
    if(mobSubmitType==="SMARTID") {mdiv=document.getElementById("smartidmessage");}
    else if (mobSubmitType==="MOBILESCA") {mdiv=document.getElementById("mobilescamessage");}
    else {mdiv=document.getElementById("mobmessage");}
    if(mdiv){
        mdiv.innerHTML=(iferror?("<"+moberroricon+">"):"")+messtext.replace(/#/g,"<br/>");
        if(mdiv.className){mdiv.className=(iferror?"mobh4err":"mobh4");}
    }
}

/***************************   s e t W a i t i n g M e s s a g e    **********/
function setWaitingMessage(messtext,iferror){
    var mdiv;
    processCallback("setWaitingMessage");
    if(mobSubmitType==="SMARTID") {mdiv=document.getElementById("smartidwait");}
    else if (mobSubmitType==="MOBILESCA") {mdiv=document.getElementById("mobilescawait");}
    else {mdiv=document.getElementById("mobwait");}
    if(mdiv){
        mdiv.innerHTML=(iferror?("<"+moberroricon+">"):"")+messtext.replace(/#/g,"<br/>");
        if(mdiv.className){mdiv.className=(iferror?"mobh4err":"mobh4");}
    }
}

/***************************   s h o w I n f o M e s s a g e   *******************/
function showInfoMessage(){
	processCallback("showInfoMessage");
    if (mobSubmitType === "IDMOB"){
        var mdiv=document.getElementById("mobinfo");
        if(mdiv){mdiv.style.display=""}
    }
}

/***************************   s e t E r r o r M e s s a g e   *******************/
function setErrorMessage(error){
	processCallback("setErrorMessage", error);
    var mdiv=document.getElementById("errmsg");
    if(mdiv){
        mdiv.style.display="";
        mdiv.innerHTML=error;
    }

    mdiv=document.getElementById("moberror");
    if (mdiv){mdiv.style.display = "";}

    mdiv=document.getElementsByClassName("groupcontent")[0];
    if (mdiv){
        mdiv = mdiv.getElementsByTagName("ul")[0];
        if (mdiv.id!="moberror"){
            mdiv.style.display="none";
        }
    }
}

/***************************   d o S e r v e r R e s p o n s e    **********/
function doServerResponse(){
	processCallback("doServerResponse");
if(isActive){
    if(intervalMobID){window.clearInterval(intervalMobID);}
    isActive=false;
    intervalMobID=0;
    var mdiv=null;
    //var vastus = ldrMob.req.responseText;
    var vastus = ldrMob.req.responseText.replace('<?xml version="1.0" encoding="UTF-8"?>','').replace(/^\s+|\s+$/g,'');
    if(vastus.substr(0,6)=="ERROR:"){
        isNewPhone = false;
        if (vastus.substr(6)==="USERNAME_IDCODE_MUST_MATCH"){if (typeof(userAndIDCodeMustMatch)=='undefined'){setErrorMessage('USERNAME_IDCODE_MUST_MATCH')}else{setErrorMessage(userAndIDCodeMustMatch)};}else{setErrorMessage(vastus.substr(6));}
        setWaitingMessage("");

        promptLogin();

        mdiv=document.getElementById("newphone");
        if (mdiv){mdiv.style.display = "none";}

        if(document.unmainform.phone){document.unmainform.phone.value = "";}
        if(document.unmainform.newphone){document.unmainform.newphone.value = "";}
        formMobDisable(false);
    }
    else if(vastus.substr(0,8)=="SKERROR:"){
        parseSkError(vastus);
    }
    else if(vastus.substr(0,6)=="REDIR:") {
        var params = vastus.split(":");
        setMobMessage(LoginOKMessage);
        if(mobSubmitType==="SMARTID") {mdiv=document.getElementById("smartidcode");}
        else if (mobSubmitType==="MOBILESCA") {mdiv=document.getElementById("mobilescacode");}
        else {mdiv=document.getElementById("midcode");}
        if(mdiv) { mdiv.innerHTML="";}
        document.unmainform.user.disabled=false;
        if(document.unmainform.businessowner){document.unmainform.businessowner.disabled=false;}
        document.unmainform.cert.value="";
        document.unmainform.pwd.value=mobSubmitType;
        if(params[1]!='') document.unmainform.act.value=params[1];
        if(params[2]!='') {
            var fields=params[2].split("¤");
            var field;
            for (var i = 0; i < fields.length; i++) {
                field = fields[i].split("=");
                 if (eval("document.unmainform."+field[0]))
                        eval("document.unmainform."+field[0]+".value=field[1]");
                 else $('FORM[name=unmainform]').append('<input type="hidden" name="'+field[0]+'" value="'+field[1]+'"/>');
            }
        };
        document.unmainform.submit();
        return;
    }
    else if(vastus.substr(0,3)=="OK:") {
        setMobMessage(LoginOKMessage);
        if(mobSubmitType==="SMARTID") {mdiv=document.getElementById("smartidcode");}
        else if (mobSubmitType==="MOBILESCA") {mdiv=document.getElementById("mobilescacode");}
        else {mdiv=document.getElementById("midcode");}
        if(mdiv) { mdiv.innerHTML="";}
        document.unmainform.user.disabled=false;
        if(document.unmainform.businessowner){document.unmainform.businessowner.disabled=false;}
        document.unmainform.cert.value="";
        document.unmainform.pwd.value=mobSubmitType;
        if (document.unmainform.sesscode)
            document.unmainform.sesscode.value=vastus.substr(3);
        /*logic for psd2 switch login*/
        if(includes(vastus,"RESTRICTED")){
            document.unmainform.action = document.unmainform.action.replace("ipank.p", "ifirma.w");
        }
        document.unmainform.submit();
        setUserTrustedDevice(vastus);
        return;
    }
    else if(vastus.substr(0,8)=="NOPHONE:"){
        mdiv=document.getElementById("mobinfo1");
        if(mdiv){ mdiv.style.display="";}
        promptPhone();
        return;
    }
    else if(vastus.substr(0,9)=="BADPHONE:"){
        promptPhone();
        mdiv=document.getElementById("mobphone");
        if (mdiv){
            msgdiv=document.getElementById("mobphonemsg");
            if (vastus.substr(9)&&msgdiv){
                msgdiv.classList.add('error');
                mdiv.classList.add('error');
                msgdiv.innerHTML = '<span class="text">' + vastus.substr(9) + '</span>';
            }
            stopitMob = false;
        }
        return;
    }
    else {
        if(vastus.substr(0,4)=="REQ:"){
            parseReq(vastus);
        }
        else if(vastus.substr(0,8)=="NONUSER:"){
            document.unmainform.code.value=vastus;
            var lastpos = -1;
            var newpos = 0;
            while((newpos=vastus.indexOf(":",lastpos+1))>0){lastpos=newpos;}
            if (mobSubmitType==="SMARTID") {mdiv=document.getElementById("smartidcode");}
            else if (mobSubmitType==="MOBILESCA") {mdiv=document.getElementById("mobilescacode");}
            else {mdiv=document.getElementById("midcode");}
            if (mdiv){mdiv.innerHTML = '<span class="text">' + koodinumbritext + '</span><span class="code">' + vastus.substr(lastpos+1) + '';}
            setMobMessage(LoginWaitingMessage);
        }
        intervalMobID=window.setInterval(testResponse,1000);
        isActive=true;
    }
    ldrMob=null;
    }
}
function setUserTrustedDevice(response) {
    var responseParts = response.split(":");
    if (responseParts.length < 3) {
        return;
    }
    var username = document.unmainform.hashedUsername;
    var userSettingsCookie = $.IbCookie("usersettings");
    if (userSettingsCookie == null) {
        userSettingsCookie = {"dev_id": responseParts[2], "settings": {}}
    } else {
        userSettingsCookie = JSON.parse(userSettingsCookie);
    }
    if (getObjectKeysCount(userSettingsCookie["settings"]) >= 30) {
        userSettingsCookie["settings"] = {};
    }
    if (userSettingsCookie["settings"][username] != null) {
        userSettingsCookie["settings"][username]["dev_exp"] = responseParts[3];
    } else {
        userSettingsCookie["settings"][username] = {"dev_exp": responseParts[3]}
    }
    if (userSettingsCookie["dev_id"] != responseParts[2]) {
        userSettingsCookie["dev_id"] = responseParts[2];
    }
    $.IbCookie("usersettings", JSON.stringify(userSettingsCookie), {expires:365});
}

function getObjectKeysCount(object) {
    if (!Object.keys) {
    // Fallback for old browsers
        var size = 0;
        for (var key in object) {
          size++
        }

        return size;
    } else {
        return Object.keys(object).length
    }
}

/***************************   p r o m p t L o g i n    **********/
function promptLogin(){
	processCallback("promptLogin");
    mdiv=document.getElementById("mobkinnitan");
    if (mdiv){mdiv.style.display = "none";}
    mdiv=document.getElementById("smartidkinnitan");
    if (mdiv){mdiv.style.display = "none";}
    mdiv=document.getElementById("mobilescalogin");
    if (mdiv){mdiv.style.display = "none";}
    mdiv=document.getElementById("mobphone");
    if (mdiv){mdiv.style.display = "none";}
    mdiv=document.getElementById("mobinfo");
    if (mdiv){mdiv.style.display = "none";}
    mdiv=document.getElementById("loginpage");
    if (mdiv){ mdiv.style.display = "";}
}

/***************************   parseReq    **********/
function parseReq(vastus){
	processCallback("parseReq");
    document.unmainform.code.value=vastus;
    var lastpos = -1;
    var newpos = 0;
    while((newpos=vastus.indexOf(":",lastpos+1))>0){lastpos=newpos;}
    mdiv=document.getElementById("koodinumber");
    if(!(mdiv)) {
        if(mobSubmitType==="SMARTID") {mdiv=document.getElementById("smartidcode");}
        else if (mobSubmitType==="MOBILESCA") {mdiv=document.getElementById("mobilescacode");}
        else {mdiv=document.getElementById("midcode");}
    }
    if (mdiv){
        if ( vastus.substr(lastpos+1).length > 0){
            if (vastus.substr(lastpos+1) === "LT"){
                isTypeLT = true;
            }
            else {
                if(document.getElementById("koodinumber")) mdiv.innerHTML = vastus.substr(lastpos+1);
                else mdiv.innerHTML = '<span class="text">' + koodinumbritext + '</span><span class="code">' + vastus.substr(lastpos+1) + '';
                setMobMessage(LoginWaitingMessage);
                showInfoMessage();
            }
        }
        else {
            mdiv.innerHTML = '<span class="label">' + LoginStartMessage + '</span>';
        }
    }
}

/***************************   parseSkError    **********/
function parseSkError(vastus){
    var mdiv;

	processCallback("parseSkError");
    setWaitingMessage("");
    setErrorMessage(vastus.substr(8));
    if (mobSubmitType === "IDMOB"){
        mdiv=document.getElementById("newphone");
        if (vastus.substr(9)&&mdiv){
            isNewPhone = true;
            mdiv.style.display = "";
            mdiv.classList.add('error');
            if(document.unmainform.newphone){document.unmainform.newphone.value = "";}
        }

        if(document.unmainform.phone){document.unmainform.phone.value = "";}
    }

    formMobDisable(false);
    promptLogin();
}

/***************************   p r o m p t P h o n e    **********/
function promptPhone(){
	processCallback("promptPhone");
    isNewPhone = false;

    mdiv=document.getElementById("mobphone");
    if (mdiv&&mdiv.style&&mdiv.style.display) {
        mdiv.style.display="";
        stopitMob = false;
    }

    var ndiv=document.getElementById("mobkinnitan");
    if (ndiv){ ndiv.style.display="none";}
    ndiv=document.getElementById("loginpage");
    if (ndiv){ ndiv.style.display="none";}
    ndiv=document.getElementById("mobinfo");
    if (ndiv){ ndiv.style.display="none";}
    ndiv=document.getElementById("moberror");
    if (ndiv){ ndiv.style.display="none";}
    mdiv=document.getElementsByClassName("groupcontent")[0].getElementsByTagName("ul")[0];
    if (mdiv&&mdiv.id!="moberror"){mdiv.style.display="none";}
}

/*************************   t e s t  R e s p o n s e     ***************/
function testResponse(){
	processCallback("testResponse");
    countMob+=1;
    intervalMob-=1;

    setWaitingMessage(mobWaitingMessage+" "+intervalMob);
    if(countMob>reqtimes[countMobind]&&ldrMob===null&&isActive){
        if(intervalMobID){window.clearInterval(intervalMobID);}
        isActive=false;
        var anurl = loginuncertURL; // document.unmainform.action;
        var cAct = "";
        if (typeof(document.unmainform.nonuserauth)!='undefined' && document.unmainform.nonuserauth.value!='')
            cAct=document.unmainform.nonuserauth.value;
        else if (typeof(document.unmainform.VK_PARMS)!='undefined' && document.unmainform.VK_PARMS.value!='' && typeof(document.getElementsByName("3D_ATN")[0])!='undefined' && document.getElementsByName("3D_ATN")[0].value!='' )
        		cAct = "MOBLOGINTEST_HT";
        	else
        	    if (document.unmainform.act.value==="REGISTERSMARTID1") {
		    	cAct='MOBLOGINTEST&pwd=IDMOB';
		    }else{
		    	cAct="MOBLOGINTEST";
		    }
        var parameters = "act=" + cAct + "&usar=" + document.unmainform.user.value + "&lang=" + document.unmainform.lang.value + "&logintype=" + (document.unmainform.logintype ? document.unmainform.logintype.value : "");
        parameters += "&mobdevice=" + (document.unmainform.mobSubmitType?document.unmainform.mobSubmitType.value:"") + "&idcode=" + encodeURIComponent(document.unmainform.IDcode ? document.unmainform.IDcode.value : "");
        parameters += "&req=" + document.unmainform.code.value;
        countMob=0;
        countMobind++;
        if(countMobind==reqtimes.length){
            formMobDisable(false);
            setWaitingMessage("");
            if(LoginTimeoutMessage)setErrorMessage(LoginTimeoutMessage);

            promptLogin();
            return;
        }
        ldrMob=new net.ContentLoader(anurl,doServerResponse,doServerError,parameters,"GET");
        if (!ldrMob||!ldrMob.req) {
            doServerError();
            return;
        }
        intervalMobID=window.setInterval(testResponse,1000);
        isActive=true;
    }
}

/***************************   d o S e r v e r E r r o r    **********/
function doServerError(){
	processCallback("doServerError");
    if(intervalMobID){window.clearInterval(intervalMobID);}
    isActive=false;
    intervalMobID=0;
    formMobDisable(false);
    setMobMessage(LoginStartError,true);
    stopitMob=0;
}

/***************************   s e T i m e o u t V a l    **********/
function setTimeoutVal(){
    var i = reqtimes.length;
	processCallback("setTimeoutVal");
    timeout=i;

    while(i--){
        timeout+=reqtimes[i];
    }
}

/***************************   s e t M e s s a g e s    **********/
function setMessages(messType) {
	processCallback("setMessages");
    if(messType && messType=="SMARTID") {
        LoginStartError = smartLoginStartError;
        LoginStartMessage = smartLoginStartMessage;
        LoginWaitingMessage = smartLoginWaitingMessage;
        LoginOKMessage = smartLoginOKMessage;
        LoginTimeoutMessage = smartLoginTimeoutMessage;
    }
    else if(messType && messType=="MOBILESCA") {
        LoginStartError = mobilescaLoginStartError;
        LoginStartMessage = mobilescaLoginStartMessage;
        LoginWaitingMessage = mobilescaLoginWaitingMessage;
        LoginOKMessage = mobilescaLoginOKMessage;
        LoginTimeoutMessage = mobilescaLoginTimeoutMessage;
    }
    else {
        LoginStartError = mobLoginStartError;
        LoginStartMessage = mobLoginStartMessage;
        LoginWaitingMessage = mobLoginWaitingMessage;
        LoginOKMessage = mobLoginOKMessage;
        LoginTimeoutMessage = mobLoginTimeoutMessage;
    }
}

function includes(container, value) {
	var returnValue = false;
	var pos = container.indexOf(value);
	if (pos >= 0) {
		returnValue = true;
	}
	return returnValue;
}

function processCallback(message, processValue) {
    if (callbackFunction != null && typeof callbackFunction === 'function') {
		callbackFunction(message, processValue);
    }
}