function frame_width(f,w)
{
  var mainTable = f.mainTable;
  if ( mainTable ) {
    return mainTable.clientWidth < w ? mainTable.clientWidth : w; 
  }
  return 0;
}

function frame_height(f,h)
{
  var mainTable = f.mainTable;
  if ( mainTable ) {
    return mainTable.clientHeight < h ? mainTable.clientHeight : h;
  }
  return 0;
}

function showAttribute(obj)
{
  var str = "";
  var k = 0;
  for( var p in obj ) {
    str += "," + p;
    if ( k++ == 5 ) {
      k = 0;
      str += "\n";
    }
  }
  alert( str );
}

function set_focus(obj)
{
  if ( obj == null ) {
    obj = document;
  } else {
		if ( obj.tagName == "INPUT" && obj.type != "hidden" && !obj.disabled && !obj.readOnly) {
      try {
        obj.focus();
        return true;;
      } catch ( e ) { }
		}
		if ( obj.tagName == "SELECT" && !obj.disabled && !obj.readOnly) {
      try {
        obj.focus();
        return true;;
      } catch ( e ) { }
		}
		if ( obj.tagName == "TEXTAREA" && !obj.disabled && !obj.readOnly) {
      try {
        obj.focus();
        return true;;
      } catch ( e ) { }
		}
  }
  var child = obj.firstChild;
  while( child != null ) {
    //alert( child.tagName );
    if ( set_focus(child) )
      return true;
    child = child.nextSibling
  }
  return false;
}

function do_resize()
{
	if ( opener ) {
    var divContent = document.getElementById("divContent");
    var mainTable = document.getElementById("mainTable");
    if ( mainTable ) {
      var ww = mainTable.clientWidth; 
      if ( ww > 1000 ) {
        ww = 1000;
        if ( divContent ) {
          //divContent.style.width = 900;
          //divContent.style.overflow = "scroll";
        }
      }
      var w = 0;
      if ( document.all ) 
        w =  ww - document.body.clientWidth + document.body.clientLeft;
      else
        w = ww - document.width + 2;
      window.resizeBy(w,0);
      var hh = mainTable.clientHeight; 
      if ( hh > 640 ) {
        window.resizeBy(20,0);
        hh = 640;
        if ( divContent ) {
          //divContent.style.width = ww + 20;
          //divContent.style.height = 590;
          //divContent.style.overflow = "scroll";
        }
      }
      var h;
      if ( document.all ) 
        h =  hh - document.body.clientHeight + document.body.clientTop;
      else
        h = hh - document.body.clientHeight + 2;
      window.resizeBy(0,h);
    }
	}
}

function popUp(url, title, size, other_features)
{
	if (size == null)
		size = "width=800,height=600";
	if (other_features == null)
		other_features = "top=0,left=0,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1";
	var popup = window.open(url, title, size+","+other_features);
	if (popup != null)
		popup.focus();
}

function open_input_form(url, title, size, other_features)
{
	if (size == null)
		size = "width=600,height=400";
	if (other_features == null)
		other_features = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1";
	var popup = window.open(url, title, size+","+other_features);
	if (popup != null)
		popup.focus();
	return popup;
}

function open_search(url, title)
{
  var searchPannel = document.getElementById("SearchPannel");
  if ( searchPannel != null ) {
    searchPannel.style.display = searchPannel.style.display == '' ? 'none' : '';
    return;
  }
  open_input_form(url, title);
}

function open_select_form(url, title, element)
{
	window.src_element = element;
	var popup = window.open(url, title, "width=460,height=280,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1");
	if (popup != null)
		popup.focus();
	return popup;
}

function delete_confirm()
{
	return confirm("Are you sure to delete selected record?");
}

function update_confirm()
{
  var count = 0;
  var list = thisForm["this"].value.split(",");
  for(var i=0; i<list.length; i++) {
    if ( list[i].charAt(0) == '-' ) {
      count++;
    }
  }
  if ( count > 0 ) {
    return confirm(msgDeleteConfirm);
  }
  return true;
} 

function findParent(node, tag, n)
{
  var e = node;
  while( e != null ) {
    if ( e.tagName == tag ) {
      if ( n == 0 ) {
        return e;
      }
      n--;
    }
    e = e.parentNode;
  }
  return null;
}

function findPrevious(node, tag, n)
{
  var e = node;
  while( e != null ) {
    if ( e.tagName == tag ) {
      if ( n == 0 )
        return e;
      n--;
    }
    e = e.previousSibling;
  }
  return null;
}

function findNext(node, tag, n)
{
  var e = node;
  while ( e != null ) {
    if ( e.tagName == tag ) {
      if ( n == 0 )
        return e;
      n--;
    }
    e = e.nextSibling;
  }
  return null;
}


function checkSingleSelect(check_me){
    if(isMultiSelect)
        return ;
    if(check_me.checked==false) {
      opener.src_element.value = "";
      //opener.src_element.nextSibling.value = "";
      findNext(opener.src_element, "INPUT", 1).value = "";
      return ;
    } else {
      opener.src_element.value = check_me.value;
      //opener.src_element.nextSibling.value = findNext(check_me.parentNode, "TD", 2).innerText;
      findNext(opener.src_element, "INPUT", 1).value = findNext(check_me.parentNode, "TD", 2).innerText;
    }
    
	var inp = document.all.tags("INPUT");
	for(var i=0; i<inp.length; i++)
	{
		if( inp[i].type == "checkbox" && inp[i]!=check_me)
		{
			if(inp[i].checked){
			    inp[i].checked=false;
			    return;
			}
	    }
	}
    
}

function doSelect(evt, id, tr)
{
  if ( o ) 
  { 
    if ( evt == null ) evt = window.event ? window.event : null;
    if ( isMultiSelect ) {
      var A = new Array();
      if ( o.value != "" ) 
      {
        A = o.value.split(",");
      }
      var inp = document.getElementsByTagName("INPUT");
      for(var i=0; i<inp.length; i++)
      {
        if( inp[i].type == "checkbox" )
        {
          if ( inp[i].value == id ) {
            inp[i].checked = !inp[i].checked;
          }
          var tid = inp[i].value;
          if ( inp[i].checked ) 
          {
            add_element(A,tid);
          } 
          else 
          {
            A = remove_element(A,tid);
          }
        }
      }
      o.value = A.join(",");
    } else {
      if ( id == null ) {
        var srcE = evt.srcElement ? evt.srcElement : evt.target;
        id = srcE.value;
        tr = findParent(srcE, "TR", 1); //srcE.parentNode.parentNode;
      }
      o.value = id;
      var disp = '';
      if ( tr != null ) {
        if ( tr.cells.length > 2 ) {
          disp = tr.cells[2].innerHTML;
        } else {
          disp = findNext(tr.cells[0].firstChild, "SPAN", 4).innerHTML;
        }
      }
      var display = o.form[o.name+"_DISP"];
      if ( display != null ) {
        display.value = disp;
      }
      if ( o.onchange )
        o.onchange(id,disp);
        
      var inp = document.getElementsByTagName("INPUT");
      for(var i=0; i<inp.length; i++)
      {
        if( inp[i].type == "checkbox" )
        {
          inp[i].checked = inp[i].value == id;
        }
      }
    }
    if ( evt ) {
      if (evt.stopPropagation){
        evt.stopPropagation();
        //evt.preventDefault();
      }else if(typeof evt.cancelBubble != "undefined"){
        evt.cancelBubble = true;
        //evt.returnValue = false;
      }
    }
  }
}

function checkbox_init(key)
{
	var A = key.split(",");
	var inp = document.getElementsByTagName("INPUT");
	for(var i=0; i<inp.length; i++)
	{
		if( inp[i].type == "checkbox" )
		{
			var id = inp[i].value;
			for(var j=0; j<A.length; j++)
			{
				if ( id == A[j] ) 
				{
					inp[i].checked = true;
				}
			}
		}
	}
}

function add_element(A, key)
{
	var exist = false;
	for(var i=0; i<A.length; i++)
	{
		if ( A[i] == key )
		{
			exist = true;
			break;
		}
	}
	if ( !exist ) 
	{
		A[A.length] = key;
	}
}

function remove_element(A, key)
{
	var B = new Array();
	for(var i=0; i<A.length; i++) {
		if ( A[i] != key ) {
			B[B.length] = A[i];
		}
	}
	return B;
}

function select_save()
{
        
	if ( isMultiSelect && opener && opener.src_element ) 
	{

		var A = new Array();
		//if ( isMultiSelect && opener.src_element.value != "" ) 
		if ( opener.src_element.value != "" ) 
		{
			A = opener.src_element.value.split(",");
		}
		var inp = document.all.tags("INPUT");
		for(var i=0; i<inp.length; i++)
		{
			if( inp[i].type == "checkbox" )
			{
				var id = inp[i].value;
				if ( inp[i].checked ) 
				{
					add_element(A,id);
				} 
				else 
				{
					A = remove_element(A,id);
				}
			}
		}
		opener.src_element.value = A.join(",");
	}
	//self.close();
}

function xmlEncode(value)
{
	var A = new Array();
	A = value.split("&");
	str = A.join("&amp;");
	A = str.split("<");
	str = A.join("&lt;");
	A = str.split(">");
	str = A.join("&gt;");
	A = str.split("\"");
	str = A.join("&quot;");
	A = str.split("\'");
	str = A.join("&apos;");
	return str;
}

function getSingleControlValue(obj)
{
	if ( obj.tagName == "INPUT" && obj.type == "checkbox" ) {
		if ( obj.checked == true )
			obj.value = 'Y';
		else
			obj.value = 'N';
	}
	return obj.value;
}

function getControlValue(obj)
{
	if ( obj != null ) {
		if ( obj.tagName ) {
			return getSingleControlValue(obj);
		} else {
			if ( obj.length ) {
				for (var i=0; i<obj.length; i++) {
					if ( obj[i].tagName == "INPUT" ) {
						if ( obj[i].type == "radio" ) {
							if ( obj[i].checked ) {
								return obj[i].value;
							}
						} else 
						if ( obj[i].type == "checkbox" ) {
							if ( obj[i].checked ) {
								return obj[i].value;
							}
						}
					}
				}
			}
		}
	}
}

function getXML(obj, tag)
{
	var strXml = "";
	if ( obj != null ) {
		var v = getControlValue(obj);
		if ( v != null && v != '' ) {
			strXml = "<" + tag + ">";
			strXml += xmlEncode(v);
			strXml += "</" + tag + ">";
		}
	}
	return strXml;
}

function getBooleanXML(obj, tag)
{
	var strXml = "";
	if ( obj != null ) {
		var v = getControlValue(obj);
		strXml = "<" + tag + ">";
		strXml += (v == "Y") ? "true" : "false";
		strXml += "</" + tag + ">";
	}
	return strXml;
}

function getDateXML(obj, tag)
{
	
}

function getDetailXML(obj, tag, tag2)
{
	var strXml = "";
	if ( obj != null ) {
		if ( obj.tagName ) {
			if ( obj.value != '' ) {
				var keys = new Array();
				keys = obj.value.split(",");
				for(var i=0; i<keys.length; i++)
				{
					strXml += "<" + tag + "><" + tag2 + ">" + keys[i] + "</" + tag2 + "></" + tag + ">";
				}
			}
		} else {
			if ( obj.length ) {
				for(var j=0; j<obj.length; j++) {
					if ( obj[j].tagName == "INPUT" ) {
						if ( obj[j].type == "checkbox" ) {
							if ( obj[j].checked ) {
								strXml += "<" + tag + "><" + tag2 + ">" + obj[j].value + "</" + tag2 + "></" + tag + ">";
							}							
						}
					} 
				}
			}
		}
	}
	return strXml;
}

function input_form_init(f, query)
{
	var A = new Array();
	A = query.split("&");
	for(var i=0; i<A.length; i++)
	{
		if ( A[i].charAt(0) == '_' )
		{
			var index = A[i].indexOf("=");
			if ( index != -1 )
			{
				var ename = A[i].substring(0,index);
				var e = f[ename];
				if ( e != null ) {
					var v = A[i].substring(index+1);
					e.value = v;
					e.disabled = true;
				}
			}
		}
	}
	
}

function clearOrderBy(f)
{
  var isChange = false;
  var inps = f.getElementsByTagName("INPUT");
  if ( inps != null ) {
    for(var i=0; i<inps.length; i++) {
      var inp = inps[i];
      if ( inp.type == "hidden" && inp.name.match(/^_\./) && inp.value.match(/./) ) {
        inp.value = "";
        isChange = true;
      }
    }
  }
  if ( isChange )
    f.submit();
  return false;
}

function setIndexedOrderBy(f, n)
{
  var ndxMax = 0;
  var inps = f.getElementsByTagName("INPUT");
  if ( inps != null ) {
    for(var i=0; i<inps.length; i++) {
      var inp = inps[i];
      if ( inp.type == "hidden" && inp.name.match(/^_\./) ) {
        var dd = inp.value.match(/^(\d*),/);
        if ( dd != null ) {
          var sn = parseInt(dd[1],10);
          if ( !isNaN(sn) && sn > ndxMax )
            ndxMax = sn;
        }
      }
    }
  }
  ndxMax++;

	var e = f["_."+n];
  var v = e.value;
	if ( v == "" ) {
		e.value = String(1000+ndxMax).substring(1) + ",ASC";
	} else if ( v.match(/^\d*,ASC$/) ) {
		e.value = v.substring(0,v.indexOf(','))+",DESC";
	} else {
		e.value = "";
    var tn = v.match(/^(\d*),/);
    if ( tn != null && inps != null ) {
      var tnv = parseInt(tn[1],10);
      for(var i=0; i<inps.length; i++) {
        var inp = inps[i];
        if ( inp.type == "hidden" && inp.name.match(/^_\./) ) {
          var dd = inp.value.match(/^(\d*)(,.*)$/);
          if ( dd != null ) {
            var sn = parseInt(dd[1],10);
            if ( !isNaN(sn) && sn > tnv ) {
              inp.value = String(1000+sn-1).substring(1) + dd[2];
            }
          }
        }
      }
    }
	}
	f.submit();
	return false;
}

function setOrderBy(f, n)
{
	var e = f["_."+n];
	if ( e.value == "" ) {
		e.value = "ASC";
	} else if ( e.value == "ASC" ) {
		e.value = "DESC";
	} else {
		e.value = "";
	}
	f.submit();
	return false;
}

function invalid_data(f,m)
{
  alert(m);
  try {
    f.focus();
  } catch ( e ) {
  }
  return false;
}

function check_string(f,l,m,re)
{
  if ( re ) {
    if ( !re.test(f.value) ) {
      return invalid_data(f,m);
    }
  }
  if ( f.value.length > l ) {
    return invalid_data(f,m);
  }
  return true;
}

function check_VARCHAR2(f,l,m,re)
{
  return check_string(f,l,m,re);
}

function check_VARCHAR(f,l,m,re)
{
  return check_string(f,l,m,re);
}

function check_CHAR(f,l,m,re)
{
  return check_string(f,l,m,re);
}

function check_NUMBER(f,l,m,re)
{
  if ( re ) {
    if ( !re.test(f.value) ) {
      return invalid_data(f,m);
    }
  }
  var exp = new RegExp("^\\d{0," + l + "}$");
  if ( ! exp.test(f.value) ) {
    return invalid_data(f,m);
  }
  return true;
}

function check_CLOB(f,l,m,re)
{
  return true;
}

function check_DATE(f, df, m, re)
{
  if ( re ) {
    if ( !re.test(f.value) ) {
      return invalid_data(f,m);
    }
  }
  if ( f.value.match(/./) != null ) {
    var dateValue = f.value;
    var dateFormat = df.toUpperCase();
    var date = myPraseDate(dateValue, dateFormat);
    if ( date == null || dateValue != formatDate(date, dateFormat) ) {
      return invalid_data(f, m );
    }
  }
  return true;
}

function check_DATETIME(f, df, m, re)
{
  if ( re ) {
    if ( !re.test(f.value) ) {
      return invalid_data(f,m);
    }
  }
  if ( f.value.match(/./) != null ) {
    var dateValue = f.value;
    var dateFormat = df.toUpperCase().replace(/ HH:MM$/,'');
    var rexp = /( (([0-1][0-9])|(2[0-3])):([0-5][0-9]))$/;
    var tm = dateValue.match(rexp);
    if ( !tm )
      return invalid_data(f,m);
    dateValue = dateValue.replace(rexp, '');
    var date = myPraseDate(dateValue, dateFormat);
    if ( date == null || dateValue != formatDate(date, dateFormat) ) {
      return invalid_data(f, m );
    }
  }
  return true;
}

function myPraseDate(dateValue,dateFormat)
{
  var y,m,d;
  switch( dateFormat ) {
  case "YYYY/MM/DD":
    y = parseInt(dateValue.substring(0,4),10);
    m = parseInt(dateValue.substring(5,7),10)-1;
    d = parseInt(dateValue.substring(8,10),10);
    break;
  case "MM/dd/yyyy":
  case "MM/DD/YYYY":
    m = parseInt(dateValue.substring(0,2),10)-1;
    d = parseInt(dateValue.substring(3,5),10);
    y = parseInt(dateValue.substring(6,10),10);
    break;
  case "dd/MM/yyyy":
  case "DD/MM/YYYY":
    d = parseInt(dateValue.substring(0,2),10);
    m = parseInt(dateValue.substring(3,5),10)-1;
    y = parseInt(dateValue.substring(6,10),10);
    break;
  default:
    //alert(dateFormat);
    return null;
    //break;
  }
  if ( isNaN(y) || isNaN(m) || isNaN(d) )
    return null;
  var date = new Date(y,m,d);
  return date;  
}

function formatDate(tm, df)
{
  if ( df == null ) {
    df = gDateFormat;
  }
  var yyyy = tm.getFullYear();
  var yy = String(tm.getYear()+100).substring(1);
  var MM = String(tm.getMonth()+101).substring(1);
  var dd = String(tm.getDate()+100).substring(1);
  var dStr = df.replace("YYYY", yyyy);
  dStr = dStr.replace("YY", yy);
  dStr = dStr.replace("MM", MM);
  dStr = dStr.replace("DD", dd);
  return dStr;
}

function check_INT(f, m)
{
  return true;
}

function check_BIGINT(f, m)
{
  return true;  
}

function check_NVARCHAR(f,l,m,re)
{
  return check_string(f,l,m,re);
}

function loadCheckboxValue(inputName,prefix)
{
	if (prefix == null)
		prefix = "";
   var value_obj = opener.document.forms[0][prefix+"_"+inputName];
   if (value_obj != null && value_obj.value != ""){
   		var value = ","+value_obj.value+",";
		var obj = document.forms[0]["_"+inputName];
		if (obj != null){
			if(obj.length) {
				for (var i=0; i<obj.length; i++){
					if ( value.indexOf(",'"+obj[i].value+"',") != -1) 
					{
						obj[i].checked = true;
					} 
				}
			} else {
				if ( value.indexOf(",'"+obj.value+"',") != -1) 
				{
					obj.checked = true;
				} 
			}
		}
	}
}

function setCheckboxValue(inputName,prefix)
{
	if (prefix == null)
		prefix = "";
    var A = new Array();
	var obj = document.forms[0][prefix+"_"+inputName];
	if (obj != null){
		if(obj.length) {
			for (var i=0; i<obj.length; i++){
				if (obj[i].checked) 
				{
					add_element(A,"'"+obj[i].value+"'");
				} 
			}
		} else {
			if (obj. checked) 
			{
				add_element(A,"'"+obj.value+"'");
			} 
		}
	}
	opener.document.forms[0][prefix+"_"+inputName].value = A.join(",");
	//alert(A.join(","));
}


function createNamedElement(doc, tag, name, type, value)
{
  var obj = null;
  if ( name != null ) {
    try {
      obj = doc.createElement("<INPUT name='" + name + "'/>");
    } catch ( e ) { }
  }
  if ( !obj || obj.nodeName != tag.toUpperCase() ) {
    obj = doc.createElement(tag);
    if ( name != null ) {
      obj.name = name;
    }
  }
  if ( type != null ) 
    obj.type = type;
  if ( value != null )
    obj.value = value;
  return obj;
}

function mark_disabled_field(f, fields, query)
{
  var A = query.split("&");
  var B = new Array();
  for(var i=0; i<A.length; i++) {
    var p = A[i];
    var index = p.indexOf("=");
    if ( index != -1 ) {
      B[p.substring(0,index)] = p.substring(index+1);
    }
  }
  for(var j=0; j<fields.length; j++) {
    var ename = fields[j];
    var val = B["_"+ename];
    if ( val != null ) {
      var e = f[ename];
      if ( e != null ) {
        e.value = val;
        e.disabled = true;
        var span = createNamedElement(f.ownerDocument, "SPAN");
        if ( e.tagName == "SELECT" ) {
          if ( e.selectedIndex < 0 ) {
            span.innerHTML = '';
          } else 
            span.innerHTML = e.options[e.selectedIndex].text;
        } else
          span.innerHTML = val;
        e.parentNode.insertBefore(span,e);
        e.parentNode.removeChild(e);
        var ne = createNamedElement(f.ownerDocument, "INPUT", ename, "hidden", val);
        f.appendChild(ne);
      }
    }
  }
}

function getObjectList()
{
  var eles = p.document.getElementsByName(o);
  if ( eles != null && eles.length > 0 ) {
    var ol = eles[0].value.split(",");
    var j = 0;
    for(var i=0; i<ol.length; i++) {
      if ( ol[i] != '' ) {
        objArray[j++] = ol[i];
        var oTR = null;
        var ch = ol[i].charAt(0);
        if ( ch == '+' ) {
          oTR = add_new_row(ol[i]);
        } else if ( ch == '-' ) {
          var oId = ol[i].substring(1);
          oTR = document.getElementById(oId);
          if ( oTR != null ) {
            var chkdel = oTR.getElementsByTagName("INPUT")[0];
            if ( chkdel.value == oId ) {
              chkdel.checked = true;
              var img = oTR.getElementsByTagName("IMG")[0];
              img.src = img.src.replace(/\/[^\/]*\.gif$/,"/delete.gif");
            }
          }
        } else {
          oTR = document.getElementById(ol[i]);
        }
        if ( oTR != null ) {
          for(var j=0; j<oTR.cells.length; j++) {
            var oTD = oTR.cells[j];
            if ( linkObject(oTD,oTR) ) {
              continue;
            }
            var oDIV = oTD.firstChild;
            while ( oDIV != null ) {
              if ( oDIV.tagName == 'DIV' || oDIV.tagName == 'SPAN' ) {
                linkObject(oDIV,oTR);
              }
              oDIV = oDIV.nextSibling;
            }
          }
        }
      }
    }
  }
}

function linkObject(oTD,oTR)
{
  var ename = oTD.getAttribute("tag");
  if ( ename != null ) {
    oTD.setAttribute("linkName", o+"["+oTR.id+"]."+oTD.getAttribute("tag"));
    var linkObj = getLinkedObject(oTD);
    if ( linkObj != null ) {
      var txt = linkObj.getAttribute("dispText");
      oTD.innerHTML = txt != null ? txt : linkObj.value;
      var img = oTR.getElementsByTagName("IMG")[0];
      if ( !img.src.match(/delete\.gif$/) )
        img.src = img.src.replace(/\/[^\/]*\.gif$/,"/but_modify.gif");
      oTR.setAttribute("isUpdated",true);
    }
    return true;
  }
  return false;
}

function updateObjectList()
{
  var eles = p.document.getElementsByName(o);
  if ( eles != null && eles.length > 0 ) {
    eles[0].value = objArray.join(",");
  }
}

function removeObject(oid)
{
  for(j=0; j<objArray.length; j++) {
    if ( objArray[j] == oid ) {
      objArray.splice(j,1);
      updateObjectList();
      return;
    }
  }
}

function addObject(oid)
{
  var j = objArray.length;
  if ( oid != null ) {
    for(j=0; j<objArray.length; j++) {
      if ( objArray[j] == oid ) {
        return null;
      }
    }
    objArray[j] = oid;
    updateObjectList();
    return oid;
  }
  objArray[j] = "+"+j;
  updateObjectList();
  return "+"+j;
}

function getLinkedObject(oTD)
{
  var linkObjs = p.document.getElementsByName(oTD.getAttribute("linkName"));
  if ( linkObjs != null && linkObjs.length > 0 ) {
    return linkObjs[0];
  }
  return null;
}

function get_linked_value(oTD)
{
  var linkObj = getLinkedObject(oTD);
  if ( linkObj != null ) {
    return linkObj.value;
  }
  var tdVal = oTD.getAttribute("value");
  return tdVal != null ? tdVal : oTD.innerHTML;
}

function reset_input_object(evt)
{
  if ( evt == null ) evt = window.event ? window.event : null;
  if ( evt != null ) {
    var obj = evt.srcElement ? evt.srcElement : evt.target;
    obj.style.top = 0;
    obj.style.left = 0;
    obj.style.display = 'none';
    //obj.form.appendChild(obj);
    //var hpid = obj.getAttribute("helpPannelId");
    //if ( hpid != null ) {
    //  close_help_pannel(hpid);
    //}
  }
}

function do_update(evt)
{
  if ( evt == null ) evt = window.event ? window.event : null;
  if ( evt != null ) {
    var obj = evt.srcElement ? evt.srcElement : evt.target;
    var oTD = document.getElementById(obj.getAttribute("dataSource"));
    if ( oTD != null ) {
      var oTR = ( oTD.tagName == 'DIV' || oTD.tagName == 'SPAN' ) ? oTD.parentNode.parentNode : oTD.parentNode;
      addObject(oTR.id);
      if ( oTD.getAttribute("linkName") == null ) {
        oTD.setAttribute("linkName", o+"["+oTR.id+"]."+oTD.getAttribute("tag"));
      }
      var linkObj = getLinkedObject(oTD);
      if ( linkObj == null ) {
        var newObj = createNamedElement(p.document, "INPUT", o+"["+oTR.id+"]."+oTD.getAttribute("tag"), "hidden");
        linkObj = p.addElement(newObj, o);
      }
      if ( linkObj != null ) 
        linkObj.value = obj.value;
      //obj.form.appendChild(obj);
      if ( obj.tagName == 'SELECT' ) {
        var txt = obj.options[obj.selectedIndex].text;
        if ( linkObj != null ) {
          linkObj.setAttribute("dispText", txt);
        }
        oTD.innerHTML = txt;
        oTD.setAttribute("value", obj.value);
      } else {
        oTD.innerHTML = obj.value;
      }
      //obj.style.display = 'none';
      oTR.setAttribute("isUpdated", true);
      var img = oTR.getElementsByTagName("IMG")[0];
      if ( !img.src.match(/delete\.gif$/) )
        img.src = img.src.replace(/\/[^\/]*\.gif$/,"/but_modify.gif");
    }
  }
}

var editTD = null;
var editControl = null;
function edit(oTD)
{
  if ( !editFrameLoaded ) {
    loadEditFrame(addNewUrl, edit);
    editTD = oTD;
    return;
  }
  if ( oTD == null ) {
    oTD = editTD;
  }
  editTD = null;
  if ( oTD == null ) 
    return;
  //var hh = oTR.clientHeight;
  //for(var i=0; i<oTR.cells.length; i++) {
    //var oTD = oTR.cells[i];
    //alert( oTD.tagName + "," + oTD.getAttribute("tag") );
    var oEd = document.getElementsByName(oTD.getAttribute("tag"));
    if ( oEd != null && oEd.length > 0 ) {
      var e = oEd[0];
      if ( e.tagName == "INPUT" && e.type == "hidden" ) {
        oEd = document.getElementsByName(e.name + "_DISP");
        if ( oEd != null && oEd.length > 0 ) {
          e = oEd[0];
        }
      }
      var cParent = e.getAttribute("dataSource");
      if ( cParent == oTD && e.style.display == '' ) 
        return true;
      if ( oTD.id == '' || oTD.id == null ) {
        oTD.id = new Date().getTime();
      }
      e.setAttribute("dataSource", oTD.id);
      var val = get_linked_value(oTD);
      e.value = val;
      e.defaultValue = val;
      
      //oTD.appendChild(oEd[0]);
      var pos = findXY(oTD, e);
      //window.status = "("+pos.left+","+pos.top+")";
      e.style.left = pos.left+1;
      e.style.top = pos.top+1;
      e.style.width = oTD.clientWidth;
      //oEd[0].style.pixelHeight = hh;
      //e.style.position = "absolute";
      e.style.display = '';
      editControl = e;
      try {
        e.focus();
      } catch ( ex ) { 
        window.status = ex.description;
        alert( e.tagName +"," + e.type );
      }
      return true;
    }
  //}
  return false;
}

function mark_delete(obj)
{
  var oTR = findParent(obj, "TR", 0);
  var img = oTR.getElementsByTagName("IMG")[0];
  //window.status = img.src;
  var oid = oTR.id;
  if ( obj.checked ) {
    removeObject(oid);
    if ( obj.value != 0 ) {
      addObject("-"+oid);
      img.src = img.src.replace(/\/[^\/]*\.gif$/,"/delete.gif");
    }
  } else {
    if ( obj.value != 0 )
      removeObject("-"+oid);
    if ( oTR.getAttribute("isUpdated") != null ) {
      addObject(oid);
      img.src = img.src.replace(/\/[^\/]*\.gif$/,"/but_modify.gif");
    } else {
      img.src = img.src.replace(/\/[^\/]*\.gif$/,"/but_detail.gif");
    }
  }
}

function add_new(url, title)
{
  if ( p == window )
    return open_input_form(url, title);
  
  if ( editFrameLoaded ) {
    add_new_inline();
  } else {
    loadEditFrame(url, add_new_inline);
  }
}

function loadEditFrame(url, onDone)
{
  var editFrame = document.getElementById("editFrame");
  if ( editFrame != null ) {
    editFrame.src = url+"&add_new_in_parent=true";
    afterEditFrameLoaded = onDone;
  }
}

function add_new_row(id)
{
  var listTable = document.getElementById("listTable");
  if ( listTable ) {
    var cTR = document.getElementById("editClone");
    var oTR = cTR.cloneNode(true);
    cTR.parentNode.appendChild( cTR.cloneNode(true) );
    oTR = listTable.rows[listTable.rows.length-1];
    if ( id != null ) 
      oTR.id = id;
    if ( oTR.cells.length == 1 ) {
      var oTD = oTR.cells[0];
      var i=0;
      var oDIV = oTD.firstChild;
      while ( oDIV != null ) {
        if ( oDIV.tagName == 'DIV' || oDIV.tagName == 'SPAN' ) {
          if ( i > 1 ) {
            oDIV.ondblclick = new Function("return edit(this)");
          }
          i++;
        }
        oDIV = oDIV.nextSibling;
      }
    } else {
      //oTR.ondblclick = new Function("edit(this)");
      for(var i=2; i<oTR.cells.length; i++) {
        var oTD = oTR.cells[i];
        oTD.ondblclick = new Function("return edit(this)");
      }
    }
    oTR.style.display = '';
    return oTR;
  }
  return null;
}

function add_new_inline()
{
  var oTR = add_new_row();
  if ( oTR != null ) {
    oTR.id = addObject();
    if ( oTR.cells.length == 1 ) {
      var oTD = oTR.cells[0];
      var i=0;
      var oDIV = oTD.firstChild;
      while ( oDIV != null ) {
        if ( oDIV.tagName == 'DIV' || oDIV.tagName == 'SPAN' ) {
          if ( i > 1 ) {
            if ( edit(oDIV) ) 
              return;
          }
          i++;
        }
        oDIV = oDIV.nextSibling;
      }
    } else {
      for(var i=2; i<oTR.cells.length; i++) {
        var oTD = oTR.cells[i];
        if ( edit(oTD) )
          return;
      }
    }
  }
}

var editFrameLoaded = false;
var afterEditFrameLoaded = null;
function onEditFrameLoaded()
{
  //alert( "loaded" );
  editFrameLoaded = true;
  var editFrame = document.frames ? document.frames("editFrame") : document.getElementById("editFrame");
  var doc = editFrame.document ? editFrame.document : editFrame.contentDocument;

  var oTR = document.getElementById("editClone");
  if ( oTR != null ) {
    for(var i=0; i<oTR.cells.length; i++) {
      var oTD = oTR.cells[i];
      var ename = oTD.getAttribute("tag");
      if ( ename != null ) {
        cloneInputControl(doc, ename);
        continue;
      }
      var oDIV = oTD.firstChild;
      while ( oDIV != null ) {
        if ( oDIV.tagName == 'DIV' || oDIV.tagName == 'SPAN' ) {
          ename = oDIV.getAttribute("tag");
          if ( ename != null ) {
            cloneInputControl(doc, ename);
          }
        }
        oDIV = oDIV.nextSibling;
      }
    }
  }
  if ( "function" == typeof(afterEditFrameLoaded) ) {
    afterEditFrameLoaded();
  }
  afterEditFrameLoaded = null;
}

function cloneInputControl(doc, ename)
{
  var inps = doc.getElementsByName(ename);
  if ( inps != null && inps.length > 0 ) {
    var inp = inps[0];
    if ( !inp.readOnly && !inp.disabled && inp.parentNode.tagName == 'TD' ) {
      var nodeDIV = document.getElementById("nodeDiv");
      nodeDIV.innerHTML = inp.parentNode.innerHTML;
      var listDIV = document.getElementById("listDIV");
      var e = nodeDIV.firstChild;
      while ( e != null ) {
        var n = e.nextSibling;
        try {
          e.style.display = 'none';
          e.style.position = 'absolute';
          if ( (e.tagName == "INPUT" && e.type != 'hidden') || e.tagName == 'SELECT' || e.tagName == 'TEXTAREA' ) {
            if ( e.attachEvent ) {
              e.attachEvent("onchange", do_update);
            } else if ( e.addEventListener ) {
              e.addEventListener("change", do_update, true);
            }
            if ( e.attachEvent ) {
              e.attachEvent("onblur", reset_input_object);
            } else if ( e.addEventListener ) {
              e.addEventListener("blur", reset_input_object, true);
            }
          }
          listDIV.appendChild(e);
        } catch ( ex ) { 
        }
        e = n;
      }
    }
  }
}

function selectEnterSubmit(evt)
{
  if ( evt == null ) evt = window.event ? window.event : null;
  if ( evt != null ) {
    if ( evt.keyCode == 13 ) {
      var srcE = evt.srcElement ? evt.srcElement : evt.target;
      if ( srcE && srcE.form ) {
        srcE.form.submit();
      }
    }
  }
}

function findXY(obj, me)
{
  var pos = new Object();
  pos.left = 0;
  pos.top = 0;
  var p = obj;
  while( p != null ) {
    if ( me != null && p == me.offsetParent ) {
      return pos;
    }
    pos.left += p.offsetLeft;
    pos.top += p.offsetTop;
    p = p.offsetParent;
  }
  if ( me != null ) {
    p = me.offsetParent;
    while ( p != null ) {
      pos.left -= p.offsetLeft;
      pos.top -= p.offsetTop;
      p = p.offsetParent;
    }
  }
  return pos;
}

function close_frame(evt)
{
  //alert(this);
  if ( evt == null ) evt = window.event ? window.event : null;
  if ( evt != null ) {
    var srcE = evt.srcElement ? evt.srcElement : evt.target;
    srcE.style.display = 'none';
    //alert( evt.toElement );
    //var iframe = srcE.getAttribute("iframe");
    //if ( iframe != null ) {
    //  iframe.style.display = 'none';
    //}
  }
}

function open_select_frame(url, title, element)
{
  var disp = document.getElementsByName(element.name + "_DISP");
  var iframe = disp[0].getAttribute("iframe");
  if ( iframe == null ) {
    var iframe = document.createElement("IFRAME");
    iframe.id = new Date().getTime();
    iframe.tabIndex = -1;
    iframe.style.position = "absolute";
    iframe.style.border = "1px solid gray";
    iframe.frameBorder = 0;
    iframe.src = url;
    //document.body.appendChild(iframe);
    disp[0].parentNode.insertBefore(iframe, disp[0]);
    disp[0].setAttribute("iframe", iframe.id);
    if ( iframe.attachEvent ) 
      iframe.attachEvent("onblur", close_frame);
  } else {
    iframe = document.getElementById(iframe);
    if ( iframe.style.display == '' ) {
      iframe.style.display = 'none';
      return;
    } else {
      iframe.style.display = '';
    }
  }
  var pos = findXY(disp[0], iframe);
  iframe.style.left = pos.left;
  iframe.style.top = pos.top + disp[0].offsetHeight + 1;
  iframe.width = document.body.clientWidth - pos.left - 5;
  iframe.height = 200;
  iframe.focus();
}

function clear_select(element)
{
  var disp = document.getElementsByName(element.name + "_DISP");
  element.value = '';
  disp[0].value = '';
  var iframe = disp[0].getAttribute("iframe");
  if ( iframe != null ) {
    iframe = document.getElementById(iframe);
    var ifWin = iframe.contentWindow ? iframe.contentWindow : document.frames(iframe.id);;
    if ( ifWin != null ) {
      ifWin.doSelect(null,'');
    } else {
      ifWin.src = 'about:blank';
    }
  }
}

function select_help(evt)
{
  if ( evt == null ) evt = window.event ? window.event : null;
  if ( evt != null ) {
    var srcE = evt.srcElement ? evt.srcElement : evt.target;
    var str = srcE.getAttribute("helpString");
    if ( str == null ) {
      addEvent(srcE, "keypress", select_help);
      addEvent(srcE, "keydown", select_keydown);
      addEvent(srcE, "change", select_changed);
      addEvent(srcE, "blur", close_help);
      str = "";
    }
    
    var ch = evt.keyCode;
    switch( ch ) {
    case 27: // ESC
      str = "";
      break;
    default:
      str += String.fromCharCode(ch).toLowerCase();
    }
    select_suggest(str, srcE, null, ch);
  }
  return false;
}

function select_suggest(str, srcE)
{
  srcE.setAttribute("helpString", str);
  //window.status = str;
  if ( str.length > 0 ) {
    for(var i=0; i<srcE.options.length; i++) {
      var txt = srcE.options[i].text.toLowerCase();
      if ( txt.length >= str.length ) {
        if ( txt.substring(0, str.length) == str ) {
          srcE.options[i].selected = true;
          break;
        }
      }
    }
  }
}

function select_changed(evt)
{
  if ( evt == null ) evt = window.event ? window.event : null;
  if ( evt != null ) {
    var srcE = evt.srcElement ? evt.srcElement : evt.target;
    srcE.setAttribute("helpString","");
  }
}

function select_keydown(evt)
{
  if ( evt == null ) evt = window.event ? window.event : null;
  if ( evt != null ) {
    var srcE = evt.srcElement ? evt.srcElement : evt.target;
    var str = srcE.getAttribute("helpString");
    if ( str == null ) {
      str = "";
    }        
    var ch = evt.keyCode;
    if ( ch == 8 ) {
      if ( str.length > 0 ) {
        str = str.substring(0, str.length-1);
        select_suggest(str, srcE);
      }
      stopEvent(evt);
      return false;
    }
  }
  return true;
}

function close_help(evt)
{
  if ( evt == null ) evt = window.event ? window.event : null;
  if ( evt != null ) {
    var srcE = evt.srcElement ? evt.srcElement : evt.target;
    if ( srcE != null ) {
      srcE.removeAttribute("helpString");
      removeEvent(srcE, "keypress", select_help);
      removeEvent(srcE, "keydown", select_keydown);
      removeEvent(srcE, "change", select_changed);
      removeEvent(srcE, "blur", close_help);
    }
  }
}

function divScroll(div)
{
    //window.status = div.scrollLeft + "," + div.scrollTop;
    var listTable = document.getElementById("listTable");
    if ( listTable != null ) {
      var oTR = listTable.rows[0];
      if ( oTR != null ) {
        oTR.style.top = div.scrollTop;
        oTR.style.zIndex = 999;
      }
    }
}

function getTab(name)
{
  var tabspan = document.getElementsByTagName("SPAN");
  for(var i=0; i<tabspan.length; i++) {
    var tab = tabspan[i];
    var tabName = tab.getAttribute("tabName");
    if ( tabName ) {
      if ( tabName == name ) {
        return tab;
      }
    }
  }
  return null;
}

function openTab(name, id, url)
{
  var tabName = "__"+name+"_"+id;
  var tab = getTab(tabName);
  if ( tab == null ) {
    var tabTD = document.getElementById("tabTD");
    var fSPAN = findNext(tabTD.firstChild, "SPAN", 0);
    var cSPAN = fSPAN.cloneNode(true);
    cSPAN.innerHTML = name + "[" + id + "]<img src=\"../../img/common/closebox.gif\" onclick=\"closeTab('"+tabName+"')\">";
    cSPAN.setAttribute("tabName", tabName);
    cSPAN.onclick = new Function("activeTab('"+tabName+"')");
    tabTD.appendChild( cSPAN );
    
    var oDivContent = document.getElementById("divContent");
    var oTable = document.createElement("TABLE");
    oTable.height = "100%";
    oTable.style.display = "none";
    oTable.setAttribute("tabName",tabName);
    oTable.className="TBM2";
    oTable.border="0";
    oTable.cellpadding="5";
    oTable.cellspacing="2";
    oTable.width="100%";
    var oTR = oTable.insertRow(-1);
    var oTD = oTR.insertCell(-1);
    oTD.colspan="2";
    oTD.className="TBM2-TD1-1";
    oTD.width="100%";
    oTD.height="100%";
    oTR.appendChild(oTD);
    
    var iframe = oTD.appendChild(document.createElement("IFRAME"));
    iframe.setAttribute("srcToLoad", url);
    iframe.frameborder="0";
    iframe.width="100%";
    iframe.src="about:blank";
    iframe.height="100%";

    oDivContent.appendChild(oTable);
  }
  activeTab(tabName);
}

function closeTab(name)
{
  var tabspan = getTab(name);
  if ( tabspan != null ) {
    tabspan.parentNode.removeChild(tabspan);
  }
  var tabcol = document.getElementsByTagName("TABLE");
  for(var i=0; i<tabcol.length; i++) {
    var tab = tabcol[i];
    var tabName = tab.getAttribute("tabName");
    if ( tabName ) {
      if ( tabName == name ) {
        if ( tab.style.display == '' ) 
          activeTab(thisForm.name);
        tab.parentNode.removeChild(tab);
        break;
      }
    }
  }
}

function activeTab(name)
{
  var tabspan = document.getElementsByTagName("SPAN");
  for(var i=0; i<tabspan.length; i++) {
    var tab = tabspan[i];
    var tabName = tab.getAttribute("tabName");
    if ( tabName ) {
      if ( tabName == name ) {
        tab.style.backgroundColor = "#7755AA";
      //} else if ( tabName.match(/^__/) ) { 
      //  tab.parentNode.removeChild(tab);
      } else {
        tab.style.backgroundColor = "";
      }
    }
  }

  var tabcol = document.getElementsByTagName("TABLE");
  for(var i=0; i<tabcol.length; i++) {
    var tab = tabcol[i];
    var tabName = tab.getAttribute("tabName");
    if ( tabName ) {
      if ( tabName == name ) {
        tab.style.display = '';
        var iframes = tab.getElementsByTagName("IFRAME");
        for(var j=0; j<iframes.length; j++) {
          var iframe = iframes[j];
          if ( iframe.src == 'about:blank' ) {
            var srcToLoad = iframe.getAttribute("srcToLoad");
            if ( srcToLoad ) {
              iframe.src = srcToLoad;
            }
          }
        }
      //} else if ( tabName.match(/^__/) ) { 
      //  tab.parentNode.removeChild(tab);
      } else {
        tab.style.display = 'none';
      }
    }
  }
}

function addElement(e, name)
{
  var els = document.getElementsByName(name);
  if ( els != null && els.length > 0 ) {
    return els[0].parentNode.insertBefore(e, els[0]);
  } else {
    return thisForm.insertBefore(e, thisForm.firstChild);
  }
}

function findStyleRule(name)
{
  var ss = document.styleSheets;
  for(var i=0; i<ss.length; i++) {
    var rules = ss[i].cssRules ? ss[i].cssRules : ss[i].rules;
    for(var j=0; j<rules.length; j++) {
      if ( rules[j].selectorText.toLowerCase()  == name ) {
        return rules[j];
      }
    }
  }
  return null;
}

function findPreviousTag(obj, name)
{
  var p = obj;
  while( p != null ) {
    p = p.previousSibling;
    if ( p.tagName == name )
      break;
  }
  return p;
}

var rulesColumn = null;
var oldX;
var divToSize = null;
var resizeBar = null;
var sWidth;
function start_resize(evt)
{
  if(!evt) evt=window.event;
  if(!evt.pageX) evt.pageX=evt.clientX;
  if(!evt.pageY) evt.pageY=evt.clientY;
  resizeBar = evt.srcElement ? evt.srcElement : evt.target;
  divToSize = findPreviousTag(resizeBar, "SPAN");
  rulesColumn = findStyleRule(".column"+resizeBar.getAttribute("target"));
  if ( rulesColumn != null && divToSize != null ) {
    oldX = evt.pageX;
    sWidth = parseInt(rulesColumn.style.width);
    addEvent(resizeBar, "mousemove", start_move);
    addEvent(resizeBar, "mouseup", stop_resize);
    if ( resizeBar.setCapture ) 
      resizeBar.setCapture();
    else if(window.captureEvents)
			window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
  }
}

function start_move(evt)
{
  if(!evt) evt=window.event;
  if(!evt.pageX) evt.pageX=evt.clientX;
  if(!evt.pageY) evt.pageY=evt.clientY;
  if ( rulesColumn != null && divToSize != null ) {
    var dx = evt.pageX - oldX;
    oldX = evt.pageX;
    sWidth += dx;
    if ( sWidth < 10 ) sWidth = 10;
    divToSize.style.width = sWidth + "px";
  }
}

function stop_resize(evt)
{
  if ( resizeBar != null ) {
    removeEvent(resizeBar, "mousemove", start_move);
    removeEvent(resizeBar, "mouseup", stop_resize);
    if ( resizeBar.releaseCapture )
      resizeBar.releaseCapture();
    else if(window.captureEvents)
      window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
    resizeBar == null;
  }
  if ( rulesColumn != null ) {
    rulesColumn.style.width = sWidth + "px";
    rulesColumn = null;
    columnWidth[resizeBar.getAttribute("target")-3] = sWidth;
    var path = document.location.href.substring(7);
    path = path.substring(path.indexOf("/"));
    if ( path.indexOf("?") > 0 ) {
      path = path.substring(0, path.indexOf("?")-1);
    }
    if ( path.lastIndexOf("/") > 0 ) {
      path = path.substring(0,path.lastIndexOf("/"));
    }
    SetCookie(thisForm.name+"ColumnWidth", columnWidth.join(","), new Date(2199,11,31), path);
  }
}

function updateOwner()
{
  if ( opener ) {
    opener.thisForm.submit();
  } else if ( p != window ) {
    var iframe = p.document.getElementById("if_"+o);
    if ( iframe != null ) {
      var win = iframe.contentWindow ? iframe.contentWindow : document.frames(iframe.id);
      win.thisForm.submit();
    } else {
      p.thisForm.submit();
    }
  }
}