// Definitions
i_Vessels = new Array(); // Суда
i_Display = new Array(); // Результат поиска

// Declare variables and create the header, footer, and caption.
var oTable = document.createElement("TABLE");
var oTHead = document.createElement("THEAD");
var oTBody0 = document.createElement("TBODY");
var oRow, oCell, vDWT;

vArrayID = new Array();
vArrayName = new Array();
vArrayNameSearch = new Array();
vArrayClass = new Array();
vArrayGroup = new Array();
vArrayType = new Array();
vArrayOwner = new Array();
vArrayDeliveryYear = new Array();
vArrayDeliveryMonth = new Array();
vArrayDeliveryDay = new Array();

var td1, td2, td3, td3_1, td3_2, td3_3, td4, td5, notFound;

// Structures ----------------------------------------
// Vessels
function i_vessel(id, name, ns, class1, group, type, owner, year, month, day) {
    this.ID = id;
    this.Name = name;
    this.NameSearch = ns;
    this.Class = class1;
    this.Group = group;
    this.Type = type;
    this.Owner = owner;
    this.Delivery = new Date();
    this.Delivery.setFullYear(year, month, day);
    
    this.ClassCount = 0;
    this.ClassCountOwn = 0;
    this.ClassCountCh = 0;
    this.ClassCountNew = 0;
    this.ClassCountTotal = 0;
    this.GroupCount = 0;
    this.TypeCount = 0;

    this.TotalOwn = 0;
    this.TotalCh = 0;
    this.TotalOwnCh = 0;
    this.TotalNew = 0;
    this.Total = 0;

    this.VP = ''; //Собственное, зафрахтованное, новое
}
//-----------------------------------------------------

// Search
function i_find(str) {
    a = new Array();
    var j = 0;
    if (str == '' || str == ' ') return i_Vessels;
    for (i = 0; i < i_Vessels.length; i++) {
        if (i_Vessels[i].NameSearch.toUpperCase().indexOf(str.toUpperCase()) != -1 || i_Vessels[i].Group.toUpperCase().indexOf(str.toUpperCase()) != -1 || i_Vessels[i].Type.toUpperCase().indexOf(str.toUpperCase()) != -1 || i_Vessels[i].Class.toUpperCase().indexOf(str.toUpperCase()) != -1) {
            a[j] = i_Vessels[i];
            j++;
        }
    }
    if (a.length == 0) a[0] = new i_vessel('', notFound, '', '', '', '', '', 1, 1, 1);
    return a;
}
// Count
function i_fillCounters(a) {
    var i,j;
    var class1, group, type;
    var classCounter = 0;
    var classCounterOwn = 0;
    var classCounterCh = 0;
    var classCounterNew = 0;
    var classCounterTotal = 0;
    var groupCounter = 0;
    var typeCounter = 0;

    a[0].TotalOwnCh = 0;
    a[0].TotalOwn = 0;
    a[0].TotalCh = 0;
    a[0].TotalNew = 0;
    a[0].Total = 0;

    var d = new Date();
    var VP = '';
    for (i = 0; i < a.length; i++) {
        // Формирование ссылки на страницу с информацией о судне
        a[i].Name = a[i].Name.replace('pages.aspx', 'npage.aspx');
        a[i].Name = '<a target="_blank" ' + a[i].Name.substr(2) // В отдельное окно
        
        if (a[i].Class != class1) {
            class1 = a[i].Class;
            j = i;
            classCounter = 0;
            classCounterOwn = 0;
            classCounterCh = 0;
            classCounterNew = 0;
            classCounterTotal = 0;
            while (a[j].Class == class1) {
                if (a[j].Delivery > d) {
                    classCounterNew++;
                    VP = 'new';
                }
                else {
                    if (a[j].Owner == 'SCF' || a[j].Owner == 'NS' || a[j].Owner == 'MAR' || a[j].Owner == 'SCF TERMNL') { classCounterOwn++; VP = 'own'; }
                    else { classCounterCh++; VP = 'charter'; }
                    classCounter++;
                }
                classCounterTotal++;
                a[j].VP = VP;
                j++;
                if (j == a.length) break;
            }
            a[0].TotalOwnCh += classCounter;
            a[0].TotalOwn += classCounterOwn;
            a[0].TotalCh += classCounterCh;
            a[0].TotalNew += classCounterNew;
            a[0].Total += classCounterTotal;
        }
        if (a[i].Group != group) {
            group = a[i].Group;
            j = i;
            groupCounter = 0;
            while (a[j].Class == class1 & a[j].Group == group) {
                groupCounter++;
                j++;
                if (j == a.length) break;
            }
        }
        if (a[i].Type + a[i].Group != type) {
            type = a[i].Type + a[i].Group;
            j = i;
            typeCounter = 0;
            while (a[j].Class == class1 & a[j].Type + a[i].Group == type) {
                typeCounter++;
                j++;
                if (j == a.length) break;
            }
        }
        a[i].ClassCount = classCounter;
        a[i].ClassCountOwn = classCounterOwn;
        a[i].ClassCountCh = classCounterCh;
        a[i].ClassCountNew = classCounterNew;
        a[i].ClassCountTotal = classCounterTotal;
        a[i].GroupCount = groupCounter;
        a[i].TypeCount = typeCounter;
    }
}
//Table functions
function i_addRow(a) {
    oRow = document.createElement("TR");
    //ChangeRowBgCollor(oRow);
    if (a == 'TR') oTBody0.appendChild(oRow);
    if (a == 'TH') oTHead.appendChild(oRow);
}
function i_addCell(data, cs, rs, a, v, align, color) {
    oCell = document.createElement(a);
    oCell.innerHTML = data;
    oCell.colSpan = cs;
    oCell.rowSpan = rs;
    oCell.style.backgroundColor = color;
    oCell.style.layoutFlow = v;
    oCell.style.textAlign = align;
    oRow.appendChild(oCell);
}
function i_delTable() {
    var i, j;
    with (oTable.childNodes) {
        for (i = length - 1; i > -1; i--) {
            for (j = item(i).childNodes.length - 1; j > -1; j--) item(i).removeChild(item(i).childNodes.item(j));
        }
    }
}
// Buttons
function ClickB3() {
    i_Display = i_find(document.Form1.T1.value);
    i_delTable();
    if (i_Display[0].ID != 0) i_fillCounters(i_Display); //Если найдены суда
    i_table(i_Display);
}
function ClickB4() {
    var str = document.location.toString().replace('pages.aspx', 'npage.aspx');
    var tmp = did;
    if (document.Form1.B4.value == prn) { // Если кнопка называется ПЕЧАТЬ, то печатаем документ
        window.print();
        return;
    }
    if (document.Form1.T1.value != '') tmp = tmp + '&find=' + document.Form1.T1.value; // Прибавляем строку поиска
    str = urlCut(str, 'did');
    if (str.indexOf('?') == -1) str = str + '?' + tmp;
    else str = str + '&' + tmp;
    window.open(str, "_blank"); 
}

// Заполняем объекты
function i_fill() {
    for (i = 0; i < vArrayID.length - 1; i++) {
        i_Vessels[i] = new i_vessel(vArrayID[i], vArrayName[i], vArrayNameSearch[i], vArrayClass[i], vArrayGroup[i], vArrayType[i], vArrayOwner[i], vArrayDeliveryYear[i], vArrayDeliveryMonth[i], vArrayDeliveryDay[i]);
    }
    i_fillCounters(i_Vessels);
    i_table(i_Vessels);
    if (getParam('find') != '') { // Осуществляем поиск, если в адресной строке найден параметр find
        document.Form1.T1.value = getParam('find');
        ClickB3();
    }
    if (document.location.toString().indexOf('npage.aspx') != -1) document.Form1.B4.value = prn; // Если страница называется npage.aspx переименовываем кнопку B4 в ПЕЧАТЬ
}

// Table
function i_table(a) {
    var tmp, i;
    var c_group = 0;
    var c_type = 0;
    var ClassName = '';
    var GroupName = '';
    var TypeName = '';
    // Insert the created elements into oTable.
    oTable.appendChild(oTHead);
    oTable.appendChild(oTBody0);

    // Set the table's border width and colors.
    oTable.border = 1;
    oTable.cellPadding = 1;

    // Insert a row into the header and set its background color.
    i_addRow('TH');

    // Create and insert cells into the header row.
    i_addCell(td1, 1, 2, 'TH', 'horizontal', '');
    i_addCell(td2, 1, 2, 'TH', 'horizontal', '');
    i_addCell(td3, 1, 2, 'TH', 'horizontal', '');
    i_addCell(td4, 3, 1, 'TH', 'horizontal', '');
    i_addCell(td5, 1, 2, 'TH', 'vertical-ideographic', '', 'YellowGreen');
    i_addCell(td6, 1, 2, 'TH', 'horizontal', '');

    i_addRow('TH');

    i_addCell(td4_1, 1, 1, 'TH', 'vertical-ideographic', '', 'lightblue');
    i_addCell(td4_2, 1, 1, 'TH', 'vertical-ideographic', '', 'coral');
    i_addCell(td4_3, 1, 1, 'TH', 'vertical-ideographic', '');

    // Create and insert rows and cells into the first body.
    for (i = 0; i < a.length; i++) {
        if (i == 0) {
            i_addRow('TR');
            i_addCell('<big><b><font color="darkblue">' + td6 + '</font></b></big>', 3, 1, 'TD', 'horizontal', 'left');

            i_addCell('<b><font color="darkblue">' + a[i].TotalOwn + '</font></b>', 1, 1, 'TD', 'horizontal', 'right');
            i_addCell('<b><font color="darkblue">' + a[i].TotalCh + '</font></b>', 1, 1, 'TD', 'horizontal', 'right');
            i_addCell('<b><font color="darkblue">' + a[i].TotalOwnCh + '</font></b>', 1, 1, 'TD', 'horizontal', 'right');
            i_addCell('<b><font color="darkblue">' + a[i].TotalNew + '</font></b>', 1, 1, 'TD', 'horizontal', 'right');
            i_addCell('<b><font color="darkblue">' + a[i].Total + '</font></b>', 1, 1, 'TD', 'horizontal', 'right');
        }
        if (a[i].Class != ClassName) {
            ClassName = a[i].Class;
            
            i_addRow('TR');
            i_addCell('&nbsp', 8, 1, 'TD', 'horizontal', 'left');

            i_addRow('TR');
            i_addCell('<big><b><font color="blue">' + a[i].Class + '</font></b></big>', 3, 1, 'TD', 'horizontal', 'left');

            i_addCell('<b><font color="blue">' + a[i].ClassCountOwn + '</font></b>', 1, 1, 'TD', 'horizontal', 'right');
            i_addCell('<b><font color="blue">' + a[i].ClassCountCh + '</font></b>', 1, 1, 'TD', 'horizontal', 'right');
            i_addCell('<b><font color="blue">' + a[i].ClassCount + '</font></b>', 1, 1, 'TD', 'horizontal', 'right');
            i_addCell('<b><font color="blue">' + a[i].ClassCountNew + '</font></b>', 1, 1, 'TD', 'horizontal', 'right');
            i_addCell('<b><font color="blue">' + a[i].ClassCountTotal + '</font></b>', 1, 1, 'TD', 'horizontal', 'right');

            i_addRow('TR');
        }
        i_addRow('TR');
        if (a[i].Group != GroupName) {
            GroupName = a[i].Group;
            i_addCell('<i><b>' + a[i].Group + '</b></i>', 1, a[i].GroupCount, 'TD', 'horizontal', 'left');
        }
        if (a[i].Type != TypeName) {
            TypeName = a[i].Type;
            i_addCell('<i><b>' + a[i].Type + '</b></i>', 1, a[i].TypeCount, 'TD', 'horizontal', 'left');
        }
        i_addCell(a[i].Name, 1, 1, 'TD', 'horizontal', 'left');
        if (a[i].VP == 'own') i_addCell('', 1, 1, 'TD', 'horizontal', 'left', 'lightblue'); else i_addCell('&nbsp', 1, 1, 'TD', 'horizontal', 'left');
        if (a[i].VP == 'charter') i_addCell('', 1, 1, 'TD', 'horizontal', 'left', 'coral'); else i_addCell('&nbsp', 1, 1, 'TD', 'horizontal', 'left');
        i_addCell('&nbsp', 1, 1, 'TD', 'horizontal', 'left');
        if (a[i].VP == 'new') i_addCell(a[i].Delivery.getYear(), 1, 1, 'TD', 'horizontal', 'left', 'YellowGreen'); else i_addCell('&nbsp', 1, 1, 'TD', 'horizontal', 'left');
        i_addCell('&nbsp', 1, 1, 'TD', 'horizontal', 'left');
    }

    // Insert the table into the document tree.
    oTableContainer.appendChild(oTable);
}
