$(function(){

var errorsPresent = false;
$('#_subm').click(function() {
	checkFormValues();
	
	if (!errorsPresent) {
		$('#order-form').append('<input type="hidden" name="subm" value="true" />');
		$('#order-form').submit();
	}
});

$('[name^=cn], #s-bnr-type, [name^=bsize]').each(function(i, el) {
	if (el.nodeName == 'SELECT') {
		$(el).change(function() {
			if ($(this).val() == '0') {
				$($(this).parent()).attr('class', 'err');
			} else {
				$($(this).parent()).attr('class', '');
			}
		});
	} else {
		$(el).blur(function() {
			if ($(this).val().length == 0) {
				$($(this).parent()).addClass('err');
			} else {
				$($(this).parent()).removeClass('err');
			}
		});		
	}
});


var forms = $('[name^=cn]');

function checkFormValues() {
	errorsPresent = false;
	for (var i=0,l=forms.length;i<l;i++) {
		checkField(forms[i]);
	}
	$('p.error').css('display', errorsPresent ? 'block' : 'none');
}

function checkField(f) {
	if ($($(f).parent()).css('display') == 'none') return;
	
	if (f.length == 0 || $(f).val() == 0) {
		$($(f).parent()).attr('class', 'err');
		errorsPresent = true;
	}
}

$('#s-type').change(function() {
	if (this.value == '0') return; 
	
	if (this.value == '2') {
		$($(this).parent().next()).fadeOut();
		$($(this).parent().next().next()).fadeOut();
	} else {
		$($(this).parent().next()).fadeIn();
		$($(this).parent().next().next()).fadeIn();
	}
});

$('#more-photos').click(addMore);
$('#more-examples').click(addMore);

function addMore() {
	var block = $(this).parent().prev().clone();
	block.find('a, small').remove();
	block.find('label').empty();
	block.find('input').replaceWith('<input type="file" name="i-photo[]" style="width: auto;" >');
	$($(this).parent().prev()).after(block);
}

$('#addMore').click(function() {
	$('#order-form').append('<input type="hidden" name="more" value="true" />');
	$('#_subm').click();
});

var bannerPrices = [0,18,37,56];
var discountPrices = [0,5,12,19];
var bannerPrice = 0;

$('#s-bnr-count').change(function(event) {
	var needed = parseInt(this.options[this.selectedIndex].value);
	var total = $('#bnrBlck').children().length-1;
	if (needed > total) {
		for (var i=0,l=needed-total;i<l;i++) {	
			
			var clonedBlock = $('<ul class="bnr-spec">' + $($('#bnrB').clone()).html().replace(/1\.\s/g, total+i+2 + '. ') + '</ul>');
			$(clonedBlock.find('input')).val('');
			$('#bnrBlck').append(clonedBlock);
			
			$('[name^=bsize]').blur(function(){
				if ($(this).val().length == 0) {
					$($(this).parent()).addClass('err');
				} else {
					$($(this).parent()).removeClass('err');
				}
			});
		}
	} else if (total > needed ) {
		var children = $('#bnrBlck').children();
		for(var c=total;c>needed;c--) {			
			$(children[c]).remove()
		}
	}
	var sprice = bannerPrices[$('#s-bnr-type').val()];
	var dprice = discountPrices[$('#s-bnr-type').val()];
	bannerPrice = sprice + (needed > 0 ? dprice*(needed) : 0);
	updatePrice();
	//var x = $('#bnrB').clone()
	//x.replace('1. ', )
	//$(x).insertAfter($('#bnrB'))
})


var updatePrice = function(){
	var p = Math.round(bannerPrice/(1+VAT)*100)/100;
	var s_vat = Math.round((bannerPrice/(1+VAT))*VAT*100)/100;
	$('#p_no_vat').text(p);
	$('#s_vat').text(s_vat);
	$('#p_total').text(bannerPrice);	
	$('#p_grand_total').text(grand_total_base+bannerPrice);
	/*
	$('#p_no_vat_eur').text(Math.round(p*100/euro)/100);
	$('#s_vat_eur').text(Math.round(s_vat*100/euro)/100);
	$('#p_total_eur').text(Math.round(bannerPrice*100/euro)/100);	
	$('#p_grand_total_eur').text(Math.round((grand_total_base+bannerPrice)*100/euro)/100);
	*/
}

$('#s-bnr-type').change(function() {
	var tBanners = parseInt($('#s-bnr-count')[0].options[$('#s-bnr-count')[0].selectedIndex].label); 
	var sprice = bannerPrices[this.options[this.selectedIndex].value]
	var dprice = discountPrices[this.options[this.selectedIndex].value];
	bannerPrice = sprice + (tBanners > 1 ? dprice*(tBanners-1) : 0);
	updatePrice();
})//.change();

$('#s-bnr-type').change();



$('a.remove').each(function(i, el) {
	$(el).click(function() {	
		var cID = $(this).prev()[0].href.match(/orderID:([0-9]+)/);
		window.location.href = '/form/remove:'+cID[1];
	})
})

});
