$j = jQuery.noConflict();

formCount = 1;
maxForms = 5;
messageLength = 160;

$j(document).ready(function()
{
	// count current voucher forms
	formCount = $j("div.voucher").length;
	
	if(formCount == 1)
	{
		$j("div.remove_item:first").hide();
	}
	
	$j("a.new_voucher").click(function(e) {
		get_voucher_form();
	});
	
	// bind form reset function, called when form is cloned
	$j("div.voucher").bind("reset",function(){
		
		$j(this).find("[name]").val('');	//reset fields
		
		$j(this).find("span.char_count").html(messageLength);
		
		$j(this).find("*").each(function() {
			
			name = $j(this).attr("name");

			if(name)
			{
				splitName = name.split('-');
				
				newName = splitName[0] + "-" + formCount;
				$j(this).attr("name",newName);

				// reset any warnings
				$j(this).removeClass("caution");
				$j(this).siblings("img.caution_image").css({display:"none"});
				$j(this).siblings("span.caution").css({display:"none"});
				
			}
		})
	})
	
	// remove form limk
	$j("div.remove_item").click(function(){
		
		$j(this).parent().parent().fadeOut('',function() {
			
			formCount--;
			
			$j(this).remove();
						
			if(formCount == 1)
			{
				$j("div.voucher:first").find("div.remove_item").hide();
			}
		});
	});
	
	// textarea character count
	$j("textarea.message").keypress(function(event) {
		
		// get current count
		charCount = $j(this).val().length;
		
		if (charCount >= messageLength)
		{
			event.preventDefault();
		}
		else
		{
			$j(this).prev("label").find("span.char_count").html(messageLength - charCount);
		}
	});
	
	// form submit event
	$j("#gift_voucher_form").submit(function(event) {
		
		valid = check_gift_options();
		
		if(!valid)
		{
			event.preventDefault();
		}
		else
		{
			$j("#form_values").val(get_form_values());
		}
	});
});

function get_voucher_form()
{
	if(formCount < maxForms)
	{
		$j("div.voucher:first").clone(true).appendTo("div.new_form_holder");
		$j("div.voucher:last div.remove_item").show();
		
		$j("div.voucher:last").hide();
		$j("div.voucher:last").fadeIn();
		
		formCount++;
		$j("div.voucher:last").trigger("reset");

		if(formCount > 1)
		{
			$j("div.voucher:first").find("div.remove_item").show();
		}
	}
}

function get_form_values()
{
	row = "";

	$j("div.voucher").each(function() {
		
		row = row + $j(this).find(":input").serialize();
		row = row + "|";
		
	});
	return row;
}

function show_voucher_forms(count)
{
	vouchers = $j("div.voucher").clone();
}

function update_gift_wrap(item)
{
	itemID = item.id;
	splitID = itemID.split('_');
	
	sku = splitID[1];
	
	window.location = '?page=shop&option=cart&action=update_gift&sku=' + sku;
}

function check_gift_options()
{
	valid = true;
	$j("div.voucher div.row > :input").each(function() {

		if(this.id.indexOf('message') == -1)
		{
			if(this.value == "")
			{
				$j(this).addClass("caution")
				$j(this).nextAll().css({display:"inline"});
				$j(this).nextAll("span.example").css({
					display: "none"
				});
				
				valid = false;
			}
			else
			{
				$j(this).removeClass("caution");
				$j(this).nextAll().css({display:"none"});
				$j(this).nextAll("span.example").css({
					display: "inline"
				});
			}
		}
	});
	
	return valid;
}

function check_voucher_code(event,claim_code)
{
	$ = jQuery.noConflict();
	if(event.keyCode == 17)
	{
		return false;
	}
	// dont check partial entries
	if ($("#claim_code").val().length < 14)
	{
		return false;
	}
	
	// check code is not already validated
	if($("#valid_claim_codes").val().length > 0) {
		if ($("#valid_claim_codes").attr("value").indexOf(claim_code) != -1) {
			// clear field error formatting
			$("#claim_code").css({
				border: "0px"
			})
			$("#claim_code").nextAll().css({
				display: "none"
			});
				
			return false;
		}
	}
	
	// check code on server
	$.get("index.php", {
		page: "remote",
		option: "voucher_code",
		code: claim_code
	}, function(amount) {
		
		if(amount == 'redirect')
		{
			window.location = "index.php";
		}
		
		if(amount > 0)
		{
			// clear field error formatting
			$("#claim_code").css({border:"0px"})
			$("#claim_code").nextAll("img").css({display:"none"});
			$("#claim_code").nextAll("span").css({display:"none"});
			
			validCodes = $("#valid_claim_codes").val();

			$("#list_claim_codes").addClass('visible');

			//add the claim code and amount to the table;
			codeHTML = "<tr><td>" + claim_code + "</td><td align=\"center\">&pound;" + amount +  "</td></tr>";
			
			$("#list_claim_codes").append(codeHTML);
			
			//append the claim code to the valid_claim_codes hidden field
			validCodes = $("#valid_claim_codes").val();
			
			if(validCodes.length==1)
			{
				validCodes = "";
			}
			validCodes +=  claim_code + "|";

			$("#valid_claim_codes").attr("value",validCodes);
			
			$("#claim_code").val('');
		}
		else
		{
			// mark field as invalid
			$("#claim_code").css({border:"2px solid #CC0033"})
			$("#claim_code").nextAll("img").css({display:"inline"});
			$("#claim_code").nextAll("span").css({display:"inline"});
			valid = false;
		}
	},
	'text');		
}

function fastnav_get_voucher_balance(event,claim_code)
{
	$ = jQuery.noConflict();
	
	if(event.keyCode == 17)
	{
		return false;
	}

	if (claim_code.length < 14)
	{
		$("div.balance_holder span.balance").html('£' + 0);
		return false
	}
	
	// check code on server
	$.get("index.php", {
		page: "remote",
		option: "voucher_code",
		code: claim_code
	}, function(amount) {
		
		if(amount == 'redirect')
		{
			amount = 0;
		}
		Number(amount).toPrecision(2);
		$("div.balance_holder span.balance").html('£' + amount);
				
		$("#claim_code").val('');
	},
	'text');		
}

function remove_claim_code(code)
{
	$ = jQuery.noConflict();
	
	validCodes = $("#valid_claim_codes").val();
	alert(validCodes);
	return false;
}

