/****************************************************************

  Traction Software, Inc. Confidential and Proprietary Information

  Copyright (c) 1996-2010 Traction Software, Inc.
  All rights reserved.

****************************************************************/

// PLEASE DO NOT DELETE THIS LINE -- make copyright depends on it.

// requires /images/modern/js/shared.js

var curItem;
var entryIds = "";

function newMenu(e, menu) {
  if (showNormalContextMenu(e)) {
    return true;
  }
  else {
    // show our menu [ajm 09.Sep.2005]
    if (itemMenu == null) {
      itemMenu = new Traction.ItemMenu();
    }
    itemMenu.showMenu(e);
    return false;
  }
}

function setItem(item) {
  curItem = item;
}

// this is kind of DIRTY! but i couldn't find a cleaner way to get around the
// nested html tags around a link. if you can find a cleaner way to do it, 
// please change it. it currently goes through three nested html tags (the
// m < 3) so you can always change that if more are needed. --cwd

function showNormalContextMenu(e) {

  var curElem;
  if (document.elementFromPoint) {
    curElem = document.elementFromPoint(e.clientX, e.clientY);
  } else if (e && e.srcElement) {
    curElem = e.srcElement;
  }
  for (var m=0; m < 3; m++) {
    if (!curElem || curElem == null) {
      return false;
    }
    else if (curElem.tagName == "img" || curElem.tagName == "IMG" ||
      curElem.tagName == "a" || curElem.tagName == "A") {
      return true;
    }
    else {
      curElem = curElem.parentNode; // to accomodate Gecko browsers, use parentNode instead of parentElement
    }
  } 
  return false;
}

function processTracking(action) {
  document.tracking.type.value = "tracking";
  document.tracking.curitem.value = decode_url_parameter(curItem);
  document.tracking.fb_submit.value = action;
  document.tracking.submit();
}

function processCustomAction(id, requireconfirm) {
  if (requireconfirm) {
    var origtypeparam = "";
    if (document.fm.type && document.fm.type.value) {
      origtypeparam = "&origtype=" + document.fm.type.value;
    }
    openDialog("reclassifydialog",
	       "modern_insertlabel_window",
	       "",
	       "&customaction=" + id + "&curitem=" + decode_url_parameter(curItem) + origtypeparam,
	       DIALOG_FLAG_MULTIPLE_WINDOWS);
  } else {
    document.tracking.type.value = "customaction";
    document.tracking.curitem.value = decode_url_parameter(curItem);
    document.tracking.fb_submit.value = id;
    document.tracking.submit();
  }
}




function processCustomActionInline(id, requireconfirm, failurecallback) {

  var itemid = curItem;

  if (requireconfirm) {
    // putting this in a timeout isn't as important because the
    // context changes to a new window and it was causing Server16479
    // [ajm 04.May.2005]
    processCustomActionInline_wakeup( itemid, id, requireconfirm, failurecallback );
  }
  else {

    setTimeout( function() {
		  processCustomActionInline_wakeup( itemid, id, requireconfirm, failurecallback );
		}, 
		1);

  }

}

function processCustomActionInline_wakeup(itemid, id, requireconfirm, failurecallback) {

  setWaitCursor( false );

  if (requireconfirm) {
    var origtypeparam = "";
    if (document.fm.type && document.fm.type.value) {
      origtypeparam = "&origtype=" + document.fm.type.value;
    }
    openDialog("reclassifydialog",
	       "modern_insertlabel_window",
	       "",
	       "&customaction=" + id + "&curitem=" + decode_url_parameter(itemid) + origtypeparam,
	       DIALOG_FLAG_MULTIPLE_WINDOWS);
  } else {

    document.tracking.type.value = "customaction";
    document.tracking.curitem.value = decode_url_parameter(itemid);
    document.tracking.fb_submit.value = id;
    var poststring = fm_submit( document.tracking );

    // signal that we want the new lables returned
    poststring = fm_append(poststring, "returnitemlabels=true");
    poststring = fm_append(poststring, "tractionid="+document.tracking.curitem.value );
    poststring = fm_append(poststring, "proj="+_get_url_param("proj", "*"));

    // do the post
    var args = [ encode_url_parameter(document.tracking.curitem.value), failurecallback ];
    xmlpost_async(FORM_ACTION_READ_WRITE, poststring, null, true, labels_refresh, args);

  }

}

function processAction(opttype, action, formtarget, opentargetjs, windowUrl, windowName, windowFeatures, javascriptToEval) {

  if (javascriptToEval && eval(javascriptToEval)) {

    // javascript handled this one, nothing more to do

  } else {

    // either no javascript or it was unable to handle this

    if (windowUrl) {

      // a url was provided, this implies a new window loaded with a get
      // request
      executeNewActionUrl(action, opttype, windowUrl, windowName, windowFeatures);

    } else {

      // a url was not provided, which implies opentargetjs was used to
      // open a new window that will be targeted or there is a post goes
      // to the current window

      var curform;
      curform = document.fm;
      eval(opentargetjs);

      curform.target = formtarget;

      if (action != '') {

	// notice that in all cases, we set the curentry and curitem values.
	// there was a bug where collecting an item, then an entry, was still
	// collecting the item

	var entryIdVal;

	if (!entryIds && curform.entryid) {
	  entryIds = curform.entryid.value;
	}

	if (opttype=="view") {
	  // don't set anything...
	  // entryid hiddens already in page

	  curform.curentry.value = "";
	  curform.curitem.value = "";

	  entryIdVal = entryIds;

	} else if (opttype=="entry") {

	  curform.curentry.value = decode_url_parameter(curItem);
	  curform.curitem.value = "";
	
	  entryIdVal = "";  // clear this out so that it doesn't present
	  // url size problems (see Server1172) -- ajm
	  // 09.19.01

	} else {

	  curform.curitem.value = decode_url_parameter(curItem);
	  curform.curentry.value = "";

	
	  entryIdVal = "";  // clear this out so that it doesn't present
	  // url size problems (see Server1172) -- ajm
	  // 09.19.01
	}

	if (curform.entryid) {
	  curform.entryid.value = entryIdVal;
	}

	//     formhidden.value = value;
	curform.action.value = action;
	curform.submit();  // ---->

      }
    }
  }
}

function executeNewActionUrl(action, opttype, windowUrl, windowName, windowFeatures) {

  if (action != '') {

    // for the Help action to work, we don't want to add all that stuff to the url
    if (windowUrl.indexOf('?') != -1) {

      if (opttype=="view") {
	// don't set anything...
	// entryid hiddens already in page

	// umm... i think i added the above comment (although i should
	// have put my initials on it) but that's not right.  we're
	// creating a url to redirect to.  it doesn't matter what is
	// hidden in the page, it's not going to get sent unless we
	// append it.  fortunately we can do that here.

	// we use the entryIds value because other actions might
	// override the value of document.fm.entryid
	if (!entryIds && document.fm.entryid) {
	  entryIds = document.fm.entryid.value;
	}

	windowUrl += "&curentry=&curitem=&entryid=" + encode_url_parameter(entryIds);

      } else if (opttype=="entry") {

	var targetId = curItem;
	var period = targetId.indexOf('.');
	if (period != -1) {
	  targetId = targetId.substring(0,period);
	}
	windowUrl += "&curentry=" + targetId + "&curitem=";

      } else {

	windowUrl += "&curentry=&curitem=" + curItem;
      }

      // hack on a cache id to compensate for sketchy reloading
      date = new Date();
      id = "" + date.getTime();
      windowUrl = removeURLParameter( windowUrl, "cacheid" );
      windowUrl += "&cacheid=" + id;

    }

    if (windowName != null) {
      do_window_open(windowUrl, windowName, windowFeatures);
    } else {
      window.location.href = windowUrl;
    }

  }
}

function addToEntryId(entries) {
  var entryid = document.fm.entryid.value;
  if (entryid.indexOf(entries) == -1) {
    if (entryid != "") {
      entryid += ",";
    }
    document.fm.entryid.value = entryid + entries;
  }
}



/**
 * This code is necessary to make the elementFromPoint function work
 * in Gecko based browsers.  [shep 10.Feb.2005]
 */
// Program:      document.elementFromPoint(int clientX, int clientY) in Gecko
// Author:       Jason Karl Davis (www.jasonkarldavis.com)
// Date:         15 June 2003
// Purpose:      Emulate Internet Explorer's document.elementFromPoint method as described here:
//               http://msdn.microsoft.com/workshop/...ntfrompoint.asp
// Requirements: A browser built off of the 1.4 branch of Mozilla (or better)
// Distribution: You may freely distribute and use this script as long as these comments remain intact

if (navigator.product == "Gecko" && navigator.userAgent.toLowerCase().indexOf("safari") == -1) {
  Document.prototype.elementFromPoint = function(x, y) {
    this.addEventListener("mousemove", this.elementFromPoint__handler, false);
    var event = this.createEvent("MouseEvents");
    var box = this.getBoxObjectFor(this.documentElement);
    var screenDelta = { x: box.screenX, y: box.screenY };
    event.initMouseEvent("mousemove", true, false, this.defaultView, 0,
			 x + screenDelta.x, y + screenDelta.y, x, y,
			 false, false, false, false, 0, null);
    this.dispatchEvent(event);
    this.removeEventListener("mousemove", this.elementFromPoint__handler, false);
    return this.elementFromPoint__target;
  }
  Document.prototype.elementFromPoint__handler = function (event) {
    this.elementFromPoint__target = event.explicitOriginalTarget;

    // reparent target if it is a text node to emulate IE's behavior
    if (this.elementFromPoint__target.nodeType == Node.TEXT_NODE)
      this.elementFromPoint__target = this.elementFromPoint__target.parentNode;
	
    // change an HTML target to a BODY target to emulate IE's behavior (if we are in an HTML document)
    if (this.elementFromPoint__target.nodeName.toUpperCase() == "HTML" && this.documentElement.nodeName.toUpperCase() == "HTML")
      this.elementFromPoint__target = this.getElementsByTagName("BODY").item(0);

    event.preventDefault();
    event.stopPropagation();
  }
  Document.prototype.elementFromPoint__target = null;
}


function item_checkbox_onclick(checkbox, item, actionChecked, actionUnchecked) {
  setItem(item);
  if (checkbox.checked) {
    processCustomActionInline(actionUnchecked, false, item_checkbox_action_failed);
  } else {
    processCustomActionInline(actionChecked, false, item_checkbox_action_failed);
  }
  return false;
}

function item_checkbox_action_failed(id) {
  var checkbox = document.getElementById(id.toLowerCase() + "icbox");
  if (checkbox != null && checkbox.tagName.toLowerCase() == "input") {
    checkbox.checked = !checkbox.checked;
  }
}
