var baseUrl;

function Tnumeric(objEvent)
{
	isNav = (navigator.appName.indexOf("Netscape") != -1) ? true : false;
	isIE = (navigator.appName.indexOf("Microsoft") != -1) ? true : false;
	if (isIE)
	{
		iKeyCode = objEvent.keyCode;
	}
	else
	{
		iKeyCode = objEvent.which;
	}
	return (((iKeyCode - 48 >= 0) && (iKeyCode - 48 <= 9))
		|| iKeyCode == 13 || iKeyCode == 0 || iKeyCode == 8 ) ? true : false;
}

function initDialog()
{
	$('#dialog').dialog({
		autoOpen: false,
		width: 300
	});
	$('#error-dialog').dialog({
		autoOpen: false,
		width: 300,
		buttons: {
			"Ok": function()
			{
				$(this).dialog("close");
			}
		}

	});
}

function removeRow(id, car)
{

	$('#dialog-text').text("Fjarlægja ökutæki með númerinu " + car + "?");
	$('#dialog').dialog('option', 'buttons', {
		"Fjarlægja": function()
		{
			$(this).dialog("close");
			removeCar(id, car);
		},
		"Hætta við": function()
		{
			$(this).dialog("close");
		}
	});
	$('#dialog').dialog('open');
}

function removeCar(id, car)
{
	$.getJSON(baseUrl + "json/removecar.htm?id=" + id,
		function(data)
		{
			var encoded = $.toJSON(data);
			var status = $.evalJSON(encoded).status;
			var message = $.evalJSON(encoded).message;

			if (status == '0')
			{
				$("#carrow" + id).remove();
			}
			else
			{
				$('#error-dialog-text').html("<p><span class=\"ui-icon ui-icon-alert\" style=\"float: left; margin-right: .3em;\"></span>Ekki tókst að fjarlægja ökutæki með númerinu " + car + ". Villa " + status + ":" + message);
				$('#error-dialog').dialog('open');
			}
		});
}

function removeRowPhone(id, phone)
{

	$('#dialog-text').text("Fjarlægja símanúmerið " + phone + "?");
	$('#dialog').dialog('option', 'buttons', {
		"Fjarlægja": function()
		{
			$(this).dialog("close");
			removePhone(id, phone);
		},
		"Hætta við": function()
		{
			$(this).dialog("close");
		}
	});
	$('#dialog').dialog('open');
}

function removePhone(id, phone)
{
	$.getJSON(baseUrl + "json/removephone.htm?id=" + id,
		function(data)
		{
			var encoded = $.toJSON(data);
			var status = $.evalJSON(encoded).status;
			var message = $.evalJSON(encoded).message;

			if (status == '0')
			{
				$("#phonerow" + id).remove();
			}
			else
			{
				$('#error-dialog-text').html("<p><span class=\"ui-icon ui-icon-alert\" style=\"float: left; margin-right: .3em;\"></span>Ekki tókst að fjarlægja símanúmerið " + phone + ". Villa " + status + ":" + message);
				$('#error-dialog').dialog('open');
			}
		});
}

function submitenter(myfield, e)
{
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;

	if (keycode == 13)
	{
		myfield.form.submit();
		return false;
	}
	else
		return true;
}

function initializeDatePickers()
{
	$("#start-date").datepicker({
												showOn: 'button',
												buttonImage: '../img/calendar.gif',
												buttonImageOnly: true,
										    dateFormat: 'dd/mm/yy'
											});
	$("#to-date").datepicker({
												showOn: 'button',
												buttonImage: '../img/calendar.gif',
												buttonImageOnly: true,
												dateFormat: 'dd/mm/yy'
											});	
}

function sendInvoice(id)
{
  $("#spinner").dialog({
            height: 140,
            modal: true
          });
  $.getJSON(baseUrl + "json/sendinvoice.htm?id=" + id,
    function(data)
    {
      $("#spinner").hide();
    });
}
