// make an AJAX call specifying the url and the respons handling function
function ajaxCall(url, f)
{
  var xmlHttp;
  try
  {
    // Firefox, Opera 8.0+, Safari
    xmlHttp = new XMLHttpRequest();
  }
  catch (e)
  {
    // Internet Explorer
    try
    {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      try
      {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e)
      {
        alert("Your browser does not support AJAX!");
        return false;
      }
    }
  }

  xmlHttp.onreadystatechange = function()
  {
    if (xmlHttp.readyState == 4)
    {
      f(xmlHttp);
    }
  }

  xmlHttp.open("GET", url, true);
  xmlHttp.send(null);
}

// Limit number of characters in text fields and text areas

function limitText(limitField, limitCount, limitNum)
{
  if (limitField.value.length > limitNum)
  {
    limitField.value = limitField.value.substring(0, limitNum);
  }
  else
  {
    document.getElementById(limitCount).innerHTML = limitNum - limitField.value.length;
  }
}

//////
// Image preloading

function loadImages(imgArray, imgNameArray, imgPath)
{
    for(var i = 0; i < imgNameArray.length; i++)
    {
      imgArray[i]= new Image();
      imgArray[i].src = imgPath + imgNameArray[i];
    }
}

// preload images for fast browsing and preview
function preloadImages(numImg, imgPath, imgNamePrefix, imgExt)
{
  if(document.images)
  {
    // enumeration of the "back" images
    var image_array = new Array();
    for (i = 0; i <= numImg; i++)
    {
      image_array.push(imgNamePrefix + i + imgExt);
    }

    var preload_image = new Array ();
    loadImages(preload_image, image_array, imgPath);
  }
}

//////
// Envelope functions

function enableCustEnvTxt()
{
  if (document.envelope.custenvtxt.checked)
  {
    document.envelope.sender.disabled = false;
    document.envelope.addressee.disabled = false;
  }
  else
  {
    document.envelope.sender.disabled = true;
    document.envelope.addressee.disabled = true;
  }
}

// enable the fields for custom size envelopes
function enableCustomSize()
{
  if (document.envelope.envformat[document.envelope.envformat.selectedIndex].value == -1)
  {
    document.envelope.envsizex.disabled = false;
    document.envelope.envsizey.disabled = false;
  }
  else
  {
    document.envelope.envsizex.disabled = true;
    document.envelope.envsizey.disabled = true;
  }
}

// preview the selected envelope picture
function previewEnvelopePicture()
{
  document.getElementById('envpicturepreview').src = "imgenvelope/envimg_" +
    document.envelope.envpicture[document.envelope.envpicture.selectedIndex].value + ".png";
}

//////
// Coupons functions

// enable/disable personalization fields (to/from names)
function enablePersonalizeCoupon()
{
  document.couponspage.fromname.disabled = ! document.couponspage.personalize.checked;
  document.couponspage.toname.disabled = ! document.couponspage.personalize.checked;
}

// enable/disable customization fields (title and to/from labels)
function enableCustomizeCoupon()
{
  document.couponspage.coupontitle.disabled = ! document.couponspage.customize.checked;
  document.couponspage.fromlabel.disabled = ! document.couponspage.customize.checked;
  document.couponspage.tolabel.disabled = ! document.couponspage.customize.checked;
}

// change coupon category by coupon slot id
function changeCategoryBySlotId(slotId)
{
  // preview category image
  cat = document.getElementById('couponcategories' + slotId);
  catId = cat[cat.selectedIndex].value;

  img = document.getElementById('categoryimg' + slotId)
  img.src = "imgcoupon/coupon_" + catId + ".png";

  // use AJAJ to load category coupons
  ajaxCall("category_coupons.php?catid=" + catId + "&slotid=" + slotId,
    function(xmlHttp)
    {
      cDiv = document.getElementById('categorycoupons' + slotId);
      cDiv.innerHTML = xmlHttp.responseText;
      // load coupon text
      changeCouponBySlotId(slotId);
    });
}

// change coupon by coupon slot id
function changeCouponBySlotId(slotId)
{
  cat = document.getElementById('couponcategories' + slotId);
  catId = cat[cat.selectedIndex].value;

  coupon = document.getElementById('coupons' + slotId);
  couponId = coupon[coupon.selectedIndex].value;

  changeBk(catId != 0, slotId);

  // use AJAX to load coupon text
  ajaxCall("coupon_text.php?catid=" + catId + "&couponid=" + couponId,
    function(xmlHttp)
    {
      ctext = "Love Coupon<br /><br />" + xmlHttp.responseText;
      if (document.couponspage.personalize.checked)
      {
        ctext = ctext +
          "<br /><br />To: " + document.couponspage.toname.value +
          "<br />From: " + document.couponspage.fromname.value;
      }

      tDiv = document.getElementById('coupontext' + slotId);
      tDiv.innerHTML = ctext;
      cancelEditCoupon(slotId);
    });
}

// change background for the div showing the coupon text
function changeBk(show, slotId)
{
  d = document.getElementById('coupontext' + slotId);
  if (show)
  {
    d.style.background = '#fc7176';
    d.style.opacity = '0.8';
    d.style.filter = 'alpha(opacity=80)';
  }
  else
  {
    d.style.background = '#ffffff';
    d.style.opacity = '0';
    d.style.filter = 'alpha(opacity=0)';
  }
}

// edit coupon text by slot id
function editCoupon(slotId)
{
  btn = document.getElementById('editcoupon' + slotId);
  btn.innerHTML = "Cancel";
  btn.onclick = function()
  {
    changeCouponBySlotId(slotId);
  };

  cat = document.getElementById('couponcategories' + slotId);
  catId = cat[cat.selectedIndex].value;

  coupon = document.getElementById('coupons' + slotId);
  couponId = coupon[coupon.selectedIndex].value;

  // use AJAX to load coupon text
  ajaxCall("coupon_text.php?catid=" + catId + "&couponid=" + couponId,
    function(xmlHttp)
    {
      ctext = xmlHttp.responseText;

      tDiv = document.getElementById('coupontext' + slotId);
      tDiv.innerHTML = "<textarea id=\"customcoupon" + slotId +
        "\" name=\"customcoupon" + slotId + "\" cols=\"19\" rows=\"11\">" + ctext + "</textarea>";
    });
}

// cance editing a coupon by slot id
function cancelEditCoupon(slotId)
{
  btn = document.getElementById('editcoupon' + slotId);
  btn.innerHTML = "Edit";
  btn.onclick = function()
  {
    editCoupon(slotId);
  };
}

//////
// Flower bouquet card functions

// preview the selected flower card picture
function previewFlowerCardPicture()
{
  document.getElementById('flcardimgpreview').src = "imgflowercard/flowercardimg_" +
    document.flowercard.flcardimg[document.flowercard.flcardimg.selectedIndex].value + ".png";
}

// enable/disable customization fields (title and to/from labels)
function enableCustomizeFlowerCard()
{
  document.flowercard.fromlabel.disabled = ! document.flowercard.customize.checked;
  document.flowercard.tolabel.disabled = ! document.flowercard.customize.checked;
}

//////
// Valentine card functions

// preview the selected flower card picture
function previewCardPicture()
{
  document.getElementById('cardimgpreview').src = "imgcard/thumb_card_" +
    document.valentinecard.cardimg[document.valentinecard.cardimg.selectedIndex].value + ".jpg";
}

