    var currentPage = 1;
    var currentCategory = 0;
    var currentSort = "";
    var currentSortOrder = "ASC";
    var maxPage;
    var defaultTableData;
    var option;
    
    
    var startimages = ''   
    
  

    function $(id) {

        return document.getElementById(id);
    }

    function changePage(p) {
        currentPage += p;
        if (option=='category') {
           getRecords();
        } else if (option=='search') {
           findproduct()
        }
    }

    function changePage2(p) {
        currentPage = p;
        if (option=='category') {
           getRecords();
        } else if (option=='search') {
           findproduct()
        }
    }
            
    function changecategory(c) {
        currentPage = 1;
        currentCategory = c;
        getRecords();
    }
    
    function parseRecords(xml) {
        with (xml.getElementsByTagName("records").item(0)) {
            page = getAttribute("page")*1;
            maxPerPage = getAttribute("max_per_page");
            startId = maxPerPage*page+1;
            total = getAttribute("total")*1;
            maxPage = Math.ceil(total/maxPerPage);
            imgwidth = getAttribute("img_width");
            imgheight = getAttribute("img_height");
            //$("dataStats").innerHTML = '<h2>' + getAttribute("category") + '</h2>';
        }
        //$("btnPrev").style.visibility = page == 1 ? "hidden" : "visible";
        //$("btnNext").style.visibility = page >= maxPage ? "hidden" : "visible";

        var n='';
        if (maxPage>1) {
           var i=0;
           for (i=1;i<=maxPage;i++)
           {
             if (i==currentPage) {
                n = n + '<strong> ' + i + ' </strong>'; 
             } else {
                n = n + '<a href="javascript:changePage2('+i+')"> ' + i + ' </a>'; 
             }
           }
        }
        //$("navigator").innerHTML=n;
        record = xml.getElementsByTagName("record");
        result = "";
        if (record.length >0) {
           x = document.createElement("div");
           t = document.createElement("table");
           t.setAttribute('cellpadding', '10');
           t.setAttribute('cellspacing', '0');
           t.setAttribute('width', '100%');
           //for (i = d.rows.length-1; i >= 0; i--)
           //  d.deleteRow(i);
           cols=1;
           i=0;
           while (i < record.length) {
               tr = document.createElement("tr");
               tr.setAttribute('class', 'productlist');
               c=0;
               while (c<cols & i < record.length) {
                   td = document.createElement("td");
                   td.setAttribute('align', 'left');
                   td.setAttribute('class', 'productlist');
                   td.setAttribute('width', imgwidth);
                   td.innerHTML = '<a href="javascript:PopUp(\'product.php?id='+ record[i].childNodes[0].childNodes[0].nodeValue +'\')"><img src="images/products/thumbs/'+ record[i].childNodes[2].childNodes[0].nodeValue + '/' + record[i].childNodes[1].childNodes[0].nodeValue + '" width="'+imgwidth+'" border="0"></a>';
                   tr.appendChild(td);
                   
                   td = document.createElement("td");
                   td.setAttribute('valign', 'top');
                   td.setAttribute('class', 'productlist');
                   td.innerHTML = '<br><a href="javascript:PopUp(\'product.php?id='+ record[i].childNodes[0].childNodes[0].nodeValue +'\')">'+
                                  '<strong>'+ record[i].childNodes[2].childNodes[0].nodeValue +'</strong><br>' +
                                  '<br>'+ record[i].childNodes[4].childNodes[0].nodeValue +'<br>';
                   tr.appendChild(td);

                   c = c +1;
                   i = i +1;
               }
               t.appendChild(tr);
           }
           x.appendChild(t);
           d = $("contentcategory");
           d.innerHTML = d.innerHTML + x.innerHTML;

        } else {
           //$("contentcategory").innerHTML='';
        }
        return result;

    }

    function getRecords() {
        option = 'category';
        $("dataStats").innerHTML = 'Loading...';
        advAJAX.get({
            url: "namecategory.php?cat="+currentCategory,
                onSuccess : function(obj) {
                    m = $("titlecategory");
                    m.innerHTML=obj.responseText;
            },
            onError : function(obj) {
                alert("BLAD");
                alert("Error: " + obj.status);
            }
        });

        advAJAX.get({
            url: "contentcategory.php?cat="+currentCategory,
                onSuccess : function(obj) {
                    m = $("contentcategory");
                    m.innerHTML=obj.responseText;
            },
            onError : function(obj) {
                alert("BLAD");
                alert("Error: " + obj.status);
            }
        });
        advAJAX.get({
            url : "products_list.php?cat="+currentCategory+"&page="+currentPage,
            onSuccess : function(obj) {
                parseRecords(obj.responseXML);
            },
            onError : function(obj) {
                alert("Error: " + obj.status);
            },
            onFinalization : function(obj) {
            }
        });
        $("dataStats").innerHTML = '';
    }
    
    function findproduct() {
        option = 'search';
        var e=document.getElementById('search');
        advAJAX.get({
            url : "search.php?s="+e.value+"&page="+currentPage,
            onInitialization : function() {
               $("dataStats").innerHTML = '<span style="font-weight: bold">Loading...</span>';
            },
            onSuccess : function(obj) {
                parseRecords(obj.responseXML);
            },
            onError : function(obj) {
                alert("Error: " + obj.status);
            },
            onFinalization : function(obj) {
            }
        });
    }
    

    function PopUp(url) {
        window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no')
    }
    
