
//ajax request object
var ajaxRequest ;  

jQuery(document).ready(function($){

    //no cache allow on ajax calls
    $.ajaxSetup({ cache: false });

    var sPath = window.location.pathname;
    var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);

    if (sPage=="index.php" ||sPage=="" )
        {
        //load default section
        var lnkDefault = "pages/search/search.php";
        loadPage(lnkDefault,false,false);
    }

    //****AJAX requests
    //change pages
    $("#btnBackToSearchPage").click(function(){
        var link="pages/search/search.php";
        loadPage(link,false,true);

        return false;
    });

    $("#btnPageEdit").click(function() {

        var link ="pages/edit/editanalytekit.php";
        loadPage(link,true,true);
        return false;
    });

    $("#btnViewCart").live('click', function(){

        var link ="pages/cart/summary.php";
        loadPage(link,true,true);
        return false;
    });

    $("#btnPageImport").click(function() {
        var link ="pages/import/import.php";
        loadPage(link,true,true);
        return false;
    });

    $("#btnPageUsers").click(function() {

        var link ="pages/users/editusers.php";
        loadPage(link,true,true);
        return false;
    });

    $("#btnPageExport").click(function() {
        var link ="pages/export/export.php";
        loadPage(link,true,true);
        return false;
    });

    $("#btnViewArchive").click(function() {
        var link ="pages/archive/archive.php";
        loadPage(link,true,true);
        return false;
    });

    $("#btnSettings").click(function() {
        var link ="pages/settings/settings.php";
        loadPage(link,true,true);
        return false;
    });

    $( "button, input:submit, input:button, a.pbutton, .pbutton").button();
    $(".lbpModal").colorbox({innerWidth:"80%",innerHeight:"80%",opacity:0.8,iframe:true});

    //end close
});

function loadPage(url, displayBackLink, bEffect)
{
    $.get(url, function(data){

        //hide
        $("#main-full").hide();
        //clear
        $("#main-full").html("");
        //load page
        $("#main-full").html(data);

        if (bEffect)
            {
            //fade
            $("#main-full").fadeIn();
        }
        else
            {
            //normal show
            $("#main-full").show();
        }

    }, "html");

    //display back to search or no
    if  (displayBackLink)
        {
        $("#liBackToSearchPage").css("display","");
    }
    else
        {
        $("#liBackToSearchPage").css("display","none");
    }
}

//open window
function openPopup(link,width,height )
{
    //center
    var left = parseInt((screen.availWidth/2) - (width/2));
    var top = parseInt((screen.availHeight/2) - (height/2));
    windowFeatures = "width=" + width + ",height=" + height + ",titlebar=no,menubar=no, scrollbars=yes,status =no,toolbar=no,resizable,left=" + left + ",top=" + top + "screenX=" + left + ",screenY=" + top;
    return  window.open(link,"wnd", windowFeatures);

}
//resize window to center
function resizeToCenter(wnd,w,h )
{

    wleft = (screen.width - w) / 2;
    wtop = (screen.height - h) / 2;

    wnd.resizeTo(w,h);
    wnd.moveTo(wleft,wtop);

}


//autosuggeste function
function setAutoSuggest(select, textbox){

    var element = document.getElementById(select);
    if (element==null){return ;}
    var type = element.value;

    if (type!="" && type!="analytename" && type!="kitname")
        {
        var options = {
            script:"classes/autocomplete.php?type="+type+"&",
            varname:"input",
            json:true,
            callback: function (obj) { }
        };
        as_json = new AutoSuggest(textbox, options);
    }
    else
        {
        as_json = new AutoSuggest(textbox, null);
    }

}

//IE doesn`t know about indexOf so I`ll teach it
if(!Array.indexOf){
    Array.prototype.indexOf = function(obj){
        for(var i=0; i<this.length; i++){
            if(this[i]==obj){
                return i;
            }
        }
        return -1;
    }
}

//trim prototype
String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
}

//array contains function
Array.prototype.contains = function(obj) {
    var i = this.length;
    while (i--) {
        if (this[i] === obj) {
            return true;
        }
    }
    return false;
}

//determine if elemnet has a scrollbar or not
function has_scrollbar(elem_id)
{
    elem = document.getElementById(elem_id);
    if (elem.clientHeight < elem.scrollHeight)
        return true;
    else
        return false;
}

//replace empty value with "-"
function replaceEmtyCell(val)
{
    return (val=="")?"-":val;
}




