$(document).ready(function(){
	$("input[name=fnumber]").bind("keyup", bind_from_elements);
	$("input[name=fstreet]").bind("keyup", bind_from_elements);
	$("input[name=fsettlement]").bind("keyup", bind_from_elements);
	$("select[name=fname]").bind("change", bind_from_elements);
	$("select[name=trainf]").bind("change", bind_from_elements);
	$("input[name=fpostalcode]").bind("keyup", bind_from_elements);
	
	
	$("input[name=tnumber]").bind("keyup", bind_to_elements);
	$("input[name=tstreet]").bind("keyup", bind_to_elements);
	$("input[name=tsettlement]").bind("keyup", bind_to_elements);
	$("select[name=tname]").bind("change", bind_to_elements);
	$("select[name=traind]").bind("change", bind_to_elements);
	$("input[name=tpostalcode]").bind("keyup", bind_to_elements);
	
	// intermediate handlers
	$("input[name=int_number]").bind("keyup", bind_int_elements);
	$("input[name=int_tstreet]").bind("keyup", bind_int_elements);
	$("input[name=int_tsettlement]").bind("keyup", bind_int_elements);
		

});
function activateTab(activateTab, hideTabsIn, hideContentsIn, showContent) {
	// make all tabs unselected first
	ulElement 	= document.getElementById(hideTabsIn);
	liElements 	= ulElement.getElementsByTagName('LI');
	for (i = 0; i < liElements.length; i++) 
	{
		liElements[i].className = 'tab';
	}
	
	// activate clicked tab second
	activateTab.className		= 'selected';

	// hide all content boxes
	divElement 	= document.getElementById(hideContentsIn);
	divElements 	= divElement.getElementsByTagName('DIV');
	for (i = 0; i < divElements.length; i++) 
	{
		divElements[i].style.display 	= 'none';
		
	}

	// show content last
	contentDiv		= document.getElementById(showContent);
	contentDiv.style.display		= 'block';
}
/*============================================*/
/*========== new 1.1============= */

var timer_from		= null;
var timer_to		= null;
var timer_int		= null;

function bind_from_elements() {
	
	if( timer_from ) {
		clearTimeout(timer_from);
	}
	timer_from		= setInterval(update_from_point, 1500);
}


function update_from_point() {
	




	$("input[id=fmatch_uid]").empty();
	var thisOpt   = document.getElementById('fmatch_name');
	$("#pickup_from2").show();
	thisOpt.value = "Loading....";
	$.getJSON("get_address_points2.php", $("#find_locations").serialize(), function(response) {
	if ( ! response["from"].length) {
		 var thisOpt   = document.getElementById('fmatch_name');
		 	thisOpt.value = "";
			thisOpt.value = "No Results Found";		
		} else {
		 var thisOpt   = document.getElementById('fmatch_uid');
		 thisOpt.value = "";
		 $.each(response["from"], function(key, value){

			var thisOpt   = document.getElementById('fmatch_uid');
			thisOpt.value = value["match_uid"]+":"+value["userreadable"];

			var thisOpt2 = document.getElementById('fmatch_name');
			thisOpt2.value = value["userreadable"];
			
			
			
			  
		 }); 
	 }
	});

	clearTimeout(timer_from);
}



/*================================================*/

function bind_to_elements() {
	
	if ( timer_to )	{
		clearTimeout(timer_to);
	}

	timer_to		= setInterval(update_to_point, 1500);
}


function update_to_point() {
	
	$("input[id=tmatch_uid]").empty();

	var thisOpt   = document.getElementById('tmatch_name');
	$("#drop_at2").show();
	thisOpt.value = "Loading....";
	$.getJSON("get_address_points2.php", $("#find_locations").serialize(), function(response) {
	if ( ! response["to"].length) {
		 var thisOpt   = document.getElementById('tmatch_name');
		 	thisOpt.value = "";
			thisOpt.value = "No Results Found";		
		} else {
		 var thisOpt   = document.getElementById('tmatch_uid');
		 thisOpt.value = "";
		 $.each(response["to"], function(key, value){

			var thisOpt   = document.getElementById('tmatch_uid');
			thisOpt.value = value["match_uid"]+":"+value["userreadable"];

			var thisOpt2 = document.getElementById('tmatch_name');
			thisOpt2.value = value["userreadable"];

			
			
			  
		 }); 
	 }
	});

	clearTimeout(timer_to);
}


/*================================================*/

function bind_int_elements() {
	
	if ( timer_int )	{
		clearTimeout(timer_int);
	}

	timer_int		= setInterval(update_int_point, 1500);
}


function update_int_point() {
	$("select[id=imatch_uid]").empty();
	var thisOpt   = document.createElement('option');
	thisOpt.value = "";
	thisOpt.appendChild(document.createTextNode("Loading.."));
	$("select[id=imatch_uid]").append(thisOpt);
	
	$.getJSON("get_address_points2.php", $("#find_locations").serialize(), function(response) {
		$("select[id=imatch_uid]").empty();
	
		if ( ! response["int"].length) {
		 var thisOpt   = document.createElement('option');
		 thisOpt.value = "";
		 thisOpt.appendChild(document.createTextNode("No results.."));	
		 $("select[id=imatch_uid]").append(thisOpt);
		 
		} else {
			var thisOpt   = document.createElement('option');
			thisOpt.value = "";
		//	thisOpt.appendChild(document.createTextNode("Select"));
		//	$("select[id=imatch_uid]").append(thisOpt);
		 $.each(response["int"], function(key, value){
			 var thisOpt   = document.createElement('option','selected');
			 thisOpt.value = value["match_uid"]+":"+value["userreadable"];
			 thisOpt.appendChild(document.createTextNode(value["userreadable"]));
				$("select[id=imatch_uid]").append(thisOpt);

		 }); 
	 }
	});
	
	clearTimeout(timer_int);
}




function render_quote_form() {
	
	$("#quote").show();
}

function calc_quote() {
	$("#quote_result").show();
	$("#quote_result").html("<br \>Calculating Please Be Patient! <br \><img src=images/bus.gif> ");
	$.get("get_quote_sales.php", $("#find_locations").serialize(), function(data){
		$("#quote_result").show();
		$("#quote_result").html(data);
	 });
	
		}

function show_int() {
	if ($("#intermediate").css("display") != "block" ) { 
		$("#intermediate").show();
			
	} else {
		$("#intermediate").hide();
	
		$("input[name=int_number]").val("");
		$("input[name=int_tstreet]").val("");
		$("input[name=int_tsettlement]").val("");
		$("input[name=imatch_uid]").val("");
	}
}


//comments

function bind_to_elements() {
	
	if ( timer_to )	{
		clearTimeout(timer_to);
	}

	timer_to		= setInterval(update_to_point, 1500);
}


// for qutation
function delete_quote(inv_id) {
	if(confirm('Are you sure to Delete this Quotation?')) {
	$('#inv_row_'+inv_id).fadeOut("fast");
	$.ajax({
		   type: "POST",
		   url: "quotes.php",
		   data: "work=delete_quote&quote_id="+inv_id,
		   success: function(){			   
			   }		});	}
}
function un_delete_quote(inv_id) {
	if(confirm('Are you sure to Recover this Quotation?')) {
	$('#inv_row_'+inv_id).fadeOut("fast");
	$.ajax({
		   type: "POST",
		   url: "quotes.php",
		   data: "work=un_delete_quote&quote_id="+inv_id,
		   success: function(){			   
			   }		});	}
}

function open_quote() {
	
	
	$.ajax({
		   type: "POST",
		   url: "quotes.php",
		   data: "keyword=All",
		   success: function(){			   
			   }		});	
}
// for quotation ends

//date pickers
$(function() {$("#datepicker").datepicker({  numberOfMonths: [3, 2] , dateFormat: 'dd-mm-yy', minDate: 0, maxDate: '+15M +10D'});});
$(function(){$("#datepicker2").datepicker({  numberOfMonths: [3, 2], dateFormat: 'dd-mm-yy',minDate: 0, maxDate: '+15M +10D'});});

// date pickers ends
function show_quote_comments() {
	$("#show_quote_comments").show();}
function hide_quote_comments() {
	$("#show_quote_comments").hide();}
	
	function delete_comment(inv_id) {
	if(confirm('Are you sure to Delete this comment?')) {
	$('#inv_row_'+inv_id).fadeOut("fast");
	$.ajax({
		   type: "POST",
		   url: "browse_quote.php",
		   data: "work=delete_comment&comment_id="+inv_id,
		   success: function(){			   
			   }		});	}
}
$(document).ready(function() {
$('#return_journey').bind('change', function(event) {
	var selected = $("#return_journey option:selected");    
    var output = "";
    if(selected.val() == 'Return'){
$("#lugg").show("drop", { direction: "down" });
} else { 
$("#lugg").hide("drop", { direction: "down" });
}
});
});
