﻿//declaring the class
var valveFinder = Class.create();

//defining the rest of the class implementation
valveFinder.prototype = {
    initialize: function() {
            
        // Create hash table of valve data
         this.Valves = {
          Products: [
            {size: '1/4', value: 0.25, coefficient: 1.2, name: 'TYPE A SERIES', seriessize: 'Pipe Size', material: 'Bronze', liquid: 1200, gas: 400, steam: 200, price:3, url: 'products_bronze.htm?frame=frames/products_bronze_A.htm', type: 'standard' },
            {size: '3/8', value: 0.375, coefficient: 2.0, name: 'TYPE A SERIES', seriessize: 'Pipe Size', material: 'Bronze', liquid: 1200, gas: 400, steam: 200, price:3, url: 'products_bronze.htm?frame=frames/products_bronze_A.htm', type: 'standard' },
            
            {size: '1/4', value: 0.25, coefficient: 1.9, name: 'TYPE B SERIES', seriessize: 'Pipe Size', material: 'Bronze', liquid: 1200, gas: 400, steam: 200, price:4, url: 'products_bronze.htm?frame=frames/products_bronze_B.htm', type: 'standard' },
            {size: '3/8', value: 0.375, coefficient: 2.8, name: 'TYPE B SERIES', seriessize: 'Pipe Size', material: 'Bronze', liquid:  1200, gas: 400, steam: 200, price:4, url: 'products_bronze.htm?frame=frames/products_bronze_B.htm', type: 'standard' },
            {size: '1/2', value: 0.5, coefficient: 3.3, name: 'TYPE B SERIES', seriessize: 'Pipe Size', material: 'Bronze', liquid: 1200, gas: 400, steam: 200, price:4, url: 'products_bronze.htm?frame=frames/products_bronze_B.htm', type: 'standard' },
            
            {size: '3/16', value: 0.1875, coefficient: 0.7, name: 'TYPE B3 SERIES: 1/4&quot; or 3/8&quot; NPT', seriessize: 'Orifice Size', material: 'Bronze', liquid: 200, gas: 200, steam: 200, price:4, url: 'products_bronze.htm?frame=frames/products_bronze_B3.htm', type: 'direct-acting' },
	        
	        {size: '3/4', value: 0.75, coefficient: 6.3, name: 'TYPE D SERIES', seriessize: 'Pipe Size', material: 'Bronze', liquid: 1200, gas: 400, steam: 200, price:5, url: 'products_bronze.htm?frame=frames/products_bronze_D.htm', type: 'standard' },
            {size: '1', value: 1.0, coefficient: 11.5, name: 'TYPE D SERIES', seriessize: 'Pipe Size', material: 'Bronze', liquid: 1200, gas: 400, steam: 200, price:5, url: 'products_bronze.htm?frame=frames/products_bronze_D.htm', type: 'standard' },
            {size: '1-1/4', value: 1.25, coefficient: 18.0, name: 'TYPE D SERIES', seriessize: 'Pipe Size', material: 'Bronze', liquid: 1200, gas: 400, steam: 200, price:5, url: 'products_bronze.htm?frame=frames/products_bronze_D.htm', type: 'standard' },
            {size: '1-1/2', value: 1.5, coefficient: 28.0, name: 'TYPE D SERIES', seriessize: 'Pipe Size', material: 'Bronze', liquid: 1200, gas: 400, steam: 200, price:5, url: 'products_bronze.htm?frame=frames/products_bronze_D.htm', type: 'standard' },
	        {size: '2', value: 2.0, coefficient: 50.0, name: 'TYPE D SERIES', seriessize: 'Pipe Size', material: 'Bronze', liquid: 1200 , gas: 400, steam: 200, price:5, url: 'products_bronze.htm?frame=frames/products_bronze_D.htm', type: 'standard' },
	        
	        {size: '1/4', value: 0.25, coefficient: 1.2, name: 'TYPE F SERIES', seriessize: 'Orifice Size', material: 'Bronze', liquid: 1000, gas: 1000, steam: 200, price:0, url: 'products_directacting.htm?frame=frames/products_directacting_F.htm', type: 'direct-acting' },
            {size: '7/32', value: 0.21875, coefficient: 1.0, name: 'TYPE F SERIES', seriessize: 'Orifice Size', material: 'Bronze', liquid: 1000, gas: 1000, steam: 200, price:0, url: 'products_directacting.htm?frame=frames/products_directacting_F.htm', type: 'direct-acting' },
            {size: '3/16', value: 0.1875, coefficient: 0.7, name: 'TYPE F SERIES', seriessize: 'Orifice Size', material: 'Bronze', liquid: 1000, gas: 1000, steam: 200, price:0, url: 'products_directacting.htm?frame=frames/products_directacting_F.htm', type: 'direct-acting' },
            {size: '5/32', value: 0.15625, coefficient: 0.5, name: 'TYPE F SERIES', seriessize: 'Orifice Size', material: 'Bronze', liquid: 1000, gas: 1000, steam: 200, price:0, url: 'products_directacting.htm?frame=frames/products_directacting_F.htm', type: 'direct-acting' },
            {size: '1/8', value: 0.125, coefficient: 0.3, name: 'TYPE F SERIES', seriessize: 'Orifice Size', material: 'Bronze', liquid: 1000, gas: 1000, steam: 200, price:0, url: 'products_directacting.htm?frame=frames/products_directacting_F.htm', type: 'direct-acting' },
            {size: '3/32', value: 0.09375, coefficient: 0.18, name: 'TYPE F SERIES', seriessize: 'Orifice Size', material: 'Bronze', liquid: 1000, gas: 1000, steam: 200, price:0, url: 'products_directacting.htm?frame=frames/products_directacting_F.htm', type: 'direct-acting' },
            {size: '1/16', value: 0.0625, coefficient: 0.08, name: 'TYPE F SERIES', seriessize: 'Orifice Size', material: 'Bronze', liquid: 1000, gas: 1000, steam: 200, price:0, url: 'products_directacting.htm?frame=frames/products_directacting_F.htm', type: 'direct-acting' },
            {size: '3/64', value: 0.046875, coefficient: 0.03, name: 'TYPE F SERIES', seriessize: 'Orifice Size', material: 'Bronze', liquid: 1000, gas: 1000, steam: 200, price:0, url: 'products_directacting.htm?frame=frames/products_directacting_F.htm', type: 'direct-acting' },
            
            {size: '1/4', value: 0.25, coefficient: 1.2, name: 'TYPE G SERIES', seriessize: 'Orifice Size', material: 'Stainless Steel', liquid: 1000, gas: 1000, steam: 200, price:0, url: 'products_directacting.htm?frame=frames/products_directacting_G.htm', type: 'direct-acting' },
            {size: '7/32', value: 0.21875, coefficient: 1.0, name: 'TYPE G SERIES', seriessize: 'Orifice Size', material: 'Stainless Steel', liquid: 1000, gas: 1000, steam: 200, price:0, url: 'products_directacting.htm?frame=frames/products_directacting_G.htm', type: 'direct-acting' },
            {size: '3/16', value: 0.1875, coefficient: 0.7, name: 'TYPE G SERIES', seriessize: 'Orifice Size', material: 'Stainless Steel', liquid: 1000, gas: 1000, steam: 200, price:0, url: 'products_directacting.htm?frame=frames/products_directacting_G.htm', type: 'direct-acting' },
            {size: '5/32', value: 0.15625, coefficient: 0.5, name: 'TYPE G SERIES', seriessize: 'Orifice Size', material: 'Stainless Steel', liquid: 1000, gas: 1000, steam: 200, price:0, url: 'products_directacting.htm?frame=frames/products_directacting_G.htm', type: 'direct-acting' },
            {size: '1/8', value: 0.125, coefficient: 0.3, name: 'TYPE G SERIES', seriessize: 'Orifice Size', material: 'Stainless Steel', liquid: 1000, gas: 1000, steam: 200, price:0, url: 'products_directacting.htm?frame=frames/products_directacting_G.htm', type: 'direct-acting' },
            {size: '3/32', value: 0.09375, coefficient: 0.18, name: 'TYPE G SERIES', seriessize: 'Orifice Size', material: 'Stainless Steel', liquid: 1000, gas: 1000, steam: 200, price:0, url: 'products_directacting.htm?frame=frames/products_directacting_G.htm', type: 'direct-acting' },
            {size: '1/16', value: 0.0625, coefficient: 0.08, name: 'TYPE G SERIES', seriessize: 'Orifice Size', material: 'Stainless Steel', liquid: 1000, gas: 1000, steam: 200, price:0, url: 'products_directacting.htm?frame=frames/products_directacting_G.htm', type: 'direct-acting' },
            {size: '3/64', value: 0.046875, coefficient: 0.03, name: 'TYPE G SERIES', seriessize: 'Orifice Size', material: 'Stainless Steel', liquid: 1000, gas: 1000, steam: 200, price:0, url: 'products_directacting.htm?frame=frames/products_directacting_G.htm', type: 'direct-acting' },
            
            {size: '1/8', value: 0.125, coefficient: 1.2, name: 'TYPE K SERIES', seriessize: 'Pipe Size', material: 'Stainless Steel', liquid: 1000, gas: 400, steam: 200, price:0, url: 'products_steel.htm?frame=frames/products_steel_K.htm', type: 'standard' },
            {size: '1/4', value: 0.25, coefficient: 2.2, name: 'TYPE K SERIES', seriessize: 'Pipe Size', material: 'Stainless Steel', liquid: 1000, gas: 400, steam: 200, price:0, url: 'products_steel.htm?frame=frames/products_steel_K.htm', type: 'standard' },
            {size: '3/8', value: 0.375, coefficient: 2.9, name: 'TYPE K SERIES', seriessize: 'Pipe Size', material: 'Stainless Steel', liquid: 1000, gas: 400, steam: 200, price:0, url: 'products_steel.htm?frame=frames/products_steel_K.htm', type: 'standard' },
            {size: '1/2', value: 0.5, coefficient: 3, name: 'TYPE K SERIES', seriessize: 'Pipe Size', material: 'Stainless Steel', liquid: 1000, gas: 400, steam: 200, price:0, url: 'products_steel.htm?frame=frames/products_steel_K.htm', type: 'standard' },
            {size: '3/4', value: 0.75, coefficient: 6, name: 'TYPE K SERIES', seriessize: 'Pipe Size', material: 'Stainless Steel', liquid: 1000, gas: 400, steam: 200, price:0, url: 'products_steel.htm?frame=frames/products_steel_K.htm', type: 'standard' },
            {size: '1', value: 1.0, coefficient: 11.0, name: 'TYPE K SERIES', seriessize: 'Pipe Size', material: 'Stainless Steel', liquid: 1000, gas: 400, steam: 200, price:0, url: 'products_steel.htm?frame=frames/products_steel_K.htm', type: 'standard' },
            {size: '1-1/4', value: 1.25, coefficient: 17.0, name: 'TYPE K SERIES', seriessize: 'Pipe Size', material: 'Stainless Steel', liquid: 1000, gas: 400, steam: 200, price:0, url: 'products_steel.htm?frame=frames/products_steel_K.htm', type: 'standard' },
            {size: '1-1/2', value: 1.5, coefficient: 25.0, name: 'TYPE K SERIES', seriessize: 'Pipe Size', material: 'Stainless Steel', liquid: 1000, gas: 400, steam: 200, price:0, url: 'products_steel.htm?frame=frames/products_steel_K.htm', type: 'standard' },
            {size: '2', value: 2.0, coefficient: 45.0, name: 'TYPE K SERIES', seriessize: 'Pipe Size', material: 'Stainless Steel', liquid: 1000, gas: 400, steam: 200, price:0, url: 'products_steel.htm?frame=frames/products_steel_K.htm', type: 'standard' },
            
            {size: '1/8', value: 0.125, coefficient: 1.2, name: 'TYPE M SERIES', seriessize: 'Pipe Size', material: 'Bronze', liquid: 250, gas: 250, steam: 125, price:1, url: 'products_bronze.htm?frame=frames/products_bronze_M.htm', type: 'standard' },
            {size: '1/4', value: 0.25, coefficient: 2.2, name: 'TYPE M SERIES', seriessize: 'Pipe Size', material: 'Bronze', liquid: 250, gas: 250, steam: 125, price:1, url: 'products_bronze.htm?frame=frames/products_bronze_M.htm', type: 'standard' },
            {size: '3/8', value: 0.375, coefficient: 2.5, name: 'TYPE M SERIES', seriessize: 'Pipe Size', material: 'Bronze', liquid: 250, gas: 250, steam: 125, price:1, url: 'products_bronze.htm?frame=frames/products_bronze_M.htm', type: 'standard' },
            {size: '1/2', value: 0.5, coefficient: 3.6, name: 'TYPE M SERIES', seriessize: 'Pipe Size', material: 'Bronze', liquid: 250, gas: 250, steam: 125, price:1, url: 'products_bronze.htm?frame=frames/products_bronze_M.htm', type: 'standard' },
            {size: '3/4', value: 0.75, coefficient: 6.8, name: 'TYPE M SERIES', seriessize: 'Pipe Size', material: 'Bronze', liquid: 250, gas: 250, steam: 125, price:1, url: 'products_bronze.htm?frame=frames/products_bronze_M.htm', type: 'standard' },
            {size: '1', value: 1.0, coefficient: 11.5, name: 'TYPE M SERIES', seriessize: 'Pipe Size', material: 'Bronze', liquid: 250, gas: 250, steam: 125, price:1, url: 'products_bronze.htm?frame=frames/products_bronze_M.htm', type: 'standard' },
            {size: '1-1/4', value: 1.25, coefficient: 18.0, name: 'TYPE M SERIES', seriessize: 'Pipe Size', material: 'Bronze', liquid: 250, gas: 250, steam: 125, price:1, url: 'products_bronze.htm?frame=frames/products_bronze_M.htm', type: 'standard' },
            {size: '1-1/2', value: 1.5, coefficient: 26.0, name: 'TYPE M SERIES', seriessize: 'Pipe Size', material: 'Bronze', liquid: 250, gas: 250, steam: 125, price:1, url: 'products_bronze.htm?frame=frames/products_bronze_M.htm', type: 'standard' },
            {size: '2', value: 2.0, coefficient: 48.0, name: 'TYPE M SERIES', seriessize: 'Pipe Size', material: 'Bronze', liquid: 250, gas: 250, steam: 125, price:1, url: 'products_bronze.htm?frame=frames/products_bronze_M.htm', type: 'standard' },
            
            {size: '1/8', value: 0.125, coefficient: 1.2, name: 'TYPE Q SERIES', seriessize: 'Pipe Size', material: 'Bronze', liquid: 400, gas: 400, steam: 200, price:2, url: 'products_bronze.htm?frame=frames/products_bronze_Q.htm', type: 'standard' },
            {size: '1/4', value: 0.25, coefficient: 2.2, name: 'TYPE Q SERIES', seriessize: 'Pipe Size', material: 'Bronze', liquid: 400, gas: 400, steam: 200, price:2, url: 'products_bronze.htm?frame=frames/products_bronze_Q.htm', type: 'standard' },
	        {size: '3/8', value: 0.375, coefficient: 2.5, name: 'TYPE Q SERIES', seriessize: 'Pipe Size', material: 'Bronze', liquid: 400, gas: 400, steam: 200, price:2, url: 'products_bronze.htm?frame=frames/products_bronze_Q.htm', type: 'standard' },
            {size: '1/2', value: 0.5, coefficient: 3.6, name: 'TYPE Q SERIES', seriessize: 'Pipe Size', material: 'Bronze', liquid: 400, gas: 400, steam: 200, price:2, url: 'products_bronze.htm?frame=frames/products_bronze_Q.htm', type: 'standard' },
            {size: '3/4', value: 0.75, coefficient: 6.8, name: 'TYPE Q SERIES', seriessize: 'Pipe Size', material: 'Bronze', liquid: 400, gas: 400, steam: 200, price:2, url: 'products_bronze.htm?frame=frames/products_bronze_Q.htm', type: 'standard' },
            {size: '1', value: 1.0, coefficient: 11.5, name: 'TYPE Q SERIES', seriessize: 'Pipe Size', material: 'Bronze', liquid: 400, gas: 400, steam: 200, price:2, url: 'products_bronze.htm?frame=frames/products_bronze_Q.htm', type: 'standard' },
            {size: '1-1/4', value: 1.25, coefficient: 18.0, name: 'TYPE Q SERIES', seriessize: 'Pipe Size', material: 'Bronze', liquid: 400, gas: 400, steam: 200, price:2, url: 'products_bronze.htm?frame=frames/products_bronze_Q.htm', type: 'standard' },
            {size: '1-1/2', value: 1.5, coefficient: 26.0, name: 'TYPE Q SERIES', seriessize: 'Pipe Size', material: 'Bronze', liquid: 400, gas: 400, steam: 200, price:2, url: 'products_bronze.htm?frame=frames/products_bronze_Q.htm', type: 'standard' },
            {size: '2', value: 2.0, coefficient: 48.0, name: 'TYPE Q SERIES', seriessize: 'Pipe Size', material: 'Bronze', liquid: 400, gas: 400, steam: 200, price:2, url: 'products_bronze.htm?frame=frames/products_bronze_Q.htm', type: 'standard' },
            {size: '2-1/2', value: 2.5,  coefficient: 75.0, name: 'TYPE Q SERIES', seriessize: 'Pipe Size', material: 'Bronze', liquid: 400, gas: 400, steam: 200, price:2, url: 'products_bronze.htm?frame=frames/products_bronze_Q.htm', type: 'standard' },
            {size: '3', value: 3.0,  coefficient: 100.0, name: 'TYPE Q SERIES', seriessize: 'Pipe Size', material: 'Bronze', liquid: 400, gas: 400, steam: 200, price:2, url: 'products_bronze.htm?frame=frames/products_bronze_Q.htm', type: 'standard' },
            
            {size: '1/8', value: 0.125, coefficient: 1.2, name: 'TYPE KD SERIES', seriessize: 'Pipe Size', material: 'Steel', liquid: 6, gas: 6, steam: 6, price:0, url: 'products_steel.htm?frame=frames/products_steel_KD.htm', type: 'direct-acting' },
            {size: '1/4', value: 0.25, coefficient: 2.2, name: 'TYPE KD SERIES', seriessize: 'Pipe Size', material: 'Steel', liquid: 6, gas: 6, steam: 6, price:0, url: 'products_steel.htm?frame=frames/products_steel_KD.htm', type: 'direct-acting' },
            {size: '3/8', value: 0.375, coefficient: 2.5, name: 'TYPE KD SERIES', seriessize: 'Pipe Size', material: 'Steel', liquid: 6, gas: 6, steam: 6, price:0, url: 'products_steel.htm?frame=frames/products_steel_KD.htm', type: 'direct-acting' },
            {size: '1/2', value: 0.5, coefficient: 2.8, name: 'TYPE KD SERIES', seriessize: 'Pipe Size', material: 'Steel', liquid: 6, gas: 6, steam: 6, price:0, url: 'products_steel.htm?frame=frames/products_steel_KD.htm', type: 'direct-acting' },
            {size: '3/4', value: 0.75, coefficient: 6.8, name: 'TYPE KD SERIES', seriessize: 'Pipe Size', material: 'Steel', liquid: 6, gas: 6, steam: 6, price:0, url: 'products_steel.htm?frame=frames/products_steel_KD.htm', type: 'direct-acting' },
            {size: '1', value: 1.0, coefficient: 11.5, name: 'TYPE KD SERIES', seriessize: 'Pipe Size', material: 'Steel', liquid: 6, gas: 6, steam: 6, price:0, url: 'products_steel.htm?frame=frames/products_steel_KD.htm', type: 'direct-acting' },
            
            {size: '1/8', value: 0.125, coefficient: 1.4, name: 'TYPE QD SERIES', seriessize: 'Pipe Size', material: 'Bronze', liquid: 6, gas: 6, steam: 6, price:0, url: 'products_bronze.htm?frame=frames/products_bronze_QD.htm', type: 'direct-acting' },
            {size: '1/4', value: 0.25, coefficient: 2.1, name: 'TYPE QD SERIES', seriessize: 'Pipe Size', material: 'Bronze', liquid: 6, gas: 6, steam: 6, price:0, url: 'products_bronze.htm?frame=frames/products_bronze_QD.htm', type: 'direct-acting' },
            {size: '3/8', value: 0.375, coefficient: 2.8, name: 'TYPE QD SERIES', seriessize: 'Pipe Size', material: 'Bronze', liquid: 6, gas: 6, steam: 6, price:0, url: 'products_bronze.htm?frame=frames/products_bronze_QD.htm', type: 'direct-acting' },
            {size: '1/2', value: 0.5, coefficient: 3.0, name: 'TYPE QD SERIES', seriessize: 'Pipe Size', material: 'Bronze', liquid: 6, gas: 6, steam: 6, price:0, url: 'products_bronze.htm?frame=frames/products_bronze_QD.htm', type: 'direct-acting' },
            {size: '3/4', value: 0.75, coefficient: 6.0, name: 'TYPE QD SERIES', seriessize: 'Pipe Size', material: 'Bronze', liquid: 6, gas: 6, steam: 6, price:0, url: 'products_bronze.htm?frame=frames/products_bronze_QD.htm', type: 'direct-acting' },
            {size: '1', value: 1.0, coefficient: 11.0, name: 'TYPE QD SERIES', seriessize: 'Pipe Size', material: 'Bronze', liquid: 6, gas: 6, steam: 6, price:0, url: 'products_bronze.htm?frame=frames/products_bronze_QD.htm', type: 'direct-acting' }

          ]

        };// End of Valve Hash Table
    
    }, // End of Intializer
    
    findValve: function(coefficient, medium, pressure){
        // Grab the coefficient we will be looking for
        coefficient = parseFloat(coefficient);
        
        // Clear results list
        $('resultslist').innerHTML = "";
        
        // Grab all valves
        var F = this.Valves;
        
        // Select valves that fall into a certain material type
        var materialType = F.Products.findAll(function(valve){
                // Return exact matches
                return valve.material == $F('material');
            });
        
        // Select valves that fulfill the pressure requirement
        var belowMaxPSI = materialType.findAll(function(valve){
                if(medium == 'liquid'){
                    return valve.liquid >= parseFloat(pressure - 14.7);
                }else if(medium == 'gas'){
                    return valve.gas >= parseFloat(pressure - 14.7);
                }else if(medium == 'steam'){
                    return valve.steam >= parseFloat(pressure - 14.7);
                }
        });
        
        // Search for exact Cv matches
        if(belowMaxPSI.any(function(valve){return valve.coefficient == coefficient;}))
        {
            // Exact matches found
            var exactMatches = belowMaxPSI.findAll(function(valve){
                // Return exact matches
                return valve.coefficient == coefficient;
            });
            return exactMatches;
        }else{
            // No Exact Matches found must select the closest valves                         
            var allCoefficients = belowMaxPSI.pluck('coefficient');
            
            /////////////////////////////////////////////////////////////
            /////////////// SELECTING COEFFS ////////////////////////////
            // Grab distinct coefficients so we can sort out coefficients
            var distinctCoefficients = [];
            distinctCoefficients = allCoefficients.unique();
            
            var sortedCoefficients = [];
            // Sort by coefficient
            sortedCoefficients = distinctCoefficients.sortBy(function(coeff){
                return parseFloat(coeff);
            });
            
            sortedCoefficients.reverse();
            
            var lowValue = [];
            var highValue = [];           
            
            var lowValue = sortedCoefficients.detect(
                function(coeff){
                    return coeff < coefficient;
                }
            );
            
            sortedCoefficients.reverse();
            
            var highValue = sortedCoefficients.detect(
                function(coeff){
                    return coeff > coefficient;
                }
            );
            /////////////////////////////////////////////////////////////
            
            var lowValues = [];
            var highValues = [];
            
            var lowValues = belowMaxPSI.findAll(
                function(valve){
                    return valve.coefficient == lowValue;
                }
            );
            
            var highValues = belowMaxPSI.findAll(
                function(valve){
                    return valve.coefficient == highValue;
                }
            );
            var combinedArray = [];
            combinedArray = lowValues.concat(highValues);
            
            // Empty all arrays at this point
            var finalArray = [];
            finalArray = combinedArray.sortBy(function(valve){
            return valve.price;
            })
            return finalArray;
         } // End of if
    }// End of Find Valve

    
}// End of Prototype
    