﻿jQuery.noConflict();
jQuery().ready(function() {

    bindRequests();
    function bindRequests() {

        jQuery(".remove-request").bind("click", removeSample);
        jQuery(".remove-quote").bind("click", removeQuote);
    }

    function unbindRequests() {

        jQuery(".remove-request").unbind();
        jQuery(".remove-quote").unbind();
    }

    /* return only the NUM portion of an ID */
    function extractID(theID) {
        return (theID.split("-")[1]);
    }

    function removeSample(e) {

        unbindRequests();

        var productId = jQuery.trim(jQuery(this).parent().closest("tr").find(".part-number a").text());
        var typeId = '2';

        var span = this;
        jQuery.get('product-remove-request.aspx', { 'partNumber': productId, 'type': typeId, 'cacheKey': Math.random() * Math.random() }, function(data) {
            //the results from this call should be a JSON object.  This object needs to be evaluated
            var retObj = eval("(" + data + ")");
            if (retObj.success) {

                //if it is the last one then hide these guys
                if (retObj.count == 0) {
                    //if the count is back to 0 then we need to redirect back to self.
                    window.location = "product-request.aspx";
                }
                jQuery('#parts-request-header .part-requests-submit').html('Process Part Requests (' + retObj.count + ')');
                jQuery(span).parent().closest("tr").remove();
            }
        });

        bindRequests();
        zebraStripe();
    }

    function removeQuote(e) {
        unbindRequests();

        var productId = jQuery.trim(jQuery(this).parent().closest("tr").find(".part-number a").text());

        var typeId = '1';
        var span = this;
        jQuery.get('product-remove-request.aspx', { 'partNumber': productId, 'type': typeId, 'cacheKey': Math.random() * Math.random() }, function(data) {
            //the results from this call should be a JSON object.  This object needs to be evaluated   
            var retObj = eval("(" + data + ")");
            if (retObj.success) {

                if (retObj.count == 0) {
                    //if the count is back to 0 then we need to redirect back to self.
                    window.location = "product-request.aspx";
                }
                jQuery('#parts-request-header .part-requests-submit').html('Process Part Requests (' + retObj.count + ')');
                jQuery(span).parent().closest("tr").remove();
            }
        });

        bindRequests();
        zebraStripe();
    }

    function zebraStripe() {
        jQuery("tr").removeClass("alt");
        jQuery("tr:even").addClass("alt");
    }


}
);
