// JavaScript Document

function SaveStockTake(action)
{
	var AfterSaveStockTake = function(oXML)
	{
		var ReturnStr = oXML.responseText;
		alert(ReturnStr);
		regularCursor();
	}
	if (ValidateStockTake())
	{
		var data = 'action='+ action +'&' + fetchInputFormData('frmStockTake'); 
		var thePage = "/modules/warehouse/stocktake/processstocktake.asp"
		hiddenSubmit(thePage,data, AfterSaveStockTake, 0, null, null,true);
	}
}

function ValidateStockTake()
{
	if (document.getElementById('txtStockTakeDesc').value == '')
	{
		alert('Please enter a valid Stock Take Description');
		document.getElementById('txtStockTakeDesc').focus();
		return false;
	}
	else
	{
		var ObjSelStorage = document.getElementById('lstSelectedStorage'); 
		if(ObjSelStorage.options.length <= 0)
		{
			alert('Please select at least one Storage for the Stock Take');
			return false;
		}
		else
		{
			for(var k=0;k<ObjSelStorage.options.length;k++)
			{
				ObjSelStorage.options[k].selected = true;
			}
			return true;	
		}
	}
}

function SaveStockTakeStorageArea(action)
{
	var AfterSaveStockTakeArea = function(oXML)
	{
		var ReturnStr = oXML.responseText;
		alert(ReturnStr);
		regularCursor();
	}
	if (ValidateStockTakeArea())
	{
		var data = 'action='+ action +'&' + fetchInputFormData('frmStockTakeStorage') + '&StockTakeID=' + document.getElementById('StockTakeID').value + '&StorageAreaID=' + document.getElementById('StorageAreaID').value; 
		//alert(data);
		var thePage = "/modules/warehouse/stocktake/processstocktake.asp";
		hiddenSubmit(thePage,data, AfterSaveStockTakeArea, 0, null, null,true);
	}
}


function SaveStockTakeCycleStock(action)
{
	var AfterSaveStockTakeArea = function(oXML)
	{
		var ReturnStr = oXML.responseText;
		alert(ReturnStr);
		regularCursor();
	}
	if (ValidateStockTakeCycle())
	{
		var data = 'action='+ action +'&' + fetchInputFormData('frmStockTakeCycleStock') + '&StockTakeID=' + document.getElementById('StockTakeID').value + '&UserID=' + document.getElementById('UserID').value; 
		//alert(data);
		var thePage = "/modules/warehouse/stocktake/processstocktake.asp";
		hiddenSubmit(thePage,data, AfterSaveStockTakeArea, 0, null, null,true);
	}
}

function SaveStockTakeStorageAreaStock(action,PageMode)
{
	var AfterSaveStockTakeAreaStock = function(oXML)
	{
		var ReturnStr = oXML.responseText;
		//var tabs = new AW.UI.Tabs;
		//tabs.setId("myTabs");
		alert(ReturnStr);
		var data = 'action=SubmitPage&StockTakeID=' + document.getElementById('StockTakeID').value + '&StorageAreaID=' + document.getElementById('StorageAreaID').value; 
		var thePage = "/modules/warehouse/stocktake/edStockTakeStorageArea.asp";
		var OutputDiv = 'StockTakeStorageArea';
		if(PageMode == 'OverView')
		{
			thePage = "/modules/warehouse/stocktake/edStockTakeOverview.asp";
			OutputDiv = 'bottomcontent';
		}		
		hiddenSubmit(thePage, data, completedSingleLoad, 1, OutputDiv, null, false);
		if(PageMode != 'OverView')
		{
			document.getElementById('StockTakeStorageArea').style.display = "block";
			document.getElementById('StockTakeFeedback').style.display = "none";
		}
		//tabs.setSelectedItems(0);
		regularCursor();
	}
	if (ValidateStockTakeStock())
	{
		var FormName = 'frmStockTakeAreaStock'
		if(PageMode == 'OverView')
		{
			FormName = 'frmStockTakeOverview'
		}
		var data = 'action='+ action +'&' + fetchInputFormData(FormName);
		if(PageMode != 'OverView')
		{
			data = data + '&StockTakeID=' + document.getElementById('StockTakeID').value + '&StorageAreaID=' + document.getElementById('StorageAreaID').value; 
		}
		//alert(data);
		var thePage = "/modules/warehouse/stocktake/processstocktake.asp";
		hiddenSubmit(thePage,data, AfterSaveStockTakeAreaStock, 0, null, null,true);
	}
}

function ChangeStorageArea()
{
	var data = 'action=SubmitPage&StockTakeID=' + document.getElementById('StockTakeID').value + '&StorageAreaID=' + document.getElementById('StorageAreaID').value; 
	//alert(data);
	var thePage = "/modules/warehouse/stocktake/edStockTakeAreaStock.asp";
	hiddenSubmit(thePage, data, completedSingleLoad, 1, 'StockTakeFeedback', null, false);
}


function ValidateStockTakeArea()
{
	var ObjSelStorageStock = document.getElementById('lstSelectedStorageAreaStock'); 
	if(ObjSelStorageStock.options.length <= 0)
	{
		alert('Please select at least one Storage Area Stock for the Stock Take');
		return false;
	}
	else
	{
		for(var k=0;k<ObjSelStorageStock.options.length;k++)
		{
			ObjSelStorageStock.options[k].selected = true;
		}
		return true;	
	}
}

function ValidateStockTakeCycle()
{
	var ObjSelStock = document.getElementById('lstSelectedStock'); 
	if(ObjSelStock.options.length <= 0)
	{
		alert('Please select a valid list of Stock for the Stock Take');
		return false;
	}
	else
	{
		for(var k=0;k<ObjSelStock.options.length;k++)
		{
			ObjSelStock.options[k].selected = true;
		}
		return true;	
	}
}

function ValidateStockTakeStock()
{
	var ObjStartDate = document.getElementById('txtStartDate'); 
	var ObjFinishDate = document.getElementById('txtFinishDate'); 
	if(ObjStartDate.value == '')
	{
		alert('Please select a valid Start Date for the Stock Take');
		return false;
	}
	else if(ObjFinishDate.value == '')
	{
		alert('Please select a valid Finish Date for the Stock Take');
		return false;
	}	
	else
	{
		var valid = true;
		var RecordCount = document.getElementById('RecordCount').value;
		for(var k=0;k<RecordCount;k++)
		{
			if (document.getElementById('txtQty'+k).value == '')
			{
				alert('Please enter a valid Stock Take Quantity');
				document.getElementById('txtQty'+k).focus();
				valid = false;
				break;
			}
		}
		if(valid == true)
		{
			return true;	
		}
	}
}

function CheckForStock(){
	var AfterSearchStock = function(oXML) {			
		var result = oXML.responseText;
		regularCursor();
	}
	var ObjStorageArea = document.getElementById('selStorageArea');
	var StorageAreaID = ObjStorageArea.options[ObjStorageArea.selectedIndex].value;
	var gridpage = "/modules/warehouse/stocktake/stocksearchgrid.asp?StorageAreaID="+StorageAreaID;

	popUpStockSearch(gridpage);
}

function popUpStockSearch(page){
	var returnid = '';
	var searchvalue = 'A1';
	returnid = openModalWindow(page,750,600,searchvalue);
	if(returnid != '' && returnid != 'undefined' && returnid != null)
	{
		AssignStockSearch(returnid);
	}
}

function AssignStockSearch(ReturnStr)
{
	var ArrRet = ReturnStr.split('|^^|');
	var StockID = ArrRet[0];
	var UOMID = ArrRet[1];
	var StockTakeID = document.getElementById('StockTakeID').value
	var UserID = document.getElementById('UserID').value
	var StorageAreaID = document.getElementById('selStorageArea').value;
	var Accepted = document.getElementById('Accepted').value;
	var AfterAddStock = function(oXML)
	{
		var ReturnStr = oXML.responseText;
		alert(ReturnStr);
		regularCursor();
	}

	var data = 'action=addstocktostocktake&StockTakeID=' + StockTakeID + '&UserID=' + UserID + '&selStorageArea=' + StorageAreaID + '&StockID=' + StockID + '&UOMID=' + UOMID + '&Accepted=' + Accepted; 
	//alert(data);
	var thePage = "/modules/warehouse/stocktake/edStockTakeAreaStock.asp";
	hiddenSubmit(thePage, data, completedSingleLoad, 1, 'StockTakeFeedback', null, false);
}

function AcceptStockTake()
{
	var AfterAcceptStockTake = function(oXML)
	{
		var ReturnStr = oXML.responseText;
		alert(ReturnStr);
		regularCursor();
	}
	if (ValidateStockTakeUpdate())
	{
		if(confirm('Accepting this stock take will update the system with immediate effect. Are you sure you want to continue?'))
		{
			var data = 'action=acceptstocktake&' + fetchInputFormData('frmStockTakeOverview'); 
			var thePage = "/modules/warehouse/stocktake/processstocktake.asp";
			//alert(data);
			hiddenSubmit(thePage,data, AfterAcceptStockTake, 0, null, null,true);
		}
	}
}

function SingleAcceptStockTake(StorageAreaID, StockTakeCountID, UserID)
{
	var AfterAcceptStockTake = function(oXML)
	{
		var ReturnStr = oXML.responseText;
		alert(ReturnStr);
		regularCursor();
	}
	if(confirm('Accepting this stock take will update the system with immediate effect. Are you sure you want to continue?'))
	{
		var data = 'action=singleacceptstocktake&' + fetchInputFormData('frmStockTakeBulkAccept') + '&STAID=' + StorageAreaID + '&STCID=' + StockTakeCountID + '&UID=' + UserID; 
		var thePage = "/modules/warehouse/stocktake/processstocktake.asp";
		//alert(data);
		hiddenSubmit(thePage,data, AfterAcceptStockTake, 0, null, null,true);
	}
}

function BulkAcceptStockTake()
{
	var thePage = "/modules/warehouse/stocktake/processstocktake.asp";
	
	var AfterAcceptStockTake = function(oXML)
	{
		var ReturnStr = oXML.responseText;
		alert(ReturnStr);
		
		var theSearchValue = document.getElementById('txtSearch').value;
		var StockTakeID = document.getElementById('StockTakeID').value;
		
		var thepage = '/modules/warehouse/stocktake/edStockTakeBulkAccept.asp';
		data = 'action=Search&txtSearch=' + encodeURIComponent(theSearchValue) + '&StockTakeID=' + StockTakeID + '&txtDisplayImages=False';
		hiddenSubmit(thepage, data, completedSingleLoad, 1, 'bottomcontent', null, false);
		regularCursor();
	}
	if(confirm('Accepting this stock take will update the system with immediate effect. Are you sure you want to continue?'))
	{
		var data = 'action=bulkacceptstocktake&' + fetchInputFormData('frmStockTakeBulkAccept'); 
		//alert(data);
		hiddenSubmit(thePage,data, AfterAcceptStockTake, 0, null, null,true);
	}
}


function GenerateStockTake(StockTakeID, StorageAreaID, StockTakeCountID, PageMode)
{
	var thepage = "/modules/Warehouse/StockTake/edGenerateStockTake.asp";
	returnid = openModalWindow(thepage,520,150,'');
	if(returnid != '' && returnid != 'undefined' && returnid != null)
	{
		data = "action=generatestocktakeareastock&StockTakeID="+StockTakeID+'&StorageAreaID='+StorageAreaID+'&StockTakeCountID='+StockTakeCountID+'&UserID='+returnid;
		//alert(data);
		thepage = "/modules/Warehouse/StockTake/processstocktake.asp";
		hiddenSubmit(thepage, data, alertHiddenResponse, 1, 'topcontent', null, false)
		var data = 'action=SubmitPage&StockTakeID=' + StockTakeID + '&StorageAreaID=' + StorageAreaID; 
		var thePage = "/modules/warehouse/stocktake/edStockTakeStorageArea.asp";
		var OutputDiv = 'StockTakeStorageArea';
		if(PageMode == 'OverView')
		{
			thePage = "/modules/warehouse/stocktake/edStockTakeOverview.asp";
			OutputDiv = 'bottomcontent';
		}
		
		hiddenSubmit(thePage, data, completedSingleLoad, 1, OutputDiv, null, false);
			
		if(PageMode != 'OverView')
		{
			document.getElementById('StockTakeStorageArea').style.display = "block";
			document.getElementById('StockTakeFeedback').style.display = "none";
		}
	}
}

function UpdateStockTakeCount()
{
	var AfterUpdateStockTakeCount = function(oXML)
	{
		var ReturnStr = oXML.responseText;
		alert(ReturnStr);
		var data = 'StockTakeID=' + document.getElementById('StockTakeID').value + '&StorageAreaID=' + document.getElementById('StorageAreaID').value; 
		var thePage = "/modules/warehouse/stocktake/edStockTakeOverview.asp";
		hiddenSubmit(thePage, data, completedSingleLoad, 1, 'bottomcontent', null, false);
		regularCursor();
	}
	if (ValidateStockTakeUpdate())
	{
		var data = 'action=UpdateStockTakeCount&' + fetchInputFormData('frmStockTakeOverview');
		//alert(data);
		var thePage = "/modules/warehouse/stocktake/processstocktake.asp";
		hiddenSubmit(thePage,data, AfterUpdateStockTakeCount, 0, null, null,true);
	}
}

function ValidateStockTakeUpdate()
{
	var valid = true;
	var RecordCount = document.getElementById('RecordCount').value;
	for(var k=0;k<RecordCount;k++)
	{
		if (document.getElementById('txtQty'+k).value == '')
		{
			alert('Please enter a valid Stock Take Quantity');
			document.getElementById('txtQty'+k).focus();
			valid = false;
			break;
		}
	}
	return valid;	
}

function ShowHideStock(StockID, inputimg,Counter)
{
	var rowids = document.getElementById('NRowLocID').value;
	var ArrRow = rowids.split('|');
	var SpaceCount = document.getElementById('SpaceLoc_'+Counter).value
	var CellID = 0;
	var DivTopMargin = document.getElementById('RowSpacer').style.marginTop;
	DivTopMargin = DivTopMargin.replace('px','');
	//alert(document.getElementById('row_'+StockID).style.display)
	//alert(document.getElementById('td_RowSpacer').height);
	if(inputimg.src.indexOf('plus') > 1)	
	{
		for(var k =0;k<ArrRow.length;k++)
		{
			ArrRowP1 = ArrRow[k].split('-');
			for(var p =0;p<ArrRowP1.length;p++)
			{
				ArrRowP2 = ArrRowP1[p].split('_');
				if(ArrRowP2[1] == StockID)
				{
					document.getElementById(ArrRowP1[p]).style.display="block";
				}
			}
		}
		//alert('show:' + eval((eval(DivTopMargin) + eval((SpaceCount * 26)))));
		document.getElementById('RowSpacer').style.marginTop = eval((eval(DivTopMargin) + eval((SpaceCount * 30)))) + 'px';
		//(SpaceCount * 5)+'px';
		//document.getElementById('td_RowSpacer').height = '200px'
		inputimg.src = '/images/minus.gif';
		inputimg.alt = 'Collaps'
	}
	else
	{
		for(var k =0;k<ArrRow.length;k++)
		{
			ArrRowP1 = ArrRow[k].split('-');
			for(var p =0;p<ArrRowP1.length;p++)
			{
//				alert(ArrRowP1[p])
				ArrRowP2 = ArrRowP1[p].split('_');
				if(ArrRowP2[1] == StockID)
				{
					document.getElementById(ArrRowP1[p]).style.display="none";
				}
			}
		}	
		//alert(eval((eval(DivTopMargin) - eval((SpaceCount * 26)))));
		document.getElementById('RowSpacer').style.marginTop = eval((eval(DivTopMargin) - eval((SpaceCount * 30)))) + 'px';
		//document.getElementById('td_RowSpacer').height = '20px';	
		inputimg.src = '/images/plus.gif';
		inputimg.alt = 'Expand'
	}
}


function StockTakeStandardSearch(DisplayImages, e)
{
	var evt = e || window.event;
  	var keyPressed = evt.which || evt.keyCode;
	
	var theSearchValue = document.getElementById('txtSearch').value;
	var StockTakeID = document.getElementById('StockTakeID').value;
	
	var thepage = '/modules/warehouse/stocktake/edStockTakeBulkAccept.asp';
	var data = 'action=Search&txtSearch=' + encodeURIComponent(theSearchValue) + '&StockTakeID=' + StockTakeID + '&txtDisplayImages=' + DisplayImages;
	//alert(data);
	if(evt.type == 'keypress')
	{
		if(keyPressed == 13)
		{
			hiddenSubmit(thepage, data, completedSingleLoad, 1, 'bottomcontent', null, false);
		}
	}
	else
	{
		hiddenSubmit(thepage, data, completedSingleLoad, 1, 'bottomcontent', null, false);
	}
}