// ================================================
//
// JSLIB / Knihovna obecných a užitečných JS funkcí
//
// verze                                       1.0
//
// ================================================


// ================================================
// Otevře nové okno                                
// ================================================
function OpenPopupWindow(PopupWindowUrl, PopupWindowWidth, PopupWindowHeight, ShowLocation) {
    
    if (PopupWindowUrl.length > 0) {
    
        if (!PopupWindowWidth)
            PopupWindowWidth = 700;
            
        if (!PopupWindowHeight)
            PopupWindowHeight = 400;
            
        if (!ShowLocation)
            ShowLocation = false;
    
        var centerPoint = window.center({width:PopupWindowWidth,height:PopupWindowHeight});
        var random = Math.round(Math.random() * 100);
        var design = 'width=' + PopupWindowWidth + ', height=' + PopupWindowHeight + ', left=' + centerPoint.x + ', top=' + centerPoint.y + ', location=' + (ShowLocation ? '1' : '0') + ',toolbar=0, menubar=0, statusbar=0, scrollbars=1, resizable=no';
        
        window.open(PopupWindowUrl, 'PopupWin' + random, design);
    }
    return false;
    
}


// ================================================
// Otevře nové okno s obrázkem                               
// ================================================
function OpenPopupImage(ImageUrl) {

    if (ImageUrl.length > 0) {
        var random = Math.round(Math.random() * 100);
        var win = window.open('', 'PopupWin' + random);
        win.document.write("<html><head><title>Picture</title></head><body onclick=\"window.close();\">");
        win.document.write("<img src=\"" + ImageUrl + "\" alt=\"\" />");
        win.document.write("</body></html>");
    }
    return false;
    
}



// ================================================
// Zjistí velikost okna, vrací objekt dvou
// proměnných: width, height
// ================================================
window.size = function()
{
	var w = 0;
	var h = 0;

	//IE
	if(!window.innerWidth)
	{
		//strict mode
		if(!(document.documentElement.clientWidth == 0))
		{
			w = document.documentElement.clientWidth;
			h = document.documentElement.clientHeight;
		}
		//quirks mode
		else
		{
			w = document.body.clientWidth;
			h = document.body.clientHeight;
		}
	}
	//w3c
	else
	{
		w = window.innerWidth;
		h = window.innerHeight;
	}
	return {width:w,height:h};
}



// ================================================
// Zjistí souřadnice bodu, na které je potřeba
// posunout okno, aby bylo ve středu obrazovky                               
// ================================================
window.center = function()
{
	var hWnd = (arguments[0] != null) ? arguments[0] : {width:0,height:0};

	var _x = 0;
	var _y = 0;
	var offsetX = 0;
	var offsetY = 0;

	//IE
	if(!window.pageYOffset)
	{
		//strict mode
		if(!(document.documentElement.scrollTop == 0))
		{
			offsetY = document.documentElement.scrollTop;
			offsetX = document.documentElement.scrollLeft;
		}
		//quirks mode
		else
		{
			offsetY = document.body.scrollTop;
			offsetX = document.body.scrollLeft;
		}
	}
	//w3c
	else
	{
		offsetX = window.pageXOffset;
		offsetY = window.pageYOffset;
	}

	_x = ((this.size().width-hWnd.width)/2)+offsetX;
	_y = ((this.size().height-hWnd.height)/2)+offsetY;

	return{x:_x,y:_y};
}


// ================================================
// Vypočte DPH z ceny bez DPH
// ================================================
function ComputeVatFromPriceWithoutVat(Price, VatCharge) {
    var coef = VatCharge / 100;
    coef = coef.round(4);
    var vat = Price * coef;
    return vat.round(2);
}


// ================================================
// Vypočte DPH z ceny s DPH
// ================================================
function ComputeVatFromPriceWithVat(Total, VatCharge) {
    var coef = VatCharge / (100 + VatCharge);
    coef = coef.round(4);
    var vat = Total * coef;
    return vat.round(2);
}


// ================================================
// Umožní správné chování prvku LinkButton
// na stisknutí Enter.
// ================================================
function AddEnterBehaviorToLinkButton(elementId) {

    var element = $(elementId);
    if (element && typeof (element.click) == 'undefined') {
        element.click = function() {
            var result = true;
            if (element.onclick) result = element.onclick();
            if (typeof (result) == 'undefined' || result) {
                eval(element.getAttribute('href'));
            }
        }
    }
    
}

