// JavaScript Document
function getStateByZip( incomingZip ) {
	//incomingZip = incomingZip.trim();
	incomingZip = incomingZip.replace(/^\s+|\s+$/g, '');
	allStates = new Array();
	allStates["AK"] = "9950099929";
	allStates["AL"] = "3500036999";
	allStates["AR"] = "71600729997550275505";
	allStates["AZ"] = "8500086599";
	allStates["CA"] = "9000096199";
	allStates["CO"] = "8000081699";
	allStates["CT"] = "0600006999";
	allStates["DC"] = "20000200992020020599";
	allStates["DE"] = "1970019999";
	allStates["FL"] = "32000339993410034999";
	allStates["GA"] = "3000031999";
	allStates["HI"] = "96700967989680096899";
	allStates["IA"] = "5000052999";
	allStates["ID"] = "8320083899";
	allStates["IL"] = "6000062999";
	allStates["IN"] = "4600047999";
	allStates["KS"] = "6600067999";
	allStates["KY"] = "40000427994527545275";
	allStates["LA"] = "70000714997174971749";
	allStates["MA"] = "0100002799";
	allStates["MD"] = "20331203312060021999";
	allStates["ME"] = "038010380103804038040390004999";
	allStates["MI"] = "4800049999";
	allStates["MN"] = "5500056799";
	allStates["MO"] = "6300065899";
	allStates["MS"] = "3860039799";
	allStates["MT"] = "5900059999";
	allStates["NC"] = "2700028999";
	allStates["ND"] = "5800058899";
	allStates["NE"] = "6800069399";
	allStates["NH"] = "03000038030380903899";
	allStates["NJ"] = "0700008999";
	allStates["NM"] = "8700088499";
	allStates["NV"] = "8900089899";
	allStates["NY"] = "004000059906390063900900014999";
	allStates["OH"] = "4300045999";
	allStates["OK"] = "73000731997340074999";
	allStates["OR"] = "9700097999";
	allStates["PA"] = "1500019699";
	allStates["PR"] = "0060000729";
	allStates["RI"] = "02800029990637906379";
	allStates["SC"] = "2900029999";
	allStates["SD"] = "5700057799";
	allStates["TN"] = "37000385997239572395";
	allStates["TX"] = "7330073399739497394975000799998850188599";
	allStates["UT"] = "8400084799";
	allStates["VA"] = "2010520199203012030120370203702200024699";
	allStates["VT"] = "0500005999";
	allStates["WA"] = "9800099499";
	allStates["WI"] = "49936499365300054999";
	allStates["WV"] = "2470026899";
	allStates["WY"] = "8200083199";
	var result = "";
	var zipLength;
	var segments;
	for( var i in allStates ) {
		zipLength = String( allStates[i].length );
		//$("body").append("<br>zip length: " + zipLength + "<br>");
		// the zips will be 10, 20, 30, or 40 in length
		segments = new Array();
		switch (zipLength) {
		case "10":
			segments[0] = allStates[i].substring( 0, 5 );
			segments[1] = allStates[i].substring( 5, 10 );
			break;
		case "20":
			segments[0] = allStates[i].substring( 0, 5 );
			segments[1] = allStates[i].substring( 5, 10 );
			segments[2] = allStates[i].substring( 10, 15 );
			segments[3] = allStates[i].substring( 15, 20 );
			break;
		case "30":
			segments[0] = allStates[i].substring( 0, 5 );
			segments[1] = allStates[i].substring( 5, 10 );
			segments[2] = allStates[i].substring( 10, 15 );
			segments[3] = allStates[i].substring( 15, 20 );
			segments[4] = allStates[i].substring( 20, 25 );
			segments[5] = allStates[i].substring( 25, 30 );
			break;
		case "40":
			segments[0] = allStates[i].substring( 0, 5 );
			segments[1] = allStates[i].substring( 5, 10 );
			segments[2] = allStates[i].substring( 10, 15 );
			segments[3] = allStates[i].substring( 15, 20 );
			segments[4] = allStates[i].substring( 20, 25 );
			segments[5] = allStates[i].substring( 25, 30 );
			segments[6] = allStates[i].substring( 30, 35 );
			segments[7] = allStates[i].substring( 35, 40 );
			break;
		}
		/*if ( segments[0] ) {
			$("body").append("<br>segment 1: " + segments[0] + "<br>");
		}
		if ( segments[1] ) {
			$("body").append("<br>segment 2: " + segments[1] + "<br>");
		}
		if ( segments[2] ) {
			$("body").append("<br>segment 3: " + segments[2] + "<br>");
		}
		if ( segments[3] ) {
			$("body").append("<br>segment 4: " + segments[3] + "<br>");
		}
		if ( segments[4] ) {
			$("body").append("<br>segment 5: " + segments[4] + "<br>");
		}
		if ( segments[5] ) {
			$("body").append("<br>segment 6: " + segments[5] + "<br>");
		}
		if ( segments[6] ) {
			$("body").append("<br>segment 7: " + segments[6] + "<br>");
		}
		if ( segments[7] ) {
			$("body").append("<br>segment 8: " + segments[7] + "<br>");
		}*/
		
		totalSegments = segments.length;
		for ( var j = 0; j <= totalSegments; j += 2 ) {
			if ( incomingZip >= segments[j] && incomingZip <= segments[j+1] ) {
				result = i;
				//$("body").append("<br>State Match Found: " + i + "<br>");
			}
		}
	}

	return result;
}

$(document).ready(function(){

	// track product add / remove
	if ( MivaProdCode != "" && MivaProdCode != "undefined" && MivaProdCode != null & MivaProdCode != " " ) {
		MivaProdCode = MivaProdCode.replace(/^\s+|\s+$/g, '') ;
		MivaAction = MivaAction.replace(/^\s+|\s+$/g, '') ;
		MivaProdCode = MivaProdCode.replace(/^&+|&+$/g, '') ;
		MivaAction = MivaAction.replace(/^&+|&+$/g, '') ;
		if ( MivaAction == "ADPR" ) {
			pageTracker._trackEvent('Products', 'Product Added', MivaProdCode );
		} else {
			pageTracker._trackEvent('Products', 'Product Removed', MivaProdCode );
		}	
	}

	if ( $("#calcship").length ) {
		//$("#calcship").css({ 'display' : 'none' });
		// this should always be true
		$("#ShipZip").keypress(function(event){
			if ( ( event.which && event.which == 13 ) || ( event.keyCode && event.keyCode == 13 ) ) {
				// they hit enter, which won't trigger a click in IE for some reason
				document.getElementById("calcShippingButton").click();
				event.preventDefault();
				return false;
			} else {
				return true;
			}
		});
		
		$("#showCalcShipForm").click(function(event){
			event.preventDefault();
			if ( !$("#calcshipLocation").dialog( "option", "height" ) ) {
				//var HW = getHW( $(this).attr("href") );
				$("#calcshipLocation").dialog({
					bgiframe: true,
					width: 180,
					height: 160,
					modal: true,
					position: 'right'
				});
			} else {
				$("#calcshipLocation").dialog( "open" );
			}
			pageTracker._trackEvent('Basket: BASK', 'Shipping Calculator Form Viewed' );
			return false;
		});
		
		$("#calcShippingButton").click(function(event){
			event.preventDefault();
			$("#calcShipLoading").toggle( "blind", "fast" );
			pageTracker._trackEvent('Basket: BASK', 'Shipping Calculator Results Viewed' );
			var theProtocol = (("https:" == document.location.protocol) ? "https:" : "http:");
			var theURL = theProtocol + "//www.favorfavor.com/mm5/merchant.mvc";
			var theShipState = getStateByZip( $.trim($("#ShipZip").val()) );
			if ( theShipState != "PR" ) {
				var shipCountry = "US";
			} else {
				var shipCountry = "PR";
			}
			//$("body").append("state: " + theShipState + " Zip: " + theShipZip);
			//alert("posting " + theURL );
			// lets try GET instead of POST
			theURL += "?ShipZip=" + $.trim($("#ShipZip").val());
			theURL += "&shipcountry=" + shipCountry;
			theURL += "&shipstate=" + theShipState;
			theURL += "&calcship=1";
			theURL += "&Store_Code=FF";
			theURL += "&Screen=CALCSHIPPING";
			$("#calcshipLocation").dialog( "close" );
			$("#calcShipResults").load( theURL, function(){
				$("#calcShipLoading").toggle( "blind", "fast" );
			});
			// post method below was not working, generated internal 500 errors
			/*$.post( theURL, {
				   ShipZip: theShipZip,
				   shipcountry: theShipCountry,
				   shipstate: theShipState,
				   calcship: "1",
				   Store_Code: "FFB",
				   Screen: "CALCSHIPPING"
				   },
				   function(returnData){
						$("#calcShipResults").html( returnData );
						jqToggleLayer( "calcShipLoading", "fast", 0 );
				   });*/
			/*theURL += "?Screen=CALCSHIPPING&Store_Code=FFB&calcship=1&ShipZip=" + theShipZip + "&shipcountry=" + theShipCountry;
			$.post( theURL, {
				   nothing: "nothing"
				   },
				   function(returnData){
					   $("#calcShipResults").html( returnData );
				   });
			//jqSlideLayer ( "calcshipLocation", "medium", 0 );*/
		});
		
	} // if ( $("#calcship").length ) {

		/*$("#showCalcShipForm").click(function(event){
			event.preventDefault();
			if ( !$("#calcshipLocation").dialog( "option", "height" ) ) {
				//var HW = getHW( $(this).attr("href") );
				$("#calcshipLocation").dialog({
					bgiframe: true,
					width: 180,
					height: 160,
					modal: true,
					position: 'right'
				});
			} else {
				$("#calcshipLocation").dialog( "open" );
			}
			pageTracker._trackEvent('Basket: BASK', 'Shipping Calculator Form Viewed' );		
		});*/



	//wishlist
	if ( $("#showWishList").length ) {
		$("#showWishList").click(function(event){
			if ( !$("#createAccountDialog").dialog( "option", "height" ) ) {
				$("#createAccountDialog").dialog({
					bgiframe: true,
					width: 500,
					height: 400,
					modal: true
				});
			} else {
				$("#createAccountDialog").dialog( "open" );
			}
			pageTracker._trackEvent('Basket: BASK', 'Save Basket Form Viewed' );							  
		});
	} // if ( $("#showWishList").length ) {
	
	/* add explanation to "sample" text */
	$("i strong").each(function(i){
		if ( $(this).html() == "SAMPLE" ) {
			$(this).append("<span style=\"font-weight: normal;\"> - <a href=\"/includes/ajax/whereDidPersGo.php\" class=\"whereDidPersGo\">where did my personalization go?</a></span>");
		}
	});
	$(".whereDidPersGo").click(function(event){
		event.preventDefault();
		if ( !$("#whereDidPersGoExplained").length ) {
			$("body").append("<div id=\"whereDidPersGoExplained\" class=\"hideMe\" title=\"Sample Order Personalization\"></div>");
			$("#whereDidPersGoExplained").dialog({
				bgiframe: true,
				height: 210,
				width: 360,
				modal: true
			}).load( $(this).attr("href") );
		} else {
			$("#whereDidPersGoExplained").dialog("open");
		}
		return false;
	});
	/* END add explanation to "sample" text */

}); // $(document).ready(function(){