$(document).ready(function(){

	var runSearch 		= 1;
	var rawZip 			= $('#ajaxZipcode_input').val();
	var rawTrimmed		= jQuery.trim(rawZip);
	var newZip;
	var newTrimmed;
	var currentZip;
	var sameZip;
	
		
	// don't let the form actually be submitted...
	$('#ajaxZipcode').submit(function(){
		//console.log('submitForm Ran');
		return false;
	}); 


	$('#ajaxZipcode_input').focus(function() { 
		//console.log('.focus Ran');
		runSearch=1 ;
	});


	$('#ajaxZipcode_input').keyup(function() { 
		//console.log('keyup Ran');
		var thisVal=$(this).val();
		var thisLen=thisVal.length;
		
		if(thisLen==5) {
			//console.log('length was 5 Ran');
			runZipCodeSearch();
			runSearch=0;
		} else {
			//console.log('length was not 5 Ran');
			runSearch=1;
			$("#ajaxZipcode_results").slideUp(700,function() {
				$("#ajaxZipcode_results").html("");
			});
		}
		
	});

	// on tab, run the search
	$('#ajaxZipcode_input').blur(function() { 
	
		runZipCodeSearch();	
			
	});	
	
	
	
	// define the zipcodeSearch
	function runZipCodeSearch() {
	
		//console.log("runZipCode Ran");
	
		if (runSearch==1) {
			runSearch=0;
			
			// shoudl probably include an activity image
			
			$("#ajaxZipcodeFloatWrapper #loadingImage").fadeIn();
			
			
			// assign currentZip the value of the previous newZip
			currentZip	= newZip; 
			
			// get a new value for newZip
			newZip 			= $('#ajaxZipcode_input').val();
			newZip			= jQuery.trim(newZip);
			
			// compare...
			if (currentZip==newZip) {
				sameZip = 1;
			} else {
				
				sameZip = 0;
			}
			
/* 			console.log('The Zip is '+newZip); */
			
			//alert("Current: "+currentZip+" Raw: "+rawZip+" new: "+newZip);
			
			var length		= newZip.length;
			var notNum		= isNaN(newZip);
		
			/*
console.log("notNum = "+notNum);
			console.log("length = "+length);
			console.log("sameZip = "+sameZip);
			console.log("currentZip = "+currentZip);
*/
			
			if (notNum==false && length==5 && sameZip==0) { // we ony want to run this if the zip has changed
			
				// the MEAT of the function should go here - submit
				// ajax request to PHP script and display the results
				
				$("#ajaxZipcode_results").slideUp(700,function() {
					$("#ajaxZipcode_results").html("").wait(1000, function() {
					
					// ajax stuff here.....
					var senddata = "z="+newZip;
		
					var results = $.ajax({
						url: "http://www.salvationarmycarolinas.org/assets/snippets/utilities/zipCodeAjax.php",
						type: "GET",
						dataType: "html",
						data: senddata,
						async: false
	
					}).responseText;
	
					$("#ajaxZipcode_results").append(results);
					$("#ajaxZipcode_results").slideDown(800);
				
					});	
				});
				
			} else if(newZip=='') { 
			
				$("#ajaxZipcode_results").slideUp(700,function() {
					$("#ajaxZipcode_results").html("");
				});
					
	
			} else if(currentZip!=newZip) {
				// Give Error here - zipcode should be 5 digits, etc
				$("#ajaxZipcode_results").slideUp(700,function() {
					$("#ajaxZipcode_results").html("");
					$("#ajaxZipcode_results").html("<p><strong>There was an error processing your request</strong></p><p>Please try again by entering the zipcode in the box above.</p><p>Also, be aware that zipcodes should be entered as a five digit number.  If you continue to have this error, please do not hesitate to contact us.</p><p>If you continue to receive this error, you may wish to consult our national website for information on locations</p><p><a href=\"http://www.salvationarmyusa.org\">http://www.salvationarmyusa.org</a></p>");
					
					$("#ajaxZipcode_results").slideDown();
				
				});
				
				
				
			} else {
			
				// Give Error here - zipcode should be 5 digits, etc
				$("#ajaxZipcode_results").slideUp(700,function() {
					$("#ajaxZipcode_results").html("");
					$("#ajaxZipcode_results").html("<p><strong>Something</strong> has gone terribly wrong.  We're working on fixing it now.</p>");
					
					$("#ajaxZipcode_results").slideDown();
				
				});

			
			
			}
			
			$("#ajaxZipcodeFloatWrapper #loadingImage").wait(250).fadeOut();
		
		}
	
	}

	
	$("a.getLinkToCommand").click(function() {
	
		var id = $(this).attr("id");
		alert(id);
	
	}); 
	
	
	function getThisURL() {
		var id = $(this).attr("alt");var linkId	= "a#"+id;$(this).click(function(){window.location=$(linkID).attr("href");console.log("got this far");return false;});
	}
	
	
	

}); // end $(document).ready ...
