﻿function CustomSearchValidation(source, arguments) {
    var hdnMinNumOfCharsForSearch = newFind('hdnMinNumOfCharsForSearch');
    var minNumOfCharsForSearch = 2 // default
    if (hdnMinNumOfCharsForSearch != null) {
        minNumOfCharsForSearch = hdnMinNumOfCharsForSearch.value;
    }
    if (arguments.Value.length < minNumOfCharsForSearch || arguments.Value.indexOf("%") > -1) 
    {
        arguments.IsValid = false;
        if (newFind("divSearchNote") != null)
            newFind("divSearchNote").style.display = '';
    }
    else
    {
        if (newFind("divSearchNote") != null)
            newFind("divSearchNote").style.display = 'none';
    }
}

function newFind(name)
{
    var elems = document.getElementsByTagName('*'); 
    var num = elems.length; 
    for(var c=0;c<num;c++)
    {
        var val = elems[c].getAttribute('FISName'); 
        if(val!=null && val==name)
            return elems[c]; 
    }
}

function HideControl(control)
{
    control.style.display = "none";
}

function RevealControl(control)
{
    control.style.display = "inline";   
}

function ClearControlValue(control)
{
    control.value = "";
}

function ShowHideControl(OpenerDiv,Div,ImagesDir)
{
    if (document.getElementById(Div).style.display == 'none')
    {
           document.getElementById(Div).style.display='inline';
           OpenerDiv.src = ImagesDir + 'inner-minus.gif';
    } 
    else
    {
           document.getElementById(Div).style.display='none';
           OpenerDiv.src = ImagesDir + 'inner-plus.gif';
                    
    }
}

function SelectRow(row_id, hdnLastRow_id)
{
    var row = document.getElementById(row_id);
    var hidden_row_id = document.getElementById(hdnLastRow_id);
    
    if(hidden_row_id != null && row != null)
    {
        if(hidden_row_id.value != "")
        {
            var prev_row_id = document.getElementById(hidden_row_id.value);
            ClearRow(prev_row_id);
        }
        hidden_row_id.value = row.id;
    }
    FillRow(row);
}

//get a row in grid and set its background
function FillRow(row)
{
    if(row != null)
    {
        ChangeRowColor(row, "#FF7F50");
    }
}

//get a row in grid and clear its background
function ClearRow(row)
{
    if(row != null)
    {
        ChangeRowColor(row, "");
    }
}

//this function get a reference to row in grid and a color name, 
//and set the row background to this color
function ChangeRowColor(row, color)
{
    var col_number = row.childNodes.length
    for(var i=0;i<col_number;i++)
        row.childNodes[i].style.background = color;
}

function ClientRedirection(url)
{
   var NScp = (navigator.appName == 'Netscape') &&
              ((navigator.appVersion.indexOf('3') != -1) ||
               (navigator.appVersion.indexOf('4') != -1));
   var MSIE = (navigator.appVersion.indexOf('4.0') != -1);
   if (NScp || MSIE)
        window.location = url;
//      location.replace(url);
   else
      location.HREF = url;
}


function SearchClicked() {
    var SearchButton = newFind('SearchButton');
    if (SearchButton != null) {
        SearchButton.click();
    }
}
  
// Searh Auto-Complete    
// Emphasize the prefix string in each auto-complete row
function AutoCompleteClientPopulated(source, eventArgs) {
    if (source._currentPrefix != null) {
        var list = source.get_completionList();
        var search = source._currentPrefix.toLowerCase();
        for (var i = 0; i < list.childNodes.length; i++) {
            var text = list.childNodes[i].innerHTML;
            var index = text.toLowerCase().indexOf(search);
            if (index != -1) {
                var value = text.substring(0, index);
                value += '<span class="AutoComplete_ListItemHiliteText">';
                value += text.substr(index, search.length);
                value += '</span>';
                value += text.substring(index + search.length);
                list.childNodes[i].innerHTML = value;
            }
        }
    }
}

// Searh Auto-Complete    
// Retreives the string value to be placed in a text box
function AutoCompleteClientItemSelected(source, e) {
    var node;
    var value = e.get_value();

    if (value) node = e.get_item();
    else {
        value = e.get_item().parentNode._value;
        node = e.get_item().parentNode;
    }

    var text = (node.innerText) ? node.innerText : (node.textContent) ? node.textContent : node.innerHtml;
    source.get_element().value = text;

    // Activate Search with the selected auto-complete value
    SearchClicked();
}

// Searh Auto-Complete    
// set search text box auto-complete mode
function CheckSearchAutoComplete() {
    var rbl1span = newFind('rbl1');
    // rbl1span.children[0] - is the "By products" radio button
    var rbl1 = rbl1span.children[0];
    if (rbl1 != null) {
        if (rbl1.checked) {
            SearchChanged(0);
        }
        else {
            SearchChanged(1);
        }
    }
}

// Searh Auto-Complete    
// itemNumber is the position of the search type radio button
// 0 - Products
// 1 - Suppliers
// Configures the search auto-suggest according to the search type
function SearchChanged(itemNumber) {
    var autoComplete = $find("SearchTextBox_AutoCompleteExtender_Behavior");
    if (autoComplete != null) {
        if (itemNumber == 0) {
            //autoComplete.set_serviceMethod('GetCategoriesAutoComplete');
            autoComplete._minimumPrefixLength = 2;
        }
        else {
            //autoComplete.set_serviceMethod('');
            autoComplete._minimumPrefixLength = 256;
        }
    }
}    