var effects = true;

function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}

function strpos( haystack, needle, offset){
    var i = (haystack+'').indexOf( needle, offset ); 
    return i===-1 ? false : i;
}

function isset(  ) {
    var a=arguments, l=a.length, i=0;
    
    if (l===0) {
       throw new Error('Empty isset');
    }
    
    while (i!==l) {
        if (typeof(a[i])=='undefined' || a[i]===null) { 
            return false; 
        } else { 
            i++; 
        }
    }
    return true;
}

function initChBox(code)
{
	$("input:checkbox").click(function () {
		DataLost = true;
		if(this.checked == true)
		{
			$("#tx" + this[code]).css('color','black');
		}
		else
		{
			$("#tx" + this[code]).css('color','#777');
		}
	});
}
// set window cords for pop-ups
function setCoords(id,pop,prTop,prLeft)
{
	prLeft = isset(prLeft) ? prLeft : 0;
	prTop = isset(prTop) ? prTop : 0;
    var pos = $(id).offset();
    $(pop).css({ 
        position: 'absolute',
        zIndex: 5000,
        left: pos.left+prLeft, 
        top: pos.top+prTop
    });
}
// detect if it ie by version
function isIE(version) {
	//var browser = navigator.userAgent;
	var browserApp=navigator.appName;	
	if (browserApp=="Microsoft Internet Explorer") {
		if (navigator.userAgent.indexOf("MSIE "+version) != -1) {			
			for (var i=version+1; i < 17; i++) {
				if (navigator.userAgent.indexOf("MSIE "+i) != -1) {
					return false;
				}
			}
			return true;
		}		
	}
	return false;
}
// init cool reszer for image
function initImgResizer(cls)
{
	var zIn = 500,
		zOut = 500;

	if(!effects || isIE(8))
	{
		zIn = zOut = 0;
	}
	
	$("."+cls).fancybox({
		'zoomOpacity': true,
		'overlayShow': true,
		'overlayOpacity': 0,
		'zoomSpeedIn': zIn,
		'zoomSpeedOut': zOut
	});
	$("."+cls).click(function () {
		this.blur();
	});

}
// display message
function showMsgWnd(selector, opacity, width, top) {	
	if ( typeof(opacity)=='undefined' || opacity===null ) {
		opacity = 0.4;
	}
	if ( typeof(width)=='undefined' || width===null ) {
		width = 560;
	}
	if ( typeof(top)=='undefined' || top===null ) {
		top = '20%';
	}
	if (isIE(6)) {
		opacity = 0;
	}

	if(effects)
	{
		var fin = 200,
			fout = 400;
	}
	else
	{
		var fin = 0,
			fout = 0;
	}
	if($(window).height() < 500)
		top = 0;

	$.blockUI({ message: $(selector),				
				fadeIn: fin,
				fadeOut: fout,
				focusInput: false,
            	css: { border:'0px',
           		'width':width+'px',
           		background:'', 
           		'top':  top, 
               	left: ($(window).width() - width) /2 + 'px' },
       			overlayCSS: { 'opacity': opacity },       			
       			onUnblock:$(document).unbind()
	});

	$(document).keydown(function (event) {
		if(event.keyCode == 27)
		{
			$.unblockUI();
		}
	});
}
// check enabled flash or not
function detectFlash(){
	var D=window,K=document,N=D.location,M=D.navigator,
        L="application/x-shockwave-flash",H="Shockwave Flash",
        A="ShockwaveFlash.ShockwaveFlash",Q="undefined";
    var S=null,U=null,R;
    if(typeof M.plugins!=Q&&typeof M.plugins[H]=="object"){
        S=M.plugins[H].description;
        if(S&&!(typeof M.mimeTypes!=Q&&M.mimeTypes[L]&&!M.mimeTypes[L].enabledPlugin)){
        	U=S.replace(/([a-zA-Z]|\s)+/,"").replace(/(\s+r|\s+b[0-9]+)/,".")
        }
    } else {
    	if(typeof D.ActiveXObject!=Q){
           try{
	            R=new ActiveXObject(A);
	            if(R){
	                    S=R.GetVariable("$version");
	                    if(S){
	                            U=S.split(" ")[1].replace(/,/g,".").replace(/[^.\d]/g,"")
	                    }
	            }
           }
           catch(T){}
        }
    }
    
    return U&&("f:"+U);
}

function StartAjaxSpinner()
{
        $.blockUI({ 
            message: 'Пожалуйста подождите,<br>идет загрузка...', 
            fadeIn: 700, 
            fadeOut: 700, 
            showOverlay: false, 
            centerY: false, 
            css: { 
                width: '300px', 
                top: '10px', 
                left: '', 
                right: '10px', 
                border: 'none', 
                padding: '5px', 
                backgroundColor: '#000', 
                '-webkit-border-radius': '10px', 
                '-moz-border-radius': '10px', 
                opacity: .7, 
                color: '#fff',
                cursor:'default'
                
            }
        });
}

function StopAjaxSpinner()
{
	$.unblockUI();
}

function simple_tooltip(target_items, name)
{
	$(target_items).each(function(i)
	{
		$("#"+name+i).remove();
	});
	
	$(target_items).each(function(i)
	{
		if($(this).attr('title') != '')
		{			
			$("body").append("<iframe class='"+name+"_frame' frameborder='0' style='display:none;' id='f"+name+i+"'><\/iframe>");
			var my_iframe = $("#f"+name+i);

			$("body").append("<div class='"+name+"' style='display:none;' id='"+name+i+"'><p align='justify'>"+$(this).attr('title')+"<\/p><\/div>");
			var my_tooltip = $("#"+name+i);

			my_iframe.css('height',my_tooltip.height());

			$(this).removeAttr("title");
			$(this).mouseover(function()
			{
				my_tooltip.css({opacity:1, display:"none"});
				my_iframe.css({opacity:0, display:"block"});
				if(effects)
					my_tooltip.fadeIn(200);
				else
					my_tooltip.css({display:"block"});
			});
			$(this).mousemove(function(kmouse)
			{
				my_tooltip.css({left:kmouse.pageX+15, top:kmouse.pageY+15});
				my_iframe.css({left:kmouse.pageX+15, top:kmouse.pageY+15});
			});
			$(this).mouseout(function()
			{
				if(effects)
					my_tooltip.fadeOut(200);
				else
					my_tooltip.css({display:"none"});

				my_iframe.css({display:"none"});
			});
		}
	});
}

function htmlspecialchars_decode (string, quote_style) {
    var optTemp = 0, i = 0, noquotes= false;
    if (typeof quote_style === 'undefined') {
        quote_style = 2;
    }
    string = string.toString().replace(/&lt;/g, '<').replace(/&gt;/g, '>');
    var OPTS = {
        'ENT_NOQUOTES': 0,
        'ENT_HTML_QUOTE_SINGLE' : 1,
        'ENT_HTML_QUOTE_DOUBLE' : 2,
        'ENT_COMPAT': 2,
        'ENT_QUOTES': 3,
        'ENT_IGNORE' : 4
    };
    if (quote_style === 0) {
        noquotes = true;
    }
    if (typeof quote_style !== 'number') { // Allow for a single string or an array of string flags
        quote_style = [].concat(quote_style);
        for (i=0; i < quote_style.length; i++) {
            // Resolve string input to bitwise e.g. 'PATHINFO_EXTENSION' becomes 4
            if (OPTS[quote_style[i]] === 0) {
                noquotes = true;
            }
            else if (OPTS[quote_style[i]]) {
                optTemp = optTemp | OPTS[quote_style[i]];
            }
        }
        quote_style = optTemp;
    }
    if (quote_style & OPTS.ENT_HTML_QUOTE_SINGLE) {
        string = string.replace(/&#0*39;/g, "'"); // PHP doesn't currently escape if more than one 0, but it should
        // string = string.replace(/&apos;|&#x0*27;/g, "'"); // This would also be useful here, but not a part of PHP
    }
    if (!noquotes) {
        string = string.replace(/&quot;/g, '"');
    }
    // Put this in last place to avoid escape being double-decoded
    string = string.replace(/&amp;/g, '&');

    return string;
}
