function tributeHelp() {
	$('#tributeInstructions').fadeIn('slow'); 
	$('.makeThisDonationMessage').effect('highlight', null, 2500);
}



function textCounterGcart(field,maxlimit,itemid) 
{
   if (field.value.length > maxlimit)
   {
     alert('You have reached '+maxlimit+' characters');
     field.value = field.value.substring(0, maxlimit);
   }
   var count = maxlimit - field.value.length;
   document.getElementById('characters'+itemid).innerHTML = '&nbsp;'+ count + '&nbsp;characters remaining';
   //return count;
}


function clearError(itemId) {
   var cells = $("td.cell" + itemId);
   $("td.cell" + itemId).attr("class","ctrlHolder cell" + itemId);
   $("#errorMessage" + itemId).css("display","none");
}

function showError(itemId, errorMessage) {
   var cells = $("td.cell" + itemId);
   $("td.cell" + itemId).attr("class","ctrlHolder error cell" + itemId);
   
   $("#errorMessage" + itemId).css("display","block");
   $("#errorMessage" + itemId).attr("innerHTML",errorMessage);
}


function validateGiftData(itemid, isRegistryGift)
{
   var cardtype = 0;
   var ecard = $("#ecard"+itemid).attr("checked");
   //document.getElementById('ecard'+itemid).checked;
   if (ecard != true) {
      cardtype = 1;
   }
   
   var carddesign = $("#carddesign"+itemid).val();
   //document.getElementById('carddesign'+itemid).value;
   if (!isRegistryGift) // only get recva fields if NOT a registry
   {
     var recva_email = $("#recva_email"+itemid).val(); 
     //document.getElementById('recva_email'+itemid).value;
     var recva_fname = $("#recva_fname"+itemid).val();
     //document.getElementById('recva_fname'+itemid).value;
     var recva_lname = $("#recva_lname"+itemid).val();
     //document.getElementById('recva_lname'+itemid).value;
     var recva_addr1 = $("#recva_addr1"+itemid).val();
     //document.getElementById('recva_addr1'+itemid).value;
     var recva_addr2 = $("#recva_addr2"+itemid).val();
     //document.getElementById('recva_addr2'+itemid).value;
     var recva_city = $("#recva_city"+itemid).val();
     //document.getElementById('recva_city'+itemid).value;
     var recva_state = $("#recva_state"+itemid).val();
     //document.getElementById('recva_state'+itemid).value;
     var recva_postal = $("#recva_postal"+itemid).val();
     //document.getElementById('recva_postal'+itemid).value;
     //var recva_select = document.getElementById('recva_ctryid'+itemid);
     //var recva_ctryid = recva_select.options[recva_select.selectedIndex].value;
     var recva_ctryid = $("#recva_ctryid"+itemid).val();
   }
   else // set empty recva fields
   {
     var recva_email = "";
     var recva_fname = "";
     var recva_lname = "";
     var recva_addr1 = "";
     var recva_addr2 = "";
     var recva_city = "";
     var recva_state = "";
     var recva_postal = "";
     var recva_select = "";
     var recva_ctryid = "";
   }   
   var giftto = $("#giftto"+itemid).val();
   var giftfrom = $("#giftfrom"+itemid).val();
   var message = $("#message"+itemid).val();
   var shippingSpeed = $("#shippingSpeed"+itemid).val();
   
   updateGiftInfo(itemid,cardtype,carddesign,recva_fname,recva_lname,recva_email,
                  recva_addr1,recva_addr2,recva_city,recva_state,recva_postal,
                  recva_ctryid,giftto,giftfrom,message,shippingSpeed);
   return false;
}

//*****************************************************************
//**  giving cart ajax calls, using jquery framework and json      **
//*****************************************************************
function updateItemAmount(itemid)
{
   var itemElem = document.getElementById('amount'+itemid);
   var newAmount = itemElem.value;

   var frequency = "ONCE";
   var frequencyElem = document.getElementById('frequency'+itemid);
   if (frequencyElem != null) {
	   frequency = frequencyElem.options[frequencyElem.selectedIndex].value;
   }
   
   var quantity = 1;
   var quantityElem = document.getElementById('quantity'+itemid);
   if (quantityElem != null) {
	   quantity = quantityElem.value;
   }
   
   if (validateNumber(itemElem, false, 'Donation amount', true, 0, 10, Number.MAX_VALUE) && validateNumber(quantityElem, false, 'Quantity', true, 0, 1, Number.MAX_VALUE))
   {
      clearError(itemid);
      $.post("/dy/cart/ajax/gg.html", { cmd: "updateAmount", itemid: itemid, newAmount: newAmount, frequency: frequency, quantity: quantity }, 
      	function(data) { updateCartAmount(data, itemid); }, "json");
   }
   else
   {
      if (newAmount == 0 || quantity == 0)
      {
        if (confirm("Are you sure you want to remove this item from your cart?"))
        {
          clearError(itemid);
          location = "/dy/cart/view/gg.html?cmd=del&itemid="+itemid;
        }
        else
        {
          itemElem.value = document.getElementById('preamount'+itemid).value;
          //itemElem.select();
          setTimeout(function(){itemElem.focus()},10);
          return false;
        }
      }
      else if (quantity != 0) // then amount is bad
      {
        var amount = 0;
        
        try {
          amount = parseFloat(newAmount.replace(/,/g,""));
        } catch (err) {
          // do nothing
        }
        
        var errorMessage = "Donaton amount must be a whole number greater than US $10.";
        
        if (amount < 10) {
           errorMessage = "Sorry, but we only accept donations greater than US $10.";
        } else if (amount >= 10) {
           errorMessage = "Sorry, but we only accept donations in whole dollar increments (no cents). Please round your donation up or down to a whole dollar.";
        }
        
        showError(itemid, errorMessage);
        //alert(errorMessage);
        //itemElem.select();
        setTimeout(function(){itemElem.focus()},10);
      }
      return false;
   }
}

function updateItemType(itemid, isGift)
{
	$.post("/dy/cart/ajax/gg.html", { cmd: "updateItemType", itemid: itemid, isGift: isGift },
		function(data) { giftInfoUpdated(data); }, "json" );
}

function updateTotals(result)
{
   // update the totals
   $('#sub_total').attr("innerHTML", result.sub_total);
   $('#shipping_and_handling').attr("innerHTML", result.shipping_and_handling);
   
   $('#gross_total').attr("innerHTML", result.gross_total);
   $('#net_total').attr("innerHTML", result.net_total);
   $('#remainingamt').attr("innerHTML", result.remainingamt);
   $('#totalLabel').attr("innerHTML", result.totalLabel);
   
   // update totals
   if (result.recurring_total.length > 0) {
      $('#recurring_total').attr("innerHTML", result.recurring_total);
      $('.recurring').show();
   } else {
      $('#recurring_total').attr("innerHTML", "0");
      $('.recurring').hide();
   }   
}

/**
 * Call back for updating an item amount, recalculated cart totals and payment methods
 * Expected result is in JSON object format
 */
function updateCartAmount(data, itemid)
{
   updateTotals(data);
   
   // update the amount
   if ($('#item'+itemid).attr("value") != data.item_amount) {
	   $('#item'+itemid).attr("value", data.item_amount);
	   $('#pitem'+itemid).attr("value", data.item_amount);
   }
   
   // update quantity
   if ($('#quantity'+itemid).attr("value") != data.item_quantity) {
	   $('#quantity'+itemid).attr("value", data.item_quantity);
   }
   // update subtotal
   $('#subtotal'+itemid).attr("innerHTML", data.item_subtotal);
   
   // update payment amounts
   updatePaymentInfo(data.payinfo);
}

function updatePaymentInfo(payinfo)
{
/*
alert('displaying payinfo returned via json:\n'+
    'payinfo.allowcc='+payinfo.allowcc+'\n'+
    'payinfo.allowpp='+payinfo.allowpp+'\n'+
    'payinfo.allowck='+payinfo.allowck+'\n'+
    'payinfo.payment_method='+payinfo.payment_method+'\n'+
    'payinfo.pay_msg='+payinfo.pay_msg); 
*/

   document.getElementById('creditcard').setAttribute("class","");
   document.getElementById('creditcard').setAttribute("className","");
   document.getElementById('paypal').setAttribute("class","");
   document.getElementById('paypal').setAttribute("className","");
   document.getElementById('check').setAttribute("class","");
   document.getElementById('check').setAttribute("className","");
   document.getElementById('giftcertcheckout').setAttribute("class","");
   document.getElementById('giftcertcheckout').setAttribute("className","");   
   //document.getElementById('chkout_butt').removeAttribute("disabled");
   //document.getElementById('payment_method').value=payinfo.payment_method;
   document.getElementById('exceedmsgholder_top').setAttribute("class","hidden");
   document.getElementById('exceedmsgholder_top').setAttribute("className","hidden");   
   document.getElementById('exceedmsgholder_bottom').setAttribute("class","hidden");
   document.getElementById('exceedmsgholder_bottom').setAttribute("className","hidden");   
   if (!payinfo.allowcc)
   {
     //alert('dont allow cc');
     document.getElementById('creditcard').setAttribute("class","hidden");
     document.getElementById('creditcard').setAttribute("className","hidden");
   } 
   if (!payinfo.allowpp)
   {
     //alert('dont allow pp');
     document.getElementById('paypal').setAttribute("class","hidden");
     document.getElementById('paypal').setAttribute("className","hidden");
   }
   if (!payinfo.allowck)
   {
     //alert('dont allow ck');
     document.getElementById('check').setAttribute("class","hidden");
     document.getElementById('check').setAttribute("className","hidden");
   }
   if (!payinfo.allowgiftcertcheckout) {
     document.getElementById('giftcertcheckout').setAttribute("class","hidden");
     document.getElementById('giftcertcheckout').setAttribute("className","hidden");
   }
   if (payinfo.payment_method=="cc") 
   {
     //alert('setting credit card checked');
     //document.getElementById('creditcard').checked=true;
   }     
   else if (payinfo.payment_method=="pp") 
   {
     //alert('setting paypal checked');
     //document.getElementById('paypal').checked=true;
   }     
   else if (payinfo.payment_method=="ck") 
   {
     //alert('setting check checked');
     //document.getElementById('check').checked=true;
   }   
   if (!payinfo.allowClick)
   {
     //document.getElementById('chkout_butt').setAttribute("disabled", true);
   }
   if (payinfo.pay_msg.length > 0)
   {
     //alert('setting exceedmsg to visible');
     document.getElementById('exceedmsgholder_top').setAttribute("class","");
     document.getElementById('exceedmsgholder_top').setAttribute("className","");   
     document.getElementById('exceedmsgholder_bottom').setAttribute("class","");
     document.getElementById('exceedmsgholder_bottom').setAttribute("className","");   
     document.getElementById('exceedmsg_top').innerHTML=payinfo.pay_msg;
     document.getElementById('exceedmsg_bottom').innerHTML=payinfo.pay_msg;
   }  
   if (payinfo.gcBalance)
   {
     if (document.getElementById('gcBalanceMsg')) {
        document.getElementById('gcBalanceMsg').style.cssText="display:inline;";
     }
   }
   else
   {
     if (document.getElementById('gcBalanceMsg')) {
        document.getElementById('gcBalanceMsg').style.cssText="display:none;";
     }
   }
   
   if (payinfo.paymentQuestion.length > 0)
   {
      document.getElementById('paymentQuestion').innerHTML=payinfo.paymentQuestion;
      document.getElementById('paymentAnswer').innerHTML=payinfo.paymentAnswer;
      document.getElementById('paymentQuestionHolder').setAttribute("class","");
      document.getElementById('paymentQuestionHolder').setAttribute("className","");
   } else {
      document.getElementById('paymentQuestionHolder').setAttribute("class","hidden");
      document.getElementById('paymentQuestionHolder').setAttribute("className","hidden");
   }   
   
   // set up recurring explaination text
   if (payinfo.recurringExplaination.length > 0)
   {
      document.getElementById('recurringExplaination').innerHTML=payinfo.recurringExplaination;
      document.getElementById('recurringExplainationHolder').setAttribute("class","");
      document.getElementById('recurringExplainationHolder').setAttribute("className","");
   } else {
      document.getElementById('recurringExplainationHolder').setAttribute("class","hidden");
      document.getElementById('recurringExplainationHolder').setAttribute("className","hidden");
   }   
}
/**
 * Javascript function to updat the gift card info entered in the view cart page.
 * Makes a call to server with params wrapped in a JSON object.
 */
function updateGiftInfo(itemid,cardtype,carddesign,recva_fname,recva_lname,recva_email,
                    recva_addr1,recva_addr2,recva_city,recva_state,recva_postal,
                    recva_ctryid,giftto,giftfrom,message,shippingSpeed)
{
	// pass the data as json to server, so utf-8 characters can be passed
	var params = '{ itemid: '+itemid+', cardtype: '+cardtype+', carddesign: '+carddesign+', '+
		'recva_fname: "'+jsonEscape(recva_fname)+'", recva_lname: "'+jsonEscape(recva_lname)+'", recva_email: "'+jsonEscape(recva_email)+'", '+
		'recva_addr1: "'+jsonEscape(recva_addr1)+'", recva_addr2: "'+jsonEscape(recva_addr2)+'", recva_city: "'+jsonEscape(recva_city)+'", '+
		'recva_state: "'+jsonEscape(recva_state)+'", recva_postal: "'+jsonEscape(recva_postal)+'", recva_ctryid: "'+recva_ctryid+'", '+
		'giftto: "'+jsonEscape(giftto)+'", giftfrom: "'+jsonEscape(giftfrom)+'", message: "'+jsonEscape(message)+'", shippingSpeed: "' + jsonEscape(shippingSpeed) + '" }';
	$.post("/dy/cart/ajax/gg.html", { cmd: "updateGiftInfo", giftinfo: params }, function(data) { giftInfoUpdated(data); }, "json" );
}
/**
 * Call back method for updating gift info on a cartitem
 * The parameter expected is a JSON object
 */
function giftInfoUpdated(result)
{
   var itemid = result.itemid;
   
   if (result.status == "SUCCESS")
   {
	  if (document.getElementById('giftoptions'+itemid)) {
	    document.getElementById('giftoptions'+itemid).style.display = 'none';
	  }
   
     // clear all of the error messages
     displayGiftError("recva_fname", itemid, null);
     displayGiftError("recva_lname", itemid, null);
     displayGiftError("recva_email", itemid, null);
     displayGiftError("recva_addr1", itemid, null);
     displayGiftError("recva_city", itemid, null);
     displayGiftError("recva_state", itemid, null);
     displayGiftError("recva_postal", itemid, null);
     displayGiftError("giftto", itemid, null);
     displayGiftError("giftfrom", itemid, null);
   
     updateMakeThisGiftMessage(result.giftMessage, result.itemid);
     updatePaymentInfo(result.payinfo);  // if its an e-card, now check is not an option!!!
     updateTotals(result);
   }
   else if (result.status == "ERRORS")
   {
     // not ok
	  if (document.getElementById('giftoptions'+itemid)) {
	    document.getElementById('giftoptions'+itemid).style.display = 'block';
	  }
	  
	  // look at the errors
	  var errors = result.errors;
	  
     displayGiftError("recva_fname", itemid, errors.recva_fname);
     displayGiftError("recva_lname", itemid, errors.recva_lname);
     displayGiftError("recva_email", itemid, errors.recva_email);
     displayGiftError("recva_addr1", itemid, errors.recva_addr1);
     displayGiftError("recva_city", itemid, errors.recva_city);
     displayGiftError("recva_state", itemid, errors.recva_state);
     displayGiftError("recva_postal", itemid, errors.recva_postal);
     displayGiftError("giftto", itemid, errors.giftto);
     displayGiftError("giftfrom", itemid, errors.giftfrom);
   }  
}

function displayGiftError(field, itemid, errorMessage) {
   if (errorMessage != null) {
      $("#div_" + field + itemid).attr("class", "ctrlHolder error");
      $("#error" + field + itemid).css("display", "block");
      $("#error" + field + itemid).html(errorMessage);
   } else {
      $("#div_" + field + itemid).attr("class", "ctrlHolder");
      $("#error" + field + itemid).css("display", "none");
      $("#error" + field + itemid).html("");
   }
}

function updateMakeThisGiftMessage(message, itemid) {
   if (message != null) {
      $('#makeThisDonationMessage' + itemid).attr("innerHTML", message);
   }
}

function addGiftCert()
{
  document.getElementById("gcmsg").style.cssText = "display:none;";
  document.getElementById("gcholder").setAttribute("class", "ctrlHolder");
  var gcnumber = document.getElementById("gcnum").value;
  if (gcnumber.length > 0)
  {
  	$.post("/dy/cart/ajax/gg.html", { cmd: "addGiftCertOrPromoCode", gcnum: gcnumber }, 
  		function(data) { updateGiftCert(data); }, "json");
  }
}
/**
 * Call back from updating a gift cert number on the cart
 * Parameter expected to be a JSON object
 */  
function updateGiftCert(result)
{
  //alert('In updateGiftCert...');
  var status = result.status;
  var msg = result.msg;
  //alert('status='+status);
  //alert('msg='+msg);
  if (status == "FAIL")
  {
    document.getElementById("gcmsg").innerHTML = msg;
    document.getElementById("gcmsg").style.cssText = "display:inline;";
    document.getElementById("gcholder").setAttribute("class", "ctrlHolder error");
    var gcnumber = document.getElementById("gcnum");
    gcnumber.select();
    setTimeout(function(){gcnumber.focus()},10);
  }
  else if (status == "LOGIN") // send user to login page
  {
     location.href=location.protocol+"//"+location.host+"/dy/v2/login/form.html?andthen="+encodeURIComponent('/dy/cart/ajax/gg.html?cmd=xaddGiftCert'); // redirect user to the home page.
  }
  else if (status == "SUCCESS")
  {
    // add a row to the table of gift certs
    table_body = document.getElementById("giftcert_tbody");
    new_row = table_body.insertRow(table_body.rows.length);
    createGiftCardRow(new_row, result.gcnum,result.expdate,"-" + result.gcamt);
    
    // update the cart totals
    updateTotals(result);

    // clear the gcnum entered.
    document.getElementById("gcnum").value = "";
    // make sure the table is "visible"
    document.getElementById('giftcert_table').style.cssText = "display:block;";
    // update the payment options and gift cert balance message
    updatePaymentInfo(result.payinfo);
    updateTotals(result);
  }    
}

function createGiftCardRow(row, gcnum, expdate, amt)
{
   var numcell = row.insertCell(row.cells.length);
   var numcell_data = document.createTextNode("Gift Card (" + gcnum + ")");
   numcell.appendChild(numcell_data);
   
   var amtcell = row.insertCell(row.cells.length);
   var amtspan1 = document.createElement("SPAN");
   amtspan1.style.cssText="float:right;";
   var amtspan2 = document.createElement("SPAN");
   amtspan2.style.cssText="padding-left: 10px;";
   //var amtdata = document.createTextNode(amt);
   //amtspan2.appendChild(amtdata);
   amtspan2.innerHTML = amt;
   amtspan1.appendChild(amtspan2);
   amtcell.appendChild(amtspan1);
}


function toggleCard(itemId) {
   var checked = $('#giftbox' + itemId).attr("checked");
   
   if (checked == true) {
      $('#giftIcon' + itemId).show();
      $('#removeGiftInfo' + itemId).show(); 
   } else { 
      $('#giftIcon' + itemId).hide();
      $('#removeGiftInfo' + itemId).hide(); 
      
      updateMakeThisGiftMessage("Make this donation a gift, in honor of, or in memory of someone.", itemId)
   }
   
   updateItemType(itemId, checked);
}


function pickCard(cardName, imageUrl, itemId, cardNumber) {
   $("#cardImage" + itemId).attr("src", "/gifts/img/" + imageUrl);
   
   $("#hope" + itemId).attr("class", "unselectedCard");
   $("#love" + itemId).attr("class", "unselectedCard");
   $("#green" + itemId).attr("class", "unselectedCard");
   $("#joy" + itemId).attr("class", "unselectedCard");
   // KMC 11/11/07: Removed holiday card due to process/supply issues
   //$("#give" + itemId).attr("class", "unselectedCard");
   $("#" + cardName + itemId).attr("class", "selectedCard");

   $("#Rcarddesign" + itemId).attr("value", cardNumber);
   $("#carddesign" + itemId).attr("value", cardNumber);
   
   // don't show the outline on the corn card
   if (cardName == "corncard") {
      $("#cardImage" + itemId).css("border", "0px");
   } else {
      $("#cardImage" + itemId).css("border", "1px solid #cccccc");
   }
}

function pickCardType(type, itemId) {
   var isGiftCert = $("#isgiftcert" + itemId).attr("value");

   if (type == "ecard") {
      if ($.browser.safari) {
         $("#ecardDetails" + itemId).css("display", "block");
         $("#paperDetails" + itemId).css("display", "none");
      } else {
         $("#ecardDetails" + itemId).fadeIn(200, null);
         $("#paperDetails" + itemId).hide();
      }
      $('#cardtype' + itemId).attr("value", 0);
      
      if (isGiftCert == "true") {
         $('#pickACard' + itemId).attr("class", "ctrlHolder");
         $('#pickAGiftCard' + itemId).attr("class", "ctrlHolder hidden");
         pickCard('hope', 'e_givehope_preview.jpg', itemId, 0);
      }
   } else if (type == "paper") {
      if ($.browser.safari) {
         $("#paperDetails" + itemId).css("display", "block");
         $("#ecardDetails" + itemId).css("display", "none");
      } else {
         $("#paperDetails" + itemId).fadeIn(200, null);
         $("#ecardDetails" + itemId).hide();
      }
      $('#cardtype' + itemId).attr("value", 1);
      
      if (isGiftCert == "true") {
         $('#pickACard' + itemId).attr("class", "ctrlHolder hidden");
         $('#pickAGiftCard' + itemId).attr("class", "ctrlHolder");
         pickCard('corncard', 'e_corncard_preview.jpg', itemId, 5);
      }
   }

}

/**
 * Gift Card Preview - Legacy
 */
function previewCard(itemid)
{
  // Retrieve values from the form. We get these
  //  from the form to handle value character
  //  escaping issues.
  
  var design = document.getElementById('carddesign'+itemid).value;
  var projtitle = document.getElementById('title'+itemid).value;
  var projid = document.getElementById('projid'+itemid).value; 
  var projpath = document.getElementById('projpath'+itemid).value;
  var giftto = document.getElementById('giftto'+itemid).value;		  
  var giftfrom = document.getElementById('giftfrom'+itemid).value;		  
  var message = document.getElementById('message'+itemid).value;
  var cardtype = document.getElementById('cardtype'+itemid).value;

  // Verify message length.
  if (document.getElementById('message'+itemid).length > 200) {
     alert("The personal message cannot exceed 200 characters in length.");
     var msgerror = "toolong";
     //return false;
  }

  popup("/gifts/preview/tributeCard.html?ptitle=" + escape(projtitle) + "&pid=" + projid
	+ "&path=" + projpath + "&recmsg=" + escape(message) + "&giftto=" + escape(giftto) + "&giftfrom=" + escape(giftfrom) + "&design=" + escape(design) + "&cardtype=" + escape(cardtype), 'Win1', 700, 550); 
  //return false;
}

/**
 *  Gift Certificate Preview
 */
function previewCertificate(itemid)
{
  // Retrieve values from the form. We get these
  //  from the form to handle value character
  //  escaping issues.
  
  var design = document.getElementById('carddesign'+itemid).value;
  var projtitle = document.getElementById('title'+itemid).value;
  var projid = document.getElementById('projid'+itemid).value; 
  var projpath = document.getElementById('projpath'+itemid).value;
  var giftto = document.getElementById('giftto'+itemid).value;		  
  var giftfrom = document.getElementById('giftfrom'+itemid).value;		  
  var message = document.getElementById('message'+itemid).value;
  var cardtype = document.getElementById('cardtype'+itemid).value;
  var amount = document.getElementById('amount'+itemid).value;

  // Verify message length.
  if (document.getElementById('message'+itemid).length > 200) {
     alert("The personal message cannot exceed 200 characters in length.");
     var msgerror = "toolong";
     //return false;
  }
  var pagename = "giftCertificate.html";
  popup("/gifts/preview/"+pagename+"?ptitle=" + escape(projtitle) + "&pid=" + projid
	+ "&path=" + projpath + "&recmsg=" + escape(message) + "&giftto=" + escape(giftto) + "&giftfrom=" + escape(giftfrom) + "&design=" + escape(design) + "&cardtype=" + escape(cardtype)
	+ "&amount=" + escape(amount), 'Win1', 700, 550); 
  //return false;
}
 
