//
//  edit_trap_handler.js
//
//  New client-side edit trap!  This is the file to include in the "trapper" frames.
//
function EditTrapVerifyClick(ev)
{
    if (!WebcatsIsModified())
        return;

    if (!ev)
        ev = window.event;

    var objElm = IsInternetExplorer() ? ev.srcElement : ev.target;

    var szTagName = objElm.tagName;
    var szTagID = objElm.id;

    if (szTagName == 'IMG')
    {
		//
		//  These are the IDs of images that are safe to click on
		//  (they don't reload the mainframe)
		//
		var arrSafeImages = new Array("btnOptionsView", "btnOptionsEcenter", "btnOptionsAdd",
			"btnOptionsTools", "btnOptionsCustom", "btnOptionsHelp", "WebcatsLogo");
			
		for(var i = 0; i < arrSafeImages.length; i++)
			if (szTagID == arrSafeImages[i])
				return;
    }
    else if (szTagName == "A")
    {
    	//
		//  Anchors that open a new window are always safe
		//
		var strTarget = objElm.target.toLowerCase();
		if (strTarget == "_blank")
			return;
		//
		//  "mailto:" links are also safe
		//
		var strHref = objElm.href.toLowerCase();
		if (strHref.substr(0, 7) == "mailto:")
			return;
    	//
		//  These are the IDs of anchors that are safe to click on
		//  (they don't reload the mainframe)
		//
		var arrSafeAnchors = new Array("btnMyWebCATS", "btnWorksheets", "btnAbout");
		for(var i = 0; i < arrSafeAnchors.length; i++)
			if (szTagID == arrSafeAnchors[i])
				return;
    }
    else if (szTagName != "A" && szTagName != "B")
        return;

    strMsg = "WARNING: By continuing, you will exit the form and lose any edits or additions you have made.\n\n" +
		 "Are you sure you wish to exit the data entry form?";
        
    if (!confirm(strMsg))
    {
        ev.returnValue = false;
		ev.cancel = true;
		ev.cancelBubble = true;
        if (!IsInternetExplorer())
            ev.preventDefault();
            
        window.top.status='';
        return(false);
    }

    WebcatsSetModified(false);
    return(true);
}

function EditTrapLoad()
{
    if (IsInternetExplorer())
        document.onclick = EditTrapVerifyClick;
    else
        document.addEventListener("click", EditTrapVerifyClick, false);
}

EditTrapLoad();
