function pausecomp(millis) 
{
	var date = new Date();
	var curDate = null;

	do { curDate = new Date(); } 
	while(curDate-date < millis);
} 
function createDataGrid(tableId,data){
	try
	{
		if (data==null){
			hideandSorter();
			return false;
		}
		var $table = $("#"+tableId+" tbody");
		$.each(data,function(rowLabel,v) {
			if (bl%2==0)
			{
				$table.append('<tr></tr>');
			}else {
				$table.append('<tr></tr>');
			}
			bl++;
			//$table.append('<tr></tr>');
			var $tr = $('tr:last',$table);
			$.each(v,function(j,cellData) {
				$tr.append('<td>' + cellData + '</td>');
			})
		});
		return true;
	}
	catch (e)
	{
		return false;
	}
}
var again=0
var isfly = 0;
function aJ(page,variable){
	$.ajax({
	   type: "POST",
	   url: page,
	   data: variable,
	   success: function(msg){
		   try{
			   eval(msg);

				if (airports!=null)
				{
					$.each(airports, function(code,name) {
							//alert(code+' : '+ name+"<br />");
							if ($(".explodeairlines").html().indexOf(code+' : ')<0)
							{
								var strong = document.createElement('STRONG');
								$(strong).append(document.createTextNode(code+' : '));
								$(".explodeairlines").append(strong);
								$(".explodeairlines").append(document.createTextNode(name+"  "));
//								$(".explodeairlines").append(document.createElement('BR'));
								$(".explodeairlines").show(1000);
							}
							//alert(code+' : '+ name+"<br />");
					});
				}

				count = companies.length;
				for (i=0;i<count ;i++ )
				{
					if (! companies[i])
					{
						companies.splice(i,1);
						i = count;
					}
				}
				if (createDataGrid("arrivalResults",dep))
				{
					$("#arrivalResults").show();
					$(".statistic").show();
					$("#arrivalResults tr").height(38);
					$("#totalresult").html(bl);
					$("#totaltime").html((new Date().getTime()-Stamp)/1000);
					$("#arrivalResults").tablesorter({ headers: { 8: { sorter:'grades' } } });
					isfly++;
				}
				if (ret!=null)
				{
					if (createDataGrid("returnResults",ret))
					{
						$("#returnResults").show();
						$("#returnResults tr").height(38);
						$("#totalresult").html(bl);
						$("#totaltime").html((new Date().getTime()-Stamp)/1000);
						//if (companies.length<1)
						//{
							$("#returnResults").tablesorter({ headers: { 8: { sorter:'gradesi' } } });
						//}
					}
				}
				again=0;
			}catch (e){
				again++;
				if (again<3)
				{
					aJ(page,variable)
				}else{
					count = companies.length;
					for (i=0;i<count ;i++ )
					{
						if (! companies[i])
						{
							companies.splice(i,1);
							i = count;
						}
					}
				}
			}
			hideandSorter();
		}
	});
}
var deptutar = 0;
var rettutar = 0;
function hideandSorter(){
	if (companies.length<1)
	{
		if (isfly<1)
		{
			$("#loading").hide(1000);
			$("#nofly").show(2000);
		}else {
			$("#loading").hide(1000);
			$(document).pngFix();
			$("tr").hover(
				  function() {$('td', this).css('background', '#D5E6EC');$('td', this).parent().css('cursor','pointer');},
				  function() {$('td', this).css('background', 'transparent')}
			);
			$('#arrivalResults td').click(function() { 
				$('#arrivalResults tr').each(function (){
					$(this).removeClass('highlight');
				});
				var thisClicked = $(this).text();
				if (thisClicked == $(this).text()) { 
					$(this).parent().toggleClass('highlight');
					deptutar = $(this).parent().find('td').eq(8).text().toLowerCase().replace(/tl/,'').replace(/ /,'');
					$("#calculate #deptl span").text(deptutar+' TL');
				} else { 
					$(this).parent().removeClass('highlight');
				} 
				$("#totaltl span").text((deptutar*1+rettutar*1)*1+' TL');
			});
			$('#returnResults td').click(function() { 
				$('#returnResults tr').each(function (){
					$(this).removeClass('highlight');
				});
				var thisClicked = $(this).text();
				if (thisClicked == $(this).text()) { 
					$(this).parent().toggleClass('highlight');
					rettutar = $(this).parent().find('td').eq(8).text().toLowerCase().replace(/tl/,'').replace(/ /,'');
					$("#calculate #rettl span").text(rettutar+' TL');
				} else { 
					$(this).parent().removeClass('highlight');
				} 
				$("#totaltl span").text((deptutar*1+rettutar*1)*1+' TL');
			});
			
			$('#calculate').show();
			$('#calculate').css('top', $(this).scrollTop()+$(this).height()-$('#calculate').height()-20 + "px");
			$('#calculate').css('left', $(this).width()-$('#calculate').width()-10 + "px");

				$( window ).wresize(function(){
					$('#calculate').css('top', $(this).scrollTop()+$(this).height()-$('#calculate').height()-20 + "px");
					$('#calculate').css('left', $(this).width()-$('#calculate').width()-10 + "px");
				});
			$(window).scroll(function() {
				$('#calculate').css('top', $(this).scrollTop()+$(this).height()-$('#calculate').height()-20 + "px");
				$('#calculate').css('left', $(this).width()-$('#calculate').width()-10 + "px");
			});
		}
	}
}

$.tablesorter.addParser({ 
        // set a unique id 
        id: 'grades', 
        is: function(s) { 
            // return false so this parser is not auto detected 
            return false; 
        }, 
        format: function(s) { 
            // format your data for normalization
            return s.toLowerCase().replace(/ tl/,'');
        }, 
        // set type, either numeric or text 
        type: 'numeric' 
    });
$.tablesorter.addParser({ 
        // set a unique id 
        id: 'gradesi', 
        is: function(s) { 
            // return false so this parser is not auto detected 
            return false; 
        }, 
        format: function(s) { 
            // format your data for normalization
            return s.toLowerCase().replace(/ tl/,'');
        }, 
        // set type, either numeric or text 
        type: 'numeric' 
    });
$(document).ready( function () {
		$("#raund").click(
			function (){
				$("#retdiv").css("visibility","visible");
			}
		);
		$("#one").click(
			function (){
				$("#retdiv").css("visibility","hidden");
			}
		);
		$("#depdate").datepicker({
			numberOfMonths: 2,minDate: 0,
			onClose: function(input) {
				$("#retdate").datepicker( 'setDate' , $("#depdate").datepicker('getDate') );
				if ($("#depdate").datepicker('getDate')!=null)
				{
					$("#retdate").datepicker('option', 'minDate' , (Math.floor(($("#depdate").datepicker('getDate').valueOf()/1000 - new Date().valueOf()/1000)/(24*60*60))+1) );
				}
			}
		});
		$("#retdate").datepicker({numberOfMonths: 2,minDate: 0 });
	}
);
function checkForm(f){
	if ($('#depport').val()=='')
	{
		$('#depportau').val('Lütfen şehir ismini doğru yazınız!');
		return false;
	}
	if ($('#arrport').val()=='')
	{
		$('#arrportau').val('Lütfen şehir ismini doğru yazınız!');
		return false;
	}
	return true;
}