// JavaScript Document
var azone = [
    "4-5", "2", "10-34", "3", "35", "4", "36-37", "3", "38-49", "4", "50-53", "3", "54", "4", "55", "3", "56", "4", "57", "3", "58-59", "4", "60-63", "3", "64-66", "2", "67", "3", "68-89", "2", "100-119", "2", "120-123", "3", "124-127", "2", "128", "3", "129", "4", "130-167", "3", "168", "2", "169", "3", "170-214", "2", "215", "3", "216-227", "2", "228-241", "3", "242-243", "4", "244-245", "3", "246-248", "4", "249", "3", "250-253", "4", "254", "2", "255-259", "4", "260", "3", "261", "4", "262-268", "3", "270-277", "4", "278-279", "3", "280-299", "4", "300-303", "5", "304-306", "4", "307", "5", "308-309", "4", "310-312", "5", "313-314", "4", "315-339", "5", "341", "5", "342-375", "5", "376-379", "4", "380-394", "5", "395", "6", "396-399", "5", "400-418", "4", "420-424", "5", "425-438", "4", "439", "3", "440-443", "4", "444-445", "3", "446-462", "4", "463-464", "5", "465-474", "4", "475-478", "5", "479-496", "4", "497-509", "5", "510-516", "6", "520-559", "5", "560-576", "6", "577", "7", "580-585", "6", "586", "7", "587", "6", "588-593", "7", "594", "8", "595", "7", "596-599", "8", "600-639", "5", "640-645", "6", "646", "5", "647-649", "6", "650-659", "5", "660-692", "6", "693", "7", "700-714", "6", "716", "5", "717-719", "6", "720-726", "5", "727-732", "6", "733", "7", "734-767", "6", "768-769", "7", "770-778", "6", "779-788", "7", "789", "6", "790-830", "7", "831-865", "8", "870-872", "7", "873", "8", "874-878", "7", "879-880", "8", "881-885", "7", "889-961", "8", "970-986", "8", "988-994", "8"
   ];

var caseShipCost = [  2, 10.00, 3, 11.00, 4, 13.00, 5, 14.00, 6, 18.00, 7, 21.00, 8, 25.00 ] //per case
var caseBagCost = [  2, 3.00, 3, 3.00, 4, 4.00, 5, 4.00, 6, 4.00, 7, 4.00, 8, 5.00 ] //per 200

function findZone( zipcode )
{
	zipcode = "" + zipcode; // ensure it's a string
	 if ( zipcode.length > 5 )
	 {
		  zipcode = zipcode.substring( 0,5);
	 }
    if ( zipcode.length == 0 )
    {
		return -9125; // No entry
    }
    if ( zipcode.length < 5 )
    {
		return -9124; // Not Valid
    }
    if (isNaN( zipcode ))
    {
        zipcode = "00000";
		return -9124; // Not Valid
    }

    var zip3 = parseInt( zipcode.substring(0,3), 10 ); // get 3 digits *as a number*
	 //alert(zip3);

    for ( az = 0; az < azone.length; az += 2 )
    {
        // get the zip range *AS* a range:
        var range = azone[az].split("-");
        var minzip3 = parseInt( range[0] );
        var maxzip3 = minzip3; // in case we do not have a range
        if ( range.length > 1 ) maxzip3 = parseInt( range[1] ); // aha!
 		//alert("minzip3 = " + minzip3 + "   maxzip3 = " + maxzip3  + "  zip3 = " + zip3)
       if ( minzip3 <= zip3 && zip3 <= maxzip3 )
        {
            return azone[az+1]; // found the right zone
        }
    } // end of for loop
    return -9123; // zone not found
}
// test it out
//alert( "The zone for zip code 44657 is " + findZone( "05A678" ) );

function findCaseShip (iZone, iNumCases) {
    if (iZone < 2 || iZone > 8)
	{
		return -8888;
	}
    if (iNumCases > case_freeship) //free shipping
	{
		return 0;
	}
	for ( az = 0; az < caseShipCost.length; az += 2 )
    {
        if (caseShipCost[az] == iZone)
        {
            return caseShipCost[az+1] * iNumCases; // found the right zone
        }
    } // end of for loop
    return -8888; // zone not found
						 
}

function findBagShip (iZone, iNumBags, iNumCases) {
var iBags = parseInt((iNumBags + 199)/200); //round to nearest 200 bags
	if (iZone < 2 || iZone > 8)
	{
		return -8888;
	}
    if (iNumBags > bag_freeship) //free shipping
	{
		return 0;
	}
    if (iNumCases < 1) //free bag shipping
	{
		return 0;
	}
for ( az = 0; az < caseBagCost.length; az += 2 )
    {
        if (caseBagCost[az] == iZone)
        {
            return caseBagCost[az+1] * iBags; // found the right zone
        }
    } // end of for loop
    return -8888; // zone not found
						 
}

