// gift_card_purchase.js for Nike By Mail
// js for gift_card_purchase.jsp
// code by Chris Nott (cnott@NOSPAM.blastradius.com)

var currentChosenImage = null;

function doPickImage(imageElement) {
   
   // write thumb image id to chosenImageInput form field
   var imageId = imageElement.id;
   document.getElementById('chosenImageInput').value = imageId;
   
   // show associated big image 
   var imageName = imageElement.src.substring(0, imageElement.src.lastIndexOf('_'));
   document.getElementById('imagePreview').src = imageName + '_lg.jpg';

   document.giftCertificateForm.productId.value = imageId;

   // remove different coloured border and "selected box" from last selected thumb image
   if (currentChosenImage != null) {
      var oldChosenImage = document.getElementById(currentChosenImage);
      oldChosenImage.parentNode.className = 'thumb';
   }

   // show same around thumb image
   imageElement.parentNode.className = 'thumb selectedThumb';
   currentChosenImage = imageId;
}


function updateSendDate() {
    document.getElementById('sendDate').value =
        document.getElementById('sendMonthInput').value + '/' +
        document.getElementById('sendDayInput').value + '/' +
        document.getElementById('sendYearInput').value;
}


function validateDate() {
  var intMonth = document.getElementById('sendMonthInput').value;
  var intDay = document.getElementById('sendDayInput').value;
  var intYear = document.getElementById('sendYearInput').value;

  if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && intDay == 31) {
    alert("Choosen month does not have 31 days.");
    return false;
  }

 // Check for February date validity (including leap years)

  if (intMonth == 2) {
  // figure out if "year" is a leap year; don't forget that
  // century years are only leap years if divisible by 400

    var isleap=(intYear%4==0 && (intYear%100!=0 || intYear%400==0));
    if (intDay > 29 || (intDay == 29 && !isleap)) {
      alert("February " + intYear + " does not have " + intDay +  " days.  ");
      return false;
    }
  }

  return true;

}

function textCounter(field, maxlimit ) {
  if ( field.value.length > maxlimit ){
    field.value = field.value.substring( 0, maxlimit );
    alert( 'Your message may not exceed ' + maxlimit + ' characters in length.' );
  }
}