

// global HTTP request
var httpRequest;

// global URL
var gausCommandURL;

// global Fail action code
var gausFailAction;

// global Fail action code
var gausRequestMethod;

// Gaus frame is loaded now
var gausFrameLoadingTime;

// Check loading interval
var gausCheckLoadInterval = 100;

// Loading timeout
var gausLoadTimeout = 10;

// Gaus timeout handler
var gausTimeout = 0;


// --------------------------------------
// Finger identification
// --------------------------------------

function IdentifyFinger(processUrl, failedAction, method)
    {

    // Process failed action
    failedAction = (failedAction==undefined?"":failedAction);

    // Process method
    method = (method==undefined?"document":method);

    //alert(processUrl);
    // Start request
    gausStartRequest("IdentifyFinger", processUrl, failedAction, method);

    }


// --------------------------------------
// Finger registration
// --------------------------------------

function RegisterFinger(processUrl, fingNum, failedAction, method)
    {

    // Process failed action
    failedAction = (failedAction==undefined?"":failedAction);

    // Process method
    method = (method==undefined?"document":method);

    // Finger number
    processUrl = processUrl + "&finger=" + fingNum;

    // Start request
    gausStartRequest("RegisterFinger", processUrl, failedAction, method);

    }

// --------------------------------------
// Start client request
// --------------------------------------

function gausStartRequest(gausCommand, processUrl, failedAction, method)
    {

    // Store request method
    gausRequestMethod = method;

    // Construct url
    gausCommandURL = "http://localhost:4423/" + gausCommand + "?p=" + processUrl + "&rnd=" + Math.random() + "&env=" + gausRequestMethod;

        //alert(gausCommandURL);

    // Store fail action
    gausFailAction = failedAction;

    // Start processing
    StartProcessing();

    }


// --------------------------------------
// Process client request
// --------------------------------------

function gausProcessRequest()
    {

    // Processing according to specific method
    switch(gausRequestMethod)
        {

        // Document location
        case "document":
            document.location = gausCommandURL;
            break;

        // Script source
        case "script":
            var old_s = document.getElementById('gausTransfer');
            if (old_s) old_s.parentNode.removeChild(old_s);
            var s = document.createElement('script');
            s.setAttribute('type', 'text/javascript');
            s.src = gausCommandURL;
            s.id = 'gausTransfer';
            document.getElementsByTagName('head')[0].appendChild(s);
            break;

        default:
            alert("Wrong request method");
            break;
        }
    }


// --------------------------------------
// Start processing
// --------------------------------------

function gausFrameLoadedNow()
    {

    // Process next request
    gausProcessRequest();

    // Delete timeout
    clearTimeout(gausTimeout);
    }


// --------------------------------------
// Start processing
// --------------------------------------

function StartProcessing()
    {

    // Initally frene does not loaded
    gausFrameLoadingTime = gausLoadTimeout * (1000/gausCheckLoadInterval);

    // Start checking timer
    gausTimeout = setTimeout("gausCheckFrameStatus()", gausCheckLoadInterval);

    // Construct check service url
    var clUrl = "http://localhost:4423/TestFingerPrint?p=check-service-js%26action%3DgausFrameLoadedNow()&rnd=" + Math.random();

    //var old_s = document.getElementById('gausChecker');if (old_s) old_s.parentNode.removeChild(old_s);

    var s = document.createElement('script');
    s.setAttribute('type', 'text/javascript');
    s.src = clUrl;
    s.id = 'gausChecker';
    //document.getElementsByTagName('head')[0].appendChild(s);        
    document.body.appendChild(s);        /**/
    //location.href=clUrl;

/*    var s = document.createElement('div');
    s.id = 'gausChecker';
    s.innerHTML = '<iframe src="'+clUrl+'"></iframe>';
    //document.getElementsByTagName('head')[0].appendChild(s);        
    document.body.appendChild(s);        /**/
    
    }



// --------------------------------------
// Loading status checker
// --------------------------------------

function gausCheckFrameStatus()
    {

    // Timeout was reached
    if(!gausFrameLoadingTime)
        {
        // Check for action
        if(gausFailAction != "") eval(gausFailAction); else alert("Error accessing Gaus.Client");

        location.href = location.href;

        // No new timer loop
        return;
        }

    // Decrease loading time
    gausFrameLoadingTime--;

    // Start checking timer
    gausTimeout = setTimeout("gausCheckFrameStatus()", gausCheckLoadInterval);

    }

