﻿var templateNames = "";
var strcartItems = "";
jQuery(document).ready(function($){

    $.fn.sendSelectedItems = function(itemsNumbers,itemTemplates ){
        //calling AJAX.NET Pro functions from code behind
        CTi.Modules.CTi_AJAXMethods.SetSession("ATCCNumList_prop", itemsNumbers);
        CTi.Modules.CTi_AJAXMethods.SetSession("TemplateList_prop", itemTemplates);
        
        return this;
    };
    $.fn.SetSessionVariable = function(name, value){
        CTi.Modules.CTi_AJAXMethods.SetSession(name, value);
        return this;
    };
    $.fn.Say = function(sayWhat){
        alert(sayWhat);
        return this;
    };
    
    $.fn.fixGridView = function() {
        if ($(this).is('table') && this.find('thead').length == 0) {
            // No 'thead' section
            this.prepend("<thead></thead>"); // add thead
            this.find('tbody tr:first').remove().appendTo( this.find('thead') ); 
            // remove first row from tbody, add it to thead
        }
        return this;
    };
    
    $.fn.setEmailResults = function(){
        //CTi.Modules.CTi_AJAXMethods.SetEmailSessionATCC();
        return this;
    }
    $.fn.openWindow = function(link, winHeight, winWidth){
       var winArgs = "menubar=yes,width=" + winWidth + ",height=" + winHeight;
       window.open(link, "", winArgs);
    }
        
    $.fn.doPostBack = function(){
        //jQuery doing a postback to the server. 
        $("#__EVENTTARGET").val($(this).attr("name"));
        $("#__EVENTARGUMENT").val("");
            
        $("form")[0].submit();
        
        return this;
    };
    
   
    
    /*$("input[id*='cbRow']:disabled").each(function() {
            var link = $(this).parent().next();
            var cell = $(this).parent().parent()
            $("<span>" + link.text() + "</span>").appendTo(cell);
            link.hide();
    });*/
    

    
    $("table[id*='gv_']")
    .fixGridView()
    .tablesorter({widgets:['zebra'],
                  sortList: [[0,0]],
                  textExtraction: "complex"})
                  //headers: {0:{sorter:'BEIRNumberNR'}},textExtraction: "complex"})
    .each(function(){
        var tableName = $(this).attr("CollectionName");
        var pager = $("div[id*='pager_" + (tableName) + "']");
        $(this).tablesorterPager({container: $(pager), 
        positionFixed: false,
        size: 20}); 
    });
    
    $("#btnClearSelection").click(function(){
        $("input[id*='cbRow']:checked").each(function() {
           this.checked = false; 
        });
    }); 
    
    $("#btnANSClearSelection").click(function(){
        $("input[id*='cbRow']:checked").each(function() {
           this.checked = false; 
        });
    }); 
    
    $(".ddlRoles").change(function(){
        
        $(this).SetSessionVariable("FilterLevel", $(this).val());
    });
    
    
    $("#btnSelectAll").click(function(){
        $("input[id*='cbRow']:enabled").each(function() {
           this.checked = true; 
        });
    });  
    
    $("#btnANSSelectAll").click(function(){
        $("input[id*='cbRow']:enabled").each(function() {
           this.checked = true; 
        });
    }); 
    
    $("#btnEmailResults").click(function(){
        var link = "SendEmail/tabid/724/Default.aspx"; 
        $(this).setEmailResults().openWindow(link, 350, 750);
    });
    
    $("#btnANSEmailResults").click(function(){
        var link = "SendEmail/tabid/724/Default.aspx"; 
        $(this).setEmailResults().openWindow(link, 350, 750);
    });
    
    $("input[id*='btnViewSelection']").click(function(){
        $(".cbatccnum > input:checked").each(function(){
            var link = $(this).parent().next();
            if ($(link).attr("href") != null){
                strcartItems += $(this).parent().next().text() + ",";
                templateNames += $(this).parent().attr("template") + ",";
            }
        });
        
        if (strcartItems != ""){
            
            $(this)
            .sendSelectedItems( strcartItems, templateNames)  
            //.doPostBack();   
        }else{
            alert("Select at least one BEI Number to view details.");
            return false;                
        }
    
    });
    
    $("input[id*='btnANSViewSelection']").click(function(){
        $(".cbatccnum > input:checked").each(function(){            
            if ($(this).parent().attr("href") == 'true'){                
                strcartItems += $(this).parent().attr("atccNumber") + ",";                
                templateNames += $(this).parent().attr("template") + ",";
            }
        });
        
        if (strcartItems != ""){
            
            $(this)
            .sendSelectedItems( strcartItems, templateNames)  
            //.doPostBack();   
        }else{
            alert("Select at least one BEI Number to view details.");
            return false;                
        }
    
    });
    
    $("input[id*='btnViewAll']").click(function(){
        $(".cbatccnum > input:enabled").each(function(){
          strcartItems += $(this).parent().next().text() + ",";
          templateNames += $(this).parent().attr("template") + ",";
        });
        
        if (strcartItems.length == 0)
        {
            alert("You do not have access to view these items.");
            return false;
        }
        
        $(this)
        .sendSelectedItems( strcartItems, templateNames)  
       // .doPostBack();  
    });
    
    $("input[id*='btnANSViewAll']").click(function(){
        $(".cbatccnum > input:enabled").each(function(){
          strcartItems += $(this).parent().attr("atccNumber") + ",";                
          templateNames += $(this).parent().attr("template") + ",";
        });
        
        if (strcartItems.length == 0)
        {
            alert("You do not have access to view these items.");
            return false;
        }
        
        $(this)
        .sendSelectedItems( strcartItems, templateNames)  
       // .doPostBack();  
    });
    
    $("input[id*='btnAddCart']").click(function(){
         $(".cbatccnum > input:checked").each(function(){
            var link = $(this).parent().next();
            if ($(link).attr("href") != null){
                strcartItems += $(this).parent().next().text() + ",";
                templateNames += $(this).parent().attr("template") + ",";
            }
        });
        
        if (strcartItems != ""){
            
            $(this)
            .sendSelectedItems( strcartItems, templateNames)  
            //.doPostBack();   
        }else{
            alert("Select at least one BEI Number to add to cart.");
            return false;                
        }
//        $(this)
//        .sendSelectedItems( strcartItems, templateNames)  
        //.doPostBack(); 
    });
    
    $("input[id*='btnANSAddCart']").click(function(){
         $(".cbatccnum > input:checked").each(function(){
            if ($(this).parent().attr("href") == 'true'){                
                strcartItems += $(this).parent().attr("atccNumber") + ",";                
                templateNames += $(this).parent().attr("template") + ",";
            }
        });
        
        if (strcartItems != ""){
            $(this)
            .sendSelectedItems( strcartItems, templateNames)  
        }else{
            alert("Select at least one BEI Number to add to cart.");
            return false;                
        }
    });
    
    $("input[id*='btnExportToExcel']").click(function(){
       // $(this).doPostBack();
    });
    
   $("input[id*='btnSaveSearch']").click(function(){
        //$(this).doPostBack();
    });
    
    //$("input[id*='btnRefineSearch']").click(function(){
        ///var Refine_Search = $("input[id*='tbRefineSearch']").val();
        //$(this).SetSessionVariable("Refine_Search", Refine_Search).doPostBack();
    //});
    
    $("input[id*='btnEmailResults']").click(function(){
        //$(this).doPostBack();
    });
    
    $("input[id*='btnANSEmailResults']").click(function(){
        //$(this).doPostBack();
    });
    
      $("input[id*='btnDeleteSelections']").click(function(){
        $(".cbatccnum > input").each(function(){
            if(this.checked)
            {
                strcartItems += $(this).parent().next().text() + ",";
                templateNames += $(this).parent().attr("template") + ",";
            }
        });
        
        if (strcartItems != ""){
            
            $(this)
            .sendSelectedItems( strcartItems, templateNames)  
            //.doPostBack();   
        }else{
            alert("Select at least one BEI Number to delete.");
            return false;                
        }
    
    });
    
    $("img[id*='imgbtnOptionSearch']").toggle(
        function(){
            $("#dvHelp").show();
        },
        function(){
            $("#dvHelp").hide();
        }
    );
    $("img[id*='OptionSearch_field']").toggle(
        function(){
            $("#Help_field").show();
        },
        function(){
            $("#Help_field").hide();
        }
    );
    
    $("input[id*='btnAddtoPrefPod']").click(function(){

        $(".cbatccnum > input").each(function(){
            if(this.checked)
            {
                strcartItems += $(this).parent().next().text() + ",";
                templateNames += $(this).parent().attr("template") + ",";
                
            }
        });
        
        if (strcartItems != ""){
            
            $(this)
            .sendSelectedItems( strcartItems, templateNames)  
            //.doPostBack();   
        }else{
            alert("Select at least one BEI Number to add to the Preferred Product List.");
            return false;                
        }
    
    });
    
    $("input[id*='btnANSAddtoPrefPod']").click(function(){

        $(".cbatccnum > input").each(function(){
            if(this.checked)
            {
                strcartItems += $(this).parent().attr("atccNumber") + ",";                
                templateNames += $(this).parent().attr("template") + ",";                
            }
        });
        
        if (strcartItems != ""){            
            $(this)
            .sendSelectedItems( strcartItems, templateNames)  
            //.doPostBack();   
        }else{
            alert("Select at least one BEI Number to add to the Preferred Product List.");
            return false;                
        }
    
    });
});