﻿// JScript File

var debug = true;

function Debug(func, e)
{
	if (debug == true)
	{
		if (e != null)
			alert('Error in ' + func + ' (' + e.name + '): ' + e.message);
		else
			alert('Error');
	}
}

function GetById(id)
{
	try
	{
		return $get(id);

		if (document.getElementById)
			return document.getElementById(id);
		else if (document.all)
			result = document.all[id];
		else if (document.layers)
			result = document.layers[id];
	}
	catch(e)
	{ Debug('GetById', e); }

	return null; 
}

function Click(id)
{
	//alert('Click(' + id + ')');
	try
	{
		var eClick = GetById(id);
		if (eClick != null)
		{
			if (eClick.click)
				eClick.click();
			else 
			{
				if (eClick.onclick)
					eClick.onclick();
				if (eClick.href)
					document.location = eClick.href;
			}
		}
	}
	catch(e)
	{ Debug('Click', e); }
}

function ClickAndSetFocus(clickId, focusId)
{
	try
	{
		Click(clickId);
	
		SetFocus(focusId);
	}
	catch(e)
	{ Debug('ClickAndFocus', e); }
}

function KeyPressed(e, keyCode)
{
	var e = e || window.event;
	if (e != null)
	{
		var code = e.keyCode || e.which;
		return (code == keyCode);
	}
	return false;
}
/*
function KeyPressed(e, charCode)
{
	var code = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
	
	return code == charCode;
}*/

function EnterPressed(e)
{
	return KeyPressed(e, 13);
}

function EscapePressed(e)
{
	return KeyPressed(e, 27);
}

function SetFocus(id)
{
	try
	{
		var element = GetById(id);	
		if (element != null)					
			element.focus();
	}
	catch(e)
	{ Debug('SetFocus', e); }
}

function ToggleVisibility(id)
{
	try
	{
		var element = GetById(id);		
		if (element != null)
		{		
			element.style.display = ((element.style.display=='none') ? '' : 'none');
			return (element.style.display != 'none');
		}
	}
	catch(e)
	{ Debug('ToggleVisibility', e); }
	return false;
}
function SetVisibility(id, visible)
{
	var element = GetById(id);		
	if (element != null)
		element.style.display = ((visible == true) ? '' : 'none');
}

function ScrollToElement(id)
{
	var element = GetById(id);
	if (element != null)
		element.scrollIntoView();
}

function ShowModalPopup(id)
{
	try
	{
		/*var element = GetById(id); //$find(behaviourId);
		if (element != null)
			element.show();*/
		ToggleVisibility(id);
	}
	catch(e)
	{ Debug('ShowModalPopup', e); }
}
/*
window.document.onkeypress = function ()
{

 if (KeyPressed(window.event, 13))
 {
  // Cancel the keystroke completely
  window.event.returnValue = false;
  window.event.cancel = true;

  return false;
 }

 return true;
}*/

function SetElementContent(id, content)
{
	var element = GetById(id);
	if (element != null)
		element.innerHTML = content;
}

function AppendElementContent(id, content)
{
	var element = GetById(id);
	if (element != null)
		element.innerHTML = element.innerHTML + content;
}

function GetWidth(element)
{
	var result = 0;
	if (element != null)
	{
		if (element.offsetWidth)
			result = element.offsetWidth;
		else if (element.scrollWidth)
			result = element.scrollWidth;
		else if (element.clientwidth)
			result = element.clientwidth;
			
		//alert('width: ' + element.offsetWidth);
	}
	
	return result;
}

function GetHeight(element)
{
	var result = 0;
	if (element != null)
	{
		if (element.offsetHeight)
			result = element.offsetHeight;
		else if (element.scrollHeight)
			result = element.scrollHeight;
		else if (element.clientHeight)
			result = element.clientHeight;
			
		//alert('width: ' + element.offsetWidth);
	}
	
	return result;
}

function GetWindowWidth()
{		
	/*if (window.innerWidth)
		return window.innerWidth;
	/*if (document.documentElement.clientWidth)
		return document.documentElement.clientWidth;
	if (document.body.clientWidth)
		return document.body.clientWidth;
	if (document.body.offsetWidth)
		return document.body.offsetWidth;*/
		
	switch(Sys.Browser.agent) 
	{
      case Sys.Browser.InternetExplorer:
          return document.documentElement.clientWidth;
          break;
      case Sys.Browser.Safari:
          return window.innerWidth;
          break;
      case Sys.Browser.Opera:
          return Math.min(window.innerWidth, document.body.clientWidth);
          break;
      default:  // Others
          return Math.min(window.innerWidth, document.documentElement.clientWidth);
          break;
  }
			
	return 0;
}

function GetWindowHeight()
{
	/*if (window.innerHeight)
		return window.innerHeight;
	/*if (document.documentElement.clientHeight)
		return document.documentElement.clientHeight;
	if (document.body.clientHeight)
		return document.body.clientHeight;
	if (document.body.offsetHeight)
		return document.body.offsetHeight;*/
		
	switch(Sys.Browser.agent) 
	{
      case Sys.Browser.InternetExplorer:
          return document.documentElement.clientHeight;
          break;
      case Sys.Browser.Safari:
          return window.innerHeight;
          break;
      case Sys.Browser.Opera:
          return Math.min(window.innerHeight, document.body.clientHeight);
          break;
      default:  // Others
          return Math.min(window.innerHeight, document.documentElement.clientHeight);
          break;
  }
			
	return 0;
}

function GetPageWidth()
{
	var clientWidth = GetWindowWidth();
	return Math.max(Math.max(document.documentElement.scrollWidth, document.body.scrollWidth), clientWidth);
}
function GetPageHeight()
{
	var clientHeight = GetWindowHeight();
	return Math.max(Math.max(document.documentElement.scrollHeight, document.body.scrollHeight), clientHeight);
}

function GetScrollX()
{
	return 0; 
		
	if (window.scrollX)
		return window.scrollX;
	return 0;
}
function GetScrollY()
{
	return 0;
	
	return window.scrollTop();
	
	if (window.scrollY)
		return window.scrollY;
	return 0;
}

/* Modal Dialog */

function LayoutDialog(id)
{
	var e = GetById(id);
	
	if (e != null)
	{
		e.style.position = 'absolute';
		
		// window.innerWidth, window.innerHeight
		e.style.left = GetScrollX() + ((GetWindowWidth()/ 2) - (GetWidth(e) / 2)) + 'px';
		e.style.top = GetScrollY() + ((GetWindowHeight() / 2) - (GetHeight(e) / 2)) + 'px';
  }
}
function LayoutBackground(id)
{
	var bgE = GetById(id);
	if (bgE != null)
	{
		bgE.style.position = 'absolute';
		
		// window.innerWidth, window.innerHeight
		bgE.style.left = GetScrollX() + 'px';
		bgE.style.top = GetScrollY() + 'px';
		bgE.style.width = GetPageWidth();
		bgE.style.height = GetPageHeight();
  }
}

function ToggleModalDialog(dialogid, backgroundid)
{
	ToggleModalDialog(dialogid, backgroundid, '');
}
function ToggleModalDialog(dialogid, backgroundid, focusid)
{
	var backVisible = ToggleVisibility(backgroundid);
	var dialogVisible = ToggleVisibility(dialogid);
	
	if (focusid != '' && dialogVisible)
		SetFocus(focusid);
	
	if (dialogVisible)
	{
		LayoutDialog(dialogid);
		// scroll back to top
		scroll(0,0);
	}
	if (backVisible)
		LayoutBackground(backgroundid);
}
function HideModalDialog(dialogid, backgroundid)
{
	SetVisibility(backgroundid, false);
	SetVisibility(dialogid, false);
}
function ShowModalDialog(dialogid, backgroundid)
{
	SetVisibility(backgroundid, true);
	SetVisibility(dialogid, true);
}

function Validate(elementid)
{
//	var element = $('#' + elementid);
//	if (element != null)
//	{
//		var value = element.val();
//		return jQuery.validator.email(value, $('#' + elementid));
//	}
//	return false;
	return $("#form1").validate().element("#" + elementid);
}

function ValidateRadio(RadioID, ErrorText)
{
	var e = $('input[@name="' + RadioID + '"]:checked');
	var checked = false;
	if (e != null)
		if (e.val() != '') 
			checked = true;
			
	if (!checked)
		alert(ErrorText);
		
	return checked;
}

/* Dynamic News */
var __NT_Personal_View_ClientId = '';
function NT_Personal_GetAddBlockColumn()
{
	var column = 0;var colId = 0;var minimum;
	$('#' + __NT_Personal_View_ClientId + '_Container .Personal-View-Column').each(function(){
		var count = jQuery_Count($(this).find('.Sortable-Item'));
		if ((typeof(minimum)=='undefined') || (count < minimum))
		{
			minimum = count;
			column = colId;
		}
		colId++;
		});
	return column;
}

function NT_Personal_GetBlock(code)
{
	if (code != '')
		PageMethods.Personal_GetBlock(__NT_Personal_View_ClientId, code, NT_Personal_GetBlock_OK, NT_Personal_GetBlock_Fail);
}

function NT_Personal_GetBlock_OK(result)
{
	//NT_Personal_Actions_CloseDialogs();
	var columnId = NT_Personal_GetAddBlockColumn();
	$('#' + __NT_Personal_View_ClientId + '_List_' + columnId + ' .Personal-View-Column-Actions').after(result);
}

function NT_Personal_GetBlock_Fail(error)
{}

/* jQuery */
function jQuery_Count(elements)
{
	var count = 0;
	elements.each(function(){count++});
	return count;
}

function jQuery_CheckToBool(val)
{
	return (val=='on');
}
function jQuery_CheckBoxToBool(id)
{
	return jQuery('#'+id).is(':checked');
}