var strCurrProductCat            =  "2";
var strCurrDesignCat             = "89";
var g_fWaitingForProductCallback = false;
var g_fWaitingForDesignCallback  = false;
var g_lastSelectedPLDiv          = null;
var g_lastSelectedDLDiv          = null;
var g_strCurrProductCode         = "";
var g_productHoverTimeout        = null;
var g_designHoverTimeout         = null;

function embedBuildStage(strItemChangeType, strItemChangeData)
{
  var params    = { wmode: "transparent", quality: "best", scale: "exactfit", base: "/" };
  var flashvars = { fv_render_type: "LAYOUT", fv_render_width: 200, fv_render_height: 200, fv_item_change_type: strItemChangeType, fv_item_change_data: strItemChangeData };
  swfobject.embedSWF("/TSDFlashStage.swf", "build_stage_container", "100%", "340", "8.0.0", "", flashvars, params);
}

function GetBuildStage()
{
  if (navigator.appName.indexOf("Microsoft") != -1)
    return window["build_stage_container"];
  else
    return document["build_stage_container"];
}

function SetDesignListTitle(strText)
{
  var objDivMain      = GetDOMObject("design_list_title");
  var objDivContainer = GetDOMObject("design_list_title_container");
  if (objDivMain && objDivContainer)
  {
    if (strText.length > 0)
    {
      objDivContainer.innerHTML = unescape(strText.replace(/\+/g, " "));
      objDivMain.style.display = "block";
    }
    else
    {
      objDivContainer.innerText = "";
      objDivMain.style.display = "none";
    }
  }
}

function SelectProductByCode_SideList(strCode)
{
  list.SelectItemByUID("pl_" + strCode, true);
}

function SelectDesignCategoryByID_SideList(strID)
{
  if (g_lastSelectedDLDiv)
    g_lastSelectedDLDiv.className = "";
  if (!design_cat_list.SelectItemByUID("dl_" + strID, true))
  {
    //Item is not in a collapsible list, so select from the main list
    design_cat_list.DeselectAllItems();
    var div = GetDOMObject("dclnp_" + strID);
    if (div)
    {
      div.className = "selected";
      g_lastSelectedDLDiv = div;
    }
  }
}

function GetCurrentProductCode()
{
  return (g_strCurrProductCode.length > 0) ? g_strCurrProductCode : null;
}

function callback_LoadStageSide(strProductCode)
{
  var iframe = GetIFrameWindow("build_stage_side_frame");
  if (iframe)
    iframe.location.href = "/build_stage_side.aspx?m=1&productCode=" + escape(strProductCode);
}

function callback_RefreshSummary()
{
  var iframe = GetIFrameWindow("build_stage_summary_frame");
  if (iframe)
    iframe.location.reload();
}

function callback_ProductChange(strCode, strSizeCode, strPriColour, strSecColour)
{
  g_strCurrProductCode = strCode;
  setTimeout("_timeout_ProductChange('" + strCode + "', '" + strSizeCode + "', '" + strPriColour + "', '" + strSecColour + "');", 2000);
}

function _timeout_ProductChange(strCode, strSizeCode, strPriColour, strSecColour)
{
  var iframe = null;
  iframe = GetIFrameWindow("build_stage_side_frame");
  if (iframe)
  {
    iframe.highlightSizeByCode(strSizeCode);
    iframe.highlightColourByNames(strPriColour, strSecColour);
  }
  iframe = GetIFrameWindow("product_list");
  if (iframe)
    iframe.highlightProductByCode(strCode);
  SelectProductByCode_SideList(strCode);
}

function callback_FlashAlert(strMessage)
{
  if (strMessage != null && strMessage.length > 0 && strMessage.toUpperCase() != "NULL")
    alert(strMessage);
}

function ChangeProductCategory(strCategoryID)
{
  var iframe = GetIFrameWindow("product_list");
  if (iframe)
  {
    iframe.location.href = "/product_list.aspx?m=c&catID=" + escape(strCategoryID);
    strCurrProductCat = strCategoryID;
  }
}

function ChangeDesignCategory(strCategoryID)
{
  var iframe = GetIFrameWindow("design_list");
  if (iframe)
  {
    iframe.location.href = "/design_list.aspx?m=STANDARD_C&catID=" + escape(strCategoryID);
    strCurrDesignCat = strCategoryID;
  }
}

function DesignList_DesignMode()
{
  var iframe = GetIFrameWindow("design_list");
  if (iframe)
    iframe.location.href = "/design_list.aspx?m=STANDARD_C";
}

function DesignList_TextMode()
{
  var iframe = GetIFrameWindow("design_list");
  if (iframe)
    iframe.location.href = "/design_list_text.aspx";
}

function DesignList_UploadMode()
{
  var iframe = GetIFrameWindow("design_list");
  if (iframe)
    iframe.location.href = "/design_list_upload.aspx";
}

function AddToBasket()
{
  var elQty = GetDOMObject("basket_qty");
  var fValid = true;
  if (elQty)
  {
    if (isNaN(elQty.value))
      fValid = false;
    else if (elQty.value < 1)
      fValid = false;
    if (fValid)
    {
      var bs = GetBuildStage();
      if (bs)
        fValid = bs.IsProductValid();
      if (fValid)
      {
        if (confirm("Please check the garment size before adding it to the basket.\n\nIf the size is correct, click OK to add it to the basket otherwise click Cancel and change the size above."))
          document.location.href = "/basket.aspx?bm=ADD&qty=" + escape(elQty.value);
      }
      else
        alert("Please select a product before adding it to the basket");
    }
    else
      alert("Please enter a number greater than zero in the box to the left");
  }
}

function ViewBasket()
{
  document.location.href = "/basket.aspx?bm=SHW";
}

function FinishEditing()
{
  var fValid = true;
  var bs = GetBuildStage();
  if (bs)
    fValid = bs.IsProductValid();
  if (fValid)
    document.location.href = "/basket.aspx?bm=FIN";
  else
    alert("Please select a product before finishing");
}

function Product_ShowHoverInfo(strCode)
{
  clearTimeout(g_productHoverTimeout);
  g_productHoverTimeout = setTimeout("_timeout_Product_ShowHoverInfo('" + strCode + "');", 300);
}

function _timeout_Product_ShowHoverInfo(strCode)
{
  if (!g_fWaitingForProductCallback)
  {
    clearTimeout(g_productHoverTimeout);
    var elDIV = GetDOMObject("hover_product");
    if (elDIV)
    {
      elDIV.style.display = "block";
      g_fWaitingForProductCallback = true;
      XHR_Get("/hover_product.aspx?code=" + escape(strCode), true, callback_XHR_ProductHover);
    }
  }
}

function Product_HideHoverInfo()
{
  clearTimeout(g_productHoverTimeout);
  g_fWaitingForProductCallback = false;
  var elDIV = GetDOMObject("hover_product");
  if (elDIV)
  {
    elDIV.style.display = "none";
    ClearContainer(elDIV);
  }
}

function Design_ShowHoverInfo(strCode)
{
  clearTimeout(g_designHoverTimeout);
  g_designHoverTimeout = setTimeout("_timeout_Design_ShowHoverInfo('" + strCode + "');", 300);
}

function _timeout_Design_ShowHoverInfo(strCode)
{
  if (!g_fWaitingForDesignCallback)
  {
    clearTimeout(g_designHoverTimeout);
    var elDIV = GetDOMObject("hover_design");
    if (elDIV)
    {
      elDIV.style.display = "block";
      g_fWaitingForDesignCallback = true;
      XHR_Get("/hover_design.aspx?code=" + escape(strCode), true, callback_XHR_DesignHover);
    }
  }
}

function Design_HideHoverInfo()
{
  clearTimeout(g_designHoverTimeout);
  g_fWaitingForDesignCallback = false;
  var elDIV = GetDOMObject("hover_design");
  if (elDIV)
  {
    elDIV.style.display = "none";
    ClearContainer(elDIV);
  }
}

function DesignToolbar_SelectByMode(strMode)
{
  var fDesigns = false;
  var fText    = false;
  var fUpload  = false;
  switch (strMode.toUpperCase())
  {
    case "DESIGNS":
      fDesigns = true;
      break;
    case "TEXT":
      fText = true;
      break;
    case "UPLOAD":
      fUpload = true;
      break;
  }
  var objDesigns = GetDOMObject("designToolbar_DesignMode");
  var objText    = GetDOMObject("designToolbar_TextMode");
  var objUpload  = GetDOMObject("designToolbar_UploadMode");
  if (objDesigns && objText && objUpload)
  {
    objDesigns.setAttribute("class", (fDesigns ? "button_selected" : "button"));
    objDesigns.className = (fDesigns ? "button_selected" : "button");
    objText.setAttribute("class", (fText ? "button_selected" : "button"));
    objText.className = (fText ? "button_selected" : "button");
    objUpload.setAttribute("class", (fUpload ? "button_selected" : "button"));
    objUpload.className = (fUpload ? "button_selected" : "button");
  }
}

function callback_XHR_ProductHover(objXHR)
{
  if (objXHR.readyState == 4)
  {
    g_fWaitingForProductCallback = false;
    if (objXHR.status == 200)
    {
      var elDIV = GetDOMObject("hover_product");
      if (elDIV)
        elDIV.innerHTML = objXHR.responseText;
    }
  }
}

function callback_XHR_DesignHover(objXHR)
{
  if (objXHR.readyState == 4)
  {
    g_fWaitingForDesignCallback = false;
    if (objXHR.status == 200)
    {
      var elDIV = GetDOMObject("hover_design");
      if (elDIV)
        elDIV.innerHTML = objXHR.responseText;
    }
  }
}
