/* catalog.js */

/* on ready functions */
$(document).ready(function() {

 //$( '#shapeCarousel' ).jcarousel( { 'itemFallbackDimension': 75, 'scroll': 6 } );  --- moved to shapechooser.cfm

 /* ITEM.CFM */

 $('.container').hide();

 $('.toggleTxtArea').each( function() {
  var optID = parseID( $(this).attr('id') );
  if ( $(this).attr('checked') ){ $('#' + optID + '_txtArea').removeAttr( 'disabled', '' ); }
  else { $('#' + optID + '_txtArea').attr( 'disabled', 'DISABLED' ); }
 });


	$("a[rel='shippingInfo']").colorbox( {iframe:true, innerWidth:700, innerHeight:600} );

	$("a[rel='monoStyles']").colorbox();

 $('.monoStyleCheck').click( function(){
  //var monoArr = [ 'A', 'B', 'C', 'D' ];
  var cBoxStatus = $(this).attr('checked');
  $.each( ['A','B','C','D'], function(){ $('#mono_CB'+this).attr('checked', ''); });
  if ( cBoxStatus ){ $(this).attr('checked', 'true'); }
 });

 $('.toggleTxtArea').click( function(){
  var txtAreaID = parseID($(this).attr('id')) + '_txtArea';
  if ($(this).attr('checked')){ $('#'+txtAreaID).removeAttr('disabled'); }
  else { $('#'+txtAreaID).attr('disabled', 'DISABLED'); }
 });

 $('.feebox').click( function(){
    var feeInputID = parseID( $( this ).attr( 'id' ) ) + '_fee';
    if ( $( this ).attr( 'checked' ) ){ $( '#' + feeInputID ).val( 'true' ); }
    else { $( '#' + feeInputID ).val( 'false' ); }
 });

 /* the next three functions open containers on document ready if options are selected */
 $('.toggleTxtArea').each( function () {
  var optID = parseID( $(this).attr('id') );
   if( $(this).attr('checked') ){
    toggleContainer( optID );
   }
 });

 $('.monoStyleCheck').each( function () {
  var optID = parseID( $(this).attr('id') );
   if( $(this).attr('checked') ){
    toggleContainer( optID );
   }
 });

 if ( $('#art_CB') ){
  if ( $('#art_CB').attr('checked') ){
   toggleContainer( 'art' );
  }
 }

 /* item.cfm form validation */
 $('#itemForm').submit(function() {
  if ( ( $('#qtySelect').val() == '' ) || ( $('#qtySelect').val() == 0 ) ){ alert('Please choose a quantity'); return false; }
  if ( ! nonPreviewTxtInputsFilled() ) { return confirm( 'Your text input fields are blank, OK to continue?' ); }
 });

 $( 'input[name=stock]' ).click( function() {
  var json = {};
  json["ITEMCODE"] = $( 'input[name=ItemCode]' ).val();
  json["AREA"] = $( 'input[name=Area]' ).val();
  json["TYPE"] = $( 'input[name=Type]' ).val();
  json["STOCK"] = $( this ).val();
  json["QUANTITY"] = '';
  json["ID"] = 'qtySelect';
  json["CALLER"] = 'ITEM';
  json["QUANTITY"] = $( '#qtySelect' ).val();

  $.post( '/ajax/priceSelectH.cfm', json, qtySelectHTML, 'json' );
 });

 /* /GIFTLABELS/ form validation */
 $('#giftlabelsAdd').submit(function() {
  if ( ( $('#qtySelect').val() != 0 ) && ( $('#qtySelect').val() != '' ) ){ return true; }
  else { alert('Please choose a quantity'); return false; }
 });

 /* PHYSICAL.CFM */

 /* physical.cfm form validation */
 $('#physicalOrder').submit(function() {
  var qtySelectTest = false;
  $('.qtySelect').each( function(){
   if( $(this).val() != 0 ){
    qtySelectTest = true;
   }
  });
  if( ! qtySelectTest ){
   alert( 'Please choose a quantity' );
   return false;
  } else { return true; }
 });

 /* STYLE.CFM */

 $('.altStyleImage').mouseover(function(){
  var styleImgID = $( this ).find( 'img' ).attr( 'id' ); /* works for one img tag only */
  var styleImgSrc = $( '#' + styleImgID ).attr( 'src' );
  styleImgSrc = styleImgSrc.replace( '_change', '' );
  styleImgSrc = styleImgSrc.replace( 'change', 'colors' );
  $( '#' + styleImgID ).attr( 'src', styleImgSrc );
  return;
 });

 $('.altStyleImage').mouseout(function(){
  var styleImgID = $(this).find( 'img' ).attr('id'); /* works for one img tag only */
  var styleImgSrc = $( '#' + styleImgID ).attr( 'src' );
  styleImgSrc = styleImgSrc.replace( '.gif', '_change.gif' );
  styleImgSrc = styleImgSrc.replace( '.jpg', '_change.jpg' );
  styleImgSrc = styleImgSrc.replace( 'colors', 'change' );
  $( '#' + styleImgID ).attr( 'src', styleImgSrc );
  return;
 });

 /* GIFT-CERTIFICATES.CFM */

 var radioToggle = function ( className ){
  $( className ).each( function(){
   var optID = parseID( $(this).attr('id') );
   if( $(this).attr('checked') == true ) { $('#'+optID+'_container').show(); }
   else { $('#'+optID+'_container').hide(); }
  });
 }

 var toggleGCInput = function(){
   if( $('#leaveBlank').attr('checked') == true ) { $('.giftBoxedInput').attr('disabled','disabled'); }
   else { $('.giftBoxedInput').removeAttr('disabled');  }
 }

 $('.radioTrigger1').click(function(){
  radioToggle( '.radioTrigger1' );
 });
 $('.radioTrigger2').click(function(){
  radioToggle( '.radioTrigger2' );
 });

 $('#leaveBlank').click(function(){
  toggleGCInput();
 });

 /* initialize containers on ready */
 radioToggle( '.radioTrigger1' );
 radioToggle( '.radioTrigger2' );
 toggleGCInput();

 $('#emailForm').submit(function(){
  var go = true;

  if( $('#toTxte').val() == '' ){
   alert( 'Please fill in the \"To\" field' );
   $('#toTxte').focus();
   go = false;
  }
  if( $('#fromTxte').val() == '' ){
   alert( 'Please fill in the \"From\" field' );
   $('#fromTxte').focus();
   go = false;
  }
  if( $('#msgTxte').val() == '' ){
   alert( 'Please fill in the \"Message\" field' );
   $('#msgTxte').focus();
   go = false;
  }
  if( $('input[name=email]').val() == '' ){
   alert( 'Please provide an email address' );
   go = false;
  }
  else {
      emailAddrOK = emailAddr_Test( $('input[name=email]').val(), 'Please provide a valid email address.' );
      if( ! emailAddrOK ){ $('input[name=email]').focus(); go = false; }
  }
  return go;
 });

 $('#gift-boxedForm').submit(function(){
  var go = true;

  if( $('#leaveBlank').attr('checked') != true ){
  /* allow blank gift boxed certificates */
   if( $('#toTxtb').val() == '' ){
    alert( 'Please fill in the \"To\" field' );
    $('#toTxtb').focus();
    go = false;
   }
   if( $('#fromTxtb').val() == '' ){
    alert( 'Please fill in the \"From\" field' );
    $('#fromTxtb').focus();
    go = false;
   }
   if( $('#msgTxtb').val() == '' ){
    alert( 'Please fill in the \"Message\" field' );
    $('#msgTxtb').focus();
    go = false;
   }
  }

  if( $('#mail_RA').attr('checked') ){
   $('.shipping').each(function(){
    if( $(this).val() == '' ){
     alert( 'Please provide shipping ' + $(this).attr('name') );
     $(this).focus();
     go = false;
    }
   });
  }
  return go;
 });
 
 // returns
 $( '#rtnDateReceived' ).simpleDatepicker();
 $( 'input[name=reasoncode]').click( function(){ rtnFormReason( $(this).val() ); });
 $( '.rtnLink' ).click( function(){ $('#rtnFormLink').submit(); } );

 //google plus one
 ( function() {
  var g = document.createElement('script');
  g.async = true;
  g.src = 'https://apis.google.com/js/plusone.js';
  document.getElementById('plus-one').appendChild(g);
  }() );

 //the kudo function should remain at the very end
 getKudos();

});

function npColorChange() {
   if ( ! $( '#instr_CB' ).attr( 'checked' ) ) {
      $( 'textarea[name="Instructions"]' ).attr( 'disabled', false ).val( '' );
   }
   $( 'input[name="cmd"]' ).val( 'COLORCHANGE' );
   $( '#itemForm' ).attr( 'action', '' ).submit();
}

function loadSlideShow( html ) {
   if( html != '' ) {
    $( '#ideaGallery' ).html( html );
    $( "a[rel='gallery']" ).colorbox({slideshow:true, innerWidth:450, innerHeight:450});
    $( '#ideaGallery' ).show();
   }
}

function rtnFormReason( reasoncode ) {
   /* the noedits are for Mac browser support */
   if( reasoncode == 7 ) { 
      $( '#rtnDateReceived' ).removeAttr( 'disabled' ); 
      $( '#rtnDateReceived' ).removeClass( 'noedit' ); 
   } 
   else { 
      $( '#rtnDateReceived' ).attr( 'disabled', 'disabled' ); 
      $( '#rtnDateReceived' ).addClass( 'noedit' );
   }
   if( reasoncode == 1 ) { 
      $( '#rtnNotes' ).removeAttr( 'disabled' ); 
      $( '#rtnNotes' ).removeClass( 'noedit' );
   } 
   else { 
      $( '#rtnNotes' ).attr( 'disabled', 'disabled' );
      $( '#rtnNotes' ).addClass( 'noedit' );
   }
}

function nonPreviewTxtInputsFilled() {
   var filled = 0;
   $( '.countChars' ).each( 
   function(){ if( ! $( this ).val().trim() == '' ) { filled += 1 } });
   return filled;
}
