jQuery(function() {
	// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
	jQuery("#dialog").dialog("destroy");
	
	var disponible = jQuery("#disponible"),
		monto = jQuery("#monto"),
		allFields = jQuery([]).add(disponible).add(monto),
		tips = jQuery(".validateTips");

	function updateTips(t) {
		tips
			.text(t)
			.addClass('ui-state-highlight');
		setTimeout(function() {
			tips.removeClass('ui-state-highlight', 1500);
		}, 500);
	}

	function checkMount(o,n,min,max) {
		var monto = parseFloat(transformar_monto(o.val()));
		if(monto <= min || monto > max){
			o.addClass('ui-state-error');
			updateTips("Invalido " + n + ", el pago minimo deber ser de "+dar_formato_moneda2(min)+" y no superior al saldo disponible de "+dar_formato_moneda2(max)+".");
			return false;
		} else {
			return true;
		}
	}

	jQuery("#dialog-form").dialog({
		autoOpen: false,
		height: 300,
		width: 350,
		modal: true,
		buttons: {
			'Agregar Pago': function() {
				var bValid = true;
				allFields.removeClass('ui-state-error');

				bValid = bValid && checkMount(monto,"Monto a Utilizar",100.00,parseFloat(transformar_monto(jQuery('#disponible').val())));

				if (bValid) {
					jQuery('#frm_checkout_group').submit(); 
					jQuery(this).dialog('close');
				}
			},
			Cancelar: function() {
				jQuery(this).dialog('close');
			}
		},
		close: function() {
			allFields.val('').removeClass('ui-state-error');
		}
	});
	
	jQuery('.ajax_pago_grupo')
	.button()
	.click(function() {
		jQuery('#dialog-form').dialog('open');
		jQuery('#disponible').val(jQuery('#saldo_grupo').html());
		jQuery("#id_ins_grupo").val(this.id);
	});
});


