jQuery(function($) {
	$(document).ready(function() {
		//alert('track and trace redirect in themes start');
		
		populatePage();	
		scrollingElement = (document.scrollingElement || document.body)

// 		$('#search, #btnBack').click(function() {
// 			var trackingNosNewLineDelimitedURL = processTrackingNos($('#trackingNum').val(), '\n', '\\n');
// 			console.log(trackingNosNewLineDelimitedURL);
// 			setupIFrame(trackingNosNewLineDelimitedURL);
			
//             $(scrollingElement).animate({
//               scrollTop: document.body.scrollHeight
//           }, 500);
// 		});
		
		$('#search, #btnBack').click(function() {
			var trackingNosNewLineDelimitedURL = processTrackingNos($('#trackingNum').val(), '\n', '\\n');
			trackingNos = trackingNosNewLineDelimitedURL. replace(/(\n|\\n)/gm,",");
			var flag = 0;
			var trackingNoSplit = trackingNos.split(",");
			trackingNoSplit.forEach(trackingNo => {
				if (trackingNo == '') return;
				
				
				if (!trackingNo.startsWith('254') && !trackingNo.startsWith('255') && !trackingNo.startsWith('249') && !trackingNo.startsWith('256')) {
					flag = 1
				}
				
				var match = trackingNo.match("^SG[0-9]{8}$");
				if (match) {
		        	flag = 0;
		        }
			})
			url = 'https://tracking.frontierforce.com/tracking_search.aspx?stid=dpexwwe&cn=' + trackingNos
			if (flag != 1) {
				if (trackingNos.substr(trackingNos.length - 1 == ',')) trackingNos = trackingNos.slice(0,-1);
				url = 'http://18.138.79.135/trace-and-track/index?id=' + trackingNos;		
			}
			var win = window.open(url, '_blank');
            win.focus();
		});
		
		


		function populatePage() {
			// var urlParams = new URLSearchParams(window.location.search);		
			// var cnValue = urlParams.get('cn');
			$.urlParam = function(name){
				var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
				if (results==null){
				   return null;
				}
				else{
				   return decodeURI(results[1]) || 0;
				}
			}
			var cnValue = $.urlParam('cn');
			if (cnValue) {
				var trackingNosNewLineDelimited = processTrackingNos(cnValue, ',', '\n');
				$('#trackingNum').val(trackingNosNewLineDelimited);

				var trackingNosNewLineDelimitedURL = processTrackingNos(cnValue, ',', '\\n');
				setupIFrame(trackingNosNewLineDelimitedURL);			
			}
			
		}
		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('1r>>' + trackingNoTrimmed);

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

		function setupIFrameOld(trackingNos) {
			//console.log('iframeCN>>' + trackingNos);
			var iframeUrl = 'https://ws01.ffdx.net/v4/etrack_blank.aspx?stid=dpex&version=dpex&txtinput=hide&reqpod=hide&option=,8,' + '&cn=' + trackingNos;
			$('#iframeURL').attr('src', iframeUrl);
			$('#iframeBackDiv').show();			
		}
		
		function setupIFrame(trackingNos) {
            
            //console.log(trackingNos. replace(/(\n|\\n)/gm,","));
            trackingNos = trackingNos. replace(/(\n|\\n)/gm,",");
            var iframeUrl = 'https://tracking.frontierforce.com/tracking_search.aspx?stid=dpexwwe&cn=' + trackingNos;
            //console.log(iframeUrl);
            $('#iframeURL').attr('src', iframeUrl);
            $('#iframeBackDiv').show();
        }
	});	
});