//$Id: productcount.js,v 1.2 2008/07/17 18:24:00 kharlin Exp $

document.write("<script src=\"/js/ajaxfunctions.js\"></script>");

function GetProductCount()
{
    // Get the value in the search text box.
    var f = document.phoneFinder;
    var serviceCorpId = f.serviceCorpId;
    var freeprice = "";
    var tNav =  GetCheckedValue(f.tNav);
    var paId = f.paId;
    
    var paIdString = "";
    var serviceCorpString = "";
    
    //The following may or may not display 
    if(f.freeprice)
        freeprice = GetCheckedValue(f.freeprice); 
        
    for(var i=0; i < paId.length; i++)
    {
        if(paId[i].checked)
        {   
            if(paIdString)
                paIdString = paIdString+"_"+paId[i].value;
            else
                paIdString = paId[i].value;
                    
        }
    }
    
    if(serviceCorpId)
    {
        for(var i=0; i < serviceCorpId.length; i++)
        {
            if(serviceCorpId[i].checked)
            {   
                if(serviceCorpString)
                    serviceCorpString = serviceCorpString+"_"+serviceCorpId[i].value;
                else
                    serviceCorpString = serviceCorpId[i].value;
                        
            }
        }
    }

    // Create the XMLHttpRequest object.
    c = CreateHTMLRequestor();

    // if the XMLHttpRequest object is in any state other than 0 (unitialized), then stop the current request.
    if (c && c.readyState!=0)
    {
        c.abort();
    }

    // Set the function that handles changes in state.
    c.onreadystatechange = processSelectionChange;

    // Send the request.
    c.open("GET", "/phonefinder/productcount.php?serviceCorpId=" + serviceCorpString + "&tNav=" + tNav + "&freeprice=" + freeprice + "&paId=" + paIdString, true);
    c.send(null);
}

function processSelectionChange()
{
    // only if req shows "complete"
    if (c.readyState == 4)
    {
        if (c.status == 200) // Numeric code returned from server. 200 means "OK".
        {
            DisplayCountResult(c.responseText);
        } else {
            alert("There was a problem retrieving the HTML data:\n" +
                c.statusText);
        }
    }
}

function GetCheckedValue(buttonname)
{
    for (var i=0; i < buttonname.length; i++)
    {
        if (buttonname[i].checked)
        {
            var fieldname = buttonname[i].value;
        }
    }
    
    return fieldname;
}

function DisplayCountResult(responseHTML)
{
    // Get xslt xml transfrom
    display = document.getElementById('productCount')
    if (responseHTML)
    {
        display.innerHTML = responseHTML;
    }

    
}