// nopcart additions for M+H DRGGroupers.com site
//
// Assumes nopcart and jquery
//
// DHK 8/19/2009

function ButtonFlash (id, img) {
    $("#"+id).attr("src",'images/'+img+'.jpg');
}

function PreloadButtons (img) {
    var on = new Image ();
    var off = new Image ();

    on.src="images/"+img+"_on.jpg";
    off.src="images/"+img+"_off.jpg";
}

// Preload images for flashy shopping cart buttons
function PreloadCartButtons () {
    PreloadButtons("addtocart");
    PreloadButtons("viewcart");
    PreloadButtons("checkout");
    return false;
}

// Update named div with contents of cart (number of items ordered)
// Counts multiples of a single item, as one item

function UpdateCartContents (div) {
   var html;

   iNumInCart = GetCookie("NumberOrdered");
   if ( iNumInCart == null ) iNumInCart = 0;
   if ( iNumInCart == 1 ) html = 'You currently have <strong>' + iNumInCart + '</strong> item in your shopping cart.';
   if ( iNumInCart >= 2 || iNumInCart == 0 ) html = 'You currently have <strong>' + iNumInCart + '</strong> items in your shopping cart.';

  if (iNumInCart > 0) {
    $("." + div).html(html);
  } else {
    $("." + div).html('');
  }

  return false;
}

//---------------------------------------------------------------------||
// FUNCTION:    RemoveAllFromCart                                         ||
// RETURNS:     Null                                                   ||
// PURPOSE:     Removes all items from a users shopping cart             ||
//---------------------------------------------------------------------||
function RemoveAllFromCart() {
   if ( confirm( strRemoveAll ) ) {
       RemoveAllFromCartNoPrompt();
   }
}

function RemoveAllFromCartNoPrompt () {
    NumberOrdered = GetCookie("NumberOrdered");
    for ( i=0; i < NumberOrdered; i++ ) {
	Order = "Order." + (i);
	DeleteCookie(Order, "/");
    }
    DeleteCookie("NumberOrdered", "/");
    //location.href=location.href;
}

strRemoveAll = "Click 'Ok' to remove all products from your shopping cart.";

// Search URL parameters for a particular parameter name
function getURLParam( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
      return "";
  else {
      var val = results[1];
      val = val.replace(/\+/g, " ");
      val = val.replace(/%40/g, "@");
      return val;
  }
}

// Decide whether to reveal the Continue Shopping button. Used
// in managecart.html, this knows too much about the structure of the
// HTML that it manipulates.
function MaybeContinueShopping (div) {
  var continue_url = getURLParam("continue_url");

  if (continue_url) {
    // If a continue_url parameter was passed in, make it the target of the anchor, and reveal the div.
    // This knows waaay too much about the structure of the HTML 
    $("#"+div).attr("href", continue_url);
    $("#"+div).show();
  }  
}

// Is my download ready? If so, kick it off. Used in thankyou.html.
function DownloadOrder () {
    var custom = getURLParam("custom");
    var txnid = getURLParam("txn_id");

    if (custom) {
	$.get("cgi-bin/order-status.cgi", {custom: custom, "txn_id": txnid},
	      function (data) {
		  if (data.length) {
		      if (data.match(/^error/)) {
			  // This happens if the transaction ID is unknown,
			  // which may be a sign of a spoofed order.
			  $("#success").hide();
			  $("#failure").show();
		      } else {
			  window.location.href = data;
		      }
		  } else {
		      // Try again soon
		      window.setTimeout(DownloadOrder, 8000);
		  }
	      } );
    }
}

// Fill in fields in download page, plus do some error handling
function SetupDownloadPage () {
  var custom=getURLParam("custom");

  // Accumulate the ordered items
  var products = getURLParam("item_number1");
  var i = 2;
  while (getURLParam("item_number"+i)) {
      products = products + ", " + getURLParam("item_number"+i);
      i++;
  }
  if (products != "") {
      $("#products").html(products);
  }

  // Display the order details
  orderParams.every( function (elt) {
                       var val = getURLParam(elt);
                       $('#'+elt).html(val);
                       return 1;
                     } );
  $("#details").show();

  if (custom != "" && custom != "undef") {
    // Download the software
    DownloadOrder();
    // Clean up the cart
    RemoveAllFromCartNoPrompt();
  } else {
    $("#success").hide();
    $("#failure").show();
  }
}

