
$(document).ready(function()
{

	var buttons = {};
	buttons['Zamknij'] = function() {
							jQuery(this).dialog('close');						
						};
	
	$("body").append('<div id=stdMsg name="standardMessage" style="display:none"></div>');
	
	$("#stdMsg").dialog({
					bgiframe: true,
					autoOpen: false,
					height: 300,
					width: 580,
					modal: true,
					draggable: false,
					resizable: false,
					zIndex: 9999,
					buttons: buttons
	});		
	
	heldeskForm();
	$('#helpdesk-menu').click(function(){$("#stdMsgForm").dialog('open')});
});


function checkRegexp( o, regexp, n ) {
			if(o.val().length!=9)
			{
				o.addClass( "ui-state-error" );
				updateTips( n )
				return false;
			}
			
			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass( "ui-state-error" );
				updateTips( n )
				return false;
			} else {
				return true;
			}
}

function updateTips( t ) {
			$( ".validateTips" )
				.text( t )
				.addClass( "ui-state-highlight" );
			setTimeout(function() {
				$( ".validateTips" ).removeClass( "ui-state-highlight", 1500 );
				setTimeout(function() {
					$( ".validateTips" ).html( "&nbsp;" );
				}, 1500);
			}, 500 );
		}


function submitHelpdesk()
{
	if(checkRegexp( jQuery("#helpdesk-code"), /^[0-9]{9}$/i, "Kod musi zawierać dokładnie dziewięć cyfr" ))
	{
								var helpdeskWin=window.open('','_heldesk_win');
								helpdeskWin.location='https://get.netviewer.com/support/join.php?sinr='+jQuery("#helpdesk-code").val()+'&sipw=nv64';
								jQuery("#helpdesk-code").val('');
								$("#stdMsgForm").dialog('close');
	}								
	return false;
}
function heldeskForm()
{
	var fbuttons = {};
	fbuttons['Zamknij'] = function() {
							jQuery(this).dialog('close');						
						};
	
	fbuttons['Połącz'] = function() {
							submitHelpdesk();
						};
						
	$("body").append('<div id="stdMsgForm" name="standardMessageForm" style="display:none"><p class="validateTips">&nbsp;</p><form onsubmit="submitHelpdesk(); return false;" action="javascript:void(0);"><fieldset>Wpisz 9-ciocyfrowy kod: <input name=code type=text id=helpdesk-code class="text ui-widget-content ui-corner-all" ></form></div>');
	
	$("#stdMsgForm").dialog({
					bgiframe: true,
					autoOpen: false,
					height: 180,
					width: 420,
					modal: true,
					title: 'Wsparcie techniczne',
					draggable: false,
					resizable: false,
					zIndex: 9999,
					buttons: fbuttons
	});	
}

function infoMessage(msg, w, h)
{
	if(h)
		$("#stdMsg").dialog('option', 'height', h);
	else
		$("#stdMsg").dialog('option', 'height', 200);
	
	if(w)
		$("#stdMsg").dialog('option', 'width', w);
	else
		$("#stdMsg").dialog('option', 'width', 350);
	
	$("#stdMsg").html('<div class="ui-state-highlight" style="width:100%;height:80%;text-align:center;padding-top:10px">'+msg+'</div>');			
	$("#stdMsg").dialog('open');
}

function errorMessage(msg, w, h)
{
	if(h)
		$("#stdMsg").dialog('option', 'height', h);
	else
		$("#stdMsg").dialog('option', 'height', 200);
	
	if(w)
		$("#stdMsg").dialog('option', 'width', w);
	else
		$("#stdMsg").dialog('option', 'width', 350);
	
	$("#stdMsg").html('<div class="ui-state-error" style="width:100%;height:80%;text-align:center;padding-top:10px">'+msg+'</div>');			
	$("#stdMsg").dialog('open');
}

function changeStatus(from)
{
	$.ajax(
		{type:"GET",
 		url:"/?/user/Status/changeWorkStatus&ajax=1&from="+from,
	 	success: changeCallback,
	 	dataType: "json"
	});
	showAjaxLoader();
}

function endDepartmentMonth(month)
{
	$.ajax(
		{type:"GET",
 		url:"/?/user/Status/endDepartmentMonth&ajax=1&date="+month,
	 	success: changeCallback,
	 	dataType: "json"
	});
	showAjaxLoader();
}

function changeCallback(data)
{
	hideAjaxLoader();
	if(data.status)
	{
		infoMessage(data.msg);
		$("#search").html(data.statusBox);	
	}
	else if(data.err)
	{
		errorMessage(data.err, 550, 400);
	}
}


function endMonthCallback(data)
{
	hideAjaxLoader();
	if(data.status=='err')
	{
		errorMessage(data.err);
		$("#search").html(data.statusBox);	
	}
	else
	{
	}
}


function priceFormat(cellvalue, options, rowObject)
{
	var x = (cellvalue * 1000);
	var rest = x % 1000;
	var main = (x - rest)/1000;
	rest = Math.round(rest/10);
	if(rest < 10)
		rest = '0' + rest;
	return main+","+rest;
}

function priceUnformat(cellvalue, options, rowObject)
{
	return cellvalue.replace(',', '.');
}

function getCsv()
{
	var dataLink = jQuery("[name='standardGrid']").getGridParam('url').replace('dataList', 'csvList');
	window.location.href=dataLink;	
}
