﻿/*
* Code bellow initialize any variables that are specific to IE or other browsers
*/
if(window.ActiveXObject)
{//Internet Explorer
    var classLiteral = "className";
    //use for DOM setAttributes for assigning a CSS class to an element
}
else
{//other (e.g. Firefox)
    var classLiteral = "class";
}
/*
* We're going to store the XML representation of the list globally
* for use in the combobox functionality
*/
var SupplierListXml = "";
/*
* This section initializes event handlers for various elements of the HTML page
* and anything else that needs done when the page is loaded.
*/
window.onload = function()
{
    //handler for submitting the search form
    document.getElementByName("form2")[0].onsubmit = function()
    {
        return DistribuidoresList(document.getElementByName("select")[0].value);
    }
    //add event handler for hiding error messages
    document.getElementByName("msgdiv").onclick = function(evt)
    {
        showMsg("");
    }
}
/*
* Function that clears a node
*/
function clearANode(node)
{
    if(node.childNodes.length>0)
    {
        var child = node.childNodes[0];
        while(child!=null)
        {
            node.removeChild(child);
            child=node.childNodes[0];
        }
    }
}
/*
* Display a message to the user.
*/
function showMsg(msg)
{
    document.getElementById("msgdiv").innerHTML = msg;
    if(msg.length==0)
    {
        document.getElementById("msgdiv").style.display = "none";
    }
    else
    {
        document.getElementById("msgdiv").style.display = "inline";
    }
}
/*
* Callback function used by DistribuidoresList function above.
* @param results an XML document that contains Suppliers List results.
*/
function handleListResults(results)
{
    //clear the messages
    showMsg("");    
    //asign the results to our global variable
    SupplierListXml=results;
    //clear existing results if any
    var resultlist = document.getElementById("resultlist");
    clearANode(resultlist);
    //get the results that were returned    
    var xmlobj=null;
    var total = 0;
    try
    {
        total =results.getElementsByTagName("supplier").length;//the number of results
        xmlobj = results;
    }
    catch(e)
    {
        if (document.implementation.createDocument)
        {
            // Mozilla, create a new DOMParser
            var parser = new DOMParser();
            xmlobj = parser.parseFromString(results, "text/xml");
        } 
        else if (window.ActiveXObject)
        {
            // Internet Explorer, create a new XML document using ActiveX
            // and use loadXML as a DOM parser.
            xmlobj = new ActiveXObject("Microsoft.XMLDOM");
            xmlobj.async="false";
            xmlobj.loadXML(results);
        }
        total = xmlobj.getElementsByTagName("supplier").length;//the number of results
    }    
    //var totalsuppliers = total;        
    for(i=0; i<total; i++)
    {
        var currentSupplier = xmlobj.getElementsByTagName("supplier").item(i);
        var id = currentSupplier.getElementsByTagName("id").item(0).firstChild.nodeValue;
        var name = currentSupplier.getElementsByTagName("name").item(0).firstChild.nodeValue;
        appendSupplier(id,name,resultlist);
    }  
    //expose the list of suppliers
    document.getElementById("resultlist").style.display="inline";
    //hide the searching message
    if(total==1)
    {
        document.getElementById("locationspan").innerHTML = total + " distribuidor encontrado.";  
    }
    else
    {
        document.getElementById("locationspan").innerHTML = total + " distribuidores encontrados.";  
    }
}
/*
* Function that appends every supplier in the list and creates it's dynamic
* html for it's display.
*/
function appendSupplier(id,name,resultlist)
{
    //clear the messages
    showMsg("");
    //create a list item to hold the data
    var li = document.createElement("li");    
    //create a link so when you click on the supplier it redirect you somewhere else
    var link = document.createElement("a");
    link.setAttribute("title","Haga click para ver la información del distribuidor");
    link.appendChild(document.createTextNode(name));
    link.setAttribute("href","default.aspx?module=Distribuidores&action=Distribuidores&subaction=Distribuidor&id="+id);   
    li.appendChild(link);
    resultlist.appendChild(li);
}
function DistribuidoresList(input)
{
//clear the messages");
    showMsg("");
    document.getElementById("locationspan").innerHTML = "Buscando...";
    var RequestURL = "ajaxdefault.aspx?module=Distribuidores&action=Distribuidores&subaction=Lista&idlocation=" + input;
    makeHttpRequest(RequestURL, "handleListResults", true);
    //prevents the browswer from trying to submit the form");
}

