// This variable is introduced to identify if the form has already been submitted for some action
// and to prevent any other action on the form till the page is refreshed.
var isFormSubmitted = false;

function AddKitToWishList()
{
	var iNoOfGroupRows		=	0;
	var allElements			=	document.forms[0].elements;
	for (var i=0; i<allElements.length; i++)
	{	
		var iStartIndex		=	allElements[i].name.indexOf("KitGroupsControl$KitGroupsAll$_ctl" + iNoOfGroupRows);
		if (iStartIndex != -1)
			iNoOfGroupRows++;
	}
	
	var iTotalNoOfComponentsSelected	=	0;
	for (var _KitGroupRowIndex=0; _KitGroupRowIndex<iNoOfGroupRows; _KitGroupRowIndex++)
	{
		for (var _KitGroupIndex=0; _KitGroupIndex<3; _KitGroupIndex++)
		{
			//Validate Attribute selection
			if (!validateSKUforGroup((_KitGroupRowIndex * 3) + _KitGroupIndex))
				return false;
			
			var iNoOfQtySelected	=	getNoOfQtySelectedInGroup((_KitGroupRowIndex * 3) + _KitGroupIndex);

			//Update total no of components
			iTotalNoOfComponentsSelected	+=	iNoOfQtySelected;

			//Get no of selects		
			var NumberOfSelects	=	getNoOfSelectsPerGroup(_KitGroupRowIndex, _KitGroupIndex);

			//Get group name
			var GroupName		=	getGroupName(_KitGroupRowIndex, _KitGroupIndex);

			if(iNoOfQtySelected < 0)	// Check here if user enter invalid Qty 0.
			{
				alert("Please enter a valid quantity.");
				return false;
			}
				
			if (iNoOfQtySelected != NumberOfSelects)
			{
				alert("Total number of item(s) in kit group \"" + GroupName + "\" must be " + NumberOfSelects + ". Please check your selections again.");
				return false;
			}
		}
	}
		
	isFormSubmitted	=	true;
	document.forms[0].Action.value = "AddToWishlist";
	//document.forms[0].submit();	
	return true;
}

function AddKitToCart()
{
	if(isFormSubmitted)
		return false;	
	
	isFormSubmitted = true;	
	//__doPostBack('AddToCart','');
	return true;
}

function AddToKitAllGroups()
{
	var iNoOfGroupRows		=	0;
	var allElements			=	document.forms[0].elements;
	for (var i=0; i<allElements.length; i++)
	{	
		var iStartIndex		=	allElements[i].name.indexOf("KitGroupsControl$KitGroupsAll$_ctl" + iNoOfGroupRows);
		if (iStartIndex != -1)
			iNoOfGroupRows++;
	}
	
	var iTotalNoOfComponentsSelected	=	0;
	for (var _KitGroupRowIndex=0; _KitGroupRowIndex<iNoOfGroupRows; _KitGroupRowIndex++)
	{
		for (var _KitGroupIndex=0; _KitGroupIndex<3; _KitGroupIndex++)
		{
			var iNoOfComponentSelected	=	getNoOfComponentsSelectedInGroup(_KitGroupRowIndex, _KitGroupIndex);

			//Update total no of components
			iTotalNoOfComponentsSelected	+=	iNoOfComponentSelected;

			//Get no of selects		
			var NumberOfSelects	=	getNoOfSelectsPerGroup(_KitGroupRowIndex, _KitGroupIndex);

			//Get group name
			var GroupName		=	getGroupName(_KitGroupRowIndex, _KitGroupIndex);

			if (iNoOfComponentSelected > NumberOfSelects)
			{
				alert("You have selected more than " + NumberOfSelects + " item(s) for the kit group \"" + GroupName + "\". Please check your selections again.");
				return false;
			}			
		}
	}
		
	if (iTotalNoOfComponentsSelected == 0)
	{
		alert("You have not selected any item for your kit. Please select valid number of item(s) before you continue.");
		return false;
	}

	isFormSubmitted = true;	
	
	document.forms[0].Action.value = "AddToKitAllGroups";
	//document.forms[0].submit();  
	return true;
}

function getNoOfSelectsPerGroup(_KitGroupRowIndex, _KitGroupIndex)
{
	var strSearchCtlName	=	"KitGroupsControl_KitGroupsAll__ctl" + _KitGroupRowIndex + "_KitGroups__ctl" + _KitGroupIndex + "_KitGroup_lblNoOfSelects";
	var ctlGroup	=	getLabelElementsByName(strSearchCtlName);
	if (ctlGroup != null)
		return ctlGroup.innerHTML;
	else
		return 0;	
}

function getGroupName(_KitGroupRowIndex, _KitGroupIndex)
{
	var strSearchCtlName	=	"KitGroupsControl_KitGroupsAll__ctl" + _KitGroupRowIndex + "_KitGroups__ctl" + _KitGroupIndex + "_KitGroup_lblGroupName";
	var ctlGroup	=	getLabelElementsByName(strSearchCtlName);
	if (ctlGroup != null)
		return ctlGroup.innerHTML;
	else
		return "";	
}

function getNoOfComponentsSelectedInGroup(_KitGroupRowIndex, _KitGroupIndex)
{
	var strSearchCtlName		=	"KitGroupsControl$KitGroupsAll$_ctl" + _KitGroupRowIndex + "$KitGroups$_ctl" + _KitGroupIndex + "$KitGroup$KitComponents$KitComponents$_ctl";
	var iNoOfComponentSelected	=	0;

	var allElements			=	document.forms[0].elements;
	for (var i=0; i<allElements.length; i++)
	{	
		if (allElements[i].name.indexOf(strSearchCtlName) != -1)
		{
			if (allElements[i].checked)
				iNoOfComponentSelected	=	iNoOfComponentSelected + 1;
		}
	}

	return iNoOfComponentSelected;
}

function getNoOfQtySelectedInGroup(_KitGroupIndex)
{
	var strSearchCtlName	=	"SelectedKitGroups$_ctl" + _KitGroupIndex + ":SelectedKitComponents$_ctl";
	var iNoOfQtySelected	=	0;

	var allElements			=	document.forms[0].elements;
	for (var i=0; i<allElements.length; i++)
	{	
		if (allElements[i].name.indexOf(strSearchCtlName) != -1)
		{
			if (allElements[i].name.indexOf(":textQuantity") != -1)
			{
				iNoOfQtySelected	=	iNoOfQtySelected + parseInt(allElements[i].value);
				if(isNaN(parseInt(allElements[i].value)) || parseInt(allElements[i].value) <= 0)//Check here if user enter invalid Qty 0.
				{
					allElements[i].focus();
					return -1;
				}
				
			}
		}
	}

	return iNoOfQtySelected;
}

function AddToKit(_KitGroupRowIndex, _KitGroupIndex)
{
	if(isFormSubmitted)
		return false;	
	
	var iNoOfComponentSelected	=	getNoOfComponentsSelectedInGroup(_KitGroupRowIndex, _KitGroupIndex);
	if (iNoOfComponentSelected == 0)
	{
		alert("You have not selected any item for your kit. Please select valid number of item(s) before you continue.");
		return false;
	}
	
	//Get no of selects		
	var NumberOfSelects	=	getNoOfSelectsPerGroup(_KitGroupRowIndex, _KitGroupIndex);

	//Get group name
	var GroupName		=	getGroupName(_KitGroupRowIndex, _KitGroupIndex);

	if (iNoOfComponentSelected > NumberOfSelects)
	{
		alert("You have selected more than " + NumberOfSelects + " item(s) for the kit group \"" + GroupName + "\". Please check your selections again.");
		return false;
	}

	isFormSubmitted = true;	
	
	document.forms[0].Action.value = "AddToKitSelectedGroup";
	document.forms[0].SelectedRowIndex.value = _KitGroupRowIndex;
	document.forms[0].SelectedGroupIndex.value = _KitGroupIndex;
	//document.forms[0].submit();  
	return true;
	
}

function DeleteFromKitBag(KitComponentID, GroupID)
{
	var bClicked = confirm('Are you sure you want to delete this item from your kit?');
	if(bClicked)
	{
		document.forms[0].Action.value = "DeleteKit";
		document.forms[0].DeletedComponentID.value = KitComponentID;
		document.forms[0].GroupID.value = GroupID;
		document.forms[0].submit();  
	}
}

function AddToBag()
{
	var iNoOfGroupRows		=	0;
	var allElements			=	document.forms[0].elements;
	for (var i=0; i<allElements.length; i++)
	{	
		var iStartIndex		=	allElements[i].name.indexOf("KitGroupsControl$KitGroupsAll$_ctl" + iNoOfGroupRows);
		if (iStartIndex != -1)
			iNoOfGroupRows++;
	}
	
		var iTotalNoOfComponentsSelected	=	0;
		for (var _KitGroupRowIndex=0; _KitGroupRowIndex<iNoOfGroupRows; _KitGroupRowIndex++)
		{
			for (var _KitGroupIndex=0; _KitGroupIndex<3; _KitGroupIndex++)
			{
				//Validate Attribute selection
				if (!validateSKUforGroup((_KitGroupRowIndex * 3) + _KitGroupIndex))
					return false;
				
				var iNoOfQtySelected	=	getNoOfQtySelectedInGroup((_KitGroupRowIndex * 3) + _KitGroupIndex);

				//Update total no of components
				iTotalNoOfComponentsSelected	+=	iNoOfQtySelected;

				//Get no of selects		
				var NumberOfSelects	=	getNoOfSelectsPerGroup(_KitGroupRowIndex, _KitGroupIndex);

				//Get group name
				var GroupName		=	getGroupName(_KitGroupRowIndex, _KitGroupIndex);
				
				if(iNoOfQtySelected < 0)	// Check here if user enter invalid Qty 0.
				{
					alert("Please enter a valid quantity.");
					return false;
				}
				
				if (iNoOfQtySelected != NumberOfSelects)
				{
					alert("Total number of item(s) in kit group \"" + GroupName + "\" must be " + NumberOfSelects + ". Please check your selections again.");
					return false;
				}
			}
		}
	isFormSubmitted	=	true;
	document.forms[0].Action.value = "AddToBag";
	//document.forms[0].submit();
	return true;
}

function validateSKUforGroup(_KitGroupIndex)
{
	var strSearchCtlName	=	"SelectedKitGroups$_ctl" + _KitGroupIndex + ":SelectedKitComponents$_ctl";

	var allElements			=	document.forms[0].elements;
	for (var i=0; i<allElements.length; i++)
	{	
		var strCtlName		=	allElements[i].name;
		var iStartIndex		=	strCtlName.indexOf(strSearchCtlName);
		if (iStartIndex != -1)
		{
			strCtlName		=	strCtlName.replace(strSearchCtlName,"");
			iStartIndex		=	strCtlName.indexOf(":");
			var _KitComponentIndex	=	strCtlName.substr(0,iStartIndex);
			
			//Validate SKU data for kit component selected
			var isValid	=	validateSKUforKitComponent(_KitGroupIndex, _KitComponentIndex);

			if (!isValid)
				return false;
		}
	}
	
	return true;
}


function validateSKUforKitComponent(_KitGroupIndex, _KitComponentIndex)
{
	var strSearchCtlName		=	"SelectedKitGroups$_ctl" + _KitGroupIndex + ":SelectedKitComponents$_ctl" + _KitComponentIndex + ":listAttribute";

	var isValid				=	true;
	var strAttributeName	=	"";
	for (var index=0; index<3; index++)
	{
		var ctlAttribue	=	getElementByName(strSearchCtlName + index);
		if (ctlAttribue != null && ctlAttribue.selectedIndex == 0)
		{
			strAttributeName	=	ctlAttribue.options[ctlAttribue.selectedIndex].text;			
			isValid	=	false;
			break;
		}
	}
		
	if (!isValid)
	{
		var strSalesName	=	"";
		var strItemNameCtl	=	"SelectedKitGroups__ctl" + _KitGroupIndex + "_SelectedKitComponents__ctl" + _KitComponentIndex + "_lblSalesName";
		var ctlItemName		=	getLabelElementsByName(strItemNameCtl);
		if (ctlItemName != null)
			strSalesName	=	ctlItemName.innerHTML;

		var strGroupName	=	"";
		var strGroupNameCtl	=	"SelectedKitGroups__ctl" + _KitGroupIndex + "_lblGroup";
		var ctlGroupName	=	getLabelElementsByName(strGroupNameCtl);
		if (ctlGroupName != null)
			strGroupName	=	ctlGroupName.innerHTML;

		alert("Please select \"" + strAttributeName + "\" for item \"" + strSalesName + "\" in kit group \"" + strGroupName + "\".");
	}

	return isValid;
}

function validateSKUAndSubmit(_KitGroupIndex, _KitComponentIndex, _AttributeIndex, GroupName, SalesName)
{
	if(isFormSubmitted)
		return false;	

	var strSearchCtlName		=	"SelectedKitGroups$_ctl" + _KitGroupIndex + ":SelectedKitComponents$_ctl" + _KitComponentIndex + ":listAttribute";

	var SubmitForm	=	true;
	for (var index=0; index<3; index++)
	{
		var ctlAttribue	=	getElementByName(strSearchCtlName + index);
		if (ctlAttribue != null && ctlAttribue.selectedIndex == 0)
		{
			SubmitForm	=	false;
			break;
		}
	}

	if (SubmitForm)
	{
		isFormSubmitted	=	true;
		document.forms[0].Action.value = "VerifySeriesItem";
		document.forms[0].SelectedGroupIndex.value = _KitGroupIndex;
		document.forms[0].SelectedComponentIndex.value = _KitComponentIndex;
		document.forms[0].SelectedAttributeIndex.value = _AttributeIndex;
		document.forms[0].submit(); 
	}
}

function getLabelElementsByName(strElementName)
{
	var ctlRequested	=	null;
	if (window.navigator.appName.toLowerCase().indexOf("microsoft") > -1) 
	{
		ctlRequested = document.all[strElementName];
	}
	else 
	{
		ctlRequested = document.getElementById(strElementName);
	}
	
	return ctlRequested;
}

function getElementByName(strElementName)
{
	var allElements			=	document.forms[0].elements;
	for (var i=0; i<allElements.length; i++)
	{	
		if (allElements[i].name == strElementName)
		{
			return allElements[i];
		}
	}

	return null;
}
