jQuery(function($) {
	$(document).ready(function() {
		//alert('track and trace in plugins start');
		
		$('#search_hp').click(function() {			
			var trackingNosCommaSeparated = processTrackingNos($('#trackingNum_hp').val(), '\n', ',');
			
			$.post(myAjax.ajaxurl, { action: "submitThenRedirect", trackingNum: trackingNosCommaSeparated } , function(data) {
				top.location.replace(data);				
			});			
		});

		function processTrackingNos(trackingNosFromInput, separatorUsedInInput, separatorUsedInOutput) {
			var trackingNosDelimited = '';

			if (trackingNosFromInput) {
				var trackingNos = trackingNosFromInput.split(separatorUsedInInput);				
				var trackingNoTrimmed = '';

				var aRegex = /^\w+$/;

				$.each(trackingNos, function(index, trackingNo) {
					trackingNoTrimmed = $.trim(trackingNo);
					//console.log('1>>' + trackingNoTrimmed);

					if (aRegex.test(trackingNoTrimmed)) {
						//console.log('2>>' + trackingNoTrimmed);
						trackingNosDelimited += trackingNoTrimmed + separatorUsedInOutput;
					}
				});
			}
			
			return trackingNosDelimited;
		}
		
	});	
});