﻿//var prvObjID = 'ctl00_ContentPlaceHolder1_FeatureCategorList1_uiFeatureCategories_ctl00_uiFeaturesDiv';
var prvObjID = 'none';

function toggleVisible_v2(objID, objID2)
{
  var toggleItem = document.getElementById(objID);
  var prvObj;
  var placeHolder = document.getElementById(objID2);
  
  if(prvObjID == 'none')
  {
    prvObjID = placeHolder.value;
  }
  
  if(prvObjID != objID)
  {
    prvObj = document.getElementById(prvObjID);
    prvObjID = objID;
    
    prvObj.style.display = 'none';
  }
  
  if (toggleItem.style.display == 'none') 
  {
    toggleItem.style.display = 'block';
  }  
  else 
  {
    toggleItem.style.display = 'none';
  }
  
  placeHolder.value = objID;
  
}

function updateOptions(id, colorID, typeID, parentID, price, type)
{
  if(type == 1)
  {
    //alert(id);
    PreRenderOptions(id, colorID, typeID, price, parentID);
  }
  else
  {
    var newOptionArray = Array();
    for(var i = 0; i < _optionArray.length; i++)
    {
      //alert(_optionArray.length + ':' + _optionArray[i].id + ':' + id);
      if(_optionArray[i].ID != id)
      {
        //alert(_optionArray[i].Price);
        newOptionArray[newOptionArray.length] = new RenderParameter(_optionArray[i].ID, _optionArray[i].colorID, _optionArray[i].type, _optionArray[i].Price, _optionArray[i].parentID);
      }
    }
    _optionArray = newOptionArray;
    
  }
}
function singleSelection(selectedItem, parentID, itemCount, aval)
{
  var id;
  var objItemID;
  var colorID;
  var objColorID;
  var price;
  var objPrice;
 
  var objID = '';
  var obj = null;
  var xID = '';
  for(var i = 0; i < itemCount; i++)
  {
    if(i < 10)
    {
      objID = parentID + '_ctl0' + i + '_uiFeature';
      objColorID = document.getElementById(parentID + '_ctl0' + i + '_ColorPallet1_Color');
    }
    else
    {
      objID = parentID + '_ctl' + i + '_uiFeature';
      objColorID = document.getElementById(parentID + '_ctl' + i + '_ColorPallet1_Color');
    }
    
    xID = parentID + '_ctl0' + i;

    obj = document.getElementById(objID);
    
    if(BrowserDetect.browser == "Explorer")
    {
      id = obj.FeatureID;
      price = obj.Price;
    }
    else
    {
      id = obj.attributes['featureid'].value;
      price = obj.attributes['price'].value;
    }
      
    if(objColorID != null)
      colorID = objColorID.value;
    else
      colorID = 0;
    
    // don't allow selection of discontinued items and remove from list
    if(aval == 3)
    {
      if(obj != null)
      {
        obj.checked = false;
        if(id != 0)
          updateOptions(id, colorID, 2, id, price, 2);
      }
    }
    // uncheck and remove other items in same group
    if(selectedItem.id != objID)
    {
      if(obj != null)
      {
        obj.checked = false;
        
        if(id != 0)
        {
          //alert(id);
          updateOptions(id, colorID, 2, id, price, 2);
        }
      }
    }
    // add selected item
    else if(selectedItem.checked == true)
    {
      if(obj != null)
      {
        if(id != 0)
        {
          //alert(id + ':' + colorID + ':' + price);
          updateOptions(id, colorID, 2, id, price, 1);
        }
      } 
    }
    // remove item if unselected
    else if(selectedItem.checked == false)
    {
      if(obj != null)
      {
        if(id != 0)
        {
          //alert(id);
          updateOptions(id, colorID, 2, id, price, 2);
        }
      }
    }
    
    var subObjID = '';
    var subObj = null;
    
    if(i < 10)
      subObjID = parentID + '_ctl0' + i + '_uiSubFeaturesDiv';
    else
      subObjID = parentID + '_ctl' + i + '_uiSubFeaturesDiv';
    
    if(obj != null && aval != 3)
    { 
      // get sub feature div element
      subObj = document.getElementById(subObjID);
      
      // determin if parent feature was checked
      var isParentChecked = obj.checked;
      
      // declare counter 2
      var i2 = 0;
      
      // loop until there are no more sub features
      do
      {
        // declare variables
        var subID = 0;
        var objSubID = null;
        var objSubItemID = '';
        var objSubItem = null;
        var objAval = null;
        
        var xsubID = '';
        var xcolorID = '';
        var xprice = '';
        
        // assign inital values
        colorID = 0;
        price = 0;
        
        // get element ids
        if(i2 < 10)
        {
          objSubItemID = xID + '_uiSubFeatures_ctl0' + i2 + '_uiFeature';
          xcolorID = xID + '_uiSubFeatures_ctl0' + i2 + '_ColorPallet1_Color';
        }
        else
        {
          objSubItemID = xID + '_uiSubFeatures_ctl' + i2 + '_uiFeature';
          xcolorID = xID + '_uiSubFeatures_ctl' + i2 + '_ColorPallet1_Color';
        }
        
        // get element uiFeature
        objSubItem = document.getElementById(objSubItemID);
        // get element Color
        objColorID = document.getElementById(xcolorID);
        
        // get feature id and price
        if(objSubItem != null && objColorID != null)
        {
          if(BrowserDetect.browser == "Explorer")
          {
            objSubID = objSubItem.FeatureID;
            objPrice = objSubItem.Price;
            objAval = objSubItem.Availability;
          }
          else
          {
            objSubID = objSubItem.attributes['featureid'].value;
            objPrice = objSubItem.attributes['price'].value;
            objAval = objSubItem.attributes['availability'].value;
          }
          
          // get color value
          if(objColorID == null || objColorID.value == '')
            colorID = 0
          else
            colorID = objColorID.value;
          
          // preload subfeatures
          PreLoadSubFeatures(objSubID, objAval, objPrice, colorID, isParentChecked, objSubItem, id);
          
          if(isParentChecked)
            subObj.style.display = "block";
          else
            subObj.style.display = "none";
        }
        
        i2++; 
      }
      while(objSubItem != null)
    }
    else
    {
      selectedItem.checked = false;
    }
  } 
  Display();
  DisplayOptionTotal();
}

function PreLoadSubFeatures(id, aval, price, colorID, isParentSelected, obj, parentID)
{
  // 1 = add
  // 2 = remove
  var updateType = 0;
  
  // set update type
  switch(isParentSelected)
  {
    case true:
      updateType = 1;
      break;
    case false:
      updateType = 2;
      break;
    default:
      break;
  }
  
  // check availability
  // 2 = set to parent value
  // else uncheck
  if(aval == 2)
  {
    obj.checked = isParentSelected;
    obj.disabled = true;
    updateOptions(id, colorID, 2, parentID, price, updateType);
  }
  else
  {
    //obj.checked = false;
    updateOptions(id, colorID, 2, parentID, price, 2);
  }
}



function checkClick(obj, aval, parentID)
{
  var base = obj.id.substring(0, (obj.id.length - 9));
  
  var featureID = 0;
  var colorID = 0;
  var price = 0;
  var uiFeature = null;
  
  uiFeature = obj;
  
  var objFeatureID = 0;
  var objPrice = 0;
  var objColorID = document.getElementById(base + 'ColorPallet1_Color');
  var objFeatureColor = document.getElementById(base + 'ColorPallet1_Color1');
  var objColorName = document.getElementById(base + 'ColorPallet1_uiColorName');
  var subObj = document.getElementById(base + 'uiSubFeaturesDiv');
  
  if(obj.checked && subObj != null)
    subObj.style.display = "block";
  else if(!obj.checked && subObj != null)
    subObj.style.display = "none";
  
  if(BrowserDetect.browser == "Explorer")
  {
    objFeatureID = uiFeature.FeatureID;
    objPrice = uiFeature.Price;
  }
  else
  {
    objFeatureID = uiFeature.attributes['featureid'].value;
    objPrice = uiFeature.attributes['price'].value;
  }
    
  if(objFeatureID == null)
  {
    base = obj.id.substring(0, (obj.id.length - 12));
    
    uiFeature = document.getElementById(base + 'uiFeature');
    objColorID = document.getElementById(base + 'ColorPallet1_Color');
    objFeatureColor = document.getElementById(base + 'ColorPallet1_Color1');
    objColorName = document.getElementById(base + 'ColorPallet1_uiID');
    objColorName = document.getElementById(base + 'ColorPallet1_uiColorName');
    
    if(BrowserDetect.browser == "Explorer")
    {
      objFeatureID = uiFeature.FeatureID;
      objPrice = uiFeature.Price;
    }
    else
    {
      objFeatureID = uiFeature.attributes['featureid'].value;
      objPrice = uiFeature.attributes['price'].value;
    }
  }
  
  if(objColorID.value != '')
  {
    colorID = objColorID.value;
  }
  
  featureID = objFeatureID;
  price = objPrice;
  
//  if(aval == 2)
//  {
//    obj.checked = true;
//  }
//  else 
  if(aval == 3)
  {
    obj.checked = false;
  }
  var select = false;
  
  if(featureID == 36746 || featureID == 36830)
  {
    for(var i = 0; i < _optionArray.length; i++)
    {
      //alert(_optionArray[i].ID + ':' + _optionArray[i].colorID);
      if(_optionArray[i].ID == 47 && _optionArray[i].colorID == 8256)
      {
        select = true;
        break;
      }
    }
    
    obj.checked = select;
  }
  
  //alert(featureID + ':' + colorID + ':' + parentID + ':' + price + ':');
  if(obj.checked == true)
  {
    //alert('add');
    updateOptions(featureID, colorID, 2, parentID, price, 1)
  }
  else if(obj.checked == false)
  {
    //alert('remove');
    updateOptions(featureID, colorID, 2, parentID, price, 2)
    //objFeatureColor.style.background = 'White';
    //objColorID.value = 0;
    //objColorName.innerHTML = '';
    //objFeatureID.value = 0;
  }
  
  DisplayOptionTotal();
}
function FeatureDescription(featureID)
{
  window.radopen('ModelDescription.aspx?featureID=' + featureID, 'ContentWindow');
  return false; 
}

function OpenWindow(featureID)
{
  var oWnd = window.radopen('ColorPicker.aspx?featureID=' + featureID, 'ContentWindow');
  __doPostBack("", "");
  /*oWnd.add_close(OnRadWindowClose);*/
}

function OnRadWindowClose()
{
  /*__doPostBack("", 'ReBindPlease');*/
  var ajaxManager = $find("ctl00_ContentPlaceHolder1_IspecImage1_RadAjaxManager1");
  alert(ajaxManager);
  ajaxManager.ajaxRequest("ReBind");
}

function LoadMoreInfo(obj, description)
{
  if(description.length > 0)
  {
    obj.style.display = 'block';
  }
  else
  {
    obj.style.display = 'none';
  }
  
}
