/*********************************************************************************
'** Extra Javascript Routines Written For The Purpose Of Being Used With (c)2003 USErp by Yconsultants
'**
'** Copyright Author       : Daniel Yadwisiak
'** Modified By            : Daniel Yadwisiak
'** Original Date          : 8/12/2003
'** Current Revision Date  : 11/21/2004
'********************************************************************************/

/*********************************************************************************
'** This Section Contains Javascript DIV and Cookie maintenance.Currently Used To
'** Show/Hide Divs in DHTML screens and retrieve and make Cookies For Individual Pages.
'** Notes: If Using switchDiv a function 'hideAll()' is required in which
'**        changeObjectVisibility' is called for Each Div That Will Be Hidden
'**        and Shown Depending On User Selection
'**        IDStr = ID (string variable) Of The Div Being Toggled On / Off
'********************************************************************************/
function ToggleDiv(IDstr) {
  var IDobj = document.getElementById(IDstr);
  var today = new Date();
  var expires = new Date(); expires.setTime(today.getTime() + 1000*60*60*24*365);
  if (IDobj == null) return;
  if (IDobj.style.display == '')
    {IDobj.style.display = 'none';
     if (IDstr == "divSearch") {setCookie("divSearch" + document.forms[0].name,"N",expires,null,null,null);} }
   else
    {IDobj.style.display = '';
     if (IDstr == "divSearch") {setCookie("divSearch" + document.forms[0].name,"Y",expires,null,null,null);} } }

function switchDiv(div_id) {
  var style_sheet = getStyleObject(div_id);
  if (style_sheet) {hideAll(); changeObjectVisibility(div_id, "visible");}
   else {alert("I'm Sorry, but this only works in browsers that do Dynamic HTML.");} }

function getStyleObject(objectId) {
  if(document.getElementById && document.getElementById(objectId)) {
    return document.getElementById(objectId).style;}
   else if (document.all && document.all(objectId)) {  
    return document.all(objectId).style;}
   else if (document.layers && document.layers[objectId]) { 
    return document.layers[objectId];}
   else {return false;} }

function changeObjectVisibility(objectId, newVisibility) {
  var styleObject = getStyleObject(objectId);
  if (styleObject) {styleObject.visibility = newVisibility; return true;}
   else {return false;} }

/*
   name - name of the cookie
   value - value of the cookie
   [expires] - expiration date of the cookie
     (defaults to end of current session)
   [path] - path for which the cookie is valid, must begin with /
     (defaults to path of calling document)
   [domain] - domain for which the cookie is valid must have 2 periods .blah.com
     (defaults to domain of calling document)
   [secure] - Boolean value indicating if the cookie transmission requires
     a secure transmission
   * an argument defaults when it is assigned null as a placeholder
   * a null placeholder is not required for trailing omitted arguments
*/
function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie; }

/*
  name - name of the desired cookie
  return string containing value of specified cookie or null
  if cookie does not exist
*/
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end)); }

/*********************************************************************************
'** This Section Contains Drag Amd Drop Routines For Use With A Table And An Absolute DIV.
'** Notes: The mousedown, mouseover, and mouseup events should be set on the Table and
'**   never the entire document. But that also means the absolute pos div must have a lower
'**   zIndex (placing that layer under the table) or never get in the way of the mouse.
'**   Choice 1: Set the table in a relative pos div and set div's zIndex in code to 1
'**   Choice 2: Keep the absolute div out from under the mouse. (ex: Add 1 to it's Left or
'**           subtract 1 from it's top) [intLeft = 5; currently does this]
'********************************************************************************/
var objDrag; var objFrom; var objTo; var objState = 0; var intTop = 0;  var intLeft = 0;

function dragStart(evt) {
  if (!evt) var evt = window.event;
  objFrom = (window.event) ? evt.srcElement : evt.target;
  if (selectMove()) return;                    //Call Custom Select For Authorization To Continue
  var objTemp = objFrom                        //Make Copy Of Cell And Find Top
  for(intTop = objTemp.offsetTop; (objTemp = objTemp.offsetParent) != null; intTop += objTemp.offsetTop);
  objTemp = objFrom                            //Make Copy Of Cell And Find Left
  for(intLeft = objTemp.offsetLeft; (objTemp = objTemp.offsetParent) != null; intLeft += objTemp.offsetLeft);
  if (evt.pageX || evt.pageY)                  //Find Mouse Top And Left
   {intX = evt.pageX;
    intY = evt.pageY;}
  else if (evt.clientX || evt.clientY)
   {intX = evt.clientX + document.body.scrollLeft;
    intY = evt.clientY + document.body.scrollTop;}
  intTop -= intY; intLeft = 5;                 //Get Mouse Down To Real Position Offset
  objDrag.style.left = (intX + intLeft) + 'px';
  objDrag.style.top = (intY + intTop) + 'px';
  objDrag.style.display = '';                  //Make Our Moving Div Visible
  objState = 1;                                //Set Object State To Mouse Down - Possible Drag
  return false;}

function dragMove(evt) {
  if (!evt) var evt = window.event;
  if (evt.pageX || evt.pageY)
    {intX = evt.pageX;
     intY = evt.pageY;}
  else if (evt.clientX || evt.clientY)
    {intX = evt.clientX + document.body.scrollLeft;
     intY = evt.clientY + document.body.scrollTop;}
  if (objState == 1) objState++;              //Set Object State To Drag Start
  if (objState == 2 && objDrag)
    {objDrag.style.left = (intX + intLeft) + 'px';
     objDrag.style.top = (intY + intTop) + 'px';}
  return false; }

function dragEnd(evt) {
  if (!evt) var evt = window.event;
  objTo = (window.event) ? evt.srcElement : evt.target;
  if (evt.pageX || evt.pageY)
   {intX = evt.pageX;
    intY = evt.pageY;}
  else if (evt.clientX || evt.clientY)
   {intX = evt.clientX + document.body.scrollLeft;
    intY = evt.clientY + document.body.scrollTop;}
  if (objState == 2) finishMove();
  if (objDrag) {objDrag.style.display = 'none'; objDrag = null; }
  objState = 0;
  return false;}

