//Cross Browser AttachEvent
//Call like this AttachEvent(window, 'load', AddPageButtons)
//Note the omitted 'on' from the evt string parameter
function mlsAttachEvent(obj, evt, func, useCap) {
    if (!useCap) {
        useCap = false;
    }
    if (obj.addEventListener) {
        obj.addEventListener(evt, func, useCap)
        return true;
    }
    else if (obj.attachEvent) {
        return obj.attachEvent("on" + evt, func);
    }
}

function disableCtrlN()
{
	var pressedKey = event.keyCode;
	if ( (78 == pressedKey) && (event.ctrlKey) ) 
	{
		if(window.document.title.indexOf("Secure") == -1)
		{
			openNewWindow();
		}
		return false;
	}
}

// Traps the F11 keypress to disable IE full screen mode.
function disableF11()
{
	if (122 == event.keyCode)
	{
		event.keyCode = 0;
		return false;
	}
}

var parentWin = null;

function GetParent()
{
	var counter = 0;
	
	if (parentWin == null || parentWin.Main_Body == null)
	{
		parentWin = window;
		while (parentWin.parent != null)
		{
		if (parentWin.Main_Body != null || counter > 10)
		break;                  
		counter = counter + 1;
		parentWin = parentWin.parent;
		}
	}
	return parentWin;
}

function trapF5()
{
	if (116 == event.keyCode)
	{
		event.keyCode = 0;
		var doc = GetParent();

		if (doc.Main_Body != null)
		{
			doc.Main_Body.location.reload(true);
		}
		return false;
	}     
}

function openNewWindow()
{
	var popUpContent = "Menu.aspx?hidMLS=" + MyMLSID;
	if (typeof(window[ 'LoginDomain' ]) != "undefined") 
	{
		popUpContent = LoginDomain + popUpContent;
	}
	
	var winName = "new_window";
	var currentDate = new Date()
	winName += currentDate.getHours();
	winName += currentDate.getMinutes();
	winName += currentDate.getSeconds();
	winName += currentDate.getMilliseconds();
	
	var properties = "status=yes,toolbar=yes,location=yes,menubar=yes,width=650,height=500,resizable=yes"
	window.open(popUpContent,winName,properties);
}

function keypressEvents()
{
	try
	{
		var isClientPortal = parent.parent.parent.document.forms[0].id == 'frmClientPortal';
		
		if(!isClientPortal)
		{
			attachKeypressEvents();
		}
	}
	catch(e)
	{
		attachKeypressEvents();
	}
}

function attachKeypressEvents() 
{
    mlsAttachEvent(document, "keydown", disableF11, null);
    mlsAttachEvent(document, "keydown", trapF5, null);    
}

function AddAspxURLParam(URL, paramName, paramValue)
{
	if (URL.indexOf('.aspx') != -1)
	{
		if (URL.indexOf('.aspx?') != -1)
		{
			URL = URL + '&' + paramName + '=' + paramValue;
		}
		else
		{
			URL = URL + '?' + paramName + '=' + paramValue;
		}
	}
	
	return URL;
}

keypressEvents();
