﻿function MultiAddToCartControl_Click()
{
	var errorMsg = "";
	
	for(var j=0; j < groupIndArray.length; j++)
	{
		var groupInd = groupIndArray[j];
		var groupQtyObj = document.getElementById("selectedQuantity" + groupInd);
		
		
		if(groupQtyObj != null)
		{
			if((groupQtyObj.value > 0 && groupQtyObj.getAttribute("type") != "checkbox") || (groupQtyObj.getAttribute("type") == "checkbox" && groupQtyObj.checked == true))
			{
				if (!ValidateSelection(groupInd, groupQtyObj.value))
				{
					errorMsg = errorMsg + "\n Please select option(s) for " +  document.getElementById("productDesc" + groupInd).value;
				}
			}
		}
	}
	
	if (errorMsg.length > 0)
	{
		document.getElementById("selectedVariant").value = "";
		alert(errorMsg);
	}
	else
	{
		// Add to cart
		addToCartButtonHidden.click();
	}
}

function ValidateSelection (goupInd, quanity)
{
	var selectedVariantForGroup = document.getElementById("selectedVariant" + goupInd);
	var selectedVariant = document.getElementById("selectedVariant");
	
	if (selectedVariantForGroup != null)
	{
		if(selectedVariantForGroup.value != "")
		{
			selectedVariant.value =  selectedVariant.value + (selectedVariant.value.length > 0 ? "," : "") +  selectedVariantForGroup.value + "|" + quanity;
			return true;
		}
	}
	return false;
}

//Highlight when 2nd DD is selected by user
function HighlightRowAfterSelection(groupInd)
{
	// Get the selected TR to change background color
    var selectHeadRow = document.getElementById("headerRow" + groupInd);
    var selectInfoRow = document.getElementById("infoRow" + groupInd);
    var selectedQty	= document.getElementById("selectedQuantity" + groupInd);
    var selectFooterRow = document.getElementById("footerRow" + groupInd);
    var secondDropdown = window.document.getElementById("secondMVDropdown" + groupInd);
    var variantID = secondDropdown.value;
    
     if( selectHeadRow != null && selectInfoRow != null && selectedQty != null)
	{
		if (selectedQty.getAttribute("type") == "checkbox")
		{
			if (selectedQty != null && selectedQty.getAttribute("type") == "checkbox" && variantID != "")
			{
				selectedQty.disabled = false;
				selectedQty.checked = true;
			}
			
			if (selectedQty.checked == true)
			{
				selectHeadRow.style.backgroundColor="#eee";
				selectInfoRow.style.backgroundColor="#eee";
				if(selectFooterRow != null)
				{
				    selectFooterRow.style.backgroundColor="#eee";
				}
			}
			else
			{
				selectHeadRow.style.backgroundColor="#fff";
				selectInfoRow.style.backgroundColor="#fff";
				if(selectFooterRow != null)
				{
				    selectFooterRow.style.backgroundColor="#fff";
				}
			} 
		}
		else
		{
			if (selectedQty.value > 0)
			{
				selectHeadRow.style.backgroundColor="#eee";
				selectInfoRow.style.backgroundColor="#eee";
				if(selectFooterRow != null)
				{
				    selectFooterRow.style.backgroundColor="#eee";
				}
			}
			else
			{
				selectHeadRow.style.backgroundColor="#fff";
				selectInfoRow.style.backgroundColor="#fff";
				if(selectFooterRow != null)
				{
				    selectFooterRow.style.backgroundColor="#fff";
				}
			} 	
		}
    }
    
    
}

//Highlight when check box/quantity is selected
function HighlightRow(groupInd)
{
	// Get the selected TR to change background color
    var selectHeadRow = document.getElementById("headerRow" + groupInd);
    var selectInfoRow = document.getElementById("infoRow" + groupInd);
    var selectedQty	= document.getElementById("selectedQuantity" + groupInd);
    var selectFooterRow = document.getElementById("footerRow" + groupInd);
    
    
    if( selectHeadRow != null && selectInfoRow != null && selectedQty != null)
	{
		if (selectedQty.getAttribute("type") == "checkbox")
		{
			if (selectedQty.checked == true)
			{
				selectHeadRow.style.backgroundColor="#eee";
				selectInfoRow.style.backgroundColor="#eee";
				if(selectFooterRow != null)
				{
				    selectFooterRow.style.backgroundColor="#eee";
				}
			}
			else
			{
				selectHeadRow.style.backgroundColor="#fff";
				selectInfoRow.style.backgroundColor="#fff";
				if(selectFooterRow != null)
				{
				    selectFooterRow.style.backgroundColor="#fff";
				}
			} 
		}
		else
		{
			if (selectedQty.value > 0)
			{
				selectHeadRow.style.backgroundColor="#eee";
				selectInfoRow.style.backgroundColor="#eee";
				if(selectFooterRow != null)
				{
				    selectFooterRow.style.backgroundColor="#eee";
				}
			}
			else
			{
				selectHeadRow.style.backgroundColor="#fff";
				selectInfoRow.style.backgroundColor="#fff";
				if(selectFooterRow != null)
				{
				    selectFooterRow.style.backgroundColor="#fff";
				}
			} 	
		}
    }
}

function AddToCartControl_Click(groupID)
{
	var submitCheck = true;
    var firstDropdown = document.getElementById("firstDropDown" + groupID);
    var secondDropdown = window.document.getElementById("secondMVDropdown" + groupID);
	
    if (firstDropdown != null)
    {
		if (firstDropdown.selectedIndex == 0)
		{
			submitCheck = false;
		}
    }

    if (secondDropdown != null)
    {
		if (secondDropdown.selectedIndex == 0)
		{
			submitCheck = false;
		}
    }

	if (submitCheck)
	{
		SetSelectedVariant(groupID);
	    
		addToCartButtonHidden.click();
    }
    else
    {
		alert ("Please select all options before adding to cart.");
		return false;
    }
}

function ShowGroupCartImage(groupID)
{
    var cartImage = document.getElementById("addToCartVariantGroupImage" + groupID);
    if (cartImage)
    {
	    cartImage.style["display"] = "block";
	}
}

function HideGroupCartImage(groupID)
{
    var cartImage = document.getElementById("addToCartVariantGroupImage" + groupID);
    if (cartImage)
    {
      	cartImage.style["display"] = "none";
    }
}

function SetSelectedVariant(groupID)
{
    var selectedVariantForGroup = document.getElementById("selectedVariant" + groupID);
    var selectedVariant = document.getElementById("selectedVariant");
    
    if (selectedVariant != null)
    {
		selectedVariant.value = selectedVariantForGroup.value;
    }   
}

function SetSelectedVariantForGroup(groupID, variantID)
{
    var selectedVariantForGroup = document.getElementById("selectedVariant" + groupID);
    if (selectedVariantForGroup != null)
    {
        selectedVariantForGroup.setAttribute('value', variantID);
    }
    
    // Get the selected TR to change background color
    var selectHeadRow = document.getElementById("headerRow" + groupID);
    var selectInfoRow = document.getElementById("infoRow" + groupID);
    var selectedQty	= document.getElementById("selectedQuantity" + groupID);
    var selectFooterRow = document.getElementById("footerRow" + groupID);
    
    if( selectHeadRow != null && selectInfoRow != null )
	{
		if (variantID != "" && selectedQty != null && selectedQty.getAttribute("type") == "checkbox")
		{
			if (selectedQty.checked == true)
			{
				selectHeadRow.style.backgroundColor="#eee";
				selectInfoRow.style.backgroundColor="#eee";
				if(selectFooterRow != null)
				{
				    selectFooterRow.style.backgroundColor="#eee";
				}
			}
		}
		else
		{
			selectHeadRow.style.backgroundColor="#fff";
			selectInfoRow.style.backgroundColor="#fff";
			if(selectFooterRow != null)
			{
			    selectFooterRow.style.backgroundColor="#fff";
			}
		} 
    }
}

function displayVariantPopup(groupNumber, largeImageID)
{
	if(groupNumber != "" && largeImageID != "" && largeImageID != "0")
	{
		var productID = document.getElementById("productIDHidden").value;
		window.open("../GenericPages/ProductImage.aspx?IsVariantMedia=1&GroupID=" + groupNumber + "&ImageID=" + largeImageID + "&ProductID=" + productID, "large_image");
	}
}

function ChangeProductImage(imageUrl, imagePath, largeImageMediaID)
{
	if (imagePath != "")
	{
		var productImage = document.getElementById("productImageFileName");
		productImage.src = imageUrl + imagePath;
		var largeImageID = document.getElementById("LargeImageID");
		largeImageID.value = largeImageMediaID;
	}
}  

function SchemaImageMouseout(groupID, imageUrl)
{
    var groupSchemaImage = document.getElementById("VariantGroupMainImage" + groupID);
    groupSchemaImage.src = document.getElementById("VariantGroupMainImageMouseout").value;
    
    var groupSchemaImageDesc = document.getElementById("VariantGroupMainImageDesc" + groupID);
    groupSchemaImageDesc.innerHTML = document.getElementById("VariantGroupMainImageDescMouseout").value;
}

function SchemaImageMouseover(groupID, imageUrl, imageDesc)
{
    var groupSchemaImage = document.getElementById("VariantGroupMainImage" + groupID);
    document.getElementById("VariantGroupMainImageMouseout").value = groupSchemaImage.src;

    var groupSchemaImageDesc = document.getElementById("VariantGroupMainImageDesc" + groupID);
    document.getElementById("VariantGroupMainImageDescMouseout").value = groupSchemaImageDesc.getAttribute("innerHTML");

    groupSchemaImageDesc.innerHTML = imageDesc;
    groupSchemaImage.src = imageUrl;
}  

function updateContent(variantID, variantGroupID)
{
    var vehicleID = document.getElementById("vehicleIDHidden").value;
    ProductMultipleOptionsWebService.GetContent(variantID, vehicleID, variantGroupID, updateContentCallback, AjaxError);
}

function updateContentCallback(result)
{       
    if(result != null)
    {
	    document.getElementById("contentSpan" + result.VariantGroupID).innerHTML = result.Content; 
	    
	    var variantGroupImageObj = document.getElementById("VariantGroupMainImage" + result.VariantGroupID);
	    
	    if (variantGroupImageObj != null && result.MediaFilename != null)
	    {
			variantGroupImageObj.src = result.MediaFilename;
		}

        var groupSchemaImageDesc = document.getElementById("VariantGroupMainImageDesc" + result.VariantGroupID);
        if (groupSchemaImageDesc != null)
        {
			groupSchemaImageDesc.innerHTML = result.MediaDesc;
		}
        
        ShowGroupCartImage(result.VariantGroupID);
    }
}

function updateSecondMVDropDown(variantGroupID)
{
    var attributeID = document.getElementById("firstDropDown" + variantGroupID).value;
    var productID = document.getElementById("productIDHidden").value;
    var vehicleID = document.getElementById("vehicleIDHidden").value;
    var attributeType = document.getElementById("attributeTypeHidden" + variantGroupID).value;
    var returnType = document.getElementById("returnAttributeHidden").value;
    
    if (attributeID != "") 
    {               
	    ProductMultipleOptionsWebService.GetSecondOption(productID, vehicleID, returnType, attributeID, attributeType, variantGroupID, getSecondMVOptionCallback, getSecondOptionError);
    } 
    else 
    {
	    clearSecondMVDropDown(variantGroupID);
    }
}

function updateSecondMVDropDownWithPreset(variantGroupID)
{
    var attributeID = document.getElementById("firstDropDown" + variantGroupID).value;
    var productID = document.getElementById("productIDHidden").value;
    var vehicleID = document.getElementById("vehicleIDHidden").value;
    var attributeType = document.getElementById("attributeTypeHidden" + variantGroupID).value;
    var returnType = document.getElementById("returnAttributeHidden").value;
    
    if (attributeID != "") 
    {               
	    ProductMultipleOptionsWebService.GetSecondOption(productID, vehicleID, returnType, attributeID, attributeType, variantGroupID, getSecondMVOptionCallbackWithPreset, getSecondOptionError);
    } 
    else 
    {
	    clearSecondMVDropDown(variantGroupID);
    }
}

function getSecondOptionError(error)
{
    alert("An error has occured while trying to process your request. Please refresh the browser and try again." + error);
}

function getSecondMVOptionCallbackWithPreset(result)
{
    getSecondMVOptionCallback(result);
    
    if(result != null)
    {
        var variantGroupID = result[0].VariantGroupID;
        var secondDropdown = window.document.getElementById("secondMVDropdown" + variantGroupID);
        
        if (secondDropdown.length == 2)
        {
            secondDropdown.selectedIndex = 1;
            setAvailability(variantGroupID);
        }
    }    
}

function getSecondMVOptionCallback(result)
{       
    var secondAttrLabel = document.getElementById("secondAttributeLabel").value;      
    
     if(result != null)
    {
        var variantGroupID = result[0].VariantGroupID;
        var s = new Array();

	    s[s.length] = "<select id=\"secondMVDropdown" + variantGroupID + "\"\" onchange=\"setAvailability(" + variantGroupID + ");HighlightRowAfterSelection(" + variantGroupID + ");\" >";
	    s[s.length] = "<option value=\"\">" + secondAttrLabel + "</option>";
        for (var i=0; i < result.length; i++)
	    {    
			if(result[i].AttributeDesc != null)
			{
			    //  Display the price if it is not a map product (Minimum Advertised Price Policy)
			    if(typeof(isMapProduct) != 'undefined' && isMapProduct != null && !isMapProduct)
			    {
			        s[s.length] = "<option value=\"" + result[i].VariantID + "\">" + result[i].AttributeDesc + " - " + result[i].SalePrice + "</option>";
			    }
			    else
			    {
			        s[s.length] = "<option value=\"" + result[i].VariantID + "\">" + result[i].AttributeDesc + "</option>";
			    }
			}
        }
		s[s.length] = "</select>";
		
        for (var i=0; i < result.length; i++)
	    {    
			s[s.length] = "<input type=\"hidden\" id=\"shipdate_" + result[i].VariantID + "\" value=\"" + result[i].ShippingDate + "\" / >";
			s[s.length] = "<input type=\"hidden\" id=\"saleprice_" + result[i].VariantID + "\" value=\"" + result[i].SalePrice + "\" / >";
			s[s.length] = "<input type=\"hidden\" id=\"media_" + result[i].VariantID + "\" value=\"" + result[i].MediaID + "\" / >";
			s[s.length] = "<input type=\"hidden\" id=\"media_file_" + result[i].VariantID + "\" value=\"" + result[i].MediaFilename + "\" / >";
        }
	    document.getElementById("secondMVDropdownSpan" + variantGroupID).innerHTML = s.join(""); 
    }
    
    if (window.document.getElementById("topOptionalBox") != null)
	{
		    fixIE7zIndexBug();
	}
}

function fixIE7zIndexBug()
{
	window.document.getElementById("topOptionalBox").style["display"] = "none";
	window.document.getElementById("topOptionalBox").style["display"] = "block";
}
		
function clearSecondMVDropDown(variantGroupID)
{
    var secondAttributeLabel = window.document.getElementById("secondAttributeLabel").getAttribute("value");	

    var s = new Array();
    s[s.length] = "<select id=\"secondMVDropdown" + variantGroupID + "\" disabled>";
    s[s.length] = "<option value=\"\">" + secondAttributeLabel + "&nbsp</option>"; 
	s[s.length] = "</select>";
    document.getElementById("secondMVDropdownSpan" + variantGroupID).innerHTML = s.join("");
    
    setAvailability(variantGroupID);
}

function setAvailability(variantGroupID)
{
    var secondDropdown = window.document.getElementById("secondMVDropdown" + variantGroupID);
	var variantID = secondDropdown.value;
	var qtyObj = window.document.getElementById("selectedQuantity" + variantGroupID);
	var VariantGroupMainImage = window.document.getElementById("VariantGroupMainImageDiv" + variantGroupID);
	
	//If the template doesn't have/require number of groups check then set it to one to bypass the logic
	if (window.document.getElementById("numberOfGroups") != null)
	{
		var numberOfVariantGroups = window.document.getElementById("numberOfGroups").value;
	}
	else
	{
		var numberOfVariantGroups = 1;
	}
	
	if (variantID != "")
	{
	    var availabilityDate = window.document.getElementById("shipdate_" + variantID).getAttribute("value");	
	    var salePrice = window.document.getElementById("saleprice_" + variantID).getAttribute("value");
	    var largeImageObj = window.document.getElementById("large_media_" + variantID);
	    
	    if (largeImageObj != null)
	    {
			var largeImageID = window.document.getElementById("large_media_" + variantID).getAttribute("value");
		}
	    
	    
		if (qtyObj != null && qtyObj.getAttribute("type") == "checkbox" && numberOfVariantGroups == 1)
		{
			qtyObj.disabled = false;
		    qtyObj.checked = true;
		}

        updateContent(variantID, variantGroupID);      
        SetSelectedVariantForGroup(variantGroupID, variantID);
        
        if(VariantGroupMainImage != null && typeof(largeImageID) != 'undefined')
        {
			VariantGroupMainImage.onclick = function(){displayVariantPopup('0', largeImageID);};
		}
	} 
	else
	{
		var availabilityDate = "z<em>Select options to see availability.</em>";
		var variantGroupSalePriceObj = window.document.getElementById("shippingPriceDefault" + variantGroupID);
		if (variantGroupSalePriceObj != null)
		{
			var salePrice =	variantGroupSalePriceObj.value;
		}
	    
	    document.getElementById("contentSpan" + variantGroupID).innerHTML = ""; 
	    
        var groupSchemaImage = document.getElementById("VariantGroupMainImage" + variantGroupID);
        
        var defaultImageFilenameObj = document.getElementById("DefaultImageFilename" + variantGroupID);
        
        if (defaultImageFilenameObj != null)
        {
			groupSchemaImage.src = document.getElementById("DefaultImageFilename" + variantGroupID) == null ? "" : defaultImageFilenameObj.value;
        }
        var groupSchemaImageDesc = document.getElementById("VariantGroupMainImageDesc" + variantGroupID);
        if (groupSchemaImageDesc != null)
        {
			groupSchemaImageDesc.innerHTML = document.getElementById("DefaultImageDesc" + variantGroupID) == null ? "" : document.getElementById("DefaultImageDesc" + variantGroupID).value;
        }
        
        if (qtyObj != null && qtyObj.getAttribute("type") == "checkbox")
		{
			qtyObj.disabled = true;
		    qtyObj.checked = false;
		}
        SetSelectedVariantForGroup(variantGroupID, "");
	}
	
	if (availabilityDate != "")
	{
	    var shipdate = window.document.getElementById('shippingDate' + variantGroupID);
        if (shipdate != null)
        {
	        shipdate.innerHTML = availabilityDate; 
	    }
	}
	
	var groupPriceObj = window.document.getElementById('variantGroupPrice' + variantGroupID);
	
	if (groupPriceObj != null)
	{
        //
        //  Update the sale price if the product is not a map product (Minimum Advertised Price Policy)
        //
        if(typeof(isMapProduct) != 'undefined' && isMapProduct != null && !isMapProduct)
        {
            window.document.getElementById('variantGroupPrice' + variantGroupID).innerHTML = salePrice;
        }
	}
	
	if (window.document.getElementById("topOptionalBox") != null)
	{
	    fixIE7zIndexBug();
	}
}

function AjaxError(error)
{
    alert("An error has occured while trying to process your request. Please refresh the browser and try again." + error);
}
