﻿    //generic function to submit filters
$(document).ready(function() {


    //===== FANCYBOX ==============

    $("a.fancyPicture").fancybox({
        'speedIn': 600,
        'speedOut': 200,
        'overlayShow': true,
        'scrolling': 'no',
        'titleShow': false,
        'showCloseButton': true,
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'orig': $("img.fancyPictureImg")
    });



    $("#txtSearch").load(function() {

        var stext = getUrlEncodedKey("stext", "");
        ($("#txtSearch").val(stext));
        //        log(stext);
    });


    $("#sltCategories").change(function() {


        var stext = $("#txtSearch").val();
        var DepartmentId = $("#sltCategories").val();

        var store = getUrlEncodedKey("store", "");
        if (store == null)
            store = 326;

        var page = getUrlEncodedKey("page", "");
        //log(page + " this is page");
        page = 1;

        if (($("#sltCategories").attr("value")) == "Field1")
            DepartmentId = "";
        else
            DepartmentId = ($("#sltCategories").attr("value"));


        setUrlEncodedKey("page", page, "");
        setUrlEncodedKey("stext", stext, "");
        setUrlEncodedKey("DepartmentId", DepartmentId, "");
        //log(setUrlEncodedKey("store", store, ""));



        //        log(setUrlEncodedKey("page", page, ""));
        //      log(setUrlEncodedKey("stext", stext, ""));

        var temp = new Array();
        temp = stext;

        if (stext == "Αναζητείστε τη μάρκα που ψάχνετε")
            self.location = "Brands.aspx" + "?store=" + store + "&page=" + page + "&DepartmentId=" + DepartmentId;
        else
            self.location = "Brands.aspx" + "?store=" + store + "&stext=" + temp.charAt(0).toUpperCase() + "&page=" + page + "&DepartmentId=" + DepartmentId;

        //        log(setUrlEncodedKey("store", store, ""));
    });


    $("#btnSearch").click(function() {


//        log($("#txtSearch").val());


        var stext = $("#txtSearch").val();
        var DepartmentId = $("#sltCategories").val();

        var store = getUrlEncodedKey("store", "");

        //        var page = getUrlEncodedKey("page", "");

        if (($("#sltCategories").attr("value")) == "Field1")
            DepartmentId = "";
        else
            DepartmentId = ($("#sltCategories").attr("value"));

        var page = 1;

        var temp = new Array();
        temp = stext;

        self.location = "Brands.aspx" + "?store=" + store + "&stext=" + temp.charAt(0) + "&page=" + page + "&DepartmentId=" + DepartmentId;

        setUrlEncodedKey("stext", stext, "");


        //        log(setUrlEncodedKey("stext",stext,""));
        //        log( setUrlEncodedKey("DepartmentId",DepartmentId,""))  ;
        //        log(setUrlEncodedKey("store",store,""));


    });

    $("#txtSearch").click(function() {
        if ($("#txtSearch").val() == "Αναζητείστε τη μάρκα που ψάχνετε")
            $("#txtSearch").val("");
    });


    $("#txtSearch").KeyPress(function(k) {
        if ($("#txtSearch").keyCode == 13)
            $("#txtSearch").KeyPress();
    });

    $("#txtSearch").change(function() {

        something = $(this).val()
        //log(something);
    });
});

    function getUrlEncodedKey(key, query) {
    if (!query)
        query = window.location.search;
    var re = new RegExp("[?|&]" + key + "=(.*?)&");
    var matches = re.exec(query + "&");
    if (!matches || matches.length < 2)
        return "";
    return decodeURIComponent(matches[1].replace("+", " "));
}


    function setUrlEncodedKey(key, value, query) {

    query = query || window.location.search;
    var q = query + "&";
    var re = new RegExp("[?|&]" + key + "=.*?&");
    if (!re.test(q))
        q += key + "=" + encodeURI(value);
    else
        q = q.replace(re, "&" + key + "=" + encodeURIComponent(value) + "&");
    q = q.trimStart("&").trimEnd("&");
    return q[0] == "?" ? q : q = "?" + q;
}

String.prototype.trimEnd = function(c) {
    if (c)
        return this.replace(new RegExp(c.escapeRegExp() + "*$"), '');
    return this.replace(/\s+$/, '');
}
String.prototype.trimStart = function(c) {
    if (c)
        return this.replace(new RegExp("^" + c.escapeRegExp() + "*"), '');
    return this.replace(/^\s+/, '');
}

String.prototype.escapeRegExp = function() {
    return this.replace(/[.*+?^${}()|[\]\/\\]/g, "\\$0");
};


    function log(writeThis)
    {
        console.log(writeThis);
    }


    function submitFilters() {
        var $inputFld = $filterColumn.contents().find("select"), searchURL = "", prevName, $this, _name, _value;
        var paramsObj = function() { //object to store the parameters
            this.name = [];
            this.value = [];
            this.addName = function(val) { //name setter
                this.name.push(val);
            }
            this.addValue = function(val) { //value setter
                this.value.push(val);
            }
        }
    }

    function setText(itemId) 
    {


//        oTextarea = oForm.elements["txtSearch"];
  //      textarea_val = oTextarea.value;
        
        
//        document.location = "Brands.aspx" + "&stext=" + oTextarea;
    } 
    
    
    function querySt(ji) {
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    
    for (i=0;i<gy.length;i++) {
        ft = gy[i].split("=");
        if (ft[0] == ji) {
            return ft[1];
    }
    }
    }
