/////////////////////
//Function to create SOAP data packet.
function createSOAPData(strMethodName, strSoapContent)
{
    return '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n'
                 +'<soap:Envelope xmlns:xsi=\'http://www.w3.org/2001/XMLSchema-instance\' xmlns:xsd=\'http://www.w3.org/2001/XMLSchema\' xmlns:soap=\'http://schemas.xmlsoap.org/soap/envelope/\'>\n'
                 + '<soap:Body>\n'
                 +        '<' + strMethodName + ' xmlns=\'http://belcomputers.rs/\'>\n'
                 +                   strSoapContent
                 +            '</' + strMethodName + '>\n'
                 + '</soap:Body>\n'
                 +'</soap:Envelope>' ;
}

//Function to create XMLHttpRequest.
var xmlhttp;
function getXmlHttpObject()
{
    if(window.ActiveXObject)
    {
        try
        {
            xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
        }
        catch (ex)
        {
            xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
        }
    }
    else if (window.XMLHttpRequest)    //For Non-IE  
    {
        xmlhttp = new XMLHttpRequest();
    }
}

//Function to send the SOAP Request.
function sendSOAPDataToWebService(url, data, soapAction)
{
    xmlhttp.open('POST', url, true);
    xmlhttp.onreadystatechange = processgetSOAPResponseData ;
    xmlhttp.setRequestHeader('Content-Type', 'text/xml; charset=utf-8');
    xmlhttp.setRequestHeader('SOAPAction', soapAction);
    xmlhttp.send(data);
}

//Function to process the SOAP Response.
function processgetSOAPResponseData()
{
    if (xmlhttp.readyState == 4)
    {
        if (xmlhttp.status == 200) // only if "OK"
        {
            try
            {
                if (window.ActiveXObject)   // code for IE
                {
                    doc = new ActiveXObject("Microsoft.XMLDOM");
               
                    doc.async="false";
                    doc.loadXML(xmlhttp.responseText);
                }
                else    // code for Mozilla, Firefox, Opera, etc.
                {
                    var parser=new DOMParser();
                    doc = parser.parseFromString(xmlhttp.responseText,"text/xml");
                }

                var rootNode = doc.documentElement;    
                // documentElement always represents the root node
                   
                var result_node=rootNode.childNodes[0].childNodes[0].childNodes[0];
                if( result_node.nodeName  ==  "GetCategoryIdBySubcategoryIdResult" )
                {
                    ShowSubCategories('divSubCategory_' + result_node.childNodes[0].data);
                }
                else if (result_node.nodeName  ==  "GetCategoryIdByItmResult" )
                {
                    ShowSubCategories('divSubCategory_' + result_node.childNodes[0].data);
                }
                else if (result_node.nodeName == 'CreateOrderResult')
                {
                    HideBussyModalPopup();
                    
                    if (result_node.childNodes[0].data == 'true')
                    {
                        alert('Vaša porudžbina je uspešno prosleđena.');
                        //clear cart
                        ClearCart('tableCart')
                    }
                    else
                    {
                        alert('Došlo je do greške prilikom prosledjivanja porudžbine. Izvinjavamo se. Kontaktirajte naše komercijaliste.');
                    }
                    
                     //hide order window
                    var mpeOrder=$find('ctl00_mpeOrder');
                    mpeOrder.hide();
                }
            }
            catch (ex)
            {
                //HideBussyModalPopup();
            }
        }
    }
}
