$(document).ready(function () {
    
    /*
    * On click of search button
    * To set the searchForm hidden fields from the current's object class parameter 
    * and submit the form 
    * @param action the action    
    */
    $("#searchButton").click(function()
    {
        var params = splitParams($(this).attr("class"));

        $("#action").val(getParam(params, 1));
        $("#searchCriteria\\.searchAction").val("");
        $("#searchCriteria\\.pageNo").val("0");
        $("#searchCriteria\\.sortAction").val("false");
        //$("#searchCriteria\\.sortCol").val("");
        //$("#searchCriteria\\.sortOrder").val("");
        $("#searchForm").submit();
        return false;
    });
    
    /*
    * On click of export button
    * To set the searchForm hidden fields from the current's object class parameter 
    * and submit the form
    * 
    * Set the form's target to _export and action to export and reset it back to empty 
    * and search respectively  
    * @param action the export action
    */
    $("#exportButton").click(function()
    {
        var params = splitParams($(this).attr("class"));

        document.getElementById("searchForm").reset();
        $("#action").val(getParam(params, 1));
        //$("#searchForm").attr("target", "_export");
        $("#searchForm").submit();
        $("#action").val(getParam(params, 2));
        //$("#searchForm").attr("target", "");
        
        removeErrors();
    });
    
    /*
     * On click of delete button
     * To set the searchForm hidden fields from the current's object class parameter 
     * and submit the form
     * 
     * @param action the delete action
     */
     $("#deleteButton").click(function()
     {
         var params = splitParams($(this).attr("class"));

         $("#action").val(getParam(params, 1));
         $("#searchForm").submit();
         
         removeErrors();
     });
      
   /*
    * On click of generic reset link 
    * set the form fields to blank 
    */
    $("#resetLink").click(function()
    {
        var parentForm = $(this).parents("form");        
        $(parentForm).find("input[type='checkbox']").each(function(){
            this.checked = false;
        });
        
        $(parentForm).find("select").each(function(){
            if ($(this).attr("multiple") != undefined && $(this).attr("multiple") == true)
            {
                $(this).find("option").each(function(){
                    $(this).attr("selected", false);
                });
            }
            else
            {
                $("option:first", this).attr("selected", true);
            }

            if($(this).hasClass("emptyOnReset")) {
                $(this).find("option").each(function(){
                    if($(this).val() != "") {
                        $(this).remove();
                    }
                });
            }
        });
        
        $(parentForm).find("input[type='text']").each(function(){
            $(this).val("");
        });
        
        $("#searchCriteria\\.pageNo").val("0");
        
        removeErrors();
    });

    /*
    * On click of pagination links
    * To set the searchForm hidden fields from the current's object class parameter 
    * and submit the form
    * @param action the action 
    * @param pageNo the searchCriteria.pageNo    
    */
    $("._paginationLink").click(function()
    {
        var params = splitParams($(this).attr("class"));
                
        $("#searchCriteria\\.pageNo").val(getParam(params, 1));
        if (getParam(params, 2) != "")
        {
            $("#action").val(getParam(params, 2));
        }
        else
        {
            $("#action").val("search");
        }
        $("#searchForm").submit();
        return false;
    });
    
    $("._paginationSelect").change(function()
    {
		$("#searchCriteria\\.pageNo").val($(this).val());
        $("#searchForm").submit();
        return false;
    });
    
    /*
    * On click of sort columns link
    * To set the searchForm hidden fields from the current's object class parameter 
    * and submit the form 
    * @param column the column to be sorted    
    */
    $("._sortLink").click(function()
    {
        var params = splitParams($(this).attr("class"));
        
        if ($("#searchCriteria\\.sortCol").val() != getParam(params, 1))
        {
            $("#searchCriteria\\.sortOrder").val("asc");
        }
        else
        {
            if ($("#searchCriteria\\.sortOrder").val() == "asc")
            {            
                $("#searchCriteria\\.sortOrder").val("desc");
            }
            else
            {
                $("#searchCriteria\\.sortOrder").val("asc");
            }
        }
        
        $("#searchCriteria\\.sortCol").val(getParam(params, 1));
        
        if (getParam(params, 2) != "")
        {
            $("#action").val(getParam(params, 2));
        }
        else
        {
            $("#action").val("search");
        }
        $("#searchCriteria\\.pageNo").val("0");
        $("#searchCriteria\\.sortAction").val("true");
        $("#searchForm").submit();
        return false;
    });
});


/*
 * On click of delete link
 * @param action the delete action
 */
function deleteRow(action, entityField, primaryKey, warningMsg)
{
	var confirmed = true;
	if (warningMsg.length != 0)
	{
		confirmed = confirm(warningMsg);
	}
	
	if (confirmed)
	{
		$("#action").val(action);
		$("#" + entityField).val(primaryKey);
		$("#searchForm").submit();
	  
		removeErrors();
	}
}

