jQuery(document).ready(function(){
	
	$('#rel-usr').get(0).selectedIndex = 0;
	$('#week').get(0).selectedIndex = 0;
	
	$( "#autocomplete" ).autocomplete({
		source: siteUrl+"routes/autousers",
		minLength: 2,
		select: function( event, ui ) {
			alert(ui.item.id);
		}
	 });
	 
/* 	 jQuery('#rel-usr').change(function(){
		var uId = jQuery(this).val();
		var elmObj = jQuery(this);
		if (uId){
			jQuery.ajax({
				type: 'POST',
				url: siteUrl+'routes/route_mapping',
				data: { uId: uId},
				beforeSend: function(){
					jQuery('#route-map').html('<img src="'+siteUrl+'img/loading20.gif" id="loading"/>');
				},
				success: function(data){
					$('#route-map').html(data);
				},
				statusCode: {
					404: function() {
						alert( message.noPage  );
					}
				}
			});
		}else{
			jQuery('#route-map').html('');
		}
	}); */


	jQuery('#rel-usr').change(function(){
		$('#week').get(0).selectedIndex = 0;
		$('#day').get(0).selectedIndex = 0;
		var uId = jQuery('#rel-usr').val();
		
		//var elmObj = jQuery(this);
		
		if (uId){
		//alert('sdfd');
		
			jQuery('#period-wrapper').show();	

			jQuery('#week').change(function(){
				//$('#week').get(0).selectedIndex = 0;
				var wId = jQuery('#week').val();
				
				//var elmObj = jQuery(this);
				if (wId){ 
					jQuery('#day-wrapper').show();	
					
					jQuery("#day, #rel-usr, #week").change(function(){						
						var uId = jQuery('#rel-usr').val();
						var period = jQuery('#period').val();
						var week = jQuery('#week').val();
						var day = jQuery('#day').val();
						var weekText = jQuery('#week option:selected').text();
						//var elmObj = jQuery(this);
						if (day){
							jQuery.ajax({
								type: 'POST',
								url: siteUrl+'routes/route_mapping',
								data: { uId: uId, period:period, week: week, day: day, weekText: weekText},
								beforeSend: function(){
									jQuery('#route-map').html('<img src="'+siteUrl+'img/loading20.gif" id="loading"/>');
								},
								success: function(data){
									
									$('#route-map').html(data);
								},
								statusCode: {
									404: function() {
										alert( message.noPage  );
									}
								}
							});
						}else{
							jQuery('#route-map').html('');

						}
				});
					
				}else{
					jQuery('#day-wrapper').hide();
				}
			});
							
		}else{
			jQuery('#week-wrapper').hide();
		}
	});


	
	
});

function submit_route_plan(){
	var elmObj = jQuery('#save-route');
	
	var selectedRoute = $( "input:checked" ).length;
	//if (selectedRoute > 0){
		jQuery.ajax({
			type: 'POST',
			url: siteUrl+'routes/update_user_route',
			data: $('#user-route-frm').serialize(),
			beforeSend: function(){
				elmObj.after('<img src="'+siteUrl+'img/loading20.gif" id="loading"/>');
			},
			success: function(data){
				//$('#route-map').html(data);
				$( "#loading" ).remove();
				alert(data);
				location.reload();
				
			},
			statusCode: { 
				404: function() { 
					alert( message.noPage );
					console.log(message);
					//location.reload();
				}
			}
		});
		
	/* }else{
		alert("Please select atleast one root");
	} */
	return false;
}