	$(function() {

		var cost = $("#cost"),
            cost_text = $("#cost_text"),
			allFields = $([]).add(cost).add(cost_text),
			tips = $("#validateTips");

		function updateTips(t) {
			tips.text(t).effect("highlight",{},1500);
		}

		function checkLength(o,n,min,max) {

			if ( o.val().length > max || o.val().length < min ) {
				o.addClass('ui-state-error');
				updateTips("Length of " + n + " must be between "+min+" and "+max+".");
				return false;
			} else {
				return true;
			}

		}

		function checkRegexp(o,regexp,n) {

			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass('ui-state-error');
				updateTips(n);
				return false;
			} else {
				return true;
			}

		}

		$("#dialog2").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 300,
			modal: true,
			buttons: {
				'Добавить заказ в корзину': function() {
					var bValid = true, cost_sert = 0;
                    allFields.removeClass('ui-state-error');
                    if($("#cost").val() == -1){   //нет радио-батонов
                        bValid = bValid && checkRegexp($("#cost_text"),/^([0-9\.])+$/i,"В поле номинала могут стоять только цифры и точка!");
                        if (bValid) cost_sert = $("#cost_text").val();
                    }
                    if($("#cost_text").val() && $("#cost_text").val() !='-1'){      //есть радио батон
                       cost_sert =$("#cost_text").val();
                    }
                    else {
                       cost_sert =$("input[name='cost']:checked").val();
                    }
					if (bValid) {
                        add_to_session($("#id_sert").val(), cost_sert);
						$(this).dialog('close');    
					}
				},
				Отмена: function() {
					$(this).dialog('close');
				}
			},
			close: function() {
				allFields.val('').removeClass('ui-state-error');
			}
		});

	});

