/*
 * @autor Łukasz Sokół
 */

var box = null;



function formsubmit(formName)
{
	document.getElementById('imperial_form_'+formName).submit();
}

function reload()
{
	window.location.href = window.location.href;
}


$(document).ready(
function()
{
	var backColor = null;
	$('.gridTable tr:gt(0)').hover(
		function(){
			backColor = $(this).css('backgroundColor');
			
			$(this).css({
				'backgroundColor':'#EFEFEF'
			});
		},
		function(){
			$(this).css({
				'backgroundColor':backColor
			});
		}
	);
});


function mBoxAlert(title, alertText, type , callbackFun)
{
	
	if(typeof(type) == 'undefined')
	{
		type = 'warning';
	}
	
	switch(type)
	{
		case 'error':
			var spanIco = '<span class="titleIcoError">&nbsp;</span>';
			var ico = '<img class="messIco" src="gfx/mbox/error.gif" alt="">';
			break;
		case 'info':
			var spanIco = '<span class="titleIcoInfo">&nbsp;</span>';
			var ico = '<img class="messIco" src="gfx/mbox/info.gif" alt="">';
			break;
		default:
			var spanIco = '<span class="titleIcoWarning">&nbsp;</span>';
			var ico = '<img class="messIco" src="gfx/mbox/warning.gif" alt="">';
	}
	
	if(typeof(callbackFun) == 'undefined')
	{
		callbackFun = function(){}
	}
	
	box = new Boxy.alert(
		ico+alertText,
		callbackFun,
		{
			title:spanIco+title
		}
	);
	
	if($.browser.msie && $.browser.version == '6.0')
		$('.boxy-modal-blackout').bgiframe();
	$('.answers .button:last').focus();
}

function mBoxQuestion(title, question, answers, callbackFun)
{
	var spanIco = '<span class="titleIcoQuestion">&nbsp;</span>';
	var ico = '<img class="messIco" src="gfx/mbox/question.gif" alt="">';
	
	if(typeof(callbackFun) == 'undefined')
	{
		callbackFun = function(){}
	}
	
	if(typeof(answers) == 'undefined')
	{
		answers = new Array();
	}
	
	new Boxy.ask(
		ico + question,
		answers,
		callbackFun,
		{
			title:spanIco+title
		}
	);
	
	if($.browser.msie && $.browser.version == '6.0')
		$('.boxy-modal-blackout').bgiframe();
	$('.answers .button:last').focus();
}

/*
 * wywołanie tylko w document.ready()
 */
function mBoxShowLoading(titleWindow,text)
{	
	if(typeof(text) != 'undefined')
	{
		text = '<br/>'+text;
	}
	else
	{
		text = '';
	}
	
	box = new Boxy(
		'<div class="loading"><img src="gfx/mbox/loading.gif" alt="loading" />'+text+'</div>',
		{
			title: '<span class="titleIcoLoading">&nbsp;</span>'+titleWindow,
			modal: true,
			closeable: false,
			center : true,
			fixed : true
		}
	);
	
	if($.browser.msie && $.browser.version == '6.0')
		$('.boxy-modal-blackout').bgiframe();
}

/*
 * wywołanie tylko w document.ready()
 */
function mBoxHide()
{
	if(typeof(box) == 'object')
	{
		box.hide();
		box = null;
	}
}

function showDatePicker(fieldId)
{
	$("#"+fieldId).datepicker("show");
}


function updateLink(fieldId,str)
{
				
	toChange = ["ą","Ą","ć","Ć","ę","Ę","ń","Ń","ś","Ś","ó","Ó","ł","Ł","ż","Ż","ź","Ź"];
	change =   ["a","a","c","c","e","e","n","n","s","s","o","o","l","l","z","z","z","z"];
	
	for(i=0;i<toChange.length;i++)
	{
		regex = new RegExp(toChange[i],'g');
		str = str.replace(regex,change[i]);
	}
	
	str = str.toLowerCase().replace(/[\s]/g,'-').replace(/[^a-z0-9-_]/g,'');
	$("#"+fieldId).val(str);
}

/*
 * przejscie do zadanego urla
 */
function redirect(url)
{
	window.location.href = url;
}

function setCookie(name,value,expires,path,domain,secure) 
{
	var today = new Date();
	today.setTime( today.getTime() );

      if(expires)
      {
		expires = expires * 1000 * 60 * 60 * 24;
      }
      var expires_date = new Date( today.getTime() + (expires) );

      document.cookie = name + "=" +escape(value) +
      ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
      ( ( path ) ? ";path=" + path : "" ) + 
      ( ( domain ) ? ";domain=" + domain : "" ) +
      ( ( secure ) ? ";secure" : "" );
	/*$.ajax({
	 	type:'post',
		url:'/admin.php?CORE_ACTION=AJAX&imperial_ajax=s%3A52%3A%22SU1QRVJJQUxfQ09PS0lFJnNldENvb2tpZSZlbXB0eSZhOjA6e30%3D%22%3B'
	 });*/
}

// this fixes an issue with the old method, ambiguous values 
// with this test document.cookie.indexOf( name + "=" );
function getCookie(check_name) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

/*
// this function gets the cookie, if it exists
// don't use this, it's weak and does not handle some cases
// correctly, this is just to maintain legacy information
function Get_Cookie( name ) {
	
var start = document.cookie.indexOf( name + "=" );
var len = start + name.length + 1;
if ( ( !start ) &&
( name != document.cookie.substring( 0, name.length ) ) )
{
return null;
}
if ( start == -1 ) return null;
var end = document.cookie.indexOf( ";", len );
if ( end == -1 ) end = document.cookie.length;
return unescape( document.cookie.substring( len, end ) );
}

*/


// this deletes the cookie when called
function deleteCookie(name,path,domain)
{
      if(getCookie(name)) document.cookie = name + "=" +
      ( ( path ) ? ";path=" + path : "") +
      ( ( domain ) ? ";domain=" + domain : "" ) +
      ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
