
function SavePriceList(Action)
{
		
	var AfterSavePriceList = function(oXML) {			
		var result = oXML.responseText;
		regularCursor();
		//prompt('', result);
		alert (result);
	}
	var thepage = '/modules/PriceList/edPriceList.asp';
	if (validatePriceList()){
		var ObjList = document.getElementById('lstSelectedPriceList');
		for(var r=0;r<ObjList.options.length;r++)
		{
			ObjList.options[r].selected = true;
		}		
		ObjList = document.getElementById('lstSelected');
		for(var r=0;r<ObjList.options.length;r++)
		{
			ObjList.options[r].selected = true;
		}
		
		var data = "action=" + Action + "&" + fetchInputFormData('frmPriceList');
		hiddenSubmit(thepage,data, AfterSavePriceList, 0, null, null,true);
		//checkAction(50,210,2,null,false,null,false);
	}  
	
}

function validatePriceList(){
	
	var thereturn;
	thereturn = false;

	if (document.frmPriceList.txtDescription.value.trim() != "")
	{
		thereturn = true;
	}
	else
	{
		alert ("Please enter a valid Stock Quantity before continuing");
	}

	return thereturn;
	
}

function ChangePriceList(Action)
{
	var thepage = '/modules/PriceList/edPriceList.asp';
	var data = "action=RemovePLLink&" + fetchInputFormData('frmPriceList');	
	if(confirm('This is will unlink all Supplier/Center and Products of this Pricelist, are you sure you want to change the type?'))
	{
		var ObjList = document.getElementById('lstSelectedPriceList');
		removeOptions(ObjList, null);	
		ObjList = document.getElementById('lstSelected');
		removeOptions(ObjList, null);			
		hiddenSubmit(thepage, data, completedSingleLoad, 1, 'bottomcontent', null, false);
	}
	else
	{
		var PriceListTypeID = document.getElementById('PriceListTypeID').value;
		var SelPriceListType = document.getElementById('selPriceListTypeID');
		for(var r=0;r<SelPriceListType.options.length;r++)
		{
			if (SelPriceListType.options[r].value == PriceListTypeID)
			{
				SelPriceListType.options[r].selected = true;
				break;
			}
		}		
	}
}

function ShowStock(mode,ObjList,Selection)
{
	var AfterPriceListStock = function(oXML) {			
		var result = oXML.responseText;
		regularCursor();
		//alert(result);
		if (result != '')
		{
			var StockArr = result.split('|');
			
			var ObjAvailStock = document.getElementById('lstAvailable');
			var ObjSelStock = document.getElementById('lstSelected');
			for(var j=0;j<StockArr.length;j++)
			{
				if (StockArr[j] != '' && StockArr[j] != 'undefined')
				{
					var StockValArr = StockArr[j].split('^'); 
					if (mode == 'ShowStock')
					{
						if (StockValArr[1] != '' && StockValArr[1] != 'undefined')
						{		
							
							if(CheckAvail(StockValArr[0], ObjAvailStock) == false)
							{	
								//alert(CheckAvail(StockValArr[0], ObjAvailStock))					
								appendOptions(ObjAvailStock,StockValArr[1],StockValArr[0]);
							}
						}
					}
					else
					{
						removeOptions(ObjAvailStock,StockValArr[0]);	
						removeOptions(ObjSelStock,StockValArr[0]);	
					}
				}
			}
			for (var k=0;k<ObjAvailStock.options.length;k++)
			{
				ObjAvailStock.options[k].selected = false;	
			}	
			if (ObjAvailStock.options.length > 0)
			{
				ObjAvailStock.options[0].selected = true;
			}
			for (var k=0;k<ObjSelStock.options.length;k++)
			{
				ObjSelStock.options[k].selected = false;	
			}	
			if (ObjSelStock.options.length > 0)
			{
				ObjSelStock.options[0].selected = true;
			}			
		}
	}
	
	var ObjSel;
	var thepage = '/modules/PriceList/edPriceList.asp';
	var ListVal = ''; 
	
	if (Selection == 'All')
	{
		for(var r=0;r<ObjList.options.length;r++)
		{
			ObjList.options[r].selected = true;
		}
	}
	for(var r=0;r<ObjList.options.length;r++)
	{
		if(ObjList.options[r].selected == true)
		{
			ListVal = ListVal + ObjList.options[r].value + '|';
		}
	}

	var data = "action="+mode+"&SelectedVal="+ encodeURIComponent(ListVal) +"&" + fetchInputFormData('frmPriceList');
	hiddenSubmit(thepage,data, AfterPriceListStock, 0, null, null,false);
}

function CheckAvail(Val, ObjAvail)
{
	var Check = false;
	for(var t=0;t<ObjAvail.options.length;t++)
	{
		if (ObjAvail.options[t].value == Val)
		{
			Check = true;
			break;
		}
	}
	return Check;
}

function appendOptions(theSel, newText, newValue)
{
  if (theSel.options.length == 0) {
    var newOpt1 = new Option(newText, newValue);
    theSel.options[0] = newOpt1;
    theSel.selectedIndex = 0;
  } else if (theSel.options.length > 0) {
    var selText = new Array();
    var selValues = new Array();
    var selIsSel = new Array();
    var newCount = -1;
    var newSelected = -1;
    var i;
	
	if(theSel.selectedIndex == -1)
	{
		theSel.selectedIndex = theSel.options.length - 1;		
	}
	
    for(i=0; i<theSel.options.length; i++)
    {
      newCount++;
      selText[newCount] = theSel.options[i].text;
      selValues[newCount] = theSel.options[i].value;
      selIsSel[newCount] = theSel.options[i].selected;
      
      if (newCount == theSel.selectedIndex) {
        newCount++;
        selText[newCount] = newText;
        selValues[newCount] = newValue;
        selIsSel[newCount] = false;
        newSelected = newCount - 1;
      }
    }
	
    for(i=0; i<=newCount; i++)
    {
      var newOpt = new Option(selText[i], selValues[i]);
      theSel.options[i] = newOpt;
      theSel.options[i].selected = selIsSel[i];
    }
  }
}

function removeOptions(theSel, Val)
{
	for(i=theSel.length-1; i>=0; i--)
    {
		if (Val != null)
		{
		  if(theSel.options[i].value == Val)
		  {
			theSel.options[i] = null;
			break;
		  }
		}
		else
		{
			theSel.options[i] = null;
		}
    }
}


function SubmitPriceList(action)
{
	var AfterSaveStockPriceList = function(oXML) {			
		var result = oXML.responseText;
		regularCursor();
		alert (result);
		//prompt('',result);
	}
	var thepage = '/modules/PriceList/processstockpricelist.asp';
	if (validateStockPriceList()){
		var PriceListID = document.getElementById('PriceListID').value;
		var StockID = document.getElementById('StockID').value;		
		var data = "action=" + action + "&PriceListID=" + PriceListID + "&StockID=" + StockID + "&" + fetchInputFormData('frmStockPriceList');
		hiddenSubmit(thepage,data, AfterSaveStockPriceList, 0, null, null,true);
	} 	
}

function validateStockPriceList(){
	
	var thereturn;
	thereturn = false;

	if (document.frmStockPriceList.txtPriceValue.value.trim() != "")
	{
		thereturn = true;
	}
	else
	{
		alert ("Please enter a valid Stock Price before continuing");
	}

	return thereturn;
}

