var util = { }
var outil = { }

util.loadJS = function (src) {
	return unescape("%3Cscript src='" +src+"' type='text/javascript'%3E%3C/script%3E");
}

util.parseFloat = function(v)
{
	v = v.replace(' ', '');
	regexp = eval("/,/g");
	return parseFloat(v.replace(regexp, "."));
}

util.doRound = function(x, places)
{
	return Math.round(x * Math.pow(10, places)) / Math.pow(10, places);
}

util.isUrl = function(s) {
	var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	return regexp.test(s);
}

util.htmlFloat = function(v, p)
{
	v = this.doRound(v, p);
	v = new String(v);
	if( v.indexOf(".")<0) v+=".0";
	while( (v.length-v.indexOf(".")-1)<p) v = v+"0";
	return v.replace(/\./g, ",");
}

util.jClock = function()
{
	// if( !jPL._bIe ) return false; // IE only
	// if( !(elEx('djPLClock')) ) return false;
	try {
		var date = new Date();
		$("#djPLClock").html(date.toLocaleString());
		setTimeout(function() {
			util.jClock();
		}, 1000);
	}
	
	catch(e){
		console.log(e);
	}
}

util.email = function() {
	var regExp = /#email\/(.*?)\/(.*)$/i;
	$('.email').each(function(i) {
		var m = this.href;
		if( regExp.test(m) )
		{
			var res = m.match(regExp);
			try {
				var email = res[1]+"@"+res[2];
				this.title = regExp.test(m) ? email : this.title;
				this.innerHTML = this.title;
				this.href = "mailto:"+email; // Zmienić tylko po kliknięciu
			}
			catch(e) {
			  // console.log(e);
			}
		} // else console.log("Nie powodzenie. "+m);
	});
}

util.sprintf = function()
{
	if(!arguments || arguments.length < 1 || !RegExp)
	{
		return;
	}
	var str = arguments[0];
	var re = /([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X)(.*)/;
	var a = b = [], numSubstitutions = 0, numMatches = 0;
	while (a = re.exec(str))
	{
		var leftpart = a[1], pPad = a[2], pJustify = a[3], pMinLength = a[4];
		var pPrecision = a[5], pType = a[6], rightPart = a[7];
				
		//alert(a + '\n' + [a[0], leftpart, pPad, pJustify, pMinLength, pPrecision);

		numMatches++;
		if (pType == '%')
		{
			subst = '%';
		}
			else
			{
				numSubstitutions++;
				if (numSubstitutions >= arguments.length)
				{
					alert('Error! Not enough function arguments (' + (arguments.length - 1) + ', excluding the string)\nfor the number of substitution parameters in string (' + numSubstitutions + ' so far).');
				}
					var param = arguments[numSubstitutions];
					var pad = '';
					       if (pPad && pPad.substr(0,1) == "'") pad = leftpart.substr(1,1);
					  else if (pPad) pad = pPad;
					var justifyRight = true;
					       if (pJustify && pJustify === "-") justifyRight = false;
					var minLength = -1;
					       if (pMinLength) minLength = parseInt(pMinLength);
					var precision = -1;
					       if (pPrecision && pType == 'f') precision = parseInt(pPrecision.substring(1));
					var subst = param;
					       if (pType == 'b') subst = parseInt(param).toString(2);
					  else if (pType == 'c') subst = String.fromCharCode(parseInt(param));
					  else if (pType == 'd') subst = parseInt(param) ? parseInt(param) : 0;
					  else if (pType == 'u') subst = Math.abs(param);
					  else if (pType == 'f') subst = (precision > -1) ? Math.round(parseFloat(param) * Math.pow(10, precision)) / Math.pow(10, precision): parseFloat(param);
					  else if (pType == 'o') subst = parseInt(param).toString(8);
					  else if (pType == 's') subst = param;
					  else if (pType == 'x') subst = ('' + parseInt(param).toString(16)).toLowerCase();
					  else if (pType == 'X') subst = ('' + parseInt(param).toString(16)).toUpperCase();
			}
			str = leftpart + subst + rightPart;
	}
	return str;
}

outil.objIsActive = function(el)
{
	if( !elEx(el) )
		return false;
	if( typeof $(el).active != 'undefined' && typeof $(el).active )
		return true
	return false
}

outil.setcookie = function(name, value, days)
{
	if(days)
	{
		var date = new Date();
      date.setTime(date.getTime()+(days*24*60*60*1000));
      var expires = "; expires="+date.toGMTString();
    }
    	else
			var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}


outil.makeEmail = function(d, u, sf, i)
{
	var it = $(i);
	var fm = '<a href="mailto:%s">%s</a>';
	
	if( !it )
		return false;
	var em = u + "@" + d + "." + sf;
	var f = sprintf(fm, em, em);
	it.innerHTML = f;
}

outil.onLoad = function()
{
	jPL.__init();
	// if( typeof jPLSiteOnLoad == "function" ) { jPLSiteOnLoad(); }
	try {
		jPLSiteOnLoad();
	}
	catch(e) {
		// alert(e);
	}
	jClock();
}

outil.html_textarea = function (o, v, n, className)
{
	// <!-- <textarea name="products[{productsrow.PRODUCT_ID}][products_name]" rows="2" style"min-width: 120px;"></textarea> -->
	if( !className ) className = null;
	o.innerHTML = '<textarea name="'+n+'" class="'+className+'">'+v+'</textarea>';
	o.edited = true;
}

function sprintf()
{
	if(!arguments || arguments.length < 1 || !RegExp)
	{
		return;
	}
	var str = arguments[0];
	var re = /([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X)(.*)/;
	var a = b = [], numSubstitutions = 0, numMatches = 0;
	while (a = re.exec(str))
	{
		var leftpart = a[1], pPad = a[2], pJustify = a[3], pMinLength = a[4];
		var pPrecision = a[5], pType = a[6], rightPart = a[7];
				
		//alert(a + '\n' + [a[0], leftpart, pPad, pJustify, pMinLength, pPrecision);

		numMatches++;
		if (pType == '%')
		{
			subst = '%';
		}
			else
			{
				numSubstitutions++;
				if (numSubstitutions >= arguments.length)
				{
					alert('Error! Not enough function arguments (' + (arguments.length - 1) + ', excluding the string)\nfor the number of substitution parameters in string (' + numSubstitutions + ' so far).');
				}
				var param = arguments[numSubstitutions];
				var pad = '';
				if(pPad && pPad.substr(0,1) == "'") pad = leftpart.substr(1,1);
				else if (pPad) pad = pPad;
				var justifyRight = true;
				if (pJustify && pJustify === "-") justifyRight = false;
				var minLength = -1;
				if (pMinLength) minLength = parseInt(pMinLength);
				var precision = -1;
				if (pPrecision && pType == 'f') precision = parseInt(pPrecision.substring(1));
				var subst = param;
				if (pType == 'b') subst = parseInt(param).toString(2);
				else if (pType == 'c') subst = String.fromCharCode(parseInt(param));
				else if (pType == 'd') subst = parseInt(param) ? parseInt(param) : 0;
				else if (pType == 'u') subst = Math.abs(param);
				 else if (pType == 'f') subst = (precision > -1) ? Math.round(parseFloat(param) * Math.pow(10, precision)) / Math.pow(10, precision): parseFloat(param);
				 else if (pType == 'o') subst = parseInt(param).toString(8);
				 else if (pType == 's') subst = param;
				 else if (pType == 'x') subst = ('' + parseInt(param).toString(16)).toLowerCase();
				 else if (pType == 'X') subst = ('' + parseInt(param).toString(16)).toUpperCase();
			}
			str = leftpart + subst + rightPart;
	}
	return str;
}

function getSelectedValue( select_field )
{
	if( !select_field ) return;
	return select_field.options[ select_field.selectedIndex ].value;
}

function setSelect( selector, value )
{
	var i;
	for( i=0; i < selector.length; i++ )
	{
		if( selector.options[i].value == value )
		{
			selector.selectedIndex = i;
			return true;
		}
	}
	return false;
}

String.prototype.toUpperCasePL = function(){
  this.replace(/ą/g,"Ą");
  this.replace(/ć/g,"Ć");
  this.replace(/ę/g,"Ę");
  this.replace(/ł/g,"Ł");
  this.replace(/ń/g,"Ń");
  this.replace(/ó/g,"Ó");
  this.replace(/ś/g,"Ś");
  this.replace(/ź/g,"Ź");
  this.replace(/ż/g,"Ż");
  return this.toUpperCase();
}
 
String.prototype.toLowerCasePL = function(){
  this.replace(/Ą/g,"ą");
  this.replace(/Ć/g,"ć");
  this.replace(/Ę/g,"ę");
  this.replace(/Ł/g,"ł");
  this.replace(/Ń/g,"ń");
  this.replace(/Ó/g,"ó");
  this.replace(/Ś/g,"ś");
  this.replace(/Ź/g,"ź");
  this.replace(/Ż/g,"ż");
  return this.toLowerCase();
}

String.prototype.addSlashes = function(limiter)
{
	if(typeof limiter == 'undefined') limiter = "'";
	var regExp = new RegExp(limiter, 'g');
	return this.replace(/\\/g, '\\\\').replace(regExp, '\\' + limiter);
}

String.prototype.ucFirst = function()
{
	return this.replace(/[a-zA-ZĄĆĘŁŃÓŚŹŻąćęłńóśźż]+/g, function(t)
	{
		return t.charAt(0).toUpperCasePL() + t.substr(1).toLowerCasePL();
	});
}

Array.prototype.inArray = function(n)
{
	for(var i=0; i<this.length; i++)
	{
		if (this[i] == n) return true;
	}
	return false;
}
