/*
    Author          : Saqib Gill
    Date Created    : 28-July-2009
    Date Changed    : 
*/
function ApproveStockItem()
{
    var Status;
    /*if (document.getElementById("Status").checked == 1)
    {
        Status = "1"
    }
    else
    {
        Status = "0"
    }*/
	var thepage = "/modules/Warehouse/Stock/StockApprovalProcess.asp";
	var lstSelected = document.getElementById("lstSelected");
    for(var i=0;i<lstSelected.options.length;i++)
    {
	    lstSelected.options[i].selected = true;	
    }	
	
	var ApproveItem = function (oXML) 
	{
		var result = oXML.responseText;
	    //document.getElementById("topcontent").innerHTML = result;
        //return;
		regularCursor();
	}	
	if (confirm('Are you sure you want to make approval mendatory for this item?'))
	{
	    /*if (validateAType())
	    {*/
	        var data = "action=ApproveStockItem&" + fetchInputFormData('frmStockApproval');
		    hiddenSubmit(thepage, data, ApproveItem, 1,'topcontent', null, false)
		    checkAction(102,272,1,null,false,null,false);
		/*}*/
	}
}

function validateAType()
{
	
	var Count = 0;
	var lstSelected = document.getElementById("lstSelected");
	for(var i=0;i<lstSelected.options.length;i++)
	{
		if(lstSelected.options[i].selected == true)	
		{
			Count++;
		}
	}	
	if (Count == 0)
	{
		alert('Please select atleast one approval type for current Stock Item.');
	}
	else
	{
	return true;
	}
}
function ApproveOrderItem(Approved)
{

   var Message = '';
   var Reason = '';
   if(document.getElementById("CheckStatus").value == "Pending")
    {
    	Message = 'Certain items on this order is subject to a content approval and should not be ordered at this stage. Click OK to approve.'
    }
	else
	{
		if(Approved == 1)
		{
			Message = 'Are you sure you want to approve this purchase order?'
		}
		else
		{
			Reason = document.getElementById('txtDisapproveReason').value;
			Message = 'Are you sure you want to disapprove this purchase order?'
		}
	}
	//alert(document.getElementById("CheckStatus").value);
	var thepage = "/modules/Warehouse/Stock/OrderApprovalProcess.asp";
	var ApproveItem = function (oXML) 
	{
		var result = oXML.responseText;
		alert(result);
		regularCursor();
	}
	
	if(Approved == 0 && Reason == '')
	{
		alert('Please enter a valid reason for disapproving order.');
		document.getElementById('txtDisapproveReason').focus();
	}
	else
	{
		if (confirm(Message))
		{
	
			var data = "action=ApproveOrderItem&ApprovedType="+ Approved +"&" + fetchInputFormData('frmOrderApproval');
			hiddenSubmit(thepage, data, ApproveItem, 1,'topcontent', null, false)
			checkAction(103,280,1,null,false,null,false);
	
		}
	}
}

function PrintPurchaseOrder()
{
    for (i=0; i < document.getElementById("itemcount").value; i++) 
    {
        if(document.getElementById("StockApproved"+i).value == "False")
        {
        alert('you needs content / Item order approval, for print this order');
        return;
        }
    }
    if(document.getElementById("ValApproved").value == "False")
    {
        alert('you needs value order approval, for print this order');
        return;
    }
    
	var thepage = "/modules/Warehouse/Stock/OrderApprovalProcess.asp";
	var PrintItem = function (oXML) 
	{
		var result = oXML.responseText;
	    //document.getElementById("topcontent").innerHTML = result;
        //return;
		regularCursor();
	}	
	if (confirm('Are you sure you want to print this order ?'))
	{

        var data = "action=PrintOderItem&" + fetchInputFormData('frmOrderApproval');
	    hiddenSubmit(thepage, data, PrintItem, 1,'topcontent', null, false)

	}
}

function UpdateOrderItemQty(InputQty,StockID,OrderID, StockCount, UnitPrice, PriceDiv)
{
    var Quantity = InputQty.value;
	if(Quantity == '')
	{
		Quantity = 0;	
	}
	
	if (Quantity > StockCount)
	{
		alert("The warehouse hasn't got sufficient stock to fulfill this request. Please reduce your quantity");
		InputQty.focus();
	}
	else
	{
		var thepage = "/modules/Warehouse/Stock/ItemApprovalProcess.asp";
		var UpdateItem = function (oXML) 
		{
			var result = oXML.responseText;
			//alert(result);
			if (result != '')
			{
				var PriceValue = (Quantity * UnitPrice);
				var SubTotal = Math.round(result);
				var Vat = (SubTotal * .14);
				var Total = Math.round(SubTotal + Vat);
				
				document.getElementById(PriceDiv).innerHTML = 'R ' + PriceValue + '.00';
				document.getElementById('spSubTotal').innerHTML = 'R ' + CurrencyFormatted(SubTotal);
				var Vat = document.getElementById('spVat').innerHTML = 'R ' + CurrencyFormatted(Vat);
				var Total = document.getElementById('spTotal').innerHTML = 'R ' + CurrencyFormatted(Total);	
			}
			regularCursor();
		}	
		var data = "action=UpdateOrderItem&StockID=" + StockID + "&Qty=" + Quantity + "&OrderID=" + OrderID;	
		//alert(data);
		hiddenSubmit(thepage, data, UpdateItem, 1,'topcontent', null, false)
		//checkAction(103,280,1,null,false,null,false);
	}
}

function ApprovedItems()
{
    var thepage = "/modules/Warehouse/Stock/ItemApprovalProcess.asp";
    var UpdateItem = function (oXML) 
    {
	    var result = oXML.responseText;
        //document.getElementById("topcontent").innerHTML = result;
        //return;
		alert(result);
	    regularCursor();
    }	
    
    if (confirm('Are you sure you want to approve this order ?'))
	{
        var data = "action=ApprovedItems&" + fetchInputFormData('frmItemApproval');	
        //alert(data);
        hiddenSubmit(thepage, data, UpdateItem, 1,'topcontent', null, false)
        //checkAction(103,280,1,null,false,null,false);
    }
}

function PrintPickingSlip(reportURL)
{
	reportURL = reportURL + '&orderid=' + document.getElementById('OrderID').value;
	window.open(reportURL, "_blank", "toolbar=no,menubar=no,location=no,resizable=yes")	
}

function StartPickingSlip()
{
    var thepage = "/modules/Warehouse/Stock/ItemApprovalProcess.asp";
    var UpdateItem = function (oXML) 
    {
	    var result = oXML.responseText;
		//alert(result);
		if(result == 'success')
		{
			alert('Picking process has been assigned successfully and started.');
		}
	    regularCursor();
    }	
	
	if (confirm('Are you sure you want to start the picking process of this order ?'))
	{
		document.getElementById('action').value = 'StartPickingSlip';
		var data = fetchInputFormData('frmEditOrderItem');
		//alert(data);
		hiddenSubmit(thepage, data, UpdateItem, 1,'topcontent', null, false);
		checkAction(113,248,2,null,false,null,false);
	}	
}

function OrderPicked()
{
    var thepage = "/modules/Warehouse/Stock/ItemApprovalProcess.asp";
    var UpdateItem = function (oXML) 
    {
	    var result = oXML.responseText;
		alert(result);
		checkAction(113,248,2,null,false,null,false);
	    regularCursor();
    }	
	if (document.getElementById('txtReceivedDate').value == '')
	{
		alert('Please select valid Received Date of the Picking Slip.');	
	}
	else
	{
		document.getElementById('action').value = 'OrderPicked';
		var data = fetchInputFormData('frmEditOrderItem');	
		hiddenSubmit(thepage, data, UpdateItem, 1,'topcontent', null, false)
	}
}

function CheckingProcess()
{
    var thepage = "/modules/Warehouse/Stock/ItemApprovalProcess.asp";
    var UpdateItem = function (oXML) 
    {
	    var result = oXML.responseText;
		alert(result);
	    regularCursor();
    }	
	document.getElementById('action').value = 'CheckingProcess';
	var data = fetchInputFormData('frmEditOrderItem');	
	hiddenSubmit(thepage, data, UpdateItem, 1,'topcontent', null, false)
	checkAction(113,248,2,null,false,null,false);
}

function OrderChecked()
{
    var thepage = "/modules/Warehouse/Stock/ItemApprovalProcess.asp";
    var UpdateItem = function (oXML) 
    {
	    var result = oXML.responseText;
		alert(result);
		checkAction(113,248,2,null,false,null,false);
	    regularCursor();
    }	
	
	if (document.getElementById('txtEndChecking').value == '')
	{
		alert('Please select valid End Checking Date of the Order.');	
	}
	else
	{
		document.getElementById('action').value = 'OrderChecked';
		var data = fetchInputFormData('frmEditOrderItem');	
		hiddenSubmit(thepage, data, UpdateItem, 1,'topcontent', null, false);
	}
}

function FinalizeOrder()
{
    var thepage = "/modules/Warehouse/Stock/ItemApprovalProcess.asp";
    var UpdateItem = function (oXML) 
    {
	    var result = oXML.responseText;
		//alert(result)
		if(result != 'Success')
		{
			alert(result);			
		}
		else
		{
			alert('Order has been finalized successfully.');
			checkAction(113,248,2,null,false,null,false);
		}
	    regularCursor();
    }	
	document.getElementById('action').value = 'FinalizeOrder';
	var data = fetchInputFormData('frmEditOrderItem');	
	hiddenSubmit(thepage, data, UpdateItem, 1,'topcontent', null, false);
	
}

function OrderDispatched()
{
    var thepage = "/modules/Warehouse/Stock/ItemApprovalProcess.asp";
    var UpdateItem = function (oXML) 
    {
	    var result = oXML.responseText;
		alert(result);
	    regularCursor();
    }	
	
	document.getElementById('action').value = 'OrderDispatched';
	var data = fetchInputFormData('frmEditOrderItem');	
	hiddenSubmit(thepage, data, UpdateItem, 1,'topcontent', null, false);
	checkAction(113,248,2,null,false,null,false);
}

function SaveOrderApprovalQty(OrderID,StockID,UOMID, Count,PriceDiv)
{
	var Quantity = document.getElementById('txtPurchaseQty_'+Count).value;
	var UnitPrice = document.getElementById('txtUnitPrice_'+Count).value;
	
	if(Quantity == '')
	{
		Quantity = 0;	
	}
	
	//alert(PriceDiv)
	var AfterSaveBasket = function (oXML) {
		var retvalue = oXML.responseText;
//		alert(retvalue)
		if (retvalue != '')
		{
			var PriceValue = (Quantity * UnitPrice);
			var SubTotal = Math.round(retvalue);
			var Vat = (SubTotal * .14);
			var Total = Math.round(SubTotal + Vat);
			
			document.getElementById(PriceDiv).innerHTML = 'R ' + CurrencyFormatted(Math.round(PriceValue));
			document.getElementById('spSubTotal').innerHTML = 'R ' + CurrencyFormatted(SubTotal);
			var Vat = document.getElementById('spVat').innerHTML = 'R ' + CurrencyFormatted(Vat);
			var Total = document.getElementById('spTotal').innerHTML = 'R ' + CurrencyFormatted(Total);	
		}
		regularCursor();
	}
	
	var data = 'action=UpdateApprovalQty&PurchaseID=' + OrderID + '&StockID=' + StockID + '&UOMID=' + UOMID + '&Qty=' + Quantity + '&UnitPrice=' + UnitPrice;	
	//alert(data);
	var thepage = "/modules/warehouse/stock/OrderApprovalProcess.asp";
	hiddenSubmit(thepage, data, AfterSaveBasket, 1, 'topcontent', null, false)
	//alert(document.getElementById(PriceDiv).innerHTML);
}


function EmailPurchaseOrder(OrderID)
{
	var AfterSendEmail = function (oXML) {
	var retvalue = oXML.responseText;
		//alert(retvalue)
		if (retvalue != '')
		{
			if(retvalue.indexOf('true') > 1)
			{
				alert('Your Purchase Order Email has been queued successfully.');	
			}
		}
		regularCursor();
	}
	
	var data = 'action=SendOrderEmail&PurchaseID=' + OrderID;	
	var thepage = "/modules/warehouse/stock/OrderApprovalProcess.asp";
	hiddenSubmit(thepage, data, AfterSendEmail, 1, 'topcontent', null, false)
}
