ToFormTo.Shipping = {};
ToFormTo.Shipping.JSON = {};

ToFormTo.Shipping.JSON.Provider = {
	'Initialize':
	function(){
		$('#ContactForShipping').hide(0);
	},	
    'GetRateByProduct': 
	function (ZipCode, ProductName) {		

		this.ShowContactForShipping();

		var zone = this.GetZoneIndex(ZipCode);
		if(ZipCode.length != 5 || zone == null){ 			 
			return; 
		}
		
		try{
        	var category = this.GetCategoryByProduct(ProductName);
		}
		catch(exc){  
			return; 
		}
		
		
		if(category.Rates[zone] != undefined){		
			return('$' + category.Rates[zone].toFixed(2) + ' per set.');
		}
		else { 
			return; 
		}
    },
    'GetZoneIndex': 
	function (ZipCode) {
        for(zoneIndex in ToFormTo.Shipping.JSON.DataSource.ZipZones){
			var zone = ToFormTo.Shipping.JSON.DataSource.ZipZones[zoneIndex];
			for(rangeIndex in zone){
				var range = zone[rangeIndex];
				var first3 = ZipCode.substr(0,3);
				if(range[0] <= first3 && range[1] >= first3){
					return(zoneIndex);	
				}
			}
		}
		return(null);
    },
	'GetCategoryByProduct' : 
	function(ProductName) { 
		return(
			$.Enumerable.From(ToFormTo.Shipping.JSON.DataSource.Categories)
			.First(
				function (c) { 
					return(
						$.Enumerable.From(c.Products)
						.Any(
							function(p) {
								return(p == ProductName);
							}
						)
					);
				}
			)
		);						
	},
	'ShowContactForShipping':
	function(){
		$('#ShippingEstimate').show(0).html('Please contact us for shipping rates.');	
	}
};

ToFormTo.Shipping.JSON.DataSource = {
	'ZipZones' : [
		[['286','289'],['373','374'],['376','379'],['385','385'],['407','409'],['413','418'],['421','422'],['425','426']],
		[['240','253'],['255','259'],['261','264'],['266','266'],['270','277'],['280','283'],['290','314'],['317','319'],['350','362'],['367','368'],['370','372'],['382','384'],['388','388'],['397','406'],['410','412'],['420','420'],['423','424'],['427','432'],['437','438'],['450','457'],['460','462'],['470','472'],['474','478']],
		[['070','104'],['110','113'],['137','239'],['254','254'],['260','260'],['265','265'],['267','268'],['278','279'],['284','285'],['315','316'],['320','329'],['335','338'],['342','347'],['363','366'],['369','369'],['375','375'],['380','381'],['386','387'],['389','396'],['433','436'],['439','449'],['458','458'],['463','469'],['473','473'],['479','496'],['520','520'],['522','538'],['600','639'],['646','647'],['650','658'],['700','705'],['707','729'],['749','749']],		
		[['005','045'],['048','069'],['105','109'],['114','136'],['330','334'],['339','341'],['349','349'],['497','516'],['521','521'],['539','566'],['570','574'],['580','581'],['640','645'],['648','648'],['660','691'],['706','706'],['730','748'],['750','784'],['786','787'],['789','792'],['795','796']],
		[['046','047'],['567','567'],['575','577'],['582','588'],['592','593'],['692','693'],['785','785'],['788','788'],['793','794'],['797','828'],['865','884']],
		[['590','591'],['594','599'],['821','821'],['829','834'],['836','837'],['840','864'],['889','893'],['898','898']],
		[['835','835'],['838','838'],['894','897'],['900','966'],['969','994']]
	],
	'Categories' : [
		{'Name' : 'A', 'Rates' : 		[25.35,29.67,32.56,37.72,44.59,51.39,57.90], 'Products' : ['Austin','Hollywood','Memphis','NewYorker','PalmBeach','SamoaSide','TahitiSide','TeakSide','WickerBarStool','WickerSide','Dallas','Orlando','Houston']},
		{'Name' : 'B', 'Rates' : 		[27.16,31.34,34.13,39.28,46.04,53.15,58.38], 'Products' : ['Aluminum','Karen','Logan','Marilyn','SamoaArm','Stanford','TahitiArm','TeakArm','WickerArm','Suma','Barcelona','Prage','OsakaSide','OsakaArm','Monaco']},
		{'Name' : 'C', 'Rates' : 		[18.86,22.29,24.68,28.89,35.19,42.40,49.57], 'Products' : ['SamoaStool','SouthBeachStool','TeakBarStool']},
		{'Name' : 'D', 'Rates' : 		[34.53,37.99,40.25,45.53,51.79,59.07,62.91], 'Products' : ['AluminumStool']},
		{'Name' : 'E', 'Rates' : 		[28.00,31.92,35.11,40.48,47.13,49.51,62.40], 'Products' : []},
		{'Name' : 'F', 'Rates' : 		[17.05,19.32,21.04,21.88,29.69,34.62,40.59], 'Products' : ['TableSteelRound28']},
		{'Name' : 'G', 'Rates' : 		[26.24,30.50,33.34,38.51,45.32,52.27,58.13], 'Products' : ['Oceanic_Sidex1','Oceanic_Sidex2','OceanicSide','Breeze','Delta','Sunrise','Horizon']},
		{'Name' : 'H', 'Rates' : 		[21.20,25.52,28.92,30.95,41.01,48.16,56.43], 'Products' : ['Oceanic_Bar24']},
		{'Name' : 'I', 'Rates' : 		[28.07,32.17,34.90,40.06,46.76,54.03,58.61], 'Products' : ['Oceanic_Bar30 ']},
		{'Name' : 'J', 'Rates' : 		[26.66,30.27,32.79,37.22,43.55,58.39,58.39], 'Products' : ['TableSteelRound32','TableSteelSquare']},
		{'Name' : 'K', 'Rates' : 		[25.31,27.07,28.76,31.06,35.28,39.02,43.84], 'Products' : ['TableBarSteel']},
		{'Name' : 'L', 'Rates' : 		[24.97,27.43,29.34,29.55,38.35,43.65,49.83], 'Products' : ['TableTeakRnd','TableTeakSqr']},
		{'Name' : 'Crystall', 'Rates' : [32.98, 32.98, 32.98, 32.98, 32.98, 32.98, 32.98], 'Products' : ['Crystall']},
		{'Name' : 'Free', 'Rates' : [0, 0, 0, 0, 0, 0, 0], 'Products':['Soho','Midtown','Manhattan','Chelsea','T18','T19','T20','T30','T5']},
		{'Name' : 'Phlex', 'Rates' : [31.5, 37, 41, 46, 52.50, 59, 67], 'Products':['Phlex']}
	]
};

$(function(){
	$('#EstimateShipping').keypress(function(e) {
		 if (e.keyCode == '13' || e.keyCode == '10') {
			 e.preventDefault();
			 
			 var code = $('.Summary').attr('productcode');
			 var zip = $(this).val();
		     $('#ShippingEstimate').show(0);
			 
			 var rate = ToFormTo.Shipping.JSON.Provider.GetRateByProduct(zip,code);
			 if(rate != null){
			 	$('#ShippingEstimate').html('Estimated shipping to ' + zip + ': ' + rate);
			 }
		 }
	});
});
