/*
toolbar.js

*/

// ------- set navigator variables -------
var ie4 = (navigator.appName.indexOf("Microsoft") != -1 && parseInt(navigator.appVersion) >= 4);
var ns4 = (navigator.appName.indexOf("Netscape") != -1 && parseInt(navigator.appVersion) >= 4);

// ------- set global variables -------
var isEditable = "Yes";

// ------- update the isEditable to No if not in admin tool box -------
if (parent.leftnavigator && parent.leftnavigator.admintoolbox == true) {
 var admintoolbox = true;
} else {
 var admintoolbox = false;
}

// return the layer object with layerName
function getLayerObj(objFrame, layerName) {
 var obj;
 if (ns4) {
 obj = objFrame.document.layers[layerName];
 } else {
 obj = objFrame.document.all[layerName];
 }
 return obj;
}

// makes the object visible
function showObj(obj) {
 if (ns4) {
 obj.visibility = 'show';
 } else {
 obj.style.visibility = 'visible'
 }
}

// returns the width of the layer object
function getObjWidth (obj) {
 var objWidth, newObjName;
 if (ns4) {
 objWidth = obj.clip.width;
 } else {
 newObjName = obj.id + 'img';
 objWidth = document.images[newObjName].width;
 }
 return objWidth;
}

// move an object to x and y
function moveTo(obj, x, y) {
 if (ns4) {
 obj.moveTo(x, y);
 } else {
 obj.style.pixelLeft = x;
 obj.style.pixelTop = y;
 }
 return true;
}

function dovoid() {}

function ignore_error(msg, url, line) {
 parent.content.location.href =parent.leftnavigator.currentview+parent.leftnavigator.currentplace;
 return true;
}

// hide all images
function hideAll () {
 var i, divs;
 if (ns4) {
 for (i=0 ; i < document.layers.length ; i++) {
 document.layers[i].visibility = 'hide';
 }
 } else {
 divs = document.all.tags("DIV");
 for (i=0 ; i < divs.length ; i++) {
 divs[i].style.visibility = 'hidden';
 }
 }
}

// hides all buttons and then shows and positions only those passed to the funcion as arguments
function showButtons () {
 var i, args, newArgs, objLayer, objID, alwaysPtr, atbPtr;
 args = showButtons.arguments;
 var j = 0;
 var newArgs = new Array();
 if (args.length == 0) return true;
 hideAll();
 for (i=0 ; i<args.length ; i++) {
 alwaysPtr = args[i].indexOf("always");
 atbPtr = args[i].indexOf("ATB");
 if (isEditable == "No" && (args[i] == "compose" || args[i] == "edit" || args[i] == "respond")) {
 // don't show - not editable
 } else if (admintoolbox == false && atbPtr != -1) {
 // don't show - not admintoolbox and button specified to show only for ATB
 } else {
 if (alwaysPtr == -1 && atbPtr == -1) {
 objID = args[i];
 } else if (alwaysPtr != -1) {
 objID = args[i].substring(0, alwaysPtr);
 } else {
 objID = args[i].substring(0, atbPtr);
 }
 objLayer = getLayerObj(self, objID);
 showObj(objLayer);
 newArgs[j] = objID;
 j++;
 }
 }
 positionButtons(newArgs);
}

function positionButtons (args) {
 var i, posLeft, posTop, imgWidth, objLayer, padder;
 posLeft = 0;
 posTop = 0;
 padder = 10;
 for (i=0 ; i<args.length ; i++) {
 objLayer = getLayerObj(self, args[i]);
 imgWidth = getObjWidth(objLayer);
 moveTo(objLayer, posLeft, posTop);
 posLeft = parseInt(posLeft) + parseInt(imgWidth) + parseInt(padder);
 }
}
function changeComposeImg(imgSRC) {
 document.composeimg.src = imgSRC;
}

function getCloseAction() {
 var objName, closeAction;
 objName = 'CloseAction';
 if (!parent.content.document.forms[0] || !parent.content.document.forms[0].elements[objName]) {
 closeAction = "";
 } else {
 closeAction = parent.content.document.forms[0].elements[objName].value;
 }
 return closeAction;
}

var goCount;
function getCloseURL(closeAction) {
 var closeURLVal, actualURL;
 if (closeAction == "") {
 actualURL = "HistoryBack";
 goCount=1;
 } else {
 closeURLVal = closeAction;
 if (closeURLVal == "BackToSearch") {
 goCount=1;
 if (parent.leftnavigator) {
 var curClickCount=parent.leftnavigator.searchClickHistory;
 var curRefreshCount=parent.leftnavigator.searchRefreshHistory;
 if (curClickCount == 0 && curRefreshCount == 0) { }
 else {
 goCount = curClickCount + curRefreshCount;
 }
 parent.leftnavigator.searchClickHistory = 0;
 parent.leftnavigator.searchRefreshHistory = 0;
 }
 actualURL = "HistoryBack";
 } else if (closeURLVal == "BackToView" || closeURLVal == "BackToEntryPage") {
 if (parent.leftnavigator) {
 if (closeURLVal == "BackToView") {
 actualURL = "parent.leftnavigator.currentview + parent.leftnavigator.currentplace";
 } else {
 actualURL = "DefaultEntryPage";
 }
 } else {
 actualURL = "HomeOrClose";
 }
 } else {
 actualURL = "ACTUALURL";
 }
 }
 return actualURL;
}

function Close() {
 var actualURL, closeAction;
 closeAction = getCloseAction();
 actualURL = getCloseURL(closeAction);
 if (actualURL == "HomeOrClose") {
 if (parent.parent.main) {
 parent.parent.main.location = "/inet/demo.nsf/mainframeset3?ReadForm";
 } else {
 parent.close();
 }
 } else if (actualURL == "DefaultEntryPage") {
 parent.content.location = "/inet/demo.nsf/LUSiteDocs/PromotionAlt2?OpenDocument";
 parent.leftnavigator.resetNavigator();
 } else if (actualURL == "ACTUALURL") {
 parent.content.location = closeAction;
 } else if (actualURL == "HistoryBack") {
 parent.content.history.go(-goCount);
 } else {
 parent.content.location = eval(actualURL);
 }
 return false;
}

function Edit() {
 location.search="?EditDocument";
}

function Save() {
 if(validateTags()) validateForm(true);
}

function CheckValidation() {
 return validateForm(false);
}

function Respond() {
 var docUNID, respForm, url;

 docUNID = document.forms[0].elements['DocUNID'].value;
 respForm = document.forms[0].elements['RespFormName'].value;
 url = '/inet/demo.nsf/'+respForm+'?OpenForm&ParentUNID='+docUNID;
 location = url;
}

function ContextHelp() {
 var objName, contextID, helpURL, win;
 objName = 'HelpContextID';
 if (!parent.content.document.forms[0] || !parent.content.document.forms[0].elements[objName]) {
 return false;
 }
 contextID = parent.content.document.forms[0].elements[objName].value;
 helpURL = "/inet/demo.nsf//vWContextHelp/" + contextID + "?OpenDocument";
 win=window.open(helpURL,"","width=425,height=475,scrollbars=yes");
}

function DBHelp() {
 var helpURL;
 helpURL = "//inet/demo.nsf//WebHelp?OpenView";
 parent.content.location.href = helpURL;
}

function Feedback() {
}

function ToTheTop() {
 parent.content.location.hash='#Top';
 return false;
}

function Delete() {
 var objName, deleteUNID, docType;
 objName = 'DocUNID';
 deleteUNID = document.forms[0].elements[objName].value;
 agree = confirm('Are you sure you want to delete this document and and responses associated with it.');
 if (agree) {
 location.href = '/inet/demo.nsf/delete?OpenAgent&ID=' + deleteUNID
 } else {
 return false;
 }
}

function Compose() {
 var formName = document.forms[0].WhichForm.value;
 location.href='/inet/demo.nsf/'+formName+'?OpenForm';
}

function Expand() {
var view = parent.content.document.forms[0].thisviewurl.value;
 parent.content.location.href=view+'&ExpandView';
}

function Collapse() {
var view = parent.content.document.forms[0].thisviewurl.value;
 parent.content.location.href=view+'&CollapseView';
}

function More() {
var val, val1, val2, val3, val4;
val = parent.content.document.links[1].onclick
val1 = val.toString();
val2 = val1.indexOf('return _')+7;
val3 = val1.indexOf('}');
val4 = val1.substring(val2,val3);
eval('parent.content.'+val4);
}

function Previous() {
var val, val1, val2, val3, val4;
val = parent.content.document.links[0].onclick
val1 = val.toString();
val2 = val1.indexOf('return _')+7;
val3 = val1.indexOf('}');
val4 = val1.substring(val2,val3);
eval('parent.content.'+val4);
}
function Search() {
 parent.leftnavigator.searchClickHistory=0;
 parent.leftnavigator.searchRefreshHistory=0;
 parent.content.location=parent.content.document.links[4].href;
 dovoid();
}

//************** begin of approval action code ***************
var reasonField;
function stampDate(fieldToChange){
 var Stamp, Year, Month, Day;
 Stamp = new Date();
 Year = Stamp.getYear();
 Day = Stamp.getDate();
 Month = parseInt(Stamp.getMonth()) + 1;
 if (Year < 2000) Year = 1900 + Year; if (Month <10) {
 Month = "0" + Month;
 }
 if (Day <10) {
 Day = "0" + Day;
 }
 fieldToChange.value = Day +"/"+Month+ "/"+Year;
}

function getReason() {
 abURL = '/inet/demo.nsf/ReasonBox$nn?OpenForm';
 wheight = "200";
 wwidth = "450";
 opts = 'width=' + wwidth + ',height=' + wheight;
 popupWin=window.open( abURL,'ReasonWindow', opts);
}

function SubmitForApproval() {
 if(validateTags()) {
   document.forms[0].Status.value = "Awaiting Approval";
   Save();
 }
}

function Approve() {
 document.forms[0].Status.value = "Approved";
 stampDate(document.forms[0].DateLastApproved);
 Save();
}

function Reject() {
 if (CheckValidation()) {
 document.forms[0].Status.value = "Rejected";
 reasonField = document.forms[0].RejectReason;
 getReason();
 }
}

function Rework() {
 if (CheckValidation()) {
 document.forms[0].Status.value = "Rework Required";
 reasonField = document.forms[0].RejectReason;
 getReason();
 }
}

// Validate the Tags
function validateTags() {
  var count = 0;
  var boxes = document.forms[0].Tags;
  for(var i=0;i<boxes.length; i++) {
    var box = boxes[i];
    if (box.checked) count++;
  }
  if (count==0) {
    alert("You select one or more tags for this document.");
    return false;
  }
  return true;
}
