// JavaScript Document
///////////////////////////validation employeeselect//////////////////
$(document).ready(function(e) {
	
	
	  var employeespry = new Array();
	  var assetspry = new Array();
	  var stationitr = document.getElementById("stationitr").value;
	  var assetsitr = document.getElementById("assetsitr").value;
	  
	  assetspry[0] = "yes";
	  employeespry[0] = "yes";
	
	
	var h ;
	for(h = 1; h <= stationitr; h++)
	{
		
		
		 employeespry[h] = new Spry.Widget.ValidationSelect("employeespry"+h, {invalidValue:"-1"});	
	
	}
	  

	var j ;
	for(j = 1; j <= assetsitr; j++)
	{
		
		
		 assetspry[j] =  new Spry.Widget.ValidationSelect("assetspry"+j, {invalidValue:"-1"});	
		

	}  
	  
	  
function isNumberKey(evt)
   {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;

         return true;
  }

	// add new produc row in bill
		$("#addwork").click(function() {
			
			//the iltration of rows to increment the  name of rows
		$i= Number(document.getElementById('stationitr').value);
		$i++;

			$("#containerpros").append(
			'<tr>'
			+ '<td>'
			+'<div id="choose'+$i+'">'
			+'<span id="employeespry'+$i+'" ><select class="select2 wdt300 xx" name="workname'+$i+'" id="workname'+$i+'" >'
          	+'<option value="-1">اختر عامل</option>'
			+ $("#newemp").html()
		   +'</select><span class="selectInvalidMsg">***.</span><span class="selectRequiredMsg">***.</span></span></div>'
			+ '</td>'
			+ '<td>'
			+'<div id="hoursx'+$i+'">'
			+'<input name="hours'+$i+'" id="hours'+$i+'"  type="text" class="form-control getprototal  numberinput txt-nonraduis workerhour" required onkeypress="return isNumberKey(event)"/>'
			+'</div></td>'
			+ '<td>'
			+'<div id="workefficency'+$i+'">'
			+'<input name="efficency'+$i+'" id="efficency'+$i+'" type="text" class="form-control getprototal  numberinput txt-nonraduis workerhour effx" required onkeypress="return isNumberKey(event)"/>'
			+'</div>'
			+'</td>'
			+'<td id="'+$i+'">'
			+'<button id="remove'+$i+'" name="remove'+$i+'" type="button" class="close flt-none removeicon removework" aria-hidden="true">&times;</button>'
			+'</td>'
			+ '</tr>');
			 employeespry[$i] = new Spry.Widget.ValidationSelect("employeespry"+$i, {invalidValue:"-1"});	
			document.getElementById('stationitr').value= $i;
			$('select').select2();
			
	
		});
		
	


			// add new produc row in assets
		$("#addasset").click(function() {
			
			//the iltration of rows to increment the  name of rows
		$r= Number(document.getElementById('assetsitr').value);
		$r++;

			$("#containerassets").append(
			'<tr>'
			+ '<td>'
			+'<div id="chooseasset'+$r+'">'
			+'<span id="assetspry'+$r+'" ><select class="select2 wdt300" name="asset'+$r+'" id="asset'+$r+'" >'
          	+'<option value="-1">اختر اله</option>'
			+ $("#newasset").html()
		   +'</select><span class="selectInvalidMsg">***.</span><span class="selectRequiredMsg">***.</span></span></div>'
			+ '</td>'
			+ '<td>'
			+'<div id="assethours'+$r+'">'
			+'<input name="whour'+$r+'" id="whour'+$r+'"  type="text" class="form-control getprototal  numberinput txt-nonraduis assethour" required onkeypress="return isNumberKey(event)"/>'
			+'</div></td>'
			+ '<td>'
			+'<div id="assetefficency'+$r+'">'
			+'<input name="wefficency'+$r+'" id="wefficency'+$r+'" type="text" class="form-control getprototal  numberinput txt-nonraduis assethour effasset" required onkeypress="return isNumberKey(event)"/>'
			+'</div>'
			+'</td>'
			+'<td id="x'+$r+'">'
			+'<button id="wremove'+$r+'" name="wremove'+$r+'" type="button" class="close flt-none removeicon removework" aria-hidden="true">&times;</button>'
			+'</td>'
			+ '</tr>');
			document.getElementById('assetsitr').value= $r;
			$('select').select2();
			assetspry[$r] = new Spry.Widget.ValidationSelect("assetspry"+$r, {invalidValue:"-1"});
			
	
		});
    // the delete action that delete row of worker
	/************************/
	$(document).on('click','.removework', function() {
	
	
		// delete by partents 
		$itemRemove = Number($(this).parent().attr("id")); 
		
	
		   employeespry[$itemRemove].reset();
			employeespry[$itemRemove].destroy();
			$("#employeespry"+$itemRemove).val("yes");
			
		
		$(this).parent().parent().remove();
		
	   
			
		 countmydataworker();
		
		// get the itration from itration colum
		//$remitr = document.getElementById("hidden_itr").value

	});
	/************************/
	
	
	
	// the delete action that delete row of assets
	/************************/
	$(document).on('click','.removeasset', function() {
		
		
		var currentId =$(this).parent().attr("id"); 
		
		$itemRemovex = currentId.substr(1)
		
		
		
		 assetspry[$itemRemovex].reset();
		 assetspry[$itemRemovex].destroy();
		$("#assetspry"+$itemRemovex).val("yes");
			
		
		$(this).parent().parent().remove();
		
	    
		
			
		 countmydataasset();
		// get the itration from itration colum
		//$remitr = document.getElementById("hidden_itr").value
		
		
	});
	
	 
	
	/************************/
	
	
	
	var  totalworkerhour = 0;
	// get the total worker hours 
	/************************/
	$(document).on('keyup','.workerhour',function(){
	
					 countmydataworker();
				 
		
		
	});
		
		/************************/
			//////////////add assets///////////////////
	/************************/
	$(document).on('keyup','.assethour',function(){
		
		countmydataasset();
	//	validateselect();
		
	});
	
	///////////////////////add value of rows////////////	
		
		function countmydataworker()
		{
			var totalworkerhour=0;
			var numericExpression = /^[0-9]+$/;

				
			var empitr = Number(document.getElementById('stationitr').value);

			var totalhour = document.getElementById('totalworkerhour');
			var totalhourx = document.getElementById('totalworkerhour1');

			for($i=1; $i<= empitr;$i++)
			{
				if($('#efficency'+$i).length !=0){
					var hour=Number(document.getElementById('hours'+$i).value);
					var eff= Number(document.getElementById('efficency'+$i).value);
				}
				
				
				if(hour != "" && eff != "")
			    {
					if($('#efficency'+$i).length ==0)
					{
						continue;
					}
					
					totalworkerhour += ((eff*hour)/100);
					//alert(totalworkerhour);
				}
			}
			
			$("#totalworkerhour").html(totalworkerhour);
			$("#totalworkerhour1").val(totalworkerhour);
			 
			totalworkerhour = 0;
		}
		
	
	
	///////////////////////// function addassets////////////////////////////	
		var countmydataasset=function()
		{
			
		var totalassethour=0;
		var numericExpression=  /^(?:[1-9]\d*|0)?(?:\.\d+)?$/; 
		
		 $assetitr= Number(document.getElementById('assetsitr').value);
		 for($i=1;$i<=$assetitr;$i++)
		 {
			 
			 if($('#wefficency'+$i).length !=0)
			 {
				 var assethour=$("#whour"+$i).val();
				 var asseteff=$("#wefficency"+$i).val();
			 }
			
			if($("#whour"+$i).val() != "" && $("#wefficency"+$i).val()!= "")	 
			    {
				
					if($('#wefficency'+$i).length ==0)
					{
						continue;
					}
					
			 totalassethour +=(assethour*asseteff)/100;
					
			
		 }
		 $("#totalassethour").html(totalassethour);
		 $("#totalassethour1").val(totalassethour);
		}
		
		}
		
		///////////////////////////////////validation///////////////////////////

	////////////////////////////////////////////validation to make value of efff =100//////////////
		$(document).on('blur','.effx', function() {
	
			var numericExpression=  /^(?:[1-9]\d*|0)?(?:\.\d+)?$/; 

				if(this.value.match(numericExpression))
				 {
					 this.style.backgroundColor = "white";
					if (this.value > 100)
					 {
						////alert 100
						 this.value = 100; 
						// countmydataworker();
						//alert("لابد ان يكون الرقم من 0 الى 100");
						this.style.backgroundColor = "yellow";
						countmydataworker();
						
					 }
					else if (this.value <  0)
					{
						this.value = 0;
		
						this.style.backgroundColor = "yellow";
					}
				 }
				
	});
//////////////////////////make select take value/////////////


$(document).on('blur','.effasset', function() {
	
			var numericExpression=  /^(?:[1-9]\d*|0)?(?:\.\d+)?$/; 

				if(this.value.match(numericExpression))
				 {
					 this.style.backgroundColor = "white";
					if (this.value > 100)
					 {
						////alert 100
						 this.value = 100; 
						// countmydataworker();
						//alert("لابد ان يكون الرقم من 0 الى 100");
						this.style.backgroundColor = "yellow";
						countmydataasset();
						
					 }
					else if (this.value <  0)
					{
						this.value = 0;
		
						this.style.backgroundColor = "yellow";
					}
				 }
				
	});
/*	function chechspary() {
			$noofworkers=Number(document.getElementById('stationitr').value);
			$noofasset= Number(document.getElementById('assetsitr').value);
			
			//alert($noofworkers);
		for($i=1;$i<$noofworkers+1;$i++)
			{
				if($("#workname"+$i+"").length != 0)
				{
					$workname=Number($("#workname"+$i+"").val());
					
					if($workname == -1 )
							{
							   document.getElementById("submit").disabled=false;
							//event.preventDefault();//will prevent default form submission
							alert("من فضلك ادخل الموظف");
						}
				}
					
			}
			
				//alert($noofworkers);
		for($j=1;$j<$noofasset+1;$j++)
			{
				$assetname=Number($("#asset"+$j+"").val());
				
				if($assetname == -1)
						{
							   document.getElementById("submit").disabled=false;
							//event.preventDefault();//will prevent default form submission
							alert("من فضلك ادخل المكينه");
						}
					
			}
			
			
			
			
	}
	function validateselect()
	{
			$noofworkers=Number(document.getElementById('stationitr').value);
			$noofasset= Number(document.getElementById('assetsitr').value);
			
			//alert($noofworkers);
		for($i=1;$i<$noofworkers+1;$i++)
			{
				$workname=Number($("#workname"+$i+"").val());
				
					for($j=1;$j<$noofasset+1;$j++)
					{
						
						$assetname=Number($("#asset"+$j+"").val());
						
						if($workname == -1 || $assetname == -1)
						{
							event.preventDefault();//will prevent default form submission
							alert("من فضلك ادخل الاسم");
						}
						else
						{
						$.ajax({
    						 url : "workstation.php?do=add",
							});
						return true;
						}
				
					}
	}
	}*/
			
});