jQuery.fn.ForceNumericOnly =
function()
{
    return this.each(function()
    {
        $(this).keydown(function(e)
        {
            var key = e.charCode || e.keyCode || 0;			
            // allow backspace, tab, delete, enter, arrows, numbers and keypad numbers ONLY
            // home, end, period, and numpad decimal
            return (
                key == 8 || 
                key == 9 ||
                key == 13 ||
                key == 46 ||
                key == 110 ||
                //key == 190 ||
                (key >= 35 && key <= 40) ||
                (key >= 48 && key <= 57) ||
                (key >= 96 && key <= 105));
        });
    });
};
$("#RegMobile").ForceNumericOnly();

//binds to onchange event of your input field
$("#fileToUpload").bind('change', function() {
	if ($(".btn-success").attr('disabled',false)){
 		$(".btn-success").attr('disabled',true);
 	}
	var ext = $("#fileToUpload").val().split('.').pop().toLowerCase();
	if ($.inArray(ext, ['csv']) == -1)
	{
		$("#errorUpload").slideDown("slow");
		a=0;
	}
	else
	{		
		a=1;		
		if (a==1)
		{
			$("#errorUpload").slideUp("slow");
			$(".btn-success").attr('disabled',false);
		}
	}
});

$(".questionSelectAll").click(function () {	
    $(".alert").hide();
	if($(this).is(":checked")){
		$(".ActionBox").show();		
	}
	else if($(this).is(":not(:checked)")){
		$(".ActionBox").hide();
	}
	$(".questionId").prop('checked', $(this).prop('checked'));
});

$(".questionId").click(function () {	
    $(".alert").hide();
	var checkCount = $('input[name="question_id[]"]:checked').length;
	if(checkCount>0)
	{
		$(".ActionBox").show();
	}
	else
	{
		$(".ActionBox").hide();	
	}
});

$(".verifyQuestion").click(function(){			
	$.blockUI({ message: '<h4 style="margin-top:0.5rem;"><img src="images/busy.gif"/> Please Wait...</h4>' });
	var questionId = [];	
	$.each($("input[name='question_id[]']:checked"), function(){            	
		questionId.push($(this).val());	
	});
	var questionAllId = questionId.join(",");		
	var dataString = "questionAllId="+questionAllId;	
	$.ajax
	({
		  type: "POST",
		  url: "ajax/verifyQuestion.php",
		  data: dataString,
		  success: function(data)
		  {
			 var obj = $.parseJSON(data);
			 if(obj.status==1)
			 { 								
				setTimeout(function(){$.unblockUI(); location.reload();}, 1000);
			 }
			 else if(obj.status==2)
			 { 				
				setTimeout(function(){$.unblockUI(); location.reload();}, 1000);
			 }				 
		  }
	});
});

$(".deleteQuestion").click(function(){			
	$.blockUI({ message: '<h4 style="margin-top:0.5rem;"><img src="images/busy.gif"/> Please Wait...</h4>' });
	var questionId = [];	
	$.each($("input[name='question_id[]']:checked"), function(){            	
		questionId.push($(this).val());	
	});
	var questionAllId = questionId.join(",");		
	var dataString = "questionAllId="+questionAllId;	
	$.ajax
	({
		  type: "POST",
		  url: "ajax/deleteQuestion.php",
		  data: dataString,
		  success: function(data)
		  {
			 var obj = $.parseJSON(data);
			 if(obj.status==1)
			 { 
				setTimeout(function(){$.unblockUI(); location.reload();}, 1000);
			 }
			 else if(obj.status==2)
			 { 
				setTimeout(function(){$.unblockUI(); location.reload();}, 1000);
			 }				 
		  }
	});
});

$(".addQuestionExcel").submit(function(e){	
	e.preventDefault();
	$.blockUI({ message: '<h4><img src="images/busy.gif" /> Please Wait...</h4>' });
	$.ajax({
		url: "ajax/addQuestionExcel.php",
		type: "POST",
		data:  new FormData(this),
		contentType: false,
		cache: false,
		processData:false,
		success: function (data) {			
			var data = $.parseJSON(data);
			if(data.status==true)
			{				
				$("#ErrorReg").html('<div class="alert alert-success"><strong>Success!</strong> '+data.SuccessMsg+'</div>');					
				$(".addQuestionExcel").trigger("reset");
				$.unblockUI();														
			}
			else
			{
				$("#ErrorReg").html('<div class="alert alert-danger"><strong>Success!</strong> '+data.SuccessMsg+'</div>');					
				$(".addQuestionExcel").trigger("reset");
				$.unblockUI();		
			}
		}
	});
});

$(".addRetailerExcel").submit(function(e){	
	e.preventDefault();
	$.blockUI({ message: '<h4><img src="images/busy.gif" /> Please Wait...</h4>' });
	$.ajax({
		url: "ajax/addRetailerExcel.php",
		type: "POST",
		data:  new FormData(this),
		contentType: false,
		cache: false,
		processData:false,
		success: function (data) {			
			var data = $.parseJSON(data);
			if(data.status==true)
			{				
				$("#ErrorReg").html('<div class="alert alert-success"><strong>Success!</strong> '+data.SuccessMsg+'</div>');					
				$(".addRetailerExcel").trigger("reset");
				$.unblockUI();														
			}
			else
			{
				$("#ErrorReg").html('<div class="alert alert-danger"><strong>Success!</strong> '+data.SuccessMsg+'</div>');					
				$(".addRetailerExcel").trigger("reset");
				$.unblockUI();		
			}
		}
	});
});

function getDistrict(state)
{
	var StringData='state='+state;
    $.ajax({
		url:'ajax/getDistrict.php',
		dataType:'html',
		data:StringData,                    
		success:function(data)
		{
			$("#district").html(data);																			  
		},
		error:function(data){
			
			}
	});
}

function getBeat(district)
{
	var state = $("#state").val();
	var StringData='state='+state+'&district='+district;
    $.ajax({
		url:'ajax/getBeat.php',
		dataType:'html',
		data:StringData,                    
		success:function(data)
		{
			$("#beat").html(data);																			  
		},
		error:function(data){
			
			}
	});
}

function getRetailer(beat)
{
	var state      = $("#state").val();
	var district   = $("#district").val();
	var StringData = 'state='+state+'&district='+district+'&beat='+beat;
    $.ajax({
		url:'ajax/getRetailer.php',
		dataType:'html',
		data:StringData,                    
		success:function(data)
		{
			$("#shop_id").html(data);																			  
		},
		error:function(data){
			
			}
	});
}

function deleteOrderConfirmation()
{
  var x = confirm("Are you sure you want to delete?");
  if (x)
      return true;
  else
    return false;
}

