
	var loadFadeTimeout = 0;
	function loadFade(id,url,in_dur,out_dur){
    	if (typeof(in_dur)=='undefined')  in_dur  = 600;
    	if (typeof(out_dur)=='undefined') out_dur = 0;
    	if (loadFadeTimeout) clearTimeout(loadFadeTimeout);
    	loadFadeTimeout = setTimeout("$('#loadFade').addClass('busy')",2500);
    	$(id).wrap("<div id='loadFade'></div>");
    	$(id).fadeOut(out_dur,function(){
        	$(id).load(url,function(){
            	clearTimeout(loadFadeTimeout);
            	$(id).unwrap().fadeIn(in_dur, function(){ $(id).removeClass('busy'); $('.fade_in').animate({ opacity: 1},600); });
        	});
    	});
	}

	function ajaxtarget(o,target){
    	ajaxclass = $(o).attr('class').split(" ");
    	for (var i=0, len=ajaxclass.length; i<len; ++i ){
        	if (ajaxclass[i].match('target_')){
            	target = "#"+ajaxclass[i].replace('target_','');
        	}
    	}
    	return target;
	}

	function newsVisibleInCallback(carousel, item, i, state, evt) {
		var idx = carousel.index(i, newsItemList.length);
		carousel.add(i, newsItemList[idx - 1].html);
	}
	
	function language_box(fk,l,price){
		$('#id_name_'+fk).val(''); 
		$('#id_name_japanese_'+fk).val('');
		if (l) $('#options_'+fk).fadeIn();
		if (l=="English"){
			$('#e_'+fk).fadeIn(); 
			$('#j_'+fk).hide();
			$('.help').hide();
		}
		if (l=="Japanese"){
			$('#e_'+fk).hide(); 
			$('#j_'+fk).fadeIn();
			$('.help').hide();
		}
		if (l=="English and Japanese"){
			$('.help').fadeIn();
			$('#e_'+fk).fadeIn(); 
			$('#j_'+fk).fadeIn(); 
		}
		recalculate_belts(fk,price);
	}
	
	function showRequest_belts(formData, jqForm, options) {
		if (!stock_check(formData, jqForm, options)) return false;
		fk = options.fk;
		valid = $('#form_options_'+fk).valid();
		if (!$('#id_name_'+fk).val().length && !$('#id_name_japanese_'+fk).val().length) $('#id_name_colour_'+fk).val('');
		if (($('#id_name_'+fk).val().length || $('#id_name_japanese_'+fk).val().length) && !$('#id_name_colour_'+fk).val()){
			alert("Please select the text colour for your belt.");
			valid = false;
		}
		return valid;
	}

	function showRequest_master_gi(formData, jqForm, options) {
		if (!stock_check(formData, jqForm, options)) return false;
		fk = options.fk;
		valid = $('#form_options_'+fk).valid();
		if (!$('#id_name_rear').val().length) $('#id_name_rear_colour').val('');
		if (!$('#id_name_skirt').val().length) $('#id_name_skirt_colour').val('');
		if (!$('#id_name_trousers').val().length) {
			$('#id_name_trousers_colour').val('');
			$('#id_trousers_size').val('');
		}
		if ($('#id_name_skirt').val().length && !$('#id_name_skirt_colour').val()){
			alert("Please select text colour for the name on the skirt of your jacket.");
			valid = false;
		} else if ($('#id_name_trousers').val().length && !$('#id_name_trousers_colour').val()){
			alert("Please select text colour for the name on your trousers.");
			valid = false;
		} else if ($('#id_name_trousers').val().length && !$('#id_trousers_size').val()){
			alert("Please select text your trousers size.");
			valid = false;
		} else if (!valid) alert("Please select jacket size and colour.");
		return valid;
	}
	
	function showRequest_label(formData, jqForm, options) {
		if (!stock_check(formData, jqForm, options)) return false;
		valid = $('#form_options_'+options.fk).valid();
		if (!valid) alert("Please select jacket size and colour.");
		return valid;
	}

	function recalculate_belts(fk,price){
		total = price;
		lang = $('#id_belt_language_'+fk).val();
		p = 0;
		if (lang=="English" && $('#id_name_'+fk).val().length) p += 12;
		if (lang=="Japanese" && $('#id_name_japanese_'+fk).val().length) p += 15;
		if (lang=="English and Japanese" && $('#id_name_'+fk).val().length && $('#id_name_japanese_'+fk).val().length) p += 27;
		total = total + p;
		total = total.toFixed(2);
		if (total>price) $('#product_price_'+fk).html('&pound;' + total + ' (Personalised)');
		else $('#product_price_'+fk).html('&pound;' + total);
		total_less_vat = (total/(100+vat_rate)*100);
		$('#id_total_'+fk).val(total_less_vat);
	}

	function update_price(fk,val,price,total_options,extras){
		/*
		firelog("-------------------------------------------------------------");
		firelog("price   = "+price);
		firelog("options = "+total_options);
		firelog("extras  = "+extras);
		*/
		discount = $('#id_discount_'+fk).val();
		if (discount) {
			discount   = discount.replace("%","");
			discount_v = (total_options * discount)/100;
			// firelog("discount "+discount+" is "+discount_v);
			total_options = total_options - discount_v;
			// firelog("options = "+total_options);
		}
		if (val<160) vat_exempt = 1; else vat_exempt = 0; // val<160 returns true/false
		$('#id_variant_vat_exempt_'+fk).val(vat_exempt);
		// firelog("vat_exempt = " +vat_exempt);
		price = price + total_options;
		if (vat_exempt){
			vat = 0;
		} else {
			vat = (price*vat_rate)/100;
		}
		// totals
		price = price + extras;
		$('#id_total_'+fk).val(price);
		total_disp = price + vat;
		total_disp = total_disp.toFixed(2);
		// firelog("ex vat total  = " +price);
		// firelog("vat           = " +vat);
		// firelog("inc vat total = " +total_disp);
		$('#product_price').html('&pound;' + total_disp);
	}

	function update_field(f,v,val,mul){
		val = $('#id_'+f).val();
		if (typeof(val) == "undefined") {
			// firelog("update_field : "+f+" is not defined");
			return 0;
		}
		if (val.length) {
			if (val<160) vat=0;
			else {
				vat = (v*vat_rate)/100;
			}
			if (mul>0) {
				// firelog("adding "+f+" at "+v+" x "+val.length+" = "+(v*val.length));
				v = v*val.length;
			} else {
				// firelog("adding "+f+" at "+v);
			}
			vv = (v).toFixed(2);
			$('#price_'+f).html('&pound;' + vv);
			return v;
		} else {
			$('#price_'+f).html('');
			return 0;
		}
	}

	function recalculate_black_label(fk,price){
		total_options = 0;
		val = $('#id_variant_jacket_size').val().split(" ");
		val = val[0];
		if ($('#id_variant_jacket_colour').val()=='white'){
			if (val>180) total_options += 8.33333333333;
			else if (val>155) total_options += 4.1666666667;
		} else { // blue
			if (val>180) total_options += 16.6666666667;
			else if (val>155) total_options += 12.5;			
			else total_options += 10;
		}
		update_price(fk,val,price,total_options,0);
	}
	
	function recalculate_red_label(fk,price){
		total_options = 0;
		val = $('#id_variant_jacket_size').val().split(" ");
		val = val[0];
		if ($('#id_variant_jacket_colour').val()=='white'){	
			     if (val>170) total_options += 16.3333333333;	
			else if (val>150) total_options += 12.1666666667;
			else if (val>130) total_options += 8;
			else if (val>110) total_options += 4;
		} else { // blue
			     if (val>170) total_options += 24.6666666667;
			else if (val>150) total_options += 20.5;
			else if (val>130) total_options += 16;
			else if (val>110) total_options += 12;
					  	 else total_options += 8;
		}
		update_price(fk,val,price,total_options,0);
	}
	
	function recalculate_master_gi(fk,price){
		total_options = 0;
		val = $('#id_variant_jacket_size').val().split(" ");
		val = val[0];
		if ($('#id_variant_jacket_colour').val()=='white'){	
			     if (val>175) total_options += 15.83333333;
			else if (val>155) total_options += 7.5;
			else if (val>140) total_options += 7.5;
		} else { // blue
			     if (val>175) total_options += 24.16666667;
			else if (val>155) total_options += 15.83333333;
			else if (val>140) total_options += 17.5;
					  	 else total_options += 10;
		}
		extras  = update_field("name_rear",4.5,val,1);
		extras += update_field("name_skirt",14,val);
		extras += update_field("name_trousers",14,val);
		extras += update_field("ladies_cut",5,val);
		extras += update_field("flag",16,val);
		extras += update_field("armed_forces",12,val);
		if (extras) {
			if (val<160){
				total_options += 10;
				// firelog("total_options = "+total_options+" (adding 10 for personalisation)");
			}
		}
		update_price(fk,val,price,total_options,extras);
	}
	
	function recalculate_superstar(fk,price){
		total_options = 0;
		val = $('#id_variant_jacket_size').val().split(" ");
		val = val[0];
		if ($('#id_variant_jacket_colour').val()=='white'){	
			     if (val>180) total_options += 25.833;
			else if (val>155) total_options += 13.33;
		} else { // blue
			     if (val>180) total_options += 42.5;
			else if (val>155) total_options += 25.833;
			             else total_options += 10;
		}
		extras  = update_field("name_skirt",14,val);
		extras += update_field("name_trousers",14,val);
		extras += update_field("flag",16,val);
		if (extras) {
			if (val<160){
				total_options += 10;
				// firelog("total_options = "+total_options+" (adding 10 for personalisation)");
			}
		}
		update_price(fk,val,price,total_options,extras);
	}

	function colour_toggle(col){
		if (col=="blue"){
			$('#thumbs').hide();
			$('#thumbs_blue').fadeIn(2500);
		} else {
			$('#thumbs_blue').hide();
			$('#thumbs').fadeIn(2500);
		}
	}
	
	function init_thumbs(){
		lastId = 0;
		$(".thumbs img").bind("mouseenter",function(){
			id = this.id;
			id = id.replace('t','');
			if (lastId) $('#i'+lastId).hide();
			$('#i'+id).show();	
			lastId = id;
		});
		$(".thumbs").bind("mouseleave",function(){
			if (lastId) $('#i'+lastId).fadeOut(2500);
		});
	}
	
	function stock_check(formData, jqForm, options){
		qid = '#qty_'+options.fk;
		qty = $(qid).val();
		stock = options.stock;
		if (stock<qty) { 
			alert("Sorry we only have "+stock+" in stock at the moment."); 
			$(qid).val(stock); 
			return false; 
		}
		valid = $('#form_options_'+options.fk).valid();
		return valid;
	}

