function insertAfter(referenceNode, node) {
  referenceNode.parentNode.insertBefore(node, referenceNode.nextSibling);
}

function cancelCart() {
   if(confirm("Are you sure you want to return to the cart?"))
      window.location = '/cart';
}

function verifyCheck(){
   var result = true;
   var elem = document.getElementById('restrictionBox');
   var elemText = document.getElementById('restrictionError');
   if(elem && !elem.checked){
      result = false;
      if(elemText)
         elemText.style.display = 'block';
   } else {
      if(elemText)
         elemText.style.display = 'none';
   }
   return result;
}

var currentTotal = '';
function calcCoupon(){
   var orderTotal = $('orderTotal');
   // set current total
   if(currentTotal == '')
      currentTotal = orderTotal.innerHTML;
   // set up a few vars
   var errClass = 'couponError';
   var okClass = 'couponMsg';
   var couponCode = $('couponCode').value;
   var couponButton = $('couponButton');
   var couponMsg = $('couponMsg');
   
   var mode = couponButton.value;
   
   // replace button with spinner
   
   if(mode == 'Add' && couponCode.length > 0){
      couponMsg.update('<p>Applying Coupon...</p>');
      couponMsg.className = okClass;
      //callback
      var ajax = new Ajax.Request('/coupon?_action=AddCoupon&code=' + couponCode, {
         evalJSON: true, 
         method: 'post', 
         onSuccess: function(t) {
            json = t.responseText.evalJSON();
            if(!json.err){
               couponButton.value = 'Remove';
               if(!$('couponRow')){
                  var newRow = document.createElement('tr');
                  newRow.className = 'totalsRow';
                  newRow.id = 'couponRow';
                  insertAfter($('couponInsert'), newRow);
               }
               $('couponRow').update(json.newRow);
               orderTotal.update(json.newTotal);
               var idx = 0;
               for(var i = 0; i < $('couponRow').parentNode.childNodes.length; i++){
                  if(idx == 0){
                     var item = $('couponRow').parentNode.childNodes[i];
                     if(item.className != undefined){
                        if(item.className.indexOf('totalsRow') > -1){
                           idx = i - 1;
                           for(var z = 0; z < json.giftRow.length; z++){
                              var newRow = document.createElement('tr');
                              newRow.className = 'itemRow giftRow';
                              insertAfter($('couponRow').parentNode.childNodes[idx], newRow);                           
                              newRow.update(json.giftRow[z]);
                           }
                        }
                     }
                  }
               }
            }
            var err = json.err ? true : false;
            var msg = err ? json.err : json.msg;
            // success message
            couponMsg.update(msg);
            couponMsg.className = err ? errClass : okClass;
         }
      });
   }else if(couponCode.length > 0){
      couponMsg.update('<p>Removing Coupon...</p>');
      couponMsg.className = okClass;
      //callback
      var ajax = new Ajax.Request('/coupon?_action=RemoveCoupon', {
         evalJSON: true, 
         method: 'post', 
         onSuccess: function(t) {
            json = t.responseText.evalJSON();
            $('orderTotal').update(currentTotal);
            couponButton.value = 'Add';
            var toGo = [];
            for(var i = 0; i < $('couponRow').parentNode.childNodes.length; i++){
               var item = $('couponRow').parentNode.childNodes[i];
               if(item.className != undefined){
                  if(item.className.indexOf('giftRow') > -1)
                     toGo.push(item);
               }
            }
            for(var i = 0; i < toGo.length; i++){
               toGo[i].parentNode.removeChild(toGo[i]);
            }
            $('couponRow').parentNode.removeChild($('couponRow'));
            couponMsg.update(json.msg);
            couponMsg.className = okClass;
         }
      });
   }
}

