//-----------------------------------------------------------------------------
//
// Copyright (c) 2004 by Computors Limited. All Rights Reserved.
//
// No part of this publication or software may be reproduced, transmitted,   
// transcribed, stored in a retrieval system, or translated into any      
// language without the prior written authorization of Computors Limited. 
//
// This is a licensed product of Computors Limitied.
//
// Program    : hcat_common.js
// Description: Common JScript functions
//
//-----------------------------------------------------------------------------


try {document.execCommand("BackgroundImageCache", false, true);} catch(err) {}

//----------------------------------------
// General Functions
//----------------------------------------
// Submit dropdown selection with the need for a form
function dropdownaction(targ,selObj,restore) {
  eval(targ+".location.href='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

var fiu = false;
function SetFocus(f,fn) {
  if(!fiu) {
    eval("document.forms['"+f+"']."+fn+".focus()");
  }
}

function dummy() {
  return;
}

function ActionSubmit(formname,fa) {
  eval("document.forms['"+formname+"'].fa.value='"+fa+"'");
  eval("document.forms['"+formname+"'].submit()");
}
function SubmitConfirm(formname,fa,msgstring) {
  if (confirm(msgstring)) {
    eval("document.forms['"+formname+"'].fa.value='"+fa+"'");
    eval("document.forms['"+formname+"'].submit()");
    return true;
  }
  return;
}
function OnClickConfirm(msgstring) {
  if (confirm(msgstring)) return true;
  if (document.all && window.event) event.returnValue = false;
  return false;
}

function textboxcommalist(obj) {
  var v=obj.value;
  var f=false;
  var r='';
  for(var i=0; i<v.length; i++) {
    var ch=v.charAt(i);
    if(ch==',') {
      if(!f) r+='\n';
      f=true;
    } else {
      r+=ch;
      f=false;
    }
  }
  obj.value=r;
}

// Stub for WYSIWYG 
//function onsubmit () {
//  return true;
//}

//----------------------------------------
// Function to do a bulk move from one list to another
//----------------------------------------
function MoveSelectedListItems(srcCombo, destCombo, doSort, intSort, useVals) {
  var numItems = 0;
  var curPos = destCombo.options.length;
  var srcLen = srcCombo.options.length;
  
  for (var x=0; x<srcCombo.options.length; x++) {
    if (srcCombo.options[x].selected) numItems++;
  }
  
  destCombo.options.length+=numItems;
  for (var x=0; x<srcCombo.options.length; x++) {
    if (srcCombo.options[x].selected==true) {
      destCombo.options[curPos].text=srcCombo.options[x].text;
      destCombo.options[curPos].value=srcCombo.options[x].value;
      curPos++;
    }
  }
  if (doSort) SortList(destCombo, intSort, useVals);
  
  // Now remove the selected items from the source listbox
  // This can be very slow for big lists, but I'm not sure how else to do it
  for (var x=srcLen-1; x>=0; x--) {
    if (srcCombo.options[x].selected) srcCombo.options[x]=null;
  }
}

//
// Compare functions used internally by the SortList routine,
//
function ListCompareNums(a,b) {
  var la = parseInt(a.split("{")[0]);
  var lb = parseInt(b.split("{")[0]);
  if (la < lb) return -1;
  if (la > lb) return 1
  return 0;
}

function ListCompareText(a,b) {
  var la = a.toLowerCase();
  var lb = b.toLowerCase();
  if (la < lb) return -1;
  if (la > lb) return 1
  return 0;
}

//
// Quite Fast way of sorting big option lists
//
function SortList(Combo, IntSort, UseVals) {
  var cmbText = new Array(Combo.options.length);
  var cmbItems;
  
  // get copy of  
  for (x=0; x<cmbText.length; x++) {
    cmbText[x]=(UseVals)?Combo.options[x].value + "{" + Combo.options[x].text:Combo.options[x].text + "{" + Combo.options[x].value;
  }
  
  if (IntSort) {
    cmbText.sort(ListCompareNums);
  } else {
    cmbText.sort(ListCompareText);
  }
      
  //rebuild main list, but don't redimension it
  for (x=0; x<cmbText.length; x++) { 
    cmbItems = cmbText[x].split("{");
    Combo.options[x].text=(UseVals)?cmbItems[1]:cmbItems[0];  
    Combo.options[x].value=(UseVals)?cmbItems[0]:cmbItems[1];
  }
}

//
// Select All elements in a Combo List
//
function ComboSelectAll(srcCombo) {
  var srcLen = srcCombo.options.length;

  for (var x=srcLen-1; x>=0; x--) {
    srcCombo.options[x].selected=true;
  }
}

//----------------------------------------
// Menu Functions
//----------------------------------------

var menu_node_parents = new Array();

// Add Node
function Menu_Add_Node(item,parent) {
   menu_node_parents[item] = parent;
}

// Open
function Menu_Open(item) {
  if (menu_node_parents[item]) {
    var parent = menu_node_parents[item];
    if (parent != "") {
      parentobj = document.getElementById(parent);
      if (parentobj) {
        parentobj.style.display="block";
        Menu_Open(parent);
      }
    }
  }
}
// Open
function Menu_Open_V215(item) {
  parentobj = document.getElementById(item);
  parentobj.style.display="block";
  if (menu_node_parents[item]) {
    var parent = menu_node_parents[item];
//   key.innerHTML="<img src='../images/hcatmenu_opened.gif' border='0'>";
    if (parent != "") {
      Menu_Open_V215(parent);
    }
  }
}
function Menu_Open_Node(item) {
  parentobj = document.getElementById(item);
  if (parentobj) {
    parentobj.style.display="block";
    if (menu_node_parents[item]) {
      var parent = menu_node_parents[item];
      if (parent != "") {
        Menu_Open_Node(parent);
      }
    }
  }
}

// Expand
function Expand() {
   divs=document.getElementsByTagName("DIV");
   for (i=0;i<divs.length;i++) {
     divs[i].style.display="block";
//     key=document.getElementById("x" + divs[i].id);
//     key.innerHTML="<img src='textfolder.gif' width='16' height='16' hspace='0' vspace='0' border='0'>";
   }
}

// Collapse All
function Collapse(parent) {
  // Loop though all nodes
//  alert("Collapse: " + parent);
  for (var i in menu_node_parents) {
    if ((menu_node_parents[i] != undefined) && (menu_node_parents[i] == parent)) {
//      alert("menu_node_parents: " + menu_node_parents[i]);
//      alert("node: " + i);
      obj = document.getElementById(i);
      if (obj) {
        obj.style.display="none";
      }
    }   
  }
}

// Toggle
function Toggle(item,collapse) {
//  alert("Toggle: " + item);
  if (collapse == 1) {
    var parent = menu_node_parents[item];
    Collapse(parent);
  }
  obj = document.getElementById(item);
  if (obj.style.display != "none") {
    obj.style.display="none";
  } else {
    obj.style.display="block";
  }
}



var hidedelay = 400;
var menuarrayvis = new Array();

function submenu_addnode(pid,id) {

  //alert("Parent Id: " + pid + "\nId: " + id);

  menuarrayvis[id] = "none";
  //menuarrayvis[id] = "hidden";
  //menuarrayvis[id] = "none";
  submenu_position(pid,id);
}

function submenu_position(pid,id) {

//  if (browser.isNS4up) {
//    width = document.getElementById(pid).clip.width;
//    height = document.getElementById(pid).clip.height;
//  } else {
//    width = document.getElementById(pid).offsetWidth;
//    height = document.getElementById(pid).offsetHeight;
//  }
  menupos = element_offset(document.getElementById(pid));

  //alert("Width: " + width + ", Menu X: " + menupos[0] + ", Menu Y: " + menupos[1]);
  //alert("Menu X: " + menupos[0] + "\nMenu Y: " + menupos[1]);


  //document.getElementById(id).style.minwidth = width;
  
  // Setup Minimum Width
  //document.getElementById(id).style.width = "auto";
  document.getElementById(id).style.left = menupos[0];
  document.getElementById(id).style.top = menupos[1] + menupos[3];
 
//  document.getElementById(id).style.wordWrap = "normal";
}

/*
function element_offset(e) {
  eo = new Array();
  eo[0] = 0;
  eo[1] = 0;
  if (e.offsetParent) {
    while (e.offsetParent) {
        eo[0] += e.offsetLeft;
        eo[1] += e.offsetTop;
        e = e.offsetParent;

       alert("Left: " + eo[0] + ", Top: " + eo[1]);
    }
  } else if (obj.x) {
    eo[0] += e.x;
    eo[1] += e.y;
  }
  return eo;
}
*/

function element_offset(e) {
  eo = new Array();
  eo[0] = 0;
  eo[1] = 0;
  eo[2] = 0;
  eo[3] = 0;
  while (e) {
    if (e.tagName != "TABLE") {
      eo[0] += e.offsetLeft;
      eo[1] += e.offsetTop;
    }
    if (e.tagName == "BODY" && lastElement == "TABLE") {
      eo[0] += lastOffsetLeft;
      eo[1] += lastOffsetTop;
    }

    lastElement = e.tagName;
    lastOffsetLeft = e.offsetLeft;
    lastOffsetTop = e.offsetTop;
    
    if (eo[2] == 0) {eo[2] = e.offsetWidth;}
    if (eo[3] == 0) {eo[3] = e.offsetHeight;}
    
    e = e.offsetParent;
  }

  //alert("Left: " + eo[0] + "\nTop: " + eo[1] + "\nWidth: " + eo[2] + "\nHeight: " + eo[3]);

  return eo;
}


function dropmenu_hideit(id) {
  menuarrayvis[id] = "none";
  //menuarrayvis[id] = "none";
  setTimeout("dropmenu_showhide('" + id + "')",hidedelay)
}

function dropmenu_showit(id) {

  for(var arrayname in menuarrayvis) {
    if (arrayname != id) {
      //menuarrayvis[arrayname] = hidestr;
      menuarrayvis[arrayname] = "none";
      dropmenu_showhide(arrayname);
    }
  }

  //menuarrayvis[id] = showstr;
  //menuarrayvis[id] = "visible";
  menuarrayvis[id] = "block";
  dropmenu_showhide(id);
}

function dropmenu_showhide(id) {

  //alert("MENU ID: " + id + "\nstyle.display: " + menuarrayvis[id]);
  obj = document.getElementById(id);
  obj.style.display = menuarrayvis[id];

//  if (browser.isIE4up) {
//    document.getElementById(id).visibility = menuarrayvis[id];
//  } else {
//    obj.display = menuarrayvis[id];
//    document.getElementById(id).style.visibility = menuarrayvis[id];
//  }
}

//----------------------------------------
// Dreamweaver Stuff
//----------------------------------------
function MM_swapImgRestore() {
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() {
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImage() {
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_jumpMenu(targ,selObj,restore){
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function MM_showHideLayers() { //v3.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
    obj.visibility=v; }
}

function MM_nbGroup(event, grpName) { //v3.0
  var i,img,nbArr,args=MM_nbGroup.arguments;
  if (event == "init" && args.length > 2) {
    if ((img = MM_findObj(args[2])) != null && !img.MM_init) {
      img.MM_init = true; img.MM_up = args[3]; img.MM_dn = img.src;
      if ((nbArr = document[grpName]) == null) nbArr = document[grpName] = new Array();
      nbArr[nbArr.length] = img;
      for (i=4; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
        if (!img.MM_up) img.MM_up = img.src;
        img.src = img.MM_dn = args[i+1];
        nbArr[nbArr.length] = img;
    } }
  } else if (event == "over") {
    document.MM_nbOver = nbArr = new Array();
    for (i=1; i < args.length-1; i+=3) if ((img = MM_findObj(args[i])) != null) {
      if (!img.MM_up) img.MM_up = img.src;
      img.src = (img.MM_dn && args[i+2]) ? args[i+2] : args[i+1];
      nbArr[nbArr.length] = img;
    }
  } else if (event == "out" ) {
    for (i=0; i < document.MM_nbOver.length; i++) {
      img = document.MM_nbOver[i]; img.src = (img.MM_dn) ? img.MM_dn : img.MM_up; }
  } else if (event == "down") {
    if ((nbArr = document[grpName]) != null)
      for (i=0; i < nbArr.length; i++) { img=nbArr[i]; img.src = img.MM_up; img.MM_dn = 0; }
    document[grpName] = nbArr = new Array();
    for (i=2; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
      if (!img.MM_up) img.MM_up = img.src;
      img.src = img.MM_dn = args[i+1];
      nbArr[nbArr.length] = img;
  } }
}


function HCat_sirest() {
  var i,x,a=document.HCat_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function HCat_findobj(n, d) {
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
  }
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=HCat_findobj(n,d.layers[i].document); return x;
}
function HCat_si() {
  var i,j=0,x,a=HCat_si.arguments; document.HCat_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=HCat_findobj(a[i]))!=null){document.HCat_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


//----------------------------------------
// Printing
//----------------------------------------
function HCat_PrintWindow() {
  window.print();
  top.close();
}

//----------------------------------------
// Marquee
//----------------------------------------
var actualwidth='';
var cross_marquee;
var ns_marquee;
var iedom=document.all||document.getElementById;

function Populate_Marquee() {
  if (iedom) {
    cross_marquee=document.getElementById? document.getElementById("iemarquee") : document.all.iemarquee
    cross_marquee.style.left=parseInt(marqueewidth)+8+"px"
    cross_marquee.innerHTML=marqueecontent
    actualwidth=document.all? temp.offsetWidth : document.getElementById("temp").offsetWidth
  } else if (document.layers) {
    ns_marquee=document.ns_marquee.document.ns_marquee2
    ns_marquee.left=parseInt(marqueewidth)+8
    ns_marquee.document.write(marqueecontent)
    ns_marquee.document.close()
    actualwidth=ns_marquee.document.width
  }
  lefttime=setInterval("scrollmarquee()",20)
}

function scrollmarquee() {
  if (iedom) {
    if (parseInt(cross_marquee.style.left)>(actualwidth*(-1)+8))
      cross_marquee.style.left=parseInt(cross_marquee.style.left)-copyspeed+"px"
    else
      cross_marquee.style.left=parseInt(marqueewidth)+8+"px"
  } else if (document.layers) {
    if (ns_marquee.left>(actualwidth*(-1)+8))
      ns_marquee.left-=copyspeed
    else
      ns_marquee.left=parseInt(marqueewidth)+8
  }
}

//----------------------------------------
// Charater Based Totals
//----------------------------------------
var cbtbv = 0;
var cbtav1 = 0;
var cbtav2 = 0;
var cbtav3 = 0;
var cbtce1;
var cbtce2;
var cbtce3;
var cbttid;
var cbtiid;
var cbtccid;
function cbt_init(bv,av1,av2,av3,cid1,cid2,cid3,tid,iid,ccid) {
  cbtbv = bv;
  cbtav1 = av1;
  cbtav2 = av2;
  cbtav3 = av3;
  cbtce1 = document.getElementById(cid1);
  cbtce2 = document.getElementById(cid2);
  cbtce3 = document.getElementById(cid3);
  cbttid = tid;
  cbtiid = iid;
  cbtccid = ccid;
  cbt_update();
}
function cbt_update() {
  var total = 0;
  var cctotal = 0;
  if (cbtce1) {
    mystr = cbtce1.value.removews();
    total = total + (mystr.length * cbtav1);
    cctotal = cctotal + mystr.length;
  }
  if (cbtce2) {
    mystr = cbtce2.value.removews();
    total = total + (mystr.length * cbtav2);
    cctotal = cctotal + mystr.length;
  }
  if (cbtce3) {
    mystr = cbtce3.value.removews();
    total = total + (mystr.length * cbtav3);
    cctotal = cctotal + mystr.length;
  }

  if (cbtiid) {
      total_e = document.getElementById(cbtiid);
      total_e.innerHTML = total.toFixed(2);
  }

  if (cbtccid) {
      total_cc = document.getElementById(cbtccid);
      total_cc.innerHTML = cctotal;
  }

  total = total + (cbtbv * 1);
  if (cbttid) {
    total_e = document.getElementById(cbttid);
    total_e.innerHTML = total.toFixed(2);
  }
}


var oSelection;
function ModalDialog(sUrl) {
   //This string specifies the features of the Dialog Box
   Args = "dialogHeight: 400px; dialogWidth: 500px; edge: Raised; center: Yes; help: No; resizable: No; status: No";

   // Show a Modal Dialog, pass the oSelection parameter
   // and display the result
   alert(window.showModalDialog(sUrl, oSelection, Args));

   //Show the Region and Territory returned from the Modal Dialog Box
//   txtRegion.value = oSelection.Region;
//   txtTerritory.value = oSelection.Territory;
}

//----------------------------------------
// Remove Spaces
//----------------------------------------
String.prototype.removews = function() {
    return this.replace(/\s/g,"");
}

// Days in Month
function daysInMonth(Month, Year) {
    return 32 - new Date(Year, Month, 32).getDate();
}


