﻿function loadinv(src) {
  var aainvscript = document.createElement("script");
  aainvscript.type = "text/javascript";
  aainvscript.src = src;
  aainvscript.charset = 'ISO-8859-1';
  document.getElementsByTagName('head')[0].appendChild(aainvscript);
}
function colorize(id, dc) {
    var parms = window.location.search.substring(1).split('&');
    var queryStringColor;
    for (var i = 0; i < parms.length; i++) {
        pos = parms[i].indexOf('=');
        if (pos > 0 && parms[i].substring(0, pos) == 'c') {
            queryStringColor = parms[i].substring(pos + 1);
            break;
        }
    }
    if (queryStringColor) {
        swatchChange(id, unescape(queryStringColor));
    }
    else {
        swatchChange(id, dc);
    }
}

function checkUnavailable(unavailable, deadItems) {
    unavailable = ' - ' + unavailable;
    var color = "";
    var size = "";
    var menuSize = document.getElementById('Size');
    var menuColor = document.getElementById('Color');

    if (menuSize && menuColor) {
        for (var j = 0; j < menuSize.options.length; j++) {
            menuSize.options[j].text = menuSize.options[j].text.replace(unavailable, "");
        }

        for (var i = 0; i < deadItems.length; i++) {
            color = deadItems[i].substring(0, deadItems[i].indexOf("_"));
            size = deadItems[i].substring(deadItems[i].indexOf("_") + 1, deadItems[i].length);

            if (menuColor.value == color) {
                for (var j = 0; j < menuSize.options.length; j++) {
                    if (menuSize.options[j].value == size) {
                        menuSize.options[j].text += unavailable;
                    }
                }
            }
        }
    }
}


function changeImage(style, color, colorArray, msg1, array1, msg2, array2) {
    var i;

    var dlColor = document.getElementById('Color');
    if (dlColor) {
        var menu = dlColor.options;
        for (i = 0; i < menu.length; i++)
            if (menu[i].value == color)
            menu.selectedIndex = i;
    }
    var element;
    for (i = 0; i < colorArray.length; i++) {
        element = document.getElementById(colorArray[i]);
        element.className = 'colorName';
        element.style.backgroundColor = '#F6F5F4';
    }
    element = document.getElementById(color);
    if (element) {
        element.style.backgroundColor = '#e0e1e3';
        element.className = 'colorNameSelect';
    }
    /**/
    color = color.replace(/ /g, '');
    color = color.replace(/\//g, '_');
    color = color.replace(/è/g, 'e');
    color = color.replace(/é/g, 'e');
    var iurl = "http://i.americanapparel.net/storefront/images/detail/serve.asp?media=" + style + "_" + color + ".jpg";
    ImageTransition(style, iurl);

    checkUnavailable(msg1, array1);
    checkUnavailable(msg2, array2);
}

function checkout(order) {
    var eS = document.getElementById('Size');
    var eC = document.getElementById('Color');
    var eQ = document.getElementById('Quantity');
    var url = order.StoreCart;
    var yahoo = (url == 'http://order.store.yahoo.net/cgi-bin/wg-order');
    if (yahoo) {
        url += "?vwitem=" + order.StyleID
            + "&vwcatalog=" + order.YahooCode
            + "&vwquantity=" + eQ.value
    }
    else {
        url += '?storeid=' + order.OnlineStoreID
            + '&languageid=' + order.LanguageCart
            + '&style=' + order.StyleID
            + '&quantity=' + eQ.value
            + '&add=' + eQ.value
    }
    var msg = '';
    if (eS) {
        if (eS.options[eS.selectedIndex].text.indexOf(order.Unavailble) >= 0) {
            msg = order.MsgUnavailble.replace('Unavailble', 'unavailble');
        }
        else if (eS.selectedIndex == 0) {
            msg = order.MsgSelectSize;
        }
        else {
          if (yahoo) {
            if (eC) {
              url += "&Color=" + escape(eC.value).replace(/\+/g, '%2B');
            }
            url += "&Size=" + escape(eS.value).replace(/\+/g, '%2B');
          } else {
            if (eC) {
              url += "&Color=" + encodeURI(eC.value).replace(/\+/g, '%2B');
            }
            url += "&Size=" + encodeURI(eS.value).replace(/\+/g, '%2B');
          }
        }
    }

    if (msg == '') {
        if (yahoo) {
            url += '&.autodone=http%3A%2F%2F'
              + order.YahooDone
              + '%2F'
              + order.StyleID
              + '.html&x='
              + (order.LanguageCart == 2 ? 'fr' : 'en');
            window.location = url;
        } else {
            if (!eC) {
                url += "&Color=None";
            }
            if (!eS) {
                url += "&Size=None";
            }

            if (order.PageTracker) {
              order.PageTracker._link(url);
            }
            else {
              window.location = url;
            }
        }
        return true;
    }
    else {
        alert(msg);
        return false;
    }
}
function popFindStores(style, gc, name) {
    window.open
    ( "http://www.americanapparel.net/storefront/findastore/?style=" + style
    + "&stylename=" + name
    + "&gc=" + gc
    + "&color=" + encodeURI(document.getElementById('Color').value).toString().replace(/\+/g, '%2B')
    + "&size=" + encodeURI(document.getElementById('Size').value).toString().replace(/\+/g, '%2B')
    , "Popup"
    , "width=451,height=450,location=no,scrollbars=yes,menubars=no,toolbars=no,resize=no"
    );
  }

function LoadPage(data) {
  var e = document.getElementById('Color');

  colorize(data.StyleID, (e && e.options.length == 1 ? e.options[0].value : data.DefaultColor));
  loadinv("http://i.americanapparel.net/storefront/inventory/skucheck_extreme.asp?r=" + data.OnlineStoreID + "&s=" + data.StyleID);

  e = document.getElementById('Size');
  if (e && e.options.length == 2) {
    e.selectedIndex = 1;
  }
}


/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
function ProductPage(data) {
  this.OnlineStoreID = data.OnlineStoreID;
  this.StyleID = data.StyleID;
  this.GenericCode = data.GenericCode;
  this.DefaultColor = data.DefaultColor;
  this.Unavailable = data.Unavailable;
  this.Backordered = data.Backordered;
  this.PreOrder = data.PreOrder;
  this.ColorArray = data.ColorArray;

  this.StoreCart = data.StoreCart;
  this.YahooCode = data.YahooCode;
  this.YahooDone = data.YahooDone;
  this.MsgUnavailble = data.MsgUnavailble;
  this.MsgSelectSize = data.MsgSelectSize;
  this.LanguageID = data.LanguageID;
  this.PageTracker = data.PageTracker;

  this.pnlUnavailableHelpID = data.pnlUnavailableHelpID;
  this.hasUnavailable = false;
  this.hasPreOrder = false;
  this.hasBackordered = false;
  this.CurrentColor = this.DefaultColor;
  this.Sizes = [];
  if (this.OnlineStoreID == 1) {
    var e = document.getElementById("cto_mg_div");
    if (e) {
      e.innerHTML = '<iframe width="1px" height="1px" style="display:none;" src="'
                  + "http://dis.us.criteo.com/dis/dis.aspx?p1=" + escape("v=2&wi=7712575&pt1=2&i=" + this.StyleID) + "&t1=sendEvent&p=2514&c=2&cb=" + Math.floor(Math.random() * 99999999999)
                  + '"></iframe>';
    }
  }
  else if (this.OnlineStoreID == 19) {
    var e = document.getElementById('Size');
    for (var j = 0; j < e.options.length; j++) {
      this.Sizes[j] =
      { "text": e.options[j].text
      , "value": e.options[j].value
      };
    }
  }
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
ProductPage.prototype.findInStores = function (gc, name, culture) {
  var e = window.open
    ("http://www.americanapparel.net/storefront/reservations/default.aspx?sid=" + this.StyleID
  //+ "&stylename=" + name
  //+ "&gc=" + gc
    + "&c=" + encodeURI(document.getElementById('Color').value).toString().replace(/\+/g, '%2B')
    + "&s=" + encodeURI(document.getElementById('Size').value).toString().replace(/\+/g, '%2B')
    + "&o=" + this.OnlineStoreID
    + "&l=" + this.LanguageID
    + "&culture=" + culture
    , "Popup"
    , "width=650,height=650,location=no,scrollbars=yes,menubars=no,toolbars=no,resize=no"
    );
  if (e) e.focus();
  return false;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
ProductPage.prototype.completeThisLook = function() {
  if (popIt) {
    popIt('760', '680', '1', 'CompleteThisLook', "product/popup/completelook.aspx?style=" + this.StyleID)
  }
  return false;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
ProductPage.prototype.showHelpLinks = function () {
  //if (this.LanguageID != 1 && this.LanguageID != 2) return;
  var e;
  e = document.getElementById("spnPushDownSize"); if (e) e.style.display = "none";
  e = document.getElementById("pnlPopupLinkPreOrder"); if (e) e.style.display = "none";
  e = document.getElementById("pnlPopupLinkUnavailable"); if (e) e.style.display = "none";
  e = document.getElementById(this.pnlUnavailableHelpID); if (e) e.style.marginTop = (this.LanguageID == 10 ? "-30px" : "-25px");
  e = document.getElementById("lblShippingInfoLink"); if (e) e.style.display = (this.hasBackordered ? "" : "none");

  if (this.hasUnavailable || this.hasPreOrder) {
    if (this.hasUnavailable && this.LanguageID != 2) {
      e = document.getElementById("pnlPopupLinkUnavailable");
      if (e) e.style.display = "";
    }
    if (this.hasPreOrder) {
      e = document.getElementById("pnlPopupLinkPreOrder");
      if (e) e.style.display = "";
    }
    if (this.hasUnavailable && this.hasPreOrder && this.LanguageID == 1) {
      e = document.getElementById(this.pnlUnavailableHelpID);
      if (e) e.style.marginTop = "-35px";

      e = document.getElementById("spnPushDownSize");
      if (e) e.style.display = "";
    }
  }
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
ProductPage.prototype.showHide = function(id) {
  var e = document.getElementById(id);
  if (e) {
    e.style.display = (e.style.display == "none" ? "" : "none");
  }
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
ProductPage.prototype.ShowReviews = function () {
  popIt('408', '444', '1', 'Ratings'
  , 'http://www.americanapparel.net/storefront/ratings/Rating.aspx'
  + '?r=' + this.OnlineStoreID
  + '&s=' + this.StyleID
  + '&gc=' + this.GenericCode
  + '&c=' + this.CurrentColor
  );
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
ProductPage.prototype.changeImage = function (color) {
  this.CurrentColor = color;
  var i;
  var e;
  var e = document.getElementById('Color');
  if (e) {
    for (i = 0; i < e.options.length; i++)
      if (e.options[i].value == color)
        e.options.selectedIndex = i;
  }
  for (i = 0; i < this.ColorArray.length; i++) {
    e = document.getElementById(this.ColorArray[i]);
    e.className = 'colorName';
    e.style.backgroundColor = '#F6F5F4';
  }
  e = document.getElementById(color);
  if (e) {
    e.style.backgroundColor = '#e0e1e3';
    e.className = 'colorNameSelect';
  }
  color = color.replace(/ /g, '');
  color = color.replace(/\//g, '_');
  color = color.replace(/è/g, 'e');
  color = color.replace(/é/g, 'e');
  var iurl = "http://i.americanapparel.net/storefront/images/detail/serve.asp?media=" + this.StyleID + "_" + color + ".jpg";
  ImageTransition(this.StyleID, iurl);

  this.checkAvailability();
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
ProductPage.prototype.swatchChange = function(style, color) {
  page.changeImage(color);
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
ProductPage.prototype.checkSKU = function (unavailable, deadItems) {
  unavailable = ' - ' + unavailable;
  var color = "";
  var size = "";
  var menuSize = document.getElementById('Size');
  var menuColor = document.getElementById('Color');
  var hasSome = false;

  if (menuSize && menuColor) {
    var pickedColor = menuColor.value;
    if (this.OnlineStoreID == 19) {

      for (var i = 0; i < deadItems.length; i++) {
        color = deadItems[i].substring(0, deadItems[i].indexOf("_"));
        size = deadItems[i].substring(deadItems[i].indexOf("_") + 1, deadItems[i].length);
        if (pickedColor == color) {
          for (var j = 0; j < this.SizesTemp.length; j++) {
            if (this.SizesTemp[j].value == size) {
              this.SizesTemp[j].value = '';
              this.SizesTemp[j].text = '';
              hasSome = true;
            }
          }
        }
      }
      var html = '<select style="font:10px verdana;color:#333333;" id="Size" name="Size">';
      for (var j = 0; j < this.SizesTemp.length; j++) {
        if (this.SizesTemp[j].value && this.SizesTemp[j].text) {
          html += '<option value="' + this.SizesTemp[j].value + '">' + this.SizesTemp[j].text + '</option>';
        }
      }
      menuSize = document.getElementById('divSize');
      if (menuSize) {
        menuSize.innerHTML = html + '</select>';
      }
    }
    else {
      for (var j = 0; j < menuSize.options.length; j++) {
        menuSize.options[j].text = menuSize.options[j].text.replace(unavailable, "");
      }

      for (var i = 0; i < deadItems.length; i++) {
        color = deadItems[i].substring(0, deadItems[i].indexOf("_"));
        size = deadItems[i].substring(deadItems[i].indexOf("_") + 1, deadItems[i].length);
        if (pickedColor == color) {
          for (var j = 0; j < menuSize.options.length; j++) {
            if (menuSize.options[j].value == size) {
              menuSize.options[j].text += unavailable;
              hasSome = true;
            }
          }
        }
      }
    }
  }
  return hasSome;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
ProductPage.prototype.checkAvailability = function () {
  this.SizesTemp = [];
  for (var j = 0; j < this.Sizes.length; j++) {
    this.SizesTemp[j] =
        { "text": this.Sizes[j].text
        , "value": this.Sizes[j].value
        };
  }
  this.hasUnavailable = this.checkSKU(this.Unavailable, DeadSKU);
  this.hasBackordered = this.checkSKU(this.Backordered, BOSKU);
  this.hasPreOrder = this.checkSKU(this.PreOrder, POSKU);

  if (this.OnlineStoreID != 19) {
    setTimeout("page.showHelpLinks()", 100);
  }
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
ProductPage.prototype.checkout = function() {
  var eS = document.getElementById('Size');
  var eC = document.getElementById('Color');
  var eQ = document.getElementById('Quantity');
  var url = this.StoreCart;
  var yahoo = (url == 'http://order.store.yahoo.net/cgi-bin/wg-order');
  if (yahoo) {
    url += "?vwitem=" + this.StyleID
            + "&vwcatalog=" + this.YahooCode
            + "&vwquantity=" + eQ.value
  }
  else {
    url += '?storeid=' + this.OnlineStoreID
            + '&languageid=' + this.LanguageID
            + '&style=' + this.StyleID
            + '&quantity=' + eQ.value
            + '&add=' + eQ.value
  }
  var msg = '';
  if (eS) {
    if (eS.options[eS.selectedIndex].text.indexOf(this.Unavailable) >= 0) {
      msg = this.MsgUnavailble.replace('Unavailable', 'unavailable');
    }
    else if (eS.selectedIndex == 0) {
      msg = this.MsgSelectSize;
    }
    else {
      if (yahoo) {
        if (eC) {
          url += "&Color=" + (this.OnlineStoreID == 5 || this.OnlineStoreID == 7 ? eC.value : escape(eC.value)).replace(/\+/g, '%2B');
        }
        url += "&Size=" + (this.OnlineStoreID == 5 || this.OnlineStoreID == 7 ? eS.value : escape(eS.value)).replace(/\+/g, '%2B');
      } else {
        if (eC) {
          url += "&Color=" + encodeURI(eC.value).replace(/\+/g, '%2B');
        }
        url += "&Size=" + encodeURI(eS.value).replace(/\+/g, '%2B');
      }
    }
  }

  if (msg == '') {
    var category = document.referrer;
    if (category) {
      if ( category.toLowerCase().indexOf('cgi-bin/wg-order?') != -1 ) category = '';
      category = category.substr(category.indexOf('/', 10) + 1);
      category = category.substr(0, category.indexOf(".html"));
    }
    if (yahoo) {
      url += '&.autodone=http%3A%2F%2F'
              + this.YahooDone
              + '%2F'
              + this.StyleID
              + '.html&x='
              + (this.LanguageID == 1 ? 'en'
              : (this.LanguageID == 2 ? 'fr'
              : (this.LanguageID == 3 ? 'de'
              : (this.LanguageID == 4 ? 'ja'
              : (this.LanguageID == 5 ? 'da'
              : (this.LanguageID == 6 ? 'nl'
              : (this.LanguageID == 7 ? 'he'
              : (this.LanguageID == 8 ? 'it'
              : (this.LanguageID == 9 ? 'ko'
              : (this.LanguageID == 10 ? 'es'
              : 'en'))))))))))
              + (category ? '-' + category : '')
              ;
      window.location = url;
    } else {
      if (!eC) {
        url += "&Color=None";
      }
      if (!eS) {
        url += "&Size=None";
      }
      if (category) url += '&Category=' + category;
      if (this.PageTracker) {
        this.PageTracker._link(url);
      }
      else {
        window.location = url;
      }
    }
    return true;
  }
  else {
    alert(msg);
    return false;
  }
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
