
function toggleDiv(id,flagit) {
					if (flagit=="1"){
					if (document.layers) document.layers[''+id+''].visibility = "show"
					else if (document.all) document.all[''+id+''].style.visibility = "visible"
					else if (document.getElementById) document.getElementById(''+id+'').style.visibility = "visible"
					}
					else
					if (flagit=="0"){
					if (document.layers) document.layers[''+id+''].visibility = "hide"
					else if (document.all) document.all[''+id+''].style.visibility = "hidden"
					else if (document.getElementById) document.getElementById(''+id+'').style.visibility = "hidden"
					}
						}


$.fn.clearForm = function() {
  return this.each(function() {
							var name = this.name; 
    var type = this.type, tag = this.tagName.toLowerCase();
    if (tag == 'form')
      return $(':input',this).clearForm();
    if (name == 'fname' || name == 'trainf' || name == 'fpostalcode' ||  name == 'int_number'||  name == 'int_tstreet' ||  name == 'int_tsettlement' ||  name == 'imatch_uid' ||   name == 'tname' ||  name == 'traind'||  name == 'tpostalcode'  )
      this.value = '';
   
  });
};

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';
		$(':input').clearForm()
	}

	// 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;

$(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);
		
	// render quote form
//$("selected[id=tmatch_uid]").bind("change", calc_quote2);

	//$("select[id=tmatch_uid]").bind("change", render_quote_form);
});


function bind_from_elements() {
	
	if( timer_from ) {
		clearTimeout(timer_from);
	}

	timer_from		= setInterval(update_from_point, 1500);
}


function update_from_point() {
	$("select[id=fmatch_uid]").empty();

	var thisOpt   = document.createElement('option');
	thisOpt.value = "";
	thisOpt.appendChild(document.createTextNode("Confirming the location....! "));
	
	  $("#pickup_from2").show();
	$("select[id=fmatch_uid]").append(thisOpt);
	
	$.getJSON("get_address_points2.php", $("#find_locations").serialize(), function(response) {
		$("select[id=fmatch_uid]").empty();
		
		if ( ! response["from"].length) {
		 var thisOpt   = document.createElement('option');
		 thisOpt.value = "";
		 thisOpt.appendChild(document.createTextNode("No results.. Please Check your inputs"));
		 $("select[id=fmatch_uid]").append(thisOpt);
		 
		} else {
		 var thisOpt   = document.createElement('option');
		 thisOpt.value = "";
		
		//thisOpt.appendChild(document.createTextNode("Select"));
		 //$("select[id=fmatch_uid]").append(thisOpt);
		 
		 $.each(response["from"], function(key, value){
			 var thisOpt   = document.createElement('option','selected');
			 thisOpt.value = value["match_uid"]+":"+value["userreadable"];
			 thisOpt.appendChild(document.createTextNode(value["userreadable"]));
			 $("select[id=fmatch_uid]").append(thisOpt);
			
			  $("#pickup_from2").show();
		 }); 
	 }
	});
	
	clearTimeout(timer_from);
}


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

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

	timer_to		= setInterval(update_to_point, 1500);
}


function update_to_point() {
	$("select[id=tmatch_uid]").empty();
	var thisOpt   = document.createElement('option');
	thisOpt.value = "";
	thisOpt.appendChild(document.createTextNode("Confirming the location...."));
	 $("#drop_at2").show();
	$("select[id=tmatch_uid]").append(thisOpt);
	
	$.getJSON("get_address_points2.php", $("#find_locations").serialize(), function(response) {
		$("select[id=tmatch_uid]").empty();
	
		if ( ! response["to"].length) {
		 var thisOpt   = document.createElement('option');
		 thisOpt.value = "";
		 thisOpt.appendChild(document.createTextNode("No results, Please Check  your Destination Inputs"));
		 $("select[id=tmatch_uid]").append(thisOpt);
		 
		} else {
			var thisOpt   = document.createElement('option');
			thisOpt.value = "";
		//	thisOpt.appendChild(document.createTextNode("Select"));
			//$("select[id=tmatch_uid]").append(thisOpt);
		 $.each(response["to"], function(key, value){
			 var thisOpt   = document.createElement('option', 'selected');
			 thisOpt.value = value["match_uid"]+":"+value["userreadable"];
			 thisOpt.appendChild(document.createTextNode(value["userreadable"]));
			 $("select[id=tmatch_uid]").append(thisOpt);
			
			 			
		 }); 
		
	
		 $("#drop_at2").show();
	 }
	});
	
	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_quote2.php", $("#find_locations").serialize(), function(data){
		$("#quote_result").show();
		$("#quote_result").html(data);
	 });
	
		}

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

function show_pickup() {
	if ($("#pickup_from").css("display") != "block" ) { 
		$("#pickup_from").show();
		$("#pickup_from2").hide();
			$(':input').clearForm()
	} else {
		$("#pickup_from2").hide();
		$("#pickup_from").hide();
	
	
	}
}
function show_drop() {
	if ($("#drop_at").css("display") != "block" ) { 
		$("#drop_at").show();
		$("#drop_at2").hide();
			$(':input').clearForm()
	} else {
		$("#drop_at2").hide();
		$("#drop_at").hide();
	
	
	}
}
function show_contacts() {
	if ($("#contacts").css("display") != "block" ) { 
		$("#contacts").show();
		$("#contacts2").hide();
			$(':input').clearForm()
	} else {
		$("#contacts").hide();
		$("#contacts2").hide();
	
	
	}
}


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

	timer_to		= setInterval(update_to_point, 1500);
}

