// JavaScript Document

$(document).ready(function(){
									
	
	/* handler for important information rollover
	---------------------------------------------------------*/
	$("dl.important.rollover").hover( function () {
		$(this).css("position", "relative");
		$("dd.rollover").bgiframe();																  
		$("dd.rollover").slideDown("fast");
	}, function () {
		$("dd.rollover").slideUp("fast");			
	});
	
	
	/* toogle billing details
	---------------------------------------------------------*/
	$("#toggle-billing").hide(); /* hide by default if user has js, display if not */
	$("#same-billing").click( function () { 
													
		if ($(this).attr("checked")) {
			$("#toggle-billing").slideUp("fast");
			$.each($("#toggle-billing input.billing"), function () { $(this).removeClass('required');	});
			$.each($("#toggle-billing select.billing"), function () { $(this).removeClass('required'); });			
		}
		else {
			$("#toggle-billing").slideDown("slow");
			$.each($("#toggle-billing input.billing"), function () { $(this).addClass('required'); });
			$.each($("#toggle-billing select.billing"), function () { $(this).addClass('required'); });			
		}
			
		return true;
		//return $(this).attr("checked") ? $("#toggle-billing").slideUp("fast") : $("#toggle-billing").slideDown("slow"); 
	});
	
	if (!$("input#same-billing").attr("checked")) { $("#toggle-billing").show() };	
	
	/* Clear quanty fields on focus
	---------------------------------------------------------*/	
	$("#quantity input.text").focus( function () { if (this.value == 0) { this.value = "";} });
	
	/* Reset quanty fields on blur if nothing entered
	---------------------------------------------------------*/	
	$("#quantity input.text").blur( function () { if (this.value == "") { this.value = "0";	} });			
	
	
	/* form validation handler
	---------------------------------------------------------*/
	$("#gift-card-main").validate({
		errorContainer: "#validation",
		focusCleanup: false,
		errorPlacement: function(error, element) {
			if (element.attr("type") == "radio") {
				error.insertBefore(element.parent());
				error.addClass("radio");
			} else {
				error.insertAfter(element);
			}				
		}
	});
	
	
	/* limit personalised message to 200 characters
	---------------------------------------------------------*/
	jQuery.validator.addMethod("char200limit", function(value, element) {
		var length = $(element).attr("value").length;
		return (length <= 140)
	}, "Your personalised message must be 140 charactes or less");		
	
	
	/* Ensure a magazine quantity is entered
	---------------------------------------------------------*/
	jQuery.validator.addMethod("quantity", function(value, element) {
		var totalQuantity = 0;
		$.each($("#quantity input"), function () { totalQuantity += Number($(this).val()); });
		return (totalQuantity <= 0) ? false : true;
	}, "Please enter an item quantity");
	

	/* content switcher for important information rollover - defaultis a link to faqs for people with out javascript
	---------------------------------------------------------*/	
	$("dl.important.rollover dt").text("important information");
	$("dl.important.rollover dd.desc").text("Rollover for details about ordering gift cards online");	
	
	/* only show dynamic total payable if visitor has javascript
	---------------------------------------------------------*/		
	$("ul#total-payable").show();
	$("fieldset.message p.chars").show();
	$("#quantity-total").show(); /* hide by default if user has js, display if not */
	$("#subtotal").show(); /* hide by default if user has js, display if not */	
	
	
	/* handler to restrict length of the interim letter text
	---------------------------------------------------------*/				
	$("textarea.char200limit").keyup( function() { 
															 
		var text = $(this).attr("value")
		var limit = 140;
		var counter = 0;
		var textLength = 0;
		
		textLength = text.length
		counter = limit - textLength;
		
		if (counter < 0) {
			text = text.substring(0, limit);
			counter = 0;
			$(this).attr("value", text);
		}
		
		$("p.chars span").text(counter);
		
	});	
	
	
	/* Format number as currency
	---------------------------------------------------------*/			
		var currency = function(value) {
			return '$' + value.toFixed(2);
		};
		
		
	/* Format number as currency
	---------------------------------------------------------*/				
	var calcTotal = function() {		
	
		var totalQuantity = 0;
		var price = 0;
		var subtotal = 0;
		
		$.each($("#quantity input.text"), function() { 
			if (!isNaN(this.value) && Number(this.value) > 0) {																 
				totalQuantity += Number(this.value); 
				price = Number($(this).parent().siblings(".details").html().replace('$', '')); 
				price = Number(this.value) * price;
				subtotal += price;
			}
		});		
		
		$("p#quantity-total span").html(totalQuantity);
		$("p#subtotal span").html(currency(subtotal));		
		
		if (isNaN(this.value) || Number(this.value) < 0) {
			this.value = '0';
		}
		
		// get shipping amounts
		$.get('inc/giftcards_new.asp?shipping_products=' + totalQuantity, function(amounts) {
			amounts = amounts.split('\n');
			var shipping;// = Number(amounts[$('#postage-handling input:checked').attr('value')]) / 100;
			$.each(['registered','courier'], function(i, type) {
				type = $('#shipping-' + type);
				type.html(currency(amounts[i] / 100));
				if (type.parent().parent().find('input:checked').length == 1) {
					shipping = Number(amounts[i]) / 100;
				}
			})
			$('#total-payable li:first span.subtotal').html(currency(subtotal));
			$('#total-payable li:first span.shipping').html(currency(shipping));			
			$('#total-payable li:last').html(currency(subtotal + shipping));
		});	

	};
	
	/* Dynamic quantity total
	---------------------------------------------------------*/		
	$("#quantity input.text").keyup(function() { calcTotal(); });
	$("fieldset.postage input:radio").click(calcTotal);	
	
	
	/* Dynamic courier service input
	---------------------------------------------------------*/			
	$("fieldset.postage input:radio").click(function () { 
		if ($(this).attr("id").match("courier-service") && $(this).attr("checked")) {
			$("#courier-service-note").slideDown("fast");
			$("#courier-service-note input").addClass("required");
		} else {
			$("#courier-service-note").slideUp("fast");
			$("#courier-service-note input").removeClass("required");			
		}		
	});
	
	if ($("input#courier-service").attr("checked")) { $("#courier-service-note").css("display", "block") };
	
	
});