var FIELD_NOT_REQUIRED	= 0;
var FIELD_RECOMMENDED	= 1;
var FIELD_REQUIRED		= 2;

var _iWindowX = 720;
var _iWindowY = 580;

function eventTrigger (e)
{
	if (! e)
	{
		e = event;
	}	
	return e.target || e.srcElement;
}

function getHtmlObj(oDoc, sId)
{
	if (!oDoc.getElementById)
	{
	   return oDoc.all[sId];
	}
	else
	{
	   return oDoc.getElementById(sId);
	}
}

function getHtmlObjArray(oDoc, sId)
{
	if (!oDoc.getElementsByTagName)
	{
	   return oDoc.all.tags(sId);
	}
	else
	{
	   return oDoc.getElementsByTagName(sId);
	}
}

function getHtmlObjAll(oDoc)
{
	if (!oDoc.getElementsByTagName)
	{
	   return oDoc.all;
	}
	else
	{
	   return oDoc.getElementsByTagName("*");
	}
}

document.onkeydown = function(){document_onkeypress();}

function document_onkeypress()
{
	var event = window.event;
	if (event.ctrlKey && event.keyCode == 80)
	{
		event.keyCode = 0;
		return false;
	}
}

function handleReturnKey()
{
	if (event.keyCode == 13)
	{
		eventTrigger(event).click();
		event.cancelBubble = true;
		return false;
	}
}

function createRelatedUrl(sType, id)
{
	var url;
	switch (sType)
	{
		case "51":
			if (!IsNull(id))
			{
				url = CURRENT_SECURE_SERVER + "/related/relatedDialog";
			}
			else
			{
				url = "/related/relatedDialog";
			}
			break;
		default:
			url = "/related/relatedDialog";
			break;
	}
		
	url += ".aspx?id=";
	if (!IsNull(id))
	{
		url += id;
	}
	url += "&entitytype=" + sType;
	return url;
}

function createFormUrl(sId, sType)
{
	var sUrl;
	switch (sType)
	{
		case "1006":
			sUrl = "/pages/OperatingHoursPage.aspx?id=" + sId;
			sUrl += "&entitytype=65&virtualEntitytype=" + sType;
			break;
		case "32":
			sUrl = "/images/imageDialog.aspx?id=" + sId;
			break;
		case "89":
		case "64":
			sUrl = "/pages/HolidayPage.aspx?id=" + sId;
			sUrl += "&entitytype=" + sType;
			break;
		case "1002":
			sUrl = "/pages/WeeklyWorkDaysPage.aspx?id=" + sId;
			sUrl += "&entitytype=59&virtualEntitytype=" + sType;
			break;
		case "1001":
			sUrl = "/pages/SingleWorkDayPage.aspx?id=" + sId;
			sUrl += "&entitytype=59&virtualEntitytype=" + sType;
			break;
		case "51":
			sUrl = CURRENT_SECURE_SERVER + "/pages/EntityPage.aspx?id=" + sId;
			sUrl += "&entitytype=" + sType;
			break;
		default:
			sUrl = "/pages/EntityPage.aspx?id=" + sId;
			sUrl += "&entitytype=" + sType;
			break;
	}
	return sUrl;
}

function entityFormHelp(sType)
{
	switch (sType)
	{
		case "50":		// shipping methods.
			return "/help/Feature%201.htm";
			break;
			
		default:
			return "/help/default.htm";
	}
}

function openForm(sType, sId, sName, sParams)
{
	var sUrl = createFormUrl(sId, sType);
	
	if (!IsNull(sParams))
	{
		sUrl += sParams;
	}
	openWindow( sUrl, sName, _iWindowX,  _iWindowY );
}

function openWindow(sPath, sName, iX, iY)
{
	if (IsNull(iX))
	{
		iX = _iWindowX;
	}
	if (IsNull(iY))
	{
		iY = _iWindowY
	}
	
	try
	{
		var window2 = window.open(sPath, sName, "width=" + iX + ",height=" + iY + ",status=1,resizable=1");
		if (window2==null)
		{
			alert('Please disable the pop up blocker to access NetOptimal system.');
		}
	}
	catch(e) {}
}

function openDialog(sPath, oArgs, iWidth, iHeight, bResizable)
{
	if (window.showModalDialog)
	{
		bResizable = bResizable ? "yes" : "no";
		return window.showModalDialog(sPath, oArgs, "dialogWidth:" + iWidth + "px;dialogHeight:" + iHeight + 
			"px;help:0;status:0;scroll:0;center:1;resizable:" + bResizable);
	}
	else
	{
		return window.open(sPath,'',"width=" + iWidth + ",height=" + iHeight + 
			",toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no ,modal=yes");
	}
}

function openPopup(oCurrentPopup)
{
	var oPopup = null;
	
	if (!IsNull(oCurrentPopup))
	{
		oPopup = oCurrentPopup;
	}
	else
	{
		oPopup = window.createPopup();
	}
	if (!IsNull(oPopup))
	{
		oPopup.document.createStyleSheet("/styles/global.css");
		oPopup.document.createStyleSheet(CURRENT_SKIN_LINK);
	}
	return oPopup;
}

function openEmailForm(templateId, entityId, tableId)
{
	var params = "&templateId=" + templateId;
	params += "&assignedEntityId=" + entityId;
	params += "&tableId=" + tableId;
	openForm("25", "", "_blank", params)
}

function executeScript(code)
{
	if (window.execScript)
	{
		window.execScript(code);
	}
	else
	{
		eval(code);
	}
}

function refreshParent()
{
	var grid = getHtmlObj(window.document, "gridTable");
	var topFrameExpression = "topFrame.aspx";

	if (!IsNull(grid))
	{
		grid.Refresh();
	}
	else if (window.document.location.href.search(topFrameExpression) > 0)
	{
		top.mainFrame.refreshParent();
	}
	else if (window.document.location.href.search("menuBarId") > 0)
	{
		var leftTabDiv = GetParamFromQueryString(window.document.location.href, "lefttab") + "_div";
		var oRelated = getHtmlObj(window.parent.document, leftTabDiv).firstChild.contentWindow;
		if (oRelated.gridTable != null)
		{	
			oRelated.gridTable.Refresh();
		}
		else if (oRelated.frames[0].gridTable != null)
		{
			oRelated.frames[0].gridTable.Refresh();
		}
		else if (oRelated.CalendarRefresh)
		{
			if (typeof(oRelated.nobpCalendar) != "undefined")
			{
				oRelated.CalendarRefresh(oRelated.nobpCalendar);
			}
		}

	}
	else if (CalendarRefresh)
	{
		if (typeof(nobpCalendar) != "undefined")
		{
			CalendarRefresh(nobpCalendar);
		}
	}
}
