var loadingTitle;

function initLoadingTitle(title)
{
	loadingTitle = title;
}
//------------------------------------------------------------------------------
//   AJAX functions for manufacturers select
//------------------------------------------------------------------------------
function getBrandsSelect()
{
    // check if brands is not loaded already
    if($("#brand option").length == 1)
    {
        var url ='index.php';
        var pars ='page=modules/advancedcategories/ajaxresponse/advancedcategories_dropdown_selector&command=getBrands&&rid='+Math.random();

        $.ajax({
            type: "POST",
            url: url,
            data: pars,
            dataType: "json",
            success: function(rsp){showBrandResponse(rsp)}
        });

        showWait4Brand();
        return true;
    }
    else
    {
        // loaded already
    }
}
function showWait4Brand()
{
    $("#brand").find("option").remove();
    $("#brand").append($("<option></option>").attr("value","0").attr("selected","selected").text(loadingTitle));
    setTimeout(function(){
        $(".activeSelect").each(function(){
            if($(this).parent().attr("id").indexOf("brand") != -1)
            {
                $(this).text(loadingTitle);
            }
        });

    }, 100);
    lockSelectBoxes();
}

function showBrandResponse(originalRequest)
{
    var data = originalRequest;
    $("#brand").find("option").remove();
    $("#brand").append($("<option></option>").attr("value","0").attr("selected","selected").text(brand_title));
    for (var i = 0; i < data.childs.length; i++)
        $('#brand').append($("<option></option>").attr("value",data.childs[i].id).text(data.childs[i].name));

    unLockSelectBoxes();
    reloadNiceSelects();
    // open select
    $("#brand").prev(".selectPrintle").trigger("click");
}


function getPrinterTypeSelect()
{
    var parent_id = $('#brand').val();
    if(parent_id == 0)
        return false;
    
    var url ='index.php';
    var pars ='page=modules/advancedcategories/ajaxresponse/advancedcategories_dropdown_selector&command=getPrinterTypes&categoryId='+parent_id+'&rid='+Math.random();

    $.ajax({
        type: "POST",
        url: url,
        data: pars,
        dataType: "json",
        success: function(rsp){showPrinterTypeResponse(rsp)}
    });
    
    showWait4PrinterType();
    return true;
    
}
//------------------------------------------------------------------------------
function showWait4PrinterType()
{
    $("#printer_type").find("option").remove();
    $("#printer_type").append($("<option></option>").attr("value","0").attr("selected","selected").text(loadingTitle));
    setTimeout(function(){
        $(".activeSelect").each(function(){
            if($(this).parent().attr("id").indexOf("type") != -1)
                $(this).text(loadingTitle);
        });

    }, 100);
    lockSelectBoxes();
}
//------------------------------------------------------------------------------
function showPrinterTypeResponse(originalRequest)
{
    data = originalRequest;
    // adding again "type" title
    $('#printer_type option:selected').text(type_title);
    
    // clear models list and add title
    $("#models").find("option").remove();
    $("#models").append($("<option></option>").attr("value","0").attr("selected","selected").text(model_title));

    // adding options to printer type select
    for (var i = 0; i < data.childs.length; i++)
	    $('#printer_type').append($("<option></option>").attr("value",data.childs[i].id).text(data.childs[i].name));
        
    unLockSelectBoxes();
    reloadNiceSelects();
}

//------------------------------------------------------------------------------
//   AJAX functions for models select
//------------------------------------------------------------------------------
function getModelSelect()
{
    var parent_id=$('#printer_type').val();
    if(parent_id == 0)
        return false;
    
    var url ='index.php';
    var pars ='page=modules/advancedcategories/ajaxresponse/advancedcategories_dropdown_selector&command=getPrinterModels&categoryId='+parent_id+'&rid='+Math.random();

    $.ajax({
        type: "POST",
        url: url,
        data: pars,
        dataType: "json",
        success: function(rsp){showModelResponse(rsp)}
    });
    
    showWait4Model();
    return true;
}
//------------------------------------------------------------------------------
function showWait4Model()
{
    $("#models").find("option").remove();
    $("#models").append($("<option></option>").attr("value","0").attr("selected","selected").text(loadingTitle));
    setTimeout(function(){
        $(".activeSelect").each(function(){
            if($(this).parent().attr("id").indexOf("model") != -1)
                $(this).text(loadingTitle);
        });

    }, 100);
    lockSelectBoxes();
}
//------------------------------------------------------------------------------
function showModelResponse(originalRequest)
{
    data = originalRequest;
    
    // adding again "model" title
    $('#models option:selected').text(model_title)
    
    for (var i = 0; i < data.childs.length; i++)
        $('#models').append($("<option></option>").attr("value",data.childs[i].id).text(data.childs[i].name));
    unLockSelectBoxes();
    reloadNiceSelects();
}
//------------------------------------------------------------------------------
function getCartridges()
{
    var parent_id=$('#models').val();
    if(parent_id == 0)
        return false;
    
    var url ='index.php';
    var pars ='page=modules/advancedcategories/ajaxresponse/get_child_categories_json&categoryId='+parent_id+'&rid='+Math.random();

    $.ajax({
        type: "POST",
        url: url,
        data: pars,
        dataType: "json",
        success: function(rsp){showCartridges(rsp)}
    });
    showWait4Model();
    return true;
}
//------------------------------------------------------------------------------
function showCartridges(originalRequest)
{
    data = originalRequest;
    document.location.href = data.path;
}

//------------------------------------------------------------------------------
function lockSelectBoxes()
{
    setTimeout(function(){
        $(".activeSelect").addClass("selectPrintleDisabled");
    }, 100);
}
function unLockSelectBoxes()
{
    $(".activeSelect").removeClass("selectPrintleDisabled");
}

//------------------------------------------------------------------------------
//   AJAX functions for manufacturers select
//------------------------------------------------------------------------------
function $F(id)
{
    return document.getElementById(id).value;
}
function getPrinterTypeSelect2()
{
    var parent_id=$F('brand2');
    var url ='index.php';
    var pars ='page=modules/advancedcategories/ajaxresponse/get_child_categories_json&categoryId='+parent_id+'&rid='+Math.random();

    $.ajax({
        type: "POST",
        url: url,
        data: pars,
        dataType: "json",
        success: function(rsp){showPrinterTypeResponse2(rsp)}
    });
    showWait4PrinterType2();
}
//------------------------------------------------------------------------------
function showWait4PrinterType2()
{
    $("#ink_laser").find("option").remove();
    $("#ink_laser").append($("<option></option>").attr("value","0").attr("selected","selected").text(loadingTitle));
    setTimeout(function(){
        $(".activeSelect").each(function(){
            if($(this).parent().attr("id").indexOf("ink_laser") != -1)
                $(this).text(loadingTitle);
        });

    }, 100);
    lockSelectBoxes();
}
//------------------------------------------------------------------------------
function showPrinterTypeResponse2(originalRequest)
{
    data = originalRequest;
    var select = document.getElementById('ink_laser');
    var categoryid = document.getElementById('categoryid');
    select.options[0] = new Option(data.type_title,0);
    categoryid.options[0] = new Option(data.model_title,0);
    var count=1;
    select.options.length=count;
    categoryid.options.length=count;
    for (var i = 0; i < data.childs.length; i++) {
	    select.options[count] = new Option(data.childs[i].name,data.childs[i].id);
	    count+=1;
	    select.options.length=count;
    }

    unLockSelectBoxes();
    reloadNiceSelects();
    $("#brand").prev(".selectPrintle").unbind('click');
    $("#brand").prev(".selectPrintle").click(function(){
        getBrandsSelect();
    });
    $("#customer-form-data .selectPrintle").width(210 + "px");
    $(".my-printers .selectPrintle").not(":last").css({marginRight:"32px"});
}

//------------------------------------------------------------------------------
//   AJAX functions for models select
//------------------------------------------------------------------------------
function getModelSelect2()
{
    var parent_id=$F('ink_laser');
    var url ='index.php';
    var pars ='page=modules/advancedcategories/ajaxresponse/get_child_categories_json&categoryId='+parent_id+'&rid='+Math.random();

    $.ajax({
        type: "POST",
        url: url,
        data: pars,
        dataType: "json",
        success: function(rsp){showModelResponse2(rsp)}
    });
    showWait4Model2();
}
//------------------------------------------------------------------------------
function showWait4Model2()
{
    $("#categoryid").find("option").remove();
    $("#categoryid").append($("<option></option>").attr("value","0").attr("selected","selected").text(loadingTitle));
    setTimeout(function(){
        $(".activeSelect").each(function(){
            if($(this).parent().attr("id").indexOf("categoryid") != -1)
                $(this).text(loadingTitle);
        });

    }, 100);
    lockSelectBoxes();
}
//------------------------------------------------------------------------------
function showModelResponse2(originalRequest)
{
    data = originalRequest;

    var select = document.getElementById('categoryid');
    select.options[0] = new Option(data.model_title,0);
    var count=1;
    select.options.length=count;
    for (var i = 0; i < data.childs.length; i++) {
	   select.options[count] = new Option(data.childs[i].name,data.childs[i].id);
	   count+=1;
	   select.options.length=count;
    }
    
    unLockSelectBoxes();
    reloadNiceSelects();
    $("#brand").prev(".selectPrintle").unbind('click');
    $("#brand").prev(".selectPrintle").click(function(){
        getBrandsSelect();
    });
    $("#customer-form-data .selectPrintle").width(210 + "px");
    $(".my-printers .selectPrintle").not(":last").css({marginRight:"32px"});
}
//------------------------------------------------------------------------------
