﻿//---------------------------------------------------------------------
// Author:		Manoj Kumar Raju
// Date:		10-02-2004
// Description: To validate the quantity before adding an item to basket and checking availability 
// Reference:	PCRedesign
// Inputs   :
//				strTextcontrol - Text Control Ids which holds the quantity concatenated by ',' seperator
//				strMinOrderQty - Minimum Order quantities of products concatenated by ',' seperator
//				strOrderQty   -Order quantities of products concatenated by ',' seperator
//				strProductNumber - Product numbers concatenated by ',' seperator
//				strMessage - Message to be displayed for  invalid quantity 
// Note		:	Ensure Validation.js is included before using this function
//---------------------------------------------------------------------
function ValidateQuantity(strTextcontrol,strMinOrderQty,strOrderQty,strProductNumber,strMessage)
{
	var arrTextControl = new Array();
	var arrMinOrderQty  = new Array();
	var arrOrderQty = new Array();
	var arrProductNumber = new Array();
	var strProductListMinQty = new String();
	var strProductListOrderQty = new String();
	//If TextControlIds is null or empty, display the message
	if (strTextcontrol == null || trimString(strTextcontrol) == '')
	{
		alert(strMessage);
		return false;
	}
	
	arrTextControl = strTextcontrol.split(",");
	arrMinOrderQty = strMinOrderQty.split(",");
	arrOrderQty = strOrderQty.split(",");
	arrProductNumber = strProductNumber.split(",");
	var counter;
	var txtControl ,numQty,numMinOrderQty,numOrderQty,strProductNo;
	for (counter = 0;counter <arrTextControl.length;counter++)
	{
		txtControl = document.getElementById(arrTextControl[counter]);
		numQty = Number(txtControl.value)
		numMinOrderQty = Number(arrMinOrderQty[counter]);
		numOrderQty = Number(arrOrderQty[counter]);
		strProductNo = arrProductNumber[counter];
		//Check for empty string and invalid number
		if(txtControl==null ||trimString(txtControl.value)==''||isNaN(numQty) || numQty ==0)
		{
			alert(strMessage);
			//Clear the quantity entered and set the focus to the quantity control
			txtControl.value = "";
			txtControl.focus();
			return false;
		}
		else
		{
			if (numQty < 0)
			{
				//If the number is lesser than zero set the quantity to 1
				txtControl.value = 1;
			}
			else if( txtControl.value.indexOf(".") != -1 )
			{
				//Rounding off the entered quantity if the number contains a decimal point 
				txtControl.value =Math.ceil(numQty);
			}
			//Check qty against min order qty and order qty
			if( numQty < numMinOrderQty)
			{
				strProductListMinQty = strProductListMinQty + numQty + '_' + strProductNo + '_' + numMinOrderQty  + ',';
				txtControl.value = numMinOrderQty;
			}
			if( numQty % numOrderQty != 0)
			{
				strProductListOrderQty = strProductListOrderQty + numQty + '_' + strProductNo + '_' + numOrderQty  + ',';
			}
			
		}
	}
	//First check if there is any Qty lesser than Min Order Qty
	if (strProductListMinQty.length > 0 )
	{
		strProductListMinQty=strProductListMinQty.slice(0,strProductListMinQty.length-1);	
		myWin = window.open('PopupInfo.aspx?ProductArray=' + strProductListMinQty + '&ErrorType=MinQuantity','Message','scrollbars=no,resizable=no,height=318,width=656');
		myWin.focus();
		return false;
	}
	else if (strProductListOrderQty.length > 0 )
	{
		strProductListOrderQty=strProductListOrderQty.slice(0,strProductListOrderQty.length-1);	
		myWin = window.open('PopupInfo.aspx?ProductArray=' + strProductListOrderQty + '&ErrorType=OrderQuantity','Message','scrollbars=no,resizable=no,height=318,width=656');
		myWin.focus();
		return false;
	}
	return true;	
	
}	