
function partsSearchEnter(e)
{
    if (getKey(e) == 13)
        partsSearchHandler();
}

function partsSearchHandler()
{
    conditions = partsGetConditions();
    storeSearch(conditions);
    clearPartsTable();
    partsSearch(null, true, conditions);
}

function partsNextHandler(point)
{
    conditions = partsGetConditions();
    storeSearch(conditions);
    clearPartsTable();
    
    // Scroll to top of control
    ltid = document.getElementById("LinkTopID");
    window.scrollTo(ltid.clientLeft, ltid.clientTop);
    
    partsSearch(point, true, conditions);
    return false;
}

function partsPrevHandler(point)
{
    conditions = partsGetConditions();
    storeSearch(conditions);
    clearPartsTable();
    
    // Scroll to top of control
    ltid = document.getElementById("LinkTopID");
    window.scrollTo(ltid.clientLeft, ltid.clientTop);
    
    partsSearch(point, false, conditions);
    return false;
}

function partsGetConditions()
{
    groupID = document.getElementById("current_group_id").value;
    markID = document.getElementById("current_mark_id").value;
    groupID = (groupID == "null") ? null : groupID;
    markID = (markID == "null") ? null : markID;
    
    number = document.getElementById("number").value;
    number = (number == "") ? null : number;
    part = document.getElementById("part").value;
    part = (part == "") ? null : part;
    manufacturerID = document.getElementById(manufacturers_box).value;
    manufacturerID = (manufacturerID == "null") ? null : manufacturerID;
        
    manufacturerName = document.getElementById("manufacturer").value;
    manufacturerName = (manufacturerName == "") ? null : manufacturerName;
    
    return new Array(number, part, groupID, markID, manufacturerID, manufacturerName);
}

function storeFocus(focused)
{
    storeFocusedID = focused.id;
}

function storeSearch(conditions)
{
    storeNumber = conditions[0]; // number
    storePart = conditions[1]; // part
    storeManufacturer = conditions[5]; // manufacturer
}

function restoreSearch()
{
    document.getElementById(storeFocusedID).focus();
    document.getElementById("number").value = (storeNumber == null) ? "" : storeNumber;
    document.getElementById("part").value = (storePart == null) ? "" : storePart;
    document.getElementById("manufacturer").value = (storeManufacturer == null) ? "" : storeManufacturer;
}

function clearPartsTable()
{
    containerNext = document.getElementById("NavigationPane");
    
    if (containerNext != null)
        containerNext.innerHTML = "";
    
    tableContent = [['Код', 'Название', 'Производитель', 'Информация']];
    tableContent[1] = getFindControls();
    tableConfig = getTableConfig();
    new gTable (tableContent, tableConfig, "PartsPane");
}

function getFindControls()
{
    return ['<input id="number" type="text" class="FindInput" maxlength="100" \
                onkeypress="javascript:partsSearchEnter(event);" \
                onfocus="javascript:storeFocus(this);return false;" />',
            '<input id="part" type="text" class="FindInput" maxlength="100" \
                onkeypress="javascript:partsSearchEnter(event);" \
                onfocus="javascript:storeFocus(this);return false;" />',
            '<input id="manufacturer" type="text" class="FindInput" maxlength="100" \
                onkeypress="javascript:partsSearchEnter(event);" \
                onfocus="javascript:storeFocus(this);return false;" />',
            ''];
}

function getTableConfig()
{
    return {'cellpadding' : 5,
            'css' : {
                'table' : 'partsTable',
                'content' : ['partsNumber','partsName','partsManufacturer','partsInfo'],
                'caption' : 'partsCaption',
                'hoveredRow' : 'partsHoverRow'}};
                //'markedRow' : 'partsMarkRow'}};
}

function partsSearch(point, next, conditions)
{
    JCAPUI.GetParts(
        point,
        next,
        pagesize,
        conditions[0],
        conditions[1],
        conditions[2],
        conditions[3],
        conditions[4],
        conditions[5],
        partsSearchCallback);
}

function partsSearchCallback(res)
{
    if (res.value.length <= 1) // No offers
    {
        pane = document.getElementById("PartsPane");
        pane.innerHTML += "<div class=info><img src=\"../img/ico_warn.gif\" \
            width=48 height=48 align=left style=\"margin:4px;\">Ничего не найдено<br><br><br></div>";
        
        restoreSearch(); // Restore search values in controls
    }
    else
    {
        partsFillTable(res.value);
        settings = res.value[0];
        prevAvailable = settings[0];
        nextAvailable = settings[1];
        first = settings[2];
        last = settings[3];
        pane = document.getElementById("NavigationPane");
        
        prevHTML = new String();
        nextHTML = new String();
        if (prevAvailable == true)
        {
            jsPrev = "'javascript:partsPrevHandler(\"" + first + "\");return false;'"
            prevHTML = "<a class='NavigationLink' href='' onclick=" + jsPrev + ">Предыдущая</a>";
        }
        
        if (nextAvailable == true)
        {
            jsNext = "'javascript:partsNextHandler(\"" + last + "\");return false;'"
            nextHTML = "<a class='NavigationLink' href='' onclick=" + jsNext + ">Следующая</a>";
        }
        delimiter = '<span style="border-right: Dotted 1px #0042A4;">&nbsp;&nbsp;</span>&nbsp;&nbsp;'
        
        paneText = prevHTML + ((prevAvailable == true && nextAvailable == true) ? delimiter : "") + nextHTML;
        pane.innerHTML = paneText;
    }
}

function partsFillTable(parts)
{
    tableContent = [['Код', 'Название', 'Производитель', '<div style="text-align: center;">Информация</div>']];
    tableContent[1] = getFindControls();
    
    for (var i = 1; i < parts.length; i++)
    {
        number = "<a class='partsNumber' href='' title='Предложения...'\
            onclick='javascript:partsShowOffers(this,\"" + parts[i][1] + "\");return false;'>" + parts[i][1] + "</a>";
        name = parts[i][2];
        manufacturer = parts[i][6];
        info = new String();
        status = parts[i][5];
        if (status == 'N')
            info += '<img src="images/parts/new.gif" border="0" title="Новинка"></a>' + '&nbsp;&nbsp;';
        else if (status == 'P')
            info += '<img src="images/parts/sale.gif" border="0" title="Распродажа"></a>' + '&nbsp;&nbsp;';
        info += '<a class="partsInfo" href="" onclick="javascript:partsShowInformation(this,' + parts[i][0] + ');return false;"><img src="images/parts/info.gif" border="0" title="Описание..."></a>';
        info += "<span style='padding-right: 15px' />";
            
        tableContent[i+1] = [number, name, manufacturer, info];
    }
    tableConfig = getTableConfig();
    
    g = new gTable (tableContent, tableConfig, "PartsPane");
    gTableAlternate(g, "partsEven", "partsOdd"); // Color parts' rows
    restoreSearch(); // Restore search values in controls
}

function partsShowOffers(caller, number)
{
    w = window.open("../search/?code=" + number + "&crs=1&cache=0", "offers_" + rand(0x7fffffff),
        "status=1, toolbar=1, location=1, menubar=1, directories=0, resizable=1, scrollbars=1");
    return false;
}

function partsShowInformation(caller, partID)
{
    x = (window.screen.width - 600) / 2 + 100;
    y = (window.screen.height - 500) / 2;
    
    w = window.open("jcapinfo.aspx?partID=" + partID, "partsinfo_" + rand(0x7fffffff),
        "left=" + x + ", top=" + y + ", width=600, height=500, status=0, toolbar=0, \
        location=0, menubar=0, directories=0, resizable=1, scrollbars=1");
    return false;
}
