/*
 * @autor Łukasz Sokół
 */

var box = null;



function formsubmit(formName)
{
	document.getElementById('imperial_form_'+formName).submit();
}

function reload()
{
	window.location.href = window.location.href;
}

/*
 * Opis: Formatuje cenę dla 444444 daje 444 444 - usuwa 0 z przodu i białe znaki
 */
function formatPrice(price,separator,rest,restSeparator)
{
	var sepRegexp = '/'+separator+'/g';
	price = price.replace(/^0{1,}/g,'').replace(/[^0-9]/g,'').replace(/[\s]/g,'').replace(sepRegexp,'');

	var pLength = price.length;

	if(typeof(separator) == 'undefined')
	{
		separator = ' ';
	}
	
	if(typeof(restSeparator) == 'undefined')
	{
		restSeparator = '.';
	}
	
	
	if(typeof(rest) == 'undefined')
	{
		rest = false;
	}
	
	if(rest)
	{
		if(pLength > 0)
		{
			if(pLength == 1)
			{
				price = '00'+price;
				pLength = 3;
			}
			else
			{
				if(pLength == 2)
				{
					price = '0'+price;
					pLength = 3;
				}
			}			
		
			var newPrice = '';
			var restString = '';
			for(i = 0;i < pLength;i++)
			{
				if(i >= pLength-2)
				{
					restString = restString + price.charAt(i);
				}
				else
					newPrice = newPrice + price.charAt(i)
			}
						
			pLength = newPrice.length;
			
			if(pLength > 0)
			{
				var rewrite = '';
				j = 1;
				for(i = pLength;i > 0;i--)
				{
					rewrite = newPrice.charAt(i-1)+rewrite;
					if((j % 3 == 0) && j != pLength)
					{
						rewrite = separator + rewrite;
					}
					j++;
				}
				
				newPrice = rewrite;
			}
			
			return newPrice+restSeparator+restString
		}
		else
		{
			return '';
		}
	}
	else
	{
		if(pLength > 0)
		{
			var rewrite = '';
			j = 1;
			for(i = pLength;i > 0;i--)
			{
				rewrite = price.charAt(i-1)+rewrite;
				if((j % 3 == 0) && j != pLength)
				{
					rewrite = separator + rewrite;
				}
				j++;
			}
			
			return rewrite;
		}
		else
		{
			return price;
		}
	}
}

/*
 * Opis: Formatuje cenę dla 444444 daje 444 444 - usuwa 0 z przodu i białe znaki
 */
function formatPrice2(price)
{
	price = price.replace(/[^0-9.,]/g,'').replace(/,/g,'.').replace(/[.]{2,}/g,'.');
	var re1 = new RegExp(/test/);
	
	return price;
}

/*
 * Opis: Formatuje cenę dla 444444 daje 444 444 - usuwa 0 z przodu i białe znaki
 */
function numbers(price)
{
	return price.replace(/[^0-9]/g,'');
}

$(document).ready(
function()
{
	var backColor = null;
	$('.gridTable tr:gt(0)').hover(
		function(){
			backColor = $(this).css('backgroundColor');
			
			$(this).css({
				'backgroundColor':'#faf4ec'
			});
		},
		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;
		case 'empty':
			var spanIco = '<span class="titleIcoInfo">&nbsp;</span>';
			var ico = '';
			break;
		default:
			var spanIco = '<span class="titleIcoWarning">&nbsp;</span>';
			var ico = '<img class="messIco" src="gfx/mbox/warning.gif" alt="">';
	}
	
	if(type == 'info' && (typeof(callbackFun) == 'undefined'))
	{
		var message = '<div class="WcontDef"><div class="WCD_title"><div class="WCD_title_left"><div class="WCD_title_right">'+title+'</div></div></div><div class="WCD_content">'+alertText+'<br/><br/><div class="left b_m"><div class="buttonL"><div class="buttonR"><input type="button" class="button" value="OK"/></div></div></div><div class="clear"></div></div><div class="WCD_foot"><div class="WCD_foot_left"><div class="WCD_foot_right"/></div></div></div>';

		$('#systemMESSAGE').removeClass('DISP_NO').html(message);
		$('#systemMESSAGE input').click(function(){
			$('#systemMESSAGE').html('').addClass('DISP_NO');
		});
	}
	else
	{
		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";
}

