$( document ).ready( function() {
	
	var oPersonalisation = $( "#id2" );
	if( oPersonalisation.length == 1 ) {
		$("#id2 .personalisation").hide();
		
		$( "#id2 .option" ).click( function () {
			$( "#id2 .personalisation" ).slideDown( 1000 );
			
			var iId = $( this ).attr( "value" );
			$( "#id2 .personalisation img" ).attr( "src", "/media/images/shop/personalisation_option" + iId + ".gif" ).css("margin-left","80px");
			
			// Clear the textarea
			$("#id2 .personalisation textarea").click(function () { 
				if( $(this).val() == "Enter your message here" ) {
					$(this).val( "" );
				}
			});

		});

	}
	
	var oPersonalisationBox = $( "#id3" );
	if( oPersonalisationBox.length == 1 ) {
		$("#id3 .personalisation").hide();
		$( "#id3 .option" ).click( function () {
			$( "#id3 .personalisation" ).slideDown( 1000 );
			
			var iIdBox = $( this ).attr( "value" );
		
			// Clear the textarea
			$("#id3 .personalisation textarea").click(function () { 
				if( $(this).val() == "Enter your message here" ) {
					$(this).val( "" );
				}
			});

		});
		
	}
	
	var oPersonalisationCard = $( "#id4" );
	if( oPersonalisationCard.length == 1 ) {
		$("#id4 .personalisation").hide();
		$( "#id4 .option" ).click( function () {
			$( "#id4 .personalisation" ).slideDown( 1000 );
			
			var iIdCard = $( this ).attr( "value" );
			
			// Clear the textarea
			$("#id4 .personalisation textarea").click(function () { 
				if( $(this).val() == "Enter your message here" ) {
					$(this).val( "" );
				}
			});

		});
		
	}
	
	var oPersonalisationBox = $( "#id6" );
	if( oPersonalisationBox.length == 1 ) {
		$("#id6 .personalisation").hide();
		$( "#id6 .option" ).click( function () {
			$( "#id6 .personalisation" ).slideDown( 1000 );
			
			var iIdBox = $( this ).attr( "value" );
		
			// Clear the textarea
			$("#id6 .personalisation textarea").click(function () { 
				if( $(this).val() == "Enter your message here" ) {
					$(this).val( "" );
				}
			});

		});
		
	}
	
	
	
	
	
	
	/**
	 * count input characters for personalisation message
	 */
	 
	 
//6" CAKE
$("#6per-message").bind('paste', null, function(e) {
		$(this).trigger('keyup');
});

$("#6per-message").keyup(function(e) {
	var letterSize = 18;
	var content = $(this).val();
	var contentLength = content.length;
			if(e.keyCode != 13){
				if(contentLength >= letterSize){
					$(this).val(content.substr(0,letterSize));
					//alert("We can't fit more than " + letterSize +" characters on the cake!");
				}	
			}
});

//8" CAKE
$("#8per-message").bind('paste', null, function(e) {	
		$(this).trigger('keyup');
});

$("#8per-message").keyup(function(e) {
	var letterSize = 22;
	var content = $(this).val();
	var contentLength = content.length;
			if(e.keyCode != 13){
				if(contentLength >= letterSize){
					$(this).val(content.substr(0,letterSize));
					//alert("We can't fit more than " + letterSize +" characters on the cake!");
				}	
			}
});
		 
//15" CAKE
$("#15per-message").bind('paste', null, function(e) {	
		$(this).trigger('keyup');
});

$("#15per-message").keyup(function(e) {
	var letterSize = 28;
	var content = $(this).val();
	var contentLength = content.length;
			if(e.keyCode != 13){
				if(contentLength >= letterSize){
					$(this).val(content.substr(0,letterSize));
					//alert("We can't fit more than " + letterSize +" characters on the cake!");
				}	
			}
});
	
	

	/**
	 * count input characters for card message with box
	 */
	
	 $("#card-message-box").keypress(function (e) {
	 	
		var textBox = $(this).val(); 
		var iCntBox = textBox.length;
	
      	if (e.which == 32 || (65 <= e.which && e.which <= 65 + 25)  || (97 <= e.which && e.which <= 97 + 25)) {
        var c = String.fromCharCode(e.which);
        		iCntBox++;
      		} else if (e.which == 8) {
        		// backspace in IE only be on keydown
       	 		iCntBox--;
      		}
  		if(iCntBox>100)
		{
			alert("we can't fit more than 100 characters on the gift card!");
		}
		
    });
    
     $("#card-message").keypress(function (e) {
	 	
		var textCard = $(this).val(); 
		var iCntCard = textCard.length;
	
      	if (e.which == 32 || (65 <= e.which && e.which <= 65 + 25)  || (97 <= e.which && e.which <= 97 + 25)) {
        var c = String.fromCharCode(e.which);
        		iCntCard++;
      		} else if (e.which == 8) {
        		// backspace in IE only be on keydown
       	 		iCntCard--;
      		}
  		if(iCntCard>100)
		{
			alert("we can't fit more than 100 characters on the gift card!");
		}
		
    });
	
	// Check that the user has supplied a personalisation message if required
	$("#product_view .form_submit").click( function( event ) {
		var bOptionChecked = $( '.option2:checked' ).length > 0;
		
		if( bOptionChecked ) {
			var bIsDefault = $( '#per-message' ).val() == "Enter your message here";
			var bIsEmpty = $( '#per-message' ).val().length == 0;
			
			if( bIsDefault || bIsEmpty ) {
				alert( "Please enter a personalisation message" );
				$( '#per-message' ).val( "" );
				$( '#per-message' ).focus();
				return false;
			}
			
			if( $( '.option1:checked' ).length > 0 ) {
				alert( "You can not add both personalisation and pre-portioning to a cake, please deselect one of these options" );
				return false;
			}
		}
		
		var bGBCheckedSub = $( '.option4:checked' ).length > 0;
		if( bGBCheckedSub && $( '#card-message' ).val().length <= 0 ) {
			alert( "Please enter a card message" );
			$( '#card-message' ).val( "" );
			$( '#card-message' ).focus();
			return false;
		}
		
		var bGBCCheckedSub = $( '.option3:checked' ).length > 0;
		if( bGBCCheckedSub && ( $( '#card-message-box' ).val().length <= 0  || $( '#card-message-box' ).val() == 'Enter your message here' ) ) {
			alert( "Please enter a card message" );
			$( '#card-message-box' ).val( "" );
			$( '#card-message-box' ).focus();
			return false;
		}
		
		var bGBCCheckedSub = $( '.option4:checked' ).length > 0;
		if( bGBCCheckedSub && ( $( '#card-message' ).val().length <= 0 || $( '#card-message' ).val() == 'Enter your message here' ) ) {
			alert( "Please enter a card message" );
			$( '#card-message' ).val( "" );
			$( '#card-message' ).focus();
			return false;
		}
	
	});
	
	
	// monitor preportioning and peronalisation
	$( ".option1" ).click(function(){
		var bPersonaliseChecked = $( '.option2:checked' ).length > 0;
		if( bPersonaliseChecked ) {
			if( $( '.option1:checked' ).length > 0 ) {
				alert( "You can not add both personalisation and pre-portioning to a cake. Please uncheck one of the options to continue." );
				$( this ).attr( "checked", false );
			}
		}
	});
	
	$( ".option2" ).click(function(){
		var bPrePortionChecked = $( '.option1:checked' ).length > 0;
		if( bPrePortionChecked ) {
			if( $( '.option2:checked' ).length > 0 ) {
				alert( "You can not add both personalisation and pre-portioning to a cake. Please uncheck one of the options to continue." );
				$( this ).attr( "checked", false );
			}
		}
	});
	
	// cant choose gift box and gift card together
	$( ".option3" ).click(function(){
		var bGBCChecked = $( '.option3:checked' ).length > 0;
		if( bGBCChecked ) {
			if( $( '.option4:checked' ).length > 0 ) {
				alert( "You can not add this and the 'gift card only'. Please uncheck one of the options to continue." );
				$( this ).attr( "checked", false );
			}
		}
	});
	
	$( ".option4" ).click(function(){
		var bGBChecked = $( '.option4:checked' ).length > 0;
		if( bGBChecked ) {
			if( $( '.option3:checked' ).length > 0 ) {
				alert( "You can not add this and the 'gift box and gift card'. Please uncheck one of the options to continue." );
				$( this ).attr( "checked", false );
			}
		}
	});
	
} );
