﻿//Copyright WebMail Wou3, Inc. 2007-2010
var g_supressErrorMessages;

function toolbarMouseDown(obj) 
{
	//try{document.getElementById(obj.id).style.background='url(/images/bg-tab-press.gif) repeat-x right top';}
	try{obj.setAttribute('style','background:url(/images/bg-tab-press.gif) repeat-x right top');}
	catch(ex){}
}

function toolbarMouseUp(obj) 
{
	//tyy{document.getElementById(obj.id).style.background='url(/images/bg-tab.gif) repeat-x right top';}
	try{obj.setAttribute('style','background:url(/images/bg-tab.gif) repeat-x right top');}
	catch(ex){}
	//alert(document.getElementById(obj.id) + "\r\n" + obj.getAttribute("style") + "\r\n" + document.getElementById(obj.id).getAttribute("style") );
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////        
//the onError method used when a callback called from a compose object fails
//ReEnables all toolbars
/////////////////////////////////////////////////////////////////////////////////////////////////////////        
function composeOnError(error)
{
	page.get_EditControl(0)._ToolBarObject.enable();
	page.get_EditControl(1)._ToolBarObject.enable();
	page.get_EditControl(2)._ToolBarObject.enable();
	hideLoading();
	onError(error);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////        
//Generic error callback
/////////////////////////////////////////////////////////////////////////////////////////////////////////        
function onError(error) 
{   
	if(g_supressErrorMessages==true)
		return;
	if(error !== null) 
	{
		hideLoading();

		//System.InvalidOperationException means the session object can not be found so the callback is not allowed.
		//if((error.get_exceptionType()=='WebMail2.WebMethodTimeoutException') || (error.get_exceptionType()=='System.InvalidOperationException'))
		if(error.get_exceptionType()=='WebMail2.WebMethodTimeoutException')
		{
			g_supressErrorMessages=true;
			alert('Your session has timed out.  The page will reload');

			window.top.location='/default.aspx';
			return;
		}
		else if (( error.get_message().toLowerCase().indexOf('authentication') > 0) && (error.get_exceptionType()=='System.InvalidOperationException'))
		{
			page.set_ErrorMessage('You are no longer logged into WebMail');
			window.top.location='/default.aspx';
			return;
		}
		else 
		{
			clientPageMethods.checkServerForUpdates();

			var errorMessage='';
			var exceptionType='';
			var stackTrace='';
			try
			{
				if(error.get_message().length>0)
					errorMessage='       Error Message='  + error.get_message();
			}
			catch(e){}
			try
			{
				if(error.get_exceptionType().length>0)
					exceptionType='       ExceptionType=' + error.get_exceptionType();
			}
			catch(e){}
			try
			{
				if(error.get_stackTrace().length>0)
					stackTrace='       StackTrace=' + error.get_stackTrace();
			}
			catch(e){}
			
			try
			{clientPageMethods.writeErrorToEventLog('Unhandled error' + errorMessage + exceptionType + stackTrace);}
			catch(e){}
			//alert('Unhandled error' + errorMessage + exceptionType + stackTrace);
			try
			{page.set_ErrorMessage('Unhandled error' + errorMessage + exceptionType);}
			catch(e)
			{
				//alert('unhandled error.  If this problem persists, logout of webmail and log back in to reset the browser');
			}
		}
	}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////////////////
//catch error message display
//e is a handled exception.
/////////////////////////////////////////////////////////////////////////////////////////////////////////
function displayMessage(e)
{
	var errorMessage='';
		
	try 
	{
		if(e.name!='undefined')
			errorMessage='\r\nError Name:\t'  + e.name;
	}
	catch(e2){}
	try
	{
		if(e.description!='undefined')
			errorMessage += '\r\nError Description:\t'  + e.description;
	}
	catch(e2){}
	try
	{
		if(e.lineNumber!='undefined')
			errorMessage += '\r\nLine Number:\t'  + e.lineNumber;
	}
	catch(e2){}
	try
	{
		if(e.number!='undefined')
			errorMessage +='\r\nNumber:\t'  + e.number;
	}
	catch(e2){}
	try
	{
		if(e.filename!='undefined')
			errorMessage += '\r\nFilename:\t'  + e.filename;
	}
	catch(e2){}
	try
	{
		errorMessage += '\r\nException.ToString:\t'  + e.toString();
	}
	catch(e2){}
	
	if(errorMessage.length>2)
		errorMessage = errorMessage.substring(2);
	
	try
	{clientPageMethods.writeErrorToEventLog('Unhandled JavaScript error.' + errorMessage);}
	catch(e2){}
	
	try{
	page.set_ErrorMessage
	(
		'Unhandled Error.  Error Message='  + 
		e.description + 
		'<br/>If the problem persists, logout and log back into webmail to reset the browser'
	); 
	}catch(e2){}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////////////////
//dummy callback for page methods where no action is required.
function doNothing(results) {};
/////////////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////////////////
//checks the object clicked and determines if the uniqueID ends with the value passed in controlID
/////////////////////////////////////////////////////////////////////////////////////////////////////////
function addViewFileName(fileName)
{
	if((fileName==null) || (fileName==""))
		return -1;
	
	//fileName does not include the folder
	if(page.get_FolderPath().charAt(page.get_FolderPath().length - 1)=='\\')
		fileName=page.get_FolderPath() + fileName;
	else
		fileName=page.get_FolderPath() + '\\' + fileName;

	//first make sure the file is not already loaded.
	//if it is already loaded, returning a -1 tells the browser nothing needs to be refreshed
	//but the tab index is changed so the selected message is displayed.
	var i;
	for(i=0; i < g_objViewFileNames.length; i++)
	{
		if ( (g_objViewFileNames[i].value.length>0) &&  (g_objViewFileNames[i].value.toLowerCase()==fileName.toLowerCase()) )
		{
			g_objSelectedTabIndex.value=i + page.ViewFilesFirstTab;
			return -1;
		}
	}
	
	for(i=0; i < g_objViewFileNames.length; i++)
	{
		if (g_objViewFileNames[i].value.length==0)
		{
			g_objViewFileNames[i].value=fileName;
			g_objSelectedTabIndex.value=i + page.ViewFilesFirstTab;
			return i;
		}
	}
	
	g_objViewFileNames[0].value=fileName;
	g_objSelectedTabIndex.value=page.ViewFilesFirstTab;
	
	return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////

function objectIsNull(obj)
{
	if(obj==null)
		return true;
	if(obj=="undefined")
		return true;
	return false;
}

function getCheckBoxValue(uniqueID)
{
	try
	{
		if(uniqueID==null)
			return false;
	}
	catch(ex){return false;}
	var ctl=null;
	try{ctl=document.getElementById(uniqueID);}
	catch(ex){}
	if(ctl==null)
		return false;
		
	try{return ctl.checked;}
	catch(e){return false;}
}
function setCheckBoxValue(uniqueID, checked)
{
	if(uniqueID==null)
		return;
	var ctl=null;
	try{ctl=document.getElementById(uniqueID);}
	catch(ex){}
	if(ctl==null)
		return;
	try
	{
		if(ctl.checked!=checked)
			ctl.checked=checked;
	}
	catch(e){}
}
	
function setObjectValue (uniqueID, value)
{
	var ctl=document.getElementById(uniqueID);
	if(ctl!=null)
		ctl.value=value;
}

function getObjectValue (uniqueID)
{
	var ctl=document.getElementById(uniqueID);
	if(ctl!=null)
	{
		return ctl.value;
	}
	else
	{
		return "";
	}
}

function setObjectInnerHTML (uniqueID, value)
{
	var ctl=document.getElementById(uniqueID);
	if(ctl!=null)
	{
		try
		{
			if(value==null)
				value='&nbsp;';
			else if(value.trim().lengt==0)
				value='&nbsp;';
			else if(value.replace(' ','').length==0)
			{
				value='&nbsp;';
				ctl.innerText="";
			}
			ctl.innerHTML=value;
		}
		catch(e)
		{
			displayMessage(e);
			ctl.innerHTML='&nbsp;';
		}
	}
}

function getObjectInnerHTML (uniqueID)
{
	var ctl=document.getElementById(uniqueID);
	if(ctl!=null)
	{
		return ctl.innerHTML;
	}
	else
	{
		return "";
	}
}

function setTextboxReadOnly (uniqueID, readOnly)
{
	var ctl=document.getElementById(uniqueID);
	if(ctl!=null)
		ctl.readOnly=readOnly;
}

function setCheckBoxEnabled (uniqueID, enabled)
{
	var ctl=document.getElementById(uniqueID);
	if(ctl!=null)
		ctl.disabled=(enabled==false);
}

function get_ObjectIsVisible(uniqueID)
{
	var ctl=document.getElementById(uniqueID);
	if(ctl!=null)
		return ('visible'==ctl.style.visibility);
	return false;
}
function set_ObjectIsVisible(uniqueID, visible)
{
	var ctl=document.getElementById(uniqueID);
	if(ctl!=null)
	{
		if(true==visible)
		{
			if(ctl.style.visibility!='visible')
				ctl.style.visibility='visible';
			if(ctl.style.height=='0px')
				ctl.style.height='';
			if(ctl.style.width=='0px')
				ctl.style.width='';
			if(ctl.style.top!='')
				ctl.style.top='';
			if(ctl.style.left!='')
				ctl.style.left='';
			if(ctl.style.position=='absolute')
				ctl.style.position='static';
		}
		else
		{
			ctl.style.visibility='hidden';
			ctl.style.height='0px';
			ctl.style.width='0px';
			//ctl.style.top='-1000px';
			ctl.style.left='-1000px';
			ctl.style.position='absolute';
		}
	}
}

function set_ObjectIsDisplayed(uniqueID, visible)
{
	var ctl=document.getElementById(uniqueID);
	if(ctl!=null)
	{
		if(true==visible)
			ctl.style.display='block';
		else
			ctl.style.display='none';
	}
}
function get_ObjectIsDisplayed(uniqueID)
{
	var ctl=document.getElementById(uniqueID);
	if(ctl!=null)
		try{return ('none'!=ctl.style.display);}
		catch(e){return false;}
	return false;
}

function formatDisplayAs(contactDisplayAs,firstName,lastName,companyName)
{
	if((contactDisplayAs==null) || (contactDisplayAs.replace(' ','').length==0))
		return '';
		
	if((firstName!=null) && (firstName.replace(' ','').length==0))
		firstName=null;
	if((lastName!=null) && (lastName.replace(' ','').length==0))
		lastName=null;
	if((companyName!=null) && (companyName.replace(' ','').length==0))
		companyName=null;


	
	//if company, last name, first name all have no value, display as is not used
	//or if the default format is email address only, display as is not used
	if ((firstName==null) && (lastName==null) && (companyName==null))
	{
		return '';
	}
	else
	{
		if(companyName==null)
		{
			contactDisplayAs=contactDisplayAs.replace('({company})','');
			contactDisplayAs=contactDisplayAs.replace('{company}','');
		}
		else
		{
			contactDisplayAs=contactDisplayAs.replace('{company}',companyName);
		}
			
		if(lastName==null)
		{
			contactDisplayAs=contactDisplayAs.replace(' {last}','');
			contactDisplayAs=contactDisplayAs.replace('{last} ','');
			contactDisplayAs=contactDisplayAs.replace('{last}, ','');
			contactDisplayAs=contactDisplayAs.replace('{last},','');
			contactDisplayAs=contactDisplayAs.replace('{last}','');
		}
		else
		{
			contactDisplayAs=contactDisplayAs.replace('{last}',lastName);
		}
			
		if(firstName==null)
		{
			contactDisplayAs=contactDisplayAs.replace('{first} ','');
			contactDisplayAs=contactDisplayAs.replace(',{first}','');
			contactDisplayAs=contactDisplayAs.replace(', {first}','');
			contactDisplayAs=contactDisplayAs.replace(' {first}','');
			contactDisplayAs=contactDisplayAs.replace('{first}','');
		}
		else
		{
			contactDisplayAs=contactDisplayAs.replace('{first}',firstName);
		}
	}
	return contactDisplayAs;
}

function showMessageListContextMenu(evt)
{
evt.returnValue = false;
evt.cancelBubble = true;
var obj=document.getElementById('calendarContextMenu');
obj.style.display='none';

if(page.Nav.PageNo()==page.Nav.LastPage())
	document.getElementById('listContextMenu4').style.display='none';
else
	document.getElementById('listContextMenu4').style.display='block';
if(page.Nav.PageNo()==1)
	document.getElementById('listContextMenu3').style.display='none';
else
	document.getElementById('listContextMenu3').style.display='block';

obj=document.getElementById('listContextMenu');
obj.style.left=evt.clientX + "px";
obj.style.top=evt.clientY + "px";
obj.style.display='block';

}

function showContactListContextMenu(evt)
{
var obj=document.getElementById('calendarContextMenu');
obj.style.display='none';

document.getElementById('listContextMenu4').style.display='none';
document.getElementById('listContextMenu3').style.display='none';

obj=document.getElementById('listContextMenu');
obj.style.left=evt.clientX + "px";
obj.style.top=evt.clientY + "px";
obj.style.display='block';

evt.returnValue = false;
evt.cancelBubble = true;
}

function hideListContextMenu()
{
	try{document.getElementById('listContextMenu').style.display='none';}
	catch(e){}
}

function showCalendarContextMenu(evt)
{
var obj=document.getElementById('listContextMenu');
obj.style.display='none';

obj=document.getElementById('calendarContextMenu');
obj.style.left=evt.clientX + "px";
obj.style.top=evt.clientY + "px";
obj.style.display='block';

evt.returnValue = false;
evt.cancelBubble = true;
}

function hideCalendarContextMenu()
{
	try{document.getElementById('calendarContextMenu').style.display='none';}
	catch(e){}
}

function setSelectedTab()
{
	try{page.setSelectedTabIndex(Number(g_objSelectedTabIndex.value));}
	catch (e){}
}


function SetRows(inputObj)
{
  var elem = document.getElementById(inputObj.id.toString().replace('_tdDescription','_trDetails'));
  ShowRow(elem);
  return true;
	
  if(!ShowRow(elem))
  {HideRow(elem);}
  return true;
}

function ShowRow(inputObj)
{
  if(inputObj.style.visibility!='visible')
  {
    inputObj.style.visibility='visible';
    inputObj.style.position='static';
    inputObj.style.height='';
		
    return true;
  }
  else
  {
    HideRow(inputObj);
    return false;	
  }

}

function HideRow(inputObj)
{
  inputObj.style.visibility='hidden';
  inputObj.style.position='absolute';
  inputObj.style.height='0px';
}

function SetHover(hoverObj)
{
  hoverObj.style.fontWeight='bold';
  hoverObj.style.cursor='hand';
  hoverObj.style.textDecoration='none';
  return true;
}

function SetUnHover(hoverObj)
{
  hoverObj.style.fontWeight='';
  hoverObj.style.cursor='';
  hoverObj.style.textDecoration='';
  return true;
}