// JavaScript Document
/////////////////////////////////////////////////////////////////////////////////////////////
//                      modal 1 : CheckReport  (من الاستقبال)                               //
/////////////////////////////////////////////////////////////////////////////////////////////

var thisInitialCost=0;
var thisOurSerial = '';
var thisCheckId=0;
var thisDetailId=0;

 $(".check").click(function(){
	 $i = this.id;
	 //alert($(this).attr('data-initialcost'));
	 thisInitialCost=$(this).attr('data-initialcost');
	 thisOurSerial=$(this).attr('data-ourSerial');
	 thisCheckId=$(this).attr('data-checkId');
	 thisDetailId=$(this).attr('data-detailId');
	
	 showcheckReportModalBox($i);
	 
	
 });
	 
 $(".closeReport").click(function(){
	 $i = this.id.substr(1);
	 //alert($i);
	 closeCheckReportModal($i);
 });
	 
 $('.insertcheckReport').click(function(e) {
	// if(document.getElementById('initialmaintenance').style.display=='block')
	 
    $i = this.id.substr(1);
	//alert($i);
	insertcheckReport($i);
});
 
 $("#nextStage").change(function(e) {
    $nextStage=document.getElementById('nextStage').value;
	
	//initialmaintenance
	if($nextStage==1)
	{
		document.getElementById('initialmaintenance').style.display="block";
		document.getElementById('externalmaintenance').style.display="none";
		$.ajax({
			type: 'POST',
			url: 'checkReportControllerAjax.php?do=loadProperities',
			//data: $("#supplierInfo").serialize(),
			success: function(data) {
			//alert(data);
				if(data != null)
				{
					$("#propertyDiv").html(data);
					
					$(document).on('keyup', '.calTotal', function(){ 
						calcTotal();
					});
					
					$(document).on('click', '.calTotalcheckbox', function(){ 
						calcTotal();
					});
					
				}
				
			}
        });
		
	}
	//externalmaintenance
	else if($nextStage==2)
	{
		document.getElementById('initialmaintenance').style.display="none";
		document.getElementById('externalmaintenance').style.display="block";
	}
	else
	{
		document.getElementById('initialmaintenance').style.display="none";
		document.getElementById('externalmaintenance').style.display="none";
	}
});

function calcTotal()
{
	/*$rows=Number(document.getElementById('noOfProperties').value);
	for($)*/
	$total=0;
	$('.calTotal').each(function(index, element) {
		$thecolnmu=this.id.substr(4);
		$mycheckbox=document.getElementById("checkboxNo"+$thecolnmu).checked;
        if($mycheckbox)
		{
			$total+=Number(document.getElementById("cost"+$thecolnmu).value);
		}
    });
	document.getElementById("total").innerHTML=$total;
	document.getElementById("total_hidden").value=$total;
}
 
 
////// Adding new check report with modal box /////////////

function insertcheckReport($i)
{
	//alert('checkReportControllerAjax.php?do=insertcheckReport&type='+$i+'&ourSerial='+thisOurSerial+'&detailId='+thisDetailId+'&checkId='+thisCheckId);
	$.ajax({
			type: 'POST',
			url: 'checkReportControllerAjax.php?do=insertcheckReport&type='+$i+'&ourSerial='+thisOurSerial+'&detailId='+thisDetailId+'&checkId='+thisCheckId,
			data: $("#checkReportInfo"+$i).serialize(),
			success: function(data) {
				//alert(data);
				
				location.reload(); 
					/*$('#checkReportModal'+$i).modal('hide');
					//empty modal box's inputs' values
					emptycheckReportModalForm($i);*/
				
			}
				
			
        });
}

function showcheckReportModalBox($i)
{
	$('#checkReportModal'+$i).modal('show');
	emptycheckReportModalForm($i);
	document.getElementById('initialCost'+$i).innerHTML=thisInitialCost;

}


function emptycheckReportModalForm($i)
{
	$('#checkReportInfo'+$i).find(':input').each(function(){
		var type = this.type, tag = this.tagName.toLowerCase();
		if (type == 'text' || type == 'password' || tag == 'textarea')
			this.value = '';
		else if (type == 'checkbox' || type == 'radio')
			this.checked = false;
		else if (tag == 'select')
		{
			//needed it foe the 1st modal 
			if($i==1)
			{
				$('#supplier').val(-1).change();// it triggers the change event as well
				$('#nextStage').val(-1).change();// it triggers the change event as well
			}
			 //needed it foe the 2nd modal 
			 if($i==2)
			 {
				$('#supplier').val(-1).change();// it triggers the change event as well
				$('#otherNextStage').val(-1).change();// it triggers the change event as well
				document.getElementById('OtherNextStageDiv').style.display='none';
				document.getElementById('HandelDiv').style.display='none';
			 }
			//this.value = '-1';
			//$("#"+this.id).select2("data", {id: '-1', text: "اختر المرحله التاليه"});
		}
		//document.getElementById('initialmaintenance').style.display="none";
		//document.getElementById('externalmaintenance').style.display="none";	
	});
}


function closeCheckReportModal($i)
{
	$('#checkReportModal'+$i).modal('hide');
	emptycheckReportModalForm($i);
}



/////////////////////////////////////////////////////////////////////////////////////////////
//                             modal 2 : OthersCheckReport                                 //
/////////////////////////////////////////////////////////////////////////////////////////////

$("[name=delevery]").change(function(e) {
     if(this.value==0)
	 {
		document.getElementById('HandelDiv').style.display='block';
		document.getElementById('HandelDiv2').style.display='none';
		document.getElementById('OtherNextStageDiv').style.display='none';
		document.getElementById('otherInitialmaintenance').style.display='none';
		document.getElementById('otherExternalmaintenance').style.display='none';
		$.ajax({
			type: 'POST',
			url: 'checkReportControllerAjax.php?do=loadCosts&ourSerial='+thisOurSerial,
			//data: $("#checkReportInfo"+$i).serialize(),
			success: function(data) {
				//alert(data);
				$("#allCostsDiv").html(data);
					
				
			}
				
			
        });
		
	 }
	 else if(this.value==1)
	 {
		document.getElementById('HandelDiv').style.display='none';
		document.getElementById('HandelDiv2').style.display='none';
		document.getElementById('OtherNextStageDiv').style.display='block';
		
		$('#supplier').val(-1).change();
		$('#otherNextStage').val(-1).change();
		
		$("#otherNextStage").change(function(e) {
				$nextStage=document.getElementById('otherNextStage').value;
				
				//initialmaintenance
				if($nextStage==1)
				{
					document.getElementById('HandelDiv2').style.display='block';
					document.getElementById('otherInitialmaintenance').style.display="block";
					document.getElementById('otherExternalmaintenance').style.display="none";
					//alert('thisInitialCost='+thisInitialCost);
					$.ajax({
						type: 'POST',
						url: 'checkReportControllerAjax.php?do=loadCosts&ourSerial='+thisOurSerial,
						//data: $("#checkReportInfo"+$i).serialize(),
						success: function(data) {
							//alert(data);
							$("#allCostsDiv2").html(data);
								
							
						}
							
						
					});
					
						
					$.ajax({
						type: 'POST',
						url: 'checkReportControllerAjax.php?do=loadProperities',
						//data: $("#supplierInfo").serialize(),
						success: function(data) {
						//alert(data);
							if(data != null)
							{
								$("#OtherPropertyDiv").html(data);
								
								$(document).on('keyup', '.calTotal', function(){ 
									calcTotal();
								});
								
								$(document).on('click', '.calTotalcheckbox', function(){ 
									calcTotal();
								});
								
							}
							
						}
					});
					
				}
				//externalmaintenance
				else if($nextStage==2)
				{
					document.getElementById('HandelDiv2').style.display='none';
					document.getElementById('otherInitialmaintenance').style.display="none";
					document.getElementById('otherExternalmaintenance').style.display="block";
				}
				else
				{
					document.getElementById('HandelDiv2').style.display='none';
					document.getElementById('otherInitialmaintenance').style.display="none";
					document.getElementById('otherExternalmaintenance').style.display="none";
				}
		});
	 } 
});



/////////////////////////////////////////////////////////////////////////////////////////////
//                                  srtop many submits                                     //
/////////////////////////////////////////////////////////////////////////////////////////////


$("#a2").click(function(ev) {
	
	
// submit more than once return false
$(this).click(function() {
return false;
});
// submit once return true
return true;

});


$("#a1").click(function(ev) {
	
	
	
// submit more than once return false
$(this).click(function() {

return false;
});
// submit once return true


return true;
});
