/** SEARCH PAGE JAVASCRIPT **/

// function to provide filter routes feature 
function show_all_direct_routes() {
	$('#route_list td').show();
	$('#show_all_route_list').hide();
}

function show_all_alt_routes(){
	$('#alt_route_list tr.routes').show();
	$('#show_all_route_list').hide();
}

function PopupCenter(pageURL, title,w,h) {
	var left = (screen.width/2)-(w/2);
	var top = (screen.height/2)-(h/2);
	var targetWin = window.open (pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
} 

$(function(){
	$('.route_link').popupWindow({height:500,width:800,top:50,left:50,centerBrowser:1});
	$('#direct_filter_routes').change( function() {
		if ($(this).val()) {
			$('#direct_route_list td').hide();
			$('#direct_route_list td.ROUTES-' + $(this).val()).show();
			$('#show_all_route_list').show();
		} else {
			$('#direct_route_list td').show();
			$('#show_all_route_list').hide();
		}
	}).val('');
	
	$('#alt_filter_routes').change(function(){
		if($(this).val()){
			$('#alt_route_list tr.routes').hide();
			$('#alt_route_list tr.ROUTES-'+$(this).val()).show();
			$('#show_all_route_list').show();
		}else{
			$('#alt_route_list tr.routes').show();
			$('#show_all_route_list').hide();
		}
	}).val('');
	
	
	// Send to Mobile Via SMS START //
	$('.send_sms').click(function(e){
		$($(this).attr('href')+' form').attr('action',$(this).attr('route_link')+'/sms');
		$('#sms_response').html('');
	});
	
	$('.send_sms').openDOMWindow({ 
		eventType:'click',
		height:100, 
		width:400
	});

	 
	
	$('#send_sms_form form').submit(function(){

		var mob_no = $.trim($('input[name="mobileno"]').val()).match(/^([\+][0-9]{1,3}[ \.\-])?([\(]{1}[0-9]{2,6}[\)])?([0-9 \.\-\/]{3,20})((x|ext|extension)[ ]?[0-9]{1,4})?$/);
		if(!mob_no){
			$('#sms_response').html('Invalid Mobile Number');
		}else{
			var sfrm = $(this);
			$('#sms_response',sfrm).html('Please wait Sending message...');
			$.post($(this).attr('action'),$(this).serialize(),function(resp){
				if(resp.status == 'error'){
					$('#sms_response',sfrm).html(resp.message);
				}else{
					$('#sms_response',sfrm).html(resp.message);
					setTimeout(function(){
						$.closeDOMWindow();
					},2000);
				}
			},'json');
			
		}
		return false;
	});
	// Send to Mobile Via SMS END //
	
});

