﻿getViewPortWidth();
getViewPortHeight();

/**
 * Hide or Show Div
 * 
 * @author Raj Gohil
 */
function setVisibility(id, visibility)
{
	var pnl = document.getElementById(id);
	if(pnl != null)
	{
		pnl.style.display = visibility;
	}
	
	
	pnl = null;
	pnl = parent.document.getElementById(id);
	if(pnl != null)
	{
		pnl.style.display = visibility;
	}
}

/**
 * Browser Body Size
 * 
 * @author Raj Gohil
 */
function getViewPortWidth()
{
	var viewportwidth;
	
	// Standards browsers (mozilla/netscape/opera/IE7+) use window.innerWidth
	if (typeof(window.innerWidth) != 'undefined')
	{
	     viewportwidth = window.innerWidth;
	}

	// IE6 in standards compliant mode
	else if (typeof(document.documentElement) != 'undefined'  && typeof(document.documentElement.clientWidth) !='undefined' && document.documentElement.clientWidth != 0)
	{
	      viewportwidth = document.documentElement.clientWidth;
	}
	// Older versions of IE
	//else
	//{
	//      viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
	//}
	document.cookie = 'Width=' + viewportwidth;
}

/**
 * Browser Body Size
 * 
 * @author Raj Gohil
 */
function getViewPortHeight()
{
	var viewportheight;

	// Standards browsers (mozilla/netscape/opera/IE7+) use window.innerHeight
	if (typeof(window.innerHeight) != 'undefined')
	{
	     viewportheight = window.innerHeight;
	}

	// IE6 in standards compliant mode
	else if (typeof(document.documentElement) != 'undefined'  && typeof(document.documentElement.clientWidth) !='undefined' && document.documentElement.clientWidth != 0)
	{
	      viewportheight = document.documentElement.clientHeight;
	}
	// Older versions of IE
	//else
	//{
	 //     viewportheight = document.getElementsByTagName('body')[0].clientHeight;
	//}
	document.cookie = 'Height=' + viewportheight;
}


/**
 * Read Cokkie by Name
 * 
 * @author Raj Gohil
 */
function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for ( var i = 0; i < ca.length; i++)
	{
		var c = ca[i];
		while (c.charAt(0) == ' ')
			c = c.substring(1, c.length);
		if(c.indexOf(nameEQ) == 0)
			return c.substring(nameEQ.length, c.length);
	}
	return "&nbsp;";
}

/**
 * Open Modal Window or Borwser Window based on parameters
 * 
 * @author Raj Gohil
 */
function OpenWin(url, width, height, mode, formName, hideCloseButton, subLevel, modalTitle, hideTitleBar, modalDialog, modalDraggable, modalResizable, modalPosition, hideLodingIndicator)
{
		url = typeof (url) != 'undefined' ? url : '';
		width = typeof (width) != 'undefined' ? width : 0;
		height = typeof (height) != 'undefined' ? height : 0;
		mode = typeof (mode) != 'undefined' ? mode : '';
		formName = typeof (formName) != 'undefined' ? formName : '';
		
		hideCloseButton = typeof (hideCloseButton) != 'undefined' ? hideCloseButton : true;
		// hideCloseButton = typeof (hideCloseButton) != 'undefined' ? hideCloseButton : false;
		subLevel = typeof (subLevel) != 'undefined' ? subLevel : 0;
		modalTitle = typeof (modalTitle) != 'undefined' ? modalTitle : '';
		hideTitleBar = typeof (hideTitleBar) != 'undefined' ? hideTitleBar : false;
		
		if (hideTitleBar == true)
		{
			hideCloseButton = true;
		}
		modalDialog = typeof (modalDialog) != 'undefined' ? modalDialog : true;
		modalDraggable = typeof (modalDraggable) != 'undefined' ? modalDraggable : true;
		modalResizable = typeof (modalResizable) != 'undefined' ? modalResizable : false;
		modalPosition = typeof (modalPosition) != 'undefined' ? modalPosition : 'center';
		hideLodingIndicator = typeof (hideLodingIndicator) != 'undefined' ? hideLodingIndicator : false;
		
		if (width == 0)
		{
			width = parseInt(readCookie('Width')) - 60;
		}
		
		if (height == 0)
		{
		    height = parseInt(readCookie('Height')) - 60;
		}
		
		if (url != '')
		{
			var _dialogId = '';
			var _dialog_Response = '';
			var _dialog_Iframe = '';
			var _dialog_LoadingIndicator = '';
			
			if (subLevel == 1)
			{
				_dialogId = window.parent.$("#" + subLevel + "_modalDiv");
				_dialog_Response = window.parent.$("#" + subLevel + "_modalResponse");
				_dialog_Iframe = window.parent.$("#" + subLevel + "_modalIFrame");
				_dialog_LoadingIndicator = window.parent.$("#" + subLevel + "_divLodingModalIndicator");
				_dialog_ParentBody =  window.parent.$('body');
				
				_dialog_TitleBar = window.parent.$(".ui-dialog-titlebar");
				_dialog_TitleBarCloseButton =  window.parent.$(".ui-dialog-titlebar-close");
				_dialog_overlay =  window.parent.$('.ui-widget-overlay');
			}
			else if (subLevel == 2) 
			{
				_dialogId = window.parent.parent.$("#" + subLevel + "_modalDiv");
				_dialog_Response = window.parent.parent.$("#" + subLevel + "_modalResponse");
				_dialog_Iframe = window.parent.parent.$("#" + subLevel + "_modalIFrame");
				_dialog_LoadingIndicator = window.parent.parent.$("#" + subLevel + "_divLodingModalIndicator");
				_dialog_ParentBody =  window.parent.parent.$('body');
				
				_dialog_TitleBar = window.parent.parent.$(".ui-dialog-titlebar");
				_dialog_TitleBarCloseButton =  window.parent.parent.$(".ui-dialog-titlebar-close");
				_dialog_overlay =  window.parent.parent.$('.ui-widget-overlay');
			}
			else if (subLevel == 2.1) 
			{
				subLevel = 2;
				
				_dialogId = window.parent.$("#" + subLevel + "_modalDiv");
				_dialog_Response = window.parent.$("#" + subLevel + "_modalResponse");
				_dialog_Iframe = window.parent.$("#" + subLevel + "_modalIFrame");
				_dialog_LoadingIndicator = window.parent.$("#" + subLevel + "_divLodingModalIndicator");
				_dialog_ParentBody =  window.parent.$('body');
				_dialog_TitleBar = window.parent.$(".ui-dialog-titlebar");
				_dialog_TitleBarCloseButton =  window.parent.$(".ui-dialog-titlebar-close");
				_dialog_overlay =  window.parent.$('.ui-widget-overlay');
			}
			else if (subLevel == 3) 
			{
				_dialogId = window.parent.parent.parent.$("#" + subLevel + "_modalDiv");
				_dialog_Response = window.parent.parent.parent.$("#" + subLevel + "_modalResponse");
				_dialog_Iframe = window.parent.parent.parent.$("#" + subLevel + "_modalIFrame");
				_dialog_LoadingIndicator = window.parent.parent.parent.$("#" + subLevel + "_divLodingModalIndicator");
				_dialog_ParentBody =  window.parent.parent.parent.$('body');
				
				_dialog_TitleBar = window.parent.parent.parent.$(".ui-dialog-titlebar");
				_dialog_TitleBarCloseButton =  window.parent.parent.parent.$(".ui-dialog-titlebar-close");
				_dialog_overlay =  window.parent.parent.parent.$('.ui-widget-overlay');
			}else
			{
				_dialogId = window.$("#" + subLevel + "_modalDiv");
				_dialog_Response = window.$("#" + subLevel + "_modalResponse");
				_dialog_Iframe = window.$("#" + subLevel + "_modalIFrame");
				_dialog_LoadingIndicator = window.$("#" + subLevel + "_divLodingModalIndicator");
				_dialog_ParentBody =  window.$('body');
				
				_dialog_TitleBar = window.$(".ui-dialog-titlebar");
				_dialog_TitleBarCloseButton =  window.$(".ui-dialog-titlebar-close");
				_dialog_overlay =  window.$('.ui-widget-overlay');
			}
			
			_dialog_Response.val("");
			_dialog_LoadingIndicator.height =height;
			_dialog_LoadingIndicator.width =width;
	
			if (_dialogId != null)
			{
				var returnValue = '';
				
				_dialogId.dialog(
				{
					autoOpen : true,
					modal : modalDialog,
					width : width,
					height : height,
					resizable : modalResizable,
					title : modalTitle,
					algin : 'center',
					draggable : modalDraggable,
					closeOnEscape : false,
					position :
					[
						modalPosition, 
					],
					open : function(event, ui)
					{
						// Show Progress Bar
						if (hideLodingIndicator == false)
						{
							_dialog_LoadingIndicator.show();
						}
						
						if (hideTitleBar == true)
						{
							// Hide Title
							_dialog_TitleBar.hide();
						} else
						{
							// Show Title
							_dialog_TitleBar.show();
						}
						
						if (hideCloseButton == true)
						{
							// Hide Close Button
							_dialog_TitleBarCloseButton.hide();
						} else
						{
							// Show Close Button
							_dialog_TitleBarCloseButton.show();
						}
						
						// Open Dialog Box
						_dialogId.dialog("open");
						
						// Set Iframe Visibility
						_dialog_Iframe.css('display', 'inline');
						
						// Set Iframe URL
						_dialog_Iframe.attr('src', url);
						
						// Bind Onload Event
						_dialog_Iframe.bind('load', function()
						{
							// Hide Progress Bar
							if (hideLodingIndicator == false)
							{
								_dialog_LoadingIndicator.hide();
							}
						});
						
						_dialog_ParentBody.css('overflow', 'hidden');
						/* Scrollbar fix */
						_dialog_overlay.css('width', '100%');
						
					},
					beforeClose : function(event, ui)
					{
						// Get Value from modalResponse hidden input value
						returnValue = _dialog_Response.val();
						
						if (formName != '' && returnValue != '')
						{
							//RefreshControl(formName, returnValue, strbtnClicked, url);
						}
						
						// On Window Close set Iframe URL = ''
						_dialog_Iframe.attr('src', '');
						
						// Unbind load event
						_dialog_Iframe.unbind('load');
						
						// Hide iframe
						_dialog_Iframe.css('display', 'none');
					},
					close : function(event, ui)
					{
						_dialog_ParentBody.css('overflow', 'auto');
						_dialog_LoadingIndicator.hide();
						
						// Clear Value
						_dialog_Response.val("");
					
						// Only apply to Sub Modal
						if (subLevel > 1)
						{
							// Checking is SubDialogResponse function exist
							if (window.SubDialogResponse)
							{
								//SubDialogResponse(returnValue);
							}
						}
					}
				});
			} else
			{
				alert('Error: Dialog undefined.');
			}
		} else
		{
			alert('Error: Dialog box URL and Title is required.');
		}
}
