﻿var http_requestOs = false;
var http_requestUs = false;

function getinfo(us_id)
{
    var uscontent = document.getElementById('uscontent');
    uscontent.style.backgroundColor = "";
    uscontent.style.textAlign = "center";
    uscontent.innerHTML = "<span style='text-align: center; color: #ffffff; font-family: Verdana'>Загрузка данных...</span>";
    var oscontent = document.getElementById('oscontent');
    oscontent.innerHTML = "";
    oscontent.style.backgroundColor = "";
    http_requestOs = false;
    http_requestUs = false;
    if (window.XMLHttpRequest)
    { // Mozilla, Safari,...
        http_requestOs = new XMLHttpRequest();
        http_requestUs = new XMLHttpRequest();
        if (http_requestOs.overrideMimeType)
        {
            http_requestOs.overrideMimeType('text/xml');
        }
        if (http_requestUs.overrideMimeType)
        {
            http_requestUs.overrideMimeType('text/xml');
        }
    }
    else if (window.ActiveXObject)
    { // IE
        try
        {
            http_requestOs = new ActiveXObject("Msxml2.XMLHTTP");
            http_requestUs = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                http_requestOs = new ActiveXObject("Microsoft.XMLHTTP");
                http_requestUs = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e)
            {}
        }
    }

    if (!http_requestOs)
    {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
    if (!http_requestUs)
    {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
    http_requestOs.onreadystatechange = alertContentsOs;
    http_requestOs.open('GET', '/rostovpost_services/services/oslistservice.asmx/getoslist?us_id=' + us_id, true);
    http_requestOs.send(null);
    http_requestUs.onreadystatechange = alertContentsUs;
    http_requestUs.open('GET', '/rostovpost_services/services/oslistservice.asmx/getusinfo?us_id=' + us_id, true);
    http_requestUs.send(null);
}

function alertContentsOs()
{
    if (http_requestOs.readyState == 4)
    {
        if (http_requestOs.status == 200)
        {
            var newTable=document.createElement('table');
            var oscontent = document.getElementById('oscontent');
            oscontent.style.backgroundColor = "#ffffff";
            oscontent.style.textAlign = "left";
            oscontent.innerHTML = '<div class="ostitle">Список отделений почтовой связи почтамта</div>';;
            newTable.setAttribute('width', '100%');
            oscontent.appendChild(newTable);
            var rowheader = newTable.insertRow(newTable.rows.length);
            var cellheader1 = rowheader.insertCell(rowheader.cells.length);
            var cellheader2 = rowheader.insertCell(rowheader.cells.length);
            cellheader1.setAttribute('class', 'osheader');
            cellheader1.setAttribute('className', 'osheader');
            cellheader2.setAttribute('class', 'osheader');
            cellheader2.setAttribute('className', 'osheader');
            cellheader1.innerHTML = 'Наименование ОПС, почтовый индекс, адрес';
            cellheader2.innerHTML = 'Режим работы';
            var xmldoc = http_requestOs.responseXML;
            var root_node = xmldoc.getElementsByTagName('oslist').item(0);
            var previouscell = '';
            var previousrowstyle = '';
            for(var i=0; i<root_node.childNodes.length;i++)
            {
                if (root_node.childNodes[i].tagName == 'os' && root_node.childNodes[i].nodeType == 1)
                {
                    var os = root_node.childNodes[i];
                    var row = newTable.insertRow(newTable.rows.length);
                    var name_os = '';
                    var index_os = '';
                    var address_os = '';
                    var days = '';
                    var day = '';
                    var per = '';
                    //
                    var dayspan = '';
                    //
                    for(var j=0; j<os.childNodes.length;j++)
                    {
                        if (os.childNodes[j].nodeType == 1)
                        {
                            switch (os.childNodes[j].nodeName)
                            {
                                case 'name_os': { name_os = os.childNodes[j].firstChild.nodeValue; break }
                                case 'index_os': { index_os = os.childNodes[j].firstChild.nodeValue; break }
                                case 'address_os': { address_os = os.childNodes[j].firstChild.nodeValue; break }
                                case 'days': { days = os.childNodes[j].firstChild.nodeValue; break }
                                case 'day': { day = os.childNodes[j].firstChild.nodeValue; break }
                                case 'per': { per = os.childNodes[j].firstChild.nodeValue; break }
                                case 'dayspan': { dayspan = os.childNodes[j].firstChild.nodeValue; break }
                                default: { ; break }
                            }
                        }
                    }
                    /*if (window.XMLHttpRequest)
                    { // Mozilla, Safari,...
                        var dayspan = os.childNodes[os.childNodes.length - 2].firstChild.nodeValue;
                        if (document.all && !window.opera && window.XMLHttpRequest)
                        {
                          var dayspan = os.childNodes[os.childNodes.length - 1].firstChild.nodeValue;
                        }
                    }
                    else if (window.ActiveXObject)
                    { // IE
                        var dayspan = os.childNodes[os.childNodes.length - 1].firstChild.nodeValue;
                    }*/
                    if (previouscell != os.childNodes[1].firstChild.nodeValue)
                    {
                        if (previousrowstyle != 'linebase')
                            previousrowstyle = 'linebase';
                        else
                            previousrowstyle = 'linealt';
                        var cell1 = row.insertCell(row.cells.length);
                        cell1.setAttribute('rowSpan', dayspan);
                        cell1.setAttribute('class', previousrowstyle);
                        cell1.setAttribute('className', previousrowstyle);
                        cell1.innerHTML = '<span class="osname">' + name_os + '</span><br />' + '<span class="osindex">' + index_os + '</span>' + ', <span class="osaddress">' + address_os + '</span>';
                    }
                    var cell2 = row.insertCell(row.cells.length);
                    cell2.setAttribute('class', previousrowstyle);
                    cell2.setAttribute('className', previousrowstyle);
                    if(day == 'выходной' || day == 'нет')
                        daytext = ' ' + day;
                    else
                        daytext = ' с ' + day.replace('-', ' до ');
                    if(per == 'выходной')
                        pertext = '';
                    else if(per == 'нет')
                        pertext = ' без перерыва';
                    else
                        pertext = ' перерыв с ' + per.replace('-', ' до ');
                    cell2.innerHTML = '<span class="osdayslist">' + days + '</span>' + '<span class="osday">' + daytext + '</span>' + '<span class="osbreak">' + pertext + '</span>';
                    previouscell = os.childNodes[1].firstChild.nodeValue;
                }
            }
        }
        else
        {
            alert('There was a problem with the request.');
        }
    }
}

function alertContentsUs()
{
    if (http_requestUs.readyState == 4)
    {
        if (http_requestUs.status == 200)
        {
            var uscontent = document.getElementById('uscontent');
            var xmldoc = http_requestUs.responseXML;
            var root_node = xmldoc.getElementsByTagName('usinfo').item(0);
            for(var i=0; i<root_node.childNodes.length;i++)
            {
                if (root_node.childNodes[i].nodeType == 1)
                {
                    switch (root_node.childNodes[i].nodeName)
                    {
                        case 'name': { var name = root_node.childNodes[i].firstChild.nodeValue; break }
                        case 'boss_position': { var boss_position = root_node.childNodes[i].firstChild.nodeValue; break }
                        case 'boss_name': { var boss_name = root_node.childNodes[i].firstChild.nodeValue; break }
                        case 'zip': { var zip = root_node.childNodes[i].firstChild.nodeValue; break }
                        case 'address': { var address = root_node.childNodes[i].firstChild.nodeValue; break }
                        case 'district': { var district = root_node.childNodes[i].firstChild.nodeValue; break }
                        case 'phone': { var phone = root_node.childNodes[i].firstChild.nodeValue; break }
                        default: { ; break }
                    }
                }
            }
            uscontent.innerHTML = '<div class="usheader">' + name + '</div>';
            uscontent.style.backgroundColor = "#ffffff";
            uscontent.style.textAlign = "left";
            var newTable=document.createElement('table');
            newTable.setAttribute('width', '100%');
            var row = newTable.insertRow(newTable.rows.length);
            var cell1 = row.insertCell(row.cells.length);
            var cell2 = row.insertCell(row.cells.length);
            cell1.innerHTML = '<span class="us_boss_position">' + boss_position + '</span>';
            cell2.innerHTML = '<span class="us_zip_address">' + zip + ', ' + address + '</span>';
            cell1.setAttribute('class', 'linealt');
            cell1.setAttribute('className', 'linealt');
            cell2.setAttribute('class', 'linealt');
            cell2.setAttribute('className', 'linealt');
            row = newTable.insertRow(newTable.rows.length);
            cell1 = row.insertCell(row.cells.length);
            cell2 = row.insertCell(row.cells.length);
            cell1.innerHTML = '<span class="us_boss_name">' + boss_name + '</span>';
            cell2.innerHTML = 'Обслуживает <span class="us_district">' + district + '</span>';
            cell1.setAttribute('class', 'linebase');
            cell1.setAttribute('className', 'linebase');
            cell2.setAttribute('class', 'linebase');
            cell2.setAttribute('className', 'linebase');
            row = newTable.insertRow(newTable.rows.length);
            cell1 = row.insertCell(row.cells.length);
            cell2 = row.insertCell(row.cells.length);
            cell1.innerHTML = '&nbsp;';
            cell2.innerHTML = 'Контакты:';
            cell1.setAttribute('class', 'linealt');
            cell1.setAttribute('className', 'linealt');
            cell2.setAttribute('class', 'linealt');
            cell2.setAttribute('className', 'linealt');
            row = newTable.insertRow(newTable.rows.length);
            cell1 = row.insertCell(row.cells.length);
            cell2 = row.insertCell(row.cells.length);
            cell1.innerHTML = '&nbsp;';
            cell2.innerHTML = 'телефон: ' + '<span class="us_phone">' + phone + '</span>';
            cell1.setAttribute('class', 'linebase');
            cell1.setAttribute('className', 'linebase');
            cell2.setAttribute('class', 'linebase');
            cell2.setAttribute('className', 'linebase');
            uscontent.appendChild(newTable);
        }
        else
        {
            alert('There was a problem with the request.');
        }
    }
}
