﻿
var http_request = false;
// ----------------------------------------------------------------------------
function makePOSTRequest(url, parameters, responseCallback, responseArgs) {
  http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
    http_request = new XMLHttpRequest();
    if (http_request.overrideMimeType) {
      http_request.overrideMimeType('text/html');
    }
  } else if (window.ActiveXObject) { // IE
    try {
      http_request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        http_request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) { }
    }
  }
  if (!http_request) {
    alert('Could not load XMLHTTP');
    return false;
  }
  http_request.onreadystatechange = function() { responseCallback(responseArgs) };
  http_request.open('POST', url, true);
  http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http_request.setRequestHeader("Content-length", parameters.length);
  http_request.setRequestHeader("Connection", "close");
  http_request.send(parameters);
}
// ----------------------------------------------------------------------------
function StoreSharing(args) {
  this.StyleID = '';
  this.Name = '';
  this.LanguageID = 1;
  this.Tweet = '';
  // --------------------------------------------------------------------------
  this._GetColor = function() {
    var e = document.getElementById('Color');
    return (e && e.value ? e.value : 'None'); 
  }
  // --------------------------------------------------------------------------
  this._GetHref = function() {
    var href = document.location.toString();
    var color = this._GetColor();
    if (color) {
      var i0 = href.indexOf('&c=');
      if (i0 > -1) {
        href = href.substr(0, i0);
      }
      href += (href.indexOf('?') < 0 ? '?' : '&') + 'c=' + escape(color);
    }
    return href;
  }
  // --------------------------------------------------------------------------
  this.UpdateShare = function() {
    var e;
    var tweet = this.Tweet;
    if (tweet) {
      tweet = tweet.replace('{0}', encodeURI(this.Name));
      tweet = tweet.replace('{1}', 'http://dim.ly/' + this.StyleID + '.html');
    }
    e = document.getElementById("popShareTweet");
    if (e) e.href = "http://twitter.com/home?status=" + tweet;
    e = document.getElementById("popShareFacebook");
    if (e) {
      var domain = document.domain;
      switch (domain) {
      case "localhost":
      case "usd.dev.web.americanapparel.net":
      case "origin-store.americanapparel.net":
        domain = "store.americanapparel.net"; 
        break;
      case "mxn.dev.web.americanapparel.net":
      case "origin-store.americanapparel.com.mx":
        domain = "store.americanapparel.com.mx"; 
        break;
      case "cad.dev.web.americanapparel.net":
      case "origin-store.americanapparel.ca":
        domain = "store.americanapparel.ca"; 
        break;
      case "gbp.dev.web.americanapparel.net":
      case "origin-store.americanapparel.co.uk":
        domain = "store.americanapparel.co.uk"; 
        break;
      }
      e.href = "http://www.facebook.com/sharer.php?u=http://" + domain + "/" + this.StyleID + ".html"
    }
  }
  // --------------------------------------------------------------------------
  this._Show = function(id, show) {
    var e = document.getElementById(id);
    if (e) e.style.display = (show ? '' : 'none');
  }
  this.ShareEmailIDs = ["txtShareEmailName", "txtShareEmailFrom", "txtShareEmailTo", "txtShareEmailMsg"];
  // --------------------------------------------------------------------------
  this.ShareEmail = function() { this._Show('popShare', true); }
  // --------------------------------------------------------------------------
  this._ShowError = function(errorID) {
    this._Show('divErrors', errorID);
    for (var i = 0; i < 2; i++) {
      this._Show('spnError' + i, false);
    }
    this._Show('spnError' + errorID, true);
  }  
  // --------------------------------------------------------------------------
  this.ShareEmailResponse = function(localThis) {
  
    //var localThis = this;
    if (http_request) {
      if (http_request.readyState == 4) {
        var sent = false;
        if (http_request.status == 200) {
          sent = (http_request.responseText == '1');
        }
        if (sent) {
          localThis._ShowError(0);
          localThis.ShareEmailReset(true);
          localThis._Show('popShareEmail', false);
          localThis._Show('popShareDone', true);

        } else {
          localThis._ShowError(2);
        }
      }
    }
  }  
  // --------------------------------------------------------------------------
  this.ShareEmailRequest = function() {
    if (this._ValidateShareEmail()) {
      var poststr = ''
      + '&l=' + this.LanguageID
      + '&StyleID=' + this.StyleID
      + '&Color=' + this._GetColor()
      ;
      var id;
      for (var i = 0; i < this.ShareEmailIDs.length; i++) {
        id = this.ShareEmailIDs[i];
        e = document.getElementById(id);
        if (e && e.value) {
          id = id.substr(13);
          poststr += '&' + id + '=' + encodeURI(e.value);
        }
      }
      var url = '/controls/email.aspx'
      makePOSTRequest(url, poststr, this.ShareEmailResponse, this);
    }
    return false;
  }
  // --------------------------------------------------------------------------
  this.ShareEmailReset = function(showSharePopUp) {
    this._Show('popShareEmail', true);
    this._Show('popShareDone', false);
    this._ShowError(0);
    var e;

    for (var i = 0; i < this.ShareEmailIDs.length; i++) {
      e = document.getElementById(this.ShareEmailIDs[i]);
      if (e) {
        e.value = '';
        e.style.borderColor = '';
      }
    }
    this._Show('popShare', showSharePopUp);
  }
  // --------------------------------------------------------------------------
  this._regexInvalidName = new RegExp("[^\d\w ]");
  this._regExpMsg = new RegExp("\s*\S*{1,2,3,4}\s*");
  this.isValidEmail = function(s) { return (s && s.lastIndexOf(".") > 2) && (s.indexOf("@") > 0); }
  // --------------------------------------------------------------------------
  this._ValidateShareEmail = function() {
    var e;
    // Errors: 
    // 0: No Error
    // 1: Missing
    // 2: Too Long
    // 3: Invalid
    var max = [64, 64, 512, 512];
    var errors = [];
    var error;
    var errorCount = 0;
    var errorBorderColor = "#E30000";
    for (var i = 0; i < this.ShareEmailIDs.length; i++) {
      e = document.getElementById(this.ShareEmailIDs[i]);
      error = 0;
      if (e) {
        if (!e.value) {
          error = 1;
        }
        else {
          if (e.value.lengh > max[i]) {
            error = 2;
          }
          else {
            switch (i) {
              case 0: // Name
                if (!this._regexInvalidName.test(e.value)) {
                  error = 3;
                }
                break;
              case 1: // Email
                if (!this.isValidEmail(e.value)) {
                  error = 3;
                }
                break;
              case 2: // Emails
                if (!this.isValidEmail(e.value)) {
                  error = 3;
                }
                break;
              case 3: // Msg
                if (false) {
                  error = 3;
                }
                break;
            }
          }
        }
        e.style.borderColor = (error > 0 ? errorBorderColor : '');
      }
      if (error > 0) {
        errorCount++;
      }
    }
    var valid = true;

    if (errorCount > 0) {
      //this._ShowError(1);
      valid = false;
    }
    return valid;
  }
  // --------------------------------------------------------------------------
}























