/* catalog.js */

/* on ready functions */
$(document).ready(function() {

 /* 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() != '' ){ 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( '.', '_change.' );
  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;
 });
 
 //the kudo function should remain at the very end
 getKudos();
   
});