﻿function updateSecondDropDownSC(productID)
{
    var vehicleYear = document.getElementById("vehicleYearHidden").value;
    var vehicleMake = document.getElementById("vehicleMakeHidden").value;
    var vehicleModel = document.getElementById("vehicleModelHidden").value;
    var fistDDID = "firstDropDown_" + productID;
    var vehicleDesc = document.getElementById(fistDDID).value;
    
    if (vehicleDesc != "") 
    {               
		ProductSeatCoverOptionWebService.GetSecondOption(productID, vehicleYear, vehicleMake, vehicleModel, vehicleDesc, getSecondOptionSCCallback, getSecondOptionErrorSC);
	} else 
	{
		clearSecondDropDowm(productID);
	}
}

function getSecondOptionSCCallback(result)
{       
    var secondAttrLable = "Select Color...";
     if(result != null)
    {
        var s = new Array();
        var productID = result[0].ProductID;
	    s[s.length] = "<select id=\"secondDropDown_" + productID + "\" onchange=\"setAvailability (" + productID + ");\" >";
	    s[s.length] = "<option value=\"\">" + secondAttrLable + "</option>";
	    
        for(var i = 0; i < result.length; i++ )
	    {    
	     s[s.length] = "<option value=\"" + result[i].VariantID + "-" + result[i].VehicleID + "\">" + result[i].DisplayText + " $" + result[i].DisplayPrice + "</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 + "\" / >";
        }
        
        for(var i = 0; i < result.length; i++ )
	    {    
			s[s.length] = "<input type=\"hidden\" id=\"saleprice_" + result[i].VariantID + "\" value=\"" + "$" + result[i].SalePrice + "\" / >";
        }
        
        var secondDDID = "secondDD_" + productID;
        
	    document.getElementById(secondDDID).innerHTML = s.join(""); 
    }
    
    setAvailability (productID);	   
    
    if (window.document.getElementById("topOptionalBox") != null)
	{
		    fixIE7zIndexBug();
	}
}

function fixIE7zIndexBug(){}
		
function clearSecondDropDowm(productID)
{
    var secondDDID = "secondDD_" + productID;
    var s = new Array();
    s[s.length] = "<select id=\"" + secondDDID + "\" disabled>";
    s[s.length] = "<option value=\"\"> Select Color...</option>"; 
    s[s.length] = "</select>";
    document.getElementById(secondDDID).innerHTML = s.join("");
}

function selectSeatCover(setProductID)
{
    var firstDDID = "firstDropDown_" + setProductID;
	var variantID = window.document.getElementById(firstDDID).value;
	var productAvailabilityDate = window.document.getElementById("shipdate" + setProductID);
	
	if (variantID != "" && variantID != "0")
	{
		var shipdate_id = "shipdate_" + variantID;
		var availabilityDate = window.document.getElementById(shipdate_id).getAttribute("prettyName");
		
		//Check
		var checkBoxID = "checkBox_" + setProductID;
		var variant_checkbox  = eval(window.document.getElementById(checkBoxID));
		
		productAvailabilityDate.innerHTML = availabilityDate;
		
		variant_checkbox.checked = true;
	}
	else
	{
		var availabilityDate = "Select options below for availability";
		
		//Uncheck
		var checkBoxID = "checkBox_" + setProductID;
		var variant_checkbox  = eval(window.document.getElementById(checkBoxID));
		
		productAvailabilityDate.innerHTML = "Select option for availability.";
		
		variant_checkbox.checked = false;
	}
	
	window.document.getElementById('availability').innerHTML= availabilityDate;
	
	if (window.document.getElementById("topOptionalBox") != null)
	{
		    fixIE7zIndexBug();
	}	
}

function setAvailability (productID)
{
    var secondDDID = "secondDropDown_" + productID;
	var variantVehicleID = window.document.getElementById(secondDDID).value;
	
	var variantID = variantVehicleID.split('-')[0];
	if (variantVehicleID != "")
	{
		//Also set hidden input w/ variantId to add to cart
		var selectedVariant = window.document.getElementById("selectedVariant");
		
		if (selectedVariant != null)
		{
			selectedVariant.value = variantID;
		}
		
		var shipdate_id = "shipdate_" + variantID;
		var availabilityDate = window.document.getElementById(shipdate_id).getAttribute("value");	
		
		setPrice(variantID);
		selectSwatch(variantID);
		
		//Check the checkbox
		var checkBoxID = "checkBox_" + productID;
		var variant_checkbox  = eval(window.document.getElementById(checkBoxID));
		
		variant_checkbox.checked = true;
	
	} 
	else
	{
		var availabilityDate = "Select options below for availability";
	}
	
	window.document.getElementById('availability').innerHTML= availabilityDate;
	
	if (window.document.getElementById("topOptionalBox") != null)
	{
		    fixIE7zIndexBug();
	}	
	
	//Set the seatSaver Variants
	selectSeatSaver();
}

function selectVariant(variantID){}
function selectSwatch(variantID){}

function setPrice(variantID)
{
	var saleprice_id = "saleprice_" + variantID;
	var salePrice = window.document.getElementById(saleprice_id).getAttribute("value");
	window.document.getElementById('AutoAnythingPriceLable').innerHTML= salePrice;
}

function getSecondOptionErrorSC(error)
{
    alert("An error has occured while trying to process your request. Please refresh the browser and try again." + error);
}

function selectSeatSaver()
{
    numOfProducts = window.document.getElementById("numberOfVariants").value;
    var allowStepTwo = false;
    var variantIDs = "";
    
    for(var i = 0; i < numOfProducts; i++ )
    {
        var productID = window.document.getElementById('productID_' + i).value;
        var checkBoxID = "checkBox_" + productID;
        var secondDropDownID = "secondDropDown_" + productID;    
        var variant_checkbox  = eval(window.document.getElementById(checkBoxID));
        
        if (variant_checkbox.checked)
        {
            if (window.document.getElementById(secondDropDownID).value != "")
            {
                allowStepTwo = true;
                // Add additional selected seatcover product-variants to the query string
                variantIDs += (variantIDs.length > 0 ? ", " : "") + window.document.getElementById(secondDropDownID).value.split('-')[0];;
            }
        }
    }
    
	//Also set hidden input w/ variantId to add to cart
	var selectedVariant = window.document.getElementById("selectedVariant");
	
	if (selectedVariant != null)
	{
		selectedVariant.value = variantIDs;
	}
} 
