function makeSublist(parent,child,isSubselectOptional,childVal)
{
	$("body").append("<select style='display:none' id='"+parent+child+"'></select>");
	$('#'+parent+child).html($("#"+child+" option"));

		var parentValue = $('#'+parent).attr('value');
		$('#'+child).html($("#"+parent+child+" .sub_"+parentValue).clone());

	childVal = (typeof childVal == "undefined")? "" : childVal ;
	$("#"+child+' option[@value="'+ childVal +'"]').attr('selected','selected');

	$('#'+parent).change( 
		function()
		{
			var parentValue = $('#'+parent).attr('value');
			$('#'+child).html($("#"+parent+child+" .sub_"+parentValue).clone());
			if(isSubselectOptional) $('#'+child).prepend("<option value='none'> -- Select -- </option>");
			$('#'+child).trigger("change");
						$('#'+child).focus();
		}
	);
}

var calcSemafor = false;
var pressAfterCalc = '';
function blink() {
	$('#iof_form input[class=blink]').animate({
							backgroundColor:skinColor
										},'slow',
				function () {
					$(this).animate({
							backgroundColor:'white'
							},'slow');
							}
				);
	if (calcSemafor)
	{
		calcSemafor = false;
		if (pressAfterCalc!='')
			$(pressAfterCalc).click();
//		$("#orderSample").removeAttr('disable');	
//		$("#pdetailsCart").removeAttr('disable');
		
	}
	
}

function calculateMark() 
{
	calcSemafor = true;
	$.scrollTo($('a[name=calc2]'),{duration:1000});
//	$("#orderSample").attr('disable','disable');	
//	$("#pdetailsCart").attr('disable','disable');	

	var ol_count = $("#iof_form select[@name=ol_count]").val();
	var mark_tech = $("#iof_form select[@name=mark_tech]").val();
	var prodcode = $("#iof_form input[@name=prodcode]").val();
	
	var psize = $("#iof_form select[@name=psize]").val();
	var quantity = $("#iof_form input[@name=quantity]").val();
	
	var pcolors = '';
	
	var counter = ol_count * 1;
	
	for (var i=1; i<=counter; i++) {
		var fname = "ol_count_" + i;
		pcolors += $("#iof_form input[@name="+ fname +"]").val() + ',';
	}
	
	$.post('/view/calculateMark/' + new Date().getTime(), {'prodcode': prodcode, 'mark_tech': mark_tech, 'ol_count': ol_count, 'pcolors': pcolors, 'psize': psize, 'quantity': quantity},blink);
}

function calculatePrice2() 
{
	$.scrollTo($('a[name=calc]'),{duration:1000});
	calculatePrice();	
}
function calculatePrice() 
{
	var psize = $("#iof_form select[@name=psize]").val();
	var quantity = $("#iof_form input[@name=quantity]").val();
	var productid = $("#iof_form input[@name=productid]").val();
	var prodcode = $("#iof_form input[@name=prodcode]").val();
	
	$.post('/view/calculatePrice/' + new Date().getTime(), {'prodcode': prodcode, 'productid': productid, 'quantity': quantity, 'psize': psize});
}

function addOptions()
{
	var psize = $("#iof_form select[@name=psize]").val();
	var quantity = $("#iof_form input[@name=quantity]").val();
	var productid = $("#iof_form input[@name=productid]").val();
	var prodcode = $("#iof_form input[@name=prodcode]").val();
	
	$.post('/view/addOptions/' + new Date().getTime(), {'prodcode': prodcode, 'productid': productid, 'quantity': quantity, 'psize': psize});	
}

function checkRange(numPlaces, max, placeNumber)
{
/*	if (numPlaces > max) {
		alert('Zbyt duża liczba miejsc znakowania w miejscu ' + placeNumber + '. Maksimum wynosi ' + max);
		var input_name = 'ol_count_' + placeNumber;
		$("#iof_form input[@name="+ input_name +"]").val("1");
	} else if (numPlaces == 0) {
		//alert('Minimalna wartość w miejscu ' + placeNumber + ' wynosi 1');
		var input_name = 'ol_count_' + placeNumber;
		$("#iof_form input[@name="+ input_name +"]").val("1");
	}*/
	
	// @_v
		
	var input_name = 'ol_count_' + placeNumber;
	if (numPlaces<=0 || numPlaces>max) {
		$("#iof_form input[@name="+ input_name +"]").css('background-color',skinColor);
		if (numPlaces>max)		
		$("#outOfRange").animate({
					height: 26
					});
		return false;		
	} else {
		$("#outOfRange").animate({
					height: 0
					});
		$("#iof_form input[@name="+ input_name +"]").css('background-color','');
		return true;		
	}
}

function checkLowPrice(price,limit)
{
	if (price<limit)
	{
		$("#tooLowPrice").animate({
					height: 30
					});
		return true;		
	}
	else
	{
		$("#tooLowPrice").animate({
					height: 0
					});
		return false;		
	}
}

$(document).ready(function()
{
	makeSublist('pcolor','psize', false, value = null);
	$('.numeric').numeric();
}); 
