//Copyright (c) 2000 Microsoft Corporation.  All rights reserved.
//<script> trick VID into colorization
var m_gobjXMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
var L_rgDayNamesShort               = L_sdShortDayNames_Text.split(";");
var L_rgDayNamesLong                = L_sdLongDayNames_Text.split(";");
var L_rgMonthNamesShort             = L_sdShortMonths_Text.split(";");
var L_rgMonthNamesLong              = L_sdLongMonths_Text.split(";");
var L_rgDayNamesLetter              = L_sdDayLetters_Text.split(";");
var L_rgszDayLettersMonthlyView     = L_sdDayLettersMonthlyView_Text.split(";");

var L_rgszFlags = new Array();
L_rgszFlags[0]="";
L_rgszFlags[1]=L_FollowUp_Text;
L_rgszFlags[2]=L_ForYourInformation_Text;
L_rgszFlags[3]=L_Forward_Text;
L_rgszFlags[4]=L_NoResponseNecessary_Text;
L_rgszFlags[5]=L_Read_Text;
L_rgszFlags[6]=L_Reply_Text;
L_rgszFlags[6]=L_ReplyToAll_Text;
L_rgszFlags[6]=L_Review_Text;
var g_iPollingIneterval = 60; //calendar polling interval in seconds

function fVerifyTimeZone(itzOffsetJan, itzOffsetJul)
{
	var objDate = new Date();
	
	objDate.setUTCDate(1);
	objDate.setUTCMonth(0);
	
	if(objDate.getTimezoneOffset() != itzOffsetJan) return(false);
	
	objDate.setUTCDate(1);
	objDate.setUTCMonth(6);
	
	if(objDate.getTimezoneOffset() != itzOffsetJul) return(false);

	return true;
}

function AccessFld( obj )
{
	return ( null != obj.length ) ? obj[0] : obj;
}

function public_IsLeapYear(yr)
{
	if ((yr % 4 == 0) && (yr % 100 != 0) || (yr % 400 == 0))
		return 1;
	else
		return 0;
}

function public_daysInMonth(mo, yr)
{
	if (mo == 2)
		return 28 + public_IsLeapYear(yr);
	else
		return 30 + ((mo + (mo > 7)) % 2);
}

//returns szValue or null on fail
function public_getURLparam(szParam,szURL)
{
	var szRetVal=null;
	var iX = szURL.indexOf(szParam + "=");
	if ( iX != -1 )
	{    
		szRetVal = szURL.substring(iX + szParam.length + 1);
		iX = szRetVal.indexOf("&");
		if (iX > -1)
		{
			szRetVal = szRetVal.substring(0,iX);
		}
	}
	return(szRetVal);
}

//removes leading and trailing space characters
function Trim(szWhat) {
	return(szWhat.replace(/^\s*|\s*$/g, ""));
}

function openGal(szFieldNameTo,szFieldNameCc,szFieldNameBcc, objMessage) 
{
	if (szFieldNameTo != null)
	{
		var rgParams=new Array();

		rgParams["ToName"]   = szFieldNameTo;
		rgParams["CcName"]   = szFieldNameCc;
		rgParams["BccName"]  = szFieldNameBcc;
		rgParams["objMessage"] = objMessage;
		var vRetval = window.showModelessDialog(g_szUserBase+"?Cmd=dialog&template=dlg_gal", rgParams, "dialogHeight:525px;dialogWidth:500px;status:no;resizable:no;help:no;");
	}
	else
	{
		var vRetval = window.showModelessDialog(g_szUserBase+"?Cmd=dialog&template=dlg_gal", null, "dialogHeight:500px;dialogWidth:500px;status:no;resizable:no;help:no;");
	}    
}

var rgNonLocShortMonths = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
function TzDateToUTC(szTzDate) {
	var szDay   = szTzDate.substring(8,10);
	var szMonth = new Number(szTzDate.substring(5,7))-1;
		szMonth = rgNonLocShortMonths[szMonth];                 
	var szYear  = szTzDate.substring(0,4);
	var szHr    = szTzDate.substring(11,13);
	var szMin   = szTzDate.substring(14,16);
	var szSec   = szTzDate.substring(17,19);
	return(szDay + " " + szMonth + " " + szYear + " " +szHr+":"+szMin+":"+szSec+" UTC");
}


function getLocalDateFormated(fConvertToLocal,szFormat,iYr,iMon,iDate,iHr,iMin)
{
	var objD = new Date(iYr,iMon,iDate,iHr,iMin);
		   
	if (fConvertToLocal)
	{
		var iYear       = objD.getUTCFullYear();
		var szMonth     = objD.getUTCMonth()+1; //0 offset adjustment
		var szDate      = objD.getUTCDate();
		var szHours     = objD.getUTCHours();
		var szMinutes   = objD.getUTCMinutes();
	}
	else
	{
		var iYear       = objD.getFullYear();
		var szMonth     = objD.getMonth()+1; //0 offset adjustment
		var szDate      = objD.getDate();
		var szHours     = objD.getHours();
		var szMinutes   = objD.getMinutes();
	}   


	if (szMonth < 10)
	{
		szMonth =  "0"+szMonth;
	}   
	if (szDate < 10)
	{
		szDate =  "0"+szDate;
	}   
	if (szHours < 10)
	{
		szHours =  "0"+szHours;
	}   
	if (szMinutes < 10)
	{
		szMinutes =  "0"+szMinutes;
	}

	if (szFormat == "yy/mm/dd hh:mm:ss") 
	{
		var szDate1 = iYear+"/"+szMonth+"/"+szDate+" "+szHours+":"+szMinutes+":00";
	}
	else //assume XML format "yyyy-mm-ddThh:mm:ss.sssZ"
	{
		var szDate1 = iYear+"-"+szMonth+"-"+szDate+"T"+szHours+":"+szMinutes+":00.000Z";
	}
	
	return(szDate1);
}

//PARAM: szDtTime = "1999-02-18T02:00:00.000Z"
//RETURN: a date object adjusted from GMT to local time
function getLocalDateObjFromGMTTime(szDtTime)
{
		
	var szDay   = szDtTime.substring(8,10);
	var szMonth = Number(szDtTime.substring(5,7))-1;    
	var szYear  = szDtTime.substring(0,4);
	var szHr    = szDtTime.substring(11,13);
	var szMin   = szDtTime.substring(14,16);
	var szSec   = szDtTime.substring(17,19);

	var objD = new Date();
	objD.setUTCFullYear(szYear,szMonth,szDay);
	objD.setUTCHours(szHr,szMin);

	return(objD);
}

//Function to properly initialize the HTMLEdit control with a fully qualified document
//Paramter objHTMLEdit has to be a referenct to the HTML Edit object
function InitializeHTMLEditor(objHTMLEdit, bodyHTML)
{
	if(bodyHTML==null)
		bodyHTML="";
	objHTMLEdit.DocumentHTML    = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">' +
									 '<HTML><HEAD>'+ 
									 '<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">' +
									 '</HEAD><BODY><DIV>&nbsp;</DIV>' +
									  String(bodyHTML) +
									 '</BODY></HTML>';
	
	
}

function openNewWindow(fileName,windowName,theWidth,theHeight) {

	if (windowName == "newMessageWindow") 
	{
		//generate random window ID
		 windowName = new String(Math.round(Math.random() * 100000));
	}
	window.open(fileName,windowName,"toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=1,width="+theWidth+",height="+theHeight)
}

//**********************************************
//spews to a textarea
//use szWindowName="" for new window each time
//else will open and keep using the same window
//**********************************************
function spewDebugText(szWindowName,szSpew,szHeader,fAppend) {
	wDebug = window.open("",szWindowName); 
	dDebug = wDebug.document.body;
	if (szHeader!="") szHeader += "\r\n**********************\r\n";
	if (wDebug["idTextarea"]==null) 
		dDebug.innerHTML="<textarea id='idTextarea' style='width:100%;height:100%'></textarea>";
	if (fAppend) wDebug["idTextarea"].value = wDebug["idTextarea"].value + "\r\n"+szHeader + szSpew;
	else wDebug["idTextarea"].value = szHeader + szSpew;
}


function MoveMessage(szUserBase, szMessageURL, szExchWebVroot, szContentClass)
{
		var rgURLSplit;
		var szParentURL = szUserBase;
		var szXML = '<transferdata xmlns:a="DAV:">' + 
					 '<item>' + 
						'<objectname></objectname>' + 
						'<objectid></objectid>' +
						'<content-class></content-class>' + 
						'<droptype></droptype>' +
						'<a:href></a:href>' +
					'</item>' +     
				'</transferdata>';
			  
	   
		rgURLSplit = szMessageURL.split("/");
		var szName = rgURLSplit[rgURLSplit.length-1];
		var szDialog                        =   szUserBase+"?Cmd=dialog&template=dlg_movecopy"
		var szNewFolderDialog               =   szUserBase +"?Cmd=dialog&template=dlg_newfolder"                
   
		
		var objXML = new ActiveXObject("Microsoft.xmldom"); 
	
		objXML.loadXML(szXML);
		var node = objXML.selectSingleNode("transferdata/item/objectid");
		node.text = szMessageURL;
		node = objXML.selectSingleNode("transferdata/item/content-class");
		node.text = szContentClass;
		node = objXML.selectSingleNode("transferdata/item/a:href");
		node.text = szMessageURL;
		node = objXML.selectSingleNode("transferdata/item/objectname");
		node.text = szName;
		node = objXML.selectSingleNode("transferdata/item/droptype");
		node.text = "message";
			   
		
		var szDataString = objXML.xml;
		
		
		var rgParams = new Array();
		
		rgParams["g_Location"]  = szUserBase;
		rgParams["g_VirtualRoot"] = szExchWebVroot;
		rgParams["g_NewFolderDialogPath"] = szNewFolderDialog;   
		rgParams["g_MessageDataString"] = szDataString;
		rgParams["g_MoveCopyMode"] = "Move"; 
		
		rgParams["status"]="";
		
		var bMove = window.showModalDialog(szDialog,    rgParams,"dialogHeight:400px;dialogWidth:350px;status:no;resizable:no;help:no;");
		
		return(bMove);

}



function fnForceImagePaint()
{
	if (window.document.all.dirtyImage == null)
	{
		window.document.body.insertAdjacentHTML("beforeEnd","<div id='dirtyImage' style='display:none;'></div>");
	}
	window.document.all.dirtyImage.innerHTML = "<img style='' src='"+g_szVirtualRoot + "/img/send.gif'>";
	
}

function LaunchHelp()
{
	if(idHelpLinkText != null)
	{
		openNewWindow(idHelpLinkText.innerText, "OutlookWebAccessHelp", 500, 400)
	}
}

var m_szDeletedItemsURL;
function DeleteMessage(szURL, szRoot, iAction)
{
	if (iAction == DeleteMessage.iACT_NO_CONFIRM)
	{
		m_gobjXMLHTTP.open( "DELETE", szURL ,true);
		m_gobjXMLHTTP.onreadystatechange = event_OnDeleteCompleted;
		event_OnDeleteCompleted.bCloseWindow = true;
		m_gobjXMLHTTP.send();
	}       
	else if (iAction == DeleteMessage.iACT_NO_CONFIRM_SYNC)
	{
		m_gobjXMLHTTP.open( "DELETE", szURL ,false);
		m_gobjXMLHTTP.send();

		var iStatus = m_gobjXMLHTTP.status;
		var szStatus = m_gobjXMLHTTP.statusText;
		
		CheckStatus(iStatus, szStatus);
	}       
	else if (null == m_szDeletedItemsURL) //we need to get the url to deleted items
	{
		setDeletedItemsFolderAndDelete(szURL, szRoot, iAction);
	}
	else
	{
		var m_szLastVerb;
		var urlpath =szURL.split("?");
		var endURL = urlpath[0].split("/");
		
		//If not in deleted items folder then move
		if(urlpath[0].toUpperCase().indexOf(m_szDeletedItemsURL.toUpperCase()) == -1 && szRoot)
		{
	
				var szDestURL = m_szDeletedItemsURL + endURL[endURL.length-1];
				m_gobjXMLHTTP.open( "MOVE", urlpath[0], true);
				m_gobjXMLHTTP.setRequestHeader( "Destination:", szDestURL);
				m_gobjXMLHTTP.setRequestHeader( "allow-rename:", "t");  
				m_gobjXMLHTTP.setRequestHeader( "Overwrite:", "f");
				m_gobjXMLHTTP.setRequestHeader( "Translate:", "f");
				m_gobjXMLHTTP.onreadystatechange = event_OnDeleteCompleted;
				event_OnDeleteCompleted.bCloseWindow = (iAction != DeleteMessage.iACT_NO_CLOSE);
				m_gobjXMLHTTP.send();
		}
		else //in delted items folder so just delete
		{
			if(confirm(L_WarningDeletePermanent_Text))
			{
				m_gobjXMLHTTP.open( "DELETE", urlpath[0] ,false);
				m_gobjXMLHTTP.send();
				var iStatus = m_gobjXMLHTTP.status;
				var szStatus = m_gobjXMLHTTP.statusText;
				CheckStatus(iStatus, szStatus, (iAction != DeleteMessage.iACT_NO_CLOSE));
			}
		}
	}
	return;    
}
// Function properties
DeleteMessage.iACT_NO_CONFIRM = 1;          // Delete w/out confirmation dialog.
DeleteMessage.iACT_NO_CONFIRM_SYNC = 3;     // Delete synchronously w/ dialog - only used by ComposeMMMsg
DeleteMessage.iACT_NO_CLOSE = 4;            // Delete w/out closing window.

var m_gobjHTTP;
function setDeletedItemsFolderAndDelete(szURL, szSearchRoot, iAction)
{
	if (szSearchRoot != null)
	{
		m_gobjHTTP = new ActiveXObject("Microsoft.XMLHTTP");     
		var szPropfind ='<?xml version="1.0"?>' + 
						'<a:propfind xmlns:a="DAV:" xmlns:b="urn:schemas:httpmail:">' + 
						'<a:prop><b:deleteditems/>' + 
						'</></>';
		m_gobjHTTP.open( "PROPFIND", szSearchRoot,true);
		m_gobjHTTP.setRequestHeader( "Depth:", "0");
		m_gobjHTTP.setRequestHeader( "Content-type:", "text/xml");
		m_gobjHTTP.onreadystatechange = eventcallback_setDeletedItemsFoldersAndDelete;
		eventcallback_setDeletedItemsFoldersAndDelete.szRoot = szSearchRoot;
		eventcallback_setDeletedItemsFoldersAndDelete.szURL = szURL;
		eventcallback_setDeletedItemsFoldersAndDelete.iAction = iAction;
		m_gobjHTTP.send(szPropfind);
	}
	return;
}
function event_NOP()
{
	return;
}

function eventcallback_setDeletedItemsFoldersAndDelete()
{
	if(m_gobjHTTP.readyState == 4)
	{
		m_gobjHTTP.onreadystatechange = event_NOP;
		var xmlResponseXML = m_gobjHTTP.responseXML;
		var xmlNodes = xmlResponseXML.selectNodes("a:multistatus/a:response/a:propstat/a:prop/*");
		var szURLNode = xmlResponseXML.selectNodes("a:multistatus/a:response/a:href");
		var istatus = m_gobjHTTP.status;
		var i;
		m_gobjHTTP.abort();
		if (xmlNodes.length) //should be only one item returned
		{
			m_szDeletedItemsURL = xmlNodes.item(0).text + "/";
			DeleteMessage(eventcallback_setDeletedItemsFoldersAndDelete.szURL, eventcallback_setDeletedItemsFoldersAndDelete.szRoot, eventcallback_setDeletedItemsFoldersAndDelete.iAction);
		}
		else
		{
			if (401 == istatus) //status returned by store may be changed
			{
				alert(L_errDeletePermission_ErrorMessage);
			}
			else
			{
				alert(L_NoDelete);
			}
		}
	}

}
// Function properties
eventcallback_setDeletedItemsFoldersAndDelete.szURL;
eventcallback_setDeletedItemsFoldersAndDelete.szRoot;
eventcallback_setDeletedItemsFoldersAndDelete.iAction;

function event_OnDeleteCompleted()
{
	if (m_gobjXMLHTTP.readyState == 4) 
	{
		var iStatus = m_gobjXMLHTTP.status;
		var szStatus = m_gobjXMLHTTP.statusText;
		m_gobjXMLHTTP.abort();
		CheckStatus(iStatus, szStatus, event_OnDeleteCompleted.bCloseWindow);
	}
}
// Function properties
event_OnDeleteCompleted.bCloseWindow = true;

function CheckStatus(iStatus, szStatus, bCloseWindow)
{
	if (iStatus < 300) 
	{
		try
		{
			if(window.opener && window.opener.RefreshMessageList)
			{
				window.opener.RefreshMessageList();
			}
			else if (RefreshMessageList) //running from the viewer
			{
				RefreshMessageList();
			}
		}
		catch(e){}
		
		if (bCloseWindow == true)
		{
			self.close();  //Close window after deleting
		}
	}
	else 
	{
		switch(iStatus) 
		{
					
			case 401:   //Status returned by store may be changed
			case 403:
			  iLockCount = 0;
			  alert(L_errDeletePermission_ErrorMessage);
			  break;
			case 404:
			  alert(L_errDelteResourceNotFound_ErrorMessage);
			  break;
			default:
			  alert(iStatus + " " + szStatus);
			  break;
		}
	}       
}

function parseURLfor(szWhat)

{
	var re = new RegExp("[?|&]"+szWhat+"=([^&]*)","gi");
	var arr = re.exec( window.document.location.search );
	return( (arr==null)?null:arr[1]);
}

function WarnUser(level, url)
{
	var fOpen = true;
	
	if (level == 1)
		fOpen = confirm(L_warnOpenUnsafeContent_Text);
	
	if (fOpen)
	{
		window.open(url);
	}
	return(false);
}

function fixIE_width()
{
	var re = /MSIE 5\.([0-9]+)/;
	var rgArgs = clientInformation.appVersion.match(re);
	if( null != rgArgs && 1 > rgArgs[1] )
	{
		var j;
		for( j = 0; j < document.styleSheets.length; j++ )
		{
			if( -1 == document.styleSheets[j].href.indexOf( "olstyle.css" ) )
			{
				continue;
			}

			var rules = document.styleSheets[j].rules;
			var i;
			for( i = 0; i < rules.length; i++ )
			{
				if( ".MSGBODY" == rules[ i ].selectorText )
				{
					rules[i].style.marginRight = 12;
					break;
				}
			}
			break;
		}
	}
}

//returns all characters utf8 encoded
function toUTF8(szInput)
{
	var wch,x,uch="",szRet="";

	for (x=0; x<szInput.length; x++)
	{
		wch=szInput.charCodeAt(x);
		if (!(wch & 0xFF80))
		{
			szRet += "%" + wch.toString(16);
		}
		else if (!(wch & 0xF000))
		{
			uch = "%" + (wch>>6 | 0xC0).toString(16) + 
				  "%" + (wch & 0x3F | 0x80).toString(16);
			szRet += uch; 
		}
		else
		{
			uch = "%" + (wch >> 12 | 0xE0).toString(16) + 
				  "%" + (((wch >> 6) & 0x3F) | 0x80).toString(16) +
				  "%" + (wch & 0x3F | 0x80).toString(16);
			szRet += uch; 
		}
	}
	return(szRet);
}