// import_x_x.js
// 
// Import functionality
//  Requires ajax_base_x_x.js
//
// --------------------------------------------------------

// --------------------------------------------------------
// Miscellaneous Functions
// --------------------------------------------------------

// Update the <div> tag
function UpdateDiv(sContent, sTargetDiv)
{
    var oDiv = document.getElementById(sTargetDiv);
    oDiv.innerHTML = sContent;
}

// Append information to the <div> tag
function AppendDiv(sContent, sTargetDiv)
{
    var oDiv = document.getElementById(sTargetDiv);
    oDiv.innerHTML = oDiv.innerHTML + sContent;
}

// Hide the element
function HideElement(sName)
{
    var oEle = document.getElementById(sName);
    oEle.style.display="none";
}

// Show the element
function ShowElement(sName)
{
    var oEle = document.getElementById(sName);
    oEle.style.display="inline";
}

// --------------------------------------------------------
// Import Function
// --------------------------------------------------------

// Start Import Process
function Import()
{
    HideElement('ImportPrompt');
    UpdateDiv(sBeginMsg + "...<br />", 'ImportStatus');
    // Start Import: setTimeout() allows <div> update to display
    setTimeout("ImportRecords(0)", 20);
}

// Import the contact file - each call to this method imports N records
// Session variables have been initialized to pass information to/from php
// URL parameter is also used to pass information into php
function ImportRecords(iDataLine)
{
    xmlhttp = CreateXMLHttp();
    if (xmlhttp)
    {
        // Target URL with Pass Number parameter
        var sURL = "./import/import_ajax.php?DataLine=" + iDataLine;

        // Invoke the Import process to Import N records
        xmlhttp.open("GET", sURL, false);
        xmlhttp.send(null);

        if (xmlhttp.readyState==4)
        {
            // Response from import
            sResponse = xmlhttp.responseText;
            aLines = sResponse.split("$$");
            AppendDiv(aLines[2] + "<br />", 'ImportStatus');
            if (aLines[0] == "Continue")    // more records to import
            {
                // setTimeout() allows <div> update to display
                iDataLine = aLines[1];
                setTimeout("ImportRecords(" + iDataLine + ")", 20);
            }
            else    // Done !
            {
                AppendDiv(sCompleteMsg + "<br />", 'ImportStatus');
                ShowElement('ImportNavigation');
            }
        }
    }
    else    // error
    {
        AppendDiv(sErrorMsg + "<br />", 'ImportStatus');
        ShowElement('ImportNavigation');
    }
}