/*******************************************************************************
 * Herramientas
 *******************************************************************************/

var req_uid = 0;
var req_tid = 0;
var script_version = '1004';

function set_req_ids(val1,val2) { req_uid = val1; req_tid = val2; }
function encode_utf8(s) { return unescape(encodeURIComponent(s)); }
function decode_utf8(s) { return decodeURIComponent(escape(s)); }

var system_title = '';
function set_system_title( title ) { system_title = title; }

function SET_history(http,history_name)
{
	history.pushState({ h_http: http }, system_title + ' - ' + history_name, "index.php");
	document.title = system_title + ' - ' + history_name;
}

/*******************************************************************************
 * Impresión de DIVs
 *******************************************************************************/

function print_DIV(div_name,report_title)
{
	if( $('#' + div_name).find('#results_fixed').length > 0 )
	{
		content = $('#' + div_name).find('#results_fixed').html();
		content = content.replace(/position:\s+relative;\s+top:\s+[0-9]+px;/g,'');
	}
	else
	{
		content = $('#' + div_name).html();
	}

	var width = 800;
	var height = 600;
	var left = parseInt((screen.availWidth/2) - (width/2));
	var top = parseInt((screen.availHeight/2) - (height/2));
	var windowFeatures = "width=" + width + ",height=" + height + ",status,resizable,left=" + left + ",top=" + top + "screenX=" + left + ",screenY=" + top;
	var pwin = window.open('','print_content',windowFeatures);

	pwin.document.open();
	pwin.document.write(' \
		<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> \
		<html xmlns="http://www.w3.org/1999/xhtml"> \
			<head> \
				<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> \
					<title>' + report_title + '</title> \
					<link type="text/css" rel="stylesheet" href="styles.php" /> \
			</head> \
			<body onload="window.print(); window.close();" style="font-size: 10px; font-family: Arial,Helvetica,sans-serif; margin: 0px; background-color: #FFFFFF;"> \
				' + content + ' \
			</body> \
		</html> \
	');
	pwin.document.close();
}

/*******************************************************************************
 * AJAX manejadores
 *******************************************************************************/

function LINK_ajax(http,div_name,no_loading,do_print,do_history,history_name)
{
	no_loading = (typeof no_loading === "undefined") ? 0 : no_loading;
	do_print = (typeof do_print === "undefined") ? 0 : do_print;
	do_history = (typeof do_history === "undefined") ? 0 : do_history;
	history_name = (typeof history_name === "undefined") ? 'SiReTT Online' : history_name;

	if( no_loading == 0 ) { popup_loading(); }

	http = http + ( http.indexOf("?") > -1 ? "&" : "?" ) + "req_uid=" + req_uid + "&req_tid=" + req_tid + "&div_name=" + div_name + "&sver=" + script_version;

	$.ajax({
		type: "GET",
		url: http,
		cache: false,
		dataType: "html",
		success: function (data) {
			if( no_loading == 0 ) { hide_loading(); }

			if( data.indexOf("ALERT|") > -1 )
			{ // Página respondió con mensaje de alerta
				var x, y;
				x = data.indexOf('ALERT|') + "ALERT|".length;
				y = data.indexOf('|ENDALERT') - x;
				popup(data.substr(x,y));
			}
			else if( data.indexOf("ALERTTO|") > -1 )
			{
				var x, y;
				x = data.indexOf('ALERTTO|') + "ALERTTO|". length;
				y = data.indexOf('|ENDALERTTO') - x;

				var strs = data.substr(x,y).split('|');
				LINK_ask_popup(strs[0],strs[1],strs[2]);
			}
			else if( data.indexOf("STOP|SCRIPT") < 0 )
			{ // Protección de Spam al servidor
				$('#' + div_name).html(data);
				if( do_history )
				{
					history.pushState({ h_http: http }, system_title + ' - ' + history_name, "index.php");
					document.title = system_title + ' - ' + history_name;
				}
				if( do_print == 1 ) { print_DIV(div_name,''); }
				if( div_name == 'main_div' && $('#module_name').length )
				{
					var module_name = http.substring(0,http.lastIndexOf("."));
					$('#module_name').val(module_name);
					$('#this_module').html(module_name);
				}
				if( div_name == 'main_div' ) { $('html, body').scrollTop(0); }
			}
		},
		error: function(xhr, textStatus, errorThrown) {
			if( no_loading == 0 ) { hide_loading(); }
			popup('Error de comunicación con el servidor. INFO: [' + textStatus + ']. Por favor verifique su conexión a internet y vuelva a cargar el módulo. Si el problema persiste reinicie su sesión de usuario o comuníquese con Soporte SiReTT.');
		}
	});

	return false;
}

/*******************************************************************************
 * AJAX avanzados con formularios
 *******************************************************************************/

function POST_ajax(http, div_name, frm_name)
{
	popup_loading();

	// Se habilitan opciones deshabilitadas temporalmente, para su serialización, pues en caso contrario son ignoradas
	var disabled = $('#' + frm_name).find(':input:disabled').prop('disabled',false);
	var str_data = 'frm_name=' + frm_name + '&req_uid=' + req_uid + '&req_tid=' + req_tid + '&' + $('#' + frm_name).serialize() + "&div_name=" + div_name + "&sver=" + script_version;
	$('#' + frm_name).find(':input:password').val(''); // Limpieza de contraseñas
	disabled.prop('disabled',true);

	$.ajax({
		type: "POST",
		url: http,
		cache: false,
		data: str_data,
		dataType: "html",
		success: function (data) {
			hide_loading();

			if( data.indexOf("ALERT|") > -1 )
			{ // Página respondió con mensaje de alerta
				var x, y;
				x = data.indexOf('ALERT|') + "ALERT|".length;
				y = data.indexOf('|ENDALERT') - x;
				popup(data.substr(x,y));
			}
			else if( data.indexOf("ALERTTO|") > -1 )
			{
				var x, y;
				x = data.indexOf('ALERTTO|') + "ALERTTO|". length;
				y = data.indexOf('|ENDALERTTO') - x;

				var strs = data.substr(x,y).split('|');
				LINK_ask_popup(strs[0],strs[1],strs[2]);
			}
			else if( data.indexOf("STOP|SCRIPT") < 0 )
			{ // Protección de Spam al servidor
				$('#' + div_name).html(data);
				if( div_name == 'main_div' && $('#module_name').length )
				{
					var module_name = http.substring(0,http.lastIndexOf("."));
					$('#module_name').val(module_name);
					$('#this_module').html(module_name);
				}
				if( div_name == 'main_div' ) { $('html, body').scrollTop(0); }
			}
		},
		error: function(xhr, textStatus, errorThrown) {
			hide_loading();
			popup('Error de comunicación con el servidor. INFO: [' + textStatus + ']. Por favor verifique su conexión a internet y vuelva a cargar el módulo. Si el problema persiste reinicie su sesión de usuario o comuníquese con Soporte SiReTT.');
		}
	});

	return false;
}

function GET_ajax(http, div_name, frm_name)
{
	popup_loading();

	// Se habilitan opciones deshabilitadas temporalmente, para su serialización, pues en caso contrario son ignoradas
	var disabled = $('#' + frm_name).find(':input:disabled').prop('disabled',false);
	var str_data = 'frm_name=' + frm_name + '&req_uid=' + req_uid + '&req_tid=' + req_tid + '&' + $('#' + frm_name).serialize() + "&div_name=" + div_name + "&sver=" + script_version;
	$('#' + frm_name).find(':input:password').val(''); // Limpieza de contraseñas
	disabled.prop('disabled',true);

	$.ajax({
		type: "GET",
		url: http,
		cache: false,
		data: str_data,
		dataType: "html",
		success: function (data) {
			hide_loading();

			if( data.indexOf("ALERT|") > -1 )
			{ // Página respondió con mensaje de alerta
				var x, y;
				x = data.indexOf('ALERT|') + "ALERT|".length;
				y = data.indexOf('|ENDALERT') - x;
				popup(data.substr(x,y));
			}
			else if( data.indexOf("ALERTTO|") > -1 )
			{
				var x, y;
				x = data.indexOf('ALERTTO|') + "ALERTTO|". length;
				y = data.indexOf('|ENDALERTTO') - x;

				var strs = data.substr(x,y).split('|');
				LINK_ask_popup(strs[0],strs[1],strs[2]);
			}
			else if( data.indexOf("STOP|SCRIPT") < 0 )
			{ // Protección de Spam al servidor
				$('#' + div_name).html(data);
				if( div_name == 'main_div' && $('#module_name').length )
				{
					var module_name = http.substring(0,http.lastIndexOf("."));
					$('#module_name').val(module_name);
					$('#this_module').html(module_name);
				}
				if( div_name == 'main_div' ) { $('html, body').scrollTop(0); }
			}
		},
		error: function(xhr, textStatus, errorThrown) {
			hide_loading();
			popup('Error de comunicación con el servidor. INFO: [' + textStatus + ']. Por favor verifique su conexión a internet y vuelva a cargar el módulo. Si el problema persiste reinicie su sesión de usuario o comuníquese con Soporte SiReTT.');
		}
	});

	return false;
}

/*******************************************************************************
 * ASK FIRST
 *******************************************************************************/

function if_LINK_ajax(message, http, div_name)
{
	LINK_ask_popup(message, http, div_name);
	return false;
}

function if_POST_ajax(message, http, div_name, frm_name)
{
	POST_ask_popup(message, http, div_name, frm_name);
	return false;
}

/*******************************************************************************
 * Otras Herramientas
 *******************************************************************************/

function div_HTML(div_name, innerHTML)
{
	document.getElementById(div_name).innerHTML = innerHTML;
	return false;
}

function load_talleres() {
	var script = document.getElementById('talleres_script');
	if( script )
	{
		script.parentNode.removeChild(script);
	}

	script = document.createElement('script');
	script.id = "talleres_script";
	script.type = "text/javascript";
	script.src = "script_talleres.php?rand=" + Math.random();

	var head = document.getElementsByTagName('HEAD')[0];
	head.appendChild(script);
}

function input_garantia(sel_tienda)
{
	var txt_display = '';
	var row_data = false;

	if( sel_tienda.options[sel_tienda.selectedIndex].value == '0' )
		txt_display = 'none';

	for( var i = 1; i < 5; i++ )
	{
		row_data = document.getElementById('row_tienda_' + i.toString());
		if( row_data ) row_data.style.display = txt_display;
	}
}

function switch_view(value)
{
	for( var i = 0; i < 3; i++ )
	{
		button = document.getElementById('s_row_' + i.toString());
		row_base = document.getElementById('row_' + i.toString());

		if( button && row_base )
		{
			if( i == value )
			{
				button.className = 'input_submit_s3';
				row_base.style.display = '';
			}
			else
			{
				button.className = 'input_submit_s1';
				row_base.style.display = 'none';
			}
		}
	}
}

function line_counter(area, target)
{
	var text = area.value.replace(/\s+$/g, "");
	var split = text.split("\n");

	document.getElementById(target).value = split.length;
	return false;
}

function select_counter(sel_box, target)
{
	var count = 0;
	for( i = 0; i < sel_box.options.length; i++ )
	{
		if( sel_box.options[i].selected )
			count = count + 1;
	}

	document.getElementById(target).value = count;
	return false;
}

function select_enable(sel_box, value, target)
{
	var tar_sel = document.getElementById(target);

	if( sel_box.options[sel_box.selectedIndex].value == value )
		tar_sel.disabled = false;
	else
	{
		tar_sel.selectedIndex = 0;
		tar_sel.disabled = true;
	}

	return false;
}

var ticket_status_timer = null;

function start_ticket_status_interval()
{
	if( ticket_status_timer != null ) { clearInterval(ticket_status_timer); }
	ticket_status_timer = setInterval(function() { return LINK_ajax('tickets_status.php?do_alert=1','tickets_div',1); },60000);
	return 0;
}

function stop_ticket_status_interval()
{
	if( ticket_status_timer != null ) { clearInterval(ticket_status_timer); }
	return 0;
}

var eval_timer;
function eval_status_start( timer )
{
	clearTimeout(eval_timer);
	LINK_ajax('mod_evaluacion_tick.php','tick_div',1);
	if( timer == 1 )
	{
		eval_timer = setTimeout(function() { eval_status_start(1); },60000);
	}
}

var monitor_timer;

function monitor_status_start()
{
	clearTimeout(monitor_timer);
	LINK_ajax('ventas_monitor-do.php','monitor_div',1);

	monitor_timer = setTimeout(function() { monitor_status_refresh(); },60000);
}

function monitor_status_refresh()
{
	clearTimeout(monitor_timer);
	if( document.getElementById('monitor_div') )
	{
		LINK_ajax('ventas_monitor-do.php','monitor_div',1);
		monitor_timer = setTimeout(function() { monitor_status_refresh(); },60000);
	}
}

var bodega_timer;

function bodega_status_start()
{
	clearTimeout(bodega_timer);
	LINK_ajax('bodega_monitor-do.php','bodega_div',1);

	bodega_timer = setTimeout(function() { bodega_status_refresh(); },60000);
}

function bodega_status_refresh()
{
	clearTimeout(bodega_timer);
	if( document.getElementById('bodega_div') )
	{
		LINK_ajax('bodega_monitor-do.php','bodega_div',1);
		bodega_timer = setTimeout(function() { bodega_status_refresh(); },60000);
	}
}

var chat_timer;

function chat_status_start( aid, cid )
{
	clearTimeout(chat_timer);
	LINK_ajax('chat_log_monitor.php?aid=' + aid + '&cid=' + cid + '&init=1','chat_log_div',1);

	chat_timer = setTimeout(function() { chat_status_refresh( aid, cid ); },3750);
}

function chat_status_refresh( aid, cid )
{
	clearTimeout(chat_timer);
	if( document.getElementById('chat_log_div') )
	{
		LINK_ajax('chat_log_monitor.php?aid=' + aid + '&cid=' + cid,'chat_log_div',1);
		chat_timer = setTimeout(function() { chat_status_refresh( aid, cid ); },3750);
	}
}

var comanda_timer;

function comanda_status_start()
{
	clearTimeout(comanda_timer);
	LINK_ajax('comanda_monitor_data.php','comanda_monitor_div',1);

	comanda_timer = setTimeout(function() { comanda_status_refresh(); },20000);
}

function comanda_status_refresh()
{
	clearTimeout(monitor_timer);
	if( document.getElementById('comanda_monitor_div') )
	{
		LINK_ajax('comanda_monitor_data.php','comanda_monitor_div',1);
		comanda_timer = setTimeout(function() { comanda_status_refresh(); },20000);
	}
}

/*
 * Fin de Scripts de actualización por Timer
 */

function ignore_enter(e)
{
	var keynum;
	if( window.event )
		keynum = e.keyCode;
	else if( e.which )
		keynum = e.which;

	return ( keynum != 13 )
}

function selected_value(select_name, value, onchange)
{
	var select_box = document.getElementById(select_name);
	if( select_box )
	{
		for( var i = 0; i < select_box.options.length; i++ )
		{
			if( select_box.options[i].value == value )
			{
				select_box.selectedIndex = i;
				if( onchange ) select_box.onchange();
				break;
			}
		}
	}
}

function reparacion_changed(sel_reparacion)
{
	var input_box = document.getElementById('new_reparacion');
	var base_box = document.getElementById('new_rep_base');

	if( input_box && base_box )
	{
		if( sel_reparacion.options[sel_reparacion.selectedIndex].value == '0' )
		{
			input_box.value = '';
			input_box.style.display = '';
			base_box.value = '';
			base_box.style.display = '';
			input_box.focus();
		}
		else
		{
			input_box.style.display = 'none';
			base_box.style.display = 'none';
		}
	}
}

function externo_changed(sel_externo)
{
	var row_box = document.getElementById('row_serie');
	var txt_box = document.getElementById('serie');

	if( row_box && txt_box )
	{
		if( sel_externo.options[sel_externo.selectedIndex].value == '3' )
		{
			txt_box.value = '';
			row_box.style.display = '';
			txt_box.focus();
		}
		else
		{
			row_box.style.display = 'none';
		}
	}
}

function parte_changed(sel_parte)
{
	var numero_box = document.getElementById('new_numero');
	var input_box = document.getElementById('new_descripcion');
	var base_box = document.getElementById('new_base');

	if( input_box && base_box && numero_box )
	{
		if( sel_parte.options[sel_parte.selectedIndex].value == '0' )
		{
			input_box.value = '';
			input_box.style.display = '';
			base_box.value = '';
			base_box.style.display = '';
			numero_box.value = '';
			numero_box.style.display = '';
			input_box.focus();
		}
		else
		{
			input_box.style.display = 'none';
			base_box.style.display = 'none';
			numero_box.style.display = 'none';
		}
	}
}

function imposeMaxLength(obj)
{
	var mlength = obj.getAttribute ? parseInt(obj.getAttribute("maxlength")) : "";
	if( obj.getAttribute && obj.value.length > mlength )
	{
		obj.value = obj.value.substring(0,mlength);
	}
}

function input_edited(this_input, o_value)
{
	if( this_input.value != o_value )
		this_input.style.color = '#FF6600';
	else
		this_input.style.color = '#000000';
}

function randomString(length, chars)
{
	var result = '';
	for( var i = length; i > 0; --i )
		result += chars[Math.round(Math.random() * (chars.length - 1))];
	return result;
}

function create_serial(txt_input)
{
	var d = new Date();
	var dd = d.getDate();
	var mm = d.getMonth() + 1;
	var yy = d.getFullYear();
	txt_input.value = 'SS' + yy + ( mm < 10 ? '0' + mm : mm ) + ( dd < 10 ? '0' + dd : dd ) + randomString(5, '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ');
}

function input_set_value(input_name, value)
{
	var input_box = document.getElementById(input_name);
	if( input_box )
	{
		input_box.value = value;
	}
}

function roundUp(number,decimals) {
	var n = BigNumber(number);
	return n.toFixed(decimals,0); // RoundUp
}

function roundDown(number,decimals) {
	var n = BigNumber(number);
	return n.toFixed(decimals,1); // RoundDown
}

function setRoundTo(this_input,decimals) {
	var value = parseFloat(this_input.value);

	if( isNaN(value) ) { value = 0; }

	this_input.value = roundUp(value,decimals);
}

function play_buzon_sound() {
	document.getElementById('buzon_sound').play();
}

function no_interes(index)
{
	document.getElementById('td_' + index.toString()).innerHTML = '<div class="shop_box">&#10151; IGNORADO</div>';
}

// **********************************************************
// Formularios por POPUP
// **********************************************************

function fac_edit_precio( factura_id, factura_item_id, precio, detalle, cli_codigo )
{
	$('#dialog-message-form').empty();

	var form = $('<form/>', { id: 'dialog-form'	});
	form.append($('<input/>', { name: 'go', type: 'hidden', value: '6' }));
	form.append($('<input/>', { name: 'factura_id', type: 'hidden', value: factura_id }));
	form.append($('<input/>', { name: 'factura_item_id', type: 'hidden', value: factura_item_id }));
	var input = $('<input/>', { id: 'nuevo_precio', name: 'precio', type: 'text', class: 'input_text_money', placeholder: '0.00', value: precio });
	var input_detalle = $('<input/>', { name: 'detalle', type: 'text', class: 'input_text_small', placeholder: 'Detalle', value: detalle });
	var input_cli_codigo = $('<input/>', { name: 'cli_codigo', type: 'text', class: 'input_text', placeholder: '01234567890123456789', value: cli_codigo });

	form.append('Edición de Línea de Detalle.<br />El descuento será autocalculado basado en el precio de lista, los límites y tu permiso.<br /><br /><span class="n_orange">Detalle / Referencia</span><br />&#10151;', input_detalle, '<br /><span class="n_orange">Código de Producto del Comprador</span><br />&#10151;', input_cli_codigo, '<br /><span class="b_red">Precio de Unidad</span><br />&#10151;', input);
	form.on('submit', function(e) {
		e.preventDefault();

		$('#dialog-message-form').dialog('destroy');
		POST_ajax('factura_agregar_detalle.php','detalle_div','dialog-form');
		$('#dialog-message-form').empty();
	});

	$('#dialog-message-form').append(form).dialog({
		modal: true,
		width: 450,
		height: "auto",
		position: { my: "center", at: "center", of: window },
		buttons: [
			{
				text: "Aceptar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy');
					POST_ajax('factura_agregar_detalle.php','detalle_div','dialog-form');
					$(this).empty();
				}
			},
			{
				text: "Cancelar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy').empty();
					if( $('#do_codigo').length ) { $('#do_codigo').focus(); }
				}
			}
		],
		open: function( event, ui ) {
			$('#nuevo_precio').autoNumeric('init',{
				minimumValue : '0',
				maximumValue : '999999999.99',
				digitGroupSeparator : ',',
				decimalCharacter : '.',
				decimalCharacterAlternative : '.'
			});
		}
	});

	input.select();
}

function fac_edit_detalle( factura_id, factura_item_id, detalle, cli_codigo )
{
	$('#dialog-message-form').empty();

	var form = $('<form/>', { id: 'dialog-form'	});
	form.append($('<input/>', { name: 'go', type: 'hidden', value: '11' }));
	form.append($('<input/>', { name: 'factura_id', type: 'hidden', value: factura_id }));
	form.append($('<input/>', { name: 'factura_item_id', type: 'hidden', value: factura_item_id }));
	var input_detalle = $('<input/>', { name: 'detalle', type: 'text', class: 'input_text_small', placeholder: 'Detalle', value: detalle });
	var input_cli_codigo = $('<input/>', { name: 'cli_codigo', type: 'text', class: 'input_text', placeholder: '01234567890123456789', value: cli_codigo });

	form.append('Edición de Línea de Detalle.<br /><br /><span class="n_orange">Detalle / Referencia</span><br />&#10151;', input_detalle, '<br /><span class="n_orange">Código de Producto del Comprador</span><br />&#10151;', input_cli_codigo);
	form.on('submit', function(e) {
		e.preventDefault();

		$('#dialog-message-form').dialog('destroy');
		POST_ajax('factura_agregar_detalle.php','detalle_div','dialog-form');
		$('#dialog-message-form').empty();
	});

	$('#dialog-message-form').append(form).dialog({
		modal: true,
		width: 450,
		height: "auto",
		position: { my: "center", at: "center", of: window },
		buttons: [
			{
				text: "Aceptar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy');
					POST_ajax('factura_agregar_detalle.php','detalle_div','dialog-form');
					$(this).empty();
				}
			},
			{
				text: "Cancelar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy').empty();
					if( $('#do_codigo').length ) { $('#do_codigo').focus(); }
				}
			}
		]
	});

	input_detalle.select();
}

function item_edit_costo( item_id, empresa_id )
{
	$('#dialog-message-form').empty();

	var form = $('<form/>', { id: 'dialog-form'	});
	form.append($('<input/>', { name: 'go', type: 'hidden', value: '1' }));
	form.append($('<input/>', { name: 'item_id', type: 'hidden', value: item_id }));
	form.append($('<input/>', { name: 'empresa_id', type: 'hidden', value: empresa_id }));
	var input = $('<input/>', { id: 'nuevo_costo', name: 'costo', type: 'text', class: 'input_text_money', placeholder: '0.00', value: 0 });

	form.append('<span class="b_red">Edición directa del Costo Promedio de un Artículo.</span><br /><br />Esta edición realiza una afectación global del costo desde que el artículo ingresó al inventario, para no afectar la matemática del producto en el transcurso del tiempo. Siendo esta operación posible en el sistema, siempre se recomienda que es mejor realizar una salida de la mercadería en el costo actual, y luego una entrada en el costo correcto..<br /><br /><span class="b_orange">Costo Promedio</span>', input, '<br/><br/><span class="b_red">Se perderán los históricos de Costos previos al aplicar.<br />Solo compras mantendrán su valor original.</span>');
	form.on('submit', function(e) {
		e.preventDefault();

		$('#dialog-message-form').dialog('destroy');
		POST_ajax('item_shop-costos.php','ic_data','dialog-form');
		$('#dialog-message-form').empty();
	});

	$('#dialog-message-form').append(form).dialog({
		modal: true,
		width: 500,
		height: "auto",
		position: { my: "center", at: "center", of: window },
		buttons: [
			{
				text: "Aceptar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy');
					POST_ajax('item_shop-costos.php','ic_data','dialog-form');
					$(this).empty();
				}
			},
			{
				text: "Cancelar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy').empty();
				}
			}
		],
		open: function( event, ui ) {
			$('#nuevo_costo').autoNumeric('init',{
				minimumValue : '0',
				maximumValue : '999999999.99',
				digitGroupSeparator : ',',
				decimalCharacter : '.',
				decimalCharacterAlternative : '.'
			});
		}
	});

	input.select();
}

function pro_edit_precio( orden_id, orden_item_id, precio, detalle, cli_codigo )
{
	$('#dialog-message-form').empty();

	var form = $('<form/>', { id: 'dialog-form'	});
	form.append($('<input/>', { name: 'go', type: 'hidden', value: '6' }));
	form.append($('<input/>', { name: 'orden_id', type: 'hidden', value: orden_id }));
	form.append($('<input/>', { name: 'orden_item_id', type: 'hidden', value: orden_item_id }));
	var input = $('<input/>', { id: 'nuevo_precio', name: 'precio', type: 'text', class: 'input_text_money', placeholder: '0.00', value: precio });
	var input_detalle = $('<input/>', { name: 'detalle', type: 'text', class: 'input_text_short', placeholder: 'Detalle', value: detalle });
	var input_cli_codigo = $('<input/>', { name: 'cli_codigo', type: 'text', class: 'input_text', placeholder: '01234567890123456789', value: cli_codigo });

	form.append('Edición de Línea de Detalle.<br />El descuento será autocalculado basado en el precio de lista, los límites y tu permiso.<br /><br /><span class="n_orange">Detalle / Referencia</span><br />&#10151;', input_detalle, '<br /><span class="n_orange">Código de Producto del Comprador</span><br />&#10151;', input_cli_codigo, '<br /><span class="b_red">Precio de Unidad</span><br />&#10151;', input);
	form.on('submit', function(e) {
		e.preventDefault();

		$('#dialog-message-form').dialog('destroy');
		POST_ajax('proforma_agregar_detalle.php','detalle_div','dialog-form');
		$('#dialog-message-form').empty();
	});

	$('#dialog-message-form').append(form).dialog({
		modal: true,
		width: 450,
		height: "auto",
		position: { my: "center", at: "center", of: window },
		buttons: [
			{
				text: "Aceptar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy');
					POST_ajax('proforma_agregar_detalle.php','detalle_div','dialog-form');
					$(this).empty();
				}
			},
			{
				text: "Cancelar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy').empty();
					if( $('#do_codigo').length ) { $('#do_codigo').focus(); }
				}
			}
		],
		open: function( event, ui ) {
			$('#nuevo_precio').autoNumeric('init',{
				minimumValue : '0',
				maximumValue : '999999999.99',
				digitGroupSeparator : ',',
				decimalCharacter : '.',
				decimalCharacterAlternative : '.'
			});
		}
	});

	input.select();
}

function pnt_edit_precio( prenota_id, prenota_item_id, precio, detalle, cli_codigo )
{
	$('#dialog-message-form').empty();

	var form = $('<form/>', { id: 'dialog-form'	});
	form.append($('<input/>', { name: 'go', type: 'hidden', value: '6' }));
	form.append($('<input/>', { name: 'prenota_id', type: 'hidden', value: prenota_id }));
	form.append($('<input/>', { name: 'prenota_item_id', type: 'hidden', value: prenota_item_id }));
	var input = $('<input/>', { id: 'nuevo_precio', name: 'precio', type: 'text', class: 'input_text_money', placeholder: '0.00', value: precio });
	var input_detalle = $('<input/>', { name: 'detalle', type: 'text', class: 'input_text_short', placeholder: 'Detalle', value: detalle });
	var input_cli_codigo = $('<input/>', { name: 'cli_codigo', type: 'text', class: 'input_text', placeholder: '01234567890123456789', value: cli_codigo });

	form.append('Edición de Línea de Detalle.<br />El descuento será autocalculado basado en el precio de lista, los límites y tu permiso.<br /><br /><span class="n_orange">Detalle / Referencia</span><br />&#10151;', input_detalle, '<br /><span class="n_orange">Código de Producto del Comprador</span><br />&#10151;', input_cli_codigo, '<br /><span class="b_red">Precio de Unidad</span><br />&#10151;', input);
	form.on('submit', function(e) {
		e.preventDefault();

		$('#dialog-message-form').dialog('destroy');
		POST_ajax('prenota_agregar_detalle.php','detalle_div','dialog-form');
		$('#dialog-message-form').empty();
	});

	$('#dialog-message-form').append(form).dialog({
		modal: true,
		width: 450,
		height: "auto",
		position: { my: "center", at: "center", of: window },
		buttons: [
			{
				text: "Aceptar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy');
					POST_ajax('prenota_agregar_detalle.php','detalle_div','dialog-form');
					$(this).empty();
				}
			},
			{
				text: "Cancelar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy').empty();
					if( $('#do_codigo').length ) { $('#do_codigo').focus(); }
				}
			}
		],
		open: function( event, ui ) {
			$('#nuevo_precio').autoNumeric('init',{
				minimumValue : '0',
				maximumValue : '999999999.99',
				digitGroupSeparator : ',',
				decimalCharacter : '.',
				decimalCharacterAlternative : '.'
			});
		}
	});

	input.select();
}

function pro_edit_detalle( orden_id, orden_item_id, detalle, cli_codigo )
{
	$('#dialog-message-form').empty();

	var form = $('<form/>', { id: 'dialog-form'	});
	form.append($('<input/>', { name: 'go', type: 'hidden', value: '11' }));
	form.append($('<input/>', { name: 'orden_id', type: 'hidden', value: orden_id }));
	form.append($('<input/>', { name: 'orden_item_id', type: 'hidden', value: orden_item_id }));
	var input_detalle = $('<input/>', { name: 'detalle', type: 'text', class: 'input_text_small', placeholder: 'Detalle', value: detalle });
	var input_cli_codigo = $('<input/>', { name: 'cli_codigo', type: 'text', class: 'input_text', placeholder: '01234567890123456789', value: cli_codigo });

	form.append('Edición de Línea de Detalle.<br /><br /><span class="n_orange">Detalle / Referencia</span><br />&#10151;', input_detalle, '<br /><span class="n_orange">Código de Producto del Comprador</span><br />&#10151;', input_cli_codigo);
	form.on('submit', function(e) {
		e.preventDefault();

		$('#dialog-message-form').dialog('destroy');
		POST_ajax('proforma_agregar_detalle.php','detalle_div','dialog-form');
		$('#dialog-message-form').empty();
	});

	$('#dialog-message-form').append(form).dialog({
		modal: true,
		width: 450,
		height: "auto",
		position: { my: "center", at: "center", of: window },
		buttons: [
			{
				text: "Aceptar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy');
					POST_ajax('proforma_agregar_detalle.php','detalle_div','dialog-form');
					$(this).empty();
				}
			},
			{
				text: "Cancelar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy').empty();
					if( $('#do_codigo').length ) { $('#do_codigo').focus(); }
				}
			}
		]
	});

	input_detalle.select();
}

function pnt_edit_detalle( prenota_id, prenota_item_id, detalle, cli_codigo )
{
	$('#dialog-message-form').empty();

	var form = $('<form/>', { id: 'dialog-form'	});
	form.append($('<input/>', { name: 'go', type: 'hidden', value: '11' }));
	form.append($('<input/>', { name: 'prenota_id', type: 'hidden', value: prenota_id }));
	form.append($('<input/>', { name: 'prenota_item_id', type: 'hidden', value: prenota_item_id }));
	var input_detalle = $('<input/>', { name: 'detalle', type: 'text', class: 'input_text_small', placeholder: 'Detalle', value: detalle });
	var input_cli_codigo = $('<input/>', { name: 'cli_codigo', type: 'text', class: 'input_text', placeholder: '01234567890123456789', value: cli_codigo });

	form.append('Edición de Línea de Detalle.<br /><br /><span class="n_orange">Detalle / Referencia</span><br />&#10151;', input_detalle, '<br /><span class="n_orange">Código de Producto del Comprador</span><br />&#10151;', input_cli_codigo);
	form.on('submit', function(e) {
		e.preventDefault();

		$('#dialog-message-form').dialog('destroy');
		POST_ajax('prenota_agregar_detalle.php','detalle_div','dialog-form');
		$('#dialog-message-form').empty();
	});

	$('#dialog-message-form').append(form).dialog({
		modal: true,
		width: 450,
		height: "auto",
		position: { my: "center", at: "center", of: window },
		buttons: [
			{
				text: "Aceptar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy');
					POST_ajax('prenota_agregar_detalle.php','detalle_div','dialog-form');
					$(this).empty();
				}
			},
			{
				text: "Cancelar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy').empty();
					if( $('#do_codigo').length ) { $('#do_codigo').focus(); }
				}
			}
		]
	});

	input_detalle.select();
}

function preajuste_series( preajuste_id, item_id )
{
	$('#dialog-message-form').empty();
	LINK_ajax('ajuste_agregar-series.php?preajuste_id=' + preajuste_id + '&item_id=' + item_id,'dialog-message-form');
	$('#dialog-message-form').dialog({
		modal: true,
		width: 600,
		height: "auto",
		position: { my: "center", at: "center", of: window },
		buttons: [
			{
				text: "Aceptar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy');
					POST_ajax('ajuste_agregar_detalle.php','detalle_div','dialog-form');
					$(this).empty();
				}
			},
			{
				text: "Cancelar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy').empty();
				}
			}
		]
	});
}

function compra_series( compra_id, item_id )
{
	$('#dialog-message-form').empty();
	LINK_ajax('compra_ver-series_script.php?compra_id=' + compra_id + '&item_id=' + item_id,'dialog-message-form');
	$('#dialog-message-form').dialog({
		modal: true,
		width: 500,
		height: "auto",
		position: { my: "center", at: "center", of: window },
		buttons: [
			{
				text: "Aceptar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy');
					POST_ajax('compra_ver-series.php','series_div','dialog-form');
					$(this).empty();
				}
			},
			{
				text: "Cancelar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy').empty();
				}
			}
		]
	});
}

function histo_item_cliente( item_id, cliente_id, empresa_id )
{
	$('#dialog-message-form').empty();
	LINK_ajax('histo_item_cliente.php?item_id=' + item_id + '&cliente_id=' + cliente_id + '&empresa_id=' + empresa_id,'dialog-message-form');
	$('#dialog-message-form').dialog({
		modal: true,
		width: 600,
		height: "auto",
		position: { my: "center", at: "center", of: window },
		buttons: [
			{
				text: "Cerrar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy').empty();
				}
			}
		]
	});
}

function compra_link( desconocido_id )
{
	$('#dialog-message-form').empty();
	LINK_ajax('compra_agregar-link.php?desconocido_id=' + desconocido_id,'dialog-message-form');
	$('#dialog-message-form').dialog({
		modal: true,
		width: 900,
		height: 600,
		position: { my: "center", at: "center", of: window },
		buttons: [
			{
				text: "Cerrar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy').empty();
				}
			}
		]
	});
}

function c_use_codigo( compra_id, desconocido_id, item_id )
{
	$('#dialog-message-form').dialog('destroy').empty();
	LINK_ajax('compra_agregar_detalle.php?go=9&compra_id=' + compra_id + '&desconocido_id=' + desconocido_id + '&item_id=' + item_id,'detalle_div');
}

function doc_edit_gasto_clase( src_doc, src_col, src_id, src_table )
{
	$('#dialog-message-form').empty();
	LINK_ajax('doc_edit_gasto_clase.php?src_doc=' + src_doc + '&src_col=' + src_col + '&src_id=' + src_id + '&src_table=' + src_table,'dialog-message-form');
	$('#dialog-message-form').dialog({
		modal: true,
		width: 500,
		height: "auto",
		position: { my: "center", at: "center", of: window },
		buttons: [
			{
				text: "Aceptar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy');
					POST_ajax(src_doc,'main_div','dialog-form');
					$(this).empty();
				}
			},
			{
				text: "Cancelar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy').empty();
				}
			}
		]
	});
}

function ask_for_integer( info, val_label, val_name, val_min, val_max, val_default, go_val, src_doc, div_name )
{
	$('#dialog-message-form').empty();
	LINK_ajax('ask_for_integer.php?info=' + info + '&val_label=' + val_label + '&val_name=' + val_name + '&val_min=' + val_min + '&val_max=' + val_max + '&val_default=' + val_default + '&go_val=' + go_val,'dialog-message-form');
	$('#dialog-message-form').dialog({
		modal: true,
		width: 500,
		height: "auto",
		position: { my: "center", at: "center", of: window },
		buttons: [
			{
				text: "Aceptar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy');
					POST_ajax(src_doc,div_name,'dialog-form');
					$(this).empty();
				}
			},
			{
				text: "Cancelar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy').empty();
				}
			}
		]
	});
}

function ask_for_printer( val_default, go_val, src_doc, div_name )
{
	$('#dialog-message-form').empty();
	LINK_ajax('ask_for_printer.php?val_default=' + val_default + '&go_val=' + go_val,'dialog-message-form');
	$('#dialog-message-form').dialog({
		modal: true,
		width: 500,
		height: "auto",
		position: { my: "center", at: "center", of: window },
		buttons: [
			{
				text: "Imprimir",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy');
					POST_ajax(src_doc,div_name,'dialog-form');
					$(this).empty();
				}
			},
			{
				text: "Cancelar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy').empty();
				}
			}
		]
	});
}

function ask_for_taller( val_default, go_val, src_doc, div_name )
{
	$('#dialog-message-form').empty();
	LINK_ajax('ask_for_taller.php?val_default=' + val_default + '&go_val=' + go_val,'dialog-message-form');
	$('#dialog-message-form').dialog({
		modal: true,
		width: 500,
		height: "auto",
		position: { my: "center", at: "center", of: window },
		buttons: [
			{
				text: "Aceptar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy');
					POST_ajax(src_doc,div_name,'dialog-form');
					$(this).empty();
				}
			},
			{
				text: "Cancelar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy').empty();
				}
			}
		]
	});
}

function doc_edit_nota( src_doc, src_col, src_id, src_table, txt_col )
{
	$('#dialog-message-form').empty();
	LINK_ajax('doc_edit_nota.php?src_doc=' + src_doc + '&src_col=' + src_col + '&src_id=' + src_id + '&src_table=' + src_table + '&txt_col=' + txt_col,'dialog-message-form');
	$('#dialog-message-form').dialog({
		modal: true,
		width: 500,
		height: "auto",
		position: { my: "center", at: "center", of: window },
		buttons: [
			{
				text: "Aceptar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy');
					POST_ajax(src_doc,'main_div','dialog-form');
					$(this).empty();
				}
			},
			{
				text: "Cancelar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy').empty();
				}
			}
		]
	});
}

function doc_edit_fecha( src_doc, src_col, src_id, src_table, txt_col, txt_label )
{
	$('#dialog-message-form').empty();
	LINK_ajax('doc_edit_fecha.php?src_doc=' + src_doc + '&src_col=' + src_col + '&src_id=' + src_id + '&src_table=' + src_table + '&txt_col=' + txt_col + '&txt_label=' + txt_label,'dialog-message-form');
	$('#dialog-message-form').dialog({
		modal: true,
		width: 500,
		height: "auto",
		position: { my: "center", at: "center", of: window },
		buttons: [
			{
				text: "Aceptar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy');
					POST_ajax(src_doc,'main_div','dialog-form');
					$(this).empty();
				}
			},
			{
				text: "Cancelar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy').empty();
				}
			}
		]
	});
}

function doc_edit_double( src_doc, src_col, src_id, src_table, txt_col, txt_label, val_min, val_max )
{
	$('#dialog-message-form').empty();
	LINK_ajax('doc_edit_double.php?src_doc=' + src_doc + '&src_col=' + src_col + '&src_id=' + src_id + '&src_table=' + src_table + '&txt_col=' + txt_col + '&txt_label=' + txt_label + '&val_min=' + val_min + '&val_max=' + val_max,'dialog-message-form');
	$('#dialog-message-form').dialog({
		modal: true,
		width: 500,
		height: "auto",
		position: { my: "center", at: "center", of: window },
		buttons: [
			{
				text: "Aceptar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy');
					POST_ajax(src_doc,'main_div','dialog-form');
					$(this).empty();
				}
			},
			{
				text: "Cancelar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy').empty();
				}
			}
		]
	});
}

function asiento_edit_linea( asiento_linea_id )
{
	$('#dialog-message-form').empty();
	LINK_ajax('cnt_asiento_edit_linea.php?asiento_linea_id=' + asiento_linea_id,'dialog-message-form');
	$('#dialog-message-form').dialog({
		modal: true,
		width: 500,
		height: "auto",
		position: { my: "center", at: "center", of: window },
		buttons: [
			{
				text: "Aceptar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy');
					POST_ajax('cnt_asiento_ver_detalle.php','detalle_div','dialog-form');
					$(this).empty();
				}
			},
			{
				text: "Cancelar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy').empty();
				}
			}
		]
	});
}

function cliente_notas( cliente_id )
{
	$('#dialog-message-form').empty();
	LINK_ajax('cliente_comentario.php?cliente_id=' + cliente_id,'dialog-message-form');
	$('#dialog-message-form').dialog({
		modal: true,
		width: 500,
		height: "auto",
		position: { my: "center", at: "center", of: window },
		buttons: [
			{
				text: "Aceptar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy');
					POST_ajax('cliente_comentario.php','cnota_div','dialog-form');
					$(this).empty();
				}
			},
			{
				text: "Cancelar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy').empty();
				}
			}
		]
	});
}

function formapago_eliminar( forma_pago_id )
{
	$('#dialog-message-form').empty();
	LINK_ajax('formapago_eliminar.php?forma_pago_id=' + forma_pago_id,'dialog-message-form');
	$('#dialog-message-form').dialog({
		modal: true,
		width: 500,
		height: "auto",
		position: { my: "center", at: "center", of: window },
		buttons: [
			{
				text: "Aceptar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy');
					POST_ajax('formapago_lista.php','main_div','dialog-form');
					$(this).empty();
				}
			},
			{
				text: "Cancelar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy').empty();
				}
			}
		]
	});
}

function f_use_codigo( codigo, row_id )
{
	$('#' + row_id).addClass('b_black');
	$('#do_codigo').val(codigo);
	$('#tabs').tabs( "option", "active", 0 );
	$('#do_codigo').trigger('blur');

	$('#cantidad').select();
}

function fac_item_manual( factura_id, item_id, bodega_id, cantidad, descripcion, detalle, precio_unidad, av_confirmed, flags )
{
	$('#dialog-message-form').empty();

	var form = $('<form/>', { id: 'dialog-form'	});
	form.append($('<input/>', { name: 'go', type: 'hidden', value: '1' }));
	form.append($('<input/>', { name: 'factura_id', type: 'hidden', value: factura_id }));
	form.append($('<input/>', { name: 'item_id', type: 'hidden', value: item_id }));
	form.append($('<input/>', { name: 'bodega_id', type: 'hidden', value: bodega_id }));
	form.append($('<input/>', { name: 'tipo_precio', type: 'hidden', value: 0 }));
	form.append($('<input/>', { name: 'detalle', type: 'hidden', value: detalle }));
	form.append($('<input/>', { name: 'av_confirmed', type: 'hidden', value: av_confirmed }));
	form.append($('<input/>', { name: 'ma_confirmed', type: 'hidden', value: 1 }));
	form.append($('<input/>', { name: 'flags', type: 'hidden', value: flags }));

	var input_desc = $('<input/>', { name: 'descripcion', type: 'text', class: 'input_text_large', value: descripcion });
	var input_precio = $('<input/>', { id: 'manual_precio', name: 'precio', type: 'text', class: 'input_text_money', placeholder: '0.00', value: precio_unidad });
	var input_descuento = $('<input/>', { name: 'descuento', type: 'number', min: 0, max: 100, step: 'any', class: 'input_text_shortint', placeholder: '0.00', value: '0.00' });
	var input_cantidad = $('<input/>', { name: 'cantidad', type: 'number', min: 1, max: 9999, step: 1, class: 'input_text_shortint', placeholder: '1', value: cantidad });

	form.append('Artículo con precio manual.<br />Indique los valores deseados de precio y descuento.<br /><br /><span class="b_red">Descripción de Artículo</span><br />&#10151;', input_desc, '<br /><span class="n_orange">Precio Unitario y Descuento</span><br />&#10151;', input_precio, input_descuento, '<br /><span class="n_green">Cantidad</span><br />&#10151;', input_cantidad);
	form.on('submit', function(e) {
		e.preventDefault();

		$('#dialog-message-form').dialog('destroy');
		POST_ajax('factura_agregar_detalle.php','detalle_div','dialog-form');
		$('#dialog-message-form').empty();
	});

	$('#dialog-message-form').append(form).dialog({
		modal: true,
		width: 500,
		height: "auto",
		position: { my: "center", at: "center", of: window },
		buttons: [
			{
				text: "Aceptar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy');
					POST_ajax('factura_agregar_detalle.php','detalle_div','dialog-form');
					$(this).empty();
				}
			},
			{
				text: "Cancelar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy').empty();
					if( $('#do_codigo').length ) { $('#do_codigo').focus(); }
				}
			}
		],
		open: function( event, ui ) {
			$('#manual_precio').autoNumeric('init',{
				minimumValue : '0',
				maximumValue : '999999999.99',
				digitGroupSeparator : ',',
				decimalCharacter : '.',
				decimalCharacterAlternative : '.'
			});
		}
	});

	if( item_id != 4 && item_id != 5 )
	{
		input_desc.prop('disabled',true);
		input_precio.select();
	}
	else
	{
		input_desc.select();
	}
}

function ord_item_manual( orden_id, item_id, cantidad, descripcion, detalle, precio_unidad )
{
	$('#dialog-message-form').empty();

	var form = $('<form/>', { id: 'dialog-form'	});
	form.append($('<input/>', { name: 'go', type: 'hidden', value: '1' }));
	form.append($('<input/>', { name: 'orden_id', type: 'hidden', value: orden_id }));
	form.append($('<input/>', { name: 'item_id', type: 'hidden', value: item_id }));
	form.append($('<input/>', { name: 'tipo_precio', type: 'hidden', value: 0 }));
	form.append($('<input/>', { name: 'detalle', type: 'hidden', value: detalle }));
	form.append($('<input/>', { name: 'ma_confirmed', type: 'hidden', value: 1 }));

	var input_desc = $('<input/>', { name: 'descripcion', type: 'text', class: 'input_text_large', value: descripcion });
	var input_precio = $('<input/>', { id: 'manual_precio', name: 'precio', type: 'text', class: 'input_text_money', placeholder: '0.00', value: precio_unidad });
	var input_descuento = $('<input/>', { name: 'descuento', type: 'number', min: 0, max: 100, step: 'any', class: 'input_text_shortint', placeholder: '0.00', value: '0.00' });
	var input_cantidad = $('<input/>', { name: 'cantidad', type: 'number', min: 1, max: 9999, step: 1, class: 'input_text_shortint', placeholder: '1', value: cantidad });

	form.append('Artículo con precio manual.<br />Indique los valores deseados de precio y descuento.<br /><br /><span class="b_red">Descripción de Artículo</span><br />&#10151;', input_desc, '<br /><span class="n_orange">Precio Unitario y Descuento</span><br />&#10151;', input_precio, input_descuento, '<br /><span class="n_green">Cantidad</span><br />&#10151;', input_cantidad);
	form.on('submit', function(e) {
		e.preventDefault();

		$('#dialog-message-form').dialog('destroy');
		POST_ajax('proforma_agregar_detalle.php','detalle_div','dialog-form');
		$('#dialog-message-form').empty();
	});

	$('#dialog-message-form').append(form).dialog({
		modal: true,
		width: 500,
		height: "auto",
		position: { my: "center", at: "center", of: window },
		buttons: [
			{
				text: "Aceptar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy');
					POST_ajax('proforma_agregar_detalle.php','detalle_div','dialog-form');
					$(this).empty();
				}
			},
			{
				text: "Cancelar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy').empty();
					if( $('#do_codigo').length ) { $('#do_codigo').focus(); }
				}
			}
		],
		open: function( event, ui ) {
			$('#manual_precio').autoNumeric('init',{
				minimumValue : '0',
				maximumValue : '999999999.99',
				digitGroupSeparator : ',',
				decimalCharacter : '.',
				decimalCharacterAlternative : '.'
			});
		}
	});

	if( item_id != 4 && item_id != 5 )
	{
		input_desc.prop('disabled',true);
		input_precio.select();
	}
	else
	{
		input_desc.select();
	}
}

function pnt_item_manual( prenota_id, item_id, cantidad, descripcion, detalle, precio_unidad )
{
	$('#dialog-message-form').empty();

	var form = $('<form/>', { id: 'dialog-form'	});
	form.append($('<input/>', { name: 'go', type: 'hidden', value: '1' }));
	form.append($('<input/>', { name: 'prenota_id', type: 'hidden', value: prenota_id }));
	form.append($('<input/>', { name: 'item_id', type: 'hidden', value: item_id }));
	form.append($('<input/>', { name: 'tipo_precio', type: 'hidden', value: 0 }));
	form.append($('<input/>', { name: 'detalle', type: 'hidden', value: detalle }));
	form.append($('<input/>', { name: 'ma_confirmed', type: 'hidden', value: 1 }));

	var input_desc = $('<input/>', { name: 'descripcion', type: 'text', class: 'input_text_large', value: descripcion });
	var input_precio = $('<input/>', { id: 'manual_precio', name: 'precio', type: 'text', class: 'input_text_money', placeholder: '0.00', value: precio_unidad });
	var input_descuento = $('<input/>', { name: 'descuento', type: 'number', min: 0, max: 100, step: 'any', class: 'input_text_shortint', placeholder: '0.00', value: '0.00' });
	var input_cantidad = $('<input/>', { name: 'cantidad', type: 'number', min: 1, max: 9999, step: 1, class: 'input_text_shortint', placeholder: '1', value: cantidad });

	form.append('Artículo con precio manual.<br />Indique los valores deseados de precio y descuento.<br /><br /><span class="b_red">Descripción de Artículo</span><br />&#10151;', input_desc, '<br /><span class="n_orange">Precio Unitario y Descuento</span><br />&#10151;', input_precio, input_descuento, '<br /><span class="n_green">Cantidad</span><br />&#10151;', input_cantidad);
	form.on('submit', function(e) {
		e.preventDefault();

		$('#dialog-message-form').dialog('destroy');
		POST_ajax('prenota_agregar_detalle.php','detalle_div','dialog-form');
		$('#dialog-message-form').empty();
	});

	$('#dialog-message-form').append(form).dialog({
		modal: true,
		width: 500,
		height: "auto",
		position: { my: "center", at: "center", of: window },
		buttons: [
			{
				text: "Aceptar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy');
					POST_ajax('prenota_agregar_detalle.php','detalle_div','dialog-form');
					$(this).empty();
				}
			},
			{
				text: "Cancelar",
				"class": "input_submit",
				click: function() {
					$(this).dialog('destroy').empty();
					if( $('#do_codigo').length ) { $('#do_codigo').focus(); }
				}
			}
		],
		open: function( event, ui ) {
			$('#manual_precio').autoNumeric('init',{
				minimumValue : '0',
				maximumValue : '999999999.99',
				digitGroupSeparator : ',',
				decimalCharacter : '.',
				decimalCharacterAlternative : '.'
			});
		}
	});

	if( item_id != 4 && item_id != 5 )
	{
		input_desc.prop('disabled',true);
		input_precio.select();
	}
	else
	{
		input_desc.select();
	}
}

// ***************************************************
// Table Fixed Headers
// ***************************************************

(function($) {

	$.fn.tableHeadFixer = function(param) {
		var defaults = {
			head: true,
			foot: false,
			left: 0,
			right: 0
		};

		var settings = $.extend({}, defaults, param);

		return this.each(function() {
			settings.table = this;
			settings.parent = $("<div></div>", { id: 'results_fixed' });
			setParent();

			if(settings.head == true)
				fixHead();

			if(settings.foot == true)
				fixFoot();

			if(settings.left > 0)
				fixLeft();

			if(settings.right > 0)
				fixRight();

			// self.setCorner();

			$(settings.parent).trigger("scroll");

			$(window).resize(function() {
				$(settings.parent).trigger("scroll");
			});
		});

		function setTable(table) {

		}


		function setParent() {
			var container = $(settings.table).parent();
			var parent = $(settings.parent);
			var table = $(settings.table);

			table.before(parent);
			parent.append(table);
			parent
				.css({
					'width' : '100%',
					'height' : container.css("height"),
					'overflow' : 'scroll',
					'max-height' : container.css("max-height"),
					'min-height' : container.css("min-height"),
					'max-width' : container.css('max-width'),
					'min-width' : container.css('min-width')
				});

			parent.scroll(function() {
				var scrollWidth = parent[0].scrollWidth;
				var clientWidth = parent[0].clientWidth;
				var scrollHeight = parent[0].scrollHeight;
				var clientHeight = parent[0].clientHeight;
				var top = parent.scrollTop();
				var left = parent.scrollLeft();

				if(settings.head)
					this.find("thead tr > *").css("top", top);

				if(settings.foot)
					this.find("tfoot tr > *").css("bottom", scrollHeight - clientHeight - top);

				if(settings.left > 0)
					settings.leftColumns.css("left", left);

				if(settings.right > 0)
					settings.rightColumns.css("right", scrollWidth - clientWidth - left);
			}.bind(table));
		}

		function fixHead () {
			var thead = $(settings.table).find("thead");
			var tr = thead.find("tr");
			var cells = thead.find("tr > *");

			setBackground(cells);
			cells.css({
				'position' : 'relative'
			});
		}

		function fixFoot () {
			var tfoot = $(settings.table).find("tfoot");
			var tr = tfoot.find("tr");
			var cells = tfoot.find("tr > *");

			setBackground(cells);
			cells.css({
				'position' : 'relative'
			});
		}

		function fixLeft () {
			var table = $(settings.table);

			var fixColumn = settings.left;

			settings.leftColumns = $();

			for(var i = 1; i <= fixColumn; i++) {
				settings.leftColumns = settings.leftColumns
					.add(table.find("tr > *:nth-child(" + i + ")"));
			}

			var column = settings.leftColumns;

			column.each(function(k, cell) {
				var cell = $(cell);

				setBackground(cell);
				cell.css({
					'position' : 'relative'
				});
			});
		}

		function fixRight () {
			var table = $(settings.table);

			var fixColumn = settings.right;

			settings.rightColumns = $();

			for(var i = 1; i <= fixColumn; i++) {
				settings.rightColumns = settings.rightColumns
					.add(table.find("tr > *:nth-last-child(" + i + ")"));
			}

			var column = settings.rightColumns;

			column.each(function(k, cell) {
				var cell = $(cell);

				setBackground(cell);
				cell.css({
					'position' : 'relative'
				});
			});

		}

		function setBackground(elements) {
			elements.each(function(k, element) {
				var element = $(element);
				var parent = $(element).parent();

				var elementBackground = element.css("background-color");
				elementBackground = (elementBackground == "transparent" || elementBackground == "rgba(0, 0, 0, 0)") ? null : elementBackground;

				var parentBackground = parent.css("background-color");
				parentBackground = (parentBackground == "transparent" || parentBackground == "rgba(0, 0, 0, 0)") ? null : parentBackground;

				var background = parentBackground ? parentBackground : "white";
				background = elementBackground ? elementBackground : background;

				element.css("background-color", background);
			});
		}
	};

})(jQuery);

// ***************************************************
// AutoNumeric
// ***************************************************

(function (factory) {
	if (typeof define === "function" && define.amd) {
		/** AMD. Register as an anonymous module. */
		define(["jquery"], factory);
	} else if (typeof module === "object" && module.exports) {
		/** Node/CommonJS */
		module.exports = factory(require("jquery"));
	} else {
		/** Browser globals */
		factory(window.jQuery);
	}
}(function ($) {
	"use strict";
	/*jslint browser: true*/
	/*global jQuery: false*/
	/*Cross browser routine for getting selected range/cursor position
	 */

	/**
	 * Cross browser routine for getting selected range/cursor position
	 */

	function getElementSelection(that) {
		var position = {};
		if (that.selectionStart === undefined) {
			that.focus();
			var select = document.selection.createRange();
			position.length = select.text.length;
			select.moveStart('character', -that.value.length);
			position.end = select.text.length;
			position.start = position.end - position.length;
		} else {
			position.start = that.selectionStart;
			position.end = that.selectionEnd;
			position.length = position.end - position.start;
		}
		return position;
	}

	/**
	 * Cross browser routine for setting selected range/cursor position
	 */

	function setElementSelection(that, start, end) {
		if (that.selectionStart === undefined) {
			that.focus();
			var r = that.createTextRange();
			r.collapse(true);
			r.moveEnd('character', end);
			r.moveStart('character', start);
			r.select();
		} else {
			that.selectionStart = start;
			that.selectionEnd = end;
		}
	}

	/**
	 * run callbacks in parameters if any
	 * any parameter could be a callback:
	 * - a function, which invoked with jQuery element, parameters and this parameter name and returns parameter value
	 * - a name of function, attached to $(selector).autoNumeric.functionName(){} - which was called previously
	 */

	function runCallbacks($this, settings) {
		/**
		 * loops through the settings object (option array) to find the following
		 * k = option name example k=aNum
		 * val = option value example val=0123456789
		 */
		$.each(settings, function (k, val) {
			if (typeof val === 'function') {
				settings[k] = val($this, settings, k);
			} else if (typeof $this.autoNumeric[val] === 'function') {
				/**
				 * calls the attached function from the html5 data example: data-a-sign="functionName"
				 */
				settings[k] = $this.autoNumeric[val]($this, settings, k);
			}
		});
	}

	/**
	 * Converts the vMin, vMax & mDec string to numeric value
	 */

	function convertKeyToNumber(settings, key) {
		if (typeof(settings[key]) === 'string') {
			settings[key] *= 1;
		}
	}

	/**
	 * Preparing user defined options for further usage
	 * merge them with defaults appropriately
	 */

	function autoCode($this, settings) {
		runCallbacks($this, settings);
		settings.tagList = ['b', 'caption', 'cite', 'code', 'dd', 'del', 'div', 'dfn', 'dt', 'em', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'ins', 'kdb', 'label', 'li', 'output', 'p', 'q', 's', 'sample', 'span', 'strong', 'td', 'th', 'u', 'var'];
		var vmax = settings.vMax.toString().split('.'),
			vmin = (!settings.vMin && settings.vMin !== 0) ? [] : settings.vMin.toString().split('.');
		convertKeyToNumber(settings, 'vMax');
		convertKeyToNumber(settings, 'vMin');
		convertKeyToNumber(settings, 'mDec'); /** set mDec if not defined by user */
		settings.mDec = (settings.mRound === 'CHF') ? '2' : settings.mDec;
		settings.allowLeading = true;
		settings.aNeg = settings.vMin < 0 ? '-' : '';
		vmax[0] = vmax[0].replace('-', '');
		vmin[0] = vmin[0].replace('-', '');
		settings.mInt = Math.max(vmax[0].length, vmin[0].length, 1);
		if (settings.mDec === null) {
			var vmaxLength = 0,
				vminLength = 0;
			if (vmax[1]) {
				vmaxLength = vmax[1].length;
			}
			if (vmin[1]) {
				vminLength = vmin[1].length;
			}
			settings.mDec = Math.max(vmaxLength, vminLength);
		} /** set alternative decimal separator key */
		if (settings.altDec === null && settings.mDec > 0) {
			if (settings.aDec === '.' && settings.aSep !== ',') {
				settings.altDec = ',';
			} else if (settings.aDec === ',' && settings.aSep !== '.') {
				settings.altDec = '.';
			}
		}
		/** cache regexps for autoStrip */
		var aNegReg = settings.aNeg ? '([-\\' + settings.aNeg + ']?)' : '(-?)';
		settings.aNegRegAutoStrip = aNegReg;
		settings.skipFirstAutoStrip = new RegExp(aNegReg + '[^-' + (settings.aNeg ? '\\' + settings.aNeg : '') + '\\' + settings.aDec + '\\d]' + '.*?(\\d|\\' + settings.aDec + '\\d)');
		settings.skipLastAutoStrip = new RegExp('(\\d\\' + settings.aDec + '?)[^\\' + settings.aDec + '\\d]\\D*$');
		var allowed = '-' + settings.aNum + '\\' + settings.aDec;
		settings.allowedAutoStrip = new RegExp('[^' + allowed + ']', 'gi');
		settings.numRegAutoStrip = new RegExp(aNegReg + '(?:\\' + settings.aDec + '?(\\d+\\' + settings.aDec + '\\d+)|(\\d*(?:\\' + settings.aDec + '\\d*)?))');
		return settings;
	}

	/**
	 * strips all unwanted characters and leave only a number alert
	 */

	function autoStrip(s, settings, strip_zero) {
		if (settings.aSign) { /** remove currency sign */
			while (s.indexOf(settings.aSign) > -1) {
				s = s.replace(settings.aSign, '');
			}
		}
		s = s.replace(settings.skipFirstAutoStrip, '$1$2'); /** first replace anything before digits */
		s = s.replace(settings.skipLastAutoStrip, '$1'); /** then replace anything after digits */
		s = s.replace(settings.allowedAutoStrip, ''); /** then remove any uninterested characters */
		if (settings.altDec) {
			s = s.replace(settings.altDec, settings.aDec);
		} /** get only number string */
		var m = s.match(settings.numRegAutoStrip);
		s = m ? [m[1], m[2], m[3]].join('') : '';
		if ((settings.lZero === 'allow' || settings.lZero === 'keep') && strip_zero !== 'strip') {
			var parts = [],
				nSign = '';
			parts = s.split(settings.aDec);
			if (parts[0].indexOf('-') !== -1) {
				nSign = '-';
				parts[0] = parts[0].replace('-', '');
			}
			if (parts[0].length > settings.mInt && parts[0].charAt(0) === '0') { /** strip leading zero if need */
				parts[0] = parts[0].slice(1);
			}
			s = nSign + parts.join(settings.aDec);
		}
		if ((strip_zero && settings.lZero === 'deny') || (strip_zero && settings.lZero === 'allow' && settings.allowLeading === false)) {
			var strip_reg = '^' + settings.aNegRegAutoStrip + '0*(\\d' + (strip_zero === 'leading' ? ')' : '|$)');
			strip_reg = new RegExp(strip_reg);
			s = s.replace(strip_reg, '$1$2');
		}
		return s;
	}

	/**
	 * places or removes brackets on negative values
	 * works only when with pSign: 'p'
	 */

	function negativeBracket(s, settings) {
		if (settings.pSign === 'p') {
			var brackets = settings.nBracket.split(',');
			if (!settings.hasFocus && !settings.removeBrackets) {
				s = s.replace(settings.aNeg, '');
				s = brackets[0] + s + brackets[1];
			} else if ((settings.hasFocus && s.charAt(0) === brackets[0]) || (settings.removeBrackets && s.charAt(0) === brackets[0])) {
				s = s.replace(brackets[0], settings.aNeg);
				s = s.replace(brackets[1], '');
			}
		}
		return s;
	}

	/**
	 * function to handle numbers less than 0 that are stored in Exponential notation ex: .0000001 stored as 1e-7
	 */

	function checkValue(value, settings) {
		if (value) {
			var checkSmall = +value;
			if (checkSmall < 0.000001 && checkSmall > -1) {
				value = +value;
				if (value < 0.000001 && value > 0) {
					value = (value + 10).toString();
					value = value.substring(1);
				}
				if (value < 0 && value > -1) {
					value = (value - 10).toString();
					value = '-' + value.substring(2);
				}
				value = value.toString();
			} else {
				var parts = value.split('.');
				if (parts[1] !== undefined) {
					if (+parts[1] === 0) {
						value = parts[0];
					} else {
						parts[1] = parts[1].replace(/0*$/, '');
						value = parts.join('.');
					}
				}
			}
		}
		return (settings.lZero === 'keep') ? value : value.replace(/^0*(\d)/, '$1');
	}

	/**
	 * prepare number string to be converted to real number
	 */

	function fixNumber(s, aDec, aNeg) {
		if (aDec && aDec !== '.') {
			s = s.replace(aDec, '.');
		}
		if (aNeg && aNeg !== '-') {
			s = s.replace(aNeg, '-');
		}
		if (!s.match(/\d/)) {
			s += '0';
		}
		return s;
	}

	/**
	 * prepare real number to be converted to our format
	 */

	function presentNumber(s, aDec, aNeg) {
		if (aNeg && aNeg !== '-') {
			s = s.replace('-', aNeg);
		}
		if (aDec && aDec !== '.') {
			s = s.replace('.', aDec);
		}
		return s;
	}

	/**
	 * private function to check for empty value
	 */

	function checkEmpty(iv, settings, signOnEmpty) {
		if (iv === '' || iv === settings.aNeg) {
			if (settings.wEmpty === 'zero') {
				return iv + '0';
			}
			if (settings.wEmpty === 'sign' || signOnEmpty) {
				return iv + settings.aSign;
			}
			return iv;
		}
		return null;
	}

	/**
	 * private function that formats our number
	 */

	function autoGroup(iv, settings) {
		iv = autoStrip(iv, settings);
		var testNeg = iv.replace(',', '.'),
			empty = checkEmpty(iv, settings, true);
		if (empty !== null) {
			return empty;
		}
		var digitalGroup = '';
		if (settings.dGroup === 2) {
			digitalGroup = /(\d)((\d)(\d{2}?)+)$/;
		} else if (settings.dGroup === 4) {
			digitalGroup = /(\d)((\d{4}?)+)$/;
		} else {
			digitalGroup = /(\d)((\d{3}?)+)$/;
		} /** splits the string at the decimal string */
		var ivSplit = iv.split(settings.aDec);
		if (settings.altDec && ivSplit.length === 1) {
			ivSplit = iv.split(settings.altDec);
		} /** assigns the whole number to the a variable (s) */
		var s = ivSplit[0];
		if (settings.aSep) {
			while (digitalGroup.test(s)) { /** re-inserts the thousand separator via a regular expression */
				s = s.replace(digitalGroup, '$1' + settings.aSep + '$2');
			}
		}
		if (settings.mDec !== 0 && ivSplit.length > 1) {
			if (ivSplit[1].length > settings.mDec) {
				ivSplit[1] = ivSplit[1].substring(0, settings.mDec);
			} /** joins the whole number with the decimal value */
			iv = s + settings.aDec + ivSplit[1];
		} else { /** if whole numbers only */
			iv = s;
		}
		if (settings.aSign) {
			var has_aNeg = iv.indexOf(settings.aNeg) !== -1;
			iv = iv.replace(settings.aNeg, '');
			iv = settings.pSign === 'p' ? settings.aSign + iv : iv + settings.aSign;
			if (has_aNeg) {
				iv = settings.aNeg + iv;
			}
		}
		if (testNeg < 0 && settings.nBracket !== null) { /** removes the negative sign and places brackets */
			iv = negativeBracket(iv, settings);
		}
		return iv;
	}

	/**
	 * round number after setting by pasting or $().autoNumericSet()
	 * private function for round the number
	 * please note this handled as text - JavaScript math function can return inaccurate values
	 * also this offers multiple rounding methods that are not easily accomplished in JavaScript
	 */

	function autoRound(iv, settings) { /** value to string */
		iv = (iv === '') ? '0' : iv.toString();
		convertKeyToNumber(settings, 'mDec'); /** set mDec to number needed when mDec set by 'update method */
		if (settings.mRound === 'CHF') {
			iv = (Math.round(iv * 20) / 20).toString();
		}
		var ivRounded = '',
			i = 0,
			nSign = '',
			rDec = (typeof(settings.aPad) === 'boolean' || settings.aPad === null) ? (settings.aPad ? settings.mDec : 0) : +settings.aPad;
		var truncateZeros = function (ivRounded) { /** truncate not needed zeros */
			var regex = (rDec === 0) ? (/(\.(?:\d*[1-9])?)0*$/) : rDec === 1 ? (/(\.\d(?:\d*[1-9])?)0*$/) : new RegExp('(\\.\\d{' + rDec + '}(?:\\d*[1-9])?)0*$');
			ivRounded = ivRounded.replace(regex, '$1'); /** If there are no decimal places, we don't need a decimal point at the end */
			if (rDec === 0) {
				ivRounded = ivRounded.replace(/\.$/, '');
			}
			return ivRounded;
		};
		if (iv.charAt(0) === '-') { /** Checks if the iv (input Value)is a negative value */
			nSign = '-';
			iv = iv.replace('-', ''); /** removes the negative sign will be added back later if required */
		}
		if (!iv.match(/^\d/)) { /** append a zero if first character is not a digit (then it is likely to be a dot)*/
			iv = '0' + iv;
		}
		if (nSign === '-' && +iv === 0) { /** determines if the value is zero - if zero no negative sign */
			nSign = '';
		}
		if ((+iv > 0 && settings.lZero !== 'keep') || (iv.length > 0 && settings.lZero === 'allow')) { /** trims leading zero's if needed */
			iv = iv.replace(/^0*(\d)/, '$1');
		}
		var dPos = iv.lastIndexOf('.'),
			/** virtual decimal position */
			vdPos = (dPos === -1) ? iv.length - 1 : dPos,
			/** checks decimal places to determine if rounding is required */
			cDec = (iv.length - 1) - vdPos; /** check if no rounding is required */
		if (cDec <= settings.mDec) {
			ivRounded = iv; /** check if we need to pad with zeros */
			if (cDec < rDec) {
				if (dPos === -1) {
					ivRounded += settings.aDec;
				}
				var zeros = '000000';
				while (cDec < rDec) {
					zeros = zeros.substring(0, rDec - cDec);
					ivRounded += zeros;
					cDec += zeros.length;
				}
			} else if (cDec > rDec) {
				ivRounded = truncateZeros(ivRounded);
			} else if (cDec === 0 && rDec === 0) {
				ivRounded = ivRounded.replace(/\.$/, '');
			}
			if (settings.mRound !== 'CHF') {
				return (+ivRounded === 0) ? ivRounded : nSign + ivRounded;
			}
			if (settings.mRound === 'CHF') {
				dPos = ivRounded.lastIndexOf('.');
				iv = ivRounded;
			}

		} /** rounded length of the string after rounding */
		var rLength = dPos + settings.mDec,
			tRound = +iv.charAt(rLength + 1),
			ivArray = iv.substring(0, rLength + 1).split(''),
			odd = (iv.charAt(rLength) === '.') ? (iv.charAt(rLength - 1) % 2) : (iv.charAt(rLength) % 2),
			onePass = true;
		if (odd !== 1) {
			odd = (odd === 0 && (iv.substring(rLength + 2, iv.length) > 0)) ? 1 : 0;
		}
		/*jslint white: true*/
		if ((tRound > 4 && settings.mRound === 'S') || /**  Round half up symmetric */
			(tRound > 4 && settings.mRound === 'A' && nSign === '') || /**  Round half up asymmetric positive values */
			(tRound > 5 && settings.mRound === 'A' && nSign === '-') || /** Round half up asymmetric negative values */
			(tRound > 5 && settings.mRound === 's') || /**  Round half down symmetric */
			(tRound > 5 && settings.mRound === 'a' && nSign === '') || /**  Round half down asymmetric positive values */
			(tRound > 4 && settings.mRound === 'a' && nSign === '-') || /** Round half down asymmetric negative values */
			(tRound > 5 && settings.mRound === 'B') || /**  Round half even "Banker's Rounding" */
			(tRound === 5 && settings.mRound === 'B' && odd === 1) || /**   Round half even "Banker's Rounding" */
			(tRound > 0 && settings.mRound === 'C' && nSign === '') || /**  Round to ceiling toward positive infinite */
			(tRound > 0 && settings.mRound === 'F' && nSign === '-') || /** Round to floor toward negative infinite */
			(tRound > 0 && settings.mRound === 'U') || /**  round up away from zero */
			(settings.mRound === 'CHF')) { /**  Round Swiss FRanc */
			/*jslint white: false*/
			for (i = (ivArray.length - 1); i >= 0; i -= 1) { /** Round up the last digit if required, and continue until no more 9's are found */
				if (ivArray[i] !== '.') {
					if (settings.mRound === 'CHF' && ivArray[i] <= 2 && onePass) {
						ivArray[i] = 0;
						onePass = false;
						break;
					}
					if (settings.mRound === 'CHF' && ivArray[i] <= 7 && onePass) {
						ivArray[i] = 5;
						onePass = false;
						break;
					}
					if (settings.mRound === 'CHF' && onePass) {
						ivArray[i] = 10;
						onePass = false;
					} else {
						ivArray[i] = +ivArray[i] + 1;
					}
					if (ivArray[i] < 10) {
						break;
					}
					if (i > 0) {
						ivArray[i] = '0';
					}
				}
			}
		}
		ivArray = ivArray.slice(0, rLength + 1); /** Reconstruct the string, converting any 10's to 0's */
		ivRounded = truncateZeros(ivArray.join('')); /** return rounded value */
		return (+ivRounded === 0) ? ivRounded : nSign + ivRounded;
	}

	/**
	 * truncate decimal part of a number
	 */

	function truncateDecimal(s, settings, paste) {
		var aDec = settings.aDec,
			mDec = settings.mDec;
		s = (paste === 'paste') ? autoRound(s, settings) : s;
		if (aDec && mDec) {
			var parts = s.split(aDec);
			/** truncate decimal part to satisfying length
			 * cause we would round it anyway */
			if (parts[1] && parts[1].length > mDec) {
				if (mDec > 0) {
					parts[1] = parts[1].substring(0, mDec);
					s = parts.join(aDec);
				} else {
					s = parts[0];
				}
			}
		}
		return s;
	}

	/**
	 * checking that number satisfy format conditions
	 * and lays between settings.vMin and settings.vMax
	 * and the string length does not exceed the digits in settings.vMin and settings.vMax
	 */

	function autoCheck(s, settings) {
		s = autoStrip(s, settings);
		s = truncateDecimal(s, settings);
		s = fixNumber(s, settings.aDec, settings.aNeg);
		var value = +s;
		return value >= settings.vMin && value <= settings.vMax;
	}

	/**
	 * Holder object for field properties
	 */

	function AutoNumericHolder(that, settings) {
		this.settings = settings;
		this.that = that;
		this.$that = $(that);
		this.formatted = false;
		this.settingsClone = autoCode(this.$that, this.settings);
		this.value = that.value;
	}
	AutoNumericHolder.prototype = {
		init: function (e) {
			this.value = this.that.value;
			this.settingsClone = autoCode(this.$that, this.settings);
			this.ctrlKey = e.ctrlKey;
			this.cmdKey = e.metaKey;
			this.shiftKey = e.shiftKey;
			this.selection = getElementSelection(this.that); /** keypress event overwrites meaningful value of e.keyCode */
			if (e.type === 'keydown' || e.type === 'keyup') {
				this.kdCode = e.keyCode;
			}
			this.which = e.which;
			this.processed = false;
			this.formatted = false;
		},
		setSelection: function (start, end, setReal) {
			start = Math.max(start, 0);
			end = Math.min(end, this.that.value.length);
			this.selection = {
				start: start,
				end: end,
				length: end - start
			};
			if (setReal === undefined || setReal) {
				setElementSelection(this.that, start, end);
			}
		},
		setPosition: function (pos, setReal) {
			this.setSelection(pos, pos, setReal);
		},
		getBeforeAfter: function () {
			var value = this.value,
				left = value.substring(0, this.selection.start),
				right = value.substring(this.selection.end, value.length);
			return [left, right];
		},
		getBeforeAfterStriped: function () {
			var parts = this.getBeforeAfter();
			parts[0] = autoStrip(parts[0], this.settingsClone);
			parts[1] = autoStrip(parts[1], this.settingsClone);
			return parts;
		},

		/**
		 * strip parts from excess characters and leading zeroes
		 */
		normalizeParts: function (left, right) {
			var settingsClone = this.settingsClone;
			right = autoStrip(right, settingsClone); /** if right is not empty and first character is not aDec, */
			/** we could strip all zeros, otherwise only leading */
			var strip = right.match(/^\d/) ? true : 'leading';
			left = autoStrip(left, settingsClone, strip); /** prevents multiple leading zeros from being entered */
			if ((left === '' || left === settingsClone.aNeg) && settingsClone.lZero === 'deny') {
				if (right > '') {
					right = right.replace(/^0*(\d)/, '$1');
				}
			}
			var new_value = left + right; /** insert zero if has leading dot */
			if (settingsClone.aDec) {
				var m = new_value.match(new RegExp('^' + settingsClone.aNegRegAutoStrip + '\\' + settingsClone.aDec));
				if (m) {
					left = left.replace(m[1], m[1] + '0');
					new_value = left + right;
				}
			} /** insert zero if number is empty and io.wEmpty == 'zero' */
			if (settingsClone.wEmpty === 'zero' && (new_value === settingsClone.aNeg || new_value === '')) {
				left += '0';
			}
			return [left, right];
		},

		/**
		 * set part of number to value keeping position of cursor
		 */
		setValueParts: function (left, right, paste) {
			var settingsClone = this.settingsClone,
				parts = this.normalizeParts(left, right),
				new_value = parts.join(''),
				position = parts[0].length;
			if (autoCheck(new_value, settingsClone)) {
				new_value = truncateDecimal(new_value, settingsClone, paste);
				if (position > new_value.length) {
					position = new_value.length;
				}
				this.value = new_value;
				this.setPosition(position, false);
				return true;
			}
			return false;
		},

		/**
		 * helper function for expandSelectionOnSign
		 * returns sign position of a formatted value
		 */
		signPosition: function () {
			var settingsClone = this.settingsClone,
				aSign = settingsClone.aSign,
				that = this.that;
			if (aSign) {
				var aSignLen = aSign.length;
				if (settingsClone.pSign === 'p') {
					var hasNeg = settingsClone.aNeg && that.value && that.value.charAt(0) === settingsClone.aNeg;
					return hasNeg ? [1, aSignLen + 1] : [0, aSignLen];
				}
				var valueLen = that.value.length;
				return [valueLen - aSignLen, valueLen];
			}
			return [1000, -1];
		},

		/**
		 * expands selection to cover whole sign
		 * prevents partial deletion/copying/overwriting of a sign
		 */
		expandSelectionOnSign: function (setReal) {
			var sign_position = this.signPosition(),
				selection = this.selection;
			if (selection.start < sign_position[1] && selection.end > sign_position[0]) { /** if selection catches something except sign and catches only space from sign */
				if ((selection.start < sign_position[0] || selection.end > sign_position[1]) && this.value.substring(Math.max(selection.start, sign_position[0]), Math.min(selection.end, sign_position[1])).match(/^\s*$/)) { /** then select without empty space */
					if (selection.start < sign_position[0]) {
						this.setSelection(selection.start, sign_position[0], setReal);
					} else {
						this.setSelection(sign_position[1], selection.end, setReal);
					}
				} else { /** else select with whole sign */
					this.setSelection(Math.min(selection.start, sign_position[0]), Math.max(selection.end, sign_position[1]), setReal);
				}
			}
		},

		/**
		 * try to strip pasted value to digits
		 */
		checkPaste: function () {
			if (this.valuePartsBeforePaste !== undefined) {
				var parts = this.getBeforeAfter(),

					oldParts = this.valuePartsBeforePaste;
				delete this.valuePartsBeforePaste; /** try to strip pasted value first */
				parts[0] = parts[0].substr(0, oldParts[0].length) + autoStrip(parts[0].substr(oldParts[0].length), this.settingsClone);
				if (!this.setValueParts(parts[0], parts[1], 'paste')) {
					this.value = oldParts.join('');
					this.setPosition(oldParts[0].length, false);
				}
			}
		},

		/**
		 * process pasting, cursor moving and skipping of not interesting keys
		 * if returns true, further processing is not performed
		 */
		skipAllways: function (e) {
			var kdCode = this.kdCode,
				which = this.which,
				ctrlKey = this.ctrlKey,
				cmdKey = this.cmdKey,
				shiftKey = this.shiftKey; /** catch the ctrl up on ctrl-v */
			if (((ctrlKey || cmdKey) && e.type === 'keyup' && this.valuePartsBeforePaste !== undefined) || (shiftKey && kdCode === 45)) {
				this.checkPaste();
				return false;
			}
			/** codes are taken from http://www.cambiaresearch.com/c4/702b8cd1-e5b0-42e6-83ac-25f0306e3e25/Javascript-Char-Codes-Key-Codes.aspx
			 * skip Fx keys, windows keys, other special keys
			 * Thanks Ney Estrabelli for the FF for Mac meta key support "keycode 224"
			 */
			if ((kdCode >= 112 && kdCode <= 123) || (kdCode >= 91 && kdCode <= 93) || (kdCode >= 9 && kdCode <= 31) || (kdCode < 8 && (which === 0 || which === kdCode)) || kdCode === 144 || kdCode === 145 || kdCode === 45 || kdCode === 224) {
				return true;
			}
			if ((ctrlKey || cmdKey) && kdCode === 65) { /** if select all (a=65)*/
				return true;
			}
			if ((ctrlKey || cmdKey) && (kdCode === 67 || kdCode === 86 || kdCode === 88)) { /** if copy (c=67) paste (v=86) or cut (x=88) */
				if (e.type === 'keydown') {
					this.expandSelectionOnSign();
				}
				if (kdCode === 86 || kdCode === 45) { /** try to prevent wrong paste */
					if (e.type === 'keydown' || e.type === 'keypress') {
						if (this.valuePartsBeforePaste === undefined) {
							this.valuePartsBeforePaste = this.getBeforeAfter();
						}
					} else {
						this.checkPaste();
					}
				}
				return e.type === 'keydown' || e.type === 'keypress' || kdCode === 67;
			}
			if (ctrlKey || cmdKey) {
				return true;
			}
			if (kdCode === 37 || kdCode === 39) { /** jump over thousand separator */
				var aSep = this.settingsClone.aSep,
					start = this.selection.start,
					value = this.that.value;
				if (e.type === 'keydown' && aSep && !this.shiftKey) {
					if (kdCode === 37 && value.charAt(start - 2) === aSep) {
						this.setPosition(start - 1);
					} else if (kdCode === 39 && value.charAt(start + 1) === aSep) {
						this.setPosition(start + 1);
					}
				}
				return true;
			}
			if (kdCode >= 34 && kdCode <= 40) {
				return true;
			}
			return false;
		},

		/**
		 * process deletion of characters
		 * returns true if processing performed
		 */
		processAllways: function () {
			var parts; /** process backspace or delete */
			if (this.kdCode === 8 || this.kdCode === 46) {
				if (!this.selection.length) {
					parts = this.getBeforeAfterStriped();
					if (this.kdCode === 8) {
						parts[0] = parts[0].substring(0, parts[0].length - 1);
					} else {
						parts[1] = parts[1].substring(1, parts[1].length);
					}
					this.setValueParts(parts[0], parts[1]);
				} else {
					this.expandSelectionOnSign(false);
					parts = this.getBeforeAfterStriped();
					this.setValueParts(parts[0], parts[1]);
				}
				return true;
			}
			return false;
		},

		/**
		 * process insertion of characters
		 * returns true if processing performed
		 */
		processKeypress: function () {
			var settingsClone = this.settingsClone,
				cCode = String.fromCharCode(this.which),
				parts = this.getBeforeAfterStriped(),
				left = parts[0],
				right = parts[1]; /** start rules when the decimal character key is pressed */
			/** always use numeric pad dot to insert decimal separator */
			if (cCode === settingsClone.aDec || (settingsClone.altDec && cCode === settingsClone.altDec) || ((cCode === '.' || cCode === ',') && this.kdCode === 110)) { /** do not allow decimal character if no decimal part allowed */
				if (!settingsClone.mDec || !settingsClone.aDec) {
					return true;
				} /** do not allow decimal character before aNeg character */
				if (settingsClone.aNeg && right.indexOf(settingsClone.aNeg) > -1) {
					return true;
				} /** do not allow decimal character if other decimal character present */
				if (left.indexOf(settingsClone.aDec) > -1) {
					return true;
				}
				if (right.indexOf(settingsClone.aDec) > 0) {
					return true;
				}
				if (right.indexOf(settingsClone.aDec) === 0) {
					right = right.substr(1);
				}
				this.setValueParts(left + settingsClone.aDec, right);
				return true;
			}
			/**
			 * start rule on negative sign & prevent minus if not allowed
			 */
			if (cCode === '-' || cCode === '+') {
				if (!settingsClone.aNeg) {
					return true;
				} /** caret is always after minus */
				if (left === '' && right.indexOf(settingsClone.aNeg) > -1) {
					left = settingsClone.aNeg;
					right = right.substring(1, right.length);
				} /** change sign of number, remove part if should */
				if (left.charAt(0) === settingsClone.aNeg) {
					left = left.substring(1, left.length);
				} else {
					left = (cCode === '-') ? settingsClone.aNeg + left : left;
				}
				this.setValueParts(left, right);
				return true;
			} /** digits */
			if (cCode >= '0' && cCode <= '9') { /** if try to insert digit before minus */
				if (settingsClone.aNeg && left === '' && right.indexOf(settingsClone.aNeg) > -1) {
					left = settingsClone.aNeg;
					right = right.substring(1, right.length);
				}
				if (settingsClone.vMax <= 0 && settingsClone.vMin < settingsClone.vMax && this.value.indexOf(settingsClone.aNeg) === -1 && cCode !== '0') {
					left = settingsClone.aNeg + left;
				}
				this.setValueParts(left + cCode, right);
				return true;
			} /** prevent any other character */
			return true;
		},

		/**
		 * formatting of just processed value with keeping of cursor position
		 */
		formatQuick: function () {
			var settingsClone = this.settingsClone,
				parts = this.getBeforeAfterStriped(),
				leftLength = this.value;
			if ((settingsClone.aSep === '' || (settingsClone.aSep !== '' && leftLength.indexOf(settingsClone.aSep) === -1)) && (settingsClone.aSign === '' || (settingsClone.aSign !== '' && leftLength.indexOf(settingsClone.aSign) === -1))) {
				var subParts = [],
					nSign = '';
				subParts = leftLength.split(settingsClone.aDec);
				if (subParts[0].indexOf('-') > -1) {
					nSign = '-';
					subParts[0] = subParts[0].replace('-', '');
					parts[0] = parts[0].replace('-', '');
				}
				if (subParts[0].length > settingsClone.mInt && parts[0].charAt(0) === '0') { /** strip leading zero if need */
					parts[0] = parts[0].slice(1);
				}
				parts[0] = nSign + parts[0];
			}
			var value = autoGroup(this.value, this.settingsClone),
				position = value.length;
			if (value) {
				/** prepare regexp which searches for cursor position from unformatted left part */
				var left_ar = parts[0].split(''),
					i = 0;
				for (i; i < left_ar.length; i += 1) { /** thanks Peter Kovari */
					if (!left_ar[i].match('\\d')) {
						left_ar[i] = '\\' + left_ar[i];
					}
				}
				var leftReg = new RegExp('^.*?' + left_ar.join('.*?'));
				/** search cursor position in formatted value */
				var newLeft = value.match(leftReg);
				if (newLeft) {
					position = newLeft[0].length;
					/** if we are just before sign which is in prefix position */
					if (((position === 0 && value.charAt(0) !== settingsClone.aNeg) || (position === 1 && value.charAt(0) === settingsClone.aNeg)) && settingsClone.aSign && settingsClone.pSign === 'p') {
						/** place caret after prefix sign */
						position = this.settingsClone.aSign.length + (value.charAt(0) === '-' ? 1 : 0);
					}
				} else if (settingsClone.aSign && settingsClone.pSign === 's') {
					/** if we could not find a place for cursor and have a sign as a suffix */
					/** place carret before suffix currency sign */
					position -= settingsClone.aSign.length;
				}
			}
			if (this.that.value !== value) {
				this.that.value = value;
				this.setPosition(position);
			}
			this.formatted = true;
		}
	};

	/**
	 * thanks to Anthony & Evan C
	 */

	function autoGet(obj) {
		if (typeof obj === 'string') {
			obj = obj.replace(/\[/g, "\\[").replace(/\]/g, "\\]");
			obj = '#' + obj.replace(/(:|\.)/g, '\\$1');
			/** obj = '#' + obj.replace(/([;&,\.\+\*\~':"\!\^#$%@\[\]\(\)=>\|])/g, '\\$1'); */
			/** possible modification to replace the above 2 lines */
		}
		return $(obj);
	}

	/**
	 * function to attach data to the element
	 * and imitate the holder
	 */

	function getHolder($that, settings, update) {
		var data = $that.data('autoNumeric');
		if (!data) {
			data = {};
			$that.data('autoNumeric', data);
		}
		var holder = data.holder;
		if ((holder === undefined && settings) || update) {
			holder = new AutoNumericHolder($that.get(0), settings);
			data.holder = holder;
		}
		return holder;
	}

	var methods = {

		/**
		 * Method to initiate autoNumeric and attached the settings (default and options passed as a parameter
		 * $(someSelector).autoNumeric('init'); // initiate autoNumeric with defaults
		 * $(someSelector).autoNumeric('init', {option}); // initiate autoNumeric with options
		 * $(someSelector).autoNumeric(); // initiate autoNumeric with defaults
		 * $(someSelector).autoNumeric({option}); // initiate autoNumeric with options
		 * options passes as a parameter example '{aSep: '.', aDec: ',', aSign: '€ '}
		 */
		init: function (options) {
			return this.each(function () {
				var $this = $(this),
					settings = $this.data('autoNumeric'),
					/** attempt to grab 'autoNumeric' settings, if they don't exist returns "undefined". */
					tagData = $this.data(),
					/** attempt to grab HTML5 data, if they don't exist we'll get "undefined".*/
					$input = $this.is('input[type=text], input[type=hidden], input[type=tel], input:not([type])');
				if (typeof settings !== 'object') { /** If we couldn't grab settings, create them from defaults and passed options. */
					settings = $.extend({}, $.fn.autoNumeric.defaults, tagData, options, {
						aNum: '0123456789',
						hasFocus: false,
						removeBrackets: false,
						runOnce: false,
						tagList: ['b', 'caption', 'cite', 'code', 'dd', 'del', 'div', 'dfn', 'dt', 'em', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'ins', 'kdb', 'label', 'li', 'output', 'p', 'q', 's', 'sample', 'span', 'strong', 'td', 'th', 'u', 'var']
					}); /** Merge defaults, tagData and options */
					if (settings.aDec === settings.aSep) {
						$.error("autoNumeric will not function properly when the decimal character aDec: '" + settings.aDec + "' and thousand separator aSep: '" + settings.aSep + "' are the same character");
					}
					$this.data('autoNumeric', settings); /** Save our new settings */
				} else {
					return this;
				}
				var holder = getHolder($this, settings);
				if (!$input && $this.prop('tagName').toLowerCase() === 'input') { /** checks for non-supported input types */
					$.error('The input type "' + $this.prop('type') + '" is not supported by autoNumeric()');

				}
				if ($.inArray($this.prop('tagName').toLowerCase(), settings.tagList) === -1 && $this.prop('tagName').toLowerCase() !== 'input') {
					$.error("The <" + $this.prop('tagName').toLowerCase() + "> is not supported by autoNumeric()");

				}
				if (settings.runOnce === false && settings.aForm) { /** routine to format default value on page load */
					if ($input) {
						var setValue = true;
						if ($this[0].value === '' && settings.wEmpty === 'empty') {
							$this[0].value = '';
							setValue = false;
						}
						if ($this[0].value === '' && settings.wEmpty === 'sign') {
							$this[0].value = settings.aSign;
							setValue = false;
						}
						/** checks for page reload from back button
						 * also checks for ASP.net form post back
						 * the following HTML data attribute is REQUIRED (data-an-default="same value as the value attribute")
						 * example: <asp:TextBox runat="server" id="someID" value="1234.56" data-an-default="1234.56">
						 */
						if (setValue && $this.val() !== '' && ((settings.anDefault === null && $this[0].value === $this.prop('defaultValue')) || (settings.anDefault !== null && settings.anDefault.toString() === $this.val()))) {
							$this.autoNumeric('set', $this.val());
						}
					}
					if ($.inArray($this.prop('tagName').toLowerCase(), settings.tagList) !== -1 && $this.text() !== '') {
						$this.autoNumeric('set', $this.text().trim());
					}
				}
				settings.runOnce = true;
				if ($this.is('input[type=text], input[type=hidden], input[type=tel], input:not([type])')) { /**added hidden type */
					$this.on('keydown.autoNumeric', function (e) {
						holder = getHolder($this);
						if (holder.settings.aDec === holder.settings.aSep) {
							$.error("autoNumeric will not function properly when the decimal character aDec: '" + holder.settings.aDec + "' and thousand separator aSep: '" + holder.settings.aSep + "' are the same character");
						}
						if (holder.that.readOnly) {
							holder.processed = true;
							return true;
						}
						/** The below streamed code / comment allows the "enter" keydown to throw a change() event */
						/** if (e.keyCode === 13 && holder.inVal !== $this.val()){
							$this.change();
							holder.inVal = $this.val();
						}*/
						holder.init(e);
						if (holder.skipAllways(e)) {
							holder.processed = true;
							return true;
						}
						if (holder.processAllways()) {
							holder.processed = true;
							holder.formatQuick();
							e.preventDefault();
							return false;
						}
						holder.formatted = false;
						return true;
					});
					$this.on('keypress.autoNumeric', function (e) {
						holder = getHolder($this);
						var processed = holder.processed;
						holder.init(e);
						if (holder.skipAllways(e)) {
							return true;
						}
						if (processed) {
							e.preventDefault();
							return false;
						}
						if (holder.processAllways() || holder.processKeypress()) {
							holder.formatQuick();
							e.preventDefault();
							return false;
						}
						holder.formatted = false;
					});
					$this.on('keyup.autoNumeric', function (e) {
						holder = getHolder($this);
						holder.init(e);
						var skip = holder.skipAllways(e),
							tab = holder.kdCode;
						holder.kdCode = 0;
						delete holder.valuePartsBeforePaste;
						if ($this[0].value === holder.settings.aSign) { /** added to properly place the caret when only the currency is present */
							if (holder.settings.pSign === 's') {
								setElementSelection(this, 0, 0);
							} else {
								setElementSelection(this, holder.settings.aSign.length, holder.settings.aSign.length);
							}
						} else if (tab === 9) {
							setElementSelection(this, 0, $this.val().length);
						}
						if (skip) {
							return true;
						}
						if (this.value === '') {
							return true;
						}
						if (!holder.formatted) {
							holder.formatQuick();
						}
					});
					$this.on('focusin.autoNumeric', function () {
						holder = getHolder($this);
						var $settings = holder.settingsClone;
						$settings.hasFocus = true;
						if ($settings.nBracket !== null) {
							var checkVal = $this.val();
							$this.val(negativeBracket(checkVal, $settings));
						}
						holder.inVal = $this.val();
						var onEmpty = checkEmpty(holder.inVal, $settings, true);
						if (onEmpty !== null && onEmpty !== '') {
							$this.val(onEmpty);
						}
					});
					$this.on('focusout.autoNumeric', function () {
						holder = getHolder($this);
						var $settings = holder.settingsClone,
							value = $this.val(),
							origValue = value;
						$settings.hasFocus = false;
						var strip_zero = ''; /** added to control leading zero */
						if ($settings.lZero === 'allow') { /** added to control leading zero */
							$settings.allowLeading = false;
							strip_zero = 'leading';
						}
						if (value !== '') {
							value = autoStrip(value, $settings, strip_zero);
							if (checkEmpty(value, $settings) === null && autoCheck(value, $settings, $this[0])) {
								value = fixNumber(value, $settings.aDec, $settings.aNeg);
								value = autoRound(value, $settings);
								value = presentNumber(value, $settings.aDec, $settings.aNeg);
							} else {
								value = '';
							}
						}
						var groupedValue = checkEmpty(value, $settings, false);
						if (groupedValue === null) {
							groupedValue = autoGroup(value, $settings);
						}
						if (groupedValue !== holder.inVal || groupedValue !== origValue) {
							$this.val(groupedValue);
							$this.change();
							delete holder.inVal;
						}
					});
				}
			});
		},

		/**
		 * method to remove settings and stop autoNumeric() - does not remove the formatting
		 * $(someSelector).autoNumeric('destroy'); // destroy autoNumeric
		 * no parameters accepted
		 */
		destroy: function () {
			return $(this).each(function () {
				var $this = $(this);
				$this.removeData('autoNumeric');
				$this.off('.autoNumeric');
			});
		},

		/**
		 * method to update settings - can be call as many times
		 * $(someSelector).autoNumeric('update', {options}); // updates the settings
		 * options passes as a parameter example '{aSep: '.', aDec: ',', aSign: '€ '}
		 */
		update: function (options) {
			return $(this).each(function () {
				var $this = autoGet($(this)),
					settings = $this.data('autoNumeric');
				if (typeof settings !== 'object') {
					$.error("You must initialize autoNumeric('init', {options}) prior to calling the 'update' method");
				}
				var strip = $this.autoNumeric('get');
				settings = $.extend(settings, options);
				getHolder($this, settings, true);
				if (settings.aDec === settings.aSep) {
					$.error("autoNumeric will not function properly when the decimal character aDec: '" + settings.aDec + "' and thousand separator aSep: '" + settings.aSep + "' are the same character");
				}
				$this.data('autoNumeric', settings);
				if ($this.val() !== '' || $this.text() !== '') {
					return $this.autoNumeric('set', strip);
				}
				return;
			});
		},

		/**
		 * method to format value sent as a parameter ""
		 * $(someSelector).autoNumeric('set', 'value'}); // formats the value being passed
		 * value passed as a string - can be a integer '1234' or double '1234.56789'
		 * must contain only numbers and one decimal (period) character
		 */
		set: function (valueIn) {
			if (valueIn === null || isNaN(valueIn)) {
				return;
			}
			return $(this).each(function () {
				var $this = autoGet($(this)),
					settings = $this.data('autoNumeric'),
					value = valueIn.toString(),
					testValue = valueIn.toString(),
					$input = $this.is('input[type=text], input[type=hidden], input[type=tel], input:not([type])');
				if (typeof settings !== 'object') {
					$.error("You must initialize autoNumeric('init', {options}) prior to calling the 'set' method");
				}
				/** allows locale decimal separator to be a comma */
				if ((testValue === $this.attr('value') || testValue === $this.text()) && settings.runOnce === false) {
					value = value.replace(',', '.');
				}
				if (!$.isNumeric(+value)) {
					$.error("The value (" + value + ") being 'set' is not numeric and has caused a error to be thrown");
				}
				value = checkValue(value, settings);
				settings.setEvent = true;
				value.toString();
				if (value !== '') {
					value = autoRound(value, settings);
				}
				value = presentNumber(value, settings.aDec, settings.aNeg);
				if (!autoCheck(value, settings)) {
					value = autoRound('', settings);
				}
				value = autoGroup(value, settings);
				if ($input) {
					return $this.val(value);
				}
				if ($.inArray($this.prop('tagName').toLowerCase(), settings.tagList) !== -1) {
					return $this.text(value);
				}
				return false;
			});
		},

		/**
		 * method to get the unformatted that accepts up to one parameter
		 * $(someSelector).autoNumeric('get'); no parameters accepted
		 * values returned as ISO numeric string "1234.56" where the decimal character is a period
		 * only the first element in the selector is returned
		 */
		get: function () {
			var $this = autoGet($(this)),
				settings = $this.data('autoNumeric');
			if (typeof settings !== 'object') {
				$.error("You must initialize autoNumeric('init', {options}) prior to calling the 'get' method");
			}
			var getValue = '';
			/** determine the element type then use .eq(0) selector to grab the value of the first element in selector */
			if ($this.is('input[type=text], input[type=hidden], input[type=tel], input:not([type])')) { /**added hidden type */
				getValue = $this.eq(0).val();
			} else if ($.inArray($this.prop('tagName').toLowerCase(), settings.tagList) !== -1) {
				getValue = $this.eq(0).text();
			} else {
				$.error("The <" + $this.prop('tagName').toLowerCase() + "> is not supported by autoNumeric()");
			}
			if ((getValue === '' && settings.wEmpty === 'empty') || (getValue === settings.aSign && (settings.wEmpty === 'sign' || settings.wEmpty === 'empty'))) {
				return '';
			}
			if (getValue !== '' && settings.nBracket !== null) {
				settings.removeBrackets = true;
				getValue = negativeBracket(getValue, settings);
				settings.removeBrackets = false;
			}
			if (settings.runOnce || settings.aForm === false) {
				getValue = autoStrip(getValue, settings);
			}
			getValue = fixNumber(getValue, settings.aDec, settings.aNeg);
			if (+getValue === 0 && settings.lZero !== 'keep') {
				getValue = '0';
			}
			if (settings.lZero === 'keep') {
				return getValue;
			}
			getValue = checkValue(getValue, settings);
			return getValue; /** returned Numeric String */
		},

		/**
		 * The 'getString' method used jQuerys .serialize() method that creates a text string in standard URL-encoded notation
		 * it then loops through the string and un-formats the inputs with autoNumeric
		 * $(someSelector).autoNumeric('getString'); no parameter accepted
		 * values returned as ISO numeric string "1234.56" where the decimal character is a period
		 */
		getString: function () {
			var isAutoNumeric = false,
				$this = autoGet($(this)),
				formFields = $this.serialize(),
				formParts = formFields.split('&'),
				formIndex = $('form').index($this),
				allFormElements = $('form:eq(' + formIndex + ')'),
				aiIndex = [],
				/* all input index */
				scIndex = [],
				/* successful control index */
				rsubmitterTypes = /^(?:submit|button|image|reset|file)$/i,
				/* from jQuery serialize method */
				rsubmittable = /^(?:input|select|textarea|keygen)/i,
				/* from jQuery serialize method */
				rcheckableType = /^(?:checkbox|radio)$/i,
				rnonAutoNumericTypes = /^(?:button|checkbox|color|date|datetime|datetime-local|email|file|image|month|number|password|radio|range|reset|search|submit|time|url|week)/i,
				count = 0;
			/*jslint unparam: true*/
			/* index of successful elements */
			$.each(allFormElements[0], function (i, field) {
				if (field.name !== '' && rsubmittable.test(field.localName) && !rsubmitterTypes.test(field.type) && !field.disabled && (field.checked || !rcheckableType.test(field.type))) {
					scIndex.push(count);
					count = count + 1;
				} else {
					scIndex.push(-1);
				}
			});
			/* index of all inputs tags except checkbox */
			count = 0;
			$.each(allFormElements[0], function (i, field) {
				if (field.localName === 'input' && (field.type === '' || field.type === 'text' || field.type === 'hidden' || field.type === 'tel')) {
					aiIndex.push(count);
					count = count + 1;
				} else {
					aiIndex.push(-1);
					if (field.localName === 'input' && rnonAutoNumericTypes.test(field.type)) {
						count = count + 1;
					}
				}
			});
			$.each(formParts, function (i, miniParts) {
				miniParts = formParts[i].split('=');
				var scElement = $.inArray(i, scIndex);
				if (scElement > -1 && aiIndex[scElement] > -1) {
					var testInput = $('form:eq(' + formIndex + ') input:eq(' + aiIndex[scElement] + ')'),
						settings = testInput.data('autoNumeric');
					if (typeof settings === 'object') {
						if (miniParts[1] !== null) {
							miniParts[1] = $('form:eq(' + formIndex + ') input:eq(' + aiIndex[scElement] + ')').autoNumeric('get').toString();
							formParts[i] = miniParts.join('=');
							isAutoNumeric = true;
						}
					}
				}
			});
			/*jslint unparam: false*/
			if (!isAutoNumeric) {
				$.error("You must initialize autoNumeric('init', {options}) prior to calling the 'getString' method");
			}
			return formParts.join('&');
		},

		/**
		 * The 'getString' method used jQuerys .serializeArray() method that creates array or objects that can be encoded as a JSON string
		 * it then loops through the string and un-formats the inputs with autoNumeric
		 * $(someSelector).autoNumeric('getArray'); no parameter accepted
		 * values returned as ISO numeric string "1234.56" where the decimal character is a period
		 */
		getArray: function () {
			var isAutoNumeric = false,
				$this = autoGet($(this)),
				formFields = $this.serializeArray(),
				formIndex = $('form').index($this),
				allFormElements = $('form:eq(' + formIndex + ')'),
				aiIndex = [],
				/* all input index */
				scIndex = [],
				/* successful control index */
				rsubmitterTypes = /^(?:submit|button|image|reset|file)$/i,
				/* from jQuery serialize method */
				rsubmittable = /^(?:input|select|textarea|keygen)/i,
				/* from jQuery serialize method */
				rcheckableType = /^(?:checkbox|radio)$/i,
				rnonAutoNumericTypes = /^(?:button|checkbox|color|date|datetime|datetime-local|email|file|image|month|number|password|radio|range|reset|search|submit|time|url|week)/i,
				count = 0;
			/*jslint unparam: true*/
			/* index of successful elements */
			$.each(allFormElements[0], function (i, field) {
				if (field.name !== '' && rsubmittable.test(field.localName) && !rsubmitterTypes.test(field.type) && !field.disabled && (field.checked || !rcheckableType.test(field.type))) {
					scIndex.push(count);
					count = count + 1;
				} else {
					scIndex.push(-1);
				}
			});
			/* index of all inputs tags */
			count = 0;
			$.each(allFormElements[0], function (i, field) {
				if (field.localName === 'input' && (field.type === '' || field.type === 'text' || field.type === 'hidden' || field.type === 'tel')) {
					aiIndex.push(count);
					count = count + 1;
				} else {
					aiIndex.push(-1);
					if (field.localName === 'input' && rnonAutoNumericTypes.test(field.type)) {
						count = count + 1;
					}
				}
			});
			$.each(formFields, function (i, field) {
				var scElement = $.inArray(i, scIndex);
				if (scElement > -1 && aiIndex[scElement] > -1) {
					var testInput = $('form:eq(' + formIndex + ') input:eq(' + aiIndex[scElement] + ')'),
						settings = testInput.data('autoNumeric');
					if (typeof settings === 'object') {
						field.value = $('form:eq(' + formIndex + ') input:eq(' + aiIndex[scElement] + ')').autoNumeric('get').toString();
						isAutoNumeric = true;
					}
				}
			});
			/*jslint unparam: false*/
			if (!isAutoNumeric) {
				$.error("None of the successful form inputs are initialized by autoNumeric.");
			}
			return formFields;
		},

		/**
		 * The 'getSteetings returns the object with autoNumeric settings for those who need to look under the hood
		 * $(someSelector).autoNumeric('getSettings'); // no parameters accepted
		 * $(someSelector).autoNumeric('getSettings').aDec; // return the aDec setting as a string - ant valid setting can be used
		 */
		getSettings: function () {
			var $this = autoGet($(this));
			return $this.eq(0).data('autoNumeric');
		}
	};

	/**
	 * autoNumeric function
	 */
	$.fn.autoNumeric = function (method) {
		if (methods[method]) {
			return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
		}
		if (typeof method === 'object' || !method) {
			return methods.init.apply(this, arguments);
		}
		$.error('Method "' + method + '" is not supported by autoNumeric()');
	};

	/**
	 * Defaults are public - these can be overridden by the following:
	 * HTML5 data attributes
	 * Options passed by the 'init' or 'update' methods
	 * Use jQuery's $.extend method - great way to pass ASP.NET current culture settings
	 */
	$.fn.autoNumeric.defaults = {
		/** allowed thousand separator characters
		 * comma = ','
		 * period "full stop" = '.'
		 * apostrophe is escaped = '\''
		 * space = ' '
		 * none = ''
		 * NOTE: do not use numeric characters
		 */
		aSep: ',',
		/** digital grouping for the thousand separator used in Format
		 * dGroup: '2', results in 99,99,99,999 common in India for values less than 1 billion and greater than -1 billion
		 * dGroup: '3', results in 999,999,999 default
		 * dGroup: '4', results in 9999,9999,9999 used in some Asian countries
		 */
		dGroup: '3',
		/** allowed decimal separator characters
		 * period "full stop" = '.'
		 * comma = ','
		 */
		aDec: '.',
		/** allow to declare alternative decimal separator which is automatically replaced by aDec
		 * developed for countries the use a comma ',' as the decimal character
		 * and have keyboards\numeric pads that have a period 'full stop' as the decimal characters (Spain is an example)
		 */
		altDec: null,
		/** allowed currency symbol
		 * Must be in quotes aSign: '$', a space is allowed aSign: '$ '
		 */
		aSign: '',
		/** placement of currency sign
		 * for prefix pSign: 'p',
		 * for suffix pSign: 's',
		 */
		pSign: 'p',
		/** maximum possible value
		 * value must be enclosed in quotes and use the period for the decimal point
		 * value must be larger than vMin
		 */
		vMax: '9999999999999.99',
		/** minimum possible value
		 * value must be enclosed in quotes and use the period for the decimal point
		 * value must be smaller than vMax
		 */
		vMin: '-9999999999999.99',
		/** max number of decimal places = used to override decimal places set by the vMin & vMax values
		 * value must be enclosed in quotes example mDec: '3',
		 * This can also set the value via a call back function mDec: 'css:#
		 */
		mDec: null,
		/** method used for rounding
		 * mRound: 'S', Round-Half-Up Symmetric (default)
		 * mRound: 'A', Round-Half-Up Asymmetric
		 * mRound: 's', Round-Half-Down Symmetric (lower case s)
		 * mRound: 'a', Round-Half-Down Asymmetric (lower case a)
		 * mRound: 'B', Round-Half-Even "Bankers Rounding"
		 * mRound: 'U', Round Up "Round-Away-From-Zero"
		 * mRound: 'D', Round Down "Round-Toward-Zero" - same as truncate
		 * mRound: 'C', Round to Ceiling "Toward Positive Infinity"
		 * mRound: 'F', Round to Floor "Toward Negative Infinity"
		 */
		mRound: 'S',
		/** controls decimal padding
		 * aPad: true - always Pad decimals with zeros
		 * aPad: false - does not pad with zeros.
		 * aPad: `some number` - pad decimals with zero to number different from mDec
		 * thanks to Jonas Johansson for the suggestion
		 */
		aPad: true,
		/** places brackets on negative value -$ 999.99 to (999.99)
		 * visible only when the field does NOT have focus the left and right symbols should be enclosed in quotes and seperated by a comma
		 * nBracket: null, nBracket: '(,)', nBracket: '[,]', nBracket: '<,>' or nBracket: '{,}'
		 */
		nBracket: null,
		/** Displayed on empty string
		 * wEmpty: 'empty', - input can be blank
		 * wEmpty: 'zero', - displays zero
		 * wEmpty: 'sign', - displays the currency sign
		 */
		wEmpty: 'empty',
		/** controls leading zero behavior
		 * lZero: 'allow', - allows leading zeros to be entered. Zeros will be truncated when entering additional digits. On focusout zeros will be deleted.
		 * lZero: 'deny', - allows only one leading zero on values less than one
		 * lZero: 'keep', - allows leading zeros to be entered. on fousout zeros will be retained.
		 */
		lZero: 'allow',
		/** determine if the select all keyboard command will select
		 * the complete input text or only the input numeric value
		 * if the currency symbol is between the numeric value and the negative sign only the numeric value will sellected
		 */
		sNumber: true,
		/** determine if the default value will be formatted on page ready.
		 * true = automatically formats the default value on page ready
		 * false = will not format the default value
		 */
		aForm: true,
		/** helper option for ASP.NET postback
		 * should be the value of the unformatted default value
		 * examples:
		 * no default value='' {anDefault: ''}
		 * value=1234.56 {anDefault: '1234.56'}
		 */
		anDefault: null
	};
}));

// ***************************************************
// JQUERY DIALOG ADDONS
// ***************************************************
/*
 * jQuery UI dialogOptions v1.0
 * @desc extending jQuery Ui Dialog - Responsive, click outside, class handling
 * @author Jason Day
 *
 * Dependencies:
 *		jQuery: http://jquery.com/
 *		jQuery UI: http://jqueryui.com/
 *		Modernizr: http://modernizr.com/Modernizr adds feature detection. dialogOptions optionally checks for html.touch for scrolling
 *
 * MIT license:
 *  http://www.opensource.org/licenses/mit-license.php
 *
 * (c) Jason Day 2014
 *
 * New Options:
 *  clickOut: true  // closes dialog when clicked outside
 *  responsive: true// fluid width & height based on viewport
 *  // true: always responsive
 *  // false: never responsive
 *  // "touch": only responsive on touch device
 *  scaleH: 0.8 // responsive scale height percentage, 0.8 = 80% of viewport
 *  scaleW: 0.8 // responsive scale width percentage, 0.8 = 80% of viewport
 *  showTitleBar: true  // false: hide titlebar
 *  showCloseButton: true   // false: hide close button
 *
 * Added functionality:
 *  add & remove dialogClass to .ui-widget-overlay for scoping styles
 *	patch for: http://bugs.jqueryui.com/ticket/4671
 *	recenter dialog - ajax loaded content
 */

// add new options with default values
$.ui.dialog.prototype.options.clickOut = false;
$.ui.dialog.prototype.options.responsive = true;
$.ui.dialog.prototype.options.scaleH = 0.9;
$.ui.dialog.prototype.options.scaleW = 0.9;
$.ui.dialog.prototype.options.showTitleBar = true;
$.ui.dialog.prototype.options.showCloseButton = false;

// extend _init
var _init = $.ui.dialog.prototype._init;
$.ui.dialog.prototype._init = function () {
	var self = this;

	// apply original arguments
	_init.apply(this, arguments);

	//patch
	if( $.ui && $.ui.dialog && $.ui.dialog.overlay )
	{
		$.ui.dialog.overlay.events = $.map('focus,keydown,keypress'.split(','), function (event) {
			return event + '.dialog-overlay';
		}).join(' ');
	}
};
// end _init

// extend open function
var _open = $.ui.dialog.prototype.open;
$.ui.dialog.prototype.open = function () {
	var self = this;
	// apply original arguments
	_open.apply(this, arguments);
	// get dialog original size on open
	var oHeight = self.element.parent().outerHeight(), oWidth = self.element.parent().outerWidth(), isTouch = $("html").hasClass("touch");

	// responsive width & height
	var dialog_resize = function () {
		// check if responsive
		// dependent on modernizr for device detection / html.touch
		if( self.element.hasClass('ui-dialog-content') && self.element.dialog('isOpen') )
		{
			if( self.options.responsive === true || (self.options.responsive === "touch" && isTouch) )
			{
				var elem = self.element, wHeight = $(window).height(), wWidth = $(window).width(), dHeight = elem.parent().outerHeight(), dWidth = elem.parent().outerWidth(), setHeight = Math.min(wHeight * self.options.scaleH, oHeight), setWidth = Math.min(wWidth * self.options.scaleW, oWidth);

				// check & set height
				if( (oHeight + 100) > wHeight || elem.hasClass("resizedH") )
				{
					elem.dialog("option", "height", setHeight).parent().css("max-height", setHeight);
					elem.addClass("resizedH");
				}

				// check & set width
				if( (oWidth + 100) > wWidth || elem.hasClass("resizedW") )
				{
					elem.dialog("option", "width", setWidth).parent().css("max-width", setWidth);
					elem.addClass("resizedW");
				}

				// only recenter & add overflow if dialog has been resized
				if( elem.hasClass("resizedH") || elem.hasClass("resizedW") )
				{
					elem.css("overflow", "auto");
				}

				elem.dialog("option", "position", { my: 'center', at: 'center', of: window }); // Centramos siempre
			}

			// add webkit scrolling to all dialogs for touch devices
			if( isTouch )
			{
				elem.css("-webkit-overflow-scrolling", "touch");
			}
		}
	};

	// call resize()
	dialog_resize();

	// resize on window resize
	if( !self.element.hasClass("rs_trigger") )
	{
		self.element.addClass("rs_trigger");
		$(window).on('resize', function () { dialog_resize(); });
		$(window).on('scroll', function () { dialog_resize(); });

		// resize on orientation change
		if( window.addEventListener )
		{  // Add extra condition because IE8 doesn't support addEventListener (or orientationchange)
			window.addEventListener("orientationchange", function () {
				dialog_resize();
			});
		}
	}

	// hide titlebar
	if( !self.options.showTitleBar )
	{
		self.uiDialogTitlebar.css({
			"height": 0,
			"padding": 0,
			"background": "none",
			"border": 0
		});

		self.uiDialogTitlebar.find(".ui-dialog-title").css("display", "none");
	}

	//hide close button
	if( !self.options.showCloseButton )
	{
		self.uiDialogTitlebar.find(".ui-dialog-titlebar-close").css("display", "none");
	}

	// close on clickOut
	if( self.options.clickOut && !self.options.modal )
	{
		// use transparent div - simplest approach (rework)
		$('<div id="dialog-overlay"></div>').insertBefore(self.element.parent());
		$('#dialog-overlay').css({
			"position": "fixed",
			"top": 0,
			"right": 0,
			"bottom": 0,
			"left": 0,
			"background-color": "transparent"
		});

		$('#dialog-overlay').click(function (e) {
			e.preventDefault();
			e.stopPropagation();
			self.close();
		});

		// else close on modal click
	}
	else if( self.options.clickOut && self.options.modal )
	{
		$('.ui-widget-overlay').click(function (e) {
			self.close();
		});
	}

	// add dialogClass to overlay
	if( self.options.dialogClass )
	{
		$('.ui-widget-overlay').addClass(self.options.dialogClass);
	}
};
//end open

// extend close function
var _close = $.ui.dialog.prototype.close;
$.ui.dialog.prototype.close = function () {
	var self = this;
	// apply original arguments
	_close.apply(this, arguments);

	// remove dialogClass to overlay
	if( self.options.dialogClass )
	{
		$('.ui-widget-overlay').removeClass(self.options.dialogClass);
	}

	//remove clickOut overlay
	if( $("#dialog-overlay").length )
	{
		$("#dialog-overlay").remove();
	}
};
//end close

/*****************************************
 * SIDENAV
 *****************************************/

function openNav()
{
	$('#mySidenav').css('width','300px');
	$('#navOverlay').show();
}

/* Set the width of the side navigation to 0 */
function closeNav()
{
	$('#mySidenav').css('width','0px');
	$('#navOverlay').hide();
}

