﻿//=============================================================
//=============================================================
//=============================================================
//===						===================================
//=== __CGlobal, g_global	===================================
//===						===================================
//=============================================================
//=============================================================
//=============================================================
var g_global = new __CGlobal();
function __CGlobal()
{
	this.guid_DialogWindow = "G_CustomDialogWindow";
	this.selectedControl = null;
	
	this.currentProtocolAndHost = "";	//...assign in /MasterPages/Global.master
	this.currentHost = "";				//...assign in /MasterPages/Global.master
	this.sessionID = "";				//...assign in /MasterPages/Global.master

	//this.oModalWindow = null;
}
//-------------------------------------------------------------
//--- GetChildNodesByAttrValue
//-------------------------------------------------------------
__CGlobal.prototype.GetChildNodesByAttrValue = function(_xPar, _tagName, _attrName, _attrValue)
{
	var arr = new Array();
	var a = g_global.GetChildElementsByTagName(_xPar, _tagName);
	for (var j = 0; j < a.length; j++)
	{
		var x = a[j];
		var v = x.getAttribute(_attrName);
		if (v == _attrValue)
		{
			arr.push(x);
		}
	}
	return arr;
}
//-------------------------------------------------------------
//--- GetFirstChildNodeByAttrValue
//-------------------------------------------------------------
__CGlobal.prototype.GetFirstChildNodeByAttrValue = function(_xPar, _tagName, _attrName, _attrValue)
{
	var arr = new Array();
	var a = g_global.GetChildElementsByTagName(_xPar, _tagName);
	for (var j = 0; j < a.length; j++)
	{
		var x = a[j];
		var v = x.getAttribute(_attrName);
		if (v == _attrValue)
		{
			return x;
		}
	}
	return null;
}
//-------------------------------------------------------------
//--- InitDDL
//-------------------------------------------------------------
__CGlobal.prototype.InitDDL = function(sel, val)
{
	var arr = sel.options;
	if (val == null) val = "";
	val = val + "";
	if (arr.length > 0) sel.selectedIndex = 0;
	for (var j = 0; j < arr.length; j++)
	{
		if (val.toUpperCase() == arr[j].value.toUpperCase())
		{
			sel.selectedIndex = j;
			break;
		}
	}
}
//-------------------------------------------------------------
//--- ClickCommand (do not cause validation)
//-------------------------------------------------------------
__CGlobal.prototype.ClickCommand = function(clientID, cmdName)
{
	var ss = clientID == "" || clientID == null ? "" : (clientID + "_");

	$get(ss + "hidCommand").value = cmdName;
	eval(unescape($get(ss + "lbCommand").href));

	//	var prm = Sys.WebForms.PageRequestManager.getInstance();
	//	prm._doPostBack(clientID + "_lbCommand", cmdName);
}
//-------------------------------------------------------------
//--- ClickCommand2 (cause validation)
//-------------------------------------------------------------
__CGlobal.prototype.ClickCommand2 = function(clientID, cmdName)
{
	$get(clientID + "_hidCommand").value = cmdName;
	eval(unescape($get(clientID + "_lbCommand2").href));
}

//-------------------------------------------------------------
//--- ShowWaitingPanel
//-------------------------------------------------------------
__CGlobal.prototype.ShowWaitingPanel = function(text, width, height, backcolor, border)
{
	var docObj = document.documentElement;
	if ((window.opera) || (document.all && (!(document.compatMode && document.compatMode == "CSS1Compat"))))
	{
		docObj = document.body;
	}
	var wwWin = parseInt(docObj.clientWidth);
	var hhWin = parseInt(docObj.clientHeight);
	//var wwDiv = 300;
	//var hhDiv = 200;
	var div = document.createElement("div");
	div.style.width = width + "px";
	div.style.height = height + "px";
	//div.style.display = "none";
	div.style.position = "absolute";
	div.style.left = (wwWin / 2 - width / 2) + "px";
	div.style.top = (hhWin / 2 - height / 2) + "px";
	div.style.backgroundColor = backcolor;  //"#9999ff";  //"#ccccff";
	div.style.border = border;  //"solid 2px #6699cc";
	div.style.textAlign = "center";
	//div.style.verticalAlign = "center";
	div.style.zIndex = "999999";
	//div.style.fontSize = "14pt";
	div.innerHTML = text;  //"<br><br><br><b>Processing...</b><br>Please wait";
	document.body.appendChild(div);
}
__CGlobal.prototype.ShowWaiting = function(text)
{
	g_global.ShowWaitingPanel(
		"<table style='font-size:10pt; height:100px;'>" +
		"<tr>" +
			"<td><img src='/images/16x16/indicator.gif' width='16' height='16'></td>" +
			"<td><b>" + text + "</b></td>" + 
		"</tr>" + 
		"</table>",
		300, 100, "#f8f8f8", "solid 10px #aaaaaa");
}
//-------------------------------------------------------------
//--- EnableDisableButton
//-------------------------------------------------------------
__CGlobal.prototype.EnableDisableButton=function(_button,_isEnabled)
{
	try
	{
		_button.disabled = _isEnabled!=true;
		var a = _button.getElementsByTagName("IMG");//.childNodes;
		for(var j=0; j<a.length; j++)
		{
			var o=a[j];
			if (o.getAttribute("c_initial_src")==null)
				o.setAttribute("c_initial_src", o.src);
			o.src = _isEnabled ? o.getAttribute("c_initial_src") : "/images/16x16/UncheckDisabled.gif";
			//o.style.filter = _isEnabled ? "" : "alpha(opacity=20)";
		}
	}catch(e){}
};
//-------------------------------------------------------------
//--- OpenDialog
//-------------------------------------------------------------
__CGlobal.prototype.OpenDialog=function(_url,_width,_height,_left,_top)
{
	var _win = document.getElementById(this.guid_DialogWindow);
	if (_win==null)
	{
		_win = document.createElement("IFRAME");
		_win.id = this.guid_DialogWindow;
		document.body.appendChild(_win);
		_win.style.display = "none";
	}
	_win.style.position	= "absolute";
	_win.style.border	= "solid 3px #888888";
	_win.style.height	= _height;
	_win.style.width	= _width;
	_win.style.top		= _top;
	_win.style.left		= _left;
	_win.style.backgroundColor = "#ffffff";
	_win.style.zIndex	= 1000;
	_win.style.display	= "block";
	_win.src = _url;
	return _win;
};
//-------------------------------------------------------------
//--- CloseDialog
//-------------------------------------------------------------
__CGlobal.prototype.CloseDialog=function(_win)
{
	if (_win==null)
		_win = document.getElementById(this.guid_DialogWindow);
	_win.style.display = "none";
}
//----------------------------------------------
//--- OpenModalWindow
//----------------------------------------------
/*
__CGlobal.prototype.OpenAJAXModalWindow=function(clientID,url,caption,width,height)
{
	$get(clientID+"_lModalWinCaption").innerHTML = caption;

	var pan = $get(clientID+"_panModal");
	var hed = $get(clientID+"_panModalHeader");

	pan.style.width = width + "px";
	pan.style.height= height + "px";
	
	var fr  = $get(clientID+"_ifrModal1");
	fr.style.width  = width + "px";
	fr.style.height = (height-19) + "px";
	fr.src = url;
	
	g_global.FireMouseEvent( document.getElementById(clientID+"_lbCallPopup"), "click");
}
*/



//----------------------------------------------
//--- GetRadWindow
//----------------------------------------------
__CGlobal.prototype.GetRadWindow = function()
{
	var oWindow = null;
	if (window.radWindow)
		oWindow = window.radWindow;
	else if (window.frameElement.radWindow)
		oWindow = window.frameElement.radWindow;
		
	return oWindow;
}

//----------------------------------------------
//--- OpenWindow
//----------------------------------------------
__CGlobal.prototype.OpenWindow = function(url,args,width,height,title,behaviors)
{
	var win = null;
	try
	{
		var oBrowserWnd = this.GetRadWindow().BrowserWindow;
		win = oBrowserWnd.radopen(); //"http://www.google.com", "NewWindow");
	}
	catch(e)
	{
		win = radopen();
	}
	setTimeout(function(){win.setActive(true);}, 10);
	if (args.callerwindow == null)
		args.callerwindow = window;
	args.window = win;
	//this.oModalWindow = win;
	var bh = behaviors != null ? behaviors : (Telerik.Web.UI.WindowBehaviors.Move + Telerik.Web.UI.WindowBehaviors.Close + Telerik.Web.UI.WindowBehaviors.Resize);
	win.set_behaviors( bh );
	win.argument = args;
	win.set_title(title);
	win.setUrl(url);
	win.setSize(width,height);
	win.set_modal(true);
	win.center();
}
//----------------------------------------------
//--- CloseWindow
//----------------------------------------------
__CGlobal.prototype.CloseWindow = function(args)
{
	//alert(args.response);
	//	if (args==null)args = this.oModalWindow.argument;
	//alert(args.callback);

	if (args.callback != null && args.callerwindow != null)
	{
		//eval(args.callback+"(args)");
		eval("args.callerwindow." + args.callback + "(args)");
	}

	//try{ eval(args.callback+"(args)"); }catch(e){
	//alert(e.message);
	//	}

	args.window.close();
}
//----------------------------------------------
//--- GetParentObject
//----------------------------------------------
__CGlobal.prototype.GetParentObject = function(o)
{
	//var p=o;while(p.getAttribute("c_token")!="wiki_Main")p=p.parentNode;
	var p=o;while(p.__obj==null)p=p.parentNode;
	return p.__obj;
}









//----------------------------------------------
//--- OpenModalWindow
//----------------------------------------------
__CGlobal.prototype.OpenAJAXModalWindow=function(clientID,url,caption,width,height)
{
	var mp = C_GLOBALBARIABLES.ModalPanel;
	
	mp.lModalWinCaption.innerHTML = caption;

	var pan = mp.panModal;
	var hed = mp.panModalHeader;

	pan.style.width = width + "px";
	pan.style.height= height + "px";
	
	mp.panObject.innerHTML = "";
	
	var ss="";
	if (window.navigator.userAgent.indexOf("MSIE") != -1)
	{
		ss = "<iframe class='modalPopupIFrame' scrolling='no' " + 
				"style='width:" + width + "px; height:" + (height-19) + "px;' " + 
				"src='" + url + "' " + 
				"title='iframe for modal popup panel'>" + 
				"iframe for modal popup panel" + 
			"</iframe>";
	}
	else
	{
		ss = "<object " + 
				//"classid='clsid:25336920-03F9-11CF-8FD0-00AA00686F13' " + 
				"style='width:" + width + "px; height:" + (height-19) + "px; overflow:hidden' " + 
				"data='" + url + "' " + 
				"title='object for modal popup panel' type='text/html'>" + 
				"<p>object for modal popup panel</p>" + 
			"</object>";
	}
	mp.panObject.innerHTML = ss;
	
	g_global.FireMouseEvent( mp.lbCallPopup, "click");
}
//----------------------------------------------
//--- Modal window commands
//----------------------------------------------
__CGlobal.prototype.CloseAJAXModalWindow=function()
{
	try
	{ 
		var mp = C_GLOBALBARIABLES.ModalPanel;
		g_global.FireMouseEvent( mp.butCancelModal, "click");
	}
	catch(e){}
}

/*
//---------------------
//--- BuildUrl
//---------------------
__CGlobal.prototype.BuildUrl=function(siteID)
{
	var args = new Function();
	args.SiteID = siteID;
	if (window.showModalDialog("/Panels/UrlBuilder/default.htm", args, "center:yes; dialogWidth:400px; dialogHeight:500px; help:no; resizable:yes; status:no; scroll:no"))
	{
		return args.value;
	}
	return null;
}
*/
//---------------------
//--- OpenUrlBuilder
//---------------------
__CGlobal.prototype.OpenUrlBuilder=function(callback,siteID,scope,actionsAllowed)
{
	var url = "/Panels/UrlBuilder/default.htm?callback="+callback;
	if (siteID!=null) url += "&siteID=" + siteID;
	if (scope!=null) url += "&scope=" + scope;
	if (actionsAllowed!=null) url += "&actionsAllowed=" + actionsAllowed;
	window.open(url, "_blank", "width=400px,height=600px,location=no,menubar=no,toolbar=no,resizable=yes");
}
//---------------------
//--- InsertRow
//---------------------
__CGlobal.prototype.InsertRow=function(tbl,rowIndex)
{
	var tr;
	try
	{
		if (rowIndex==null)rowIndex=-1;
		tr = tbl.insertRow(rowIndex);
	}
	catch(e)
	{
		tr = document.createElement("TR");
		tbl.appendChild(tr);
	}
	return tr;
}
//---------------------
//--- InsertCell
//---------------------
__CGlobal.prototype.InsertCell=function(tr,cellIndex)
{
	var td;
	try
	{
		if(cellIndex==null)cellIndex=-1;
		td = tr.insertCell(cellIndex);
	}
	catch(e)
	{
		td = document.createElement("TD");
		tr.appendChild(td);
	}
	return td;
}
//---------------------
//--- 
//---------------------
__CGlobal.prototype.GetQueryStringParam=function(href,paramName)
{
	try
	{
		var nn = paramName.toLowerCase();
		var ss = href;
		var i1 = ss.indexOf("?");
		if (i1 != -1)
		{
			ss = ss.substr(i1+1);
		}
		var arr = ss.split("&");
		for(var j=0; j<arr.length; j++)
		{
			var a = arr[j].split("=");
			if (a[0].toLowerCase() == nn)
			{
				return a[1];
			}
		}
	}
	catch(e){}
	return null;
}
//---------------------
//--- GetWindowParameter
//---------------------
__CGlobal.prototype.GetWindowParameter=function(win,paramName)
{
	var v = null;
	try{ v = this.GetQueryStringParam(win.location.href, paramName); }catch(e){}
	if (v==null) try{ v = eval("win.dialogArguments."+paramName); }catch(e){}
	return v;
}
//---------------------
//--- InsertImage
//---------------------
__CGlobal.prototype.InsertImage=function(container, imgUrl, imgWidth, imgHeight)
{
	var img = document.createElement("IMG");	
	container.appendChild(img);
	img.src = imgUrl;
	img.style.width = imgWidth;
	img.style.height = imgHeight;
	return img;
}
//---------------------
//--- Trim
//---------------------
__CGlobal.prototype.Trim=function(str)
{
	var i1 = 0;
	var i2 = str.length-1;
	for(var j=0; j<str.length; j++){	if (str.charAt(j)==' ')	i1++; else	break;	}
	for(var j=str.length-1; j>=0; j--){	if (str.charAt(j)==' ')	i2--; else	break;	}
	return (i1 <= i2) ? str.substr(i1,i2+1) : "";
}
//---------------------
//--- Trim
//---------------------
__CGlobal.prototype.Replace=function(str,oldSubstr,newSubstr)
{
	var i1;
	while((i1=str.indexOf(oldSubstr))!=-1)str=str.replace(oldSubstr,newSubstr);
	return str;
}

//-----------------------------------------------------------
__CGlobal.prototype.GetEvent=function(e,win)
{
	//::::::::::::::::::::::::::::: IE, NC, FF, Opera, Safari, IE(Mac)
	var ev = (e) ? e : (win==null?window.event:win.event);
	var k = (ev.keyCode) ? ev.keyCode : ev.which;
	var o = (ev.srcElement) ? ev.srcElement : ev.target;
	if (o.nodeType == 3) o = o.parentNode;	//...defeat Safari bug
	//::::::::::::::::::::::::::::: 
	var f=new Function();
	f.srcElement = o;
	f.keyCode = k;
	f.eventObject = ev;
	return f;
}
//-----------------------------------------------------------
__CGlobal.prototype.FireMouseEvent=function(c,eventName)
{
	//::::::::::::::::::::::::::::: IE
	if (c.fireEvent)
	{
		c.fireEvent("on"+eventName);
	}
	//::::::::::::::::::::::::::::: NC, FF
	else
	{
		var ev = document.createEvent("MouseEvents")
		ev.initEvent(eventName, true, true)
		c.dispatchEvent(ev);
	}
	//::::::::::::::::::::::::::::: Opera, Safari, IE(Mac) - ? need to find for these browsers
}
//-----------------------------------------------------------
__CGlobal.prototype.DoMouseClick=function(c)
{
	//::::::::::::::::::::::::::::: IE
	if (c.click)
	{
		c.click();
	}
	//::::::::::::::::::::::::::::: NC, FF
	else
	{
		var ev = document.createEvent("MouseEvents")
		ev.initEvent("click", true, true)
		c.dispatchEvent(ev);
	}
	//::::::::::::::::::::::::::::: Opera, Safari, IE(Mac) - ? need to find for these browsers
}

//---------------------
//--- AJAX - GetErrorAfterCallingWebService
//---------------------
__CGlobal.prototype.GetAJAXErrorDetails=function(err)
{
	var stackTrace		= err.get_stackTrace();
	var message			= err.get_message();
	var statusCode		= err.get_statusCode();
	var exceptionType	= err.get_exceptionType();
	var timedout		= err.get_timedOut();

	// Display the error.    
	var msg =
		"Error Message: " + message + "\n" +
		"Status Code: " + statusCode + "\n" +
		"Exception Type: " + exceptionType + "\n" +
		"Timedout: " + timedout + "\n-------------------------\n" +
		"Stack Trace: " +  stackTrace;
	return msg;
}
//-------------------------------------------------
//--- GetChildElementsByTagName
//-------------------------------------------------
__CGlobal.prototype.GetChildElementsByTagName=function(o,tagName)
{
	var a =o.childNodes;
	var a2=new Array();
	var tnU=tagName.toUpperCase();
	for(var j=0; j<a.length; j++)
	{
		var c=a[j];
		var tn=c.tagName; 
		if(tn!=null && tn.toUpperCase()==tnU)
		{
			a2.push(c);
		}
	}
	return a2;
}
//-------------------------------------------------
//--- OpenWindow_Properties
//-------------------------------------------------
__CGlobal.prototype.OpenWindow_Properties = function( _caller, _control, _callback, _width, _height, _title )
{
	var t = _control.getAttribute("c_type");
	var title = _title!=null ? _title : ("<strong>Set Properties for <span style='color:#660000'>\"" + t + "\"</span></strong>");
	var args = new Function();
	args.obj = _caller;
	args.control = _control;
	args.response = null;
	args.callback = _callback;
	var url = "/Panels/PropertiesGrid/default.aspx?ct=" + t;
	return g_global.OpenWindow(url,args,_width,_height,title,
		Telerik.Web.UI.WindowBehaviors.Move + Telerik.Web.UI.WindowBehaviors.Close + Telerik.Web.UI.WindowBehaviors.Resize);

//	var win = radopen();
//	args.window = win;
//	win.set_behaviors(Telerik.Web.UI.WindowBehaviors.Move + Telerik.Web.UI.WindowBehaviors.Close + Telerik.Web.UI.WindowBehaviors.Resize);
//	win.argument = args;
//	win.set_title(title);
//	win.setUrl(url);
//	win.setSize(_width,_height);
//	win.center();
//	return win;
}





//=============================================================
//=============================================================
//=============================================================
//===							===============================
//=== __CCookies, g_cookies		===============================
//===							===============================
//=============================================================
//=============================================================
//=============================================================
var g_cookies = new __CCookies();
function __CCookies()
{
}
__CCookies.prototype.Set=function(sName,sValue)
{
  //date = Date.parse("01/01/2100");
  date = new Date(2100,1,1);// Date.parse("Fri, 31 Dec 2100 23:59:59 GMT");
  document.cookie = sName + "=" + escape(sValue) + "; expires=" + date.toString();
};
__CCookies.prototype.Get=function(sName)
{
	// cookies are separated by semicolons
	var aCookie = document.cookie.split("; ");
	for (var i=0; i < aCookie.length; i++)
	{
		// a name/value pair (a crumb) is separated by an equal sign
		var aCrumb = aCookie[i].split("=");
		if (sName == aCrumb[0]) 
			return unescape(aCrumb[1]);
	}
	// a cookie with the requested name does not exist
	return null;
};
__CCookies.prototype.Delete=function(sName)
{
	document.cookie = sName + "=; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
};
