var saveBackground, selectedObj, offsetX, offsetY ;

var currentPlace = ""
var moveMode     = false;

/* init(); */

function onEnter( evt, frm ) {
	var keyCode = null;

	if( evt.which ) {
		keyCode = evt.which;
	} else if( evt.keyCode ) {
		keyCode = evt.keyCode;
	}
	if( 13 == keyCode ) {
		ObjFormSubmit("ADDTOCART");
		//frm.@BUTTON.ADDTOCART.click();
		return false;
	}
	return true;
}

function vwInit()
{
   for(var i=0;i < document.all.length;i++) {
     obj = document.all[i]
     if (obj.className=="input") {
       obj.onfocus =  new Function("HighLight(this)");
       obj.onblur  =  new Function("LowLight(this)");
     } 
     if (obj.movable=="movable") {
       obj.moved = false;
     }
   }
}

function mouseMoving(evt)
{
  if (selectedObj != null) {
    selectedObj.style.pixelLeft = window.event.clientX - offsetX + document.body.scrollLeft - 2;
    selectedObj.style.pixelTop  = window.event.clientY - offsetY + document.body.scrollTop - 1;
    return false ;
  }
}

function mouseButtonDown(evt)
{
  if (event.button == 2) {
    return true;
  }
  if (moveMode==false)
  {
    return true;
  }
  selectedObj = findSourceElement(evt) ;
  if (selectedObj != null) {
    offsetX = window.event.offsetX + selectedObj.parentElement.parentElement.style.pixelLeft + 1 ;
    offsetY = window.event.offsetY + selectedObj.parentElement.parentElement.style.pixelTop + 2;
    selectedObj.style.zIndex = 100 ; 
  }
 return false ;
}

function mouseButtonUp(evt)
{
  if (selectedObj != null) {
    selectedObj.style.zIndex = 0 ;
    selectedObj.moved = true;
    selectedObj  = null ;
  }
}

function findSourceElement(evt)
{
  var divObj = window.event.srcElement.parentElement ;
  for (var i = document.all.length - 1; i >= 0 ; i--) {
    if (divObj != null) {
      movable = divObj.movable
      if (movable == "movable") {
        return divObj;
      }
      divObj = divObj.parentElement ;
      if (divObj != null) {
        movable = divObj.movable
        if (movable == "movable") {
          return divObj ;
        }
      }
    }
  }
  return null ;
}

function testClick(evt)
{
  if (moveMode) {
    return false;
  }
}

function moveSwitch()
{
  if (currentPlace=="") {
    alert("No current place has been defined...moving is disabled.");
    return;
  }
  if (moveMode) {
     moveMode = false;
     document.body.background=saveBackground;
     anchorMove();
     return;
    } else {
     moveMode = true;
     saveBackground = document.body.background;
     document.body.background="../GIF/movebg.gif";
   }
}

function contextDelete()
{
  if (currentPlace=="") {
    alert("No current place has been defined...moving is disabled.");
    return;
  }
  if (moveMode)
      {
       alert('Please exit "Move Mode" before deleting');
       return;
      } else {
       document.write("Preparing...");
       window.location = "ps?" + currentPlace + "~ICONPALDEL~RTN=" + window.location.search.substring(1);
      }
 }

function anchorMove()
{
  var browseString = "ps?" + currentPlace + "~IMOVE";
  var buttons= "";
  var tops   = "";
  var lefts  = "";
  for(var i=0;i < document.all.length;i++) {
   obj = document.all[i];
   if (obj.moved==true) {
     buttons = buttons + obj.id         + ",";
     tops    = tops    + obj.offsetTop  + ",";
     lefts   = lefts   + obj.offsetLeft + ",";
   }
  }
  if (buttons != "") {
    document.write("Saving changes...");
    window.location = browseString + "~DIV=" + buttons + "~TOP=" + tops + "~LEFT=" + lefts + "~RTN=" + window.location.search.substring(1);
  }
}

function contextPalette()
{
  if (currentPlace != "")
  {
    window.location = "ps?" + currentPlace + "~ICONPAL~RTN=" + window.location.search.substring(1);
  }
}

/* function init()
{
	document.onmousedown = mouseButtonDown;
	document.onmouseup   = mouseButtonUp;
	document.onmousemove = mouseMoving;
	document.onclick     = testClick;
} */

function HighLight(obj)
{
	obj.className = "inputfocus";
}

function LowLight(obj)
{
	obj.className = "input";
}

function ObjFormSubmit(ButtonVal)
{
	document.Obj_Form.Button.value = ButtonVal;
	document.Obj_Form.submit();
}

function IdFormSubmit(ButtonVal)
{
	document.Id_Form.Button.value = ButtonVal;
	document.Id_Form.submit();
}

function NavTo(fieldname)
{
	si = document.forms['Obj_Form'].elements[fieldname].selectedIndex;
	if (si == -1)
	{
	alert('You must make a selection from the list before navigating.');
	} else {
	sv = document.forms['Obj_Form'].elements[fieldname].options[si].value;
	window.open(sv,'SocketNav','Width=640,Height=400,Resizable=Yes,toolbar=1,copyhistory=0,Scrollbars=Yes,Status=Yes,MenuBar=1,Border=0');
	}
}

function DocLoc(loc)
{
  if (moveMode) 
  {}
  else
  {
    document.location=loc;
  }
}

function vwlookup(fieldname,searchid)
{
	words = document.forms['Obj_Form'].elements[fieldname].value;
	thrf  = 'ps?' + searchid + '~ILOOKUP~FIELD=' + escape(fieldname) + '~WORDS=' + words;
	window.open(thrf,'Lookup','Width=640,Height=400,Resizable=Yes,toolbar=1,copyhistory=0,Scrollbars=Yes,Status=Yes,MenuBar=1,Border=0');
}

function showmenuie5()
{
  var rightedge = document.body.clientWidth-event.clientX;
  var bottomedge = document.body.clientHeight-event.clientY;
  if (rightedge < ie5menu.offsetWidth)
    ie5menu.style.left = document.body.scrollLeft + event.clientX - ie5menu.offsetWidth;
  else
    ie5menu.style.left = document.body.scrollLeft + event.clientX;
    if (bottomedge < ie5menu.offsetHeight)
      ie5menu.style.top = document.body.scrollTop + event.clientY - ie5menu.offsetHeight;
    else
      ie5menu.style.top = document.body.scrollTop + event.clientY;
      ie5menu.style.visibility = "visible";
      return false;
}

function hidemenuie5() {
  ie5menu.style.visibility = "hidden";
}

function highlightie5() {
  if (event.srcElement.className == "menuitems")
   {
    event.srcElement.style.backgroundColor = "highlight";
    event.srcElement.style.color = "white";
   }
}

function lowlightie5() {
  if (event.srcElement.className == "menuitems")
   {
     event.srcElement.style.backgroundColor = "";
     event.srcElement.style.color = "black";
   }
}
function jumptoie5() {
  if (event.srcElement.className == "menuitems")
    location.replace(event.srcElement.url);
}

function deleteObj(Handle) {
  if (Handle=="undefined") {Handle="this object"}
  if (confirm('Are you sure you want to delete ' + Handle + '?'))
  ObjFormSubmit('ERASE');
}

function vwFocus(fieldname) {
  if (document.all[fieldname]) {
    if (document.all[fieldname].className == "input") {
      document.all[fieldname].focus();
    }
  }
}

function TermsOfUse() {
  var popurl="ps?template_display.terms~iact"
  winpops=window.open(popurl,"","width=600,height=400,scrollbars='AUTO',") 
}


function InitMenu()
   {
     if (document.all["menuBar"])
       {
       }
       else
       {
	return;
       }
    
     document.onclick=HideMenuBar;
     var bar = menuBar.children;
     for(var i=0;i < bar.length;i++) 
        {
          var menuName = bar[i].menu;
          if (menuName != null)
          {
            bar[i].onmouseover = new Function("ShowMenu("+bar[i].id+")");
            var menu=eval(menuName);
            menu.style.visibility = "hidden";
            var Items = menu.children;
            for(var j=0; j<Items.length; j++)
            {
              var menuItem = eval(Items[j].id);
              if(menuItem.menu != null)
                 {
                  if (menuItem.innerHTML.indexOf("_Arrow",1) < 1)
                     {
                     // menuItem.innerHTML += "<Span Id="+menuItem.id+"_Arrow class='Arrow'> 4</Span>" ;
                     //menuItem.innerHTML += "<Span Id="+menuItem.id+"_Arrow class='Arrow'><img src='../gif/menu_arrow.gif'></Span>" ;
                    menuItem.innerHTML += "<img class='Arrow' src='../gif/menu_arrow.gif'>" ;
                     }
                  FindSubMenu(menuItem.menu);
                 }
               
              if(menuItem.cmd != null) 
                 {
                 menuItem.onclick = new Function("Do("+menuItem.id+")");
                 } 
                   
              menuItem.onmouseover = new Function("highlight("+Items[j].id+")");        
            }    
          } else 
            {
            bar[i].onmouseover = new Function("ShowBar("+bar[i].id+")");
            }
          if (bar[i].cmd != null)
            {
              bar[i].onclick = new Function("Do("+bar[i].id+")");
            }
        }  
   }

   function FindSubMenu(subMenu)
   {
         var menu=eval(subMenu);
         var Items = menu.children;
          for(var j=0; j<Items.length; j++)
            {
              menu.style.visibility = "hidden";
              var menuItem = eval(Items[j].id);
           
              if(menuItem.menu!= null)
                 {
                    menuItem.innerHTML += "<img class='Arrow' src='../gif/menu_arrow.gif'>" ;
                    FindSubMenu(menuItem.menu);
                 }

                 if(menuItem.cmd != null) 
                   {
                   menuItem.onclick = new Function("Do("+menuItem.id+")");
                   } 
                  
                 menuItem.onmouseover = new Function("highlight("+Items[j].id+")");  
              }  
   } 

function ShowMenu(obj)
   {
     HideMenu(menuBar);
     var menu = eval(obj.menu);
     var bar = eval(obj.id);
     bar.className="barOver";
     menu.style.visibility = "visible";
     menu.style.pixelTop =  obj.getBoundingClientRect().top + obj.offsetHeight + document.body.scrollTop - 2;
     menu.style.pixelLeft = obj.getBoundingClientRect().left + document.body.scrollLeft - 2;
     if (obj.cmd != null)
        {
         window.status = obj.cmd;
        }
   }
   
function highlight(obj)
   {
     var PElement = eval(obj.parentElement.id);
     if(PElement.hasChildNodes() == true)
     {
      var Elements = PElement.children;
      for(var i=0;i<Elements.length;i++)
       {
          TE = eval(Elements[i].id);
          TE.className = "menuItem";
       }
     } 
     obj.className="ItemMouseOver";
     ShowSubMenu(obj);
     if (obj.cmd != null)
     {
       window.status = obj.cmd;
     } else window.status = "";

   }
   
 function Do(obj)
   {
    var cmd = eval(obj).cmd;
    var new_browser = eval(obj).nb;
    if (new_browser=="1")
      {
        window.open(cmd);
      } else window.navigate(cmd);
   }
   
function HideMenu(obj)
   {
     window.status = '';
     if(obj.hasChildNodes()==true)
     {  
        var child = obj.children;

        for(var j =0;j<child.length;j++)
        {
           if (child[j].className=="barOver")
           {
              var bar = eval(child[j].id);
              bar.className="Bar";
           }
                      
           if(child[j].menu != null)
           {
             var childMenu = eval(child[j].menu);
             if(childMenu.hasChildNodes()==true) 
               HideMenu(childMenu);       
             childMenu.style.visibility = "hidden" ;
           }
        }           
     }
   }

function ShowSubMenu(obj)
  {
     PMenu = eval(obj.parentElement.id);
     HideMenu(PMenu);
     if(obj.menu != null)
     {
      var menu = eval(obj.menu);
      menu.style.visibility = "visible";
      menu.style.pixelTop   = obj.getBoundingClientRect().top + document.body.scrollTop +3;
      menu.style.pixelLeft  = obj.getBoundingClientRect().right + document.body.scrollLeft - 5;
      if(menu.getBoundingClientRect().right > window.screen.availWidth )
        menu.style.pixelLeft = obj.getBoundingClientRect().left - menu.offsetWidth;
     }
  } 

function HideMenuBar(e)
{
 HideMenu(menuBar);
 if (moveMode) {
   return false;
 }
}

function ShowBar(obj)
{
 HideMenu(menuBar);
 if (obj.cmd != null)
    {
     window.status = obj.cmd;
     obj.className="barOver";
    }
}

