// JavaScript Document
var zipInputs = $('#zipcode').val();

var ShippingEstimator = {
	
	init: function()
    {
	//hide results at begining of load
	$('#results').hide();
	//bind click listeners to buttons
			//shipping estimator
  	$('#zipCodeButton').bind("click", ShippingEstimator.matchZip, false);
			//check another ZIP code
	$('#checkZipAgain').bind("click", ShippingEstimator.checkAgain, false);
			//require 5 digit zip code numbers only
	jQuery(function($){
		$('#zipcode').mask("99999");
		});
	
	//need to disable submit form when user hits enter
			// used inline javascript to cancel user submit via enter key (onsubmit="return false;")
	},
	
	matchZip: function()
	{
		
	//remove error text if any
		$('#zipError').empty();
		
    // get the zip code entry from user
    var zipInputs = $('#zipcode').val();
	
	
	//remove leading zeros
	var noZeroInputs = trimNumber(zipInputs);
	function trimNumber(s) {
  		while (s.substr(0,1) == '0' && s.length>1) { s = s.substr(1,9999); }
  		return s;
			}
	var noZeroInputs = trimNumber(zipInputs);
	
	//parse the user entry to make sure it's a number
	var zipParsed = parseInt(noZeroInputs);
	
	//if user entry is a number than execute process
	if (!isNaN(zipParsed))
	{
			//find the value from zips array
			var finalZip = zips[zipParsed];
			
			//check if zip code exists in zips array
			if (finalZip != undefined)
			{
				//zip code exists, pass it to showZipRes
				ShippingEstimator.showZipRes(finalZip);
			}else{
				
				//Tell user that the zip code does not exist
				$('#zipError').append("Sorry, we do not have that ZIP code in our system. Please try again.");
			}
	}else{
		
		//Tell the user to please enter a valid zip code
		$('#zipError').append("Please enter a valid zip code");
	}
	

	},
	
 	showZipRes: function (finalZip)
	{

		//place each excelResults in an array.
		var classExcelArray = $('.excelResults');
		
if ($(classExcelArray[0]).is(':empty')){		//make sure not to double print
	
		
			zipInputs =  parseInt($('#zipcode').val(), 10);
			//take current time, adjust for user's entered zipcode
			
			if (today2 >= dst_start && today < dst_end){ //does today fall inside of DST period?
				var timezoneChange = timezone[zipInputs] + timezonedst[zipInputs];
		}else{
				var timezoneChange = timezone[zipInputs];
		}
			//alert("timezone" + timezone[zipInputs] + "\n dst" + timezonedst[zipInputs]);
			
			var shoppersTime = curTime + timezoneChange;
			var verDisplayShoppersTime = shoppersTime;
			
			var shippingDateSkipper = new Date();
			 							//start out with current date
			shippingDateSkipper.setDate(today); 
			var verDisplayDate = shippingDateSkipper.getDate(today);
		
/*
			if (shoppersTime < 0){
				shippingDateSkipper.setDate(shippingDateSkipper.getDate() + 1);
			}
*/
							
		// script to skip over sat or sunday for shipping date, also passes a new amount of days to deliverydate
			
			// This part gets the date that the item will ship
			
			var delayShipping = 0;											//set a variable for transit days
			 																//and push back delivery date
			 if (shoppersTime >= 15){											 //check if time is after 3pm
			shippingDateSkipper.setDate(shippingDateSkipper.getDate() + 1);	//add a day because after 3pm
			delayShipping++;												//add a delayed day: after 3pm
			 }else{
			shippingDateSkipper.setDate(shippingDateSkipper.getDate());		//don't add because before 3pm
			}
			 
			while (!(shippingDateSkipper.getDay() % 6)){					//while the day is sat or sun
			shippingDateSkipper.setDate(shippingDateSkipper.getDate() + 1);	// add a day and test again
			delayShipping++;												//also adds a delay to transit days
			}
			var shipDate =  shippingDateSkipper.format("mm/dd/yy");			//save and format the result
		

			var deliveryDateSkipper = shippingDateSkipper;							//get the shipping date

			var countDownZip = finalZip;
			while(countDownZip >0){
			
				if((deliveryDateSkipper.getDay() % 6)){
					countDownZip--;
				}
				deliveryDateSkipper.setDate(deliveryDateSkipper.getDate() + 1);
			
			}
			
			while(!((deliveryDateSkipper.getDay()) % 6)){							//if tomorrow is sat
			  deliveryDateSkipper.setDate(deliveryDateSkipper.getDate() + 1);	//add a day
					delayShipping++;
			}
			 
			 //document.write(deliveryDate);
			var deliveryDate =  deliveryDateSkipper.format("mm/dd/yy");
			
			//document.write(delayShipping,  moreDelayShipping, moreMoreDelayShipping, finalZip);
		
		//var shipDate = month + "/" + day + "/" + year;
		var travelTime = (finalZip);
		
		var daysForDeliver =  travelTime +" Business day(s)";
		
		//month + "/" + (day+finalZip) + "/" + year;
		
	//Place all shipping data into an array for classExcelArray Loop
		var shippingArray = [ shipDate, daysForDeliver, deliveryDate];	
		
	
			// place the array in the span classes (classExcelArray Loop)
	 for (i=0, ii=shippingArray.length; i<ii; i++) 
			{
				
			$(classExcelArray[i]).append(shippingArray[i]);
						
 			} 
		
		
		var html= "";
		html += "Version 2.01,";
		
		if (verDisplayShoppersTime < 0){
			verDisplayShoppersTime = verDisplayShoppersTime + 24;
			verDisplayDate = verDisplayDate -1;
		}
		html += "h" +verDisplayShoppersTime;
		//verDisplayDate = verDisplayDate.format("mm/dd/yy");
		html += ",d" +verDisplayDate;
		
		$('#version').html(html);
										
		$('#results').slideDown(); 
		} //is empty end
 	}, 
	
	checkAgain: function()
	{
			//Delete old spans and remake the spans
			$('.excelResults').empty();
			//$('#excelResults').replaceWith("<span class="excelResults"></span>");
			//hide results so user can try again
			$('#results').slideUp();

	}
	
	};

ShippingEstimator.init();
	
	
