var saveBackground = "";
var reqFields      = "";
var reqMsgs        = "";
var moveMode       = false;
var isDrag         = false;
var x, y;
var sObj;
var nn6=document.getElementById&&!document.all;
var form_submitted = false;

init();

function vwInit() {
  objs = document.getElementsByTagName("*");
  for(var i=0;i < objs.length;i++) {
    obj = objs[i]
    if (obj.className=="input") {
      obj.onfocus =  new Function("HighLight(this)");
      obj.onblur  =  new Function("LowLight(this)");
    } 
  }
}

function enterEqTab(e,objName,submitName) {
  if (e.keyCode==13) {
    if (submitName) {
      ObjFormSubmit(submitName);
    } else {
      obj = document.getElementsByName(objName)[0];
      obj.focus();
    }
    return false;
  }
}

function mouseMoving(e) {
  if (isDrag) {
    sObj.style.left = nn6 ? tx + e.clientX - x : tx + event.clientX - x;
    sObj.style.top  = nn6 ? ty + e.clientY - y : ty + event.clientY - y;
    return false;
  }
}

function mouseButtonDown(e) {
  var clickType = 1;
  clickType = nn6 ? e.which : event.button;
  if (clickType != 1) {
    return true;
  }
  var tObj = nn6 ? e.target : event.srcElement;
  if (moveMode==false) {
    if(tObj.className != "ToolBarTitle") {
      return true;
    }
  }
  var topElement = nn6 ? "HTML" : "BODY";
  var canMove    = nn6 ? tObj.getAttribute("movable") : tObj.movable;
  while (tObj.tagName != topElement && canMove != "movable") {
    tObj    = nn6 ? tObj.parentNode : tObj.parentElement;
    canMove = nn6 ? tObj.getAttribute("movable") : tObj.movable;
  }
  if (canMove == "movable") {
    isDrag = true;
    sObj   = tObj;
    tx     = parseInt(sObj.style.left+0);
    ty     = parseInt(sObj.style.top+0);
    x      = nn6 ? e.clientX : event.clientX;
    y      = nn6 ? e.clientY : event.clientY;
    document.onmousemove=mouseMoving;
    return false;
  }
}

function mouseButtonUp(e) {
  if (sObj != null) {
    sObj.style.zIndex = 0 ;
    sObj.moved = true;
    sObj  = null;
    isDrag = false;
  }
}

function testClick(e) {
  if (moveMode) {
    return false;
  }
}

function newWindow(e, winName, windowSpecs) {
  if (moveMode) {
    return false;
  }

  var topElement = nn6 ? "HTML" : "BODY";
  var tObj       = nn6 ? e.target : e.srcElement;

  while (tObj.tagName != topElement && tObj.nodeName != "A") {
    tObj    = nn6 ? tObj.parentNode : tObj.parentElement;
  }

  if (winName == undefined) {
    winName = ""
  }
  if (winName == "") {
    winName = "_blank"
  }
  if (windowSpecs == undefined) {
    windowSpecs = "Width=675,Height=575,Resizable=Yes,CopyHistory=No,ScrollBars=Yes,Toolbar=No,Status=Yes,MenuBar=Yes,Border=No"
  }
  window.open(tObj.href, winName, windowSpecs);
  e.cancelBubble = true;
  if (e.stopPropagation) e.stopPropagation();
  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 Anchor(toolBarId,toolBarDiv) {
  obj   = document.getElementById(toolBarDiv);
  tops  = obj.offsetTop;
  lefts = obj.offsetLeft;

  var browseString = "ps?TOOLBAR_" + toolBarId + "~IANCHOR~TOP=" + tops + "~LEFT=" + lefts + "~CurPlace=" + currentPlace;
  window.location = browseString;
}

function anchorMove() {
  var buttons= "";
  var tops   = "";
  var lefts  = "";
  objs = document.getElementsByTagName("*");
  for(var i=0;i < objs.length;i++) {
   obj = objs[i];
   if (obj.moved==true) {
     buttons = buttons + obj.id         + ",";
     tops    = tops    + obj.offsetTop  + ",";
     lefts   = lefts   + obj.offsetLeft + ",";
   }
  }
  if (buttons != "") {
    document.write("<html>Saving changes...<html>");
    window.location = "ps?" + currentPlace + "~IMOVE~DIV=" + buttons + "~TOP=" + tops + "~LEFT=" + lefts;
  }
}

function ie_getElementsByTagName(str) {
  if (str=="*") {
    return document.all
  } else { 
    return document.all.tags(str)
  }
}

function init() {
  document.onmousedown = mouseButtonDown;
  document.onmouseup   = mouseButtonUp;
  document.onclick     = testClick;

  if (document.all) {
    document.getElementsByTagName = ie_getElementsByTagName 
  }
}

function HighLight(obj) {
  obj.className = "inputfocus";
}

function LowLight(obj) {
  obj.className = "input";
}


function IdFormSubmit(ButtonVal) {
  document.Id_Form.Button.value = ButtonVal;
  document.Id_Form.submit();
}

function ObjFormSubmit(ButtonVal) {
  if (form_submitted) {
      if (!confirm("Processing: Are you sure you want to re-submit?")) {
         return;
      }
   } 
  if (((ButtonVal != "SAVE") && (ButtonVal != "ADD")) || checkReqs()) {
    form_submitted = true;
    document.Obj_Form.Button.value = ButtonVal;
    document.Obj_Form.submit();
  }
}

function checkReqs() {
  with (document.Obj_Form) {
    for (var i=0; i < reqFields.length; i++) {
      var thisType  = eval (reqFields[i] + ".type");
      switch(thisType) {
	case "text":
          thisValue = eval (reqFields[i] + ".value");
	  if (thisValue == "")  { 
            alert(reqMsgs[i]);
	    return false;
          }
	  break;
	case "textarea":
          thisValue = eval (reqFields[i] + ".value");
          if (thisValue == "") { 
            alert(reqMsgs[i]);
            return false;
	  }
          break;
	case "checkbox":
	  thisValue = eval (reqFields[i] + ".checked");
	  if (thisValue == false) { 
            alert(reqMsgs[i]);
	    return false;
	  }
	  break;
      }
    }
  }
  return true;
}

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,templateId) {
  words = document.forms['Obj_Form'].elements[fieldname].value;
  thrf  = 'ps?' + searchid + '~ILOOKUP~FIELD=' + escape(fieldname) + '~WORDS=' + words;
  if (templateId != undefined) {
    thrf  = thrf + '~Template=' + escape(templateId);
  }
  window.open(thrf,'Lookup','Width=640,Height=400,Resizable=Yes,toolbar=1,copyhistory=0,Scrollbars=Yes,Status=Yes,MenuBar=1,Border=0');
}

function deleteObj(msg) {
  if (msg=="undefined") {msg="delete this object"}
  if (confirm('Are you sure you want to ' + msg + '?'))
  ObjFormSubmit('ERASE');
}

function vwFocus(fieldname) {
  if (fieldname == "") {
    tObjs = document.forms[0].elements
  } else {
    tObjs = document.getElementsByName(fieldname)
  }
  for(var i=0;i < tObjs.length;i++) {
    tObj = tObjs[i]
    if (tObj.className=="input") {
      tObj.focus();
      return
    }
  return
  }
}

function TermsOfUse() {
  var popurl="ps?template_display.terms~iact"
  winpops=window.open(popurl,"","width=600,height=400,scrollbars='AUTO',") 
}


function InitMenu() {
  return;
}
   
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 SendAnswer(fieldname,selectedval) {
  window.close();
  opener.document.forms["Obj_Form"].elements[fieldname].value=selectedval;
  opener.document.forms["Obj_Form"].Button.value="REFRESH-" + fieldname;
  opener.document.forms["Obj_Form"].submit();
}

function decision(url, msg) {
  if (url=="undefined") return;
  if (msg=="undefined") msg="Are you sure?";
  if(confirm(msg)) location.href = url;
}

function readonlyRadio(rad) { 
 var el, e = 0, f = rad.form; 
 while (el = f[rad.name][e++]) el.checked = el.defaultChecked; 
} 

function isInteger(s) {
    return (s.toString().search(/^-?[0-9]+$/) == 0);
}

