﻿var Server = null;
var divCategories = null;
var divAttributes = null;
var divResults = null;
var Container = null;
var NotesBlock = null;
var objPrevPage = null;
var objNextPage = null;
var AccountName = "";
var SelectedCategory = 0;
var SelectedAttributes = "";
var ProductID = 0;
var Wait = null;
var SkipSearch = false; // Used when loading the Attributes block
var AutoSearch = true;
var startCategory = 3; // Set the start category here
var loadingMsg = "Loading...";
var resultsTemplate = "Configurator";
var divWait = "divWait";
var BodyMain = "MainBody";
var divcategories = "jk_categories";
var divattributes = "jk_attributes";
var divresults = "jk_results";
var divnotes = "jk_notes";
var prevpageobj = "prevpage";
var nextpageobj = "nextpage";
var pagesize = 10;
var pagenumber = 1;
var pagecount = 0;

function init(accountName)
{
    Wait = document.getElementById(divWait);
    AccountName = accountName;
    try{Server = new Configurator();}catch(e){}
    Container = document.getElementById(BodyMain);
    divCategories = document.getElementById(divcategories);
    divAttributes = document.getElementById(divattributes);
    divResults = document.getElementById(divresults);
    NotesBlock = document.getElementById(divnotes);
    objPrevPage = document.getElementById(prevpageobj);
    objNextPage = document.getElementById(nextpageobj);
    LoadCategories(startCategory);
    ChangePage(1);
}
function ShowLoading(show)
{
    if(show)
    {
        Wait.style.visibility = "visible";
        Wait.style.display = "inline";
    }
    else
    {
        Wait.style.visibility = "hidden";
        Wait.style.display = "none";
    }
}
function Test(cnt)
{
    var tst = Server.TestMethod(cnt.outerHTML);
    alert(tst.name);
}
function LoadCategories(category)
{
    ShowLoading(true);
    SelectedCategory=startCategory;
    var selectedIndex = -1;
    var AddCategory = true;
    
    if(divCategories.childNodes.length>0)
    {
        var skip = false;
        for(i=0;i<divCategories.childNodes.length;i++)
        {
            var cat = null;
            for(c=0;c<divCategories.childNodes[i].childNodes.length;c++)
            {
                cat = divCategories.childNodes[i].childNodes[c];
                if(cat.name && cat.name.indexOf("category_key_")>-1)
                {
                    selectedIndex = i;
                    if(cat.options[cat.selectedIndex].value.length>0)
                        SelectedCategory=parseInt(cat.options[cat.selectedIndex].value,10);
                    if(cat.id==category.id){skip=true;break;}
                }
            }
            if(skip)break;
        }
    }
    if(selectedIndex>-1)
    {
        for(i=divCategories.childNodes.length-1;i>0;i--)
        {
            if(i>selectedIndex)
            {
                AddCategory=false;
                divCategories.removeChild(divCategories.childNodes[i]);
            }
        }
    }
    var newCategory = document.createElement("div");
    newCategory.innerHTML = Server.GetCategory(SelectedCategory);
    if(AddCategory)
        divCategories.appendChild(newCategory.firstChild); 
    // Clearing the selected attributes and loading the new attributes available
    SelectedAttributes = "";
    if(category)
        LoadAttributes(null);
    ShowLoading(false);
}
function LoadAttributes (attribute)
{
    ShowLoading(true);
    // Wheather to load the new block or not
    SelectedAttributes = "";
    if(divAttributes.childNodes.length>0)
    {
        for(i=0;i<divAttributes.childNodes.length;i++)
        {
            var att = null;
            for(a=0;a<divAttributes.childNodes[i].childNodes.length;a++)
            {
                att = divAttributes.childNodes[i].childNodes[a].childNodes[1];
                if(att && att.name && att.name.indexOf("attribute_list_")>-1)
                {
                    if(att.selectedIndex>0)
                    {
                        if(SelectedAttributes.length==0)
                            SelectedAttributes = att.id+'='+att.options[att.selectedIndex].value;
                        else
                            SelectedAttributes += ','+att.id+'='+att.options[att.selectedIndex].value;
                    }
                }
            }
        }
    }
    if(attribute && attribute.options)
        divAttributes.innerHTML = Server.GetAttributes(SelectedCategory, parseInt(attribute.options[attribute.selectedIndex].value, 10), SelectedAttributes);
    else
        divAttributes.innerHTML = Server.GetAttributes(SelectedCategory, 0, SelectedAttributes);
    pagenumber = 1;
    GetResults(false);
    ShowLoading(false);
}
function GetResults(forced)
{
    var dotheload = AutoSearch;
    if (forced)
    {
        dotheload = true;
    }
    divResults.innerHTML = '<span class="epc_loadingResults">' + loadingMsg + '</span>';
    var Results = Server.GetResults(SelectedCategory, SelectedAttributes, dotheload, resultsTemplate, pagenumber, pagesize);
    var recordcount = parseInt(Results.slice(4,Results.indexOf("-->")));
    pagecount = (recordcount / pagesize) + 1;
    divResults.innerHTML = Results;
}
function ChangePage(newpage)
{
    pagenumber = newpage;
    GetResults(false);
//    if (pagenumber < 2)
//    {
//        pagenumber = 1;
//        objPrevPage.style.visibility = "hidden";
//        objPrevPage.style.display = "none";
//    } else {
//        objPrevPage.style.visibility = "visible";
//        objPrevPage.style.display = "inline";
//    }
//    if (pagenumber >= pagecount)
//    {
//        objNextPage.style.visibility = "hidden";
//        objNextPage.style.display = "none";
//    } else {
//        objNextPage.style.visibility = "visible";
//        objNextPage.style.display = "inline";
//    }
}
