﻿// JScript File

Type.registerNamespace("QPM");
Type.registerNamespace("QPM.Web");


// Closes broser windo
//
QPM.Web.CloseWindow = function()
{
	if (!window.opener) 
	{
		window.opener = window;
	}
	window.close();
}

// Refreshes parent window
//
QPM.Web.RefreshParentWindow = function()
{
	if (window.opener) 
	{
		if (typeof(window.opener.ShowPleaseWait) != 'undefined') 
			window.opener.ShowPleaseWait();
			
		window.opener.window.history.go();
	}
}


// Screen functions
//
QPM.Web.cursorToScreenX = function() { return event.screenX; }

QPM.Web.cursorToScreenY = function() { return event.screenY; }

QPM.Web.toScreenX = function(object) { return QPM.Web.toDocumentX(object,true) + window.screenLeft; }

QPM.Web.toScreenY = function(object) { return toDocumentY(object,true) + window.screenTop; }

QPM.Web.fromScreenX = function(i) { return i - window.screenLeft; }

QPM.Web.fromScreenY = function(i) {	return i - window.screenTop; }

QPM.Web.toDocumentY = function(object,isForScreen)
{
	var y = 0

	while(object) {
		y += (object.offsetTop ? object.offsetTop : 0)
		if (isForScreen) {
			y -= (object.scrollTop ? object.scrollTop : 0)
		}
		object = object.offsetParent
	}

	return y
}

QPM.Web.toDocumentX = function(object,isForScreen)
{
	var x = 0
	while(object) {
		x += (object.offsetLeft ? object.offsetLeft : 0)
		if (isForScreen) {
			x -= (object.scrollLeft ? object.scrollLeft : 0)
		}
		object = object.offsetParent
	}
	return x
}

QPM.Web.diffStyleOffsetWidth = function(object) { return object.offsetWidth- parseInt(object.currentStyle.width); }

QPM.Web.windowSize = 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 };
}

QPM.Web.windowCenter = 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 = ((QPM.Web.windowSize().width - hWnd.width) / 2) + offsetX;
	_y = ((QPM.Web.windowSize().height - hWnd.height) / 2) + offsetY;

	return { x: _x, y: _y };
}

// ****************************************************************
// SiteManager
// ****************************************************************

QPM.Web.SiteManager = function(currentSectionName, currentCultureName, cookiesManager) 
{ 
	QPM.Web.SiteManager.initializeBase(this);

	this._currentSectionName = currentSectionName;
	this._currentCultureName = currentCultureName;
	this._cookiesManager = cookiesManager;
	
	// Constants
	// 
	this._USER_CULTURE_NAME = "LanguageName";
	this._USER_ACCOUNT_NAME = "UserAccount";
	this._GUI_LAYOUT = "GuiLayout";
	this._SESSION_COOKIE_NAME = "SessionCookie";
	this._SUPPRESS_MODE_COOKIE_NAME = "SuppressMode";
}

QPM.Web.SiteManager.prototype =
{
	getCurrentSectionName : function() { return this._currentSectionName; }
	,
	getCurrentCultureName : function() { return this._currentCultureName; }
	,
	getSessionCookieTest : function() { return this._cookiesManager.getCookie(this._SESSION_COOKIE_NAME) == "Test"; }
	,
	getSuppressMode : function() { return this._cookiesManager.getCookie(this._SUPPRESS_MODE_COOKIE_NAME) == "True"; }
	,
	setSuppressMode : function(status) { this._cookiesManager.setCookie(this._SUPPRESS_MODE_COOKIE_NAME, (status == true) ? "True" : "False"); }
	,
	getUserCultureName : function() { return this._cookiesManager.getCookie(this._USER_CULTURE_NAME); }
	,
	getUserAccountName : function() { return this._cookiesManager.getCookie(this._USER_ACCOUNT_NAME); }
	,
	getGuiLayout : function() { return this._cookiesManager.getCookie(this._GUI_LAYOUT); }
}

QPM.Web.SiteManager.registerClass('QPM.Web.SiteManager');


// ****************************************************************
// CookiesManager
// ****************************************************************

QPM.Web.CookiesManager = function(cookiePath, cookieExpiredDate) 
{ 
	QPM.Web.CookiesManager.initializeBase(this);
	
	this._cookiePath = cookiePath;
	this._cookieExpiredDate = cookieExpiredDate;
}

QPM.Web.CookiesManager.prototype =
{
	setCookie : function(name, value, persistent)
	{
		var curCookie = name + "=" + value + // escape(value)
				((persistent) ? "; expires=" + this._cookieExpiredDate : "") +
				"; path=" + this._cookiePath;
		document.cookie = curCookie;
	},
	
	getCookie : function(name)
	{
		var dc = document.cookie;
		var prefix = name + "=";
		var begin = dc.indexOf("; " + prefix);

		if (begin == -1) 
		{
			begin = dc.indexOf(prefix);

			if (begin != 0) 
			{
				return null;
			}
  	}
  	else 
  	{
    	begin += 2;
		}

		var end = document.cookie.indexOf(";", begin);

		if (end == -1) 
		{
			end = dc.length;
		}

		var result = dc.substring(begin + prefix.length, end); 
		
		if (result.indexOf('&') > 0)
		{
			var splitted = result.split('&');
			var resultArray = new Array();
			for(var i = 0; i < splitted.length; i++)
			{
				var nameValue = splitted[i].split('=');
				resultArray[unescape(nameValue[0])] = unescape(nameValue[1]);
			}
			return resultArray;
		}
		return unescape(result);
	},
	
	deleteCookie : function(name)
	{
		if (getCookie(name)) {
			document.cookie = name + "=" +
			"; path=" + this._cookiePath +
			"; expires=Thu, 01-Jan-70 00:00:01 GMT";
		}
	}
}

QPM.Web.CookiesManager.registerClass('QPM.Web.CookiesManager');


// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"
QPM.Web.FixDate = function(date)
{
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

QPM.Web.IsVisible = function(obj)
{
	if (Sys.UI.DomElement.containsCssClass(obj, 'hide')) return false;

	var parent = obj.parentNode;

	while (parent != null && parent != document.body)
	{
		if (Sys.UI.DomElement.containsCssClass(parent, 'hide')) return false;
		parent = parent.parentNode;
	}

	return true;
}