var cstDependFilterFields = Array("ext_custom_1005", "ext_custom_1007", "ext_custom_1004", "ext_custom_1008", "ext_custom_1011", "ext_custom_1006", "ext_custom_1003");
var cstFilterFieldsFirstOptionCaption = Array("Βρε", "Βρε", "Βρε", "Βρε", "Βρε", "Βρε", "Βρε");
var cstLastChangedFilterField = "";
var cstFilterUpdated = false;

var cstDebug = false;

//+
/*
function cstChangeFirstSelectOption() {
  var i, el;

  if(!cstFilterUpdated) {
    cstFilterUpdated = true;
    cstUpdateFilter(cstDependFilterFields[0]);
  }

  for(i=0; i<cstDependFilterFields.length; i++) {
    if(cstFilterFieldsFirstOptionCaption[i] != "") {
      el = document.forms[_cms_filter_form].elements[cstDependFilterFields[i]];
      if(el.selectedIndex == 0) {
        el.options[0].text = cstFilterFieldsFirstOptionCaption[i];
      }
    }
  }
}
//*/

function cstDisableFields(disable) {
    var el;
    for(i=0; i<cstDependFilterFields.length; i++) {
        //el = document.forms[_cms_filter_form].elements[cstDependFilterFields[i]].disabled = disable;
        if(document.getElementById(cstDependFilterFields[i])){
            el = document.getElementById(cstDependFilterFields[i]);
            el.disabled = disable;
        }
    }
}

//+
function cstUpdateFilter(fieldName) {
    var i, urlParams, el;

    cstLastChangedFilterField = fieldName;

    // Strip http://www
    var baseHr = cstStripHttpWww(frontBaseHref);
    var modLnk = cstStripHttpWww(active_module_link);

    // Prepare category path
    var subpath = "katalog";
    var filters = "";
    var aTmp = modLnk.split('?');
    //subpath = aTmp[0].substr(baseHr.length);

    // Prepare filter values
    for(i=0; i<cstDependFilterFields.length; i++) {
        if(document.getElementById(cstDependFilterFields[i])){
            filters += "&" + cstDependFilterFields[i] + "=" + encodeURIComponent(document.getElementById(cstDependFilterFields[i]).value);
        }
    }

    // Prepare catid
    var fform = document.getElementById("fltform");
    var catid = "&catid="+fform.catid.value;
    urlParams = "&subpath=" + subpath + catid + filters;

    cstDisableFields(true);

    if(typeof(DEBUG_BY_IP) != "undefined"){
        //alert("urlParams = "+urlParams);
    }

    amiAjax.getContent('GET', frontBaseHref+'cst_get_filter4.php', urlParams, cstUpdateFilterCB);
}


function cstRebuildSelectBox(fieldName) {
    var el, n, i, value;
    //el = document.forms[_cms_filter_form].elements[fieldName];
    if(document.getElementById(fieldName)){
        el = document.getElementById(fieldName);
        n = el.length;
        //value = el.value;
        for(i=n-1; i>0; i--) {
            el.options[i] = null;
        }
        var valuesArray;
        var defVal;
        eval("valuesArray="+fieldName+"_resp;");
        eval("defVal="+fieldName+"_def;");
        for(i=0; i<valuesArray.length; i++) {
            //el.options[i+1] = new Option(valuesArray[i], encodeURIComponent(valuesArray[i]));
            el.options[i+1] = new Option(valuesArray[i], valuesArray[i]);

            // Apply styles to options
            if(cstIsUrlVal(fieldName, valuesArray[i])){
                el.options[i+1].className = "uz_flt_item_highlight";
            } else {
                el.options[i+1].className = "uz_flt_item_normal";
            }
        }
        //el.value = value;
        //el.value = encodeURIComponent(defVal);

        el.value = defVal;

        // Apply normal style to 'all' option
        el.options[0].className = "uz_flt_item_normal";


        // Apply styles to select
        if(el.value != "" && cstIsUrlVal(fieldName, el.value)){
            el.className = "uz_flt_item_highlight";
        } else {
            el.className = "uz_flt_item_normal";
        }
    }
}

function cstGetUrlVals() {
    var baseHr = cstStripHttpWww(frontBaseHref);
    var modLnk = cstStripHttpWww(active_module_link);
    var aTmp = modLnk.split('?');
    //subpath = aTmp[0].substr(baseHr.length);
    var aRes = {};
    if(aTmp.length > 1){
        var aFilters = aTmp[1].split('&');
        for(var i = 0; i < aFilters.length; i++){
            if(aFilters[i].indexOf('=') > 0){
                var aTmp = aFilters[i].split('=');
                aRes[aTmp[0]] = decodeURIComponent(aTmp[1]);
            }
        }
    }
    return aRes;
}

function cstIsUrlVal(fieldName, val) {
    var res = false;
    var aUrlParams = cstGetUrlVals();
    //alert(uzObjToString(aUrlParams));
    if(typeof(aUrlParams[fieldName]) != 'undefined'){
        if(aUrlParams[fieldName] == val){
            res = true;
        }
    }
    return res;
}


function uzObjToString(oData, indent){
    if(typeof(indent) == 'undefined'){
        var indent = "";
    }
    var res = "";
    if(typeof(oData) == 'object'){
        for(var i in oData){
            if(typeof(oData[i]) == 'object'){
              res += indent + i+": {\n"+uzObjToString(oData[i], indent + "  ")+indent + "}\n";
            } else {
              res += indent + i+":"+oData[i]+"\n";
            }
        }
    } else {
        res += indent + oData+"\n";
    }
    return res;
}


function cstUpdateFilterCB(status, content) {
    if(typeof(DEBUG_BY_IP) != "undefined"){
        //alert(status);
    }
    if(status == 1) {
        if(content != "") {
            //document.getElementById("debug_resp").innerHTML = content;
            if(typeof(DEBUG_BY_IP) != "undefined"){
                //alert(content);
            }
            eval(content);

            var i, listIsFound = false;
            for(i=0; i<cstDependFilterFields.length; i++) {
                //if(cstDependFilterFields[i] != cstLastChangedFilterField || document.getElementById(cstDependFilterFields[i]).value == "") {
                    cstRebuildSelectBox(cstDependFilterFields[i]);
                //}
            }
        }
    }
    cstDisableFields(false);
}


function cstOnLoadFillFilter() {
    var i, urlParams, el, aTmp;

    cstDisableFields(true);

    // strip http://www
    var baseHr = cstStripHttpWww(frontBaseHref);
    var modLnk = cstStripHttpWww(active_module_link);
    //var modLnk = "katalog";

    // prepare category path
    var subpath = "katalog";
    var filters = "";
    var aTmp = modLnk.split('?');
    //subpath = aTmp[0].substr(baseHr.length);
    if(aTmp.length > 1){
        filters = aTmp[1];
    }
   
    urlParams = "&subpath=" + subpath + "&" + filters;
    //alert(urlParams);
    amiAjax.getContent('GET', frontBaseHref+'cst_get_filter4.php', urlParams, cstUpdateFilterCB);
}

function cstStripHttpWww(str) {
    var res = str.substr(7);
    if(res.substr(0, 4) == "www."){
        res = res.substr(4);
    }
    return res;
}


// Set form sorting and offset fields by current URL params
function cstOnLoadFillSortOffset() {
    var modLnk = cstStripHttpWww(active_module_link);
    var aTmp = modLnk.split('?');
    //subpath = aTmp[0].substr(baseHr.length);
    if(aTmp.length > 1){
        var fform = document.forms[_cms_document_form];
        var aParams = aTmp[1].split('&');
        for(var i = 0; i < aParams.length; i++){
            var aParam = aParams[i].split('=');
            if(aParam.length == 2){
                switch(aParam[0]){
                    case "sort":
                    case "sdim":
                    case "offset":
                        fform.elements[aParam[0]].value = aParam[1];
                        //alert(aParam[0]+" = "+aParam[1]);
                        break;
                    default:
                        break;
                }
            }
        }
    }
}


