var post_lock_n = 0;

function WaitForPost(wait)
{
	if( wait == true )
	{
		var page = jQuery(".page");
		var top = parseInt(page.height() / 2) - 20;
		var html = "<div id='post_lock' style='padding-top:"+ top +"px;height:"+ page.height() +"px;width:"+ page.width() +"px'><b>Trwa pobieranie danych...</b></div>";
		if( post_lock_n == 0 )	page.append(html);
		post_lock_n++;
	}
	else
	{
		var el = jQuery(".page > div[id=post_lock]");
		post_lock_n--;
		if( post_lock_n == 0 )	el.remove();
	}
}

function AlertBox(w, h, text, refresh)
{
	MessageBox(true, w, h, "<div style='margin:5px; text-align:center'>"+ text +"</div>", refresh);
}

var window_i = 0;
function ShowWindow(w, h, text, refresh)
{
	MessageBox(true, w, h, text, refresh);
}

function MessageBox(show, w, h, text, refresh)
{
	if( refresh == undefined )	refresh = 0;
	if( show == true )
	{
		var page = jQuery(".page");
	
		var left = (parseInt(page.width()) - w) / 2;
		var top = (parseInt(page.height()) - h) / 2;
		
		var html = "";
		html +=		"<div id='msg_box_bg_"+ window_i +"' class='msg_box_bg' style='height:100%;width:100%'></div>";
		html +=		"<div id='msg_box_"+ window_i +"' class='msg_box' style='top:"+ top +"px;left:"+ left +"px;height:"+ h +"px;width:"+ (w + 40) +"px'>";
		html += 		"<div>";
		html +=				"<img class='tl_corner' src='" + base_url + "/public/images/bg_cont_corner_tl.png'>";
		html +=				"<img class='tr_corner' src='" + base_url + "/public/images/bg_cont_corner_tr.png'>";
		html += 		"</div>";
		html += 		"<div style='float:left; height:"+ (h+2) +"px'><div class='left' style='height:"+ (h-7) +"px'></div></div>";
		html += 		"<div class='main_cont' style='float:left; height:"+ h +"px; width:"+ w +"px'>";
		html += 			"<div class='msg_cont'>"+ text +"</div>";
		html += 			"<div class='close_button'><input type='button' value='Zamknij' onClick='MessageBox(false, "+ refresh +")'></div>";
		html += 		"</div>";
		html += 		"<div style='height:"+ (h+13) +"px'><div  class='right' style='height:"+ (h-7) +"px'></div></div>";
		html += 		"<div style='margin-top:-10px'>";
		html +=				"<img class='bl_corner' src='" + base_url + "/public/images/bg_cont_corner_bl.png'>";
		html +=				"<img class='br_corner' src='" + base_url + "/public/images/bg_cont_corner_br.png'>";
		html += 		"</div>";
		html += 	"</div>";
		page.append(html);
		window_i += 1;
	}
	else
	{
		window_i -= 1;
		jQuery("#msg_box_"+ window_i +"").remove();
		jQuery("#msg_box_bg_"+ window_i +"").remove();
		
		if( w != 0 )	{	location.reload(true);	}
	}
}

function ConfirmBox(show, w, h, text, action, refresh)
{
	if( refresh == undefined )	refresh = 0;
	if( show == true )
	{
		var page = jQuery(".page");
	
		var left = (parseInt(page.width()) - w) / 2;
		var top = (parseInt(page.height()) - h) / 2;
		
		var html = "";
		html +=		"<div id='msg_box_bg_"+ window_i +"' class='msg_box_bg' style='height:100%;width:100%'></div>";
		html +=		"<div id='msg_box_"+ window_i +"' class='msg_box' style='top:"+ top +"px;left:"+ left +"px;height:"+ h +"px;width:"+ (w + 40) +"px'>";
		html += 		"<div>";
		html +=				"<img class='tl_corner' src='" + base_url + "/public/images/bg_cont_corner_tl.png'>";
		html +=				"<img class='tr_corner' src='" + base_url + "/public/images/bg_cont_corner_tr.png'>";
		html += 		"</div>";
		html += 		"<div style='float:left; height:"+ (h+2) +"px'><div class='left' style='height:"+ (h-7) +"px'></div></div>";
		html += 		"<div class='main_cont' style='float:left; height:"+ h +"px; width:"+ w +"px'>";
		html += 			"<div class='msg_cont'>"+ text +"</div>";
		html += 			"<center><div class='msg_conf'>";
		html += 				"<div class='cancel_button' style='float:left'><input type='button' value='Anuluj' onClick='ConfirmBox(false, 0)'></div>";
		html += 				"<div class='close_button' style='float:right'><input type='button' value='Potwierdz' onClick=\"ConfirmBox(false, 0);"+ action +"\"></div>";
		html +=					"<div style='clear: both;'></div>";
		html += 			"</div></center>";
		html += 		"</div>";
		html += 		"<div style='height:"+ (h+13) +"px'><div  class='right' style='height:"+ (h-7) +"px'></div></div>";
		html += 		"<div style='margin-top:-10px'>";
		html +=				"<img class='bl_corner' src='" + base_url + "/public/images/bg_cont_corner_bl.png'>";
		html +=				"<img class='br_corner' src='" + base_url + "/public/images/bg_cont_corner_br.png'>";
		html += 		"</div>";
		html += 	"</div>";
		page.append(html);
		window_i += 1;
	}
	else
	{
		window_i -= 1;
		jQuery("#msg_box_"+ window_i +"").remove();
		jQuery("#msg_box_bg_"+ window_i +"").remove();
		
		if( w != 0 )	{	location.reload(true);	}
	}
}

function StingToClient(_str)
{
	var data = _str.split(";");
	var arr = new Array();
	arr['name'] = data[0];
	arr['nip'] = data[1];
	arr['email'] = data[2];
	arr['phone'] = data[3];
	arr['fax'] = data[4];
	arr['addr'] = data[5];
	
	return arr;
}

function FormSelectCid(_form_name)
{
	var form = jQuery("#"+ _form_name);
	form.find("div[id="+ _form_name +"_select_spid] > select").attr("value", "");
	form.submit();
}

function detectBrowserVersion()
{
	var userAgent = navigator.userAgent.toLowerCase();
	$.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase());
	var version = 0;

	// Is this a version of IE?
	if($.browser.msie)
	{
		userAgent = $.browser.version;
		userAgent = userAgent.substring(0,userAgent.indexOf('.'));
		return new Array("ie", userAgent);
	}

	// Is this a version of Mozilla?
	if($.browser.mozilla)
	{
		//Is it Firefox?
		if(navigator.userAgent.toLowerCase().indexOf('firefox') != -1)
		{
			userAgent = userAgent.substring(userAgent.indexOf('firefox/') +8);
			userAgent = userAgent.substring(0,userAgent.indexOf('.'));
		return new Array("firefox", userAgent);
		}
		// If not then it must be another Mozilla
		else
		{
		}
	}

	// Is this a version of Chrome?
	if($.browser.chrome)
	{
		userAgent = userAgent.substring(userAgent.indexOf('chrome/') +7);
		userAgent = userAgent.substring(0,userAgent.indexOf('.'));
		return new Array("chrome", userAgent);
		// If it is chrome then jQuery thinks it's safari so we have to tell it it isn't
		$.browser.safari = false;
	}

	// Is this a version of Opera?
	if($.browser.opera)
	{
		userAgent = userAgent.substring(userAgent.indexOf('version/') +8);
		userAgent = userAgent.substring(0,userAgent.indexOf('.'));
		return new Array("opera", userAgent);
	}
	
	// Is this a version of Safari?
	if($.browser.safari)
	{
		userAgent = userAgent.substring(userAgent.indexOf('safari/') +7);
		userAgent = userAgent.substring(0,userAgent.indexOf('.'));
		return new Array("safari", userAgent);
	}

	return version;
} 
