// JavaScript Document
 var userClickedYes = false;
$(document).ready(function () {
     $(".numberinput").forceNumeric();
});


 // forceNumeric() plug-in implementation
 jQuery.fn.forceNumeric = function () {

	 return this.each(function () {
		 $(this).keydown(function (e) {
			 var key = e.which || e.keyCode;

			 if (!e.shiftKey && !e.altKey && !e.ctrlKey &&
			 // numbers   
				 key >= 48 && key <= 57 ||
			 // Numeric keypad
				 key >= 96 && key <= 105 ||
			 // comma, period and minus, . on keypad
				key == 190 || key == 188 || key == 109 || key == 110 ||
			 // Backspace and Tab and Enter
				key == 8 || key == 9 || key == 13 ||
			 // Home and End
				key == 35 || key == 36 ||
			 // left and right arrows
				key == 37 || key == 39 ||
			 // Del and Ins
				key == 46 || key == 45)
				 return true;

			// return false;
		 });
	 });
 }
 
 

function checkName(e, tableName, inputName)
{
	
	
	if(!userClickedYes)
	{
		
		
		e.preventDefault();
	}

	
	var name = document.getElementById(""+inputName).value;
	if(name != null && name != "")
	{
		$.ajax({
			url: 'checkRedundantNames.php?name='+name+'&tablename='+tableName,
			   success: function(data) {
				   //
				//alert(data);
				   if(data == '0')
				   {
					 
					   userClickedYes = true;
						
					//$(".nameToBeChecked").submit();
						$("#nameError").css("display","none");
						//alert(555);
						return true;
						
					}
				   else if(data == '1')
				   {
						userClickedYes = false;
						$("#"+inputName).val("");
						$("#"+inputName).focus();
						
						$("#nameError").css("display","block");
						return false;
				   }
			   }
	
		}); 
	}
	
	//return 1;
}



function checkNameWithId(e, tableName, inputName, inputId)
{
	if(!userClickedYes)
	{
		e.preventDefault();
	}
	
	
	var name = document.getElementById(""+inputName).value;
	var id = document.getElementById(""+inputId).value;
	//alert('inputName'+ document.getElementById(""+inputName).value);
	//alert('inputId'+ document.getElementById(""+inputId).value);
	//document.write('checkRedundantNames.php?do=checkNameWithId&name='+name+'&id='+id+'&tablename='+tableName);
	//alert('checkRedundantNames.php?do=checkNameWithId&name='+name+'&id='+id+'&tablename='+tableName);
		$.ajax({
			url: 'checkRedundantNames.php?do=checkNameWithId&name='+name+'&id='+id+'&tablename='+tableName,
			   success: function(data) {
				
				   if(data == '0')
				   {
					 
					    userClickedYes = true;
						
						$("#nameError").css("display","none");
						//  alert('data'+data);
					  // $(".nameToBeChecked").submit();
					     //alert('data11'+data);
						//// break;
					 return true;
						
					}
				   else if(data == '1')
				   {
					    
						 userClickedYes = false;
						 $("#"+inputName).val("");
						 
						 $("#nameError").css("display","block");
						 //alert("هذا الإسم موجود من قبل");
						 // return false;
				   }
				   
				   
			   }
	
		  }); 
		
}


//customized function which used only for adding product categories in add product page (productview/add.html), and for adding expenses type in add expense page(expenseview/add.html)

function checkCatName(e, tableName, inputName)
{
	if(!userClickedYes)
	{
		e.preventDefault();
	}
	
	var name = document.getElementById(""+inputName).value;
	if(name != null && name != "")
	{
		$.ajax({
			url: 'checkRedundantNames.php?name='+name+'&tablename='+tableName,
			   success: function(data) {
				   //
			//	alert(data);
				   if(data == '0')
				   {
					    userClickedYes = true;
						$("#nameError").css("display","none");
						$(".nameToBeChecked2").submit();
					}
				   else if(data == '1')
				   {
						 userClickedYes = false;
						 $("#"+inputName).val("");
						 $("#"+inputName).focus();
					
						$("#nameError").css("display","block");
				   }
			   }
		  }); 
	}
	return data;
}


