﻿var timeoutMake;
var timeoutModel;
var timeoutRegion;
var keyID = 0;
var callActive = false;

$(document).ready(SearchLoad);

function ChangeType()
{
    AjaxPopulate('components');
}

function SearchLoad()
{
    AdvertService.set_defaultSucceededCallback(AdvertCallback);
    $('#txtSearch').bind('keyup', SearchUser);
}

function GetSServerVar(clientID)
{
    return $get($get("hdSearchModuleID").value + "_" + clientID);
}

function GetSSubServerVar(clientID)
{
    return $get($get("hdSearchSubModuleID").value + "_" + clientID);
}

function GetMPAServerVar(clientID)
{
    return $get($get("hdMPAModuleID").value + "_" + clientID);
}

function GetAServerVar(clientID)
{
    return $get($get("hdModuleID").value + "_" + clientID);
}

function SaveDllVal(ddl, mpa, sub)
{
    var name = ddl.id.substring(ddl.id.lastIndexOf("_") + 1, ddl.id.length);
    var GetSVar = GetSServerVar;

    if (mpa)
        GetSVar = GetMPAServerVar;

    if (sub)
        GetSVar = GetSSubServerVar;

    GetSVar('hd' + name).value = ddl.value;

    if (name == "ddlCountry")
    {
        GetSVar('hdddlRegion').value = '-1';

        GetSVar('hdddlCountry').value = ddl.value;
    }

    if (name == "ddlMake")
    {
        if (GetSVar('hdddlModel'))
            GetSVar('hdddlModel').value = '-1';

        GetSVar('hdddlMake').value = ddl.value;
    }

    if (name == "ddlCategory")
    {
        if (GetSVar('hdddlSubCategory'))
        {
            GetSVar('hdddlSubCategory').value = '-1';

        }

        if (GetSVar('hdddlMake'))
        {
            GetSVar('hdddlMake').value = '-1';
            GetSVar('hdddlCategory').value = ddl.value;
        }
    }
}

function doClick(buttonName, e)
{
    var key;
    //window.alert("do click");
    if (window.event)
        key = window.event.keyCode;     //IE
    else
        key = e.which;     //firefox

    if (key == 13)
    {
        var btn = (buttonName);

        if (btn != null)
        {
            WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(btn.replace(/_/g, "$"), "", true, "", "", false, true));

        }
    }
}

function ShowContactSeller(adID, usrID)
{
    $get('divContactSeller').style.display = '';
    $get('hdAdvertID').value = adID;
    $get('hdAdvertUserID').value = usrID;
    GetAServerVar('txtMessage').value = '';
    GetAServerVar('txtSubject').value = 'Automart - ' + $('#aClass' + adID).text();
}

function CloseBox()
{
    $get('divContactSeller').style.display = 'none';
}

function ContactSeller()
{
    if (Page_ClientValidate('ContactSeller'))
    {
        AdvertService.ContactSellerAdmin($get('hdAdvertID').value, $get('hdAdvertUserID').value, GetServerVar('txtSubject').value, GetServerVar('txtMessage').value);
        $get('aContact').style.display = '';
    }
}

function AdvertCallback(result, userContext, methodName)
{
    switch (methodName)
    {
        case ("ContactSellerAdmin"):
            {
                CloseContact();
                ClientMessage(result);
                break;
            }
        case ("SearchUser"):
            {
                callActive = false;
                $('.AutoComplete').css('display', $('#txtSearch').val() == '' ? 'none' : 'inline-block');
                BuildGrid($get('UserTemplate').innerHTML, result, $get("divUsers"), BuildUser, '<tr><td>No Users found</td></tr>');
            }
    }
}

function BuildUser(i, template, pageUrl)
{
    return parseTemplate(template, { i: i });
}

function fireEvent(element, eventName)
{
    evt = document.getElementById(element)[eventName];

    if (typeof (evt) == "function")
    {
        document.getElementById(element)[eventName]();

    } else { }
}

function CloseContact()
{
    $get('divContactSeller').style.display = 'none';
}

function SetContextKey(ddl)
{
    $find('bMake')._contextKey = ddl.value;
    $find('bModel')._contextKey = ddl.value;
    if (ddl.value == '4')
        $('[id*=divCriteria]').css('display', 'none');
    else
        $('[id*=divCriteria]').css('display', '');
}

function ToggleAdminSelect()
{
    var rpt = $get('hdRpt').value + "_ctl";

    for (var c = 1; c < 11; c++)
    {
        var d = '0';

        if (c == 10)
        {
            d = '';
        }

        var id = rpt + d + c + '_ctl00_chkDelete';
        $get(id).checked = !$get(id).checked;
    }
}

function SearchUser()
{
    StartSearch(++keyID);
}

function StartSearch(id)
{
    if (id == keyID)
    {
        if (callActive)
            window.setTimeout('StartSearch(' + id + ')', 20);
        else
        {
            callActive = true;
            AdvertService.SearchUser($('#txtSearch').val());
        }
    }
}

function Redirect(location)
{
    window.location = location;
}

function ClientMessage(message)
{
    $('#divClientMessage').text(message).css('display', 'inline-block');
    window.setTimeout("$('#divClientMessage').css('display', 'none')", 8000);
} 

function AjaxPopulate(type)
{
    $('#imgSLoadIndicator').show();
    m_type = type;

    $.ajax({
        url: '/categories.aspx',
        cache: false,
        data: {
            type: type,
            portalID: $('[id*=ddlType]').val()
        },
        dataType: 'json',
        success: function (data)
        {
            var ddl = $('[id*=ddlComponent]');

            PopulateDropdown(ddl, data);
            ddl.val($('[id*=hdComponentID]').val());

            $('#imgSLoadIndicator').hide();
        }
    });
}

function PopulateDropdown(ddl, result)
{
    ddl.find('option').remove();
    if (result.length == 0)
        ddl.append($('<option value="-2">No Components</option>'));
    else
    {
        ddl.append($('<option value="-1">Select</option>'));

        for (var i in result)
        {
            ddl.append($('<option></option>').val(result[i].ID).html(result[i].Name));
        }
    }
}

$('[id*=ddlType]').change(ChangeType);

$('[id*=ddlCategory]').change(function ()
{
    if ($(this).val() == '6')
    {
        $('[id*=divCriteria]').hide();
        $('[id*=divSparesCriteria]').show();
    }
    else
    {
        $('[id*=divCriteria]').show();
        $('[id*=divSparesCriteria]').hide();
    }
});

if ($('[id*=ddlType]').val() != '-1')
{
    ChangeType();
}

$('[id*=ddlComponent]').change(function ()
{
    $('[id*=hdComponentID]').val($(this).val());
});
