﻿// JScript File

function addTextBox(cell, id, name, disp, value, cssClass)
{
    var txtBox = document.createElement("INPUT");
    
    cell.appendChild(txtBox);
    txtBox.id = id;
    txtBox.name = name;
    if (cssClass) txtBox.className = cssClass;
    if (disp) txtBox.style.display = disp;
    else txtBox.style.display = "";
    if (value) txtBox.value = value;
    return txtBox;
}

function addSelect(cell, kss, id, name, disp, cssClass, selected)
{
    var attr, opt;
    var selFound = false;
    var select = document.createElement("SELECT");
    var ks = (typeof(kss) == "string" ? new ks_keyString(kss) : kss);
    
    cell.appendChild(select);
    select.id = id;
    select.name = name;
    if (cssClass) select.className = cssClass;
    if (disp) select.style.display = disp;
    else select.style.display = "";
    for (var i=0;i<ks.attributes.length;i++)
    {
        attr = ks.attributes[i];
        opt = document.createElement("OPTION");
        addOption(select, opt);
        opt.value = attr.key;
        opt.text = attr.value;
        if (selected && (selected == opt.value))
        {
          opt.selected = true;
          selFound = true;
        }
    }
    if (!selFound) select.selectedIndex = 0;
    return select;
}

function addTextArea(cell, id, name, disp, value, cssClass)
{
    var txtArea = document.createElement("TEXTAREA");
    
    cell.appendChild(txtArea);
    txtArea.id = id;
    txtArea.name = name;
    if (cssClass) txtArea.className = cssClass;
    txtArea.rows = 3;
    if (disp) txtArea.style.display = disp;
    else txtArea.style.display = "";
    if (value) txtArea.value = value;
    return txtArea;
}

function addCheckBox(cell, id, name)
{
    var cb = document.createElement("INPUT");

    cb.type = "checkbox";
    cell.appendChild(cb);
    cb.checked = false;    
    cb.id = id;
    cb.name = name;
    return cb;
}

function addFile(cell, id, name, cssClass)
{
    var file = document.createElement("INPUT");

    file.type = "file";
    cell.appendChild(file);
    file.id = id;
    file.name = name;
    if (cssClass) file.className = cssClass;
    return file;
}

function addHidden(cell, id, name)
{
    var hidden = document.createElement("INPUT");

    hidden.type = "hidden";
    cell.appendChild(hidden);
    hidden.value = "0";    
    hidden.id = id;
    hidden.name = name;
    return hidden;
}

function addButton(cell, id, name, cssClass)
{
    var b = document.createElement("INPUT");

    b.type = "button";
    cell.appendChild(b);
    b.id = id;
    if (name) b.name = name;
    if (cssClass) b.className = cssClass;
    return b;
}

function addImageFlipper(cell, id, name, srcArray, cssClass, height, width, cursor)
{
    if (srcArray && srcArray.length)
    {
        var img;
        
        for (var i=0;i<srcArray.length;i++)
        {
            img = document.createElement("IMG");
            cell.appendChild(img);
            img.id = id + '-' + i;
            if (cssClass) img.className = cssClass;
            if (height) img.style.height = height;
            if (width) img.style.width = width;
            if (cursor) img.style.cursor = cursor;
            if (i == 0) img.style.display = '';
            else img.style.display = 'none';
            img.src = srcArray[i];
            addEvent(img, 'onclick', hif_imageFlipperOnClick)
        }
        if (name)
        {
            var hidden = document.createElement("INPUT");
        
            hidden.type = "hidden";
            hidden.id = id;
            hidden.name = name;
            hidden.value = '0';
            cell.appendChild(hidden);
        }
    }
    return cell;
}

function addDateTable(cell, id, name, format, disp, dd, mm, yyyy, dir, cssClass, yyCssClass, sepCssClass)
{
		var tr, td;
    var dateTable = document.createElement("TABLE");
    var spl = (format ? format.split('/') : new Array('y', 'm', 'd'));
    
    if (spl.length != 3) spl = new Array('y', 'm', 'd');
    else for (var i=0;i<spl.length;i++) spl[i] = spl[i].toLowerCase().charAt(0);
    cell.appendChild(dateTable);
    dateTable.id = id;
    dateTable.border = 0;
    if (dir) dateTable.dir = dir;
    tr = dateTable.insertRow();
    for (var i=0;i<spl.length;i++)
    {
			if (i > 0)
			{
				td = tr.insertCell(-1);
				td.innerHTML = '/';
				td.className = sepCssClass;
			}
			td = tr.insertCell(-1);
			addTextBox(td, id + spl[i] + 'd', name + spl[i] + 'd', '', getDateCellValue(spl[i], yyyy, mm, dd), getDateCellClass(spl[i], yyCssClass, cssClass));
    }
}

function addLabel(cell, id, value, cssClass)
{
    var lbl = document.createElement("SPAN");
    
    cell.appendChild(lbl);
    lbl.id = id;
    if (cssClass) lbl.className = cssClass;
    if (value) lbl.innerHTML = value;
    return lbl;
}

function getDateCellValue(pref, y, m, d)
{
	if (pref == 'y') return y;
	if (pref == 'm') return m;
	return d;
}

function getDateCellClass(pref, yClass, otherClass)
{
	if (pref == 'y') return yClass;
	return otherClass;
}

function rearangeIds(container, startIndex, endIndex)
{
    if (!_tagName(container)) return;
    switch (_tagName(container))
    {
        case "table":
            rearangeIdsInTable(container, startIndex, endIndex);
            break;
    }
}

function rearangeIdsInTable(table, startIndex, endIndex)
{
    var row, cell, ctl, pos, oldIx, idSuff, newId;
    var si = parseInt(startIndex?startIndex:0);
    var ei = parseInt(endIndex?endIndex:table.rows.length);
    
    if (isNaN(si) || (si < 0) || (si > table.rows.length)) si = 0;
    if (isNaN(ei) || (ei < 0) || (ei > table.rows.length)) ei = table.rows.length;
    for (var i=si;i<ei;i++)
    {
        row = table.rows[i];
        for (var j=0;j<row.cells.length;j++)
        {
            cell = row.cells[j];
            ctl = getChildNode(cell, 0);
            while (ctl)
            {
                rearangeControlId(ctl);
                ctl = getNextSibling(ctl);
            }
        }    
    }
}

function rearangeControlIdsInTable(table)
{
  var row, cell, ctl;
  
  for (var i=0;i<table.rows.length;i++)
  {
    row = table.rows[i];
    for (var j=0;j<row.cells.length;j++)
    {
      cell = row.cells[j];
      ctl = getChildNode(cell, 0);
      while (ctl)
      {
          rearangeControlId(ctl);
          ctl = getNextSibling(ctl);
      }
    }
  }
}

function rearangeChildNodesIds(ctl)
{
  if (ctl.childNodes && (ctl.childNodes.length > 0))
  {
    var child = getChildNode(ctl, 0);
    
    while (child)
    {
      rearangeControlId(child);
      child = getNextSibling(child);
    }
  }
}

function rearangeControlId(ctl)
{
  if (ctl.id)
  {
     ctl.id = setIdOrName(ctl.id);
     ctl.name = setIdOrName(ctl.name);
  }
  if (ctl.tagName.toLowerCase() == "table") rearangeControlIdsInTable(ctl)
  else rearangeChildNodesIds(ctl);
}

function setIdOrName(input)
{
  if (input)
  {
    var spl = input.split('-');
    var id = spl[0];
    var oldIx = (spl.length > 1 ? parseInt(spl[1]) : -1);
    var spl1 = spl[spl.length - 1].split('_');
    
    if (spl1.length > 1) spl[spl.length - 1] = spl1[0];
    if (isNaN(oldIx)) oldIx = -1;
    if (oldIx > 0)
    {
       oldIx--;
       id += '-' + oldIx;
       for (var i=2;i<spl.length;i++) id += '-' + spl[i];
       if (spl1.length > 1) for (var i=1;i<spl1.length;i++) id += '_' + spl1[i];
       return id;
    }
    return id;
  }
  return input;
}

function getIndexPosition(id)
{
    var ch;
    
    for (var i=id.length-2;i>=0;i--)
    {
        ch = id.charAt(i);
        if ((ch == '-') || (ch == '_')) return i + 1;
    }
    return -1;
}

function getIndex(id)
{
    var pos = getIndexPosition(id);

    if (pos > 0)
    {
				var ixPart = id.substr(pos);
        var ix = parseInt(ixPart);
				var ixPref = "";
				var currPos = 0;
				var ch = ixPart.charAt(0);
				
				while ((currPos < ixPart.length) && !isNaN(parseInt(ch)))
				{
					ixPref += ch;
					currPos++;
					if (currPos < ixPart.length) ch = ixPart.charAt(currPos);
				}
				ix = parseInt(ixPref);
				if (currPos == ixPart.length) return ix;
				return new Array(ix, ixPart.substr(currPos));
    }
    return -1;
}

function getId(clientId, id, index)
{
  var retId = clientId;
  var ix = parseInt(index);
  
  if (retId && id) retId += '_' + id;
  if (!isNaN(ix) && (ix >= 0)) retId += '-' + ix;
  return retId;
}

function getName(uniqueId, id, index)
{
  var retId = uniqueId;
  
  if (retId) retId += '$';
  retId += id;
  if (index && (index >= 0)) retId += '-' + index;
  return retId;
}

function getClientId(src, offset)
{
    var split;
    var srcId = "";
    var clientId = "";
    var offs = (offset?offset:1);
    
    if ((typeof(src) == "object") && src.id) srcId = src.id;
    else if (typeof(src) == "string") srcId = src;
    split = srcId.split('_');
    if (split.length <= 1) clientId = srcId;
    else for (var i=0;i<split.length-offs;i++)
    {
        if (i > 0) clientId += '_';
        clientId += split[i];
    }
    return clientId;
}

function getUniqueId(clientId)
{
	var unqId = "";
    var split = clientId.split('_');

	for (var i=0;i<split.length;i++)
	{
		if (i > 0) unqId += '$';
		unqId += split[i];
	}
    return unqId;
}

function aip_resize(src)
{
    var goOn = true;
    var parent = src.parentNode;
    
    while (goOn && parent && (parent.nodeType == 1))
    {
        if ((parent.id.indexOf("envelope") > -1) && (typeof(ed_resizeObject) == "function"))
        {
            ed_resizeObject(parent);
            goOn = false;
        }
        else if ((parent.id.indexOf("MailActivities") > -1) && (typeof(mm_resizeObject) == "function"))
        {
            mm_resizeObject(parent);
            goOn = false;
        }
        else if ((parent.id.indexOf("pnlActivities") > -1) && (typeof(hm_resizeObject) == "function"))
        {
            hm_resizeObject(parent);
            goOn = false;
        }
        if (goOn) parent = parent.parentNode;
    }
}

function setSelectedIndex(select, value)
{
    if (_tagName(select) == "select")
    {
        var index = -1;
        var opt = getChildNode(select.options, 0);
        
        while (opt)
        {
            index++;
            if ((opt.value == value) || (opt.text == value))
            {
                opt.selected = true;
                return index;
            }
            opt = getNextSibling(opt);
        }
        return -1;
    }
}

function setParentHeight(parent, exclude)
{
    var top;
    var h = 0;
    var child = getChildNode(parent, 0);
    
    
    while (child)
    {
        top = parseInt(child.style.top);
        if (isNaN(top)) top = 0;
        if ((top > -20) &&
            (_tagName(child) != "script") &&
            (child.type != "hidden") &&
            (child.style.display == "") &&
            !controlExcluded(child.id, exclude)) h += child.offsetHeight;
        child = getNextSibling(child);
    }
    parent.style.height = h + 20 + "px";
}

function controlExcluded(id, excludeList)
{
    if (excludeList)
    {
        var exc;
        
        if (typeof(exludeList) == "String") return (id == excludeList);
        if (excludeList.length) for (var i=0;i<excludeList.length;i++)
        {
            exc = excludeList[i];
            if ((typeof(exc) == "String") && (exc == id)) return true;
        }
    }
    return false;
}

var hc_lblList = null;

function getLabel(byFor, doReset)
{
  if (!hc_lblList || doReset) hc_lblList = document.getElementsByTagName("LABEL");
  
  for (var i=0;i<hc_lblList.length;i++)
    if (hc_lblList[i].getAttribute("for") == byFor) return hc_lblList[i];
  return null;
}
