/*
------------------------
Cart/Checkout JavaScript
(c) Shoebuy.com, Inc.
------------------------
*/

/*
 This extends jquery so that you can create a counter for inputs that have a maximum length
 eg. $('#some_id_of_textarea').counter()
 the following options can also be defined:

 counter_text -- this is the text defined after the number of characters default is characters left
 max_length -- this is the maximum characters of the field default is 250

*/
if (typeof(jQuery) != "undefined") {
    (function($) {
        $.fn.counter = function(options) {
            var defaults = {
                counter_text: 'characters left',
                max_length: 250
            };
            var opts = $.extend(defaults, options);

            var $this = $(this);
            $this.after('<p><span class="counter"></span> ' + opts.counter_text + '</p>');
            $this.parent().find('.counter').html(opts.max_length - $this.val().length);
            $this.keyup(function(e) {
                $(e.target).parent().find('.counter').html(opts.max_length - $(this).val().length);
            });
        }
    })(jQuery);
}

function popup(url, height) {

  if (!height) {
    height = 560;
  }
  var params = 'width=525,height=' + height + ',dependent=1,resizable=1,scrollbars=1';
  newwindow=window.open(url,'name',params);
  return false;
}

var stayOffer = true;
if (document.URL.indexOf("/cust") > 0 && ("$in{OrderID}".length == 0  || "$in{myaction}" == "checkoutLogin")) {
   stayOffer = false;
} else if( document.URL.indexOf("/cart") > 0 && "$in{checkout}".length == 0) {
   stayOffer = false;
}

// Retrieve the value of the cookie with the specified name.
function GetCookie(sName) {
   // cookies are separated by semicolons
   var aCookie = document.cookie.split("; ");
   for (var i=0; i < aCookie.length; i++)
   {
      // a name/value pair (a crumb) is separated by an equal sign
      var aCrumb = aCookie[i].split("=");
      if (sName == aCrumb[0])
         return unescape(aCrumb[1]);
   }

  // a cookie with the requested name does not exist
  return null;
}

function notOffer() {
   stayOffer = false;
}

function setOnClicks(widget) {
   var links;
   var link_arr = document.links;
   var numlinks = link_arr.length;
   for (var iter = 0; iter < numlinks; iter++) {
      var mylink = link_arr[iter];
      var l_action = mylink.href;
      if (l_action.indexOf("javascript") != 0 && l_action.indexOf("shoebuy.com/") > 0 && !mylink.onclick) {
          mylink.onclick = notOffer;
      }
   }

   link_arr = document.forms;
   for (var iter = 0; iter < link_arr.length; iter++)
   {
      if (link_arr[iter].name != "internationalCheckoutForm") {
         link_arr[iter].onsubmit = notOffer;
      }
   }
}

function pleaseStay(popupUrl) {
   if (GetCookie('id') == null || GetCookie('seenLeaveCart') != null || GetCookie('thispromo') != null) {
      stayOffer = false;
   }
   
   if (stayOffer) {
      var coords = "";
      var winx = window.screenLeft; // + 10; 
      var winy = window.screenTop;// + 10;
      if (winx != null && winy != null) {
         winx += 10;
         winy += 10;
         coords = 'left=' + winx + ',top=' + winy + ',';
      }
      if (popupUrl == undefined)
         popupUrl = '/exitPopup.html';
      var newmp = window.open(popupUrl, 'marketingPopup',coords + 'width=1,height=1,location=0,toolbar=0,status=0,menubar=0');
      window.focus();
   }
}

function setBML90dayOffer() {
   var offer_text = document.getElementById("bml90_text");
   var bml_radio = document.getElementById("paymentType_bml");
   
   if (bml_radio && offer_text) {
      if (bml_radio.checked) {
         offer_text.style.cssText = "color:black;display:inline;";
         document.getElementById("bml90day_checkbox").disabled=false;
      } else {
         offer_text.style.cssText = "color:gray;display:inline;";
         document.getElementById("bml90day_checkbox").disabled=true;
      }
   }
}

function goBorderFree() {
   var exp_date = new Date();
   notOffer(); //Don't offer exitpop '
   document.cookie = "id=''; path=/; expires=" + exp_date.toGMTString();
   return true;
}
